Merge branch 'master' into patch-1

pull/31/head
StoyanDimitrov 7 years ago committed by GitHub
commit 7a990fc5bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -45,12 +45,8 @@ public class ActivityCompose extends ActivityBase implements FragmentManager.OnB
getSupportFragmentManager().addOnBackStackChangedListener(this); getSupportFragmentManager().addOnBackStackChangedListener(this);
if (getSupportFragmentManager().getFragments().size() == 0) { if (getSupportFragmentManager().getFragments().size() == 0) {
Bundle args = getIntent().getExtras();
if (args == null)
args = new Bundle();
FragmentCompose fragment = new FragmentCompose(); FragmentCompose fragment = new FragmentCompose();
fragment.setArguments(args); fragment.setArguments(getIntent().getExtras());
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, fragment).addToBackStack("compose"); fragmentTransaction.replace(R.id.content_frame, fragment).addToBackStack("compose");

@ -64,15 +64,16 @@ public class ActivityView extends ActivityBase implements FragmentManager.OnBack
static final int LOADER_ACCOUNT_PUT = 2; static final int LOADER_ACCOUNT_PUT = 2;
static final int LOADER_IDENTITY_PUT = 3; static final int LOADER_IDENTITY_PUT = 3;
static final int LOADER_FOLDER_PUT = 4; static final int LOADER_FOLDER_PUT = 4;
static final int LOADER_MESSAGE_VIEW = 5; static final int LOADER_MESSAGE_ACCOUNT = 5;
static final int LOADER_MESSAGE_SEEN = 6; static final int LOADER_MESSAGE_VIEW = 6;
static final int LOADER_MESSAGE_EDIT = 7; static final int LOADER_MESSAGE_SEEN = 7;
static final int LOADER_MESSAGE_SPAM = 8; static final int LOADER_MESSAGE_EDIT = 8;
static final int LOADER_MESSAGE_TRASH = 9; static final int LOADER_MESSAGE_SPAM = 9;
static final int LOADER_MESSAGE_MOVE = 10; static final int LOADER_MESSAGE_TRASH = 10;
static final int LOADER_MESSAGE_ARCHIVE = 11; static final int LOADER_MESSAGE_MOVE = 11;
static final int LOADER_SEEN_UNTIL = 12; static final int LOADER_MESSAGE_ARCHIVE = 12;
static final int LOADER_DEBUG_INFO = 13; static final int LOADER_SEEN_UNTIL = 13;
static final int LOADER_DEBUG_INFO = 14;
static final int REQUEST_VIEW = 1; static final int REQUEST_VIEW = 1;
static final int REQUEST_UNSEEN = 2; static final int REQUEST_UNSEEN = 2;

@ -45,7 +45,7 @@ public class AdapterMessage extends PagedListAdapter<TupleMessageEx, AdapterMess
enum ViewType {FOLDER, THREAD} enum ViewType {FOLDER, THREAD}
public class ViewHolder extends RecyclerView.ViewHolder public class ViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener { implements View.OnClickListener, View.OnLongClickListener {
View itemView; View itemView;
TextView tvFrom; TextView tvFrom;
TextView tvTime; TextView tvTime;
@ -68,10 +68,12 @@ public class AdapterMessage extends PagedListAdapter<TupleMessageEx, AdapterMess
private void wire() { private void wire() {
itemView.setOnClickListener(this); itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);
} }
private void unwire() { private void unwire() {
itemView.setOnClickListener(null); itemView.setOnClickListener(null);
itemView.setOnLongClickListener(null);
} }
private void clear() { private void clear() {
@ -126,11 +128,12 @@ public class AdapterMessage extends PagedListAdapter<TupleMessageEx, AdapterMess
int pos = getAdapterPosition(); int pos = getAdapterPosition();
if (pos == RecyclerView.NO_POSITION) if (pos == RecyclerView.NO_POSITION)
return; return;
final TupleMessageEx message = getItem(pos); TupleMessageEx message = getItem(pos);
if (EntityFolder.DRAFTS.equals(message.folderType)) if (EntityFolder.DRAFTS.equals(message.folderType))
context.startActivity( context.startActivity(
new Intent(context, ActivityCompose.class) new Intent(context, ActivityCompose.class)
.putExtra("action", "edit")
.putExtra("id", message.id)); .putExtra("id", message.id));
else { else {
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context); LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
@ -139,6 +142,21 @@ public class AdapterMessage extends PagedListAdapter<TupleMessageEx, AdapterMess
.putExtra("id", message.id)); .putExtra("id", message.id));
} }
} }
@Override
public boolean onLongClick(View view) {
int pos = getAdapterPosition();
if (pos == RecyclerView.NO_POSITION)
return false;
TupleMessageEx message = getItem(pos);
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
lbm.sendBroadcast(
new Intent(ActivityView.ACTION_VIEW_MESSAGE)
.putExtra("id", message.id));
return true;
}
} }
AdapterMessage(Context context, ViewType viewType) { AdapterMessage(Context context, ViewType viewType) {

@ -38,6 +38,9 @@ public interface DaoAttachment {
@Query("SELECT * FROM attachment WHERE message = :message AND sequence = :sequence") @Query("SELECT * FROM attachment WHERE message = :message AND sequence = :sequence")
EntityAttachment getAttachment(long message, int sequence); EntityAttachment getAttachment(long message, int sequence);
@Query("SELECT COUNT(attachment.id) FROM attachment WHERE message = :message")
int getAttachmentCount(long message);
@Query("UPDATE attachment SET progress = :progress WHERE id = :id") @Query("UPDATE attachment SET progress = :progress WHERE id = :id")
void setProgress(long id, int progress); void setProgress(long id, int progress);

@ -94,6 +94,7 @@ public class FragmentAbout extends FragmentEx {
if (result.ex == null) { if (result.ex == null) {
long id = (Long) result.data; long id = (Long) result.data;
startActivity(new Intent(getContext(), ActivityCompose.class) startActivity(new Intent(getContext(), ActivityCompose.class)
.putExtra("action", "edit")
.putExtra("id", id)); .putExtra("id", id));
} else } else
Toast.makeText(getContext(), executor.toString(), Toast.LENGTH_LONG).show(); Toast.makeText(getContext(), executor.toString(), Toast.LENGTH_LONG).show();

@ -73,15 +73,12 @@ 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.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import static android.app.Activity.RESULT_OK; import static android.app.Activity.RESULT_OK;
public class FragmentCompose extends FragmentEx { public class FragmentCompose extends FragmentEx {
private boolean once = false;
private String thread = null;
private long rid = -1;
private Spinner spFrom; private Spinner spFrom;
private ImageView ivIdentityAdd; private ImageView ivIdentityAdd;
private AutoCompleteTextView etTo; private AutoCompleteTextView etTo;
@ -98,6 +95,9 @@ public class FragmentCompose extends FragmentEx {
private Group grpAddresses; private Group grpAddresses;
private Group grpAttachments; private Group grpAttachments;
private Group grpReady; private Group grpReady;
private MenuItem menuAttachment = null;
private AdapterAttachment adapter;
@Override @Override
@Nullable @Nullable
@ -106,11 +106,6 @@ public class FragmentCompose extends FragmentEx {
View view = inflater.inflate(R.layout.fragment_compose, container, false); View view = inflater.inflate(R.layout.fragment_compose, container, false);
// Get arguments
Bundle args = getArguments();
String action = (args == null ? null : args.getString("action"));
final long id = (TextUtils.isEmpty(action) ? (args == null ? -1 : args.getLong("id", -1)) : -1);
// Get controls // Get controls
spFrom = view.findViewById(R.id.spFrom); spFrom = view.findViewById(R.id.spFrom);
ivIdentityAdd = view.findViewById(R.id.ivIdentityAdd); ivIdentityAdd = view.findViewById(R.id.ivIdentityAdd);
@ -173,17 +168,7 @@ public class FragmentCompose extends FragmentEx {
bottom_navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() { bottom_navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override @Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) { public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) { onAction(item.getItemId());
case R.id.action_trash:
actionPut(id, "trash");
return true;
case R.id.action_save:
actionPut(id, "save");
return true;
case R.id.action_send:
actionPut(id, "send");
return true;
}
return false; return false;
} }
@ -248,6 +233,13 @@ public class FragmentCompose extends FragmentEx {
}); });
} }
rvAttachment.setHasFixedSize(false);
LinearLayoutManager llm = new LinearLayoutManager(getContext());
rvAttachment.setLayoutManager(llm);
adapter = new AdapterAttachment(getContext());
rvAttachment.setAdapter(adapter);
return view; return view;
} }
@ -258,6 +250,7 @@ public class FragmentCompose extends FragmentEx {
DB.getInstance(getContext()).identity().liveIdentities(true).observe(getViewLifecycleOwner(), new Observer<List<EntityIdentity>>() { DB.getInstance(getContext()).identity().liveIdentities(true).observe(getViewLifecycleOwner(), new Observer<List<EntityIdentity>>() {
@Override @Override
public void onChanged(@Nullable final List<EntityIdentity> identities) { public void onChanged(@Nullable final List<EntityIdentity> identities) {
// Sort identities
Collections.sort(identities, new Comparator<EntityIdentity>() { Collections.sort(identities, new Comparator<EntityIdentity>() {
@Override @Override
public int compare(EntityIdentity i1, EntityIdentity i2) { public int compare(EntityIdentity i1, EntityIdentity i2) {
@ -265,6 +258,7 @@ public class FragmentCompose extends FragmentEx {
} }
}); });
// Show identities
ArrayAdapter<EntityIdentity> adapter = new ArrayAdapter<>(getContext(), R.layout.spinner_item, identities); ArrayAdapter<EntityIdentity> adapter = new ArrayAdapter<>(getContext(), R.layout.spinner_item, identities);
adapter.setDropDownViewResource(R.layout.spinner_dropdown_item); adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
spFrom.setAdapter(adapter); spFrom.setAdapter(adapter);
@ -279,9 +273,16 @@ public class FragmentCompose extends FragmentEx {
spFrom.setVisibility(View.VISIBLE); spFrom.setVisibility(View.VISIBLE);
ivIdentityAdd.setVisibility(View.VISIBLE); ivIdentityAdd.setVisibility(View.VISIBLE);
// Get draft
new Handler().post(new Runnable() {
@Override
public void run() {
// Get might select another identity // Get might select another identity
LoaderManager.getInstance(FragmentCompose.this) LoaderManager.getInstance(FragmentCompose.this)
.restartLoader(ActivityCompose.LOADER_COMPOSE_GET, getArguments(), getLoaderCallbacks).forceLoad(); .restartLoader(ActivityCompose.LOADER_COMPOSE_GET, getArguments(), getLoaderCallbacks).forceLoad();
}
});
} }
}); });
} }
@ -292,6 +293,13 @@ public class FragmentCompose extends FragmentEx {
super.onCreateOptionsMenu(menu, inflater); super.onCreateOptionsMenu(menu, inflater);
} }
@Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
menuAttachment = menu.findItem(R.id.menu_attachment);
menuAttachment.setEnabled(false);
}
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) { switch (item.getItemId()) {
@ -321,60 +329,14 @@ public class FragmentCompose extends FragmentEx {
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) {
if (requestCode == ActivityCompose.REQUEST_ATTACHMENT) { if (requestCode == ActivityCompose.REQUEST_ATTACHMENT) {
if (data != null) { if (data != null)
Bundle args = new Bundle(); handleAddAttachment(data);
args.putParcelable("uri", data.getData()); } else
new SimpleLoader() { handlePickContact(requestCode, data);
@Override
public Object onLoad(Bundle args) throws IOException {
Uri uri = args.getParcelable("uri");
Cursor cursor = null;
try {
cursor = getActivity().getContentResolver().query(uri, null, null, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
EntityAttachment attachment = new EntityAttachment();
attachment.name = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
String extension = MimeTypeMap.getFileExtensionFromUrl(attachment.name.toLowerCase());
if (extension != null)
attachment.type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
if (extension == null)
attachment.type = "application/octet-stream";
InputStream is = null;
try {
is = getContext().getContentResolver().openInputStream(uri);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int len;
byte[] buffer = new byte[8192];
while ((len = is.read(buffer)) > 0)
bos.write(buffer, 0, len);
attachment.size = bos.size();
attachment.content = bos.toByteArray();
} finally {
if (is != null)
is.close();
} }
return attachment;
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
} }
@Override private void handlePickContact(int requestCode, Intent data) {
public void onLoaded(Bundle args, Result result) {
EntityAttachment attachment = (EntityAttachment) result.data;
}
}.load(this, ActivityCompose.LOADER_COMPOSE_ATTACHMENT, args);
}
} else {
Cursor cursor = null; Cursor cursor = null;
try { try {
cursor = getContext().getContentResolver().query(data.getData(), cursor = getContext().getContentResolver().query(data.getData(),
@ -418,21 +380,97 @@ public class FragmentCompose extends FragmentEx {
cursor.close(); cursor.close();
} }
} }
private void handleAddAttachment(Intent data) {
Bundle args = new Bundle();
args.putLong("id", getArguments().getLong("id"));
args.putParcelable("uri", data.getData());
new SimpleLoader() {
@Override
public Object onLoad(Bundle args) throws IOException {
Cursor cursor = null;
try {
Uri uri = args.getParcelable("uri");
cursor = getActivity().getContentResolver().query(uri, null, null, null, null, null);
if (cursor == null || !cursor.moveToFirst())
return null;
DB db = DB.getInstance(getContext());
long id = args.getLong("id");
EntityMessage draft = db.message().getMessage(id);
EntityAttachment attachment = new EntityAttachment();
attachment.message = draft.id;
attachment.sequence = db.attachment().getAttachmentCount(draft.id);
attachment.name = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
String extension = MimeTypeMap.getFileExtensionFromUrl(attachment.name.toLowerCase());
if (extension != null)
attachment.type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
if (extension == null)
attachment.type = "application/octet-stream";
String size = cursor.getString(cursor.getColumnIndex(OpenableColumns.SIZE));
attachment.size = (size == null ? null : Integer.parseInt(size));
attachment.progress = 0;
db.attachment().insertAttachment(attachment);
InputStream is = null;
try {
is = getContext().getContentResolver().openInputStream(uri);
ByteArrayOutputStream os = new ByteArrayOutputStream();
int len;
byte[] buffer = new byte[8192];
while ((len = is.read(buffer)) > 0) {
os.write(buffer, 0, len);
// Update progress
if (attachment.size != null) {
attachment.progress = os.size() * 100 / attachment.size;
db.attachment().updateAttachment(attachment);
}
}
attachment.size = os.size();
attachment.progress = null;
attachment.content = os.toByteArray();
db.attachment().updateAttachment(attachment);
} finally {
if (is != null)
is.close();
}
return null;
} finally {
if (cursor != null)
cursor.close();
}
}
@Override
public void onLoaded(Bundle args, Result result) {
if (result.ex != null)
Toast.makeText(getContext(), result.ex.toString(), Toast.LENGTH_LONG).show();
} }
}.load(this, ActivityCompose.LOADER_COMPOSE_ATTACHMENT, args);
} }
private void actionPut(long id, String action) { private void onAction(int action) {
Log.i(Helper.TAG, "Put id=" + id + " action=" + action);
bottom_navigation.getMenu().setGroupEnabled(0, false); bottom_navigation.getMenu().setGroupEnabled(0, false);
long id = getArguments().getLong("id", -1);
EntityIdentity identity = (EntityIdentity) spFrom.getSelectedItem(); EntityIdentity identity = (EntityIdentity) spFrom.getSelectedItem();
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putString("action", action);
args.putLong("id", id); args.putLong("id", id);
args.putLong("iid", identity == null ? -1 : identity.id); args.putInt("action", action);
args.putString("thread", FragmentCompose.this.thread); args.putLong("identity", identity == null ? -1 : identity.id);
args.putLong("rid", FragmentCompose.this.rid);
args.putString("to", etTo.getText().toString()); args.putString("to", etTo.getText().toString());
args.putString("cc", etCc.getText().toString()); args.putString("cc", etCc.getText().toString());
args.putString("bcc", etBcc.getText().toString()); args.putString("bcc", etBcc.getText().toString());
@ -443,7 +481,7 @@ public class FragmentCompose extends FragmentEx {
.restartLoader(ActivityCompose.LOADER_COMPOSE_PUT, args, putLoaderCallbacks).forceLoad(); .restartLoader(ActivityCompose.LOADER_COMPOSE_PUT, args, putLoaderCallbacks).forceLoad();
} }
private static class GetLoader extends AsyncTaskLoader<Bundle> { private static class GetLoader extends AsyncTaskLoader<EntityMessage> {
private Bundle args; private Bundle args;
GetLoader(Context context) { GetLoader(Context context) {
@ -456,167 +494,152 @@ public class FragmentCompose extends FragmentEx {
@Nullable @Nullable
@Override @Override
public Bundle loadInBackground() { public EntityMessage loadInBackground() {
Bundle result = new Bundle(); EntityMessage draft = null;
try { try {
String action = args.getString("action"); String action = args.getString("action");
long id = args.getLong("id", -1); long id = args.getLong("id", -1);
long account = args.getLong("account", -1);
long reference = args.getLong("reference", -1);
Log.i(Helper.TAG, "Get action=" + action + " id=" + id + " account=" + account + " reference=" + reference);
DB db = DB.getInstance(getContext());
draft = db.message().getMessage(id);
if (draft == null) {
EntityMessage ref = DB.getInstance(getContext()).message().getMessage(reference);
if (ref != null)
account = ref.account;
EntityFolder drafts;
drafts = db.folder().getFolderByType(account, EntityFolder.DRAFTS);
if (drafts == null)
drafts = db.folder().getPrimaryDrafts();
draft = new EntityMessage();
draft.account = account;
draft.folder = drafts.id;
if (ref != null) {
draft.thread = ref.thread;
if ("reply".equals(action)) {
draft.replying = ref.id;
draft.to = (ref.reply == null || ref.reply.length == 0 ? ref.from : ref.reply);
draft.from = ref.to;
result.putString("action", action);
EntityMessage msg = DB.getInstance(getContext()).message().getMessage(id);
if (msg != null) {
if (msg.identity != null)
result.putLong("iid", msg.identity);
if (msg.replying != null)
result.putLong("rid", msg.replying);
result.putSerializable("cc", msg.cc);
result.putSerializable("bcc", msg.bcc);
result.putString("thread", msg.thread);
result.putString("subject", msg.subject);
result.putString("body", msg.body);
}
if (TextUtils.isEmpty(action)) {
if (msg != null) {
result.putSerializable("from", msg.from);
result.putSerializable("to", msg.to);
}
} else if ("reply".equals(action)) {
Address[] to = null;
if (msg != null)
to = (msg.reply == null || msg.reply.length == 0 ? msg.from : msg.reply);
result.putLong("rid", msg.id);
result.putSerializable("from", msg.to);
result.putSerializable("to", to);
} else if ("reply_all".equals(action)) { } else if ("reply_all".equals(action)) {
Address[] to = null; draft.replying = ref.id;
if (msg != null) {
List<Address> addresses = new ArrayList<>(); List<Address> addresses = new ArrayList<>();
if (msg.reply != null) if (draft.reply != null && ref.reply.length > 0)
addresses.addAll(Arrays.asList(msg.reply)); addresses.addAll(Arrays.asList(ref.reply));
else if (msg.from != null) else if (draft.from != null)
addresses.addAll(Arrays.asList(msg.from)); addresses.addAll(Arrays.asList(ref.from));
if (msg.cc != null) if (draft.cc != null)
addresses.addAll(Arrays.asList(msg.cc)); addresses.addAll(Arrays.asList(ref.cc));
to = addresses.toArray(new Address[0]); draft.to = addresses.toArray(new Address[0]);
} draft.from = ref.to;
result.putLong("rid", msg.id);
result.putSerializable("from", msg.to); } else if ("forward".equals(action)) {
result.putSerializable("to", to); //msg.replying = ref.id;
draft.from = ref.to;
}
if ("reply".equals(action) || "reply_all".equals(action)) {
draft.subject = getContext().getString(R.string.title_subject_reply, ref.subject);
draft.body = String.format("<br><br>%s %s:<br><br>%s",
Html.escapeHtml(new Date().toString()),
Html.escapeHtml(TextUtils.join(", ", draft.to)),
HtmlHelper.sanitize(getContext(), ref.body, true));
} else if ("forward".equals(action)) { } else if ("forward".equals(action)) {
Address[] to = null; draft.subject = getContext().getString(R.string.title_subject_forward, ref.subject);
if (msg != null) draft.body = String.format("<br><br>%s %s:<br><br>%s",
to = (msg.reply == null || msg.reply.length == 0 ? msg.from : msg.reply); Html.escapeHtml(new Date().toString()),
result.putSerializable("from", msg.to); Html.escapeHtml(TextUtils.join(", ", ref.from)),
result.putSerializable("to", to); HtmlHelper.sanitize(getContext(), ref.body, true));
}
}
draft.received = new Date().getTime();
draft.seen = false;
draft.ui_seen = false;
draft.ui_hide = false;
draft.id = DB.getInstance(getContext()).message().insertMessage(draft);
} }
} catch (Throwable ex) { } catch (Throwable ex) {
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex)); Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
} }
return result;
return draft;
} }
} }
private LoaderManager.LoaderCallbacks getLoaderCallbacks = new LoaderManager.LoaderCallbacks<Bundle>() { private LoaderManager.LoaderCallbacks getLoaderCallbacks = new LoaderManager.LoaderCallbacks<EntityMessage>() {
@NonNull @NonNull
@Override @Override
public Loader<Bundle> onCreateLoader(int id, @Nullable Bundle args) { public Loader<EntityMessage> onCreateLoader(int id, @Nullable Bundle args) {
GetLoader loader = new GetLoader(getContext()); GetLoader loader = new GetLoader(getContext());
loader.setArgs(args); loader.setArgs(args);
return loader; return loader;
} }
@Override @Override
public void onLoadFinished(@NonNull Loader<Bundle> loader, Bundle result) { public void onLoadFinished(@NonNull Loader<EntityMessage> loader, EntityMessage msg) {
LoaderManager.getInstance(FragmentCompose.this).destroyLoader(loader.getId()); LoaderManager.getInstance(FragmentCompose.this).destroyLoader(loader.getId());
long iid = result.getLong("iid", -1); getArguments().putLong("id", msg.id);
long rid = result.getLong("rid", -1); String action = getArguments().getString("action");
String thread = result.getString("thread");
Address[] from = (Address[]) result.getSerializable("from");
Address[] to = (Address[]) result.getSerializable("to");
Address[] cc = (Address[]) result.getSerializable("cc");
Address[] bcc = (Address[]) result.getSerializable("bcc");
String subject = result.getString("subject");
String body = result.getString("body");
String action = result.getString("action");
menuAttachment.setEnabled(true);
pbWait.setVisibility(View.GONE); pbWait.setVisibility(View.GONE);
grpAddresses.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; ArrayAdapter aa = (ArrayAdapter) spFrom.getAdapter();
FragmentCompose.this.rid = rid; if (aa != null) {
for (int pos = 0; pos < aa.getCount(); pos++) {
ArrayAdapter adapter = (ArrayAdapter) spFrom.getAdapter(); EntityIdentity identity = (EntityIdentity) aa.getItem(pos);
if (adapter != null) { if (msg.identity == null
for (int pos = 0; pos < adapter.getCount(); pos++) { ? msg.from != null && msg.from.length > 0 && ((InternetAddress) msg.from[0]).getAddress().equals(identity.email)
EntityIdentity identity = (EntityIdentity) adapter.getItem(pos); : msg.identity.equals(identity.id)) {
if (iid < 0
? from != null && from.length > 0 && ((InternetAddress) from[0]).getAddress().equals(identity.email)
: iid == identity.id) {
spFrom.setSelection(pos); spFrom.setSelection(pos);
break; break;
} }
} }
} }
if (!once) { etTo.setText(msg.to == null ? null : TextUtils.join(", ", msg.to));
// Prevent changed fields from being overwritten etCc.setText(msg.cc == null ? null : TextUtils.join(", ", msg.cc));
once = true; etBcc.setText(msg.bcc == null ? null : TextUtils.join(", ", msg.bcc));
etSubject.setText(msg.subject);
Handler handler = new Handler();
etCc.setText(cc == null ? null : TextUtils.join(", ", cc));
etBcc.setText(bcc == null ? null : TextUtils.join(", ", bcc));
if (action == null) { DB db = DB.getInstance(getContext());
etTo.setText(to == null ? null : TextUtils.join(", ", to)); db.attachment().liveAttachments(msg.id).removeObservers(getViewLifecycleOwner());
etSubject.setText(subject); db.attachment().liveAttachments(msg.id).observe(getViewLifecycleOwner(),
if (body != null) new Observer<List<TupleAttachment>>() {
etBody.setText(Html.fromHtml(HtmlHelper.sanitize(getContext(), body, false)));
handler.post(new Runnable() {
@Override @Override
public void run() { public void onChanged(@Nullable List<TupleAttachment> attachments) {
etTo.requestFocus(); adapter.set(attachments);
grpAttachments.setVisibility(attachments.size() > 0 ? View.VISIBLE : View.GONE);
} }
}); });
} else if ("reply".equals(action) || "reply_all".equals(action)) {
etTo.setText(to == null ? null : TextUtils.join(", ", to));
String text = String.format("<br><br>%s %s:<br><br>%s", etBody.setText(TextUtils.isEmpty(msg.body) ? null : Html.fromHtml(msg.body));
Html.escapeHtml(new Date().toString()),
Html.escapeHtml(to == null ? "" : TextUtils.join(", ", to)), if ("edit".equals(action))
HtmlHelper.sanitize(getContext(), body, true)); etTo.requestFocus();
etSubject.setText(getContext().getString(R.string.title_subject_reply, subject)); else if ("reply".equals(action) || "reply_all".equals(action))
etBody.setText(Html.fromHtml(text));
handler.post(new Runnable() {
@Override
public void run() {
etBody.requestFocus(); etBody.requestFocus();
} else if ("forward".equals(action))
});
} else if ("forward".equals(action)) {
String text = String.format("<br><br>%s %s:<br><br>%s",
Html.escapeHtml(new Date().toString()),
Html.escapeHtml(to == null ? "" : TextUtils.join(", ", to)),
HtmlHelper.sanitize(getContext(), body, true));
etSubject.setText(getContext().getString(R.string.title_subject_forward, subject));
etBody.setText(Html.fromHtml(text));
handler.post(new Runnable() {
@Override
public void run() {
etTo.requestFocus(); etTo.requestFocus();
}
});
}
}
bottom_navigation.getMenu().setGroupEnabled(0, true); bottom_navigation.getMenu().setGroupEnabled(0, true);
} }
@Override @Override
public void onLoaderReset(@NonNull Loader<Bundle> loader) { public void onLoaderReset(@NonNull Loader<EntityMessage> loader) {
} }
}; };
@ -634,45 +657,29 @@ public class FragmentCompose extends FragmentEx {
@Override @Override
public Throwable loadInBackground() { public Throwable loadInBackground() {
try { try {
String action = args.getString("action");
long id = args.getLong("id");
Log.i(Helper.TAG, "Put load id=" + id + " action=" + action);
DB db = DB.getInstance(getContext());
DaoMessage message = db.message();
DaoIdentity identity = db.identity();
DaoFolder folder = db.folder();
// Get data // Get data
EntityMessage draft = message.getMessage(id); long id = args.getLong("id");
EntityIdentity ident = identity.getIdentity(args.getLong("iid")); int action = args.getInt("action");
if (ident == null) long iid = args.getLong("identity");
throw new IllegalArgumentException(getContext().getString(R.string.title_from_missing));
EntityFolder drafts = db.folder().getFolderByType(ident.account, EntityFolder.DRAFTS);
long rid = args.getLong("rid", -1);
String thread = args.getString("thread");
String to = args.getString("to"); String to = args.getString("to");
String cc = args.getString("cc"); String cc = args.getString("cc");
String bcc = args.getString("bcc"); String bcc = args.getString("bcc");
String body = args.getString("body");
String subject = args.getString("subject"); String subject = args.getString("subject");
String body = args.getString("body");
// Get draft & selected identity
DB db = DB.getInstance(getContext());
EntityMessage draft = db.message().getMessage(id);
EntityIdentity identity = db.identity().getIdentity(iid);
Address afrom[] = (ident == null ? null : new Address[]{new InternetAddress(ident.email, ident.name)}); // Convert data
Address afrom[] = (identity == null ? null : new Address[]{new InternetAddress(identity.email, identity.name)});
Address ato[] = (TextUtils.isEmpty(to) ? null : InternetAddress.parse(to)); Address ato[] = (TextUtils.isEmpty(to) ? null : InternetAddress.parse(to));
Address acc[] = (TextUtils.isEmpty(cc) ? null : InternetAddress.parse(cc)); Address acc[] = (TextUtils.isEmpty(cc) ? null : InternetAddress.parse(cc));
Address abcc[] = (TextUtils.isEmpty(bcc) ? null : InternetAddress.parse(bcc)); Address abcc[] = (TextUtils.isEmpty(bcc) ? null : InternetAddress.parse(bcc));
// Build draft // Update draft
boolean update = (draft != null); draft.identity = (identity == null ? null : identity.id);
if (draft == null)
draft = new EntityMessage();
draft.account = drafts.account;
draft.folder = drafts.id;
draft.identity = (ident == null ? null : ident.id);
draft.replying = (rid < 0 ? null : rid);
draft.thread = thread;
draft.from = afrom; draft.from = afrom;
draft.to = ato; draft.to = ato;
draft.cc = acc; draft.cc = acc;
@ -680,20 +687,15 @@ public class FragmentCompose extends FragmentEx {
draft.subject = subject; draft.subject = subject;
draft.body = "<pre>" + body.replaceAll("\\r?\\n", "<br />") + "</pre>"; draft.body = "<pre>" + body.replaceAll("\\r?\\n", "<br />") + "</pre>";
draft.received = new Date().getTime(); draft.received = new Date().getTime();
draft.seen = false;
draft.ui_seen = false;
draft.ui_hide = false;
// Store draft db.message().updateMessage(draft);
if (!update)
draft.id = message.insertMessage(draft);
// Check data // Check data
try { try {
db.beginTransaction(); db.beginTransaction();
if ("trash".equals(action)) { if (action == R.id.action_trash) {
EntityFolder trash = db.folder().getFolderByType(ident.account, EntityFolder.TRASH); EntityFolder trash = db.folder().getFolderByType(draft.account, EntityFolder.TRASH);
boolean move = (draft.uid != null); boolean move = (draft.uid != null);
if (move) if (move)
@ -706,7 +708,7 @@ public class FragmentCompose extends FragmentEx {
if (!move) if (!move)
EntityOperation.queue(db, draft, EntityOperation.ADD); EntityOperation.queue(db, draft, EntityOperation.ADD);
} else if ("save".equals(action)) { } else if (action == R.id.action_save) {
// Delete previous draft // Delete previous draft
draft.ui_hide = true; draft.ui_hide = true;
db.message().updateMessage(draft); db.message().updateMessage(draft);
@ -719,7 +721,11 @@ public class FragmentCompose extends FragmentEx {
draft.id = db.message().insertMessage(draft); draft.id = db.message().insertMessage(draft);
EntityOperation.queue(db, draft, EntityOperation.ADD); EntityOperation.queue(db, draft, EntityOperation.ADD);
} else if ("send".equals(action)) { } else if (action == R.id.action_send) {
// Check data
if (draft.identity == null)
throw new IllegalArgumentException(getContext().getString(R.string.title_from_missing));
if (draft.to == null && draft.cc == null && draft.bcc == null) if (draft.to == null && draft.cc == null && draft.bcc == null)
throw new IllegalArgumentException(getContext().getString(R.string.title_to_missing)); throw new IllegalArgumentException(getContext().getString(R.string.title_to_missing));
@ -730,7 +736,7 @@ public class FragmentCompose extends FragmentEx {
// Copy message to outbox // Copy message to outbox
draft.id = null; draft.id = null;
draft.folder = folder.getOutbox().id; draft.folder = db.folder().getOutbox().id;
draft.uid = null; draft.uid = null;
draft.ui_hide = false; draft.ui_hide = false;
draft.id = db.message().insertMessage(draft); draft.id = db.message().insertMessage(draft);
@ -766,19 +772,18 @@ public class FragmentCompose extends FragmentEx {
public void onLoadFinished(@NonNull Loader<Throwable> loader, Throwable ex) { public void onLoadFinished(@NonNull Loader<Throwable> loader, Throwable ex) {
LoaderManager.getInstance(FragmentCompose.this).destroyLoader(loader.getId()); LoaderManager.getInstance(FragmentCompose.this).destroyLoader(loader.getId());
Bundle args = ((PutLoader) loader).args;
String action = args.getString("action");
Log.i(Helper.TAG, "Put finished action=" + action + " ex=" + ex);
bottom_navigation.getMenu().setGroupEnabled(0, true); bottom_navigation.getMenu().setGroupEnabled(0, true);
if (ex == null) { if (ex == null) {
if ("trash".equals(action)) { Bundle args = ((PutLoader) loader).args;
int action = args.getInt("action");
if (action == R.id.action_trash) {
getFragmentManager().popBackStack(); getFragmentManager().popBackStack();
Toast.makeText(getContext(), R.string.title_draft_trashed, Toast.LENGTH_LONG).show(); Toast.makeText(getContext(), R.string.title_draft_trashed, Toast.LENGTH_LONG).show();
} else if ("save".equals(action)) } else if (action == R.id.action_save)
Toast.makeText(getContext(), R.string.title_draft_saved, Toast.LENGTH_LONG).show(); Toast.makeText(getContext(), R.string.title_draft_saved, Toast.LENGTH_LONG).show();
else if ("send".equals(action)) { else if (action == R.id.action_send) {
getFragmentManager().popBackStack(); getFragmentManager().popBackStack();
Toast.makeText(getContext(), R.string.title_queued, Toast.LENGTH_LONG).show(); Toast.makeText(getContext(), R.string.title_queued, Toast.LENGTH_LONG).show();
} }

@ -460,6 +460,7 @@ public class FragmentMessage extends FragmentEx {
if (result.ex == null) if (result.ex == null)
getContext().startActivity( getContext().startActivity(
new Intent(getContext(), ActivityCompose.class) new Intent(getContext(), ActivityCompose.class)
.putExtra("action", "edit")
.putExtra("id", (long) result.data)); .putExtra("id", (long) result.data));
else else
Toast.makeText(getContext(), result.ex.toString(), Toast.LENGTH_LONG).show(); Toast.makeText(getContext(), result.ex.toString(), Toast.LENGTH_LONG).show();
@ -469,14 +470,14 @@ public class FragmentMessage extends FragmentEx {
private void onActionForward(long id) { private void onActionForward(long id) {
startActivity(new Intent(getContext(), ActivityCompose.class) startActivity(new Intent(getContext(), ActivityCompose.class)
.putExtra("id", id) .putExtra("action", "forward")
.putExtra("action", "forward")); .putExtra("reference", id));
} }
private void onActionReplyAll(long id) { private void onActionReplyAll(long id) {
startActivity(new Intent(getContext(), ActivityCompose.class) startActivity(new Intent(getContext(), ActivityCompose.class)
.putExtra("id", id) .putExtra("action", "reply_all")
.putExtra("action", "reply_all")); .putExtra("reference", id));
} }
private void onActionSpam(final long id) { private void onActionSpam(final long id) {
@ -711,8 +712,8 @@ public class FragmentMessage extends FragmentEx {
private void onActionReply(long id) { private void onActionReply(long id) {
startActivity(new Intent(getContext(), ActivityCompose.class) startActivity(new Intent(getContext(), ActivityCompose.class)
.putExtra("id", id) .putExtra("action", "reply")
.putExtra("action", "reply")); .putExtra("reference", id));
} }
private static class MoveLoader extends AsyncTaskLoader<List<EntityFolder>> { private static class MoveLoader extends AsyncTaskLoader<List<EntityFolder>> {

@ -28,6 +28,7 @@ import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.google.android.material.floatingactionbutton.FloatingActionButton;
@ -83,7 +84,10 @@ public class FragmentMessages extends FragmentEx {
fab.setOnClickListener(new View.OnClickListener() { fab.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
startActivity(new Intent(getContext(), ActivityCompose.class)); startActivity(new Intent(getContext(), ActivityCompose.class)
.putExtra("action", "new")
.putExtra("account", (Long) fab.getTag())
);
} }
}); });
@ -91,6 +95,7 @@ public class FragmentMessages extends FragmentEx {
tvNoEmail.setVisibility(View.GONE); tvNoEmail.setVisibility(View.GONE);
grpReady.setVisibility(View.GONE); grpReady.setVisibility(View.GONE);
pbWait.setVisibility(View.VISIBLE); pbWait.setVisibility(View.VISIBLE);
fab.setVisibility(View.GONE);
return view; return view;
} }
@ -126,7 +131,6 @@ public class FragmentMessages extends FragmentEx {
messages = new LivePagedListBuilder<>(db.message().pagedThread(thread, debug), PAGE_SIZE).build(); messages = new LivePagedListBuilder<>(db.message().pagedThread(thread, debug), PAGE_SIZE).build();
} }
Log.i(Helper.TAG, "Observing messages");
messages.observe(getViewLifecycleOwner(), new Observer<PagedList<TupleMessageEx>>() { messages.observe(getViewLifecycleOwner(), new Observer<PagedList<TupleMessageEx>>() {
@Override @Override
public void onChanged(@Nullable PagedList<TupleMessageEx> messages) { public void onChanged(@Nullable PagedList<TupleMessageEx> messages) {
@ -145,5 +149,32 @@ public class FragmentMessages extends FragmentEx {
} }
} }
}); });
new SimpleLoader() {
@Override
public Object onLoad(Bundle args) throws Throwable {
long folder = (args == null ? -1 : args.getLong("folder", -1));
long thread = (args == null ? -1 : args.getLong("thread", -1)); // message ID
DB db = DB.getInstance(getContext());
if (thread < 0)
if (folder < 0)
return db.folder().getPrimaryDrafts().account;
else
return db.folder().getFolder(folder).account;
else
return db.message().getMessage(thread).account;
}
@Override
public void onLoaded(Bundle args, Result result) {
if (result.ex == null) {
fab.setTag(result.data);
fab.setVisibility(View.VISIBLE);
} else
Toast.makeText(getContext(), result.ex.toString(), Toast.LENGTH_LONG).show();
}
}.load(this, ActivityView.LOADER_MESSAGE_ACCOUNT, getArguments());
} }
} }

@ -102,4 +102,13 @@
port="465" port="465"
starttls="false" /> starttls="false" />
</provider--> </provider-->
<provider name="gmx.com">
<imap
host="imap.gmx.com"
port="993" />
<smtp
host="mail.gmx.com"
port="587"
starttls="false" />
</provider>
</providers> </providers>

Loading…
Cancel
Save