Virtual messages

pull/72/head
M66B 7 years ago
parent 6b9a83cb32
commit 17c894b3d1

@ -647,18 +647,16 @@ public class ActivityView extends ActivityBase implements FragmentManager.OnBack
new SimpleTask<Void>() { new SimpleTask<Void>() {
@Override @Override
protected Void onLoad(Context context, Bundle args) { protected Void onLoad(Context context, Bundle args) {
long id = args.getLong("id"); TupleMessageEx message = (TupleMessageEx) args.getSerializable("message");
DB db = DB.getInstance(context); DB db = DB.getInstance(context);
try { try {
db.beginTransaction(); db.beginTransaction();
EntityMessage message = db.message().getMessage(id); for (EntityMessage tmessage : db.message().getMessageByThread(message.account, message.thread)) {
if (message != null) // Searched messages are not stored in the database db.message().setMessageUiSeen(tmessage.id, true);
for (EntityMessage tmessage : db.message().getMessageByThread(message.account, message.thread)) { EntityOperation.queue(db, tmessage, EntityOperation.SEEN, true);
db.message().setMessageUiSeen(tmessage.id, true); }
EntityOperation.queue(db, tmessage, EntityOperation.SEEN, true);
}
db.setTransactionSuccessful(); db.setTransactionSuccessful();
} finally { } finally {

@ -190,7 +190,7 @@ public class AdapterMessage extends PagedListAdapter<TupleMessageEx, AdapterMess
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context); LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
lbm.sendBroadcast( lbm.sendBroadcast(
new Intent(ActivityView.ACTION_VIEW_MESSAGE) new Intent(ActivityView.ACTION_VIEW_MESSAGE)
.putExtra("id", message.id)); .putExtra("message", message));
} }
} }
@ -205,7 +205,7 @@ public class AdapterMessage extends PagedListAdapter<TupleMessageEx, AdapterMess
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context); LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
lbm.sendBroadcast( lbm.sendBroadcast(
new Intent(ActivityView.ACTION_VIEW_MESSAGE) new Intent(ActivityView.ACTION_VIEW_MESSAGE)
.putExtra("id", message.id)); .putExtra("message", message));
return true; return true;
} }

@ -20,7 +20,6 @@ package eu.faircode.email;
*/ */
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log; import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
@ -36,7 +35,6 @@ import java.util.List;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.lifecycle.LifecycleOwner; import androidx.lifecycle.LifecycleOwner;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.recyclerview.widget.DiffUtil; import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListUpdateCallback; import androidx.recyclerview.widget.ListUpdateCallback;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
@ -50,7 +48,7 @@ public class AdapterOperation extends RecyclerView.Adapter<AdapterOperation.View
private DateFormat df = SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.SHORT, SimpleDateFormat.LONG); private DateFormat df = SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.SHORT, SimpleDateFormat.LONG);
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener { public class ViewHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener {
View itemView; View itemView;
TextView tvMessage; TextView tvMessage;
TextView tvName; TextView tvName;
@ -66,12 +64,10 @@ public class AdapterOperation extends RecyclerView.Adapter<AdapterOperation.View
} }
private void wire() { private void wire() {
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this); itemView.setOnLongClickListener(this);
} }
private void unwire() { private void unwire() {
itemView.setOnClickListener(null);
itemView.setOnLongClickListener(null); itemView.setOnLongClickListener(null);
} }
@ -81,20 +77,6 @@ public class AdapterOperation extends RecyclerView.Adapter<AdapterOperation.View
tvTime.setText(df.format(new Date(operation.created))); tvTime.setText(df.format(new Date(operation.created)));
} }
@Override
public void onClick(View view) {
int pos = getAdapterPosition();
if (pos == RecyclerView.NO_POSITION)
return;
EntityOperation operation = filtered.get(pos);
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
lbm.sendBroadcast(
new Intent(ActivityView.ACTION_VIEW_MESSAGE)
.putExtra("id", operation.message));
}
@Override @Override
public boolean onLongClick(View view) { public boolean onLongClick(View view) {
int pos = getAdapterPosition(); int pos = getAdapterPosition();

@ -28,6 +28,7 @@ import java.io.File;
import java.io.FileReader; import java.io.FileReader;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.Serializable;
import java.util.Date; import java.util.Date;
import java.util.Random; import java.util.Random;
@ -36,6 +37,7 @@ import javax.mail.Address;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.room.Entity; import androidx.room.Entity;
import androidx.room.ForeignKey; import androidx.room.ForeignKey;
import androidx.room.Ignore;
import androidx.room.Index; import androidx.room.Index;
import androidx.room.PrimaryKey; import androidx.room.PrimaryKey;
@ -64,7 +66,7 @@ import static androidx.room.ForeignKey.CASCADE;
@Index(value = {"ui_hide"}) @Index(value = {"ui_hide"})
} }
) )
public class EntityMessage { public class EntityMessage implements Serializable {
static final String TABLE_NAME = "message"; static final String TABLE_NAME = "message";
@PrimaryKey(autoGenerate = true) @PrimaryKey(autoGenerate = true)
@ -98,6 +100,11 @@ public class EntityMessage {
public Boolean ui_hide; public Boolean ui_hide;
public String error; public String error;
@Ignore
String body = null;
@Ignore
boolean virtual = false;
static String generateMessageId() { static String generateMessageId() {
StringBuffer sb = new StringBuffer(); StringBuffer sb = new StringBuffer();
sb.append('<') sb.append('<')
@ -131,7 +138,9 @@ public class EntityMessage {
} }
String read(Context context) throws IOException { String read(Context context) throws IOException {
return read(context, this.id); if (body == null)
body = read(context, this.id);
return body;
} }
static String read(Context context, Long id) throws IOException { static String read(Context context, Long id) throws IOException {

@ -138,6 +138,12 @@ public class FragmentMessage extends FragmentEx {
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
if (savedInstanceState == null)
message = (TupleMessageEx) getArguments().getSerializable("message");
else
message = (TupleMessageEx) savedInstanceState.getSerializable("message");
openPgpConnection = new OpenPgpServiceConnection(getContext(), "org.sufficientlysecure.keychain"); openPgpConnection = new OpenPgpServiceConnection(getContext(), "org.sufficientlysecure.keychain");
openPgpConnection.bindToService(); openPgpConnection.bindToService();
} }
@ -156,10 +162,6 @@ public class FragmentMessage extends FragmentEx {
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = (ViewGroup) inflater.inflate(R.layout.fragment_message, container, false); view = (ViewGroup) inflater.inflate(R.layout.fragment_message, container, false);
// Get arguments
Bundle args = getArguments();
final long id = (args == null ? -1 : args.getLong("id"));
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
debug = prefs.getBoolean("debug", false); debug = prefs.getBoolean("debug", false);
@ -193,7 +195,7 @@ public class FragmentMessage extends FragmentEx {
tvCount.setOnClickListener(new View.OnClickListener() { tvCount.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
onMenuThread(message.id); onMenuThread();
} }
}); });
@ -301,19 +303,19 @@ public class FragmentMessage extends FragmentEx {
public boolean onNavigationItemSelected(@NonNull MenuItem item) { public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) { switch (item.getItemId()) {
case R.id.action_spam: case R.id.action_spam:
onActionSpam(id); onActionSpam();
return true; return true;
case R.id.action_trash: case R.id.action_trash:
onActionDelete(id); onActionDelete();
return true; return true;
case R.id.action_move: case R.id.action_move:
onActionMove(id); onActionMove();
return true; return true;
case R.id.action_archive: case R.id.action_archive:
onActionArchive(id); onActionArchive();
return true; return true;
case R.id.action_reply: case R.id.action_reply:
onActionReply(id); onActionReply();
return true; return true;
} }
return false; return false;
@ -348,6 +350,7 @@ public class FragmentMessage extends FragmentEx {
@Override @Override
public void onSaveInstanceState(Bundle outState) { public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
outState.putSerializable("message", message);
outState.putBoolean("free", free); outState.putBoolean("free", free);
if (free) { if (free) {
outState.putInt("tag_count", (int) tvCount.getTag()); outState.putInt("tag_count", (int) tvCount.getTag());
@ -361,269 +364,255 @@ public class FragmentMessage extends FragmentEx {
public void onActivityCreated(@Nullable final Bundle savedInstanceState) { public void onActivityCreated(@Nullable final Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState); super.onActivityCreated(savedInstanceState);
// Get arguments if (savedInstanceState == null) {
Bundle args = getArguments(); setSubtitle(Helper.localizeFolderName(getContext(), message.folderName));
final long id = (args == null ? -1 : args.getLong("id"));
final DB db = DB.getInstance(getContext());
// Observe message
db.message().liveMessage(id).observe(getViewLifecycleOwner(), new Observer<TupleMessageEx>() {
private boolean once = false;
@Override tvFrom.setText(message.from == null ? null : MessageHelper.getFormattedAddresses(message.from, true));
public void onChanged(@Nullable final TupleMessageEx message) { tvTime.setText(message.sent == null ? null : df.format(new Date(message.sent)));
if (message == null || (!(debug && BuildConfig.DEBUG) && message.ui_hide)) { tvTo.setText(message.to == null ? null : MessageHelper.getFormattedAddresses(message.to, true));
// Message gone (moved, deleted) tvSubject.setText(message.subject);
finish();
return;
}
FragmentMessage.this.message = message;
if (savedInstanceState == null) {
if (once)
return;
once = true;
setSubtitle(Helper.localizeFolderName(getContext(), message.folderName)); tvCount.setText(Integer.toString(message.count));
tvFrom.setText(message.from == null ? null : MessageHelper.getFormattedAddresses(message.from, true)); tvReplyTo.setText(message.reply == null ? null : MessageHelper.getFormattedAddresses(message.reply, true));
tvTime.setText(message.sent == null ? null : df.format(new Date(message.sent))); tvCc.setText(message.cc == null ? null : MessageHelper.getFormattedAddresses(message.cc, true));
tvTo.setText(message.to == null ? null : MessageHelper.getFormattedAddresses(message.to, true)); tvBcc.setText(message.bcc == null ? null : MessageHelper.getFormattedAddresses(message.bcc, true));
tvSubject.setText(message.subject);
tvCount.setText(Integer.toString(message.count)); tvError.setText(message.error);
} else {
tvReplyTo.setText(message.reply == null ? null : MessageHelper.getFormattedAddresses(message.reply, true)); free = savedInstanceState.getBoolean("free");
tvCc.setText(message.cc == null ? null : MessageHelper.getFormattedAddresses(message.cc, true)); if (free) {
tvBcc.setText(message.bcc == null ? null : MessageHelper.getFormattedAddresses(message.bcc, true)); tvCount.setTag(savedInstanceState.getInt("tag_count"));
tvCc.setTag(savedInstanceState.getInt("tag_cc"));
rvAttachment.setTag(savedInstanceState.getInt("tag_attachment"));
tvError.setTag(savedInstanceState.getInt("tag_error"));
}
decrypted = savedInstanceState.getString("decrypted");
}
tvError.setText(message.error); if (tvBody.getTag() == null) {
// Spanned text needs to be loaded after recreation too
final Bundle args = new Bundle();
args.putLong("id", message.id);
args.putBoolean("has_images", false);
args.putBoolean("show_images", false);
} else { pbBody.setVisibility(View.VISIBLE);
free = savedInstanceState.getBoolean("free"); final SimpleTask<Spanned> bodyTask = new SimpleTask<Spanned>() {
if (free) { @Override
tvCount.setTag(savedInstanceState.getInt("tag_count")); protected Spanned onLoad(final Context context, final Bundle args) throws Throwable {
tvCc.setTag(savedInstanceState.getInt("tag_cc")); final long id = args.getLong("id");
rvAttachment.setTag(savedInstanceState.getInt("tag_attachment")); final boolean show_images = args.getBoolean("show_images");
tvError.setTag(savedInstanceState.getInt("tag_error")); String body = (decrypted == null ? message.read(context) : decrypted);
} args.putInt("size", body.length());
decrypted = savedInstanceState.getString("decrypted");
}
getActivity().invalidateOptionsMenu(); return Html.fromHtml(HtmlHelper.sanitize(getContext(), body, false), new Html.ImageGetter() {
@Override
public Drawable getDrawable(String source) {
float scale = context.getResources().getDisplayMetrics().density;
int px = (int) (24 * scale + 0.5f);
if (show_images) {
// Get cache folder
File dir = new File(context.getCacheDir(), "images");
dir.mkdir();
// Cleanup cache
long now = new Date().getTime();
File[] images = dir.listFiles();
if (images != null)
for (File image : images)
if (image.isFile() && image.lastModified() + CACHE_IMAGE_DURATION < now) {
Log.i(Helper.TAG, "Deleting from image cache " + image.getName());
image.delete();
}
if (tvBody.getTag() == null) { // Create unique file name
// Spanned text needs to be loaded after recreation too File file = new File(dir, id + "_" + source.hashCode());
final Bundle args = new Bundle();
args.putLong("id", message.id);
args.putBoolean("has_images", false);
args.putBoolean("show_images", false);
pbBody.setVisibility(View.VISIBLE); InputStream is = null;
final SimpleTask<Spanned> bodyTask = new SimpleTask<Spanned>() { FileOutputStream os = null;
@Override try {
protected Spanned onLoad(final Context context, final Bundle args) throws Throwable { // Get input stream
final long id = args.getLong("id"); if (file.exists())
final boolean show_images = args.getBoolean("show_images"); is = new FileInputStream(file);
String body = (decrypted == null ? EntityMessage.read(context, id) : decrypted); else
args.putInt("size", body.length()); is = new URL(source).openStream();
// Decode image from stream
Bitmap bm = BitmapFactory.decodeStream(is);
if (bm == null)
throw new IllegalArgumentException();
// Cache bitmap
if (!file.exists()) {
os = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 100, os);
}
return Html.fromHtml(HtmlHelper.sanitize(getContext(), body, false), new Html.ImageGetter() { // Create drawable from bitmap
@Override Drawable d = new BitmapDrawable(context.getResources(), bm);
public Drawable getDrawable(String source) { d.setBounds(0, 0, bm.getWidth(), bm.getHeight());
float scale = context.getResources().getDisplayMetrics().density; return d;
int px = (int) (24 * scale + 0.5f); } catch (Throwable ex) {
// Show warning icon
if (show_images) { Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
// Get cache folder Drawable d = context.getResources().getDrawable(R.drawable.baseline_warning_24, context.getTheme());
File dir = new File(context.getCacheDir(), "images"); d.setBounds(0, 0, px, px);
dir.mkdir(); return d;
} finally {
// Cleanup cache // Close streams
long now = new Date().getTime(); if (is != null) {
File[] images = dir.listFiles();
if (images != null)
for (File image : images)
if (image.isFile() && image.lastModified() + CACHE_IMAGE_DURATION < now) {
Log.i(Helper.TAG, "Deleting from image cache " + image.getName());
image.delete();
}
// Create unique file name
File file = new File(dir, id + "_" + source.hashCode());
InputStream is = null;
FileOutputStream os = null;
try { try {
// Get input stream is.close();
if (file.exists()) } catch (IOException e) {
is = new FileInputStream(file); Log.w(Helper.TAG, e + "\n" + Log.getStackTraceString(e));
else }
is = new URL(source).openStream(); }
if (os != null) {
// Decode image from stream try {
Bitmap bm = BitmapFactory.decodeStream(is); os.close();
if (bm == null) } catch (IOException e) {
throw new IllegalArgumentException(); Log.w(Helper.TAG, e + "\n" + Log.getStackTraceString(e));
// Cache bitmap
if (!file.exists()) {
os = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 100, os);
}
// Create drawable from bitmap
Drawable d = new BitmapDrawable(context.getResources(), bm);
d.setBounds(0, 0, bm.getWidth(), bm.getHeight());
return d;
} catch (Throwable ex) {
// Show warning icon
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
Drawable d = context.getResources().getDrawable(R.drawable.baseline_warning_24, context.getTheme());
d.setBounds(0, 0, px, px);
return d;
} finally {
// Close streams
if (is != null) {
try {
is.close();
} catch (IOException e) {
Log.w(Helper.TAG, e + "\n" + Log.getStackTraceString(e));
}
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
Log.w(Helper.TAG, e + "\n" + Log.getStackTraceString(e));
}
}
} }
} else {
// Show placeholder icon
args.putBoolean("has_images", true);
Drawable d = context.getResources().getDrawable(R.drawable.baseline_image_24, context.getTheme());
d.setBounds(0, 0, px, px);
return d;
} }
} }
}, new Html.TagHandler() { } else {
@Override // Show placeholder icon
public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) { args.putBoolean("has_images", true);
// Do nothing Drawable d = context.getResources().getDrawable(R.drawable.baseline_image_24, context.getTheme());
} d.setBounds(0, 0, px, px);
}); return d;
}
} }
}, new Html.TagHandler() {
@Override @Override
protected void onLoaded(Bundle args, Spanned body) { public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) {
boolean has_images = args.getBoolean("has_images"); // Do nothing
boolean show_images = args.getBoolean("show_images");
tvSize.setText(Helper.humanReadableByteCount(args.getInt("size"), false));
tvBody.setText(body);
tvBody.setTag(true);
btnImages.setVisibility(has_images && !show_images ? View.VISIBLE : View.GONE);
grpMessage.setVisibility(View.VISIBLE);
fab.setVisibility(free ? View.GONE : View.VISIBLE);
pbBody.setVisibility(View.GONE);
} }
}; });
}
bodyTask.load(FragmentMessage.this, args); @Override
protected void onLoaded(Bundle args, Spanned body) {
boolean has_images = args.getBoolean("has_images");
boolean show_images = args.getBoolean("show_images");
tvSize.setText(Helper.humanReadableByteCount(args.getInt("size"), false));
tvBody.setText(body);
tvBody.setTag(true);
btnImages.setVisibility(has_images && !show_images ? View.VISIBLE : View.GONE);
grpMessage.setVisibility(View.VISIBLE);
fab.setVisibility(free ? View.GONE : View.VISIBLE);
pbBody.setVisibility(View.GONE);
}
};
btnImages.setOnClickListener(new View.OnClickListener() { bodyTask.load(FragmentMessage.this, args);
@Override
public void onClick(View v) { btnImages.setOnClickListener(new View.OnClickListener() {
v.setEnabled(false); @Override
args.putBoolean("show_images", true); public void onClick(View v) {
bodyTask.load(FragmentMessage.this, args); v.setEnabled(false);
} args.putBoolean("show_images", true);
}); bodyTask.load(FragmentMessage.this, args);
} }
});
}
int typeface = (message.ui_seen ? Typeface.NORMAL : Typeface.BOLD); int typeface = (message.ui_seen ? Typeface.NORMAL : Typeface.BOLD);
tvFrom.setTypeface(null, typeface); tvFrom.setTypeface(null, typeface);
tvTime.setTypeface(null, typeface); tvTime.setTypeface(null, typeface);
tvSubject.setTypeface(null, typeface); tvSubject.setTypeface(null, typeface);
tvCount.setTypeface(null, typeface); tvCount.setTypeface(null, typeface);
int colorUnseen = Helper.resolveColor(getContext(), message.ui_seen int colorUnseen = Helper.resolveColor(getContext(), message.ui_seen
? android.R.attr.textColorSecondary : R.attr.colorUnread); ? android.R.attr.textColorSecondary : R.attr.colorUnread);
tvFrom.setTextColor(colorUnseen); tvFrom.setTextColor(colorUnseen);
tvTime.setTextColor(colorUnseen); tvTime.setTextColor(colorUnseen);
pbWait.setVisibility(View.GONE); pbWait.setVisibility(View.GONE);
grpHeader.setVisibility(free ? View.GONE : View.VISIBLE); grpHeader.setVisibility(free ? View.GONE : View.VISIBLE);
vSeparatorBody.setVisibility(free ? View.GONE : View.VISIBLE); vSeparatorBody.setVisibility(free ? View.GONE : View.VISIBLE);
if (free) { if (free) {
tvCount.setVisibility((int) tvCount.getTag()); tvCount.setVisibility((int) tvCount.getTag());
grpAddresses.setVisibility((int) tvCc.getTag()); grpAddresses.setVisibility((int) tvCc.getTag());
grpError.setVisibility((int) tvError.getTag()); grpError.setVisibility((int) tvError.getTag());
} else { } else {
tvCount.setVisibility(!free && message.count > 1 ? View.VISIBLE : View.GONE); tvCount.setVisibility(!free && message.count > 1 ? View.VISIBLE : View.GONE);
grpError.setVisibility(free || message.error == null ? View.GONE : View.VISIBLE); grpError.setVisibility(free || message.error == null ? View.GONE : View.VISIBLE);
}
DB db = DB.getInstance(getContext());
if (!message.virtual) {
// Observe message
db.message().liveMessage(message.id).observe(getViewLifecycleOwner(), new Observer<TupleMessageEx>() {
@Override
public void onChanged(@Nullable final TupleMessageEx message) {
if (message == null || (!(debug && BuildConfig.DEBUG) && message.ui_hide)) {
// Message gone (moved, deleted)
finish();
return;
}
} }
});
// Observe attachments // Observe attachments
db.attachment().liveAttachments(id).removeObservers(getViewLifecycleOwner()); db.attachment().liveAttachments(message.id).observe(getViewLifecycleOwner(),
db.attachment().liveAttachments(id).observe(getViewLifecycleOwner(), new Observer<List<EntityAttachment>>() {
new Observer<List<EntityAttachment>>() { @Override
@Override public void onChanged(@Nullable List<EntityAttachment> attachments) {
public void onChanged(@Nullable List<EntityAttachment> attachments) { if (attachments == null)
if (attachments == null) attachments = new ArrayList<>();
attachments = new ArrayList<>();
adapter.set(attachments); adapter.set(attachments);
grpAttachments.setVisibility(!free && attachments.size() > 0 ? View.VISIBLE : View.GONE); grpAttachments.setVisibility(!free && attachments.size() > 0 ? View.VISIBLE : View.GONE);
} }
}); });
// Observe folders // Observe folders
db.folder().liveFolders(message.account).removeObservers(getViewLifecycleOwner()); db.folder().liveFolders(message.account).observe(getViewLifecycleOwner(), new Observer<List<TupleFolderEx>>() {
db.folder().liveFolders(message.account).observe(getViewLifecycleOwner(), new Observer<List<TupleFolderEx>>() { @Override
@Override public void onChanged(@Nullable List<TupleFolderEx> folders) {
public void onChanged(@Nullable List<TupleFolderEx> folders) { if (folders == null)
if (folders == null) folders = new ArrayList<>();
folders = new ArrayList<>();
boolean inInbox = EntityFolder.INBOX.equals(message.folderType);
boolean inInbox = EntityFolder.INBOX.equals(message.folderType); boolean inOutbox = EntityFolder.OUTBOX.equals(message.folderType);
boolean inOutbox = EntityFolder.OUTBOX.equals(message.folderType); boolean inArchive = EntityFolder.ARCHIVE.equals(message.folderType);
boolean inArchive = EntityFolder.ARCHIVE.equals(message.folderType); boolean inTrash = EntityFolder.TRASH.equals(message.folderType);
boolean inTrash = EntityFolder.TRASH.equals(message.folderType); boolean inJunk = EntityFolder.JUNK.equals(message.folderType);
boolean inJunk = EntityFolder.JUNK.equals(message.folderType);
boolean hasTrash = false;
boolean hasTrash = false; boolean hasJunk = false;
boolean hasJunk = false; boolean hasArchive = false;
boolean hasArchive = false; boolean hasUser = false;
boolean hasUser = false; if (folders != null)
if (folders != null) for (EntityFolder folder : folders) {
for (EntityFolder folder : folders) { if (EntityFolder.TRASH.equals(folder.type))
if (EntityFolder.TRASH.equals(folder.type)) hasTrash = true;
hasTrash = true; else if (EntityFolder.JUNK.equals(folder.type))
else if (EntityFolder.JUNK.equals(folder.type)) hasJunk = true;
hasJunk = true; else if (EntityFolder.ARCHIVE.equals(folder.type))
else if (EntityFolder.ARCHIVE.equals(folder.type)) hasArchive = true;
hasArchive = true; else if (EntityFolder.USER.equals(folder.type))
else if (EntityFolder.USER.equals(folder.type)) hasUser = true;
hasUser = true; }
}
bottom_navigation.setTag(inTrash || !hasTrash || inOutbox); bottom_navigation.setTag(inTrash || !hasTrash || inOutbox);
bottom_navigation.getMenu().findItem(R.id.action_spam).setVisible(message.uid != null && !inArchive && !inJunk && hasJunk); bottom_navigation.getMenu().findItem(R.id.action_spam).setVisible(message.uid != null && !inArchive && !inJunk && hasJunk);
bottom_navigation.getMenu().findItem(R.id.action_trash).setVisible((message.uid != null && hasTrash) || (inOutbox && !TextUtils.isEmpty(message.error))); bottom_navigation.getMenu().findItem(R.id.action_trash).setVisible((message.uid != null && hasTrash) || (inOutbox && !TextUtils.isEmpty(message.error)));
bottom_navigation.getMenu().findItem(R.id.action_move).setVisible(message.uid != null && (!inInbox || hasUser)); bottom_navigation.getMenu().findItem(R.id.action_move).setVisible(message.uid != null && (!inInbox || hasUser));
bottom_navigation.getMenu().findItem(R.id.action_archive).setVisible(message.uid != null && !inArchive && hasArchive); bottom_navigation.getMenu().findItem(R.id.action_archive).setVisible(message.uid != null && !inArchive && hasArchive);
bottom_navigation.getMenu().findItem(R.id.action_reply).setVisible(!inOutbox); bottom_navigation.getMenu().findItem(R.id.action_reply).setVisible(!inOutbox);
bottom_navigation.setVisibility(View.VISIBLE); bottom_navigation.setVisibility(View.VISIBLE);
} }
}); });
} }
});
} }
@Override @Override
@ -636,22 +625,20 @@ public class FragmentMessage extends FragmentEx {
public void onPrepareOptionsMenu(Menu menu) { public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu); super.onPrepareOptionsMenu(menu);
boolean inOutbox = (message != null && EntityFolder.OUTBOX.equals(message.folderType)); boolean inOutbox = EntityFolder.OUTBOX.equals(message.folderType);
menu.findItem(R.id.menu_addresses).setVisible(!free); menu.findItem(R.id.menu_addresses).setVisible(!free);
menu.findItem(R.id.menu_thread).setVisible(!free && message != null && message.count > 1); menu.findItem(R.id.menu_thread).setVisible(!free && !message.virtual && message.count > 1);
menu.findItem(R.id.menu_seen).setVisible(!free && message != null && !inOutbox); menu.findItem(R.id.menu_seen).setVisible(!free && !message.virtual && !inOutbox);
menu.findItem(R.id.menu_forward).setVisible(!free && message != null && !inOutbox); menu.findItem(R.id.menu_forward).setVisible(!free && !message.virtual && !inOutbox);
menu.findItem(R.id.menu_reply_all).setVisible(!free && message != null && message.cc != null && !inOutbox); menu.findItem(R.id.menu_reply_all).setVisible(!free && !message.virtual && message.cc != null && !inOutbox);
menu.findItem(R.id.menu_decrypt).setVisible(decrypted == null); menu.findItem(R.id.menu_decrypt).setVisible(decrypted == null);
if (message != null) { MenuItem menuSeen = menu.findItem(R.id.menu_seen);
MenuItem menuSeen = menu.findItem(R.id.menu_seen); menuSeen.setIcon(message.ui_seen
menuSeen.setIcon(message.ui_seen ? R.drawable.baseline_visibility_off_24
? R.drawable.baseline_visibility_off_24 : R.drawable.baseline_visibility_24);
: R.drawable.baseline_visibility_24); menuSeen.setTitle(message.ui_seen ? R.string.title_unseen : R.string.title_seen);
menuSeen.setTitle(message.ui_seen ? R.string.title_unseen : R.string.title_seen);
}
} }
@Override @Override
@ -661,19 +648,19 @@ public class FragmentMessage extends FragmentEx {
onMenuAddresses(); onMenuAddresses();
return true; return true;
case R.id.menu_thread: case R.id.menu_thread:
onMenuThread(message.id); onMenuThread();
return true; return true;
case R.id.menu_seen: case R.id.menu_seen:
onMenuSeen(message.id); onMenuSeen();
return true; return true;
case R.id.menu_forward: case R.id.menu_forward:
onMenuForward(message.id); onMenuForward();
return true; return true;
case R.id.menu_reply_all: case R.id.menu_reply_all:
onMenuReplyAll(message.id); onMenuReplyAll();
return true; return true;
case R.id.menu_decrypt: case R.id.menu_decrypt:
onMenuDecrypt(message); onMenuDecrypt();
return true; return true;
default: default:
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
@ -684,11 +671,11 @@ public class FragmentMessage extends FragmentEx {
grpAddresses.setVisibility(grpAddresses.getVisibility() == View.GONE ? View.VISIBLE : View.GONE); grpAddresses.setVisibility(grpAddresses.getVisibility() == View.GONE ? View.VISIBLE : View.GONE);
} }
private void onMenuThread(long id) { private void onMenuThread() {
getFragmentManager().popBackStack("thread", FragmentManager.POP_BACK_STACK_INCLUSIVE); getFragmentManager().popBackStack("thread", FragmentManager.POP_BACK_STACK_INCLUSIVE);
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putLong("thread", id); // message ID args.putLong("thread", message.id);
FragmentMessages fragment = new FragmentMessages(); FragmentMessages fragment = new FragmentMessages();
fragment.setArguments(args); fragment.setArguments(args);
@ -698,11 +685,11 @@ public class FragmentMessage extends FragmentEx {
fragmentTransaction.commit(); fragmentTransaction.commit();
} }
private void onMenuSeen(long id) { private void onMenuSeen() {
Helper.setViewsEnabled(view, false); Helper.setViewsEnabled(view, false);
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putLong("id", id); args.putLong("id", message.id);
new SimpleTask<Void>() { new SimpleTask<Void>() {
@Override @Override
@ -741,19 +728,19 @@ public class FragmentMessage extends FragmentEx {
}.load(this, args); }.load(this, args);
} }
private void onMenuForward(long id) { private void onMenuForward() {
startActivity(new Intent(getContext(), ActivityCompose.class) startActivity(new Intent(getContext(), ActivityCompose.class)
.putExtra("action", "forward") .putExtra("action", "forward")
.putExtra("reference", id)); .putExtra("reference", message.id));
} }
private void onMenuReplyAll(long id) { private void onMenuReplyAll() {
startActivity(new Intent(getContext(), ActivityCompose.class) startActivity(new Intent(getContext(), ActivityCompose.class)
.putExtra("action", "reply_all") .putExtra("action", "reply_all")
.putExtra("reference", id)); .putExtra("reference", message.id));
} }
private void onMenuDecrypt(EntityMessage message) { private void onMenuDecrypt() {
Log.i(Helper.TAG, "On decrypt"); Log.i(Helper.TAG, "On decrypt");
try { try {
if (!PreferenceManager.getDefaultSharedPreferences(getContext()).getBoolean("pro", false)) if (!PreferenceManager.getDefaultSharedPreferences(getContext()).getBoolean("pro", false))
@ -826,14 +813,14 @@ public class FragmentMessage extends FragmentEx {
public void onActivityResult(int requestCode, int resultCode, Intent data) { public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i(Helper.TAG, "Message onActivityResult request=" + requestCode + " result=" + resultCode + " data=" + data); Log.i(Helper.TAG, "Message onActivityResult request=" + requestCode + " result=" + resultCode + " data=" + data);
if (resultCode == RESULT_OK) { if (resultCode == RESULT_OK) {
if (requestCode == ActivityView.REQUEST_OPENPGP && message != null) { if (requestCode == ActivityView.REQUEST_OPENPGP) {
Log.i(Helper.TAG, "User interacted"); Log.i(Helper.TAG, "User interacted");
onMenuDecrypt(message); onMenuDecrypt();
} }
} }
} }
private void onActionSpam(final long id) { private void onActionSpam() {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder builder
.setMessage(R.string.title_ask_spam) .setMessage(R.string.title_ask_spam)
@ -843,7 +830,7 @@ public class FragmentMessage extends FragmentEx {
Helper.setViewsEnabled(view, false); Helper.setViewsEnabled(view, false);
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putLong("id", id); args.putLong("id", message.id);
new SimpleTask<Void>() { new SimpleTask<Void>() {
@Override @Override
@ -886,7 +873,7 @@ public class FragmentMessage extends FragmentEx {
.setNegativeButton(android.R.string.cancel, null).show(); .setNegativeButton(android.R.string.cancel, null).show();
} }
private void onActionDelete(final long id) { private void onActionDelete() {
boolean delete = (Boolean) bottom_navigation.getTag(); boolean delete = (Boolean) bottom_navigation.getTag();
if (delete) { if (delete) {
// No trash or is trash // No trash or is trash
@ -899,7 +886,7 @@ public class FragmentMessage extends FragmentEx {
Helper.setViewsEnabled(view, false); Helper.setViewsEnabled(view, false);
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putLong("id", id); args.putLong("id", message.id);
new SimpleTask<Void>() { new SimpleTask<Void>() {
@Override @Override
@ -946,7 +933,7 @@ public class FragmentMessage extends FragmentEx {
Helper.setViewsEnabled(view, false); Helper.setViewsEnabled(view, false);
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putLong("id", id); args.putLong("id", message.id);
new SimpleTask<Void>() { new SimpleTask<Void>() {
@Override @Override
@ -986,9 +973,9 @@ public class FragmentMessage extends FragmentEx {
} }
} }
private void onActionMove(long id) { private void onActionMove() {
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putLong("id", id); args.putLong("id", message.id);
new SimpleTask<List<EntityFolder>>() { new SimpleTask<List<EntityFolder>>() {
@Override @Override
@ -1102,11 +1089,11 @@ public class FragmentMessage extends FragmentEx {
}.load(FragmentMessage.this, args); }.load(FragmentMessage.this, args);
} }
private void onActionArchive(long id) { private void onActionArchive() {
Helper.setViewsEnabled(view, false); Helper.setViewsEnabled(view, false);
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putLong("id", id); args.putLong("id", message.id);
new SimpleTask<Void>() { new SimpleTask<Void>() {
@Override @Override
@ -1147,9 +1134,9 @@ public class FragmentMessage extends FragmentEx {
}.load(FragmentMessage.this, args); }.load(FragmentMessage.this, args);
} }
private void onActionReply(long id) { private void onActionReply() {
startActivity(new Intent(getContext(), ActivityCompose.class) startActivity(new Intent(getContext(), ActivityCompose.class)
.putExtra("action", "reply") .putExtra("action", "reply")
.putExtra("reference", id)); .putExtra("reference", message.id));
} }
} }

@ -218,17 +218,25 @@ public class FragmentMessages extends FragmentEx {
@Override @Override
public void loadInitial(LoadInitialParams params, LoadInitialCallback<TupleMessageEx> callback) { public void loadInitial(LoadInitialParams params, LoadInitialCallback<TupleMessageEx> callback) {
Log.i(Helper.TAG, "loadInitial(" + params.requestedStartPosition + ", " + params.requestedLoadSize + ")"); Log.i(Helper.TAG, "loadInitial(" + params.requestedStartPosition + ", " + params.requestedLoadSize + ")");
callback.onResult(search(search, params.requestedStartPosition, params.requestedLoadSize), params.requestedStartPosition); SearchResult result = search(search, params.requestedStartPosition, params.requestedLoadSize);
callback.onResult(result.messages, params.requestedStartPosition, result.total);
} }
@Override @Override
public void loadRange(LoadRangeParams params, LoadRangeCallback<TupleMessageEx> callback) { public void loadRange(LoadRangeParams params, LoadRangeCallback<TupleMessageEx> callback) {
Log.i(Helper.TAG, "loadRange(" + params.startPosition + ", " + params.loadSize + ")"); Log.i(Helper.TAG, "loadRange(" + params.startPosition + ", " + params.loadSize + ")");
callback.onResult(search(search, params.startPosition, params.loadSize)); SearchResult result = search(search, params.startPosition, params.loadSize);
callback.onResult(result.messages);
} }
private List<TupleMessageEx> search(String term, int from, int count) { class SearchResult {
List<TupleMessageEx> list = new ArrayList<>(); int total;
List<TupleMessageEx> messages;
}
private SearchResult search(String term, int from, int count) {
SearchResult result = new SearchResult();
result.messages = new ArrayList<>();
IMAPStore istore = null; IMAPStore istore = null;
try { try {
DB db = DB.getInstance(getContext()); DB db = DB.getInstance(getContext());
@ -251,11 +259,12 @@ public class FragmentMessages extends FragmentEx {
new OrTerm( new OrTerm(
new SubjectTerm(term), new SubjectTerm(term),
new BodyTerm(term))); new BodyTerm(term)));
result.total = imessages.length;
Log.i(Helper.TAG, "Found messages=" + imessages.length); Log.i(Helper.TAG, "Found messages=" + imessages.length);
List<Message> selected = new ArrayList<>(); List<Message> selected = new ArrayList<>();
int base = imessages.length - 1 - from; int base = imessages.length - 1 - from;
for (int i = base; i >= 0 && i >= base - count - 1; i--) for (int i = base; i >= 0 && i >= base - count + 1; i--)
selected.add(imessages[i]); selected.add(imessages[i]);
Log.i(Helper.TAG, "Selected messages=" + selected.size()); Log.i(Helper.TAG, "Selected messages=" + selected.size());
@ -303,7 +312,10 @@ public class FragmentMessages extends FragmentEx {
message.unseen = (seen ? 0 : 1); message.unseen = (seen ? 0 : 1);
message.attachments = 0; message.attachments = 0;
list.add(message); message.body = helper.getHtml();
message.virtual = true;
result.messages.add(message);
} }
} catch (Throwable ex) { } catch (Throwable ex) {
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex)); Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
@ -316,17 +328,16 @@ public class FragmentMessages extends FragmentEx {
} }
} }
return list; return result;
} }
}; };
} }
}; };
PagedList.Config.Builder plcb = new PagedList.Config.Builder() PagedList.Config.Builder plcb = new PagedList.Config.Builder()
.setEnablePlaceholders(false) .setEnablePlaceholders(true)
.setInitialLoadSizeHint(10) .setInitialLoadSizeHint(10)
.setPageSize(PAGE_SIZE); .setPageSize(10);
messages = new LivePagedListBuilder<>(dsf, plcb.build()).build(); messages = new LivePagedListBuilder<>(dsf, plcb.build()).build();
} }

Loading…
Cancel
Save