Files
FairEmail/app/src/main/java/eu/faircode/email/AdapterMessage.java

423 lines
18 KiB
Java
Raw Normal View History

2018-08-02 13:33:06 +00:00
package eu.faircode.email;
/*
2018-08-14 05:53:24 +00:00
This file is part of FairEmail.
2018-08-02 13:33:06 +00:00
2018-08-14 05:53:24 +00:00
FairEmail is free software: you can redistribute it and/or modify
2018-08-02 13:33:06 +00:00
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
NetGuard is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with NetGuard. If not, see <http://www.gnu.org/licenses/>.
Copyright 2018 by Marcel Bokhorst (M66B)
*/
2018-09-08 14:57:03 +00:00
import android.Manifest;
import android.content.ContentResolver;
2018-08-02 13:33:06 +00:00
import android.content.Context;
2018-09-19 10:00:58 +00:00
import android.content.DialogInterface;
2018-08-02 13:33:06 +00:00
import android.content.Intent;
import android.content.SharedPreferences;
2018-09-08 14:57:03 +00:00
import android.content.pm.PackageManager;
2018-09-12 16:54:48 +00:00
import android.graphics.Color;
2018-08-02 13:33:06 +00:00
import android.graphics.Typeface;
2018-09-08 14:57:03 +00:00
import android.graphics.drawable.Drawable;
import android.net.Uri;
2018-09-01 06:27:13 +00:00
import android.os.Bundle;
2018-08-06 13:36:55 +00:00
import android.preference.PreferenceManager;
2018-09-08 14:57:03 +00:00
import android.provider.ContactsContract;
2018-08-02 13:33:06 +00:00
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
2018-08-02 13:33:06 +00:00
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
2018-08-07 06:38:00 +00:00
import android.widget.ProgressBar;
2018-08-02 13:33:06 +00:00
import android.widget.TextView;
2018-09-08 14:57:03 +00:00
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
2018-09-08 17:04:10 +00:00
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
2018-08-08 06:55:47 +00:00
import androidx.annotation.NonNull;
import androidx.appcompat.widget.PopupMenu;
2018-09-08 14:57:03 +00:00
import androidx.core.content.ContextCompat;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.Observer;
2018-08-08 06:55:47 +00:00
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.paging.PagedListAdapter;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;
2018-08-07 06:38:00 +00:00
public class AdapterMessage extends PagedListAdapter<TupleMessageEx, AdapterMessage.ViewHolder> {
2018-08-02 13:33:06 +00:00
private Context context;
private LifecycleOwner owner;
2018-08-06 13:36:55 +00:00
private ViewType viewType;
private boolean avatars;
2018-08-06 16:22:01 +00:00
private boolean debug;
2018-09-08 17:04:10 +00:00
private ExecutorService executor = Executors.newCachedThreadPool(Helper.backgroundThreadFactory);
private DateFormat df = SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.SHORT, SimpleDateFormat.LONG);
2018-08-02 13:33:06 +00:00
2018-08-26 12:17:09 +00:00
enum ViewType {UNIFIED, FOLDER, THREAD, SEARCH}
2018-08-02 13:33:06 +00:00
public class ViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener, View.OnLongClickListener {
2018-08-02 13:33:06 +00:00
View itemView;
2018-09-12 16:54:48 +00:00
View vwColor;
2018-09-08 14:57:03 +00:00
ImageView ivAvatar;
2018-09-07 15:12:43 +00:00
ImageView ivFlagged;
2018-08-03 07:39:43 +00:00
TextView tvFrom;
TextView tvSize;
2018-08-02 13:33:06 +00:00
TextView tvTime;
ImageView ivAttachments;
2018-08-02 13:33:06 +00:00
TextView tvSubject;
2018-08-22 16:37:10 +00:00
TextView tvFolder;
2018-08-02 13:33:06 +00:00
TextView tvCount;
2018-09-04 07:56:30 +00:00
ImageView ivThread;
TextView tvError;
2018-08-07 06:38:00 +00:00
ProgressBar pbLoading;
2018-08-02 13:33:06 +00:00
2018-09-19 10:00:58 +00:00
private static final int action_flag = 1;
private static final int action_seen = 2;
private static final int action_delete = 3;
2018-08-02 13:33:06 +00:00
ViewHolder(View itemView) {
super(itemView);
2018-09-21 15:36:24 +00:00
this.itemView = itemView.findViewById(R.id.clItem);
2018-09-12 16:54:48 +00:00
vwColor = itemView.findViewById(R.id.vwColor);
2018-09-07 15:12:43 +00:00
ivFlagged = itemView.findViewById(R.id.ivFlagged);
ivAvatar = itemView.findViewById(R.id.ivAvatar);
2018-08-03 07:39:43 +00:00
tvFrom = itemView.findViewById(R.id.tvFrom);
tvSize = itemView.findViewById(R.id.tvSize);
2018-08-02 13:33:06 +00:00
tvTime = itemView.findViewById(R.id.tvTime);
ivAttachments = itemView.findViewById(R.id.ivAttachments);
2018-08-02 13:33:06 +00:00
tvSubject = itemView.findViewById(R.id.tvSubject);
2018-08-22 16:37:10 +00:00
tvFolder = itemView.findViewById(R.id.tvFolder);
2018-08-02 13:33:06 +00:00
tvCount = itemView.findViewById(R.id.tvCount);
2018-09-04 07:56:30 +00:00
ivThread = itemView.findViewById(R.id.ivThread);
tvError = itemView.findViewById(R.id.tvError);
2018-08-07 06:38:00 +00:00
pbLoading = itemView.findViewById(R.id.pbLoading);
2018-08-02 13:33:06 +00:00
}
private void wire() {
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);
2018-08-02 13:33:06 +00:00
}
private void unwire() {
itemView.setOnClickListener(null);
itemView.setOnLongClickListener(null);
2018-08-02 13:33:06 +00:00
}
2018-08-07 06:38:00 +00:00
private void clear() {
2018-10-07 12:31:29 +00:00
vwColor.setBackgroundColor(Color.TRANSPARENT);
2018-09-07 15:12:43 +00:00
ivFlagged.setVisibility(View.GONE);
2018-09-08 17:39:35 +00:00
ivAvatar.setVisibility(View.GONE);
2018-08-07 06:38:00 +00:00
tvFrom.setText(null);
tvSize.setText(null);
2018-08-07 06:38:00 +00:00
tvTime.setText(null);
ivAttachments.setVisibility(View.GONE);
2018-09-07 15:12:43 +00:00
tvSubject.setText(null);
2018-08-23 06:57:24 +00:00
tvFolder.setText(null);
2018-08-07 06:38:00 +00:00
tvCount.setText(null);
2018-09-04 07:56:30 +00:00
ivThread.setVisibility(View.GONE);
2018-09-08 17:39:35 +00:00
tvError.setVisibility(View.GONE);
2018-08-07 06:38:00 +00:00
pbLoading.setVisibility(View.VISIBLE);
}
private void bindTo(final TupleMessageEx message) {
2018-08-07 06:38:00 +00:00
pbLoading.setVisibility(View.GONE);
2018-09-20 05:27:45 +00:00
itemView.setAlpha(viewType == ViewType.THREAD && EntityFolder.ARCHIVE.equals(message.folderType) ? 0.5f : 1.0f);
2018-09-20 09:27:29 +00:00
boolean photo = false;
if (avatars && message.avatar != null) {
2018-09-08 17:04:10 +00:00
ContentResolver resolver = context.getContentResolver();
InputStream is = ContactsContract.Contacts.openContactPhotoInputStream(resolver, Uri.parse(message.avatar));
2018-09-20 09:27:29 +00:00
if (is != null) {
photo = true;
ivAvatar.setImageDrawable(Drawable.createFromStream(is, "avatar"));
}
2018-09-08 17:04:10 +00:00
}
2018-09-20 09:27:29 +00:00
ivAvatar.setVisibility(photo ? View.VISIBLE : View.GONE);
2018-09-08 14:57:03 +00:00
2018-09-12 16:54:48 +00:00
vwColor.setBackgroundColor(message.accountColor == null ? Color.TRANSPARENT : message.accountColor);
2018-10-12 06:33:43 +00:00
vwColor.setVisibility(viewType == ViewType.UNIFIED && message.accountColor != null ? View.VISIBLE : View.GONE);
2018-09-12 16:54:48 +00:00
2018-10-01 08:32:58 +00:00
if (viewType == ViewType.THREAD)
ivFlagged.setVisibility(message.unflagged == 1 ? View.GONE : View.VISIBLE);
else
ivFlagged.setVisibility(message.count - message.unflagged > 0 ? View.VISIBLE : View.GONE);
2018-09-07 15:12:43 +00:00
2018-08-09 07:02:41 +00:00
if (EntityFolder.DRAFTS.equals(message.folderType) ||
EntityFolder.OUTBOX.equals(message.folderType) ||
EntityFolder.SENT.equals(message.folderType)) {
2018-08-09 21:33:14 +00:00
tvFrom.setText(MessageHelper.getFormattedAddresses(message.to, false));
2018-08-09 05:37:52 +00:00
tvTime.setText(DateUtils.getRelativeTimeSpanString(context, message.sent == null ? message.received : message.sent));
2018-08-07 06:38:00 +00:00
} else {
2018-08-09 21:33:14 +00:00
tvFrom.setText(MessageHelper.getFormattedAddresses(message.from, false));
2018-08-09 05:37:52 +00:00
tvTime.setText(DateUtils.getRelativeTimeSpanString(context, message.received));
2018-08-07 06:38:00 +00:00
}
tvSize.setText(message.size == null ? null : Helper.humanReadableByteCount(message.size, true));
tvSize.setTypeface(null, message.content ? Typeface.NORMAL : Typeface.BOLD);
tvSize.setVisibility(message.size == null ? View.GONE : View.VISIBLE);
2018-08-23 06:57:24 +00:00
ivAttachments.setVisibility(message.attachments > 0 ? View.VISIBLE : View.GONE);
tvSubject.setText(message.subject);
2018-08-07 06:38:00 +00:00
2018-08-23 06:57:24 +00:00
if (viewType == ViewType.UNIFIED)
2018-08-22 16:37:10 +00:00
tvFolder.setText(message.accountName);
2018-08-23 06:57:24 +00:00
else if (viewType == ViewType.FOLDER)
2018-08-22 16:37:10 +00:00
tvFolder.setVisibility(View.GONE);
2018-09-19 17:47:03 +00:00
else {
String name = (message.folderDisplay == null
? Helper.localizeFolderName(context, message.folderName)
: message.folderDisplay);
tvFolder.setText(name);
}
2018-08-23 06:57:24 +00:00
2018-09-04 07:56:30 +00:00
if (viewType == ViewType.THREAD) {
2018-08-23 06:57:24 +00:00
tvCount.setVisibility(View.GONE);
2018-09-04 07:56:30 +00:00
ivThread.setVisibility(View.GONE);
} else {
2018-08-11 09:18:49 +00:00
tvCount.setText(Integer.toString(message.count));
2018-09-04 07:56:30 +00:00
ivThread.setVisibility(View.VISIBLE);
2018-09-28 15:33:35 +00:00
tvCount.setAlpha(message.threaded ? 1.0f : 0.5f);
ivThread.setAlpha(message.threaded ? 1.0f : 0.5f);
2018-08-09 05:37:52 +00:00
}
2018-08-07 06:38:00 +00:00
if (debug) {
DB db = DB.getInstance(context);
db.operation().getOperationsByMessage(message.id).removeObservers(owner);
db.operation().getOperationsByMessage(message.id).observe(owner, new Observer<List<EntityOperation>>() {
@Override
public void onChanged(List<EntityOperation> operations) {
String text = message.error +
"\n" + message.id + " " + df.format(new Date(message.received)) +
2018-08-12 15:31:43 +00:00
"\n" + (message.ui_hide ? "HIDDEN " : "") +
"seen=" + message.seen + "/" + message.ui_seen + "/" + message.unseen +
" " + message.uid + "/" + message.id +
"\n" + message.msgid;
2018-08-12 15:31:43 +00:00
if (operations != null)
for (EntityOperation op : operations)
2018-08-14 14:50:41 +00:00
text += "\n" + op.id + ":" + op.name + " " + df.format(new Date(op.created));
tvError.setText(text);
tvError.setVisibility(View.VISIBLE);
}
});
}
2018-08-11 09:18:49 +00:00
tvError.setText(message.error);
tvError.setVisibility(message.error == null ? View.GONE : View.VISIBLE);
2018-08-07 06:38:00 +00:00
2018-08-09 05:37:52 +00:00
int typeface = (message.unseen > 0 ? Typeface.BOLD : Typeface.NORMAL);
2018-08-07 06:38:00 +00:00
tvFrom.setTypeface(null, typeface);
tvTime.setTypeface(null, typeface);
tvSubject.setTypeface(null, typeface);
tvCount.setTypeface(null, typeface);
2018-08-09 05:37:52 +00:00
int colorUnseen = Helper.resolveColor(context, message.unseen > 0
2018-08-07 20:12:45 +00:00
? R.attr.colorUnread : android.R.attr.textColorSecondary);
tvFrom.setTextColor(colorUnseen);
tvTime.setTextColor(colorUnseen);
2018-08-07 06:38:00 +00:00
}
2018-08-02 13:33:06 +00:00
@Override
public void onClick(View view) {
int pos = getAdapterPosition();
if (pos == RecyclerView.NO_POSITION)
return;
TupleMessageEx message = getItem(pos);
2018-08-02 13:33:06 +00:00
2018-10-15 07:22:51 +00:00
Helper.hapticFeedback(itemView);
2018-08-09 20:45:42 +00:00
if (EntityFolder.DRAFTS.equals(message.folderType))
context.startActivity(
new Intent(context, ActivityCompose.class)
.putExtra("action", "edit")
2018-08-09 20:45:42 +00:00
.putExtra("id", message.id));
else {
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
lbm.sendBroadcast(
new Intent(ActivityView.ACTION_VIEW_MESSAGE)
2018-08-26 13:24:16 +00:00
.putExtra("message", message));
2018-08-09 20:45:42 +00:00
}
2018-08-02 13:33:06 +00:00
}
@Override
public boolean onLongClick(View view) {
int pos = getAdapterPosition();
if (pos == RecyclerView.NO_POSITION)
return false;
final TupleMessageEx message = getItem(pos);
2018-09-01 06:27:13 +00:00
PopupMenu popupMenu = new PopupMenu(context, itemView);
2018-09-28 14:05:19 +00:00
if (!message.threaded && !EntityFolder.OUTBOX.equals(message.folderType)) {
2018-09-23 04:28:13 +00:00
popupMenu.getMenu().add(Menu.NONE, action_flag, 1, message.ui_flagged ? R.string.title_unflag : R.string.title_flag);
popupMenu.getMenu().add(Menu.NONE, action_seen, 2, message.ui_seen ? R.string.title_unseen : R.string.title_seen);
}
2018-09-19 10:00:58 +00:00
if (EntityFolder.TRASH.equals(message.folderType))
popupMenu.getMenu().add(Menu.NONE, action_delete, 3, R.string.title_delete);
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
2018-09-01 06:27:13 +00:00
@Override
public boolean onMenuItemClick(MenuItem target) {
Bundle args = new Bundle();
args.putLong("id", message.id);
args.putInt("action", target.getItemId());
2018-09-19 10:00:58 +00:00
if (target.getItemId() == action_delete) {
2018-09-20 16:47:28 +00:00
new DialogBuilderLifecycle(context, owner)
2018-09-19 10:00:58 +00:00
.setMessage(R.string.title_ask_delete)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Bundle args = new Bundle();
args.putLong("id", message.id);
new SimpleTask<Void>() {
@Override
protected Void onLoad(Context context, Bundle args) {
long id = args.getLong("id");
DB db = DB.getInstance(context);
try {
db.beginTransaction();
EntityMessage message = db.message().getMessage(id);
db.message().setMessageUiHide(id, true);
EntityOperation.queue(db, message, EntityOperation.DELETE);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
EntityOperation.process(context);
return null;
}
@Override
protected void onException(Bundle args, Throwable ex) {
Helper.unexpectedError(context, ex);
}
}.load(context, owner, args);
}
2018-09-19 10:00:58 +00:00
})
.setNegativeButton(android.R.string.cancel, null)
.show();
} else
new SimpleTask<Void>() {
@Override
protected Void onLoad(final Context context, Bundle args) {
long id = args.getLong("id");
int action = args.getInt("action");
DB db = DB.getInstance(context);
try {
db.beginTransaction();
EntityMessage message = db.message().getMessage(id);
if (action == action_flag) {
db.message().setMessageUiFlagged(message.id, !message.ui_flagged);
EntityOperation.queue(db, message, EntityOperation.FLAG, !message.ui_flagged);
} else if (action == action_seen) {
db.message().setMessageUiSeen(message.id, !message.ui_seen);
EntityOperation.queue(db, message, EntityOperation.SEEN, !message.ui_seen);
}
2018-09-19 10:00:58 +00:00
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
2018-09-19 10:00:58 +00:00
EntityOperation.process(context);
2018-09-19 10:00:58 +00:00
return null;
}
2018-09-01 06:27:13 +00:00
2018-09-19 10:00:58 +00:00
@Override
public void onException(Bundle args, Throwable ex) {
Helper.unexpectedError(context, ex);
}
}.load(context, owner, args);
2018-09-01 06:27:13 +00:00
return true;
2018-09-01 06:27:13 +00:00
}
});
2018-09-01 06:27:13 +00:00
2018-09-23 04:28:13 +00:00
if (popupMenu.getMenu().hasVisibleItems())
popupMenu.show();
return true;
}
2018-08-02 13:33:06 +00:00
}
AdapterMessage(Context context, LifecycleOwner owner, ViewType viewType) {
2018-08-07 06:38:00 +00:00
super(DIFF_CALLBACK);
2018-08-02 13:33:06 +00:00
this.context = context;
this.owner = owner;
2018-08-02 13:33:06 +00:00
this.viewType = viewType;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
2018-09-08 17:04:10 +00:00
this.avatars = (prefs.getBoolean("avatars", true) &&
ContextCompat.checkSelfPermission(context, Manifest.permission.READ_CONTACTS)
== PackageManager.PERMISSION_GRANTED);
this.debug = prefs.getBoolean("debug", false);
2018-08-02 13:33:06 +00:00
}
2018-08-12 16:14:20 +00:00
private static final DiffUtil.ItemCallback<TupleMessageEx> DIFF_CALLBACK =
2018-08-07 06:38:00 +00:00
new DiffUtil.ItemCallback<TupleMessageEx>() {
@Override
public boolean areItemsTheSame(
@NonNull TupleMessageEx prev, @NonNull TupleMessageEx next) {
return prev.id.equals(next.id);
}
2018-08-02 13:33:06 +00:00
2018-08-07 06:38:00 +00:00
@Override
public boolean areContentsTheSame(
@NonNull TupleMessageEx prev, @NonNull TupleMessageEx next) {
return prev.equals(next);
}
};
2018-08-02 13:33:06 +00:00
@Override
@NonNull
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_message, parent, false));
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.unwire();
2018-08-07 06:38:00 +00:00
TupleMessageEx message = getItem(position);
if (message == null)
holder.clear();
else {
holder.bindTo(message);
holder.wire();
2018-08-02 13:33:06 +00:00
}
}
}