Prepare for adding attachments

pull/30/head
M66B 6 years ago
parent 53d77fe205
commit b48d54f98f

@ -28,10 +28,12 @@ import androidx.fragment.app.FragmentTransaction;
public class ActivityCompose extends ActivityBase implements FragmentManager.OnBackStackChangedListener { public class ActivityCompose extends ActivityBase implements FragmentManager.OnBackStackChangedListener {
static final int LOADER_COMPOSE_GET = 1; static final int LOADER_COMPOSE_GET = 1;
static final int LOADER_COMPOSE_PUT = 2; static final int LOADER_COMPOSE_PUT = 2;
static final int LOADER_COMPOSE_ATTACHMENT = 3;
static final int REQUEST_CONTACT_TO = 1; static final int REQUEST_CONTACT_TO = 1;
static final int REQUEST_CONTACT_CC = 2; static final int REQUEST_CONTACT_CC = 2;
static final int REQUEST_CONTACT_BCC = 3; static final int REQUEST_CONTACT_BCC = 3;
static final int REQUEST_ATTACHMENT = 4;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {

@ -24,9 +24,11 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.database.Cursor; import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.provider.ContactsContract; import android.provider.ContactsContract;
import android.provider.OpenableColumns;
import android.text.Html; import android.text.Html;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
@ -36,6 +38,7 @@ import android.view.MenuInflater;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.webkit.MimeTypeMap;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView; import android.widget.AutoCompleteTextView;
import android.widget.EditText; import android.widget.EditText;
@ -47,6 +50,9 @@ import android.widget.Toast;
import com.google.android.material.bottomnavigation.BottomNavigationView; import com.google.android.material.bottomnavigation.BottomNavigationView;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
@ -67,6 +73,7 @@ import androidx.lifecycle.Observer;
import androidx.loader.app.LoaderManager; import androidx.loader.app.LoaderManager;
import androidx.loader.content.AsyncTaskLoader; import androidx.loader.content.AsyncTaskLoader;
import androidx.loader.content.Loader; import androidx.loader.content.Loader;
import androidx.recyclerview.widget.RecyclerView;
import static android.app.Activity.RESULT_OK; import static android.app.Activity.RESULT_OK;
@ -84,10 +91,12 @@ public class FragmentCompose extends FragmentEx {
private AutoCompleteTextView etBcc; private AutoCompleteTextView etBcc;
private ImageView ivBccAdd; private ImageView ivBccAdd;
private EditText etSubject; private EditText etSubject;
private RecyclerView rvAttachment;
private EditText etBody; private EditText etBody;
private BottomNavigationView bottom_navigation; private BottomNavigationView bottom_navigation;
private ProgressBar pbWait; private ProgressBar pbWait;
private Group grpCc; private Group grpAddresses;
private Group grpAttachments;
private Group grpReady; private Group grpReady;
@Override @Override
@ -112,10 +121,12 @@ public class FragmentCompose extends FragmentEx {
etBcc = view.findViewById(R.id.etBcc); etBcc = view.findViewById(R.id.etBcc);
ivBccAdd = view.findViewById(R.id.ivBccAdd); ivBccAdd = view.findViewById(R.id.ivBccAdd);
etSubject = view.findViewById(R.id.etSubject); etSubject = view.findViewById(R.id.etSubject);
rvAttachment = view.findViewById(R.id.rvAttachment);
etBody = view.findViewById(R.id.etBody); etBody = view.findViewById(R.id.etBody);
bottom_navigation = view.findViewById(R.id.bottom_navigation); bottom_navigation = view.findViewById(R.id.bottom_navigation);
pbWait = view.findViewById(R.id.pbWait); pbWait = view.findViewById(R.id.pbWait);
grpCc = view.findViewById(R.id.grpCc); grpAddresses = view.findViewById(R.id.grpAddresses);
grpAttachments = view.findViewById(R.id.grpAttachments);
grpReady = view.findViewById(R.id.grpReady); grpReady = view.findViewById(R.id.grpReady);
// Wire controls // Wire controls
@ -183,7 +194,8 @@ public class FragmentCompose extends FragmentEx {
// Initialize // Initialize
spFrom.setVisibility(View.GONE); spFrom.setVisibility(View.GONE);
ivIdentityAdd.setVisibility(View.GONE); ivIdentityAdd.setVisibility(View.GONE);
grpCc.setVisibility(View.GONE); grpAddresses.setVisibility(View.GONE);
grpAttachments.setVisibility(View.GONE);
grpReady.setVisibility(View.GONE); grpReady.setVisibility(View.GONE);
pbWait.setVisibility(View.VISIBLE); pbWait.setVisibility(View.VISIBLE);
bottom_navigation.getMenu().setGroupEnabled(0, false); bottom_navigation.getMenu().setGroupEnabled(0, false);
@ -276,69 +288,135 @@ public class FragmentCompose extends FragmentEx {
@Override @Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_message, menu); inflater.inflate(R.menu.menu_compose, menu);
super.onCreateOptionsMenu(menu, inflater); super.onCreateOptionsMenu(menu, inflater);
} }
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) { switch (item.getItemId()) {
case R.id.menu_address: case R.id.menu_attachment:
onMenuCc(); onMenuAttachment();
return true;
case R.id.menu_addresses:
onMenuAddresses();
return true; return true;
default: default:
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
} }
} }
private void onMenuCc() { private void onMenuAttachment() {
grpCc.setVisibility(grpCc.getVisibility() == View.GONE ? View.VISIBLE : View.GONE); Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
startActivityForResult(intent, ActivityCompose.REQUEST_ATTACHMENT);
}
private void onMenuAddresses() {
grpAddresses.setVisibility(grpAddresses.getVisibility() == View.GONE ? View.VISIBLE : View.GONE);
} }
@Override @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) { public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) { if (resultCode == RESULT_OK) {
Cursor cursor = null; if (requestCode == ActivityCompose.REQUEST_ATTACHMENT) {
try { if (data != null) {
cursor = getContext().getContentResolver().query(data.getData(), Bundle args = new Bundle();
new String[]{ args.putParcelable("uri", data.getData());
ContactsContract.CommonDataKinds.Email.ADDRESS, new SimpleLoader() {
ContactsContract.Contacts.DISPLAY_NAME @Override
}, public Object onLoad(Bundle args) throws IOException {
null, null, null); Uri uri = args.getParcelable("uri");
if (cursor != null && cursor.moveToFirst()) { Cursor cursor = null;
int colEmail = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS); try {
int colName = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME); cursor = getActivity().getContentResolver().query(uri, null, null, null, null, null);
String email = cursor.getString(colEmail); if (cursor != null && cursor.moveToFirst()) {
String name = cursor.getString(colName); EntityAttachment attachment = new EntityAttachment();
attachment.name = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
String text = null;
if (requestCode == ActivityCompose.REQUEST_CONTACT_TO) String extension = MimeTypeMap.getFileExtensionFromUrl(attachment.name.toLowerCase());
text = etTo.getText().toString(); if (extension != null)
else if (requestCode == ActivityCompose.REQUEST_CONTACT_CC) attachment.type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
text = etCc.getText().toString(); if (extension == null)
else if (requestCode == ActivityCompose.REQUEST_CONTACT_BCC) attachment.type = "application/octet-stream";
text = etBcc.getText().toString();
InputStream is = null;
InternetAddress address = new InternetAddress(email, name); try {
StringBuilder sb = new StringBuilder(text); is = getContext().getContentResolver().openInputStream(uri);
if (sb.length() > 0) ByteArrayOutputStream bos = new ByteArrayOutputStream();
sb.append("; ");
sb.append(address.toString()); int len;
byte[] buffer = new byte[8192];
if (requestCode == ActivityCompose.REQUEST_CONTACT_TO) while ((len = is.read(buffer)) > 0)
etTo.setText(sb.toString()); bos.write(buffer, 0, len);
else if (requestCode == ActivityCompose.REQUEST_CONTACT_CC)
etCc.setText(sb.toString()); attachment.size = bos.size();
else if (requestCode == ActivityCompose.REQUEST_CONTACT_BCC) attachment.content = bos.toByteArray();
etBcc.setText(sb.toString()); } finally {
if (is != null)
is.close();
}
return attachment;
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
@Override
public void onLoaded(Bundle args, Result result) {
EntityAttachment attachment = (EntityAttachment) result.data;
}
}.load(this, ActivityCompose.LOADER_COMPOSE_ATTACHMENT, args);
}
} else {
Cursor cursor = null;
try {
cursor = getContext().getContentResolver().query(data.getData(),
new String[]{
ContactsContract.CommonDataKinds.Email.ADDRESS,
ContactsContract.Contacts.DISPLAY_NAME
},
null, null, null);
if (cursor != null && cursor.moveToFirst()) {
int colEmail = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS);
int colName = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
String email = cursor.getString(colEmail);
String name = cursor.getString(colName);
String text = null;
if (requestCode == ActivityCompose.REQUEST_CONTACT_TO)
text = etTo.getText().toString();
else if (requestCode == ActivityCompose.REQUEST_CONTACT_CC)
text = etCc.getText().toString();
else if (requestCode == ActivityCompose.REQUEST_CONTACT_BCC)
text = etBcc.getText().toString();
InternetAddress address = new InternetAddress(email, name);
StringBuilder sb = new StringBuilder(text);
if (sb.length() > 0)
sb.append("; ");
sb.append(address.toString());
if (requestCode == ActivityCompose.REQUEST_CONTACT_TO)
etTo.setText(sb.toString());
else if (requestCode == ActivityCompose.REQUEST_CONTACT_CC)
etCc.setText(sb.toString());
else if (requestCode == ActivityCompose.REQUEST_CONTACT_BCC)
etBcc.setText(sb.toString());
}
} catch (Throwable ex) {
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
Toast.makeText(getContext(), ex.getMessage(), Toast.LENGTH_LONG).show();
} finally {
if (cursor != null)
cursor.close();
} }
} catch (Throwable ex) {
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
Toast.makeText(getContext(), ex.getMessage(), Toast.LENGTH_LONG).show();
} finally {
if (cursor != null)
cursor.close();
} }
} }
} }
@ -465,7 +543,7 @@ public class FragmentCompose extends FragmentEx {
String action = result.getString("action"); String action = result.getString("action");
pbWait.setVisibility(View.GONE); pbWait.setVisibility(View.GONE);
grpCc.setVisibility("reply_all".equals(action) ? View.VISIBLE : View.GONE); grpAddresses.setVisibility("reply_all".equals(action) ? View.VISIBLE : View.GONE);
grpReady.setVisibility(View.VISIBLE); grpReady.setVisibility(View.VISIBLE);
FragmentCompose.this.thread = thread; FragmentCompose.this.thread = thread;

@ -82,7 +82,7 @@ public class FragmentMessage extends FragmentEx {
private TextView tvBody; private TextView tvBody;
private BottomNavigationView bottom_navigation; private BottomNavigationView bottom_navigation;
private ProgressBar pbWait; private ProgressBar pbWait;
private Group grpAddress; private Group grpAddresses;
private Group grpAttachments; private Group grpAttachments;
private Group grpReady; private Group grpReady;
@ -114,7 +114,7 @@ public class FragmentMessage extends FragmentEx {
tvBody = view.findViewById(R.id.tvBody); tvBody = view.findViewById(R.id.tvBody);
bottom_navigation = view.findViewById(R.id.bottom_navigation); bottom_navigation = view.findViewById(R.id.bottom_navigation);
pbWait = view.findViewById(R.id.pbWait); pbWait = view.findViewById(R.id.pbWait);
grpAddress = view.findViewById(R.id.grpAddress); grpAddresses = view.findViewById(R.id.grpAddresses);
grpAttachments = view.findViewById(R.id.grpAttachments); grpAttachments = view.findViewById(R.id.grpAttachments);
grpReady = view.findViewById(R.id.grpReady); grpReady = view.findViewById(R.id.grpReady);
@ -216,7 +216,7 @@ public class FragmentMessage extends FragmentEx {
}); });
// Initialize // Initialize
grpAddress.setVisibility(View.GONE); grpAddresses.setVisibility(View.GONE);
grpAttachments.setVisibility(View.GONE); grpAttachments.setVisibility(View.GONE);
top_navigation.setVisibility(View.GONE); top_navigation.setVisibility(View.GONE);
bottom_navigation.setVisibility(View.GONE); bottom_navigation.setVisibility(View.GONE);
@ -350,24 +350,24 @@ public class FragmentMessage extends FragmentEx {
@Override @Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_message, menu); inflater.inflate(R.menu.menu_view, menu);
super.onCreateOptionsMenu(menu, inflater); super.onCreateOptionsMenu(menu, inflater);
} }
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) { switch (item.getItemId()) {
case R.id.menu_address: case R.id.menu_addresses:
onMenuAddress(); onMenuAddresses();
return true; return true;
default: default:
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
} }
} }
private void onMenuAddress() { private void onMenuAddresses() {
if (grpReady.getVisibility() == View.VISIBLE) if (grpReady.getVisibility() == View.VISIBLE)
grpAddress.setVisibility(grpAddress.getVisibility() == View.GONE ? View.VISIBLE : View.GONE); grpAddresses.setVisibility(grpAddresses.getVisibility() == View.GONE ? View.VISIBLE : View.GONE);
} }
private void onActionThread(long id) { private void onActionThread(long id) {

@ -113,6 +113,30 @@
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/etBcc" /> app:layout_constraintTop_toBottomOf="@+id/etBcc" />
<View
android:id="@+id/vSeparatorAttachments"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="3dp"
android:background="?attr/colorSeparator"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/etSubject" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvAttachment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="6dp"
android:layout_marginStart="6dp"
android:layout_marginTop="3dp"
android:scrollbarStyle="outsideOverlay"
android:scrollbars="vertical"
app:layout_constrainedHeight="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_max="150dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/vSeparatorAttachments" />
<View <View
android:id="@+id/vSeparator" android:id="@+id/vSeparator"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -120,7 +144,7 @@
android:layout_marginTop="6dp" android:layout_marginTop="6dp"
android:background="?attr/colorSeparator" android:background="?attr/colorSeparator"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/etSubject" /> app:layout_constraintTop_toBottomOf="@+id/rvAttachment" />
<ScrollView <ScrollView
android:id="@+id/scroll" android:id="@+id/scroll"
@ -171,11 +195,17 @@
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.Group <androidx.constraintlayout.widget.Group
android:id="@+id/grpCc" android:id="@+id/grpAddresses"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="0dp" android:layout_height="0dp"
app:constraint_referenced_ids="etCc,ivCcAdd,etBcc,ivBccAdd" /> app:constraint_referenced_ids="etCc,ivCcAdd,etBcc,ivBccAdd" />
<androidx.constraintlayout.widget.Group
android:id="@+id/grpAttachments"
android:layout_width="0dp"
android:layout_height="0dp"
app:constraint_referenced_ids="vSeparatorAttachments,rvAttachment" />
<androidx.constraintlayout.widget.Group <androidx.constraintlayout.widget.Group
android:id="@+id/grpReady" android:id="@+id/grpReady"
android:layout_width="0dp" android:layout_width="0dp"

@ -223,7 +223,7 @@
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.Group <androidx.constraintlayout.widget.Group
android:id="@+id/grpAddress" android:id="@+id/grpAddresses"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="0dp" android:layout_height="0dp"
app:constraint_referenced_ids="vSeparatorAddress,tvToTitle,tvTo,tvCcTitle,tvCc,tvBccTitle,tvBcc" /> app:constraint_referenced_ids="vSeparatorAddress,tvToTitle,tvTo,tvCcTitle,tvCc,tvBccTitle,tvBcc" />

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_attachment"
android:icon="@drawable/baseline_attachment_24"
android:title="@string/title_add_attachment"
app:showAsAction="always" />
<item
android:id="@+id/menu_addresses"
android:icon="@drawable/baseline_people_24"
android:title="@string/title_show_addresses"
app:showAsAction="always" />
</menu>

@ -3,8 +3,8 @@
xmlns:app="http://schemas.android.com/apk/res-auto"> xmlns:app="http://schemas.android.com/apk/res-auto">
<item <item
android:id="@+id/menu_address" android:id="@+id/menu_addresses"
android:icon="@drawable/baseline_people_24" android:icon="@drawable/baseline_people_24"
android:title="@string/title_cc" android:title="@string/title_show_addresses"
app:showAsAction="always" /> app:showAsAction="always" />
</menu> </menu>

@ -136,6 +136,9 @@
<string name="title_save">Save</string> <string name="title_save">Save</string>
<string name="title_send">Send</string> <string name="title_send">Send</string>
<string name="title_show_addresses">Show CC/BCC</string>
<string name="title_add_attachment">Add attachment</string>
<string name="title_from_missing">Sender missing</string> <string name="title_from_missing">Sender missing</string>
<string name="title_to_missing">Recipient missing</string> <string name="title_to_missing">Recipient missing</string>
<string name="title_draft_trashed">Draft trashed</string> <string name="title_draft_trashed">Draft trashed</string>

Loading…
Cancel
Save