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

934 lines
41 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
2024-01-01 07:50:49 +00:00
Copyright 2018-2024 by Marcel Bokhorst (M66B)
2018-08-02 13:33:06 +00:00
*/
2022-07-16 14:24:54 +00:00
import static eu.faircode.email.ServiceAuthenticator.AUTH_TYPE_GMAIL;
2021-08-07 15:06:11 +00:00
import static eu.faircode.email.ServiceAuthenticator.AUTH_TYPE_PASSWORD;
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.DialogInterface;
2018-08-02 13:33:06 +00:00
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.Rect;
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;
2021-08-07 15:06:11 +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;
2018-08-02 13:33:06 +00:00
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.TouchDelegate;
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;
import android.widget.ImageButton;
2018-08-02 13:33:06 +00:00
import android.widget.ImageView;
import android.widget.TextView;
2021-02-22 12:35:25 +00:00
import android.widget.Toast;
2018-08-02 13:33:06 +00:00
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.PopupMenu;
import androidx.constraintlayout.widget.Group;
2022-08-04 13:24:27 +00:00
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
2021-08-17 09:43:09 +00:00
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
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;
2021-12-20 06:59:58 +00:00
private boolean compact;
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
2021-11-30 10:27:50 +00:00
private int colorStripeWidth;
2021-05-12 20:08:56 +00:00
private int colorWarning;
private int colorUnread;
private int textColorSecondary;
private int textColorTertiary;
2021-02-22 12:35:25 +00:00
private boolean debug;
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 ImageButton ibInbox;
2019-03-19 10:55:16 +00:00
private TextView tvUser;
private ImageView ivState;
private TextView tvHost;
2021-02-22 12:35:25 +00:00
private TextView tvCreated;
2019-03-19 10:55:16 +00:00
private TextView tvLast;
2020-10-29 10:06:50 +00:00
private TextView tvUsage;
2020-10-29 19:03:09 +00:00
private TextView tvBackoff;
2020-01-07 16:43:27 +00:00
private TextView tvQuota;
2020-07-03 07:57:43 +00:00
private TextView tvMaxSize;
2021-07-29 16:59:05 +00:00
private TextView tvId;
2021-08-07 15:06:11 +00:00
private TextView tvCapabilities;
private TextView tvIdentity;
private TextView tvDrafts;
2021-07-11 19:04:47 +00:00
private TextView tvSent;
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);
ibInbox = itemView.findViewById(R.id.ibInbox);
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);
2021-02-22 12:35:25 +00:00
tvCreated = itemView.findViewById(R.id.tvCreated);
tvLast = itemView.findViewById(R.id.tvLast);
2020-10-29 10:06:50 +00:00
tvUsage = itemView.findViewById(R.id.tvUsage);
2020-10-29 19:03:09 +00:00
tvBackoff = itemView.findViewById(R.id.tvBackoff);
2020-01-07 16:43:27 +00:00
tvQuota = itemView.findViewById(R.id.tvQuota);
2020-07-03 07:57:43 +00:00
tvMaxSize = itemView.findViewById(R.id.tvMaxSize);
2021-07-29 16:59:05 +00:00
tvId = itemView.findViewById(R.id.tvId);
2021-08-07 15:06:11 +00:00
tvCapabilities = itemView.findViewById(R.id.tvCapabilities);
tvIdentity = itemView.findViewById(R.id.tvIdentity);
tvDrafts = itemView.findViewById(R.id.tvDrafts);
2021-07-11 19:04:47 +00:00
tvSent = itemView.findViewById(R.id.tvSent);
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);
2021-11-30 10:27:50 +00:00
2021-11-30 13:48:46 +00:00
if (vwColor != null)
vwColor.getLayoutParams().width = colorStripeWidth;
2018-08-02 13:33:06 +00:00
}
private void wire() {
if (!settings)
view.post(new Runnable() {
@Override
public void run() {
int left;
int right;
if (view.getLayoutDirection() == View.LAYOUT_DIRECTION_LTR) {
2022-05-06 06:58:15 +00:00
left = view.getWidth() - view.getWidth() / 4;
right = view.getWidth();
} else {
left = 0;
2022-05-06 06:58:15 +00:00
right = view.getWidth() / 4;
}
Rect rect = new Rect(
left,
view.getTop(),
right,
view.getBottom());
view.setTouchDelegate(new TouchDelegate(rect, ibInbox));
2022-03-12 18:11:07 +00:00
}
});
2019-03-15 11:53:22 +00:00
view.setOnClickListener(this);
view.setOnLongClickListener(this);
ibInbox.setOnClickListener(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);
ibInbox.setOnClickListener(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);
view.setAlpha(account.synchronize ? 1.0f : Helper.LOW_LIGHT);
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
2020-09-28 07:18:56 +00:00
ivSync.setImageResource(account.synchronize ? R.drawable.twotone_sync_24 : R.drawable.twotone_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
2022-08-04 13:24:27 +00:00
ivOAuth.setImageDrawable(ContextCompat.getDrawable(context, account.auth_type == AUTH_TYPE_GMAIL
2022-07-16 14:24:54 +00:00
? R.drawable.twotone_android_24 : R.drawable.twotone_security_24));
2019-12-27 20:05:58 +00:00
ivOAuth.setVisibility(
2020-10-25 21:20:48 +00:00
settings && account.auth_type != 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);
2021-05-12 20:08:56 +00:00
if (settings) {
tvName.setText(account.name);
2021-05-12 20:08:56 +00:00
tvName.setTextColor(account.protocol == EntityAccount.TYPE_IMAP
? textColorSecondary : colorWarning);
} 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);
}
2022-03-22 09:56:51 +00:00
StringBuilder user = new StringBuilder(account.user);
if (account.provider != null && (BuildConfig.DEBUG || debug))
user.append(" (").append(account.provider).append(')');
tvUser.setText(user);
2018-08-15 10:32:51 +00:00
if ("connected".equals(account.state)) {
2020-09-28 16:37:00 +00:00
ivState.setImageResource(R.drawable.twotone_cloud_done_24);
ivState.setContentDescription(context.getString(R.string.title_legend_connected));
} else if ("connecting".equals(account.state)) {
2020-09-28 07:18:56 +00:00
ivState.setImageResource(R.drawable.twotone_cloud_queue_24);
ivState.setContentDescription(context.getString(R.string.title_legend_connecting));
} else if ("closing".equals(account.state)) {
ivState.setImageResource(R.drawable.twotone_cancel_24);
ivState.setContentDescription(context.getString(R.string.title_legend_closing));
} else {
2020-10-31 08:06:06 +00:00
if (account.backoff_until == null) {
ivState.setImageResource(R.drawable.twotone_cloud_off_24);
ivState.setContentDescription(context.getString(R.string.title_legend_disconnected));
} else {
ivState.setImageResource(R.drawable.twotone_update_24);
ivState.setContentDescription(context.getString(R.string.title_legend_backoff));
}
}
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
tvHost.setText(String.format("%s:%d/%s",
account.host,
account.port,
EmailService.getEncryptionName(account.encryption)));
tvHost.setTextColor(account.insecure ? colorWarning : textColorTertiary);
2021-02-22 12:35:25 +00:00
tvCreated.setVisibility(debug ? View.VISIBLE : View.GONE);
tvCreated.setText(context.getString(R.string.title_created_at,
account.created == null ? null : DTF.format(account.created)));
2021-12-20 06:59:58 +00:00
tvLast.setVisibility(compact ? View.GONE : View.VISIBLE);
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.last_modified == null ? "-" : DTF.format(account.last_modified)) +
" " + account.poll_interval +
"/" + account.keep_alive_ok +
2020-04-01 06:57:27 +00:00
"/" + account.keep_alive_failed +
"/" + account.keep_alive_succeeded : "")));
2020-10-29 19:03:09 +00:00
tvBackoff.setText(context.getString(R.string.title_backoff_until,
account.backoff_until == null ? "-" : DTF.format(account.backoff_until)));
2020-10-31 08:38:18 +00:00
tvBackoff.setVisibility(account.backoff_until == null || !settings ? View.GONE : View.VISIBLE);
2020-10-29 19:03:09 +00:00
Integer percent = account.getQuotaPercentage();
boolean warning = (percent != null && percent > EntityAccount.QUOTA_WARNING);
tvUsage.setText(settings || percent == null ? null : NF.format(percent) + "%");
tvUsage.setVisibility(settings || percent == null || (compact && !warning) ? View.GONE : View.VISIBLE);
2020-01-07 16:43:27 +00:00
tvQuota.setText(context.getString(R.string.title_storage_quota,
2020-07-02 08:19:01 +00:00
(account.quota_usage == null ? "-" : Helper.humanReadableByteCount(account.quota_usage)),
(account.quota_limit == null ? "-" : Helper.humanReadableByteCount(account.quota_limit))));
2020-10-21 16:54:47 +00:00
tvQuota.setVisibility(settings && (account.quota_usage != null || account.quota_limit != null) ? View.VISIBLE : View.GONE);
tvUsage.setTextColor(warning ? colorWarning : textColorSecondary);
tvUsage.setTypeface(warning ? Typeface.DEFAULT_BOLD : Typeface.DEFAULT);
tvQuota.setTextColor(warning ? colorWarning : textColorSecondary);
tvQuota.setTypeface(warning ? Typeface.DEFAULT_BOLD : Typeface.DEFAULT);
2020-07-03 07:57:43 +00:00
tvMaxSize.setText(account.max_size == null ? null : Helper.humanReadableByteCount(account.max_size));
2020-10-21 16:54:47 +00:00
tvMaxSize.setVisibility(settings && account.max_size != null && BuildConfig.DEBUG ? View.VISIBLE : View.GONE);
2021-04-29 18:27:01 +00:00
if (tvMaxSize.getVisibility() == View.VISIBLE)
tvQuota.setVisibility(View.VISIBLE);
2020-07-03 07:57:43 +00:00
2021-07-29 16:59:05 +00:00
tvId.setText(account.id + "/" + account.uuid);
2021-08-07 15:06:11 +00:00
tvId.setVisibility(settings && BuildConfig.DEBUG ? View.VISIBLE : View.GONE);
tvCapabilities.setText(account.capabilities);
2021-08-08 12:13:21 +00:00
tvCapabilities.setVisibility(settings && (debug || BuildConfig.DEBUG) &&
2021-08-08 06:06:54 +00:00
!TextUtils.isEmpty(account.capabilities) ? View.VISIBLE : View.GONE);
2021-07-29 16:59:05 +00:00
tvIdentity.setVisibility(account.identities > 0 || !settings ? View.GONE : View.VISIBLE);
2020-08-20 20:43:42 +00:00
tvDrafts.setVisibility(account.drafts != null || !settings ? View.GONE : View.VISIBLE);
2021-07-11 19:04:47 +00:00
tvSent.setVisibility(account.protocol != EntityAccount.TYPE_IMAP ||
account.sent != null || !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);
btnHelp.setVisibility(account.error == null ? View.GONE : View.VISIBLE);
2019-03-19 10:55:16 +00:00
ibInbox.setVisibility(settings ? 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) {
2023-12-08 09:46:17 +00:00
if (view.getId() == R.id.btnHelp) {
2019-09-01 16:20:03 +00:00
int pos = getAdapterPosition();
2023-12-08 09:46:17 +00:00
TupleAccountEx account = (pos == RecyclerView.NO_POSITION ? null : items.get(pos));
if (account == null)
Helper.viewFAQ(context, 22);
else {
Intent intent = new Intent(context, ActivityError.class);
intent.putExtra("title", "Test");
intent.putExtra("message", account.error);
intent.putExtra("provider", account.provider);
intent.putExtra("account", account.id);
intent.putExtra("protocol", account.protocol);
intent.putExtra("auth_type", account.auth_type);
intent.putExtra("personal", "personal");
intent.putExtra("address", "address");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
return;
}
2019-09-01 16:20:03 +00:00
2023-12-08 09:46:17 +00:00
int pos = getAdapterPosition();
if (pos == RecyclerView.NO_POSITION)
return;
2019-09-01 16:20:03 +00:00
2023-12-08 09:46:17 +00:00
TupleAccountEx account = items.get(pos);
if (account.tbd != null)
return;
2023-12-08 09:46:17 +00:00
if (view.getId() == R.id.ibInbox) {
Bundle args = new Bundle();
args.putLong("id", account.id);
2023-12-08 09:46:17 +00:00
new SimpleTask<EntityFolder>() {
@Override
protected EntityFolder onExecute(Context context, Bundle args) {
long id = args.getLong("id");
DB db = DB.getInstance(context);
return db.folder().getFolderByType(id, EntityFolder.INBOX);
}
2023-12-08 09:46:17 +00:00
@Override
protected void onExecuted(Bundle args, EntityFolder inbox) {
if (inbox == null)
return;
2023-12-08 09:46:17 +00:00
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
lbm.sendBroadcast(
new Intent(ActivityView.ACTION_VIEW_MESSAGES)
.putExtra("account", inbox.account)
.putExtra("folder", inbox.id)
.putExtra("type", inbox.type));
2023-12-08 09:46:17 +00:00
}
2023-12-08 09:46:17 +00:00
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(parentFragment.getParentFragmentManager(), ex);
}
}.execute(context, owner, args, "account:inbox");
} else {
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
lbm.sendBroadcast(
new Intent(settings ? ActivitySetup.ACTION_EDIT_ACCOUNT : ActivityView.ACTION_VIEW_FOLDERS)
.putExtra("id", account.id)
.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);
int order = 0;
2020-01-21 09:52:34 +00:00
SpannableString ss = new SpannableString(account.name);
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);
2019-06-25 08:53:06 +00:00
if (settings) {
2022-07-28 19:02:23 +00:00
popupMenu.getMenu().add(Menu.NONE, R.string.title_enabled, order++, R.string.title_enabled)
.setCheckable(true).setChecked(account.synchronize);
popupMenu.getMenu().add(Menu.NONE, R.string.title_account_ondemand, order++, R.string.title_account_ondemand)
.setCheckable(true).setChecked(account.ondemand);
}
popupMenu.getMenu().add(Menu.NONE, R.string.title_primary, order++, R.string.title_primary)
.setCheckable(true).setChecked(account.primary);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelId = EntityAccount.getNotificationChannelId(account.id);
2022-04-13 20:27:33 +00:00
NotificationManager nm = Helper.getSystemService(context, NotificationManager.class);
NotificationChannel channel = nm.getNotificationChannel(channelId);
if (channel == null)
popupMenu.getMenu().add(Menu.NONE, R.string.title_create_channel, order++, R.string.title_create_channel);
else {
popupMenu.getMenu().add(Menu.NONE, R.string.title_edit_channel, order++, R.string.title_edit_channel);
popupMenu.getMenu().add(Menu.NONE, R.string.title_delete_channel, order++, R.string.title_delete_channel);
}
}
if (settings)
popupMenu.getMenu().add(Menu.NONE, R.string.title_edit_properties, order++, R.string.title_edit_properties);
2019-11-23 12:48:59 +00:00
if (account.protocol == EntityAccount.TYPE_IMAP && settings)
popupMenu.getMenu().add(Menu.NONE, R.string.title_copy, order++, R.string.title_copy);
2019-08-13 10:07:30 +00:00
2022-06-18 05:30:22 +00:00
if (settings) {
popupMenu.getMenu().add(Menu.NONE, R.string.title_delete, order++, R.string.title_delete);
2021-10-27 06:03:26 +00:00
popupMenu.getMenu().add(Menu.NONE, R.string.title_log, order++, R.string.title_log);
2022-06-18 05:30:22 +00:00
}
2021-08-17 09:43:09 +00:00
2023-08-06 14:05:50 +00:00
if (debug || BuildConfig.DEBUG) {
popupMenu.getMenu().add(Menu.NONE, R.string.title_reset, order++, R.string.title_reset);
2023-08-06 14:05:50 +00:00
popupMenu.getMenu().add(Menu.NONE, R.string.title_setup_oauth_authorize, order++, R.string.title_setup_oauth_authorize);
}
2021-02-22 12:35:25 +00:00
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
2021-02-05 10:15:02 +00:00
int itemId = item.getItemId();
if (itemId == R.string.title_enabled) {
onActionSync(!item.isChecked());
return true;
} else if (itemId == R.string.title_account_ondemand) {
onActionOnDemand(!item.isChecked());
return true;
2021-02-05 10:15:02 +00:00
} else if (itemId == R.string.title_primary) {
onActionPrimary(!item.isChecked());
return true;
} else if (itemId == R.string.title_create_channel) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
onActionCreateChannel();
return true;
2021-02-05 10:15:02 +00:00
} else if (itemId == R.string.title_edit_channel) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
onActionEditChannel();
return true;
} else if (itemId == R.string.title_delete_channel) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
onActionDeleteChannel();
2021-02-05 10:15:02 +00:00
return true;
} else if (itemId == R.string.title_edit_properties) {
ViewHolder.this.onClick(view);
return true;
2021-02-05 10:15:02 +00:00
} else if (itemId == R.string.title_copy) {
onActionCopy();
return true;
} else if (itemId == R.string.title_delete) {
onActionDelete();
return true;
2021-08-17 09:43:09 +00:00
} else if (itemId == R.string.title_log) {
onActionLog();
return true;
2021-02-22 12:35:25 +00:00
} else if (itemId == R.string.title_reset) {
onActionReset();
return true;
2023-08-06 14:05:50 +00:00
} else if (itemId == R.string.title_setup_oauth_authorize) {
onActionAuthorize();
return true;
}
2021-02-05 10:15:02 +00:00
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);
2021-03-27 15:03:58 +00:00
try {
db.beginTransaction();
2021-03-27 15:03:58 +00:00
if (!sync) {
db.account().setAccountWarning(id, null);
db.account().setAccountError(id, null);
db.account().setAccountConnected(id, null);
2021-03-27 15:03:58 +00:00
}
db.account().setAccountSynchronize(id, sync);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
2019-12-09 18:44:27 +00:00
ServiceSynchronize.eval(context, "account sync=" + sync);
2021-03-27 15:03:58 +00:00
return 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");
}
private void onActionOnDemand(boolean enable) {
Bundle args = new Bundle();
args.putLong("id", account.id);
args.putBoolean("enable", enable);
new SimpleTask<Boolean>() {
@Override
protected Boolean onExecute(Context context, Bundle args) {
long id = args.getLong("id");
boolean enable = args.getBoolean("enable");
DB db = DB.getInstance(context);
try {
db.beginTransaction();
db.account().setAccountOnDemand(id, enable);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
return enable;
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(parentFragment.getParentFragmentManager(), ex);
}
}.execute(context, owner, args, "account:ondemand");
}
private void onActionPrimary(boolean primary) {
Bundle args = new Bundle();
args.putLong("id", account.id);
args.putBoolean("primary", primary);
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) {
long id = args.getLong("id");
boolean primary = args.getBoolean("primary");
DB db = DB.getInstance(context);
try {
db.beginTransaction();
if (primary)
db.account().resetPrimary();
db.account().setAccountPrimary(id, primary);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
return null;
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(parentFragment.getParentFragmentManager(), ex);
}
}.execute(context, owner, args, "account:primary");
}
@TargetApi(Build.VERSION_CODES.O)
private void onActionCreateChannel() {
if (!ActivityBilling.isPro(context)) {
context.startActivity(new Intent(context, ActivityBilling.class));
return;
}
account.createNotificationChannel(context);
Bundle args = new Bundle();
args.putLong("id", account.id);
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) {
long id = args.getLong("id");
DB db = DB.getInstance(context);
db.account().setAccountNotify(id, true);
return null;
}
@Override
protected void onExecuted(Bundle args, Void data) {
onActionEditChannel();
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(parentFragment.getParentFragmentManager(), ex);
}
}.execute(context, owner, args, "create:channel");
}
@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));
2020-06-25 14:02:25 +00:00
try {
context.startActivity(intent);
2022-01-16 07:43:56 +00:00
} catch (Throwable ex) {
Helper.reportNoViewer(context, intent, ex);
2020-06-25 14:02:25 +00:00
}
}
2019-08-13 10:07:30 +00:00
@RequiresApi(api = Build.VERSION_CODES.O)
private void onActionDeleteChannel() {
Bundle args = new Bundle();
args.putLong("id", account.id);
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) {
long id = args.getLong("id");
DB db = DB.getInstance(context);
db.account().setAccountNotify(id, false);
return null;
}
@Override
protected void onExecuted(Bundle args, Void data) {
account.deleteNotificationChannel(context);
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(parentFragment.getParentFragmentManager(), ex);
}
}.execute(context, owner, args, "create:channel");
}
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));
}
2021-02-22 12:35:25 +00:00
private void onActionDelete() {
new AlertDialog.Builder(view.getContext())
.setIcon(R.drawable.twotone_warning_24)
.setTitle(account.name)
.setMessage(R.string.title_account_delete)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
onDelete();
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing
}
})
.show();
}
2021-08-17 09:43:09 +00:00
private void onActionLog() {
if (owner.getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED))
parentFragment.getParentFragmentManager().popBackStack("logs", FragmentManager.POP_BACK_STACK_INCLUSIVE);
Bundle args = new Bundle();
args.putLong("account", account.id);
Fragment fragment = new FragmentLogs();
fragment.setArguments(args);
FragmentTransaction fragmentTransaction = parentFragment.getParentFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, fragment).addToBackStack("logs");
fragmentTransaction.commit();
}
2021-02-22 12:35:25 +00:00
private void onActionReset() {
Bundle args = new Bundle();
args.putLong("id", account.id);
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) throws Throwable {
long id = args.getLong("id");
DB db = DB.getInstance(context);
db.account().resetCreated(id);
return null;
}
2022-04-28 08:57:13 +00:00
@Override
protected void onExecuted(Bundle args, Void data) {
ToastEx.makeText(context, R.string.title_completed, Toast.LENGTH_LONG).show();
}
2021-02-22 12:35:25 +00:00
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(parentFragment.getParentFragmentManager(), ex);
}
}.execute(context, owner, args, "account:reset");
}
2023-08-06 14:05:50 +00:00
private void onActionAuthorize() {
Intent intent = new Intent(context, ActivityError.class);
intent.putExtra("title", "Test");
intent.putExtra("message", account.error);
intent.putExtra("provider", account.provider);
intent.putExtra("account", account.id);
intent.putExtra("protocol", account.protocol);
intent.putExtra("auth_type", account.auth_type);
intent.putExtra("personal", "personal");
intent.putExtra("address", "address");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
private void onDelete() {
Bundle args = new Bundle();
args.putLong("id", account.id);
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) {
long id = args.getLong("id");
DB db = DB.getInstance(context);
db.account().setAccountTbd(id);
ServiceSynchronize.eval(context, "delete account");
return null;
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(parentFragment.getParentFragmentManager(), ex);
}
}.execute(context, owner, args, "account:delete");
}
});
popupMenu.show();
return true;
}
2018-08-02 13:33:06 +00:00
}
2021-12-20 06:59:58 +00:00
AdapterAccount(final Fragment parentFragment, boolean settings, boolean compact) {
this.parentFragment = parentFragment;
this.settings = settings;
2021-12-20 06:59:58 +00:00
this.compact = compact;
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);
2021-11-30 10:27:50 +00:00
boolean color_stripe_wide = prefs.getBoolean("color_stripe_wide", false);
this.colorStripeWidth = Helper.dp2pixels(context, color_stripe_wide ? 12 : 6);
boolean highlight_unread = prefs.getBoolean("highlight_unread", true);
2021-05-12 20:08:56 +00:00
this.colorWarning = Helper.resolveColor(context, R.attr.colorWarning);
2020-10-07 06:23:51 +00:00
int colorHighlight = prefs.getInt("highlight_color", Helper.resolveColor(context, R.attr.colorUnreadHighlight));
this.colorUnread = (highlight_unread ? colorHighlight : Helper.resolveColor(context, R.attr.colorUnread));
this.textColorSecondary = Helper.resolveColor(context, android.R.attr.textColorSecondary);
this.textColorTertiary = Helper.resolveColor(context, android.R.attr.textColorTertiary);
2021-02-22 12:35:25 +00:00
this.debug = prefs.getBoolean("debug", false);
2020-10-31 08:06:06 +00:00
this.DTF = Helper.getDateTimeInstance(context, DateFormat.SHORT, DateFormat.MEDIUM);
2019-07-15 19:28:25 +00:00
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;
2021-09-28 08:18:20 +00:00
owner.getLifecycle().removeObserver(this);
}
});
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
}
});
2022-03-23 17:19:14 +00:00
try {
diff.dispatchUpdatesTo(this);
} catch (Throwable ex) {
Log.e(ex);
}
2018-08-02 13:33:06 +00:00
}
2021-12-20 06:59:58 +00:00
void setCompact(boolean compact) {
if (this.compact != compact)
this.compact = compact;
}
2020-12-30 18:29:20 +00:00
private static 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);
2020-08-20 18:42:17 +00:00
return f1.equals(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
}
2021-10-03 19:45:18 +00:00
public TupleAccountEx getItemAtPosition(int pos) {
if (pos >= 0 && pos < items.size())
return items.get(pos);
else
return null;
}
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) {
TupleAccountEx account = items.get(position);
2020-11-12 07:07:30 +00:00
holder.powner.recreate(account == null ? null : account.id);
2018-08-02 13:33:06 +00:00
2020-11-12 07:07:30 +00:00
holder.unwire();
holder.bindTo(account);
2018-08-02 13:33:06 +00:00
holder.wire();
}
}