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/>.
|
|
|
|
|
|
|
|
Copyright 2018-2019 by Marcel Bokhorst (M66B)
|
|
|
|
*/
|
|
|
|
|
2019-03-14 19:00:26 +00:00
|
|
|
import android.Manifest;
|
2019-03-14 18:47:10 +00:00
|
|
|
import android.content.Context;
|
2019-03-15 08:36:23 +00:00
|
|
|
import android.content.res.ColorStateList;
|
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-03-14 18:47:10 +00:00
|
|
|
import android.text.format.DateUtils;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
2019-03-14 19:00:26 +00:00
|
|
|
import java.text.NumberFormat;
|
2019-03-14 18:47:10 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
2019-03-15 08:36:23 +00:00
|
|
|
import androidx.lifecycle.LifecycleOwner;
|
2019-03-14 18:47:10 +00:00
|
|
|
import androidx.recyclerview.widget.DiffUtil;
|
|
|
|
import androidx.recyclerview.widget.ListUpdateCallback;
|
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
|
|
|
|
public class AdapterContact extends RecyclerView.Adapter<AdapterContact.ViewHolder> {
|
|
|
|
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-15 11:31:26 +00:00
|
|
|
private List<EntityContact> items = new ArrayList<>();
|
2019-03-14 18:47:10 +00:00
|
|
|
|
2019-03-14 19:00:26 +00:00
|
|
|
private static NumberFormat nf = NumberFormat.getNumberInstance();
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
private void bindTo(EntityContact 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
|
|
|
|
2019-03-14 18:47:10 +00:00
|
|
|
if (contact.type == EntityContact.TYPE_FROM)
|
2019-03-16 16:14:49 +00:00
|
|
|
ivType.setImageResource(R.drawable.baseline_call_received_24);
|
2019-03-14 18:47:10 +00:00
|
|
|
else if (contact.type == EntityContact.TYPE_TO)
|
2019-03-16 16:14:49 +00:00
|
|
|
ivType.setImageResource(R.drawable.baseline_call_made_24);
|
2019-03-14 18:47:10 +00:00
|
|
|
else
|
|
|
|
ivType.setImageDrawable(null);
|
|
|
|
|
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
|
|
|
|
ivAvatar.setImageURI(Uri.parse(contact.avatar + "/photo"));
|
|
|
|
|
2019-03-14 19:00:26 +00:00
|
|
|
tvName.setText(contact.name == null ? contact.email : contact.name);
|
2019-03-14 18:47:10 +00:00
|
|
|
tvEmail.setText(contact.email);
|
2019-03-14 19:00:26 +00:00
|
|
|
tvTimes.setText(nf.format(contact.times_contacted));
|
|
|
|
tvLast.setText(contact.last_contacted == null ? null
|
2019-03-14 18:47:10 +00:00
|
|
|
: DateUtils.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));
|
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-15 11:31:26 +00:00
|
|
|
EntityContact contact = items.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) {
|
|
|
|
Helper.unexpectedError(context, owner, ex);
|
|
|
|
}
|
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;
|
|
|
|
|
2019-03-15 11:31:26 +00:00
|
|
|
EntityContact contact = items.get(pos);
|
2019-03-15 08:41:22 +00:00
|
|
|
|
|
|
|
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);
|
2019-03-16 16:58:57 +00:00
|
|
|
db.contact().setContactState(id, EntityContact.STATE_IGNORE);
|
2019-03-15 08:41:22 +00:00
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onExecuted(Bundle args, Void data) {
|
|
|
|
Shortcuts.update(context, owner);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onException(Bundle args, Throwable ex) {
|
|
|
|
Helper.unexpectedError(context, owner, ex);
|
|
|
|
}
|
|
|
|
}.execute(context, owner, args, "contact:delete");
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2019-03-14 18:47:10 +00:00
|
|
|
}
|
|
|
|
|
2019-03-15 08:36:23 +00:00
|
|
|
AdapterContact(Context context, LifecycleOwner owner) {
|
2019-03-14 18:47:10 +00:00
|
|
|
this.context = context;
|
2019-03-15 08:36:23 +00:00
|
|
|
this.owner = owner;
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void set(@NonNull List<EntityContact> contacts) {
|
|
|
|
Log.i("Set contacts=" + contacts.size());
|
|
|
|
|
2019-03-15 11:53:22 +00:00
|
|
|
DiffUtil.DiffResult diff = DiffUtil.calculateDiff(new DiffCallback(items, contacts), false);
|
2019-03-14 18:47:10 +00:00
|
|
|
|
2019-03-15 11:31:26 +00:00
|
|
|
items = contacts;
|
2019-03-14 18:47:10 +00:00
|
|
|
|
|
|
|
diff.dispatchUpdatesTo(new ListUpdateCallback() {
|
|
|
|
@Override
|
|
|
|
public void onInserted(int position, int count) {
|
|
|
|
Log.i("Inserted @" + position + " #" + count);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRemoved(int position, int count) {
|
|
|
|
Log.i("Removed @" + position + " #" + count);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onMoved(int fromPosition, int toPosition) {
|
|
|
|
Log.i("Moved " + fromPosition + ">" + toPosition);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onChanged(int position, int count, Object payload) {
|
|
|
|
Log.i("Changed @" + position + " #" + count);
|
|
|
|
}
|
|
|
|
});
|
2019-03-15 11:53:22 +00:00
|
|
|
diff.dispatchUpdatesTo(this);
|
2019-03-14 18:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private class DiffCallback extends DiffUtil.Callback {
|
2019-03-15 11:31:26 +00:00
|
|
|
private List<EntityContact> prev = new ArrayList<>();
|
|
|
|
private List<EntityContact> next = new ArrayList<>();
|
2019-03-14 18:47:10 +00:00
|
|
|
|
|
|
|
DiffCallback(List<EntityContact> prev, List<EntityContact> 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) {
|
|
|
|
EntityContact c1 = prev.get(oldItemPosition);
|
|
|
|
EntityContact c2 = next.get(newItemPosition);
|
|
|
|
return c1.id.equals(c2.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
|
|
|
|
EntityContact c1 = prev.get(oldItemPosition);
|
|
|
|
EntityContact c2 = next.get(newItemPosition);
|
|
|
|
return c1.equals(c2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public long getItemId(int position) {
|
2019-03-15 11:31:26 +00:00
|
|
|
return items.get(position).id;
|
2019-03-14 18:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemCount() {
|
2019-03-15 11:31:26 +00:00
|
|
|
return items.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-15 11:31:26 +00:00
|
|
|
EntityContact contact = items.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
|
|
|
}
|
|
|
|
}
|