FairEmail/app/src/main/java/eu/faircode/email/AdapterContact.java

462 lines
18 KiB
Java
Raw Normal View History

2019-03-14 18:47:10 +00:00
package eu.faircode.email;
/*
This file is part of FairEmail.
FairEmail is free software: you can redistribute it and/or modify
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.
FairEmail 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 FairEmail. If not, see <http://www.gnu.org/licenses/>.
2020-01-05 17:32:53 +00:00
Copyright 2018-2020 by Marcel Bokhorst (M66B)
2019-03-14 18:47:10 +00:00
*/
2019-03-14 19:00:26 +00:00
import android.Manifest;
2019-03-14 18:47:10 +00:00
import android.content.Context;
2020-01-09 15:21:02 +00:00
import android.content.Intent;
2019-03-15 08:36:23 +00:00
import android.content.res.ColorStateList;
2020-01-21 09:52:34 +00:00
import android.graphics.Typeface;
2019-03-14 18:47:10 +00:00
import android.net.Uri;
2019-03-15 08:36:23 +00:00
import android.os.Bundle;
2019-06-29 07:23:50 +00:00
import android.os.ParcelFileDescriptor;
2020-01-09 15:21:02 +00:00
import android.provider.ContactsContract;
2020-01-21 09:52:34 +00:00
import android.text.SpannableString;
2019-03-16 18:10:56 +00:00
import android.text.TextUtils;
2020-01-21 10:41:07 +00:00
import android.text.style.RelativeSizeSpan;
2020-01-21 09:52:34 +00:00
import android.text.style.StyleSpan;
2019-03-14 18:47:10 +00:00
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
2019-03-14 18:47:10 +00:00
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.widget.PopupMenu;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
2019-03-15 08:36:23 +00:00
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.OnLifecycleEvent;
2019-03-14 18:47:10 +00:00
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListUpdateCallback;
import androidx.recyclerview.widget.RecyclerView;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
2019-03-14 18:47:10 +00:00
public class AdapterContact extends RecyclerView.Adapter<AdapterContact.ViewHolder> {
private Fragment parentFragment;
2019-03-14 18:47:10 +00:00
private Context context;
2019-03-15 08:36:23 +00:00
private LifecycleOwner owner;
2019-03-14 18:47:10 +00:00
private LayoutInflater inflater;
2019-03-14 19:00:26 +00:00
private boolean contacts;
2019-03-15 08:36:23 +00:00
private int colorAccent;
private int textColorSecondary;
2019-03-14 18:47:10 +00:00
2019-03-16 18:10:56 +00:00
private String search = null;
2019-03-17 08:26:17 +00:00
private List<TupleContactEx> all = new ArrayList<>();
private List<TupleContactEx> selected = new ArrayList<>();
2019-03-14 18:47:10 +00:00
2019-07-22 07:52:08 +00:00
private NumberFormat NF = NumberFormat.getNumberInstance();
2019-03-14 19:00:26 +00:00
2019-03-15 08:41:22 +00:00
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
2019-03-15 11:31:26 +00:00
private View view;
2019-03-14 18:47:10 +00:00
private ImageView ivType;
private ImageView ivAvatar;
private TextView tvName;
private TextView tvEmail;
private TextView tvTimes;
private TextView tvLast;
2019-03-15 08:36:23 +00:00
private ImageView ivFavorite;
2019-03-14 18:47:10 +00:00
private TwoStateOwner powner = new TwoStateOwner(owner, "ContactPopup");
2019-03-14 18:47:10 +00:00
ViewHolder(View itemView) {
super(itemView);
2019-03-15 11:31:26 +00:00
view = itemView.findViewById(R.id.clItem);
2019-03-14 18:47:10 +00:00
ivType = itemView.findViewById(R.id.ivType);
ivAvatar = itemView.findViewById(R.id.ivAvatar);
tvName = itemView.findViewById(R.id.tvName);
tvEmail = itemView.findViewById(R.id.tvEmail);
tvTimes = itemView.findViewById(R.id.tvTimes);
tvLast = itemView.findViewById(R.id.tvLast);
2019-03-15 08:36:23 +00:00
ivFavorite = itemView.findViewById(R.id.ivFavorite);
}
private void wire() {
2019-03-15 11:31:26 +00:00
view.setOnClickListener(this);
2019-03-16 16:58:57 +00:00
view.setOnLongClickListener(this);
2019-03-15 08:36:23 +00:00
}
private void unwire() {
2019-03-15 11:31:26 +00:00
view.setOnClickListener(null);
2019-03-16 16:58:57 +00:00
view.setOnLongClickListener(null);
2019-03-14 18:47:10 +00:00
}
2019-03-17 08:26:17 +00:00
private void bindTo(TupleContactEx contact) {
2019-03-16 16:58:57 +00:00
view.setAlpha(contact.state == EntityContact.STATE_IGNORE ? Helper.LOW_LIGHT : 1.0f);
2019-03-15 11:31:26 +00:00
if (contact.type == EntityContact.TYPE_FROM) {
2019-03-16 16:14:49 +00:00
ivType.setImageResource(R.drawable.baseline_call_received_24);
ivType.setContentDescription(context.getString(R.string.title_accessibility_from));
} else if (contact.type == EntityContact.TYPE_TO) {
2019-03-16 16:14:49 +00:00
ivType.setImageResource(R.drawable.baseline_call_made_24);
ivType.setContentDescription(context.getString(R.string.title_accessibility_to));
} else {
2019-03-14 18:47:10 +00:00
ivType.setImageDrawable(null);
ivType.setContentDescription(null);
2020-01-08 07:31:34 +00:00
}
2019-03-14 18:47:10 +00:00
2019-03-14 19:00:26 +00:00
if (contact.avatar == null || !contacts)
2019-03-14 18:47:10 +00:00
ivAvatar.setImageDrawable(null);
else
2019-05-13 14:38:39 +00:00
try {
2019-06-29 07:23:50 +00:00
Uri uri = Uri.parse(contact.avatar + "/photo");
2019-05-13 14:38:39 +00:00
/*
java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.FileDescriptor android.content.res.AssetFileDescriptor.getFileDescriptor()' on a null object reference
java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.FileDescriptor android.content.res.AssetFileDescriptor.getFileDescriptor()' on a null object reference
at android.graphics.ImageDecoder$ContentResolverSource.createImageDecoder(ImageDecoder.java:286)
at android.graphics.ImageDecoder.decodeDrawableImpl(ImageDecoder.java:1652)
at android.graphics.ImageDecoder.decodeDrawable(ImageDecoder.java:1645)
at android.widget.ImageView.getDrawableFromUri(ImageView.java:952)
at android.widget.ImageView.resolveUri(ImageView.java:921)
2019-06-29 07:23:50 +00:00
2019-05-13 14:38:39 +00:00
at android.widget.ImageView.setImageURI(ImageView.java:532)
at androidx.appcompat.widget.AppCompatImageView.setImageURI(SourceFile:116)
2019-06-29 07:23:50 +00:00
at android.widget.ImageView.onMeasure(ImageView.java:1056)
at android.view.View.measure(View.java:23188)
at androidx.constraintlayout.widget.ConstraintLayout$Measurer.measure(SourceFile:806)
2019-05-13 14:38:39 +00:00
*/
2019-06-29 07:23:50 +00:00
ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(uri, "r");
if (pfd == null)
2019-12-24 09:24:53 +00:00
throw new IllegalArgumentException("Contact inaccessible");
2019-06-29 07:23:50 +00:00
pfd.close();
ivAvatar.setImageURI(uri);
2019-05-13 14:38:39 +00:00
} catch (Throwable ex) {
Log.e(ex);
2019-06-29 07:23:50 +00:00
ivAvatar.setImageResource(R.drawable.baseline_broken_image_24);
2019-05-13 14:38:39 +00:00
}
2019-03-14 18:47:10 +00:00
2019-03-14 19:00:26 +00:00
tvName.setText(contact.name == null ? contact.email : contact.name);
2019-03-17 08:26:17 +00:00
tvEmail.setText(contact.accountName + "/" + contact.email);
2019-07-22 07:52:08 +00:00
tvTimes.setText(NF.format(contact.times_contacted));
2019-03-14 19:00:26 +00:00
tvLast.setText(contact.last_contacted == null ? null
: Helper.getRelativeTimeSpanString(context, contact.last_contacted));
2019-03-15 08:36:23 +00:00
2019-03-16 16:58:57 +00:00
ivFavorite.setImageResource(contact.state == EntityContact.STATE_FAVORITE
? R.drawable.baseline_star_24 : R.drawable.baseline_star_border_24);
ivFavorite.setImageTintList(ColorStateList.valueOf(
contact.state == EntityContact.STATE_FAVORITE ? colorAccent : textColorSecondary));
2020-01-09 13:17:54 +00:00
ivFavorite.setContentDescription(contact.state == EntityContact.STATE_FAVORITE
? context.getString(R.string.title_accessibility_flagged) : null);
2019-03-15 11:31:26 +00:00
view.requestLayout();
2019-03-15 08:36:23 +00:00
}
@Override
public void onClick(View view) {
int pos = getAdapterPosition();
if (pos == RecyclerView.NO_POSITION)
return;
2019-03-17 08:26:17 +00:00
TupleContactEx contact = selected.get(pos);
2019-03-16 16:58:57 +00:00
if (contact.state == EntityContact.STATE_DEFAULT)
contact.state = EntityContact.STATE_FAVORITE;
else
contact.state = EntityContact.STATE_DEFAULT;
2019-03-15 11:31:26 +00:00
notifyItemChanged(pos);
2019-03-15 08:36:23 +00:00
Bundle args = new Bundle();
args.putLong("id", contact.id);
2019-03-15 11:31:26 +00:00
args.putInt("state", contact.state);
2019-03-15 08:36:23 +00:00
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) {
long id = args.getLong("id");
2019-03-15 11:31:26 +00:00
int state = args.getInt("state");
2019-03-15 08:36:23 +00:00
DB db = DB.getInstance(context);
2019-03-15 11:31:26 +00:00
db.contact().setContactState(id, state);
2019-03-15 08:36:23 +00:00
return null;
}
@Override
protected void onExecuted(Bundle args, Void data) {
Shortcuts.update(context, owner);
}
@Override
protected void onException(Bundle args, Throwable ex) {
2019-12-06 07:50:46 +00:00
Log.unexpectedError(parentFragment.getParentFragmentManager(), ex);
2019-03-15 08:36:23 +00:00
}
2019-03-15 11:31:26 +00:00
}.execute(context, owner, args, "contact:state");
2019-03-14 18:47:10 +00:00
}
2019-03-15 08:41:22 +00:00
@Override
public boolean onLongClick(View view) {
int pos = getAdapterPosition();
if (pos == RecyclerView.NO_POSITION)
return false;
2020-01-09 15:21:02 +00:00
final TupleContactEx contact = selected.get(pos);
final Intent share = new Intent(Intent.ACTION_INSERT);
share.setType(ContactsContract.Contacts.CONTENT_TYPE);
share.putExtra(ContactsContract.Intents.Insert.NAME, contact.name);
share.putExtra(ContactsContract.Intents.Insert.EMAIL, contact.email);
2019-03-15 08:41:22 +00:00
PopupMenuLifecycle popupMenu = new PopupMenuLifecycle(context, powner, view);
2019-03-15 08:41:22 +00:00
2020-01-21 09:52:34 +00:00
SpannableString ss = new SpannableString(contact.email);
ss.setSpan(new StyleSpan(Typeface.ITALIC), 0, ss.length(), 0);
2020-01-21 10:41:07 +00:00
ss.setSpan(new RelativeSizeSpan(0.9f), 0, ss.length(), 0);
2020-01-21 09:52:34 +00:00
popupMenu.getMenu().add(Menu.NONE, 0, 0, ss).setEnabled(false);
if (contact.state != EntityContact.STATE_IGNORE)
popupMenu.getMenu().add(Menu.NONE, R.string.title_advanced_never_favorite, 1, R.string.title_advanced_never_favorite);
2020-01-09 15:21:02 +00:00
if (share.resolveActivity(context.getPackageManager()) != null)
popupMenu.getMenu().add(Menu.NONE, R.string.title_share, 2, R.string.title_share);
popupMenu.getMenu().add(Menu.NONE, R.string.title_delete, 3, R.string.title_delete);
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
2019-03-15 08:41:22 +00:00
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.string.title_advanced_never_favorite:
onActionNeverFavorite();
return true;
2020-01-09 15:21:02 +00:00
case R.string.title_share:
onActionShare();
return true;
case R.string.title_delete:
onActionDelete();
return true;
default:
return false;
}
}
2019-03-15 08:41:22 +00:00
private void onActionNeverFavorite() {
Bundle args = new Bundle();
args.putLong("id", contact.id);
2019-03-15 08:41:22 +00:00
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) {
long id = args.getLong("id");
2019-03-15 08:41:22 +00:00
DB db = DB.getInstance(context);
db.contact().setContactState(id, EntityContact.STATE_IGNORE);
return null;
}
@Override
protected void onExecuted(Bundle args, Void data) {
Shortcuts.update(context, owner);
}
@Override
protected void onException(Bundle args, Throwable ex) {
2019-12-06 07:50:46 +00:00
Log.unexpectedError(parentFragment.getParentFragmentManager(), ex);
}
}.execute(context, owner, args, "contact:favorite");
2019-03-15 08:41:22 +00:00
}
2020-01-09 15:21:02 +00:00
private void onActionShare() {
try {
context.startActivity(share);
} catch (Throwable ex) {
Log.unexpectedError(parentFragment.getParentFragmentManager(), ex);
}
}
private void onActionDelete() {
Bundle args = new Bundle();
args.putLong("id", contact.id);
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) {
long id = args.getLong("id");
DB db = DB.getInstance(context);
db.contact().deleteContact(id);
return null;
}
@Override
protected void onExecuted(Bundle args, Void data) {
Shortcuts.update(context, owner);
}
@Override
protected void onException(Bundle args, Throwable ex) {
2019-12-06 07:50:46 +00:00
Log.unexpectedError(parentFragment.getParentFragmentManager(), ex);
}
}.execute(context, owner, args, "contact:delete");
2019-03-15 08:41:22 +00:00
}
});
popupMenu.show();
2019-03-15 08:41:22 +00:00
return true;
}
2019-03-14 18:47:10 +00:00
}
AdapterContact(Fragment parentFragment) {
this.parentFragment = parentFragment;
this.context = parentFragment.getContext();
this.owner = parentFragment.getViewLifecycleOwner();
2019-03-14 18:47:10 +00:00
this.inflater = LayoutInflater.from(context);
2019-03-14 19:00:26 +00:00
this.contacts = Helper.hasPermission(context, Manifest.permission.READ_CONTACTS);
2019-03-15 08:36:23 +00:00
this.colorAccent = Helper.resolveColor(context, R.attr.colorAccent);
this.textColorSecondary = Helper.resolveColor(context, android.R.attr.textColorSecondary);
2019-03-14 18:47:10 +00:00
setHasStableIds(true);
owner.getLifecycle().addObserver(new LifecycleObserver() {
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void onDestroyed() {
2019-12-07 16:02:42 +00:00
Log.d(AdapterContact.this + " parent destroyed");
AdapterContact.this.parentFragment = null;
}
});
2019-03-14 18:47:10 +00:00
}
2019-03-17 08:26:17 +00:00
public void set(@NonNull List<TupleContactEx> contacts) {
2019-03-14 18:47:10 +00:00
Log.i("Set contacts=" + contacts.size());
2019-03-16 18:10:56 +00:00
all = contacts;
2019-03-17 08:26:17 +00:00
List<TupleContactEx> items;
2019-03-16 18:10:56 +00:00
if (TextUtils.isEmpty(search))
items = all;
else {
items = new ArrayList<>();
2020-02-15 14:53:11 +00:00
String query = search.toLowerCase().trim();
2019-03-17 08:26:17 +00:00
for (TupleContactEx contact : contacts)
2020-02-15 14:53:11 +00:00
if (contact.email.toLowerCase().contains(query) ||
2019-03-16 18:10:56 +00:00
(contact.name != null && contact.name.toLowerCase().contains(query)))
items.add(contact);
}
DiffUtil.DiffResult diff = DiffUtil.calculateDiff(new DiffCallback(selected, items), false);
2019-03-14 18:47:10 +00:00
2019-03-16 18:10:56 +00:00
selected = items;
2019-03-14 18:47:10 +00:00
diff.dispatchUpdatesTo(new ListUpdateCallback() {
@Override
public void onInserted(int position, int count) {
2019-12-07 16:02:42 +00:00
Log.d("Inserted @" + position + " #" + count);
2019-03-14 18:47:10 +00:00
}
@Override
public void onRemoved(int position, int count) {
2019-12-07 16:02:42 +00:00
Log.d("Removed @" + position + " #" + count);
2019-03-14 18:47:10 +00:00
}
@Override
public void onMoved(int fromPosition, int toPosition) {
2019-12-07 16:02:42 +00:00
Log.d("Moved " + fromPosition + ">" + toPosition);
2019-03-14 18:47:10 +00:00
}
@Override
public void onChanged(int position, int count, Object payload) {
2019-12-07 16:02:42 +00:00
Log.d("Changed @" + position + " #" + count);
2019-03-14 18:47:10 +00:00
}
});
2019-03-15 11:53:22 +00:00
diff.dispatchUpdatesTo(this);
2019-03-14 18:47:10 +00:00
}
2019-03-16 18:10:56 +00:00
public void search(String query) {
search = query;
set(all);
}
2019-03-14 18:47:10 +00:00
private class DiffCallback extends DiffUtil.Callback {
2019-03-17 08:26:17 +00:00
private List<TupleContactEx> prev = new ArrayList<>();
private List<TupleContactEx> next = new ArrayList<>();
2019-03-14 18:47:10 +00:00
2019-03-17 08:26:17 +00:00
DiffCallback(List<TupleContactEx> prev, List<TupleContactEx> next) {
2019-03-15 11:31:26 +00:00
this.prev.addAll(prev);
this.next.addAll(next);
2019-03-14 18:47:10 +00:00
}
@Override
public int getOldListSize() {
return prev.size();
}
@Override
public int getNewListSize() {
return next.size();
}
@Override
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
2019-03-17 08:26:17 +00:00
TupleContactEx c1 = prev.get(oldItemPosition);
TupleContactEx c2 = next.get(newItemPosition);
2019-03-14 18:47:10 +00:00
return c1.id.equals(c2.id);
}
@Override
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
2019-03-17 08:26:17 +00:00
TupleContactEx c1 = prev.get(oldItemPosition);
TupleContactEx c2 = next.get(newItemPosition);
2019-03-14 18:47:10 +00:00
return c1.equals(c2);
}
}
@Override
public long getItemId(int position) {
2019-03-16 18:10:56 +00:00
return selected.get(position).id;
2019-03-14 18:47:10 +00:00
}
@Override
public int getItemCount() {
2019-03-16 18:10:56 +00:00
return selected.size();
2019-03-14 18:47:10 +00:00
}
@Override
@NonNull
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ViewHolder(inflater.inflate(R.layout.item_contact, parent, false));
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
2019-03-15 08:36:23 +00:00
holder.unwire();
2019-03-17 08:26:17 +00:00
TupleContactEx contact = selected.get(position);
2019-03-14 18:47:10 +00:00
holder.bindTo(contact);
2019-03-15 08:36:23 +00:00
holder.wire();
2019-03-14 18:47:10 +00:00
}
@Override
2019-09-21 18:28:03 +00:00
public void onViewDetachedFromWindow(@NonNull ViewHolder holder) {
holder.powner.recreate();
}
2019-03-14 18:47:10 +00:00
}