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

595 lines
24 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/>.
2024-01-01 07:50:49 +00:00
Copyright 2018-2024 by Marcel Bokhorst (M66B)
2019-03-14 18:47:10 +00:00
*/
2019-03-14 19:00:26 +00:00
import android.Manifest;
import android.content.ClipData;
import android.content.ClipboardManager;
2020-04-15 16:01:45 +00:00
import android.content.ContentResolver;
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-04-15 16:01:45 +00:00
import android.graphics.BitmapFactory;
2023-10-16 06:18:04 +00:00
import android.graphics.Rect;
2020-01-21 09:52:34 +00:00
import android.graphics.Typeface;
2019-03-14 18:47:10 +00:00
import android.net.Uri;
2020-06-25 10:37:25 +00:00
import android.os.Build;
2019-03-15 08:36:23 +00:00
import android.os.Bundle;
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 android.widget.Toast;
2019-03-14 18:47:10 +00:00
import androidx.annotation.NonNull;
import androidx.appcompat.widget.PopupMenu;
2020-03-22 12:43:12 +00:00
import androidx.core.content.pm.ShortcutInfoCompat;
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;
2020-04-15 16:01:45 +00:00
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.text.NumberFormat;
import java.util.ArrayList;
2022-03-27 09:54:17 +00:00
import java.util.Arrays;
import java.util.List;
import javax.mail.Address;
import javax.mail.internet.InternetAddress;
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;
2022-03-27 09:54:17 +00:00
private Long account;
2021-09-18 17:08:58 +00:00
private List<Integer> types = new ArrayList<>();
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);
}
2023-10-16 06:18:04 +00:00
Rect getItemRect() {
return new Rect(
super.itemView.getLeft(),
super.itemView.getTop(),
super.itemView.getRight(),
super.itemView.getBottom());
}
2019-03-15 08:36:23 +00:00
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) {
2020-09-28 07:18:56 +00:00
ivType.setImageResource(R.drawable.twotone_call_received_24);
ivType.setContentDescription(context.getString(R.string.title_accessibility_from));
} else if (contact.type == EntityContact.TYPE_TO) {
2020-09-28 07:18:56 +00:00
ivType.setImageResource(R.drawable.twotone_call_made_24);
ivType.setContentDescription(context.getString(R.string.title_accessibility_to));
2021-09-18 16:15:44 +00:00
} else if (contact.type == EntityContact.TYPE_JUNK) {
ivType.setImageResource(R.drawable.twotone_report_24);
ivType.setContentDescription(context.getString(R.string.title_legend_junk));
2021-09-04 15:28:15 +00:00
} else if (contact.type == EntityContact.TYPE_NO_JUNK) {
2022-09-01 06:38:29 +00:00
ivType.setImageResource(R.drawable.twotone_report_off_24);
2021-09-04 15:28:15 +00:00
ivType.setContentDescription(context.getString(R.string.title_no_junk));
} 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
2021-09-30 10:44:12 +00:00
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
ivType.setTooltipText(ivType.getContentDescription());
2019-03-14 19:00:26 +00:00
if (contact.avatar == null || !contacts)
2019-03-14 18:47:10 +00:00
ivAvatar.setImageDrawable(null);
2020-04-15 16:01:45 +00:00
else {
ContentResolver resolver = context.getContentResolver();
Uri lookupUri = Uri.parse(contact.avatar);
try (InputStream is = ContactsContract.Contacts.openContactPhotoInputStream(
resolver, lookupUri, false)) {
ivAvatar.setImageBitmap(BitmapFactory.decodeStream(is));
2019-05-13 14:38:39 +00:00
} catch (Throwable ex) {
Log.e(ex);
}
2020-04-15 16:01:45 +00:00
}
2019-03-14 18:47:10 +00:00
2022-03-27 08:21:06 +00:00
tvName.setText(contact.name == null ? "-" : contact.name + (contact.group == null ? "" : "/" + contact.group));
tvEmail.setText(contact.email + "/" + contact.accountName);
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
2021-09-19 21:07:06 +00:00
? R.drawable.baseline_star_24 : R.drawable.twotone_star_border_24);
2019-03-16 16:58:57 +00:00
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);
2023-01-24 19:29:49 +00:00
ivFavorite.setVisibility(contact.type == EntityContact.TYPE_JUNK ||
contact.type == EntityContact.TYPE_NO_JUNK
? View.GONE : View.VISIBLE);
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;
2021-10-30 16:52:01 +00:00
else if (contact.state == EntityContact.STATE_FAVORITE)
contact.state = EntityContact.STATE_IGNORE;
2019-03-16 16:58:57 +00:00
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
int order = 0;
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);
popupMenu.getMenu().add(Menu.NONE, 0, order++, ss).setEnabled(false);
2020-01-21 09:52:34 +00:00
2022-04-19 10:42:25 +00:00
if (!TextUtils.isEmpty(contact.identityEmail)) {
String via = context.getString(R.string.title_via, contact.identityEmail);
popupMenu.getMenu().add(Menu.NONE, 0, order++, via).setEnabled(false);
}
2023-01-24 19:29:49 +00:00
if (contact.state != EntityContact.STATE_IGNORE &&
contact.type != EntityContact.TYPE_JUNK &&
contact.type != EntityContact.TYPE_NO_JUNK)
popupMenu.getMenu().add(Menu.NONE, R.string.title_advanced_never_favorite, order++, R.string.title_advanced_never_favorite);
popupMenu.getMenu().add(Menu.NONE, R.string.title_share, order++, R.string.title_share); // should be system whitelisted
popupMenu.getMenu().add(Menu.NONE, R.string.title_clipboard_copy, order++, R.string.title_clipboard_copy); // should be system whitelisted
2021-04-12 05:56:00 +00:00
if (Shortcuts.can(context))
popupMenu.getMenu().add(Menu.NONE, R.string.title_pin, order++, R.string.title_pin);
2022-03-27 07:10:21 +00:00
popupMenu.getMenu().add(Menu.NONE, R.string.title_edit_contact, order++, R.string.title_edit_contact);
popupMenu.getMenu().add(Menu.NONE, R.string.title_search, order++, R.string.title_search);
popupMenu.getMenu().add(Menu.NONE, R.string.title_delete, order++, R.string.title_delete);
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
2019-03-15 08:41:22 +00:00
@Override
public boolean onMenuItemClick(MenuItem item) {
2021-02-05 10:15:02 +00:00
int itemId = item.getItemId();
if (itemId == R.string.title_advanced_never_favorite) {
onActionNeverFavorite();
return true;
} else if (itemId == R.string.title_share) {
onActionShare();
return true;
} else if (itemId == R.string.title_clipboard_copy) {
onActionCopy();
return true;
2021-02-05 10:15:02 +00:00
} else if (itemId == R.string.title_pin) {
onActionPin();
return true;
2022-03-27 07:10:21 +00:00
} else if (itemId == R.string.title_edit_contact) {
2021-02-05 10:15:02 +00:00
onActionEdit();
return true;
} else if (itemId == R.string.title_search) {
onActionSearch();
return true;
2021-02-05 10:15:02 +00:00
} else if (itemId == R.string.title_delete) {
onActionDelete();
return true;
}
2021-02-05 10:15:02 +00:00
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);
2022-01-16 07:43:56 +00:00
} catch (Throwable ex) {
Helper.reportNoViewer(context, share, ex);
2020-01-09 15:21:02 +00:00
}
}
private void onActionCopy() {
ClipboardManager clipboard = Helper.getSystemService(context, ClipboardManager.class);
if (clipboard == null)
return;
String copy;
try {
InternetAddress address = new InternetAddress(contact.email, contact.name, StandardCharsets.UTF_8.name());
copy = MessageHelper.formatAddresses(new Address[]{address});
} catch (UnsupportedEncodingException ex) {
Log.e(ex);
copy = contact.email;
}
ClipData clip = ClipData.newPlainText(context.getString(R.string.app_name), copy);
clipboard.setPrimaryClip(clip);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU)
ToastEx.makeText(context, R.string.title_clipboard_copied, Toast.LENGTH_LONG).show();
}
2020-03-22 12:43:12 +00:00
private void onActionPin() {
ShortcutInfoCompat.Builder builder = Shortcuts.getShortcut(context, contact);
2022-04-14 05:57:34 +00:00
Shortcuts.requestPinShortcut(context, builder.build());
2020-03-22 12:43:12 +00:00
}
2020-10-02 20:13:00 +00:00
private void onActionEdit() {
Bundle args = new Bundle();
args.putLong("id", contact.id);
2022-03-27 07:10:21 +00:00
args.putLong("account", contact.account);
args.putInt("type", contact.type);
args.putString("email", contact.email);
2020-10-02 20:13:00 +00:00
args.putString("name", contact.name);
2022-03-27 08:21:06 +00:00
args.putString("group", contact.group);
2020-10-02 20:13:00 +00:00
2023-03-14 07:22:43 +00:00
FragmentDialogContactEdit fragment = new FragmentDialogContactEdit();
2020-10-02 20:13:00 +00:00
fragment.setArguments(args);
2022-03-27 07:10:21 +00:00
fragment.setTargetFragment(parentFragment, FragmentContacts.REQUEST_EDIT_CONTACT);
fragment.show(parentFragment.getParentFragmentManager(), "contact:edit");
2020-10-02 20:13:00 +00:00
}
private void onActionSearch() {
2022-08-27 07:39:14 +00:00
Intent search = new Intent(context, ActivityView.class)
.putExtra(Intent.EXTRA_PROCESS_TEXT, contact.email)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(search);
}
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);
2023-07-21 05:16:41 +00:00
this.colorAccent = Helper.resolveColor(context, androidx.appcompat.R.attr.colorAccent);
2019-03-15 08:36:23 +00:00
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;
2021-09-28 08:18:20 +00:00
owner.getLifecycle().removeObserver(this);
}
});
2019-03-14 18:47:10 +00:00
}
2019-03-17 08:26:17 +00:00
public void set(@NonNull List<TupleContactEx> contacts) {
2021-09-18 17:08:58 +00:00
Log.i("Set contacts=" + contacts.size() +
2022-03-27 09:54:17 +00:00
" search=" + search +
" account=" + account +
" types=" + types.size());
2019-03-14 18:47:10 +00:00
2019-03-16 18:10:56 +00:00
all = contacts;
2021-09-29 06:27:29 +00:00
new SimpleTask<List<TupleContactEx>>() {
@Override
2022-03-27 09:54:17 +00:00
protected List<TupleContactEx> onExecute(Context context, Bundle args) {
2021-09-29 06:27:29 +00:00
List<TupleContactEx> filtered;
2022-03-27 09:54:17 +00:00
if (account == null && types.size() == 0)
2021-09-29 06:27:29 +00:00
filtered = contacts;
else {
filtered = new ArrayList<>();
for (TupleContactEx contact : contacts)
2022-03-27 09:54:17 +00:00
if ((account == null || contact.account.equals(account)) &&
(types.size() == 0 || types.contains(contact.type)))
2021-09-29 06:27:29 +00:00
filtered.add(contact);
}
2019-03-14 18:47:10 +00:00
2021-09-29 06:27:29 +00:00
List<TupleContactEx> items;
if (TextUtils.isEmpty(search))
items = filtered;
else {
items = new ArrayList<>();
String query = search.toLowerCase().trim();
for (TupleContactEx contact : filtered)
if (contact.email.toLowerCase().contains(query) ||
(contact.name != null && contact.name.toLowerCase().contains(query)))
items.add(contact);
}
2019-03-14 18:47:10 +00:00
2021-09-29 06:27:29 +00:00
return items;
2019-03-14 18:47:10 +00:00
}
@Override
2021-09-29 06:27:29 +00:00
protected void onExecuted(Bundle args, List<TupleContactEx> items) {
DiffUtil.DiffResult diff = DiffUtil.calculateDiff(new DiffCallback(selected, items), false);
2019-03-14 18:47:10 +00:00
2021-09-29 06:27:29 +00:00
selected = items;
diff.dispatchUpdatesTo(new ListUpdateCallback() {
@Override
public void onInserted(int position, int count) {
Log.d("Inserted @" + position + " #" + count);
}
@Override
public void onRemoved(int position, int count) {
Log.d("Removed @" + position + " #" + count);
}
@Override
public void onMoved(int fromPosition, int toPosition) {
Log.d("Moved " + fromPosition + ">" + toPosition);
}
@Override
public void onChanged(int position, int count, Object payload) {
Log.d("Changed @" + position + " #" + count);
}
});
2022-03-23 17:19:14 +00:00
try {
diff.dispatchUpdatesTo(AdapterContact.this);
} catch (Throwable ex) {
Log.e(ex);
}
2019-03-14 18:47:10 +00:00
}
@Override
2021-09-29 06:27:29 +00:00
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(parentFragment.getParentFragmentManager(), ex);
2019-03-14 18:47:10 +00:00
}
2022-12-13 09:52:39 +00:00
}.serial().execute(context, owner, new Bundle(), "contacts:filter");
2019-03-14 18:47:10 +00:00
}
2019-03-16 18:10:56 +00:00
public void search(String query) {
2021-03-27 07:14:21 +00:00
Log.i("Contacts query=" + query);
2019-03-16 18:10:56 +00:00
search = query;
set(all);
}
2022-03-27 09:54:17 +00:00
public void filter(Long account, boolean junk) {
this.account = account;
this.types = junk
? Arrays.asList(EntityContact.TYPE_JUNK, EntityContact.TYPE_NO_JUNK)
2022-03-27 13:36:20 +00:00
: Arrays.asList(EntityContact.TYPE_TO, EntityContact.TYPE_FROM);
2021-09-18 17:08:58 +00:00
set(all);
}
2020-12-30 18:29:20 +00:00
private static 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) {
2023-10-15 07:07:24 +00:00
if (position < 0 || position >= selected.size())
return -1L;
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-17 08:26:17 +00:00
TupleContactEx contact = selected.get(position);
2020-11-12 07:07:30 +00:00
holder.powner.recreate(contact == null ? null : contact.id);
holder.unwire();
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
}
}