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

788 lines
34 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-02-11 09:44:59 +00:00
import static android.app.Activity.RESULT_OK;
2018-08-02 13:33:06 +00:00
import android.content.Context;
2019-06-30 11:06:12 +00:00
import android.content.Intent;
2021-11-12 18:38:59 +00:00
import android.content.SharedPreferences;
2020-11-08 19:21:50 +00:00
import android.database.sqlite.SQLiteConstraintException;
2019-10-07 16:15:02 +00:00
import android.graphics.Color;
2018-08-02 13:33:06 +00:00
import android.os.Bundle;
import android.text.TextUtils;
2023-04-25 10:37:50 +00:00
import android.util.Pair;
2018-08-02 13:33:06 +00:00
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
2018-08-02 13:33:06 +00:00
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
2018-08-02 13:33:06 +00:00
import android.widget.EditText;
2021-07-03 08:34:51 +00:00
import android.widget.ImageButton;
import android.widget.ScrollView;
2019-06-26 06:55:27 +00:00
import android.widget.TextView;
2018-08-02 13:33:06 +00:00
2022-06-12 07:22:55 +00:00
import androidx.activity.OnBackPressedCallback;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
2019-06-26 06:55:27 +00:00
import androidx.constraintlayout.widget.Group;
2019-07-12 12:18:56 +00:00
import androidx.lifecycle.Lifecycle;
2021-11-12 18:38:59 +00:00
import androidx.preference.PreferenceManager;
import com.google.android.material.snackbar.Snackbar;
2019-06-16 08:22:43 +00:00
import java.util.Objects;
2019-01-15 17:41:55 +00:00
public class FragmentFolder extends FragmentBase {
private ViewGroup view;
private ScrollView scroll;
2019-06-26 06:55:27 +00:00
private TextView tvParent;
private EditText etName;
2018-09-19 17:47:03 +00:00
private EditText etDisplay;
2019-10-07 16:15:02 +00:00
private ViewButtonColor btnColor;
2019-06-15 09:58:56 +00:00
private CheckBox cbHide;
private CheckBox cbHideSeen;
private CheckBox cbUnified;
private CheckBox cbNavigation;
2023-02-03 08:38:06 +00:00
private CheckBox cbCountUnread;
private CheckBox cbNotify;
2018-08-02 13:33:06 +00:00
private CheckBox cbSynchronize;
private CheckBox cbPoll;
2020-02-25 20:06:17 +00:00
private EditText etPoll;
private TextView tvPoll;
2021-03-13 17:09:45 +00:00
private TextView tvPollRemark;
private CheckBox cbDownload;
private CheckBox cbAutoClassifySource;
private CheckBox cbAutoClassifyTarget;
2021-01-10 17:43:23 +00:00
private TextView tvAutoClassifyPro;
2021-07-03 08:34:51 +00:00
private ImageButton ibInfo;
2018-11-14 09:49:59 +00:00
private EditText etSyncDays;
private EditText etKeepDays;
private CheckBox cbKeepAll;
private CheckBox cbAutoDelete;
2021-04-05 13:05:24 +00:00
private TextView tvAutoDeleteArchiveHint;
2018-08-06 10:27:21 +00:00
private Button btnSave;
2018-12-27 12:31:02 +00:00
private ContentLoadingProgressBar pbSave;
2020-05-21 11:17:42 +00:00
private TextView tvInboxRootHint;
2018-12-27 12:31:02 +00:00
private ContentLoadingProgressBar pbWait;
2020-11-15 14:09:06 +00:00
private Group grpImap;
2019-06-26 06:55:27 +00:00
private Group grpParent;
2020-02-25 20:06:17 +00:00
private Group grpPoll;
2020-04-18 06:24:15 +00:00
private Group grpAutoDelete;
2018-08-02 13:33:06 +00:00
2018-08-27 14:30:16 +00:00
private long id = -1;
2020-11-15 14:09:06 +00:00
private boolean imap = true;
private long account = -1;
2019-06-26 06:55:27 +00:00
private String parent = null;
private boolean saving = false;
2018-12-22 11:40:47 +00:00
private boolean deletable = false;
2018-08-27 14:30:16 +00:00
2019-10-07 16:15:02 +00:00
private static final int REQUEST_COLOR = 1;
2019-06-30 11:06:12 +00:00
private static final int REQUEST_SAVE_CHANGES = 101;
private static final int REQUEST_DELETE_FOLDER = 102;
2018-08-27 14:30:16 +00:00
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get arguments
Bundle args = getArguments();
2018-12-04 10:40:15 +00:00
id = args.getLong("id", -1);
2020-11-15 14:09:06 +00:00
imap = args.getBoolean("imap", true);
2018-12-04 10:40:15 +00:00
account = args.getLong("account", -1);
2019-06-26 06:55:27 +00:00
parent = args.getString("parent");
2018-08-27 14:30:16 +00:00
}
2018-08-02 13:33:06 +00:00
@Override
@Nullable
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
2024-01-09 19:48:48 +00:00
setSubtitle(id < 0 ? R.string.title_new_folder : R.string.title_edit_folder);
setHasOptionsMenu(true);
view = (ViewGroup) inflater.inflate(R.layout.fragment_folder, container, false);
scroll = view.findViewById(R.id.scroll);
2018-08-02 13:33:06 +00:00
// Get controls
etName = view.findViewById(R.id.etName);
2019-06-26 06:55:27 +00:00
tvParent = view.findViewById(R.id.tvParent);
2018-09-19 17:47:03 +00:00
etDisplay = view.findViewById(R.id.etDisplay);
2019-10-07 16:15:02 +00:00
btnColor = view.findViewById(R.id.btnColor);
2019-06-15 09:58:56 +00:00
cbHide = view.findViewById(R.id.cbHide);
cbHideSeen = view.findViewById(R.id.cbHideSeen);
cbUnified = view.findViewById(R.id.cbUnified);
cbNavigation = view.findViewById(R.id.cbNavigation);
2023-02-03 08:38:06 +00:00
cbCountUnread = view.findViewById(R.id.cbCountUnread);
cbNotify = view.findViewById(R.id.cbNotify);
2018-08-02 13:33:06 +00:00
cbSynchronize = view.findViewById(R.id.cbSynchronize);
cbPoll = view.findViewById(R.id.cbPoll);
2020-02-25 20:06:17 +00:00
etPoll = view.findViewById(R.id.etPoll);
tvPoll = view.findViewById(R.id.tvPoll);
2021-03-13 17:09:45 +00:00
tvPollRemark = view.findViewById(R.id.tvPollRemark);
cbDownload = view.findViewById(R.id.cbDownload);
cbAutoClassifySource = view.findViewById(R.id.cbAutoClassifySource);
cbAutoClassifyTarget = view.findViewById(R.id.cbAutoClassifyTarget);
2021-01-10 17:43:23 +00:00
tvAutoClassifyPro = view.findViewById(R.id.tvAutoClassifyPro);
2021-07-03 08:34:51 +00:00
ibInfo = view.findViewById(R.id.ibInfo);
2018-11-14 09:49:59 +00:00
etSyncDays = view.findViewById(R.id.etSyncDays);
etKeepDays = view.findViewById(R.id.etKeepDays);
cbKeepAll = view.findViewById(R.id.cbKeepAll);
cbAutoDelete = view.findViewById(R.id.cbAutoDelete);
2021-04-05 13:05:24 +00:00
tvAutoDeleteArchiveHint = view.findViewById(R.id.tvAutoDeleteArchiveHint);
2018-08-06 10:27:21 +00:00
btnSave = view.findViewById(R.id.btnSave);
pbSave = view.findViewById(R.id.pbSave);
2020-05-21 11:17:42 +00:00
tvInboxRootHint = view.findViewById(R.id.tvInboxRootHint);
2018-08-04 17:41:48 +00:00
pbWait = view.findViewById(R.id.pbWait);
2020-11-15 14:09:06 +00:00
grpImap = view.findViewById(R.id.grpImap);
2019-06-26 06:55:27 +00:00
grpParent = view.findViewById(R.id.grpParent);
2020-02-25 20:06:17 +00:00
grpPoll = view.findViewById(R.id.grpPoll);
2020-04-18 06:24:15 +00:00
grpAutoDelete = view.findViewById(R.id.grpAutoDelete);
2018-08-02 13:33:06 +00:00
2019-10-07 16:15:02 +00:00
btnColor.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle args = new Bundle();
args.putInt("color", btnColor.getColor());
args.putString("title", getString(R.string.title_color));
args.putBoolean("reset", true);
FragmentDialogColor fragment = new FragmentDialogColor();
fragment.setArguments(args);
fragment.setTargetFragment(FragmentFolder.this, REQUEST_COLOR);
2019-10-12 15:16:53 +00:00
fragment.show(getParentFragmentManager(), "account:color");
2019-10-07 16:15:02 +00:00
}
});
cbHide.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
cbHideSeen.setEnabled(!isChecked);
}
});
cbSynchronize.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
cbPoll.setEnabled(isChecked);
2021-03-13 17:09:45 +00:00
tvPollRemark.setVisibility(imap && cbPoll.isEnabled() && !cbPoll.isChecked()
? View.VISIBLE : View.GONE);
grpPoll.setVisibility(imap && cbPoll.isEnabled() && cbPoll.isChecked()
? View.VISIBLE : View.GONE);
}
});
2020-02-25 20:06:17 +00:00
cbPoll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
2021-03-13 17:09:45 +00:00
tvPollRemark.setVisibility(imap && cbPoll.isEnabled() && !cbPoll.isChecked()
? View.VISIBLE : View.GONE);
grpPoll.setVisibility(imap && cbPoll.isEnabled() && cbPoll.isChecked()
? View.VISIBLE : View.GONE);
2020-02-25 20:06:17 +00:00
}
});
2021-01-16 14:09:12 +00:00
cbDownload.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
2021-01-15 21:46:01 +00:00
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
2021-01-16 14:09:12 +00:00
cbAutoClassifySource.setEnabled(isChecked);
cbAutoClassifyTarget.setEnabled(isChecked && cbAutoClassifySource.isChecked());
2021-01-15 21:46:01 +00:00
}
});
2021-01-16 14:09:12 +00:00
cbAutoClassifySource.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
2021-01-16 14:09:12 +00:00
cbAutoClassifyTarget.setEnabled(isChecked);
}
});
2021-01-10 17:43:23 +00:00
Helper.linkPro(tvAutoClassifyPro);
2021-07-03 08:34:51 +00:00
ibInfo.setOnClickListener(new View.OnClickListener() {
2019-08-22 08:20:07 +00:00
@Override
2020-11-11 16:35:32 +00:00
public void onClick(View v) {
Helper.viewFAQ(v.getContext(), 39);
2019-08-22 08:20:07 +00:00
}
});
cbKeepAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
etKeepDays.setEnabled(!isChecked);
cbAutoDelete.setEnabled(!isChecked);
}
});
2018-08-06 10:27:21 +00:00
btnSave.setOnClickListener(new View.OnClickListener() {
2018-08-02 13:33:06 +00:00
@Override
public void onClick(View v) {
2019-06-16 08:22:43 +00:00
onSave(false);
}
});
2022-06-15 14:30:50 +00:00
setBackPressedCallback(new OnBackPressedCallback(true) {
2020-01-30 19:52:18 +00:00
@Override
2022-06-12 07:22:55 +00:00
public void handleOnBackPressed() {
2022-06-12 11:19:38 +00:00
if (Helper.isKeyboardVisible(view))
Helper.hideKeyboard(view);
else
onSave(true);
2018-08-02 13:33:06 +00:00
}
});
2018-08-04 17:41:48 +00:00
// Initialize
2020-11-15 14:09:06 +00:00
Helper.setViewsEnabled(view, false);
grpImap.setVisibility(imap ? View.VISIBLE : View.GONE);
2019-06-26 06:55:27 +00:00
tvParent.setText(parent);
grpParent.setVisibility(parent == null ? View.GONE : View.VISIBLE);
cbAutoClassifySource.setVisibility(View.GONE);
cbAutoClassifyTarget.setVisibility(View.GONE);
2021-01-10 17:43:23 +00:00
tvAutoClassifyPro.setVisibility(View.GONE);
2021-04-05 13:05:24 +00:00
tvAutoDeleteArchiveHint.setVisibility(View.GONE);
2020-04-18 06:24:15 +00:00
grpAutoDelete.setVisibility(View.GONE);
btnSave.setEnabled(false);
pbSave.setVisibility(View.GONE);
2020-05-21 11:17:42 +00:00
tvInboxRootHint.setVisibility(View.GONE);
2020-08-03 06:31:48 +00:00
pbWait.setVisibility(View.VISIBLE);
2018-08-04 17:41:48 +00:00
return view;
}
2019-06-30 11:06:12 +00:00
@Override
public void onActivityCreated(@Nullable final Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Bundle args = new Bundle();
args.putLong("id", id);
2023-04-25 10:37:50 +00:00
args.putLong("account", account);
args.putString("parent", parent);
2019-06-30 11:06:12 +00:00
2023-04-25 10:37:50 +00:00
new SimpleTask<Pair<EntityFolder, EntityFolder>>() {
2020-08-03 06:31:48 +00:00
@Override
protected void onPreExecute(Bundle args) {
pbWait.setVisibility(View.VISIBLE);
}
@Override
protected void onPostExecute(Bundle args) {
// Consider previous save as cancelled
pbWait.setVisibility(View.GONE);
}
2019-06-30 11:06:12 +00:00
@Override
2023-04-25 10:37:50 +00:00
protected Pair<EntityFolder, EntityFolder> onExecute(Context context, Bundle args) {
2019-06-30 11:06:12 +00:00
long id = args.getLong("id");
2023-04-25 10:37:50 +00:00
long aid = args.getLong("account");
String parentName = args.getString("parent");
2020-02-25 20:06:17 +00:00
DB db = DB.getInstance(context);
2023-04-25 10:37:50 +00:00
EntityFolder parent = db.folder().getFolderByName(aid, parentName);
2020-02-25 20:06:17 +00:00
EntityFolder folder = db.folder().getFolder(id);
if (folder != null) {
EntityAccount account = db.account().getAccount(folder.account);
if (account != null) {
2020-02-25 20:06:17 +00:00
args.putInt("interval", account.poll_interval);
}
2020-02-25 20:06:17 +00:00
}
2023-04-25 10:37:50 +00:00
return new Pair<>(parent, folder);
2019-06-30 11:06:12 +00:00
}
@Override
2023-04-25 10:37:50 +00:00
protected void onExecuted(Bundle args, Pair<EntityFolder, EntityFolder> data) {
EntityFolder parent = data.first;
EntityFolder folder = data.second;
int interval = args.getInt("interval", EntityAccount.DEFAULT_KEEP_ALIVE_INTERVAL);
2019-06-30 11:06:12 +00:00
if (savedInstanceState == null) {
etName.setText(folder == null ? null : folder.name);
etDisplay.setText(folder == null ? null : folder.display);
2020-06-06 08:02:11 +00:00
etDisplay.setHint(folder == null ? null : EntityFolder.localizeName(getContext(), folder.name));
2019-10-07 16:15:02 +00:00
btnColor.setColor(folder == null ? null : folder.color);
2019-06-30 11:06:12 +00:00
cbHide.setChecked(folder == null ? false : folder.hide);
cbHideSeen.setChecked(folder == null ? false : folder.hide_seen);
2019-06-30 11:06:12 +00:00
cbUnified.setChecked(folder == null ? false : folder.unified);
cbNavigation.setChecked(folder == null ? false : folder.navigation);
2023-02-03 08:38:06 +00:00
cbCountUnread.setChecked(folder == null ? true : folder.count_unread);
2023-04-25 10:37:50 +00:00
cbNotify.setChecked(folder == null ? parent != null && parent.notify : folder.notify);
2019-06-30 11:06:12 +00:00
cbSynchronize.setChecked(folder == null || folder.synchronize);
2020-08-04 16:25:50 +00:00
cbPoll.setChecked(folder == null ? true : folder.poll);
2020-02-25 20:06:17 +00:00
etPoll.setText(folder == null ? null : Integer.toString(folder.poll_factor));
tvPoll.setText(getString(R.string.title_factor_minutes, interval));
2019-06-30 11:06:12 +00:00
cbDownload.setChecked(folder == null ? true : folder.download);
cbAutoClassifySource.setChecked(folder == null ? false : folder.auto_classify_source);
cbAutoClassifyTarget.setChecked(folder == null ? false : folder.auto_classify_target);
2019-06-30 11:06:12 +00:00
etSyncDays.setText(Integer.toString(folder == null ? EntityFolder.DEFAULT_SYNC : folder.sync_days));
if (folder != null && folder.keep_days == Integer.MAX_VALUE)
cbKeepAll.setChecked(true);
else
etKeepDays.setText(Integer.toString(folder == null ? EntityFolder.DEFAULT_KEEP : folder.keep_days));
2021-01-03 07:50:12 +00:00
cbAutoDelete.setChecked(folder != null && folder.auto_delete);
2019-06-30 11:06:12 +00:00
}
Helper.setViewsEnabled(view, true);
cbHideSeen.setEnabled(!cbHide.isChecked());
boolean canAutoClassify = (imap && MessageClassifier.isEnabled(getContext()));
2021-04-05 13:05:24 +00:00
boolean canAutoDelete = (imap && (folder == null || !folder.read_only));
boolean isArchive = (folder != null && EntityFolder.ARCHIVE.equals(folder.type));
2021-01-10 17:43:23 +00:00
boolean pro = (ActivityBilling.isPro(getContext()) ||
(folder != null && EntityFolder.JUNK.equals(folder.type)));
2020-06-28 09:33:03 +00:00
2019-07-29 19:53:32 +00:00
etName.setEnabled(folder == null || EntityFolder.USER.equals(folder.type));
2021-03-13 10:22:41 +00:00
cbPoll.setEnabled(cbSynchronize.isChecked());
2021-03-13 17:09:45 +00:00
tvPollRemark.setVisibility(imap && cbPoll.isEnabled() && !cbPoll.isChecked()
? View.VISIBLE : View.GONE);
grpPoll.setVisibility(imap && cbPoll.isEnabled() && cbPoll.isChecked()
? View.VISIBLE : View.GONE);
2021-01-16 14:09:12 +00:00
cbAutoClassifySource.setEnabled(cbDownload.isChecked());
cbAutoClassifyTarget.setEnabled(cbDownload.isChecked() && cbAutoClassifySource.isChecked());
cbAutoClassifySource.setVisibility(canAutoClassify ? View.VISIBLE : View.GONE);
cbAutoClassifyTarget.setVisibility(canAutoClassify ? View.VISIBLE : View.GONE);
2021-01-10 17:43:23 +00:00
tvAutoClassifyPro.setVisibility(canAutoClassify && !pro ? View.VISIBLE : View.GONE);
2019-06-30 11:06:12 +00:00
etKeepDays.setEnabled(!cbKeepAll.isChecked());
cbAutoDelete.setEnabled(!cbKeepAll.isChecked());
2022-02-22 12:00:01 +00:00
cbAutoDelete.setText(folder != null &&
(EntityFolder.TRASH.equals(folder.type) ||
EntityFolder.JUNK.equals(folder.type))
2021-01-03 07:50:12 +00:00
? R.string.title_auto_delete : R.string.title_auto_trash);
2021-04-05 13:05:24 +00:00
grpAutoDelete.setVisibility(canAutoDelete ? View.VISIBLE : View.GONE);
tvAutoDeleteArchiveHint.setVisibility(canAutoDelete && isArchive ? View.VISIBLE : View.GONE);
2019-06-30 11:06:12 +00:00
btnSave.setEnabled(true);
2021-01-03 07:50:12 +00:00
tvInboxRootHint.setVisibility(folder == null && parent == null ? View.VISIBLE : View.GONE);
2019-06-30 11:06:12 +00:00
2021-12-10 10:41:20 +00:00
deletable = (folder != null &&
!folder.read_only &&
EntityFolder.USER.equals(folder.type));
2021-11-03 17:28:51 +00:00
invalidateOptionsMenu();
2019-06-30 11:06:12 +00:00
}
@Override
protected void onException(Bundle args, Throwable ex) {
2019-12-06 07:50:46 +00:00
Log.unexpectedError(getParentFragmentManager(), ex);
2019-06-30 11:06:12 +00:00
}
}.execute(this, args, "folder:get");
}
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
2019-07-15 12:11:55 +00:00
try {
switch (requestCode) {
2019-10-07 16:15:02 +00:00
case REQUEST_COLOR:
if (resultCode == RESULT_OK && data != null) {
if (ActivityBilling.isPro(getContext())) {
Bundle args = data.getBundleExtra("args");
btnColor.setColor(args.getInt("color"));
} else
startActivity(new Intent(getContext(), ActivityBilling.class));
}
break;
2019-07-15 12:11:55 +00:00
case REQUEST_SAVE_CHANGES:
if (resultCode == RESULT_OK) {
2020-08-23 15:34:14 +00:00
getMainHandler().post(new Runnable() {
2019-07-15 12:11:55 +00:00
@Override
public void run() {
2021-09-28 19:15:46 +00:00
if (!getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED))
return;
2019-07-15 12:11:55 +00:00
scroll.smoothScrollTo(0, btnSave.getBottom());
}
});
onSave(false);
2022-06-12 07:22:55 +00:00
} else
finish();
2019-07-15 12:11:55 +00:00
break;
case REQUEST_DELETE_FOLDER:
if (resultCode == RESULT_OK)
onDelete();
break;
}
} catch (Throwable ex) {
Log.e(ex);
2019-06-30 11:06:12 +00:00
}
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_folder, menu);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
menu.findItem(R.id.menu_delete).setVisible(id > 0 && !saving && deletable);
super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
if (itemId == android.R.id.home) {
onSave(true);
return true;
} else if (itemId == R.id.menu_delete) {
2021-02-05 10:15:02 +00:00
onMenuDelete();
return true;
2019-06-30 11:06:12 +00:00
}
2021-02-05 10:15:02 +00:00
return super.onOptionsItemSelected(item);
2019-06-30 11:06:12 +00:00
}
private void onMenuDelete() {
Bundle aargs = new Bundle();
aargs.putString("question", getString(R.string.title_folder_delete));
2020-10-25 13:55:58 +00:00
aargs.putBoolean("warning", true);
2019-06-30 11:06:12 +00:00
2019-06-30 17:56:23 +00:00
FragmentDialogAsk ask = new FragmentDialogAsk();
2019-06-30 11:06:12 +00:00
ask.setArguments(aargs);
ask.setTargetFragment(FragmentFolder.this, REQUEST_DELETE_FOLDER);
2019-10-12 15:16:53 +00:00
ask.show(getParentFragmentManager(), "folder:delete");
2019-06-30 11:06:12 +00:00
}
2019-06-16 08:22:43 +00:00
private void onSave(boolean should) {
2019-01-17 09:57:01 +00:00
Bundle args = new Bundle();
args.putLong("id", id);
args.putLong("account", account);
2019-06-26 06:55:27 +00:00
args.putString("parent", parent);
2021-01-13 20:05:12 +00:00
args.putString("name", etName.getText().toString().trim());
2019-01-17 09:57:01 +00:00
args.putString("display", etDisplay.getText().toString());
2019-10-07 16:15:02 +00:00
args.putInt("color", btnColor.getColor());
2019-06-15 09:58:56 +00:00
args.putBoolean("hide", cbHide.isChecked());
args.putBoolean("hide_seen", cbHideSeen.isChecked());
2019-01-17 09:57:01 +00:00
args.putBoolean("unified", cbUnified.isChecked());
args.putBoolean("navigation", cbNavigation.isChecked());
2023-02-03 08:38:06 +00:00
args.putBoolean("count_unread", cbCountUnread.isChecked());
2019-09-30 10:06:10 +00:00
args.putBoolean("notify", cbNotify.isChecked());
2019-01-17 09:57:01 +00:00
args.putBoolean("synchronize", cbSynchronize.isChecked());
args.putBoolean("poll", cbPoll.isChecked());
2020-02-25 20:06:17 +00:00
args.putString("factor", etPoll.getText().toString());
2019-01-17 09:57:01 +00:00
args.putBoolean("download", cbDownload.isChecked());
args.putBoolean("auto_classify_source", cbAutoClassifySource.isChecked());
args.putBoolean("auto_classify_target", cbAutoClassifyTarget.isChecked());
2019-01-17 09:57:01 +00:00
args.putString("sync", etSyncDays.getText().toString());
args.putString("keep", cbKeepAll.isChecked()
? Integer.toString(Integer.MAX_VALUE)
: etKeepDays.getText().toString());
args.putBoolean("auto_delete", cbAutoDelete.isChecked());
2019-01-17 09:57:01 +00:00
2020-11-15 14:09:06 +00:00
args.putBoolean("imap", imap);
2019-06-16 08:22:43 +00:00
args.putBoolean("should", should);
new SimpleTask<Boolean>() {
2019-01-17 09:57:01 +00:00
@Override
protected void onPreExecute(Bundle args) {
saving = true;
2021-11-03 17:28:51 +00:00
invalidateOptionsMenu();
2019-01-17 09:57:01 +00:00
Helper.setViewsEnabled(view, false);
pbSave.setVisibility(View.VISIBLE);
}
@Override
protected void onPostExecute(Bundle args) {
saving = false;
2021-11-03 17:28:51 +00:00
invalidateOptionsMenu();
2019-01-17 09:57:01 +00:00
Helper.setViewsEnabled(view, true);
pbSave.setVisibility(View.GONE);
}
@Override
2019-06-16 08:22:43 +00:00
protected Boolean onExecute(Context context, Bundle args) {
2019-01-17 09:57:01 +00:00
long id = args.getLong("id");
long aid = args.getLong("account");
2019-06-26 06:55:27 +00:00
String parent = args.getString("parent");
2019-01-17 09:57:01 +00:00
String name = args.getString("name");
String display = args.getString("display");
2019-10-07 16:15:02 +00:00
Integer color = args.getInt("color");
2019-06-15 09:58:56 +00:00
boolean hide = args.getBoolean("hide");
boolean hide_seen = args.getBoolean("hide_seen");
2019-01-17 09:57:01 +00:00
boolean unified = args.getBoolean("unified");
boolean navigation = args.getBoolean("navigation");
2023-02-03 08:38:06 +00:00
boolean count_unread = args.getBoolean("count_unread");
2019-01-17 09:57:01 +00:00
boolean notify = args.getBoolean("notify");
boolean synchronize = args.getBoolean("synchronize");
boolean poll = args.getBoolean("poll");
2020-02-25 20:06:17 +00:00
String factor = args.getString("factor");
2019-01-17 09:57:01 +00:00
boolean download = args.getBoolean("download");
boolean auto_classify_source = args.getBoolean("auto_classify_source");
boolean auto_classify_target = args.getBoolean("auto_classify_target");
2019-01-17 09:57:01 +00:00
String sync = args.getString("sync");
String keep = args.getString("keep");
boolean auto_delete = args.getBoolean("auto_delete");
2019-01-17 09:57:01 +00:00
2019-10-07 16:15:02 +00:00
boolean pro = ActivityBilling.isPro(context);
2020-11-15 14:09:06 +00:00
boolean imap = args.getBoolean("imap");
2019-06-16 08:22:43 +00:00
boolean should = args.getBoolean("should");
2019-10-07 16:15:02 +00:00
if (color == Color.TRANSPARENT || !pro)
color = null;
2019-07-29 19:53:32 +00:00
if (TextUtils.isEmpty(display) || display.equals(name))
2019-07-23 17:50:53 +00:00
display = null;
2019-01-17 09:57:01 +00:00
int sync_days = (TextUtils.isEmpty(sync) ? EntityFolder.DEFAULT_SYNC : Integer.parseInt(sync));
int keep_days = (TextUtils.isEmpty(keep) ? EntityFolder.DEFAULT_KEEP : Integer.parseInt(keep));
if (keep_days < sync_days)
keep_days = sync_days;
2020-11-15 14:09:06 +00:00
if (!imap) {
sync_days = Integer.MAX_VALUE;
keep_days = Integer.MAX_VALUE;
}
2020-02-26 09:48:16 +00:00
int poll_factor = (TextUtils.isEmpty(factor) ? 1 : Integer.parseInt(factor));
2020-02-25 20:06:17 +00:00
if (poll_factor < 1)
poll_factor = 1;
2019-01-17 09:57:01 +00:00
boolean reload;
DB db = DB.getInstance(context);
try {
db.beginTransaction();
EntityFolder folder = db.folder().getFolder(id);
2019-06-16 08:22:43 +00:00
if (should) {
if (folder == null)
return !TextUtils.isEmpty(name);
2019-07-29 19:53:32 +00:00
if (!Objects.equals(folder.name, name))
return true;
2019-06-16 08:22:43 +00:00
if (!Objects.equals(folder.display, display))
return true;
2019-10-13 09:23:47 +00:00
if (!Objects.equals(folder.color, color))
return true;
2019-06-16 08:22:43 +00:00
if (!Objects.equals(folder.unified, unified))
return true;
if (!Objects.equals(folder.navigation, navigation))
return true;
2023-02-03 08:38:06 +00:00
if (!Objects.equals(folder.count_unread, count_unread))
return true;
2019-06-16 08:22:43 +00:00
if (!Objects.equals(folder.notify, notify))
return true;
if (!Objects.equals(folder.hide, hide))
return true;
if (!Objects.equals(folder.hide_seen, hide_seen))
return true;
2019-06-16 08:22:43 +00:00
if (!Objects.equals(folder.synchronize, synchronize))
return true;
2020-11-15 14:09:06 +00:00
if (imap) {
if (!Objects.equals(folder.poll, poll))
return true;
if (!Objects.equals(folder.poll_factor, poll_factor))
return true;
if (!Objects.equals(folder.download, download))
return true;
if (!Objects.equals(folder.auto_classify_source, auto_classify_source))
return true;
if (!Objects.equals(folder.auto_classify_target, auto_classify_target))
2021-01-02 21:52:41 +00:00
return true;
2020-11-15 14:09:06 +00:00
if (!Objects.equals(folder.sync_days, sync_days))
return true;
if (!Objects.equals(folder.keep_days, keep_days))
return true;
if (!Objects.equals(folder.auto_delete, auto_delete))
return true;
}
2019-06-16 08:22:43 +00:00
return false;
}
2019-01-17 09:57:01 +00:00
if (folder == null) {
reload = true;
2019-06-26 06:55:27 +00:00
Log.i("Creating folder=" + name + " parent=" + parent);
if (parent != null) {
2021-09-24 07:51:57 +00:00
EntityFolder p = db.folder().getFolderByName(aid, parent);
if (p == null || p.separator == null)
2019-06-26 06:55:27 +00:00
return false;
2021-09-24 07:51:57 +00:00
name = parent + p.separator + name;
2019-06-26 06:55:27 +00:00
}
2019-01-17 09:57:01 +00:00
if (TextUtils.isEmpty(name))
2019-02-04 16:10:30 +00:00
throw new IllegalArgumentException(context.getString(R.string.title_folder_name_missing));
2019-07-29 19:53:32 +00:00
EntityFolder target = db.folder().getFolderByName(aid, name);
if (target != null)
2019-05-09 10:33:27 +00:00
throw new IllegalArgumentException(context.getString(R.string.title_folder_exists, name));
2019-01-17 09:57:01 +00:00
EntityFolder create = new EntityFolder();
create.account = aid;
create.name = name;
create.display = display;
2019-10-07 16:15:02 +00:00
create.color = color;
2019-01-17 09:57:01 +00:00
create.type = EntityFolder.USER;
create.unified = unified;
create.navigation = navigation;
2023-02-03 08:38:06 +00:00
create.count_unread = count_unread;
2019-01-17 09:57:01 +00:00
create.notify = notify;
2019-06-16 08:22:43 +00:00
create.hide = hide;
create.hide_seen = hide;
2019-01-17 09:57:01 +00:00
create.synchronize = synchronize;
create.poll = poll;
2020-02-25 20:06:17 +00:00
create.poll_factor = poll_factor;
2019-01-17 09:57:01 +00:00
create.download = download;
create.auto_classify_source = auto_classify_source;
create.auto_classify_target = auto_classify_target;
2019-01-17 09:57:01 +00:00
create.sync_days = sync_days;
create.keep_days = keep_days;
2019-10-13 09:23:47 +00:00
create.auto_delete = auto_delete;
2019-01-17 09:57:01 +00:00
create.tbc = true;
db.folder().insertFolder(create);
} else {
2019-07-29 19:53:32 +00:00
if (!folder.name.equals(name)) {
2021-03-17 06:48:39 +00:00
if (TextUtils.isEmpty(name))
throw new IllegalArgumentException(context.getString(R.string.title_folder_name_missing));
2019-07-29 19:53:32 +00:00
EntityFolder target = db.folder().getFolderByName(folder.account, name);
if (target != null)
throw new IllegalArgumentException(context.getString(R.string.title_folder_exists, name));
}
reload = (!folder.name.equals(name) ||
2019-01-17 09:57:01 +00:00
!folder.poll.equals(poll));
2019-07-29 19:53:32 +00:00
Log.i("Updating folder=" + folder.name);
2019-03-31 12:19:09 +00:00
db.folder().setFolderProperties(id,
2019-07-29 19:53:32 +00:00
folder.name.equals(name) ? null : name,
2023-02-03 08:38:06 +00:00
display, color, unified,
navigation, count_unread, notify,
hide, hide_seen,
synchronize, poll, poll_factor, download,
auto_classify_source, auto_classify_target,
sync_days, keep_days, auto_delete);
2019-03-31 12:19:09 +00:00
db.folder().setFolderError(id, null);
2019-05-31 06:33:48 +00:00
if (!reload && synchronize)
EntityOperation.sync(context, folder.id, true);
2019-01-17 09:57:01 +00:00
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
if (reload)
2021-06-12 18:21:02 +00:00
ServiceSynchronize.reload(context, aid, true, "save folder");
else
ServiceSynchronize.eval(context, "save folder");
2019-01-17 09:57:01 +00:00
2019-06-16 08:22:43 +00:00
return false;
2019-01-17 09:57:01 +00:00
}
@Override
2019-06-16 08:22:43 +00:00
protected void onExecuted(Bundle args, Boolean dirty) {
2019-06-30 11:06:12 +00:00
if (dirty) {
Bundle aargs = new Bundle();
aargs.putString("question", getString(R.string.title_ask_save));
2019-06-30 17:56:23 +00:00
FragmentDialogAsk ask = new FragmentDialogAsk();
2019-06-30 11:06:12 +00:00
ask.setArguments(aargs);
ask.setTargetFragment(FragmentFolder.this, REQUEST_SAVE_CHANGES);
2019-10-12 15:16:53 +00:00
ask.show(getParentFragmentManager(), "folder:save");
2021-11-12 18:38:59 +00:00
} else if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED)) {
String name = args.getString("name");
Integer color = args.getInt("color");
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
String key = "label.color." + name;
if (color == Color.TRANSPARENT)
prefs.edit().remove(key).apply();
else
prefs.edit().putInt(key, color).apply();
2022-06-12 07:22:55 +00:00
finish();
2021-11-12 18:38:59 +00:00
}
2019-01-17 09:57:01 +00:00
}
@Override
protected void onException(Bundle args, Throwable ex) {
if (ex instanceof IllegalArgumentException)
2023-12-12 19:55:55 +00:00
Snackbar.make(view, new ThrowableWrapper(ex).getSafeMessage(), Snackbar.LENGTH_LONG)
.setGestureInsetBottomIgnored(true).show();
2019-01-17 09:57:01 +00:00
else
2020-11-08 19:21:50 +00:00
Log.unexpectedError(getParentFragmentManager(), ex, !(ex instanceof SQLiteConstraintException));
2019-01-17 09:57:01 +00:00
}
2019-09-11 12:03:59 +00:00
}.execute(this, args, "folder:save");
2019-01-17 09:57:01 +00:00
}
2019-06-30 11:06:12 +00:00
private void onDelete() {
Helper.setViewsEnabled(view, false);
pbSave.setVisibility(View.VISIBLE);
2019-04-25 16:47:52 +00:00
Bundle args = new Bundle();
args.putLong("id", id);
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) {
long id = args.getLong("id");
2019-12-09 18:44:27 +00:00
EntityFolder folder;
2019-06-30 11:06:12 +00:00
DB db = DB.getInstance(context);
2019-12-09 18:44:27 +00:00
try {
db.beginTransaction();
folder = db.folder().getFolder(id);
if (folder == null)
return null;
int count = db.operation().getOperationCount(folder.id, null);
if (count > 0)
throw new IllegalArgumentException(
context.getResources().getQuantityString(
R.plurals.title_notification_operations, count, count));
db.folder().setFolderTbd(folder.id);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
2021-06-12 18:21:02 +00:00
ServiceSynchronize.reload(context, folder.account, true, "delete folder");
2019-04-25 16:47:52 +00:00
2019-06-30 11:06:12 +00:00
return null;
2019-04-25 16:47:52 +00:00
}
2018-08-02 13:33:06 +00:00
@Override
2019-06-30 11:06:12 +00:00
protected void onExecuted(Bundle args, Void data) {
2022-06-12 07:22:55 +00:00
finish();
2018-11-14 16:21:53 +00:00
}
2018-11-14 16:21:53 +00:00
@Override
2019-06-30 11:06:12 +00:00
protected void onException(Bundle args, Throwable ex) {
Helper.setViewsEnabled(view, true);
2019-06-30 11:06:12 +00:00
pbSave.setVisibility(View.GONE);
2019-06-30 11:06:12 +00:00
if (ex instanceof IllegalArgumentException)
2023-12-12 19:55:55 +00:00
Snackbar.make(view, new ThrowableWrapper(ex).getSafeMessage(), Snackbar.LENGTH_LONG)
.setGestureInsetBottomIgnored(true).show();
2019-06-30 11:06:12 +00:00
else
2019-12-06 07:50:46 +00:00
Log.unexpectedError(getParentFragmentManager(), ex);
2018-12-01 09:47:08 +00:00
}
2019-09-11 12:03:59 +00:00
}.execute(this, args, "folder:delete");
2018-08-02 13:33:06 +00:00
}
}