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

1813 lines
82 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
*/
import android.app.NotificationChannel;
import android.app.NotificationManager;
2018-08-02 13:33:06 +00:00
import android.content.Context;
2021-10-24 13:13:30 +00:00
import android.content.DialogInterface;
2018-08-02 13:33:06 +00:00
import android.content.Intent;
2018-11-26 13:48:59 +00:00
import android.content.SharedPreferences;
import android.content.res.ColorStateList;
import android.graphics.Color;
2018-08-02 13:33:06 +00:00
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;
2018-11-26 08:12:44 +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;
2019-01-22 15:43:58 +00:00
import android.util.TypedValue;
2018-08-02 13:33:06 +00:00
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
2020-11-18 21:32:41 +00:00
import android.view.SubMenu;
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;
2019-08-16 14:35:33 +00:00
import android.widget.ImageButton;
2018-08-02 13:33:06 +00:00
import android.widget.ImageView;
import android.widget.TextView;
2021-02-12 12:46:21 +00:00
import android.widget.Toast;
2018-08-02 13:33:06 +00:00
2018-08-08 06:55:47 +00:00
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
2021-10-24 13:13:30 +00:00
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.PopupMenu;
2019-10-03 13:57:21 +00:00
import androidx.constraintlayout.widget.Group;
2021-01-31 12:40:19 +00:00
import androidx.core.content.pm.ShortcutInfoCompat;
2019-06-30 18:47:30 +00:00
import androidx.fragment.app.Fragment;
2022-01-11 12:34:31 +00:00
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.OnLifecycleEvent;
2022-01-11 12:34:31 +00:00
import androidx.lifecycle.ViewModelProvider;
2018-08-08 06:55:47 +00:00
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
2019-03-15 13:54:25 +00:00
import androidx.preference.PreferenceManager;
2018-08-08 06:55:47 +00:00
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListUpdateCallback;
import androidx.recyclerview.widget.RecyclerView;
2021-02-13 07:49:57 +00:00
import com.google.android.material.snackbar.Snackbar;
import java.text.DateFormat;
import java.text.NumberFormat;
2021-04-16 06:50:32 +00:00
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
2021-09-14 06:11:34 +00:00
import java.util.Comparator;
2021-04-16 06:50:32 +00:00
import java.util.Date;
2019-05-25 20:09:15 +00:00
import java.util.HashMap;
import java.util.List;
2019-05-25 20:09:15 +00:00
import java.util.Map;
2018-08-02 13:33:06 +00:00
public class AdapterFolder extends RecyclerView.Adapter<AdapterFolder.ViewHolder> {
2019-06-30 18:47:30 +00:00
private Fragment parentFragment;
private long account;
private boolean unified;
2020-04-15 18:53:46 +00:00
private boolean primary;
2019-10-03 13:57:21 +00:00
private boolean show_compact;
private boolean show_hidden;
private boolean show_flagged;
2020-03-31 11:43:58 +00:00
private boolean subscribed_only;
2021-09-14 06:11:34 +00:00
private boolean sort_unread_atop;
2019-05-25 20:51:44 +00:00
private IFolderSelectedListener listener;
2019-05-25 20:09:15 +00:00
private Context context;
private LifecycleOwner owner;
private LayoutInflater inflater;
2022-01-11 12:34:31 +00:00
private ViewModelSelected selectedModel;
private boolean subscriptions;
2019-08-14 11:49:40 +00:00
2022-02-14 07:54:02 +00:00
private int dp3;
2022-09-12 06:47:45 +00:00
private int dp6;
2018-12-03 09:39:44 +00:00
private int dp12;
2019-01-22 15:43:58 +00:00
private float textSize;
2021-11-30 10:27:50 +00:00
private int colorStripeWidth;
2019-08-16 14:24:03 +00:00
private int textColorPrimary;
private int textColorSecondary;
private int colorUnread;
2020-12-08 07:12:20 +00:00
private int colorControlNormal;
2022-01-11 12:34:31 +00:00
private int colorSeparator;
2023-06-08 18:10:05 +00:00
private boolean debug;
2018-08-02 13:33:06 +00:00
2021-09-04 07:12:45 +00:00
private String search = null;
private List<Long> disabledIds = new ArrayList<>();
private List<TupleFolderEx> all = new ArrayList<>();
2021-09-04 07:12:45 +00:00
private List<TupleFolderEx> selected = new ArrayList<>();
2018-08-02 13:33:06 +00:00
2019-07-22 07:52:08 +00:00
private NumberFormat NF = NumberFormat.getNumberInstance();
2019-03-09 13:22:49 +00:00
2022-09-12 06:47:45 +00:00
private static final int DENSE_ITEMS_THRESHOLD_FEW = 10;
private static final int DENSE_ITEMS_THRESHOLD_MANY = 50;
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
2019-03-15 11:53:22 +00:00
private View view;
2019-08-14 11:49:40 +00:00
2019-08-14 15:21:37 +00:00
private View vwColor;
2018-11-26 08:12:44 +00:00
private ImageView ivState;
2019-04-30 07:06:32 +00:00
private ImageView ivReadOnly;
2019-06-14 19:14:51 +00:00
2019-03-18 08:33:40 +00:00
private View vwLevel;
2019-09-01 16:59:51 +00:00
private ImageButton ibExpander;
2019-06-14 19:14:51 +00:00
private ImageView ivUnified;
2019-04-25 16:11:18 +00:00
private ImageView ivSubscribed;
2019-06-21 06:39:36 +00:00
private ImageView ivRule;
private ImageView ivNotify;
2021-10-26 07:39:31 +00:00
private ImageView ivAutoAdd;
2018-11-26 08:12:44 +00:00
private TextView tvName;
private TextView tvMessages;
2022-05-03 18:03:56 +00:00
private ImageButton ibMessages;
2019-06-14 19:14:51 +00:00
2019-05-22 14:29:18 +00:00
private ImageView ivType;
2018-11-26 08:12:44 +00:00
private TextView tvType;
2019-05-26 07:57:47 +00:00
private TextView tvTotal;
2018-11-26 08:12:44 +00:00
private TextView tvAfter;
private ImageButton ibSync;
2019-06-14 19:14:51 +00:00
2018-11-26 08:12:44 +00:00
private TextView tvKeywords;
private TextView tvFlagged;
2022-05-03 18:03:56 +00:00
private ImageButton ibFlagged;
2018-11-26 08:12:44 +00:00
private TextView tvError;
2019-09-01 16:20:03 +00:00
private Button btnHelp;
2019-05-03 16:59:27 +00:00
private Group grpFlagged;
2019-10-03 13:57:21 +00:00
private Group grpExtended;
2019-05-03 16:59:27 +00:00
private TwoStateOwner powner = new TwoStateOwner(owner, "FolderPopup");
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);
2019-08-14 11:49:40 +00:00
2019-08-14 15:21:37 +00:00
vwColor = itemView.findViewById(R.id.vwColor);
ivState = itemView.findViewById(R.id.ivState);
2019-04-30 07:06:32 +00:00
ivReadOnly = itemView.findViewById(R.id.ivReadOnly);
2019-06-14 19:14:51 +00:00
2019-03-18 08:33:40 +00:00
vwLevel = itemView.findViewById(R.id.vwLevel);
2019-09-01 16:59:51 +00:00
ibExpander = itemView.findViewById(R.id.ibExpander);
2019-06-14 19:14:51 +00:00
ivUnified = itemView.findViewById(R.id.ivUnified);
2019-04-25 16:11:18 +00:00
ivSubscribed = itemView.findViewById(R.id.ivSubscribed);
2019-06-21 06:39:36 +00:00
ivRule = itemView.findViewById(R.id.ivRule);
ivNotify = itemView.findViewById(R.id.ivNotify);
2021-10-26 07:39:31 +00:00
ivAutoAdd = itemView.findViewById(R.id.ivAutoAdd);
2018-08-02 13:33:06 +00:00
tvName = itemView.findViewById(R.id.tvName);
tvMessages = itemView.findViewById(R.id.tvMessages);
2022-05-03 18:03:56 +00:00
ibMessages = itemView.findViewById(R.id.ibMessages);
2019-06-14 19:14:51 +00:00
2019-05-22 14:29:18 +00:00
ivType = itemView.findViewById(R.id.ivType);
tvType = itemView.findViewById(R.id.tvType);
2019-05-26 07:57:47 +00:00
tvTotal = itemView.findViewById(R.id.tvTotal);
2018-08-02 13:33:06 +00:00
tvAfter = itemView.findViewById(R.id.tvAfter);
ibSync = itemView.findViewById(R.id.ibSync);
2019-06-14 19:14:51 +00:00
2018-11-26 08:12:44 +00:00
tvKeywords = itemView.findViewById(R.id.tvKeywords);
tvFlagged = itemView.findViewById(R.id.tvFlagged);
ibFlagged = itemView.findViewById(R.id.ibFlagged);
tvError = itemView.findViewById(R.id.tvError);
2019-09-01 16:20:03 +00:00
btnHelp = itemView.findViewById(R.id.btnHelp);
2019-10-03 13:57:21 +00:00
grpFlagged = itemView.findViewById(R.id.grpFlagged);
2019-10-03 13:57:21 +00:00
grpExtended = itemView.findViewById(R.id.grpExtended);
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() {
2019-03-15 11:53:22 +00:00
view.setOnClickListener(this);
2021-08-10 11:16:51 +00:00
view.setOnLongClickListener(this);
2019-09-01 16:59:51 +00:00
ibExpander.setOnClickListener(this);
2022-05-03 18:03:56 +00:00
if (tvMessages != null)
tvMessages.setOnClickListener(this);
if (ibMessages != null)
ibMessages.setOnClickListener(this);
if (tvFlagged != null)
tvFlagged.setOnClickListener(this);
if (ibFlagged != null)
ibFlagged.setOnClickListener(this);
if (ibSync != null)
ibSync.setOnClickListener(this);
2019-09-01 16:20:03 +00:00
if (btnHelp != null)
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);
2021-08-10 11:16:51 +00:00
view.setOnLongClickListener(null);
2019-09-01 16:59:51 +00:00
ibExpander.setOnClickListener(null);
2022-05-03 18:03:56 +00:00
if (tvMessages != null)
tvMessages.setOnClickListener(null);
if (ibMessages != null)
ibMessages.setOnClickListener(null);
if (tvFlagged != null)
tvFlagged.setOnClickListener(null);
if (ibFlagged != null)
ibFlagged.setOnClickListener(null);
if (ibSync != null)
ibSync.setOnClickListener(null);
2019-09-01 16:20:03 +00:00
if (btnHelp != null)
btnHelp.setOnClickListener(null);
2018-08-02 13:33:06 +00:00
}
2019-03-18 08:33:40 +00:00
private void bindTo(final TupleFolderEx folder) {
2021-10-14 11:40:37 +00:00
boolean disabled = isDisabled(folder);
2022-09-12 06:47:45 +00:00
int p = 0;
if (show_compact)
if (all.size() < DENSE_ITEMS_THRESHOLD_FEW)
p = dp6;
else if (all.size() < DENSE_ITEMS_THRESHOLD_MANY)
p = dp3;
2022-02-14 07:54:02 +00:00
view.setPadding(p, p, p, p);
2019-07-29 19:53:32 +00:00
view.setActivated(folder.tbc != null || folder.rename != null || folder.tbd != null);
view.setAlpha(folder.hide || folder.isHidden(listener != null) || disabled ? Helper.LOW_LIGHT : 1.0f);
2022-01-11 12:34:31 +00:00
if (listener == null && selectedModel != null)
2022-01-11 21:42:30 +00:00
itemView.setBackgroundColor(
selectedModel.isSelected(folder.id)
? colorSeparator : Color.TRANSPARENT);
2022-01-11 12:34:31 +00:00
2019-01-22 15:43:58 +00:00
if (textSize != 0)
tvName.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
2019-05-25 20:51:44 +00:00
if (listener == null) {
Integer color =
(folder.color == null && unified && EntityFolder.INBOX.equals(folder.type)
? folder.accountColor : folder.color);
vwColor.setBackgroundColor(color == null ? Color.TRANSPARENT : color);
2019-10-07 16:15:02 +00:00
vwColor.setVisibility(ActivityBilling.isPro(context) ? View.VISIBLE : View.GONE);
2019-05-25 20:51:44 +00:00
if (folder.sync_state == null || "requested".equals(folder.sync_state)) {
if (folder.executing > 0) {
2020-09-28 07:18:56 +00:00
ivState.setImageResource(R.drawable.twotone_dns_24);
ivState.setContentDescription(context.getString(R.string.title_legend_executing));
} else if ("connected".equals(folder.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(folder.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));
2019-12-29 12:02:08 +00:00
} else if ("closing".equals(folder.state)) {
ivState.setImageResource(R.drawable.twotone_cancel_24);
ivState.setContentDescription(context.getString(R.string.title_legend_closing));
} else if (folder.state == null) {
ivState.setImageResource(R.drawable.twotone_cloud_off_24);
ivState.setContentDescription(context.getString(R.string.title_legend_disconnected));
} else
2020-09-28 07:18:56 +00:00
ivState.setImageResource(R.drawable.twotone_warning_24);
2019-05-25 20:51:44 +00:00
} else {
if ("syncing".equals(folder.sync_state)) {
2020-09-28 07:18:56 +00:00
ivState.setImageResource(R.drawable.twotone_compare_arrows_24);
ivState.setContentDescription(context.getString(R.string.title_legend_synchronizing));
} else if ("downloading".equals(folder.sync_state)) {
2020-09-28 07:18:56 +00:00
ivState.setImageResource(R.drawable.twotone_cloud_download_24);
ivState.setContentDescription(context.getString(R.string.title_legend_downloading));
} else
2020-09-28 07:18:56 +00:00
ivState.setImageResource(R.drawable.twotone_warning_24);
2019-05-25 20:51:44 +00:00
}
ivState.setVisibility(
2021-01-04 07:43:24 +00:00
(folder.selectable && folder.synchronize) || folder.state != null || folder.sync_state != null
2019-05-25 20:51:44 +00:00
? View.VISIBLE : View.INVISIBLE);
2018-08-14 09:09:44 +00:00
2019-07-08 13:22:52 +00:00
if (folder.selectable)
2019-10-03 13:57:21 +00:00
ivReadOnly.setVisibility(!show_compact && folder.read_only ? View.VISIBLE : View.GONE);
2019-05-25 20:51:44 +00:00
}
2019-04-30 07:06:32 +00:00
2019-03-18 08:33:40 +00:00
ViewGroup.LayoutParams lp = vwLevel.getLayoutParams();
2020-04-15 18:53:46 +00:00
lp.width = (account < 0 && !primary ? 1 : folder.indentation) * dp12;
2019-03-18 08:33:40 +00:00
vwLevel.setLayoutParams(lp);
2019-09-01 16:59:51 +00:00
ibExpander.setImageLevel(folder.collapsed ? 1 /* more */ : 0 /* less */);
ibExpander.setContentDescription(context.getString(folder.collapsed ? R.string.title_accessibility_expand : R.string.title_accessibility_collapse));
2020-04-15 18:53:46 +00:00
ibExpander.setVisibility((account < 0 && !primary) || !folder.expander
2019-05-25 20:09:15 +00:00
? View.GONE
2019-06-05 05:46:06 +00:00
: folder.child_refs != null && folder.child_refs.size() > 0
? View.VISIBLE : View.INVISIBLE);
2019-03-18 08:33:40 +00:00
2019-07-08 13:22:52 +00:00
if (listener == null && folder.selectable) {
ivUnified.setVisibility(
(account > 0 || primary) && folder.unified && !show_compact
? View.VISIBLE : View.GONE);
ivSubscribed.setVisibility(
subscriptions && folder.subscribed != null && folder.subscribed && !show_compact
? View.VISIBLE : View.GONE);
ivRule.setVisibility(
folder.rules > 0 && !show_compact
? View.VISIBLE : View.GONE);
ivNotify.setVisibility(
folder.notify && !show_compact
? View.VISIBLE : View.GONE);
2021-10-26 07:39:31 +00:00
ivAutoAdd.setVisibility(BuildConfig.DEBUG &&
EntityFolder.SENT.equals(folder.type) &&
(folder.auto_add == null || folder.auto_add)
? View.VISIBLE : View.GONE);
2019-05-25 20:51:44 +00:00
}
2021-02-27 19:37:25 +00:00
int cunseen = (folder.collapsed ? folder.childs_unseen : 0);
int unseen = folder.unseen + cunseen;
if (unseen > 0)
2019-03-12 15:26:21 +00:00
tvName.setText(context.getString(R.string.title_name_count,
2019-06-05 05:46:06 +00:00
folder.getDisplayName(context, folder.parent_ref == null ? null : folder.parent_ref),
2021-02-27 19:37:25 +00:00
(cunseen > 0 ? "" : "") + NF.format(unseen)));
2019-03-12 15:26:21 +00:00
else
2019-10-04 14:16:23 +00:00
tvName.setText(folder.getDisplayName(context, folder.parent_ref));
2019-03-12 15:26:21 +00:00
2021-02-27 19:37:25 +00:00
tvName.setTypeface(unseen > 0 ? Typeface.DEFAULT_BOLD : Typeface.DEFAULT);
tvName.setTextColor(unseen > 0 ? colorUnread : textColorSecondary);
2019-07-08 13:22:52 +00:00
if (listener == null && folder.selectable) {
2019-05-25 20:51:44 +00:00
StringBuilder sb = new StringBuilder();
if (folder.account == null)
2019-07-22 07:52:08 +00:00
sb.append(NF.format(folder.messages));
2019-05-25 20:51:44 +00:00
else {
if (!show_compact) {
sb.append(NF.format(folder.content));
sb.append('/');
}
2019-07-22 07:52:08 +00:00
sb.append(NF.format(folder.messages));
2019-05-25 20:51:44 +00:00
}
tvMessages.setText(sb.toString());
2019-03-09 08:20:46 +00:00
2022-05-03 18:03:56 +00:00
ibMessages.setImageResource(folder.download || EntityFolder.OUTBOX.equals(folder.type)
2020-09-28 07:18:56 +00:00
? R.drawable.twotone_mail_24 : R.drawable.twotone_mail_outline_24);
2019-05-25 20:51:44 +00:00
}
2019-10-03 13:57:21 +00:00
if (folder.selectable)
2019-07-08 13:22:52 +00:00
ivType.setImageResource(EntityFolder.getIcon(folder.type));
2019-10-03 13:57:21 +00:00
2020-12-08 07:12:20 +00:00
if (listener != null) {
2019-10-03 13:57:21 +00:00
ivType.setVisibility(folder.selectable ? View.VISIBLE : View.GONE);
2020-12-08 07:12:20 +00:00
ivType.setImageTintList(ColorStateList.valueOf(folder.color == null ? colorControlNormal : folder.color));
}
2019-07-08 13:22:52 +00:00
if (listener == null && folder.selectable) {
2020-04-15 18:53:46 +00:00
if (account < 0 && !primary)
2019-05-25 20:51:44 +00:00
tvType.setText(folder.accountName);
2019-07-19 06:27:44 +00:00
else
2022-08-07 13:27:17 +00:00
tvType.setText(EntityFolder.localizeType(context, folder.type) +
(folder.inherited_type == null || !(BuildConfig.DEBUG || EntityFolder.SENT.equals(folder.inherited_type))
? ""
2023-01-18 19:54:42 +00:00
: "/" + EntityFolder.localizeType(context, folder.inherited_type)) +
(EntityFolder.FLAGGED.equals(folder.subtype) ? "*" : ""));
2020-10-26 15:25:31 +00:00
tvTotal.setText(folder.total == null ? null : NF.format(folder.total));
2019-05-26 07:57:47 +00:00
2019-05-25 20:51:44 +00:00
if (folder.account == null) {
tvAfter.setText(null);
ibSync.setImageResource(R.drawable.twotone_sync_24);
ibSync.setContentDescription(context.getString(R.string.title_legend_synchronize_on));
2019-05-25 20:51:44 +00:00
} else {
StringBuilder a = new StringBuilder();
2019-09-19 15:41:26 +00:00
if (folder.sync_days == Integer.MAX_VALUE)
a.append('∞');
else
a.append(NF.format(folder.sync_days));
2019-05-25 20:51:44 +00:00
a.append('/');
2019-09-19 15:41:26 +00:00
2019-05-25 20:51:44 +00:00
if (folder.keep_days == Integer.MAX_VALUE)
a.append('∞');
else
2019-07-22 07:52:08 +00:00
a.append(NF.format(folder.keep_days));
2019-09-19 15:41:26 +00:00
2019-05-25 20:51:44 +00:00
tvAfter.setText(a.toString());
if (folder.synchronize) {
ibSync.setImageResource(folder.poll
? R.drawable.twotone_hourglass_top_24
: R.drawable.twotone_sync_24);
ibSync.setContentDescription(context.getString(folder.poll
? R.string.title_legend_synchronize_poll
: R.string.title_legend_synchronize_on));
} else {
ibSync.setImageResource(R.drawable.twotone_sync_disabled_24);
ibSync.setContentDescription(context.getString(R.string.title_legend_synchronize_off));
}
2019-05-25 20:51:44 +00:00
}
ibSync.setImageTintList(ColorStateList.valueOf(
2020-11-16 10:05:32 +00:00
folder.synchronize && folder.initialize != 0 &&
!EntityFolder.OUTBOX.equals(folder.type) &&
folder.accountProtocol == EntityAccount.TYPE_IMAP
2019-08-16 14:24:03 +00:00
? textColorPrimary : textColorSecondary));
ibSync.setEnabled(folder.last_sync != null);
2018-08-13 14:44:47 +00:00
2023-06-08 18:10:05 +00:00
tvKeywords.setText(debug ?
2021-09-24 07:51:57 +00:00
(folder.separator == null ? "" : folder.separator + " ") +
(folder.namespace == null ? "" : folder.namespace + " ") +
2023-04-18 15:59:28 +00:00
(folder.flags == null ? null : TextUtils.join(" ", folder.flags) + " ") +
2021-09-24 07:51:57 +00:00
TextUtils.join(" ", folder.keywords) : null);
2020-05-19 07:47:28 +00:00
tvKeywords.setVisibility(show_flagged ? View.VISIBLE : View.GONE);
tvFlagged.setText(NF.format(folder.flagged));
ibFlagged.setImageResource(folder.flagged == 0
2021-09-20 05:28:10 +00:00
? R.drawable.twotone_star_border_24 : R.drawable.twotone_star_24);
2018-11-26 08:12:44 +00:00
2019-05-25 20:51:44 +00:00
tvError.setText(folder.error);
tvError.setVisibility(folder.error != null ? View.VISIBLE : View.GONE);
2019-09-01 16:20:03 +00:00
if (btnHelp != null)
btnHelp.setVisibility(folder.error == null ? View.GONE : View.VISIBLE);
2019-10-03 13:57:21 +00:00
grpFlagged.setVisibility(show_flagged ? View.VISIBLE : View.GONE);
2019-10-03 13:57:21 +00:00
grpExtended.setVisibility(show_compact ? View.GONE : View.VISIBLE);
2019-05-25 20:51:44 +00:00
}
}
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)
2021-01-29 16:19:10 +00:00
Helper.viewFAQ(view.getContext(), 22);
2019-09-01 16:20:03 +00:00
else {
int pos = getAdapterPosition();
if (pos == RecyclerView.NO_POSITION)
return;
2021-09-04 07:12:45 +00:00
TupleFolderEx folder = selected.get(pos);
if (folder.tbd != null)
2019-09-01 16:20:03 +00:00
return;
2021-02-05 10:15:02 +00:00
int id = view.getId();
if (id == R.id.ibExpander) {
2021-05-12 19:49:35 +00:00
onCollapse(folder, pos);
2022-05-03 18:03:56 +00:00
} else if (show_flagged &&
(id == R.id.tvMessages || id == R.id.ibMessages)) {
onUnread(folder);
2021-02-05 10:15:02 +00:00
} else if (id == R.id.tvFlagged || id == R.id.ibFlagged) {
onFlagged(folder);
} else if (id == R.id.ibSync) {
onLastSync(folder);
2021-02-05 10:15:02 +00:00
} else {
2021-10-14 11:40:37 +00:00
if (isDisabled(folder))
return;
2021-02-05 10:15:02 +00:00
2021-10-14 11:40:37 +00:00
if (listener == null) {
2022-01-11 12:34:31 +00:00
if (selectedModel != null)
selectedModel.select(folder.id);
2021-02-05 10:15:02 +00:00
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
lbm.sendBroadcast(
new Intent(ActivityView.ACTION_VIEW_MESSAGES)
.putExtra("account", folder.account)
.putExtra("folder", folder.id)
.putExtra("type", folder.type));
2021-10-14 11:40:37 +00:00
} else
2021-02-05 10:15:02 +00:00
listener.onFolderSelected(folder);
}
2019-03-17 21:23:55 +00:00
}
2018-08-02 13:33:06 +00:00
}
2021-10-14 11:40:37 +00:00
private boolean isDisabled(EntityFolder folder) {
return (!folder.selectable ||
(folder.read_only && listener != null) ||
disabledIds.contains(folder.id));
}
2021-05-12 19:49:35 +00:00
private void onCollapse(TupleFolderEx folder, int pos) {
2019-05-25 20:51:44 +00:00
if (listener != null) {
folder.collapsed = !folder.collapsed;
2021-05-12 19:49:35 +00:00
notifyItemChanged(pos); // Update expander
set(all);
2019-05-25 20:51:44 +00:00
return;
}
2019-03-18 08:33:40 +00:00
Bundle args = new Bundle();
args.putLong("id", folder.id);
args.putBoolean("collapsed", !folder.collapsed);
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) {
long id = args.getLong("id");
boolean collapsed = args.getBoolean("collapsed");
DB db = DB.getInstance(context);
db.folder().setFolderCollapsed(id, collapsed);
return null;
}
@Override
protected void onException(Bundle args, Throwable ex) {
2019-12-06 07:50:46 +00:00
Log.unexpectedError(parentFragment.getParentFragmentManager(), ex);
2019-03-18 08:33:40 +00:00
}
}.execute(context, owner, args, "folder:collapse");
}
2022-05-03 18:03:56 +00:00
private void onUnread(TupleFolderEx folder) {
BoundaryCallbackMessages.SearchCriteria criteria = new BoundaryCallbackMessages.SearchCriteria();
criteria.in_senders = false;
criteria.in_recipients = false;
criteria.in_subject = false;
criteria.in_keywords = false;
criteria.in_message = false;
criteria.in_notes = false;
criteria.with_unseen = true;
FragmentMessages.search(
context, owner, parentFragment.getParentFragmentManager(),
folder.account, folder.id, false, criteria);
}
private void onFlagged(TupleFolderEx folder) {
BoundaryCallbackMessages.SearchCriteria criteria = new BoundaryCallbackMessages.SearchCriteria();
criteria.in_senders = false;
criteria.in_recipients = false;
criteria.in_subject = false;
criteria.in_keywords = false;
criteria.in_message = false;
2021-02-28 15:12:48 +00:00
criteria.in_notes = false;
criteria.with_flagged = true;
FragmentMessages.search(
context, owner, parentFragment.getParentFragmentManager(),
folder.account, folder.id, false, criteria);
}
private void onLastSync(TupleFolderEx folder) {
if (folder.last_sync == null)
return;
DateFormat DTF = Helper.getDateTimeInstance(context, SimpleDateFormat.LONG, SimpleDateFormat.LONG);
ToastEx.makeText(context, DTF.format(folder.last_sync), Toast.LENGTH_LONG).show();
}
@Override
public boolean onLongClick(View v) {
int pos = getAdapterPosition();
if (pos == RecyclerView.NO_POSITION)
return false;
2021-09-04 07:12:45 +00:00
final TupleFolderEx folder = selected.get(pos);
2020-12-17 14:05:44 +00:00
if (folder.tbd != null || folder.local)
return false;
2021-08-10 11:16:51 +00:00
if (listener != null)
return listener.onFolderLongPress(folder);
2019-09-19 15:41:26 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
2021-02-12 12:46:21 +00:00
boolean perform_expunge = prefs.getBoolean("perform_expunge", true);
boolean debug = prefs.getBoolean("debug", false);
2019-09-19 15:41:26 +00:00
2020-10-14 08:10:18 +00:00
int order = 1;
2019-05-25 20:09:15 +00:00
PopupMenuLifecycle popupMenu = new PopupMenuLifecycle(context, powner, view);
String title;
if (folder.last_sync == null)
title = folder.getDisplayName(context);
else
title = context.getString(R.string.title_name_count,
folder.getDisplayName(context),
Helper.getRelativeTimeSpanString(context, folder.last_sync));
2020-10-26 15:25:31 +00:00
SpannableString ss = new SpannableString(title);
ss.setSpan(new StyleSpan(Typeface.ITALIC), 0, ss.length(), 0);
ss.setSpan(new RelativeSizeSpan(0.9f), 0, ss.length(), 0);
popupMenu.getMenu().add(Menu.NONE, 0, 0, ss).setEnabled(false);
2020-01-21 09:52:34 +00:00
if (folder.selectable)
2020-10-14 08:10:18 +00:00
popupMenu.getMenu().add(Menu.NONE, R.string.title_synchronize_now, order++, R.string.title_synchronize_now);
if (folder.selectable) {
2019-11-23 12:48:59 +00:00
if (folder.account != null && folder.accountProtocol == EntityAccount.TYPE_IMAP) {
2020-10-14 08:10:18 +00:00
popupMenu.getMenu().add(Menu.NONE, R.string.title_synchronize_more, order++, R.string.title_synchronize_more);
2020-10-14 08:10:18 +00:00
popupMenu.getMenu().add(Menu.NONE, R.string.title_delete_local, order++, R.string.title_delete_local);
popupMenu.getMenu().add(Menu.NONE, R.string.title_delete_browsed, order++, R.string.title_delete_browsed);
2021-02-12 12:46:21 +00:00
if (!perform_expunge || BuildConfig.DEBUG)
2021-10-24 13:13:30 +00:00
popupMenu.getMenu().add(Menu.NONE, R.string.title_expunge, order++, R.string.title_expunge);
2019-11-11 18:29:19 +00:00
}
2019-07-01 19:32:41 +00:00
2020-07-26 17:58:13 +00:00
if (EntityFolder.TRASH.equals(folder.type))
2020-10-14 08:10:18 +00:00
popupMenu.getMenu().add(Menu.NONE, R.string.title_empty_trash, order++, R.string.title_empty_trash);
2020-07-26 17:58:13 +00:00
else if (EntityFolder.JUNK.equals(folder.type))
2020-10-14 08:10:18 +00:00
popupMenu.getMenu().add(Menu.NONE, R.string.title_empty_spam, order++, R.string.title_empty_spam);
2020-07-26 17:58:13 +00:00
2019-11-11 18:29:19 +00:00
if (folder.account != null) {
2020-10-14 08:10:18 +00:00
popupMenu.getMenu().add(Menu.NONE, R.string.title_unified_folder, order++, R.string.title_unified_folder)
2020-04-15 09:42:15 +00:00
.setCheckable(true).setChecked(folder.unified);
2019-05-25 15:14:51 +00:00
2020-10-14 08:10:18 +00:00
popupMenu.getMenu().add(Menu.NONE, R.string.title_navigation_folder, order++, R.string.title_navigation_folder)
2019-11-11 18:29:19 +00:00
.setCheckable(true).setChecked(folder.navigation);
2019-05-25 15:14:51 +00:00
2020-10-14 08:10:18 +00:00
popupMenu.getMenu().add(Menu.NONE, R.string.title_notify_folder, order++, R.string.title_notify_folder)
2019-11-11 18:29:19 +00:00
.setCheckable(true).setChecked(folder.notify);
}
2019-05-25 15:14:51 +00:00
2019-11-23 12:48:59 +00:00
if (folder.account != null && folder.accountProtocol == EntityAccount.TYPE_IMAP) {
2019-11-11 18:29:19 +00:00
boolean subscriptions = prefs.getBoolean("subscriptions", false);
2020-04-01 11:21:40 +00:00
if (subscriptions && !folder.read_only)
2020-10-14 08:10:18 +00:00
popupMenu.getMenu().add(Menu.NONE, R.string.title_subscribe, order++, R.string.title_subscribe)
2020-03-31 11:43:58 +00:00
.setCheckable(true).setChecked(folder.subscribed != null && folder.subscribed);
2019-11-11 18:29:19 +00:00
2020-10-14 08:10:18 +00:00
popupMenu.getMenu().add(Menu.NONE, R.string.title_synchronize_enabled, order++, R.string.title_synchronize_enabled)
2019-11-11 18:29:19 +00:00
.setCheckable(true).setChecked(folder.synchronize);
2021-04-15 06:02:14 +00:00
if (!folder.read_only) {
2020-10-14 08:10:18 +00:00
popupMenu.getMenu().add(Menu.NONE, R.string.title_edit_rules, order++, R.string.title_edit_rules);
2021-04-15 06:02:14 +00:00
popupMenu.getMenu().add(Menu.NONE, R.string.title_execute_rules, order++, R.string.title_execute_rules);
}
2020-11-15 14:09:06 +00:00
}
popupMenu.getMenu().add(Menu.NONE, R.string.title_edit_properties, order++, R.string.title_edit_properties);
2019-11-11 18:29:19 +00:00
2020-11-15 14:09:06 +00:00
if (folder.account != null && folder.accountProtocol == EntityAccount.TYPE_IMAP) {
2019-11-11 18:29:19 +00:00
if (folder.notify && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelId = EntityFolder.getNotificationChannelId(folder.id);
2022-04-13 20:27:33 +00:00
NotificationManager nm = Helper.getSystemService(context, NotificationManager.class);
2019-11-11 18:29:19 +00:00
NotificationChannel channel = nm.getNotificationChannel(channelId);
if (channel == null)
2020-10-14 08:10:18 +00:00
popupMenu.getMenu().add(Menu.NONE, R.string.title_create_channel, order++, R.string.title_create_channel);
2019-11-11 18:29:19 +00:00
else {
2020-10-14 08:10:18 +00:00
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);
2019-11-11 18:29:19 +00:00
}
}
}
2019-01-18 08:23:53 +00:00
}
2021-04-15 06:02:14 +00:00
if (EntityFolder.INBOX.equals(folder.type) && folder.accountProtocol == EntityAccount.TYPE_POP) {
2021-01-31 11:30:08 +00:00
popupMenu.getMenu().add(Menu.NONE, R.string.title_edit_rules, order++, R.string.title_edit_rules);
2021-04-15 06:02:14 +00:00
popupMenu.getMenu().add(Menu.NONE, R.string.title_execute_rules, order++, R.string.title_execute_rules);
}
2020-07-01 06:57:17 +00:00
if (folder.accountProtocol == EntityAccount.TYPE_POP ||
(folder.selectable && (debug || BuildConfig.DEBUG)))
2021-04-16 06:50:32 +00:00
popupMenu.getMenu().add(Menu.NONE, R.string.title_export_messages, order++, R.string.title_export_messages);
2023-01-09 16:42:57 +00:00
if (!folder.selectable)
popupMenu.getMenu()
.add(Menu.NONE, R.string.title_hide_folder, order++, R.string.title_hide_folder)
.setCheckable(true)
.setChecked(folder.hide);
2023-08-16 05:47:26 +00:00
int children = 0;
2020-11-19 12:51:00 +00:00
if (folder.child_refs != null)
for (TupleFolderEx child : folder.child_refs)
if (child.selectable)
2023-08-16 05:47:26 +00:00
children++;
if (children > 0) {
2020-11-19 12:51:00 +00:00
SubMenu submenu = popupMenu.getMenu()
.addSubMenu(Menu.NONE, Menu.NONE, order++, R.string.title_synchronize_subfolders);
submenu.add(Menu.FIRST, R.string.title_synchronize_now, 1, R.string.title_synchronize_now);
submenu.add(Menu.FIRST, R.string.title_synchronize_batch_enable, 2, R.string.title_synchronize_batch_enable);
submenu.add(Menu.FIRST, R.string.title_synchronize_batch_disable, 3, R.string.title_synchronize_batch_disable);
submenu.add(Menu.FIRST, R.string.title_notify_batch_enable, 4, R.string.title_notify_batch_enable);
submenu.add(Menu.FIRST, R.string.title_notify_batch_disable, 5, R.string.title_notify_batch_disable);
submenu.add(Menu.FIRST, R.string.title_unified_inbox_add, 6, R.string.title_unified_inbox_add);
submenu.add(Menu.FIRST, R.string.title_unified_inbox_delete, 7, R.string.title_unified_inbox_delete);
submenu.add(Menu.FIRST, R.string.title_navigation_folder, 6, R.string.title_navigation_folder);
submenu.add(Menu.FIRST, R.string.title_navigation_folder_hide, 7, R.string.title_navigation_folder_hide);
submenu.add(Menu.FIRST, R.string.title_synchronize_more, 8, R.string.title_synchronize_more);
submenu.add(Menu.FIRST, R.string.title_download_batch_enable, 9, R.string.title_download_batch_enable);
submenu.add(Menu.FIRST, R.string.title_download_batch_disable, 10, R.string.title_download_batch_disable);
2020-11-19 12:51:00 +00:00
}
2019-11-23 12:48:59 +00:00
if (folder.account != null && folder.accountProtocol == EntityAccount.TYPE_IMAP)
2021-01-31 11:30:08 +00:00
popupMenu.getMenu().add(Menu.NONE, R.string.title_create_sub_folder, order++, R.string.title_create_sub_folder)
2020-02-24 08:32:03 +00:00
.setEnabled(folder.inferiors);
2019-11-11 18:29:19 +00:00
if (folder.selectable && Shortcuts.can(context))
2021-01-31 12:40:19 +00:00
popupMenu.getMenu().add(Menu.NONE, R.string.title_pin, order++, R.string.title_pin);
2021-12-10 10:41:20 +00:00
if (!folder.read_only && EntityFolder.USER.equals(folder.type))
popupMenu.getMenu().add(Menu.NONE, R.string.title_delete, order++, R.string.title_delete);
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
2020-11-18 21:32:41 +00:00
if (item.getGroupId() == Menu.FIRST) {
2021-02-05 10:15:02 +00:00
int itemId = item.getItemId();
if (itemId == R.string.title_synchronize_now) {
onActionSync(true);
2020-10-14 08:10:18 +00:00
return true;
2021-02-05 10:15:02 +00:00
} else if (itemId == R.string.title_synchronize_batch_enable) {
onActionEnableSync(true);
return true;
2021-02-05 10:15:02 +00:00
} else if (itemId == R.string.title_synchronize_batch_disable) {
onActionEnableSync(false);
return true;
} else if (itemId == R.string.title_notify_batch_enable) {
onActionEnableNotify(true);
return true;
} else if (itemId == R.string.title_notify_batch_disable) {
onActionEnableNotify(false);
return true;
} else if (itemId == R.string.title_unified_inbox_add) {
onActionUnifiedInbox(true);
return true;
} else if (itemId == R.string.title_unified_inbox_delete) {
onActionUnifiedInbox(false);
return true;
} else if (itemId == R.string.title_navigation_folder) {
onActionEnableNavigationMenu(true);
return true;
} else if (itemId == R.string.title_navigation_folder_hide) {
onActionEnableNavigationMenu(false);
return true;
2022-03-21 15:33:08 +00:00
} else if (itemId == R.string.title_synchronize_more) {
onActionSyncMore(true);
return true;
} else if (itemId == R.string.title_download_batch_enable) {
onActionEnableDownload(true);
return true;
} else if (itemId == R.string.title_download_batch_disable) {
onActionEnableDownload(false);
return true;
2021-02-05 10:15:02 +00:00
}
return false;
}
2021-02-05 10:15:02 +00:00
int itemId = item.getItemId();
if (itemId == R.string.title_synchronize_now) {
onActionSync(false);
return true;
} else if (itemId == R.string.title_synchronize_more) {
2022-03-21 15:33:08 +00:00
onActionSyncMore(false);
2021-02-05 10:15:02 +00:00
return true;
} else if (itemId == R.string.title_unified_folder || itemId == R.string.title_navigation_folder || itemId == R.string.title_notify_folder || itemId == R.string.title_synchronize_enabled) {
2021-04-16 13:40:00 +00:00
onActionProperty(itemId, !item.isChecked());
2021-02-05 10:15:02 +00:00
return true;
} else if (itemId == R.string.title_subscribe) {
onActionSubscribe();
return true;
} else if (itemId == R.string.title_delete_local) {
OnActionDeleteLocal(false);
return true;
} else if (itemId == R.string.title_delete_browsed) {
OnActionDeleteLocal(true);
return true;
2021-10-24 13:13:30 +00:00
} else if (itemId == R.string.title_expunge) {
2021-02-12 12:46:21 +00:00
onActionExpunge();
return true;
2021-02-05 10:15:02 +00:00
} else if (itemId == R.string.title_empty_trash) {
onActionEmpty(EntityFolder.TRASH);
return true;
} else if (itemId == R.string.title_empty_spam) {
onActionEmpty(EntityFolder.JUNK);
return true;
} else if (itemId == R.string.title_edit_rules) {
onActionEditRules();
return true;
2021-04-15 06:02:14 +00:00
} else if (itemId == R.string.title_execute_rules) {
onActionExecuteRules();
return true;
2021-04-16 06:50:32 +00:00
} else if (itemId == R.string.title_export_messages) {
onActionExportMessages();
return true;
2021-02-05 10:15:02 +00:00
} else if (itemId == R.string.title_edit_properties) {
onActionEditProperties();
return true;
} else if (itemId == R.string.title_create_channel) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
onActionCreateChannel();
return true;
} 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();
return true;
2023-01-09 16:42:57 +00:00
} else if (itemId == R.string.title_hide_folder) {
onActionHide();
return true;
2021-02-05 10:15:02 +00:00
} else if (itemId == R.string.title_create_sub_folder) {
onActionCreateFolder();
return true;
} else if (itemId == R.string.title_pin) {
onActionPinFolder();
return true;
} else if (itemId == R.string.title_delete) {
onActionDeleteFolder();
return true;
2018-11-22 07:57:34 +00:00
}
2021-02-05 10:15:02 +00:00
return false;
2018-11-22 07:57:34 +00:00
}
2018-09-19 14:09:00 +00:00
2023-08-16 05:47:26 +00:00
private void onActionSync(boolean children) {
2019-07-19 05:08:30 +00:00
Bundle args = new Bundle();
args.putLong("folder", folder.id);
2023-08-16 05:47:26 +00:00
args.putBoolean("children", children);
2020-10-14 08:10:18 +00:00
2021-02-13 07:49:57 +00:00
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) {
long fid = args.getLong("folder");
2023-08-16 05:47:26 +00:00
boolean children = args.getBoolean("children");
2021-02-13 07:49:57 +00:00
if (!ConnectionHelper.getNetworkState(context).isSuitable())
throw new IllegalStateException(context.getString(R.string.title_no_internet));
boolean now = true;
DB db = DB.getInstance(context);
try {
db.beginTransaction();
EntityFolder folder = db.folder().getFolder(fid);
if (folder == null)
return null;
if (folder.selectable)
2023-08-16 05:47:26 +00:00
EntityOperation.sync(context, folder.id, true, !children);
2021-02-13 07:49:57 +00:00
2023-08-16 05:47:26 +00:00
if (children) {
2021-02-13 07:49:57 +00:00
List<EntityFolder> folders = db.folder().getChildFolders(folder.id);
if (folders != null)
for (EntityFolder child : folders)
if (child.selectable)
EntityOperation.sync(context, child.id, true);
}
if (folder.account != null) {
EntityAccount account = db.account().getAccount(folder.account);
if (account != null && !"connected".equals(account.state))
now = false;
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
ServiceSynchronize.eval(context, "refresh/folder");
2021-02-13 07:49:57 +00:00
if (!now)
throw new IllegalArgumentException(context.getString(R.string.title_no_connection));
return null;
}
@Override
protected void onException(Bundle args, Throwable ex) {
if (ex instanceof IllegalStateException) {
2023-12-12 19:55:55 +00:00
Snackbar snackbar = Snackbar.make(parentFragment.getView(), new ThrowableWrapper(ex).getSafeMessage(), Snackbar.LENGTH_LONG)
2021-02-13 07:49:57 +00:00
.setGestureInsetBottomIgnored(true);
snackbar.setAction(R.string.title_fix, new View.OnClickListener() {
@Override
2021-08-13 14:39:44 +00:00
public void onClick(View v) {
v.getContext().startActivity(new Intent(v.getContext(), ActivitySetup.class)
2022-08-25 05:28:06 +00:00
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK)
2021-08-13 14:39:44 +00:00
.putExtra("tab", "connection"));
2021-02-13 07:49:57 +00:00
}
});
snackbar.show();
} else if (ex instanceof IllegalArgumentException)
2023-12-12 19:55:55 +00:00
Snackbar.make(view, new ThrowableWrapper(ex).getSafeMessage(), Snackbar.LENGTH_LONG)
2021-02-13 07:49:57 +00:00
.setGestureInsetBottomIgnored(true).show();
else
Log.unexpectedError(parentFragment.getParentFragmentManager(), ex);
}
}.execute(context, owner, args, "folder:sync");
2019-07-19 05:08:30 +00:00
}
private void onActionEnableSync(boolean enabled) {
2020-11-18 21:32:41 +00:00
Bundle args = new Bundle();
args.putLong("id", folder.id);
args.putLong("account", folder.account);
args.putBoolean("enabled", enabled);
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) throws Throwable {
long id = args.getLong("id");
long aid = args.getLong("account");
boolean enabled = args.getBoolean("enabled");
DB db = DB.getInstance(context);
try {
db.beginTransaction();
2023-08-16 05:47:26 +00:00
List<EntityFolder> children = db.folder().getChildFolders(id);
if (children == null)
2020-11-18 21:32:41 +00:00
return null;
2023-08-16 05:47:26 +00:00
for (EntityFolder child : children)
2020-11-18 21:32:41 +00:00
db.folder().setFolderSynchronize(child.id, enabled);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
ServiceSynchronize.reload(context, aid, false, "child sync=" + enabled);
return null;
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(parentFragment.getParentFragmentManager(), ex);
}
2023-08-16 05:47:26 +00:00
}.execute(context, owner, args, "children:sync");
}
private void onActionEnableNotify(boolean enabled) {
Bundle args = new Bundle();
args.putLong("id", folder.id);
args.putBoolean("enabled", enabled);
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) throws Throwable {
long id = args.getLong("id");
boolean enabled = args.getBoolean("enabled");
DB db = DB.getInstance(context);
try {
db.beginTransaction();
2023-08-16 05:47:26 +00:00
List<EntityFolder> children = db.folder().getChildFolders(id);
if (children == null)
return null;
2023-08-16 05:47:26 +00:00
for (EntityFolder child : children)
db.folder().setFolderNotify(child.id, enabled);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
return null;
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(parentFragment.getParentFragmentManager(), ex);
}
2023-08-16 05:47:26 +00:00
}.execute(context, owner, args, "children:notify");
}
private void onActionUnifiedInbox(boolean add) {
Bundle args = new Bundle();
args.putLong("id", folder.id);
args.putBoolean("add", add);
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) throws Throwable {
long id = args.getLong("id");
boolean add = args.getBoolean("add");
DB db = DB.getInstance(context);
try {
db.beginTransaction();
2023-08-16 05:47:26 +00:00
List<EntityFolder> children = db.folder().getChildFolders(id);
if (children == null)
return null;
2023-08-16 05:47:26 +00:00
for (EntityFolder child : children)
db.folder().setFolderUnified(child.id, add);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
return null;
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(parentFragment.getParentFragmentManager(), ex);
}
2023-08-16 05:47:26 +00:00
}.execute(context, owner, args, "children:unified");
}
private void onActionEnableNavigationMenu(boolean enabled) {
Bundle args = new Bundle();
args.putLong("id", folder.id);
args.putBoolean("enabled", enabled);
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) throws Throwable {
long id = args.getLong("id");
boolean enabled = args.getBoolean("enabled");
DB db = DB.getInstance(context);
try {
db.beginTransaction();
2023-08-16 05:47:26 +00:00
List<EntityFolder> children = db.folder().getChildFolders(id);
if (children == null)
return null;
2023-08-16 05:47:26 +00:00
for (EntityFolder child : children)
db.folder().setFolderNavigation(child.id, enabled);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
return null;
}
@Override
protected void onException(Bundle args, Throwable ex) {
2020-11-18 21:32:41 +00:00
Log.unexpectedError(parentFragment.getParentFragmentManager(), ex);
}
2023-08-16 05:47:26 +00:00
}.execute(context, owner, args, "children:navigation");
2020-11-18 21:32:41 +00:00
}
2022-03-21 15:33:08 +00:00
private void onActionSyncMore(boolean children) {
2020-03-23 07:33:19 +00:00
Bundle args = new Bundle();
args.putLong("folder", folder.id);
args.putString("name", folder.getDisplayName(context));
2022-03-21 15:33:08 +00:00
args.putBoolean("children", children);
2019-06-30 18:47:30 +00:00
2020-03-23 07:33:19 +00:00
FragmentDialogSync sync = new FragmentDialogSync();
sync.setArguments(args);
2021-02-13 07:49:57 +00:00
sync.show(parentFragment.getParentFragmentManager(), "folder:months");
2019-06-24 10:45:52 +00:00
}
private void onActionEnableDownload(boolean enabled) {
Bundle args = new Bundle();
args.putLong("id", folder.id);
args.putBoolean("enabled", enabled);
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) throws Throwable {
long id = args.getLong("id");
boolean enabled = args.getBoolean("enabled");
DB db = DB.getInstance(context);
try {
db.beginTransaction();
List<EntityFolder> children = db.folder().getChildFolders(id);
if (children == null)
return null;
for (EntityFolder child : children)
db.folder().setFolderDownload(child.id, enabled);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
return null;
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(parentFragment.getParentFragmentManager(), ex);
}
}.execute(context, owner, args, "children:download");
}
2019-05-25 15:14:51 +00:00
private void onActionProperty(int property, boolean enabled) {
Bundle args = new Bundle();
args.putLong("id", folder.id);
2019-12-09 18:44:27 +00:00
args.putLong("account", folder.account);
2019-05-25 15:14:51 +00:00
args.putInt("property", property);
args.putBoolean("enabled", enabled);
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) {
long id = args.getLong("id");
2019-12-09 18:44:27 +00:00
long aid = args.getLong("account");
2019-05-25 15:14:51 +00:00
int property = args.getInt("property");
boolean enabled = args.getBoolean("enabled");
DB db = DB.getInstance(context);
2021-02-05 10:15:02 +00:00
if (property == R.string.title_unified_folder) {
db.folder().setFolderUnified(id, enabled);
} else if (property == R.string.title_navigation_folder) {
db.folder().setFolderNavigation(id, enabled);
} else if (property == R.string.title_notify_folder) {
db.folder().setFolderNotify(id, enabled);
} else if (property == R.string.title_synchronize_enabled) {
db.folder().setFolderSynchronize(id, enabled);
ServiceSynchronize.reload(context, aid, false, "folder sync=" + enabled);
} else {
throw new IllegalArgumentException("Unknown folder property=" + property);
2019-05-25 15:14:51 +00:00
}
return null;
}
@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, "folder:enable");
}
private void onActionSubscribe() {
Bundle args = new Bundle();
args.putLong("id", folder.id);
2020-03-31 11:43:58 +00:00
args.putBoolean("subscribed", !(folder.subscribed != null && folder.subscribed));
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) {
long id = args.getLong("id");
boolean subscribed = args.getBoolean("subscribed");
EntityOperation.subscribe(context, id, subscribed);
ServiceSynchronize.eval(context, "subscribed=" + subscribed);
return null;
}
@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, "folder:subscribe");
}
2019-03-13 06:40:15 +00:00
private void OnActionDeleteLocal(final boolean browsed) {
2019-06-30 18:47:30 +00:00
Bundle aargs = new Bundle();
aargs.putString("question", context.getString(R.string.title_ask_delete_local));
aargs.putLong("folder", folder.id);
aargs.putBoolean("browsed", browsed);
FragmentDialogAsk ask = new FragmentDialogAsk();
ask.setArguments(aargs);
ask.setTargetFragment(parentFragment, FragmentFolders.REQUEST_DELETE_LOCAL);
2019-10-12 15:16:53 +00:00
ask.show(parentFragment.getParentFragmentManager(), "folder:delete_local");
2018-11-22 07:57:34 +00:00
}
2018-09-19 14:09:00 +00:00
2021-02-12 12:46:21 +00:00
private void onActionExpunge() {
2021-10-24 13:13:30 +00:00
new AlertDialog.Builder(view.getContext())
2021-10-26 10:57:23 +00:00
.setIcon(R.drawable.twotone_warning_24)
2021-10-24 13:13:30 +00:00
.setTitle(R.string.title_expunge)
.setMessage(R.string.title_expunge_remark)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
expunge();
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing
}
})
.show();
}
private void expunge() {
2021-02-12 12:46:21 +00:00
Bundle args = new Bundle();
args.putLong("id", folder.id);
new SimpleTask<Void>() {
@Override
protected void onPreExecute(Bundle args) {
ToastEx.makeText(context, R.string.title_executing, Toast.LENGTH_LONG).show();
}
@Override
protected Void onExecute(Context context, Bundle args) throws Throwable {
long id = args.getLong("id");
DB db = DB.getInstance(context);
EntityFolder folder = db.folder().getFolder(id);
if (folder == null)
return null;
EntityOperation.queue(context, folder, EntityOperation.EXPUNGE);
return null;
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(parentFragment.getParentFragmentManager(), ex);
}
}.execute(context, owner, args, "folder:expunge");
}
2019-09-30 07:15:47 +00:00
private void onActionEmpty(String type) {
2019-06-30 18:47:30 +00:00
Bundle aargs = new Bundle();
2019-09-30 07:15:47 +00:00
if (EntityFolder.TRASH.equals(type))
aargs.putString("question", context.getString(R.string.title_empty_trash_ask));
else if (EntityFolder.JUNK.equals(type))
aargs.putString("question", context.getString(R.string.title_empty_spam_ask));
else
throw new IllegalArgumentException("Invalid folder type=" + type);
2020-10-25 13:55:58 +00:00
aargs.putBoolean("warning", true);
2020-07-26 10:16:48 +00:00
aargs.putString("remark", context.getString(R.string.title_empty_all));
2019-06-30 18:47:30 +00:00
aargs.putLong("folder", folder.id);
2019-09-30 07:15:47 +00:00
aargs.putString("type", type);
2019-06-30 18:47:30 +00:00
FragmentDialogAsk ask = new FragmentDialogAsk();
ask.setArguments(aargs);
2019-09-30 07:15:47 +00:00
ask.setTargetFragment(parentFragment, FragmentFolders.REQUEST_EMPTY_FOLDER);
2019-10-12 15:16:53 +00:00
ask.show(parentFragment.getParentFragmentManager(), "folder:empty");
2018-12-29 10:33:22 +00:00
}
2019-01-18 08:23:53 +00:00
private void onActionEditRules() {
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
lbm.sendBroadcast(
new Intent(ActivityView.ACTION_EDIT_RULES)
.putExtra("account", folder.account)
2020-09-05 20:55:34 +00:00
.putExtra("protocol", folder.accountProtocol)
2020-07-01 06:57:17 +00:00
.putExtra("folder", folder.id)
2020-09-05 20:55:34 +00:00
.putExtra("type", folder.type));
2019-01-18 08:23:53 +00:00
}
2019-02-21 07:38:30 +00:00
2021-04-15 06:02:14 +00:00
private void onActionExecuteRules() {
Bundle args = new Bundle();
2021-04-15 07:17:49 +00:00
args.putString("question", context.getString(R.string.title_execute_rules));
2021-12-07 18:34:23 +00:00
args.putLong("id", folder.id);
2021-04-15 06:02:14 +00:00
2021-04-15 07:17:49 +00:00
FragmentDialogAsk ask = new FragmentDialogAsk();
ask.setArguments(args);
ask.setTargetFragment(parentFragment, FragmentFolders.REQUEST_EXECUTE_RULES);
ask.show(parentFragment.getParentFragmentManager(), "folder:execute");
2021-04-15 06:02:14 +00:00
}
2021-04-16 06:50:32 +00:00
private void onActionExportMessages() {
2022-07-23 05:25:57 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
prefs.edit().putBoolean("debug", false).apply();
String filename = Helper.sanitizeFilename(
folder.accountName.replace(" ", "_") + "_" +
folder.getDisplayName(context).replace(" ", "_") + "_" +
new SimpleDateFormat("yyyyMMdd").format(new Date().getTime()) + ".mbox");
2021-04-16 06:50:32 +00:00
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
2021-04-16 06:50:32 +00:00
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_TITLE, filename);
2023-01-27 08:56:32 +00:00
Helper.openAdvanced(context, intent);
2021-04-16 06:50:32 +00:00
if (intent.resolveActivity(context.getPackageManager()) == null) { // // system/GET_CONTENT whitelisted
2023-12-06 10:06:33 +00:00
Log.unexpectedError(parentFragment.getParentFragmentManager(),
new IllegalArgumentException(context.getString(R.string.title_no_saf)), 25);
2021-04-16 06:50:32 +00:00
return;
}
parentFragment.getArguments().putLong("selected_folder", folder.id);
parentFragment.startActivityForResult(
Helper.getChooser(context, intent),
FragmentFolders.REQUEST_EXPORT_MESSAGES);
}
2019-02-21 07:38:30 +00:00
private void onActionEditProperties() {
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
lbm.sendBroadcast(
new Intent(ActivityView.ACTION_EDIT_FOLDER)
2020-11-15 14:09:06 +00:00
.putExtra("id", folder.id)
.putExtra("imap", folder.accountProtocol == EntityAccount.TYPE_IMAP));
2019-02-21 07:38:30 +00:00
}
@RequiresApi(api = Build.VERSION_CODES.O)
private void onActionCreateChannel() {
2019-08-13 08:27:17 +00:00
if (!ActivityBilling.isPro(context)) {
context.startActivity(new Intent(context, ActivityBilling.class));
2019-05-16 09:10:01 +00:00
return;
}
folder.createNotificationChannel(context);
onActionEditChannel();
}
@RequiresApi(api = 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, EntityFolder.getNotificationChannelId(folder.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
}
}
@RequiresApi(api = Build.VERSION_CODES.O)
private void onActionDeleteChannel() {
folder.deleteNotificationChannel(context);
}
2019-06-26 06:55:27 +00:00
2023-01-09 16:42:57 +00:00
private void onActionHide() {
Bundle args = new Bundle();
args.putLong("id", folder.id);
args.putBoolean("hide", !folder.hide);
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) throws Throwable {
long id = args.getLong("id");
boolean hide = args.getBoolean("hide");
DB db = DB.getInstance(context);
db.folder().setFolderHide(id, hide);
return null;
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(parentFragment.getParentFragmentManager(), ex);
}
}.execute(context, owner, args, "folder:hide");
}
2019-06-26 06:55:27 +00:00
private void onActionCreateFolder() {
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
lbm.sendBroadcast(
new Intent(ActivityView.ACTION_EDIT_FOLDER)
.putExtra("account", folder.account)
.putExtra("parent", folder.name));
}
2021-01-31 12:40:19 +00:00
private void onActionPinFolder() {
2021-04-13 18:47:47 +00:00
ShortcutInfoCompat.Builder builder = Shortcuts.getShortcut(context, folder);
2022-04-14 05:57:34 +00:00
Shortcuts.requestPinShortcut(context, builder.build());
2021-01-31 12:40:19 +00:00
}
private void onActionDeleteFolder() {
Bundle aargs = new Bundle();
aargs.putLong("id", folder.id);
2022-03-03 08:14:44 +00:00
aargs.putString("remark", folder.name);
aargs.putString("question", context.getString(R.string.title_folder_delete));
2020-10-25 13:55:58 +00:00
aargs.putBoolean("warning", true);
FragmentDialogAsk ask = new FragmentDialogAsk();
ask.setArguments(aargs);
ask.setTargetFragment(parentFragment, FragmentFolders.REQUEST_DELETE_FOLDER);
ask.show(parentFragment.getParentFragmentManager(), "folder:delete");
}
});
popupMenu.show();
return true;
}
2018-08-02 13:33:06 +00:00
}
AdapterFolder(Fragment parentFragment, long account, boolean unified, boolean primary, boolean show_compact, boolean show_hidden, boolean show_flagged, IFolderSelectedListener listener) {
this(parentFragment.getContext(), parentFragment.getViewLifecycleOwner(), account, unified, primary, show_compact, show_hidden, show_flagged, listener);
2019-06-30 18:47:30 +00:00
this.parentFragment = parentFragment;
}
AdapterFolder(Context context, LifecycleOwner owner, long account, boolean unified, boolean primary, boolean show_compact, boolean show_hidden, boolean show_flagged, IFolderSelectedListener listener) {
2019-05-25 20:09:15 +00:00
this.account = account;
this.unified = unified;
2020-04-15 18:53:46 +00:00
this.primary = primary;
2019-10-03 13:57:21 +00:00
this.show_compact = show_compact;
this.show_hidden = show_hidden;
this.show_flagged = show_flagged;
2019-05-25 20:51:44 +00:00
this.listener = listener;
2018-11-26 13:48:59 +00:00
this.context = context;
this.owner = owner;
this.inflater = LayoutInflater.from(context);
2022-01-12 13:40:14 +00:00
if (context instanceof FragmentActivity && BuildConfig.DEBUG)
2022-01-11 12:34:31 +00:00
this.selectedModel = new ViewModelProvider((FragmentActivity) context)
.get(ViewModelSelected.class);
2018-11-26 13:48:59 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
2019-01-22 15:43:58 +00:00
boolean compact = prefs.getBoolean("compact", false);
2019-11-15 15:08:03 +00:00
int zoom = prefs.getInt("view_zoom", compact ? 0 : 1);
2019-01-22 15:43:58 +00:00
if (zoom == 0)
zoom = 1;
this.subscriptions = prefs.getBoolean("subscriptions", false);
2020-03-31 11:43:58 +00:00
this.subscribed_only = prefs.getBoolean("subscribed_only", false) && subscriptions;
2021-09-14 06:11:34 +00:00
this.sort_unread_atop = prefs.getBoolean("sort_unread_atop", false);
2018-11-26 13:48:59 +00:00
2022-02-14 07:54:02 +00:00
this.dp3 = Helper.dp2pixels(context, 3);
2022-09-12 06:47:45 +00:00
this.dp6 = Helper.dp2pixels(context, 6);
2018-12-30 14:35:19 +00:00
this.dp12 = Helper.dp2pixels(context, 12);
2019-01-22 15:43:58 +00:00
this.textSize = Helper.getTextSize(context, zoom);
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);
2019-08-16 14:24:03 +00:00
this.textColorPrimary = Helper.resolveColor(context, android.R.attr.textColorPrimary);
this.textColorSecondary = Helper.resolveColor(context, android.R.attr.textColorSecondary);
2018-12-03 09:39:44 +00:00
boolean highlight_unread = prefs.getBoolean("highlight_unread", true);
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));
2023-07-21 05:16:41 +00:00
this.colorControlNormal = Helper.resolveColor(context, androidx.appcompat.R.attr.colorControlNormal);
2022-01-11 12:34:31 +00:00
this.colorSeparator = Helper.resolveColor(context, R.attr.colorSeparator);
2023-06-08 18:10:05 +00:00
this.debug = prefs.getBoolean("debug", false);
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(AdapterFolder.this + " parent destroyed");
AdapterFolder.this.parentFragment = null;
2021-09-28 08:18:20 +00:00
owner.getLifecycle().removeObserver(this);
}
});
2018-08-02 13:33:06 +00:00
}
2019-10-03 13:57:21 +00:00
void setCompact(boolean compact) {
2020-11-19 10:46:20 +00:00
if (this.show_compact != compact)
2019-10-03 13:57:21 +00:00
this.show_compact = compact;
}
2019-06-15 09:58:56 +00:00
void setShowHidden(boolean show_hidden) {
if (this.show_hidden != show_hidden) {
this.show_hidden = show_hidden;
set(all);
}
}
void setShowFlagged(boolean show_flagged) {
2020-11-19 10:46:20 +00:00
if (this.show_flagged != show_flagged)
this.show_flagged = show_flagged;
}
2020-03-31 11:43:58 +00:00
void setSubscribedOnly(boolean subscribed_only) {
if (this.subscribed_only != subscribed_only) {
this.subscribed_only = subscribed_only;
set(all);
}
}
2021-09-14 06:11:34 +00:00
void setSortUnreadAtop(boolean sort_unread_atop) {
if (this.sort_unread_atop != sort_unread_atop) {
this.sort_unread_atop = sort_unread_atop;
set(all);
}
}
void setDisabled(List<Long> ids) {
disabledIds = ids;
}
2019-05-25 20:09:15 +00:00
public void set(@NonNull List<TupleFolderEx> folders) {
Log.i("Set folders=" + folders.size() + " search=" + search);
all = folders;
2019-03-18 13:08:31 +00:00
List<TupleFolderEx> hierarchical;
2020-04-15 18:53:46 +00:00
if (account < 0 && !primary) {
List<TupleFolderEx> filtered = new ArrayList<>();
for (TupleFolderEx folder : folders)
if (show_hidden || !folder.isHidden(listener != null))
filtered.add(folder);
if (filtered.size() > 0)
Collections.sort(filtered, filtered.get(0).getComparator(context));
2021-09-14 06:11:34 +00:00
if (sort_unread_atop)
Collections.sort(filtered, new Comparator<TupleFolderEx>() {
2021-09-14 06:11:34 +00:00
@Override
public int compare(TupleFolderEx f1, TupleFolderEx f2) {
return -Boolean.compare(f1.unseen > 0, f2.unseen > 0);
}
});
hierarchical = filtered;
} else {
List<TupleFolderEx> parents = new ArrayList<>();
Map<Long, TupleFolderEx> idFolder = new HashMap<>();
Map<Long, List<TupleFolderEx>> parentChilds = new HashMap<>();
for (TupleFolderEx folder : folders) {
2023-11-19 19:35:02 +00:00
folder.indentation = 0;
folder.expander = true;
folder.parent_ref = null;
folder.child_refs = null;
folder.childs_unseen = 0;
idFolder.put(folder.id, folder);
if (folder.parent == null)
parents.add(folder);
else {
if (!parentChilds.containsKey(folder.parent))
parentChilds.put(folder.parent, new ArrayList<TupleFolderEx>());
parentChilds.get(folder.parent).add(folder);
}
2019-05-25 20:09:15 +00:00
}
TupleFolderEx root = new TupleFolderEx();
root.name = "[root]";
root.child_refs = parents;
for (TupleFolderEx parent : parents)
parent.parent_ref = root;
for (long pid : parentChilds.keySet()) {
TupleFolderEx parent = idFolder.get(pid);
if (parent != null) {
parent.child_refs = parentChilds.get(pid);
for (TupleFolderEx child : parent.child_refs)
child.parent_ref = parent;
}
2019-05-25 20:51:44 +00:00
}
2019-05-25 20:09:15 +00:00
2019-06-07 16:25:32 +00:00
boolean anyChild = false;
for (TupleFolderEx parent : parents)
if (parent.child_refs != null && parent.child_refs.size() > 0) {
anyChild = true;
break;
}
for (TupleFolderEx parent : parents) {
2019-06-07 16:25:32 +00:00
parent.expander = anyChild;
if (!parent.selectable && parent.child_refs != null && EntityFolder.USER.equals(parent.type))
for (TupleFolderEx child : parent.child_refs)
if (!EntityFolder.USER.equals(child.type)) {
parent.type = EntityFolder.SYSTEM;
break;
}
}
2021-09-14 06:11:34 +00:00
hierarchical = getHierarchical(parents, anyChild ? 0 : 1, sort_unread_atop);
}
2019-05-25 20:09:15 +00:00
2021-09-04 07:12:45 +00:00
List<TupleFolderEx> items;
if (TextUtils.isEmpty(search))
items = hierarchical;
else {
items = new ArrayList<>();
String query = search.toLowerCase().trim();
for (TupleFolderEx item : hierarchical)
if (item.getDisplayName(context).toLowerCase().contains(query))
items.add(item);
}
DiffUtil.DiffResult diff = DiffUtil.calculateDiff(new DiffCallback(selected, items), false);
2019-05-25 20:09:15 +00:00
2021-09-04 07:12:45 +00:00
selected = items;
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);
/*
java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling eu.faircode.email.FixedRecyclerView{bc0fa01 VFED..... ........ 0,0-1080,1984 #7f0a0533 app:id/rvFolder}, adapter:eu.faircode.email.AdapterFolder@b1cf0a6, layout:androidx.recyclerview.widget.LinearLayoutManager@3093ae7, context:eu.faircode.email.ActivityView@832e020
at androidx.recyclerview.widget.RecyclerView.assertNotInLayoutOrScroll(SourceFile:3)
at androidx.recyclerview.widget.RecyclerView$RecyclerViewDataObserver.onItemRangeChanged(SourceFile:1)
at androidx.recyclerview.widget.RecyclerView$AdapterDataObservable.notifyItemRangeChanged(SourceFile:3)
at androidx.recyclerview.widget.RecyclerView$Adapter.notifyItemRangeChanged(SourceFile:2)
at androidx.recyclerview.widget.AdapterListUpdateCallback.onChanged(SourceFile:1)
at androidx.recyclerview.widget.BatchingListUpdateCallback.dispatchLastEvent(SourceFile:2)
at androidx.recyclerview.widget.BatchingListUpdateCallback.onChanged(SourceFile:4)
at androidx.recyclerview.widget.DiffUtil$DiffResult.dispatchUpdatesTo(SourceFile:34)
at androidx.recyclerview.widget.DiffUtil$DiffResult.dispatchUpdatesTo(SourceFile:1)
at eu.faircode.email.AdapterFolder.set(SourceFile:46)
at eu.faircode.email.FragmentFolders$12.onChanged(SourceFile:3)
at eu.faircode.email.FragmentFolders$12.onChanged(SourceFile:1)
at androidx.lifecycle.LiveData.considerNotify(SourceFile:6)
at androidx.lifecycle.LiveData.dispatchingValue(SourceFile:8)
at androidx.lifecycle.LiveData.setValue(SourceFile:4)
at androidx.lifecycle.LiveData$1.run(SourceFile:5)
at android.os.Handler.handleCallback(Handler.java:938)
*/
}
2018-08-02 13:33:06 +00:00
}
2021-09-04 07:12:45 +00:00
public void search(String query) {
Log.i("Contacts query=" + query);
search = query;
set(all);
}
2020-02-05 12:37:36 +00:00
public void search(String query, final int result, final ISearchResult intf) {
if (TextUtils.isEmpty(query)) {
intf.onNotFound();
return;
}
// Expand all
for (TupleFolderEx folder : all)
folder.collapsed = false;
set(all);
// Delay search until after expanding
2020-08-23 15:34:14 +00:00
ApplicationEx.getMainHandler().post(new Runnable() {
2020-02-05 12:37:36 +00:00
@Override
public void run() {
int pos = -1;
int next = -1;
int count = result + 1;
2021-09-04 07:12:45 +00:00
for (int i = 0; i < selected.size(); i++)
if (selected.get(i).getDisplayName(context).toLowerCase().contains(query.toLowerCase())) {
2020-02-05 12:37:36 +00:00
count--;
if (count == 0)
pos = i;
else if (count < 0) {
next = i;
break;
}
}
if (pos < 0)
intf.onNotFound();
else
intf.onFound(pos, next >= 0);
}
});
}
interface ISearchResult {
void onFound(int pos, boolean hasNext);
2019-12-08 16:22:55 +00:00
2020-02-05 12:37:36 +00:00
void onNotFound();
2019-12-08 16:22:55 +00:00
}
2021-09-14 06:11:34 +00:00
private List<TupleFolderEx> getHierarchical(List<TupleFolderEx> parents, int indentation, boolean sort_unread_atop) {
2019-05-25 20:09:15 +00:00
List<TupleFolderEx> result = new ArrayList<>();
if (parents.size() > 0)
Collections.sort(parents, parents.get(0).getComparator(context));
2021-09-14 06:11:34 +00:00
if (sort_unread_atop)
Collections.sort(parents, new Comparator<TupleFolderEx>() {
@Override
public int compare(TupleFolderEx f1, TupleFolderEx f2) {
return -Boolean.compare(f1.unseen > 0, f2.unseen > 0);
}
});
for (TupleFolderEx parent : parents) {
if (parent.hide && !show_hidden)
continue;
List<TupleFolderEx> childs = null;
if (parent.child_refs != null) {
2021-09-14 06:11:34 +00:00
childs = getHierarchical(parent.child_refs, indentation + 1, sort_unread_atop);
2021-02-27 19:37:25 +00:00
for (TupleFolderEx child : childs) {
parent.childs_unseen += child.unseen;
if (child.collapsed)
parent.childs_unseen += child.childs_unseen;
}
}
if (!subscribed_only ||
2021-04-11 17:27:08 +00:00
EntityFolder.INBOX.equals(parent.type) ||
parent.accountProtocol != EntityAccount.TYPE_IMAP ||
(parent.subscribed != null && parent.subscribed) ||
(childs != null && childs.size() > 0)) {
2019-06-15 09:58:56 +00:00
parent.indentation = indentation;
if (show_hidden || !parent.isHidden(listener != null)) {
2023-01-13 12:57:25 +00:00
result.add(parent);
if (!parent.collapsed && childs != null)
result.addAll(childs);
}
2019-06-15 09:58:56 +00:00
}
}
2019-05-25 20:09:15 +00:00
return result;
}
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<TupleFolderEx> prev = new ArrayList<>();
private List<TupleFolderEx> next = new ArrayList<>();
2018-08-02 13:33:06 +00:00
2019-02-10 18:02:55 +00:00
DiffCallback(List<TupleFolderEx> prev, List<TupleFolderEx> 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) {
TupleFolderEx f1 = prev.get(oldItemPosition);
TupleFolderEx f2 = next.get(newItemPosition);
return f1.id.equals(f2.id);
}
@Override
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
TupleFolderEx f1 = prev.get(oldItemPosition);
TupleFolderEx f2 = next.get(newItemPosition);
2019-05-31 06:32:20 +00:00
if (!f1.equals(f2))
return false;
2019-11-18 14:27:31 +00:00
if (f1.indentation != f2.indentation ||
f1.expander != f2.expander)
return false;
2019-05-31 06:32:20 +00:00
TupleFolderEx p1 = f1.parent_ref;
TupleFolderEx p2 = f2.parent_ref;
while (p1 != null && p2 != null) {
2019-06-15 09:58:56 +00:00
if (p1.hide != p2.hide)
return false;
2019-05-31 06:32:20 +00:00
if (p1.collapsed != p2.collapsed)
return false;
p1 = p1.parent_ref;
p2 = p2.parent_ref;
}
2023-11-19 18:03:02 +00:00
if (p1 != null || p2 != null)
return false;
2019-05-31 06:32:20 +00:00
return true;
2018-08-02 13:33:06 +00:00
}
}
@Override
public long getItemId(int position) {
2021-09-04 07:12:45 +00:00
return selected.get(position).id;
2018-08-02 13:33:06 +00:00
}
int getPositionForKey(long key) {
for (int pos = 0; pos < selected.size(); pos++)
if (selected.get(pos).id.equals(key))
return pos;
return RecyclerView.NO_POSITION;
}
public TupleFolderEx getItemAtPosition(int pos) {
if (pos >= 0 && pos < selected.size())
return selected.get(pos);
else
return null;
}
2018-08-02 13:33:06 +00:00
@Override
public int getItemCount() {
2021-09-04 07:12:45 +00:00
return selected.size();
2018-08-02 13:33:06 +00:00
}
2019-07-08 13:22:52 +00:00
@Override
public int getItemViewType(int position) {
2019-10-03 13:57:21 +00:00
if (listener == null)
2021-09-04 07:12:45 +00:00
return (selected.get(position).selectable ? R.layout.item_folder : R.layout.item_folder_unselectable);
2019-10-03 13:57:21 +00:00
else
2019-07-08 13:22:52 +00:00
return R.layout.item_folder_select;
}
2018-08-02 13:33:06 +00:00
@Override
@NonNull
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
2019-07-08 13:22:52 +00:00
return new ViewHolder(inflater.inflate(viewType, parent, false));
2018-08-02 13:33:06 +00:00
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
2021-09-04 07:12:45 +00:00
TupleFolderEx folder = selected.get(position);
2020-11-12 07:07:30 +00:00
holder.powner.recreate(folder == null ? null : folder.id);
2018-08-02 13:33:06 +00:00
2020-11-12 07:07:30 +00:00
holder.unwire();
holder.bindTo(folder);
holder.wire();
2018-08-02 13:33:06 +00:00
}
2019-05-25 20:51:44 +00:00
interface IFolderSelectedListener {
2021-04-01 20:00:18 +00:00
void onFolderSelected(@NonNull TupleFolderEx folder);
2021-08-10 11:16:51 +00:00
boolean onFolderLongPress(@NonNull TupleFolderEx folder);
2019-05-25 20:51:44 +00:00
}
2018-08-02 13:33:06 +00:00
}