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

455 lines
18 KiB
Java
Raw Normal View History

2018-08-02 13:33:06 +00:00
package eu.faircode.email;
/*
2018-08-14 05:53:24 +00:00
This file is part of FairEmail.
2018-08-02 13:33:06 +00:00
2018-08-14 05:53:24 +00:00
FairEmail is free software: you can redistribute it and/or modify
2018-08-02 13:33:06 +00:00
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-08-02 13:33:06 +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-08-02 13:33:06 +00:00
2020-01-05 17:32:53 +00:00
Copyright 2018-2020 by Marcel Bokhorst (M66B)
2018-08-02 13:33:06 +00:00
*/
import android.annotation.TargetApi;
import android.app.NotificationChannel;
import android.app.NotificationManager;
2018-08-02 13:33:06 +00:00
import android.content.Context;
import android.content.Intent;
2019-10-07 11:07:26 +00:00
import android.content.SharedPreferences;
2018-09-12 16:54:48 +00:00
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
2020-01-21 09:52:34 +00:00
import android.text.SpannableString;
import android.text.style.StyleSpan;
2018-08-02 13:33:06 +00:00
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
2018-08-02 13:33:06 +00:00
import android.view.View;
import android.view.ViewGroup;
2019-09-01 16:20:03 +00:00
import android.widget.Button;
2018-08-02 13:33:06 +00:00
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.widget.PopupMenu;
import androidx.constraintlayout.widget.Group;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
2019-04-25 06:18:01 +00:00
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.OnLifecycleEvent;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
2019-10-07 11:07:26 +00:00
import androidx.preference.PreferenceManager;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListUpdateCallback;
import androidx.recyclerview.widget.RecyclerView;
2019-01-23 09:16:33 +00:00
import java.text.DateFormat;
import java.text.NumberFormat;
2018-08-02 13:33:06 +00:00
import java.util.ArrayList;
import java.util.List;
public class AdapterAccount extends RecyclerView.Adapter<AdapterAccount.ViewHolder> {
private Fragment parentFragment;
private boolean settings;
2018-08-02 13:33:06 +00:00
private Context context;
2019-04-25 06:18:01 +00:00
private LifecycleOwner owner;
2018-12-24 13:41:49 +00:00
private LayoutInflater inflater;
2018-08-02 13:33:06 +00:00
private int colorUnread;
private int textColorSecondary;
2018-08-02 13:33:06 +00:00
private List<TupleAccountEx> items = new ArrayList<>();
2019-07-22 07:52:08 +00:00
private NumberFormat NF = NumberFormat.getNumberInstance();
2019-07-15 19:28:25 +00:00
private DateFormat DTF;
2019-01-23 09:16:33 +00:00
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
2019-03-19 10:55:16 +00:00
private View view;
private View vwColor;
2019-12-20 20:00:17 +00:00
private ImageView ivOAuth;
2019-03-19 10:55:16 +00:00
private ImageView ivPrimary;
2019-03-19 17:55:54 +00:00
private ImageView ivNotify;
2019-03-19 10:55:16 +00:00
private TextView tvName;
private ImageView ivSync;
private TextView tvUser;
private ImageView ivState;
private TextView tvHost;
private TextView tvLast;
2020-01-07 16:43:27 +00:00
private TextView tvQuota;
private TextView tvIdentity;
private TextView tvDrafts;
2019-03-27 08:19:11 +00:00
private TextView tvWarning;
2019-03-19 10:55:16 +00:00
private TextView tvError;
2019-09-01 16:20:03 +00:00
private Button btnHelp;
2019-03-19 10:55:16 +00:00
private Group grpSettings;
2018-08-02 13:33:06 +00:00
2019-05-03 16:59:27 +00:00
private TwoStateOwner powner = new TwoStateOwner(owner, "AccountPopup");
2018-08-02 13:33:06 +00:00
ViewHolder(View itemView) {
super(itemView);
2019-03-15 11:53:22 +00:00
view = itemView.findViewById(R.id.clItem);
2018-09-12 16:54:48 +00:00
vwColor = itemView.findViewById(R.id.vwColor);
2019-03-19 10:55:16 +00:00
ivSync = itemView.findViewById(R.id.ivSync);
2019-12-20 20:00:17 +00:00
ivOAuth = itemView.findViewById(R.id.ivOAuth);
2018-08-02 13:33:06 +00:00
ivPrimary = itemView.findViewById(R.id.ivPrimary);
2019-03-19 17:55:54 +00:00
ivNotify = itemView.findViewById(R.id.ivNotify);
2018-08-02 13:33:06 +00:00
tvName = itemView.findViewById(R.id.tvName);
tvUser = itemView.findViewById(R.id.tvUser);
2018-08-13 14:44:47 +00:00
ivState = itemView.findViewById(R.id.ivState);
2019-01-25 18:12:44 +00:00
tvHost = itemView.findViewById(R.id.tvHost);
tvLast = itemView.findViewById(R.id.tvLast);
2020-01-07 16:43:27 +00:00
tvQuota = itemView.findViewById(R.id.tvQuota);
tvIdentity = itemView.findViewById(R.id.tvIdentity);
tvDrafts = itemView.findViewById(R.id.tvDrafts);
2019-03-27 08:19:11 +00:00
tvWarning = itemView.findViewById(R.id.tvWarning);
tvError = itemView.findViewById(R.id.tvError);
2019-09-01 16:20:03 +00:00
btnHelp = itemView.findViewById(R.id.btnHelp);
2019-03-19 10:55:16 +00:00
grpSettings = itemView.findViewById(R.id.grpSettings);
2018-08-02 13:33:06 +00:00
}
private void wire() {
2019-03-15 11:53:22 +00:00
view.setOnClickListener(this);
view.setOnLongClickListener(this);
2019-09-01 16:20:03 +00:00
btnHelp.setOnClickListener(this);
2018-08-02 13:33:06 +00:00
}
private void unwire() {
2019-03-15 11:53:22 +00:00
view.setOnClickListener(null);
view.setOnLongClickListener(null);
2019-09-01 16:20:03 +00:00
btnHelp.setOnClickListener(null);
2018-08-02 13:33:06 +00:00
}
private void bindTo(TupleAccountEx account) {
2019-03-15 11:53:22 +00:00
view.setActivated(account.tbd != null);
2018-09-12 16:54:48 +00:00
vwColor.setBackgroundColor(account.color == null ? Color.TRANSPARENT : account.color);
2019-08-13 08:27:17 +00:00
vwColor.setVisibility(ActivityBilling.isPro(context) ? View.VISIBLE : View.INVISIBLE);
2019-03-19 10:55:16 +00:00
ivSync.setImageResource(account.synchronize ? R.drawable.baseline_sync_24 : R.drawable.baseline_sync_disabled_24);
ivSync.setContentDescription(context.getString(account.synchronize ? R.string.title_legend_synchronize_on : R.string.title_legend_synchronize_off));
2019-03-19 10:55:16 +00:00
2019-12-27 20:05:58 +00:00
ivOAuth.setVisibility(
settings && account.auth_type != MailService.AUTH_TYPE_PASSWORD ? View.VISIBLE : View.GONE);
2019-03-19 10:55:16 +00:00
ivPrimary.setVisibility(account.primary ? View.VISIBLE : View.GONE);
2019-03-19 17:55:54 +00:00
ivNotify.setVisibility(account.notify ? View.VISIBLE : View.GONE);
if (settings)
tvName.setText(account.name);
else {
if (account.unseen > 0)
2019-07-22 07:52:08 +00:00
tvName.setText(context.getString(R.string.title_name_count, account.name, NF.format(account.unseen)));
else
tvName.setText(account.name);
tvName.setTypeface(account.unseen > 0 ? Typeface.DEFAULT_BOLD : Typeface.DEFAULT);
tvName.setTextColor(account.unseen > 0 ? colorUnread : textColorSecondary);
}
tvUser.setText(account.user);
2018-08-15 10:32:51 +00:00
if ("connected".equals(account.state)) {
2018-08-13 14:44:47 +00:00
ivState.setImageResource(R.drawable.baseline_cloud_24);
ivState.setContentDescription(context.getString(R.string.title_legend_connected));
} else if ("connecting".equals(account.state)) {
2018-08-13 14:44:47 +00:00
ivState.setImageResource(R.drawable.baseline_cloud_queue_24);
ivState.setContentDescription(context.getString(R.string.title_legend_connecting));
} else if ("closing".equals(account.state)) {
2018-08-15 19:34:22 +00:00
ivState.setImageResource(R.drawable.baseline_close_24);
ivState.setContentDescription(context.getString(R.string.title_legend_closing));
} else {
2018-08-13 14:44:47 +00:00
ivState.setImageResource(R.drawable.baseline_cloud_off_24);
ivState.setContentDescription(context.getString(R.string.title_legend_disconnected));
}
2019-12-09 08:31:09 +00:00
ivState.setVisibility(account.synchronize || account.state != null ? View.VISIBLE : View.INVISIBLE);
2018-08-13 14:44:47 +00:00
2019-01-25 18:12:44 +00:00
tvHost.setText(String.format("%s:%d", account.host, account.port));
2019-01-23 09:16:33 +00:00
tvLast.setText(context.getString(R.string.title_last_connected,
(account.last_connected == null ? "-" : DTF.format(account.last_connected)) +
(BuildConfig.DEBUG ?
" " + account.poll_interval +
"/" + account.keep_alive_ok +
"/" + account.keep_alive_failed : "")));
2020-01-07 16:43:27 +00:00
tvQuota.setText(context.getString(R.string.title_storage_quota,
(account.quota_usage == null ? "-" : Helper.humanReadableByteCount(account.quota_usage, true)),
(account.quota_limit == null ? "-" : Helper.humanReadableByteCount(account.quota_limit, true))));
tvQuota.setVisibility(account.quota_usage != null || account.quota_limit != null ? View.VISIBLE : View.GONE);
tvIdentity.setVisibility(account.identities > 0 || !settings ? View.GONE : View.VISIBLE);
tvDrafts.setVisibility(account.drafts || !settings ? View.GONE : View.VISIBLE);
2019-03-27 08:19:11 +00:00
tvWarning.setText(account.warning);
tvWarning.setVisibility(account.warning == null || !settings ? View.GONE : View.VISIBLE);
tvError.setText(account.error);
tvError.setVisibility(account.error == null ? View.GONE : View.VISIBLE);
2019-09-01 16:20:03 +00:00
btnHelp.setVisibility(account.error == null ? View.GONE : View.VISIBLE);
2019-03-19 10:55:16 +00:00
grpSettings.setVisibility(settings ? View.VISIBLE : View.GONE);
}
2018-08-02 13:33:06 +00:00
@Override
public void onClick(View view) {
2019-09-01 16:20:03 +00:00
if (view.getId() == R.id.btnHelp)
Helper.viewFAQ(context, 22);
else {
int pos = getAdapterPosition();
if (pos == RecyclerView.NO_POSITION)
return;
TupleAccountEx account = items.get(pos);
if (account.tbd != null)
return;
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
lbm.sendBroadcast(
new Intent(settings ? ActivitySetup.ACTION_EDIT_ACCOUNT : ActivityView.ACTION_VIEW_FOLDERS)
2019-09-19 15:41:26 +00:00
.putExtra("id", account.id)
2019-11-23 12:48:59 +00:00
.putExtra("protocol", account.protocol));
2019-09-01 16:20:03 +00:00
}
2018-08-02 13:33:06 +00:00
}
@Override
public boolean onLongClick(View v) {
int pos = getAdapterPosition();
if (pos == RecyclerView.NO_POSITION)
return false;
final TupleAccountEx account = items.get(pos);
if (account.tbd != null)
return false;
2019-05-03 16:59:27 +00:00
PopupMenuLifecycle popupMenu = new PopupMenuLifecycle(context, powner, view);
2020-01-21 09:52:34 +00:00
SpannableString ss = new SpannableString(account.name);
ss.setSpan(new StyleSpan(Typeface.ITALIC), 0, ss.length(), 0);
popupMenu.getMenu().add(Menu.NONE, 0, 0, ss).setEnabled(false);
2019-06-25 08:53:06 +00:00
2019-09-13 17:38:41 +00:00
popupMenu.getMenu().add(Menu.NONE, R.string.title_enabled, 1, R.string.title_enabled)
.setCheckable(true).setChecked(account.synchronize);
2019-11-23 12:48:59 +00:00
if (account.protocol == EntityAccount.TYPE_IMAP && account.notify &&
Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelId = EntityAccount.getNotificationChannelId(account.id);
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel channel = nm.getNotificationChannel(channelId);
if (channel != null)
popupMenu.getMenu().add(Menu.NONE, R.string.title_edit_channel, 2, R.string.title_edit_channel);
}
2019-11-23 12:48:59 +00:00
if (account.protocol == EntityAccount.TYPE_IMAP && settings)
2019-08-13 14:02:46 +00:00
popupMenu.getMenu().add(Menu.NONE, R.string.title_copy, 3, R.string.title_copy);
2019-08-13 10:07:30 +00:00
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
2019-09-13 17:38:41 +00:00
case R.string.title_enabled:
onActionSync(!item.isChecked());
return true;
case R.string.title_edit_channel:
onActionEditChannel();
return true;
2019-08-13 10:07:30 +00:00
case R.string.title_copy:
onActionCopy();
return true;
default:
return false;
}
}
private void onActionSync(boolean sync) {
Bundle args = new Bundle();
args.putLong("id", account.id);
args.putBoolean("sync", sync);
new SimpleTask<Boolean>() {
@Override
protected Boolean onExecute(Context context, Bundle args) {
long id = args.getLong("id");
boolean sync = args.getBoolean("sync");
DB db = DB.getInstance(context);
if (!sync) {
db.account().setAccountWarning(id, null);
db.account().setAccountError(id, null);
}
db.account().setAccountSynchronize(id, sync);
return sync;
}
@Override
protected void onExecuted(Bundle args, Boolean sync) {
2019-12-09 18:44:27 +00:00
ServiceSynchronize.eval(context, "account sync=" + sync);
}
@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, "account:enable");
}
@TargetApi(Build.VERSION_CODES.O)
private void onActionEditChannel() {
Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS)
.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName())
.putExtra(Settings.EXTRA_CHANNEL_ID, EntityAccount.getNotificationChannelId(account.id));
context.startActivity(intent);
}
2019-08-13 10:07:30 +00:00
private void onActionCopy() {
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
lbm.sendBroadcast(
2019-08-13 14:02:46 +00:00
new Intent(ActivitySetup.ACTION_EDIT_ACCOUNT)
2019-08-13 10:07:30 +00:00
.putExtra("id", account.id)
2019-11-23 12:48:59 +00:00
.putExtra("protocol", account.protocol)
2019-08-13 10:07:30 +00:00
.putExtra("copy", true));
}
});
popupMenu.show();
return true;
}
2018-08-02 13:33:06 +00:00
}
AdapterAccount(final Fragment parentFragment, boolean settings) {
this.parentFragment = parentFragment;
this.settings = settings;
this.context = parentFragment.getContext();
this.owner = parentFragment.getViewLifecycleOwner();
2018-12-24 13:41:49 +00:00
this.inflater = LayoutInflater.from(context);
2019-10-07 11:07:26 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean highlight_unread = prefs.getBoolean("highlight_unread", false);
this.colorUnread = Helper.resolveColor(context, highlight_unread ? R.attr.colorUnreadHighlight : android.R.attr.textColorPrimary);
this.textColorSecondary = Helper.resolveColor(context, android.R.attr.textColorSecondary);
2019-07-15 19:28:25 +00:00
this.DTF = Helper.getDateTimeInstance(context, DateFormat.SHORT, DateFormat.SHORT);
2018-08-02 13:33:06 +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(AdapterAccount.this + " parent destroyed");
AdapterAccount.this.parentFragment = null;
}
});
2018-08-02 13:33:06 +00:00
}
public void set(@NonNull List<TupleAccountEx> accounts) {
2018-12-24 12:27:45 +00:00
Log.i("Set accounts=" + accounts.size());
2018-08-02 13:33:06 +00:00
2019-03-15 11:53:22 +00:00
DiffUtil.DiffResult diff = DiffUtil.calculateDiff(new DiffCallback(items, accounts), false);
2018-08-02 13:33:06 +00:00
2019-03-15 11:53:22 +00:00
items = accounts;
2018-08-02 13:33:06 +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-08-02 13:33:06 +00:00
}
@Override
public void onRemoved(int position, int count) {
2019-12-07 16:02:42 +00:00
Log.d("Removed @" + position + " #" + count);
2018-08-02 13:33:06 +00:00
}
@Override
public void onMoved(int fromPosition, int toPosition) {
2019-12-07 16:02:42 +00:00
Log.d("Moved " + fromPosition + ">" + toPosition);
2018-08-02 13:33:06 +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-08-02 13:33:06 +00:00
}
});
diff.dispatchUpdatesTo(this);
2018-08-02 13:33:06 +00:00
}
2019-02-10 18:02:55 +00:00
private class DiffCallback extends DiffUtil.Callback {
private List<TupleAccountEx> prev = new ArrayList<>();
private List<TupleAccountEx> next = new ArrayList<>();
2018-08-02 13:33:06 +00:00
DiffCallback(List<TupleAccountEx> prev, List<TupleAccountEx> next) {
2019-03-15 11:53:22 +00:00
this.prev.addAll(prev);
this.next.addAll(next);
2018-08-02 13:33:06 +00:00
}
@Override
public int getOldListSize() {
return prev.size();
}
@Override
public int getNewListSize() {
return next.size();
}
@Override
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
TupleAccountEx f1 = prev.get(oldItemPosition);
TupleAccountEx f2 = next.get(newItemPosition);
2018-08-02 13:33:06 +00:00
return f1.id.equals(f2.id);
}
@Override
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
TupleAccountEx f1 = prev.get(oldItemPosition);
TupleAccountEx f2 = next.get(newItemPosition);
2019-03-19 17:50:48 +00:00
return f1.uiEquals(f2);
2018-08-02 13:33:06 +00:00
}
}
@Override
public long getItemId(int position) {
2019-03-15 11:53:22 +00:00
return items.get(position).id;
2018-08-02 13:33:06 +00:00
}
@Override
public int getItemCount() {
2019-03-15 11:53:22 +00:00
return items.size();
2018-08-02 13:33:06 +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_account, parent, false));
2018-08-02 13:33:06 +00:00
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.unwire();
TupleAccountEx account = items.get(position);
holder.bindTo(account);
2018-08-02 13:33:06 +00:00
holder.wire();
}
2019-05-03 16:59:27 +00:00
@Override
2019-09-21 18:28:03 +00:00
public void onViewDetachedFromWindow(@NonNull ViewHolder holder) {
2019-05-03 16:59:27 +00:00
holder.powner.recreate();
}
2018-08-02 13:33:06 +00:00
}