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

275 lines
11 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/>.
Copyright 2018-2019 by Marcel Bokhorst (M66B)
*/
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;
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-05-06 07:10:13 +00:00
private SwitchCompat swPull;
private SwitchCompat swAutoScroll;
private SwitchCompat swSwipeNav;
2019-09-17 09:31:28 +00:00
private SwitchCompat swReversed;
2019-06-26 11:55:35 +00:00
private SwitchCompat swDoubleTap;
2019-05-06 07:10:13 +00:00
private SwitchCompat swAutoExpand;
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;
2019-05-06 07:10:13 +00:00
private SwitchCompat swCollapse;
private SwitchCompat swAutoRead;
private SwitchCompat swAutoMove;
private SwitchCompat swDiscardDelete;
2019-07-07 08:49:16 +00:00
private SwitchCompat swDisableTracking;
2019-05-06 07:10:13 +00:00
2019-05-06 12:41:03 +00:00
private final static String[] RESET_OPTIONS = new String[]{
"pull", "autoscroll", "swipenav", "reversed", "doubletap", "autoexpand", "expand_one", "autoclose", "onclose",
2019-09-11 20:00:21 +00:00
"collapse", "autoread", "automove", "discard_delete", "disable_tracking"
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
swPull = view.findViewById(R.id.swPull);
swAutoScroll = view.findViewById(R.id.swAutoScroll);
swSwipeNav = view.findViewById(R.id.swSwipeNav);
2019-09-17 09:31:28 +00:00
swReversed = view.findViewById(R.id.swReversed);
2019-06-26 11:55:35 +00:00
swDoubleTap = view.findViewById(R.id.swDoubleTap);
2019-05-06 07:10:13 +00:00
swAutoExpand = view.findViewById(R.id.swAutoExpand);
2019-09-14 16:51:51 +00:00
swExpandOne = view.findViewById(R.id.swExpandOne);
2019-05-06 07:10:13 +00:00
swAutoClose = view.findViewById(R.id.swAutoClose);
spOnClose = view.findViewById(R.id.spOnClose);
2019-05-06 07:10:13 +00:00
swCollapse = view.findViewById(R.id.swCollapse);
swAutoRead = view.findViewById(R.id.swAutoRead);
swAutoMove = view.findViewById(R.id.swAutoMove);
swDiscardDelete = view.findViewById(R.id.swDiscardDelete);
2019-07-07 08:49:16 +00:00
swDisableTracking = view.findViewById(R.id.swDisableTracking);
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());
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();
}
});
swSwipeNav.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("swipenav", checked).apply();
}
});
2019-09-17 09:31:28 +00:00
swReversed.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
2019-09-17 09:31:28 +00:00
prefs.edit().putBoolean("reversed", checked).apply();
2019-05-06 07:10:13 +00:00
}
});
2019-06-26 11:55:35 +00:00
swDoubleTap.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("doubletap", checked).apply();
}
});
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-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-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
}
});
swCollapse.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("collapse", checked).apply();
}
});
swAutoRead.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("autoread", checked).apply();
}
});
swAutoMove.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("automove", !checked).apply();
}
});
swDiscardDelete.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("discard_delete", checked).apply();
}
});
2019-07-07 08:49:16 +00:00
swDisableTracking.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("disable_tracking", checked).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());
swPull.setChecked(prefs.getBoolean("pull", true));
swAutoScroll.setChecked(prefs.getBoolean("autoscroll", false));
swSwipeNav.setChecked(prefs.getBoolean("swipenav", true));
2019-09-17 09:31:28 +00:00
swReversed.setChecked(prefs.getBoolean("reversed", false));
2019-06-26 11:55:35 +00:00
swDoubleTap.setChecked(prefs.getBoolean("doubletap", false));
2019-05-06 07:10:13 +00:00
swAutoExpand.setChecked(prefs.getBoolean("autoexpand", true));
2019-09-17 15:24:11 +00:00
swExpandOne.setChecked(prefs.getBoolean("expand_one", true));
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());
2019-05-06 07:10:13 +00:00
swCollapse.setChecked(prefs.getBoolean("collapse", false));
swAutoRead.setChecked(prefs.getBoolean("autoread", false));
swAutoMove.setChecked(!prefs.getBoolean("automove", false));
swDiscardDelete.setChecked(prefs.getBoolean("discard_delete", false));
2019-07-07 08:49:16 +00:00
swDisableTracking.setChecked(prefs.getBoolean("disable_tracking", true));
2019-05-06 07:10:13 +00:00
}
}