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

361 lines
15 KiB
Java
Raw Normal View History

2019-05-06 07:10:13 +00:00
package eu.faircode.email;
/*
This file is part of FairEmail.
FairEmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FairEmail is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
2020-01-05 17:32:53 +00:00
Copyright 2018-2020 by Marcel Bokhorst (M66B)
2019-05-06 07:10:13 +00:00
*/
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
2019-05-06 07:10:13 +00:00
import android.view.LayoutInflater;
2019-05-06 12:41:03 +00:00
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
2019-05-06 07:10:13 +00:00
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
2019-05-06 07:10:13 +00:00
import android.widget.CompoundButton;
2019-10-20 08:22:21 +00:00
import android.widget.NumberPicker;
import android.widget.Spinner;
2019-07-14 10:32:32 +00:00
import android.widget.Toast;
2019-05-06 07:10:13 +00:00
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.SwitchCompat;
2019-09-10 06:53:07 +00:00
import androidx.lifecycle.Lifecycle;
2019-05-06 07:10:13 +00:00
import androidx.preference.PreferenceManager;
2019-05-06 12:41:03 +00:00
public class FragmentOptionsBehavior extends FragmentBase implements SharedPreferences.OnSharedPreferenceChangeListener {
2019-10-20 08:22:21 +00:00
private SwitchCompat swDoubleBack;
2019-05-06 07:10:13 +00:00
private SwitchCompat swPull;
private SwitchCompat swAutoScroll;
2019-09-29 16:44:59 +00:00
private SwitchCompat swDoubleTap;
2019-05-06 07:10:13 +00:00
private SwitchCompat swSwipeNav;
2020-01-30 19:52:18 +00:00
private SwitchCompat swVolumeNav;
2019-09-17 09:31:28 +00:00
private SwitchCompat swReversed;
2019-05-06 07:10:13 +00:00
private SwitchCompat swAutoExpand;
2019-10-10 16:06:44 +00:00
private SwitchCompat swExpandAll;
2019-09-14 16:51:51 +00:00
private SwitchCompat swExpandOne;
2019-05-06 07:10:13 +00:00
private SwitchCompat swAutoClose;
private Spinner spOnClose;
private SwitchCompat swQuickFilter;
private SwitchCompat swQuickScroll;
2019-10-09 17:23:03 +00:00
private SwitchCompat swCollapseMultiple;
2019-05-06 07:10:13 +00:00
private SwitchCompat swAutoRead;
2020-02-16 12:49:05 +00:00
private SwitchCompat swFlagSnoozed;
2019-09-28 18:25:19 +00:00
private SwitchCompat swAutoUnflag;
2020-02-03 18:25:46 +00:00
private SwitchCompat swResetImportance;
private SwitchCompat swDiscardDelete;
2019-10-20 08:22:21 +00:00
private NumberPicker npDefaultSnooze;
2019-05-06 07:10:13 +00:00
2019-05-06 12:41:03 +00:00
private final static String[] RESET_OPTIONS = new String[]{
2020-01-30 19:52:18 +00:00
"double_back", "pull", "autoscroll", "doubletap", "swipenav", "volumenav", "reversed",
2019-10-13 06:57:01 +00:00
"autoexpand", "expand_all", "expand_one", "collapse_multiple",
2020-02-03 18:25:46 +00:00
"autoclose", "onclose", "quick_filter", "quick_scroll",
2020-02-16 12:49:05 +00:00
"autoread", "flag_snoozed", "autounflag", "reset_importance", "discard_delete",
2019-10-20 08:22:21 +00:00
"default_snooze"
2019-05-06 12:41:03 +00:00
};
2019-05-06 07:10:13 +00:00
@Override
@Nullable
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
2019-06-12 14:15:46 +00:00
setSubtitle(R.string.title_setup);
2019-05-06 12:41:03 +00:00
setHasOptionsMenu(true);
2019-05-06 07:10:13 +00:00
View view = inflater.inflate(R.layout.fragment_options_behavior, container, false);
// Get controls
2019-10-20 08:22:21 +00:00
swDoubleBack = view.findViewById(R.id.swDoubleBack);
2019-05-06 07:10:13 +00:00
swPull = view.findViewById(R.id.swPull);
swAutoScroll = view.findViewById(R.id.swAutoScroll);
2019-09-29 16:44:59 +00:00
swDoubleTap = view.findViewById(R.id.swDoubleTap);
2019-05-06 07:10:13 +00:00
swSwipeNav = view.findViewById(R.id.swSwipeNav);
2020-01-30 19:52:18 +00:00
swVolumeNav = view.findViewById(R.id.swVolumeNav);
2019-09-17 09:31:28 +00:00
swReversed = view.findViewById(R.id.swReversed);
2019-05-06 07:10:13 +00:00
swAutoExpand = view.findViewById(R.id.swAutoExpand);
2019-10-10 16:06:44 +00:00
swExpandAll = view.findViewById(R.id.swExpandAll);
2019-09-14 16:51:51 +00:00
swExpandOne = view.findViewById(R.id.swExpandOne);
2019-10-09 17:23:03 +00:00
swCollapseMultiple = view.findViewById(R.id.swCollapseMultiple);
2019-10-11 08:50:55 +00:00
swAutoClose = view.findViewById(R.id.swAutoClose);
spOnClose = view.findViewById(R.id.spOnClose);
swQuickFilter = view.findViewById(R.id.swQuickFilter);
swQuickScroll = view.findViewById(R.id.swQuickScroll);
2019-05-06 07:10:13 +00:00
swAutoRead = view.findViewById(R.id.swAutoRead);
2020-02-16 12:49:05 +00:00
swFlagSnoozed = view.findViewById(R.id.swFlagSnoozed);
2019-09-28 18:25:19 +00:00
swAutoUnflag = view.findViewById(R.id.swAutoUnflag);
2020-02-03 18:25:46 +00:00
swResetImportance = view.findViewById(R.id.swResetImportance);
swDiscardDelete = view.findViewById(R.id.swDiscardDelete);
2019-10-20 08:22:21 +00:00
npDefaultSnooze = view.findViewById(R.id.npDefaultSnooze);
npDefaultSnooze.setMinValue(1);
npDefaultSnooze.setMaxValue(999);
2019-05-06 07:10:13 +00:00
2019-05-06 13:30:30 +00:00
setOptions();
2019-05-06 07:10:13 +00:00
// Wire controls
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
2019-10-20 08:22:21 +00:00
swDoubleBack.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("double_back", checked).apply();
}
});
2019-05-06 07:10:13 +00:00
swPull.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("pull", checked).apply();
}
});
swAutoScroll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("autoscroll", checked).apply();
}
});
2019-09-29 16:44:59 +00:00
swDoubleTap.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
2019-05-06 07:10:13 +00:00
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
2019-09-29 16:44:59 +00:00
prefs.edit().putBoolean("doubletap", checked).apply();
}
});
2019-09-29 16:44:59 +00:00
swSwipeNav.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
2019-09-29 16:44:59 +00:00
prefs.edit().putBoolean("swipenav", checked).apply();
2019-05-06 07:10:13 +00:00
}
});
2020-01-30 19:52:18 +00:00
swVolumeNav.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("volumenav", checked).apply();
}
});
2019-09-29 16:44:59 +00:00
swReversed.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
2019-06-26 11:55:35 +00:00
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
2019-09-29 16:44:59 +00:00
prefs.edit().putBoolean("reversed", checked).apply();
2019-06-26 11:55:35 +00:00
}
});
2019-05-06 07:10:13 +00:00
swAutoExpand.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("autoexpand", checked).apply();
}
});
2019-10-10 16:06:44 +00:00
swExpandAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("expand_all", checked).apply();
swExpandOne.setEnabled(!checked);
2019-10-11 08:50:55 +00:00
swCollapseMultiple.setEnabled(!swExpandOne.isChecked() || swExpandAll.isChecked());
2019-10-10 16:06:44 +00:00
}
});
2019-09-14 16:51:51 +00:00
swExpandOne.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("expand_one", checked).apply();
2019-10-11 08:50:55 +00:00
swCollapseMultiple.setEnabled(!swExpandOne.isChecked() || swExpandAll.isChecked());
}
});
swCollapseMultiple.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("collapse_multiple", checked).apply();
2019-09-14 16:51:51 +00:00
}
});
2019-05-06 07:10:13 +00:00
swAutoClose.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("autoclose", checked).apply();
spOnClose.setEnabled(!checked);
2019-05-06 07:10:13 +00:00
}
});
spOnClose.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
2019-05-06 07:10:13 +00:00
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
String[] values = getResources().getStringArray(R.array.onCloseValues);
String value = values[position];
if (TextUtils.isEmpty(value))
prefs.edit().remove("onclose").apply();
else
prefs.edit().putString("onclose", value).apply();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
prefs.edit().remove("onclose").apply();
2019-05-06 07:10:13 +00:00
}
});
swQuickFilter.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("quick_filter", checked).apply();
}
});
swQuickScroll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("quick_scroll", checked).apply();
}
});
2019-05-06 07:10:13 +00:00
swAutoRead.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("autoread", checked).apply();
}
});
2020-02-16 12:49:05 +00:00
swFlagSnoozed.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("flag_snoozed", checked).apply();
}
});
2019-09-28 18:25:19 +00:00
swAutoUnflag.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("autounflag", checked).apply();
}
});
2020-02-03 18:25:46 +00:00
swResetImportance.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("reset_importance", checked).apply();
}
});
swDiscardDelete.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("discard_delete", checked).apply();
}
});
2019-10-20 08:22:21 +00:00
npDefaultSnooze.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
prefs.edit().putInt("default_snooze", newVal).apply();
}
});
2019-05-06 12:41:03 +00:00
PreferenceManager.getDefaultSharedPreferences(getContext()).registerOnSharedPreferenceChangeListener(this);
2019-05-06 07:10:13 +00:00
return view;
}
2019-05-06 12:41:03 +00:00
@Override
public void onDestroyView() {
PreferenceManager.getDefaultSharedPreferences(getContext()).unregisterOnSharedPreferenceChangeListener(this);
super.onDestroyView();
}
@Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
2019-09-10 06:53:07 +00:00
if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED))
setOptions();
2019-05-06 12:41:03 +00:00
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_options, menu);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_default:
onMenuDefault();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void onMenuDefault() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
SharedPreferences.Editor editor = prefs.edit();
for (String option : RESET_OPTIONS)
editor.remove(option);
editor.apply();
2019-07-14 10:32:32 +00:00
ToastEx.makeText(getContext(), R.string.title_setup_done, Toast.LENGTH_LONG).show();
2019-05-06 12:41:03 +00:00
}
2019-05-06 07:10:13 +00:00
private void setOptions() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
2019-10-20 08:22:21 +00:00
swDoubleBack.setChecked(prefs.getBoolean("double_back", true));
2019-05-06 07:10:13 +00:00
swPull.setChecked(prefs.getBoolean("pull", true));
2019-10-04 13:27:34 +00:00
swAutoScroll.setChecked(prefs.getBoolean("autoscroll", true));
2019-09-29 16:44:59 +00:00
swDoubleTap.setChecked(prefs.getBoolean("doubletap", false));
2019-05-06 07:10:13 +00:00
swSwipeNav.setChecked(prefs.getBoolean("swipenav", true));
2020-01-30 19:52:18 +00:00
swVolumeNav.setChecked(prefs.getBoolean("volumenav", false));
2019-09-17 09:31:28 +00:00
swReversed.setChecked(prefs.getBoolean("reversed", false));
2019-10-11 08:50:55 +00:00
2019-05-06 07:10:13 +00:00
swAutoExpand.setChecked(prefs.getBoolean("autoexpand", true));
2019-10-10 16:06:44 +00:00
swExpandAll.setChecked(prefs.getBoolean("expand_all", false));
2019-09-17 15:24:11 +00:00
swExpandOne.setChecked(prefs.getBoolean("expand_one", true));
2019-10-10 16:06:44 +00:00
swExpandOne.setEnabled(!swExpandAll.isChecked());
2019-10-11 08:50:55 +00:00
swCollapseMultiple.setChecked(prefs.getBoolean("collapse_multiple", true));
swCollapseMultiple.setEnabled(!swExpandOne.isChecked() || swExpandAll.isChecked());
2019-05-06 07:10:13 +00:00
swAutoClose.setChecked(prefs.getBoolean("autoclose", true));
String onClose = prefs.getString("onclose", "");
String[] onCloseValues = getResources().getStringArray(R.array.onCloseValues);
for (int pos = 0; pos < onCloseValues.length; pos++)
if (onCloseValues[pos].equals(onClose)) {
spOnClose.setSelection(pos);
break;
}
spOnClose.setEnabled(!swAutoClose.isChecked());
swQuickFilter.setChecked(prefs.getBoolean("quick_filter", false));
swQuickScroll.setChecked(prefs.getBoolean("quick_scroll", true));
2019-05-06 07:10:13 +00:00
swAutoRead.setChecked(prefs.getBoolean("autoread", false));
2020-02-16 12:49:05 +00:00
swFlagSnoozed.setChecked(prefs.getBoolean("flag_snoozed", false));
2019-09-28 18:25:19 +00:00
swAutoUnflag.setChecked(prefs.getBoolean("autounflag", false));
2020-02-03 18:25:46 +00:00
swResetImportance.setChecked(prefs.getBoolean("reset_importance", false));
swDiscardDelete.setChecked(prefs.getBoolean("discard_delete", false));
2019-10-20 08:22:21 +00:00
npDefaultSnooze.setValue(prefs.getInt("default_snooze", 1));
2019-05-06 07:10:13 +00:00
}
}