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

321 lines
12 KiB
Java
Raw Normal View History

2019-12-04 10:54:02 +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/>.
2022-01-01 08:46:36 +00:00
Copyright 2018-2022 by Marcel Bokhorst (M66B)
2019-12-04 10:54:02 +00:00
*/
import android.content.Context;
2020-01-21 09:52:34 +00:00
import android.graphics.Typeface;
2019-12-04 12:10:33 +00:00
import android.os.Bundle;
2020-01-21 09:52:34 +00:00
import android.text.SpannableString;
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-12-04 10:54:02 +00:00
import android.view.LayoutInflater;
2019-12-04 19:36:33 +00:00
import android.view.Menu;
import android.view.MenuItem;
2019-12-04 10:54:02 +00:00
import android.view.View;
import android.view.ViewGroup;
2020-01-30 13:19:15 +00:00
import android.widget.ImageView;
2019-12-04 10:54:02 +00:00
import android.widget.TextView;
import androidx.annotation.NonNull;
2019-12-04 19:36:33 +00:00
import androidx.appcompat.widget.PopupMenu;
2019-12-04 10:54:02 +00:00
import androidx.fragment.app.Fragment;
2022-02-05 18:42:18 +00:00
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
2019-12-04 10:54:02 +00:00
import androidx.lifecycle.LifecycleOwner;
2022-02-05 18:42:18 +00:00
import androidx.lifecycle.OnLifecycleEvent;
2019-12-04 10:54:02 +00:00
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListUpdateCallback;
import androidx.recyclerview.widget.RecyclerView;
2022-02-05 18:42:18 +00:00
import java.io.File;
import java.io.IOException;
import java.security.cert.CertificateException;
2019-12-06 10:32:41 +00:00
import java.text.DateFormat;
import java.text.SimpleDateFormat;
2019-12-04 10:54:02 +00:00
import java.util.ArrayList;
import java.util.List;
public class AdapterCertificate extends RecyclerView.Adapter<AdapterCertificate.ViewHolder> {
2022-02-05 18:42:18 +00:00
private Fragment parentFragment;
2019-12-04 10:54:02 +00:00
private Context context;
private LifecycleOwner owner;
private LayoutInflater inflater;
private List<EntityCertificate> items = new ArrayList<>();
2019-12-06 10:32:41 +00:00
private DateFormat TF;
2019-12-05 14:18:53 +00:00
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener {
2019-12-04 10:54:02 +00:00
private View view;
private TextView tvEmail;
2020-01-30 13:19:15 +00:00
private ImageView ivIntermediate;
2019-12-04 10:54:02 +00:00
private TextView tvSubject;
2019-12-06 10:32:41 +00:00
private TextView tvAfter;
private TextView tvBefore;
2019-12-06 11:30:38 +00:00
private TextView tvExpired;
2019-12-04 10:54:02 +00:00
2019-12-04 19:36:33 +00:00
private TwoStateOwner powner = new TwoStateOwner(owner, "CertificatePopup");
2019-12-04 10:54:02 +00:00
ViewHolder(View itemView) {
super(itemView);
view = itemView.findViewById(R.id.clItem);
tvEmail = itemView.findViewById(R.id.tvEmail);
2020-01-30 13:19:15 +00:00
ivIntermediate = itemView.findViewById(R.id.ivIntermediate);
2019-12-04 10:54:02 +00:00
tvSubject = itemView.findViewById(R.id.tvSubject);
2019-12-06 10:32:41 +00:00
tvAfter = itemView.findViewById(R.id.tvAfter);
tvBefore = itemView.findViewById(R.id.tvBefore);
2019-12-06 11:30:38 +00:00
tvExpired = itemView.findViewById(R.id.tvExpired);
2019-12-04 10:54:02 +00:00
}
2019-12-04 12:10:33 +00:00
@Override
public boolean onLongClick(View v) {
int pos = getAdapterPosition();
if (pos == RecyclerView.NO_POSITION)
return false;
2019-12-04 19:36:33 +00:00
final EntityCertificate certificate = items.get(pos);
2019-12-04 12:10:33 +00:00
2019-12-04 19:36:33 +00:00
PopupMenuLifecycle popupMenu = new PopupMenuLifecycle(context, powner, view);
2019-12-04 12:10:33 +00:00
2020-01-21 09:52:34 +00:00
SpannableString ss = new SpannableString(certificate.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);
2019-12-04 12:10:33 +00:00
2022-02-05 18:42:18 +00:00
popupMenu.getMenu().add(Menu.NONE, R.string.title_save, 1, R.string.title_save);
popupMenu.getMenu().add(Menu.NONE, R.string.title_delete, 2, R.string.title_delete);
2019-12-04 12:10:33 +00:00
2019-12-04 19:36:33 +00:00
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
2022-02-05 18:42:18 +00:00
int id = item.getItemId();
if (id == R.string.title_save) {
onActionSave();
return true;
} else if (id == R.string.title_delete) {
2021-02-05 10:15:02 +00:00
onActionDelete();
return true;
2019-12-04 19:36:33 +00:00
}
2021-02-05 10:15:02 +00:00
return false;
2019-12-04 12:10:33 +00:00
}
2022-02-05 18:42:18 +00:00
private void onActionSave() {
Bundle args = new Bundle();
args.putLong("id", certificate.id);
new SimpleTask<File>() {
@Override
protected File onExecute(Context context, Bundle args) throws CertificateException, IOException {
long id = args.getLong("id");
DB db = DB.getInstance(context);
EntityCertificate certificate = db.certificate().getCertificate(id);
if (certificate == null)
return null;
2022-05-01 06:48:20 +00:00
File dir = new File(context.getFilesDir(), "shared");
2022-02-05 18:42:18 +00:00
if (!dir.exists())
dir.mkdir();
String name = Helper.sanitizeFilename(certificate.email);
File file = new File(dir, name + ".pem");
Helper.writeText(file, certificate.getPem());
return file;
}
@Override
protected void onExecuted(Bundle args, File file) {
if (file == null)
return;
Helper.share(context, file, "application/*", file.getName());
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(parentFragment.getParentFragmentManager(), ex);
}
}.execute(context, owner, args, "certificate:save");
}
2019-12-04 19:36:33 +00:00
private void onActionDelete() {
Bundle args = new Bundle();
args.putLong("id", certificate.id);
new SimpleTask<Void>() {
@Override
2019-12-05 14:18:53 +00:00
protected Void onExecute(Context context, Bundle args) {
2019-12-04 19:36:33 +00:00
long id = args.getLong("id");
DB db = DB.getInstance(context);
db.certificate().deleteCertificate(id);
return null;
}
@Override
protected void onException(Bundle args, Throwable ex) {
2022-02-05 18:42:18 +00:00
Log.unexpectedError(parentFragment.getParentFragmentManager(), ex);
2019-12-04 19:36:33 +00:00
}
}.execute(context, owner, args, "certificate:delete");
2019-12-04 12:10:33 +00:00
}
2019-12-04 19:36:33 +00:00
});
popupMenu.show();
2019-12-04 12:10:33 +00:00
return true;
}
2019-12-04 10:54:02 +00:00
private void wire() {
2019-12-04 12:10:33 +00:00
view.setOnLongClickListener(this);
2019-12-04 10:54:02 +00:00
}
private void unwire() {
2019-12-04 19:36:33 +00:00
view.setOnLongClickListener(null);
2019-12-04 10:54:02 +00:00
}
private void bindTo(EntityCertificate certificate) {
tvEmail.setText(certificate.email);
2020-01-30 13:19:15 +00:00
ivIntermediate.setVisibility(certificate.intermediate ? View.VISIBLE : View.INVISIBLE);
2019-12-04 10:54:02 +00:00
tvSubject.setText(certificate.subject);
2019-12-06 10:32:41 +00:00
tvAfter.setText(certificate.after == null ? null : TF.format(certificate.after));
tvBefore.setText(certificate.before == null ? null : TF.format(certificate.before));
2019-12-06 11:30:38 +00:00
tvExpired.setVisibility(certificate.isExpired() ? View.VISIBLE : View.GONE);
2019-12-04 10:54:02 +00:00
}
}
2019-12-05 14:18:53 +00:00
AdapterCertificate(Fragment parentFragment) {
2019-12-04 10:54:02 +00:00
this.context = parentFragment.getContext();
this.owner = parentFragment.getViewLifecycleOwner();
this.inflater = LayoutInflater.from(parentFragment.getContext());
2019-12-06 10:32:41 +00:00
this.TF = Helper.getDateTimeInstance(context, SimpleDateFormat.SHORT, SimpleDateFormat.SHORT);
2019-12-04 10:54:02 +00:00
setHasStableIds(true);
2022-02-05 18:42:18 +00:00
owner.getLifecycle().addObserver(new LifecycleObserver() {
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void onDestroyed() {
Log.d(AdapterCertificate.this + " parent destroyed");
AdapterCertificate.this.parentFragment = null;
owner.getLifecycle().removeObserver(this);
}
});
2019-12-04 10:54:02 +00:00
}
2019-12-06 10:32:41 +00:00
public void set(@NonNull List<EntityCertificate> certificates) {
Log.i("Set certificates=" + certificates.size());
2019-12-04 10:54:02 +00:00
DiffUtil.DiffResult diff = DiffUtil.calculateDiff(new DiffCallback(items, certificates), false);
2019-12-04 12:41:20 +00:00
this.items = certificates;
2019-12-04 10:54:02 +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-12-04 10:54:02 +00:00
}
@Override
public void onRemoved(int position, int count) {
2019-12-07 16:02:42 +00:00
Log.d("Removed @" + position + " #" + count);
2019-12-04 10:54:02 +00:00
}
@Override
public void onMoved(int fromPosition, int toPosition) {
2019-12-07 16:02:42 +00:00
Log.d("Moved " + fromPosition + ">" + toPosition);
2019-12-04 10:54:02 +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-12-04 10:54:02 +00:00
}
});
2022-03-23 17:19:14 +00:00
try {
diff.dispatchUpdatesTo(this);
} catch (Throwable ex) {
Log.e(ex);
}
2019-12-04 10:54:02 +00:00
}
2020-12-30 18:29:20 +00:00
private static class DiffCallback extends DiffUtil.Callback {
2019-12-04 10:54:02 +00:00
private List<EntityCertificate> prev = new ArrayList<>();
private List<EntityCertificate> next = new ArrayList<>();
DiffCallback(List<EntityCertificate> prev, List<EntityCertificate> next) {
this.prev.addAll(prev);
this.next.addAll(next);
}
@Override
public int getOldListSize() {
return prev.size();
}
@Override
public int getNewListSize() {
return next.size();
}
@Override
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
EntityCertificate c1 = prev.get(oldItemPosition);
EntityCertificate c2 = next.get(newItemPosition);
return c1.id.equals(c2.id);
}
@Override
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
EntityCertificate c1 = prev.get(oldItemPosition);
EntityCertificate c2 = next.get(newItemPosition);
return c1.equals(c2);
}
}
@Override
public long getItemId(int position) {
return items.get(position).id;
}
@Override
public int getItemCount() {
return items.size();
}
@Override
@NonNull
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ViewHolder(inflater.inflate(R.layout.item_certificate, parent, false));
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
EntityCertificate certificate = items.get(position);
2020-11-12 07:07:30 +00:00
holder.powner.recreate(certificate == null ? null : certificate.id);
2019-12-04 10:54:02 +00:00
2020-11-12 07:07:30 +00:00
holder.unwire();
holder.bindTo(certificate);
2019-12-04 10:54:02 +00:00
holder.wire();
}
}