2018-09-03 19:11:16 +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.
|
|
|
|
|
2018-10-29 10:46:49 +00:00
|
|
|
FairEmail is distributed in the hope that it will be useful,
|
2018-09-03 19:11:16 +00:00
|
|
|
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
|
2018-10-29 10:46:49 +00:00
|
|
|
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
|
2018-09-03 19:11:16 +00:00
|
|
|
|
2021-01-01 07:56:36 +00:00
|
|
|
Copyright 2018-2021 by Marcel Bokhorst (M66B)
|
2018-09-03 19:11:16 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
import android.content.Context;
|
2021-08-15 18:46:28 +00:00
|
|
|
import android.text.SpannableStringBuilder;
|
|
|
|
import android.text.style.ForegroundColorSpan;
|
2018-09-03 19:11:16 +00:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
2021-08-16 14:05:30 +00:00
|
|
|
import androidx.core.content.ContextCompat;
|
2019-07-01 07:30:07 +00:00
|
|
|
import androidx.fragment.app.Fragment;
|
|
|
|
import androidx.lifecycle.Lifecycle;
|
|
|
|
import androidx.lifecycle.LifecycleObserver;
|
|
|
|
import androidx.lifecycle.LifecycleOwner;
|
|
|
|
import androidx.lifecycle.OnLifecycleEvent;
|
2018-09-03 19:11:16 +00:00
|
|
|
import androidx.recyclerview.widget.DiffUtil;
|
|
|
|
import androidx.recyclerview.widget.ListUpdateCallback;
|
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
|
2019-04-17 18:21:44 +00:00
|
|
|
import java.text.DateFormat;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2018-09-03 19:11:16 +00:00
|
|
|
public class AdapterLog extends RecyclerView.Adapter<AdapterLog.ViewHolder> {
|
2019-07-01 07:30:07 +00:00
|
|
|
private Fragment parentFragment;
|
2018-09-03 19:11:16 +00:00
|
|
|
private Context context;
|
2019-07-01 07:30:07 +00:00
|
|
|
private LifecycleOwner owner;
|
2018-12-24 13:41:49 +00:00
|
|
|
private LayoutInflater inflater;
|
2018-09-03 19:11:16 +00:00
|
|
|
|
2021-08-15 18:46:28 +00:00
|
|
|
private int textColorSecondary;
|
2021-08-16 14:05:30 +00:00
|
|
|
private int solarizedYellow;
|
|
|
|
private int solarizedOrange;
|
|
|
|
private int solarizedRed;
|
|
|
|
private int solarizedMagenta;
|
|
|
|
private int solarizedViolet;
|
|
|
|
private int solarizedBlue;
|
|
|
|
private int solarizedCyan;
|
|
|
|
private int solarizedGreen;
|
2021-08-15 18:46:28 +00:00
|
|
|
|
2021-08-16 11:07:41 +00:00
|
|
|
private Long account = null;
|
|
|
|
private Long folder = null;
|
|
|
|
private Long message = null;
|
|
|
|
private List<EntityLog.Type> types = new ArrayList<>();
|
2021-08-16 07:36:23 +00:00
|
|
|
private List<EntityLog> all = new ArrayList<>();
|
|
|
|
private List<EntityLog> selected = new ArrayList<>();
|
2018-09-03 19:11:16 +00:00
|
|
|
|
2019-07-15 19:28:25 +00:00
|
|
|
private DateFormat TF;
|
2018-09-03 19:11:16 +00:00
|
|
|
|
|
|
|
public class ViewHolder extends RecyclerView.ViewHolder {
|
2019-03-14 18:21:42 +00:00
|
|
|
private TextView tvTime;
|
|
|
|
private TextView tvData;
|
2018-09-03 19:11:16 +00:00
|
|
|
|
|
|
|
ViewHolder(View itemView) {
|
|
|
|
super(itemView);
|
|
|
|
|
|
|
|
tvTime = itemView.findViewById(R.id.tvTime);
|
|
|
|
tvData = itemView.findViewById(R.id.tvData);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void bindTo(EntityLog log) {
|
2019-04-19 11:08:30 +00:00
|
|
|
tvTime.setText(TF.format(log.time));
|
2021-09-09 10:52:10 +00:00
|
|
|
SpannableStringBuilder ssb = new SpannableStringBuilderEx(log.data);
|
2021-08-15 18:46:28 +00:00
|
|
|
switch (log.type) {
|
2021-08-16 07:36:23 +00:00
|
|
|
case General:
|
2021-08-15 18:46:28 +00:00
|
|
|
break;
|
2021-08-16 07:36:23 +00:00
|
|
|
case Statistics:
|
2021-08-16 14:05:30 +00:00
|
|
|
ssb.setSpan(new ForegroundColorSpan(solarizedGreen), 0, ssb.length(), 0);
|
2021-08-15 18:46:28 +00:00
|
|
|
break;
|
2021-08-16 07:36:23 +00:00
|
|
|
case Scheduling:
|
2021-08-16 14:05:30 +00:00
|
|
|
ssb.setSpan(new ForegroundColorSpan(solarizedYellow), 0, ssb.length(), 0);
|
2021-08-15 18:46:28 +00:00
|
|
|
break;
|
2021-08-16 07:36:23 +00:00
|
|
|
case Network:
|
2021-08-16 14:05:30 +00:00
|
|
|
ssb.setSpan(new ForegroundColorSpan(solarizedOrange), 0, ssb.length(), 0);
|
|
|
|
break;
|
|
|
|
case Account:
|
|
|
|
ssb.setSpan(new ForegroundColorSpan(solarizedMagenta), 0, ssb.length(), 0);
|
|
|
|
break;
|
2021-08-16 07:36:23 +00:00
|
|
|
case Protocol:
|
2021-08-15 18:46:28 +00:00
|
|
|
ssb.setSpan(new ForegroundColorSpan(textColorSecondary), 0, ssb.length(), 0);
|
|
|
|
break;
|
2021-08-16 08:32:53 +00:00
|
|
|
case Classification:
|
2021-08-16 14:05:30 +00:00
|
|
|
ssb.setSpan(new ForegroundColorSpan(solarizedViolet), 0, ssb.length(), 0);
|
|
|
|
break;
|
2021-08-16 13:03:16 +00:00
|
|
|
case Notification:
|
2021-08-16 14:05:30 +00:00
|
|
|
ssb.setSpan(new ForegroundColorSpan(solarizedBlue), 0, ssb.length(), 0);
|
|
|
|
break;
|
2021-08-16 13:03:16 +00:00
|
|
|
case Rules:
|
2021-08-16 14:05:30 +00:00
|
|
|
ssb.setSpan(new ForegroundColorSpan(solarizedCyan), 0, ssb.length(), 0);
|
2021-08-16 08:32:53 +00:00
|
|
|
break;
|
2021-08-15 18:46:28 +00:00
|
|
|
}
|
|
|
|
tvData.setText(ssb);
|
2018-09-03 19:11:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-01 07:30:07 +00:00
|
|
|
AdapterLog(Fragment parentFragment) {
|
|
|
|
this.parentFragment = parentFragment;
|
|
|
|
this.context = parentFragment.getContext();
|
|
|
|
this.owner = parentFragment.getViewLifecycleOwner();
|
|
|
|
this.inflater = LayoutInflater.from(parentFragment.getContext());
|
|
|
|
|
2021-08-15 18:46:28 +00:00
|
|
|
this.textColorSecondary = Helper.resolveColor(context, android.R.attr.textColorSecondary);
|
2021-08-16 14:05:30 +00:00
|
|
|
this.solarizedYellow = ContextCompat.getColor(context, R.color.solarizedYellow);
|
|
|
|
this.solarizedOrange = ContextCompat.getColor(context, R.color.solarizedOrange);
|
|
|
|
this.solarizedRed = ContextCompat.getColor(context, R.color.solarizedRed);
|
|
|
|
this.solarizedMagenta = ContextCompat.getColor(context, R.color.solarizedMagenta);
|
|
|
|
this.solarizedViolet = ContextCompat.getColor(context, R.color.solarizedViolet);
|
|
|
|
this.solarizedBlue = ContextCompat.getColor(context, R.color.solarizedBlue);
|
|
|
|
this.solarizedCyan = ContextCompat.getColor(context, R.color.solarizedCyan);
|
|
|
|
this.solarizedGreen = ContextCompat.getColor(context, R.color.solarizedGreen);
|
2021-08-15 18:46:28 +00:00
|
|
|
|
2019-07-15 19:28:25 +00:00
|
|
|
this.TF = Helper.getTimeInstance(context);
|
|
|
|
|
2018-09-03 19:11:16 +00:00
|
|
|
setHasStableIds(true);
|
2019-07-01 07:30:07 +00:00
|
|
|
|
|
|
|
owner.getLifecycle().addObserver(new LifecycleObserver() {
|
|
|
|
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
|
|
|
|
public void onDestroyed() {
|
2019-12-07 16:02:42 +00:00
|
|
|
Log.d(AdapterLog.this + " parent destroyed");
|
2019-07-01 07:30:07 +00:00
|
|
|
AdapterLog.this.parentFragment = null;
|
|
|
|
}
|
|
|
|
});
|
2018-09-03 19:11:16 +00:00
|
|
|
}
|
|
|
|
|
2021-08-16 11:07:41 +00:00
|
|
|
public void set(@NonNull List<EntityLog> logs,
|
|
|
|
Long account, Long folder, Long message,
|
|
|
|
@NonNull List<EntityLog.Type> types) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.i("Set logs=" + logs.size());
|
2018-09-03 19:11:16 +00:00
|
|
|
|
2021-08-16 07:36:23 +00:00
|
|
|
this.all = logs;
|
2021-08-16 11:07:41 +00:00
|
|
|
this.account = account;
|
|
|
|
this.folder = folder;
|
|
|
|
this.message = message;
|
2021-08-16 07:36:23 +00:00
|
|
|
this.types = types;
|
2018-09-03 19:11:16 +00:00
|
|
|
|
2021-08-16 07:36:23 +00:00
|
|
|
List<EntityLog> items = new ArrayList<>();
|
|
|
|
for (EntityLog log : all)
|
2021-08-16 11:07:41 +00:00
|
|
|
if (account == null && folder == null && message == null) {
|
|
|
|
if (types.contains(log.type))
|
|
|
|
items.add(log);
|
|
|
|
} else {
|
|
|
|
if ((account == null || account.equals(log.account)) &&
|
|
|
|
(folder == null || folder.equals(log.folder)) &&
|
|
|
|
(message == null || message.equals(log.message)))
|
|
|
|
items.add(log);
|
|
|
|
}
|
2021-08-16 07:36:23 +00:00
|
|
|
|
|
|
|
DiffUtil.DiffResult diff = DiffUtil.calculateDiff(new DiffCallback(selected, items), false);
|
|
|
|
|
|
|
|
this.selected = items;
|
2018-09-03 19:11:16 +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);
|
2018-09-03 19:11:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRemoved(int position, int count) {
|
2019-12-07 16:02:42 +00:00
|
|
|
Log.d("Removed @" + position + " #" + count);
|
2018-09-03 19:11:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onMoved(int fromPosition, int toPosition) {
|
2019-12-07 16:02:42 +00:00
|
|
|
Log.d("Moved " + fromPosition + ">" + toPosition);
|
2018-09-03 19:11:16 +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);
|
2018-09-03 19:11:16 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
diff.dispatchUpdatesTo(this);
|
|
|
|
}
|
|
|
|
|
2021-08-16 07:36:23 +00:00
|
|
|
public void setTypes(@NonNull List<EntityLog.Type> types) {
|
2021-08-16 11:07:41 +00:00
|
|
|
set(all, account, folder, message, types);
|
2021-08-16 07:36:23 +00:00
|
|
|
}
|
|
|
|
|
2020-12-30 18:29:20 +00:00
|
|
|
private static class DiffCallback extends DiffUtil.Callback {
|
2019-03-15 11:53:22 +00:00
|
|
|
private List<EntityLog> prev = new ArrayList<>();
|
|
|
|
private List<EntityLog> next = new ArrayList<>();
|
2018-09-03 19:11:16 +00:00
|
|
|
|
2019-02-10 18:02:55 +00:00
|
|
|
DiffCallback(List<EntityLog> prev, List<EntityLog> next) {
|
2019-03-15 11:53:22 +00:00
|
|
|
this.prev.addAll(prev);
|
|
|
|
this.next.addAll(next);
|
2018-09-03 19:11:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getOldListSize() {
|
|
|
|
return prev.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getNewListSize() {
|
|
|
|
return next.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
|
|
|
|
EntityLog l1 = prev.get(oldItemPosition);
|
|
|
|
EntityLog l2 = next.get(newItemPosition);
|
|
|
|
return l1.id.equals(l2.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
|
|
|
|
EntityLog l1 = prev.get(oldItemPosition);
|
|
|
|
EntityLog l2 = next.get(newItemPosition);
|
|
|
|
return l1.equals(l2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public long getItemId(int position) {
|
2021-08-16 07:36:23 +00:00
|
|
|
return selected.get(position).id;
|
2018-09-03 19:11:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemCount() {
|
2021-08-16 07:36:23 +00:00
|
|
|
return selected.size();
|
2018-09-03 19:11:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@NonNull
|
|
|
|
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
2018-12-24 13:41:49 +00:00
|
|
|
return new ViewHolder(inflater.inflate(R.layout.item_log, parent, false));
|
2018-09-03 19:11:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
|
2021-08-16 07:36:23 +00:00
|
|
|
EntityLog log = selected.get(position);
|
2018-09-03 19:11:16 +00:00
|
|
|
holder.bindTo(log);
|
|
|
|
}
|
|
|
|
}
|