Reset default per options section

This commit is contained in:
M66B 2019-05-06 14:41:03 +02:00
parent bf36d6789e
commit fba2d11365
7 changed files with 282 additions and 91 deletions

View File

@ -19,12 +19,8 @@ package eu.faircode.email;
Copyright 2018-2019 by Marcel Bokhorst (M66B)
*/
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
@ -33,7 +29,6 @@ import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.preference.PreferenceManager;
import androidx.viewpager.widget.ViewPager;
import com.google.android.material.tabs.TabLayout;
@ -50,24 +45,10 @@ public class FragmentOptions extends FragmentBase {
"authentication", "debug"
};
private final static String[] ADVANCED_OPTIONS = new String[]{
"enabled", "poll_interval", "schedule", "schedule_start", "schedule_end",
"metered", "download", "roaming",
"startup", "date", "threading", "avatars", "identicons", "circular", "name_email", "subject_italic", "flags", "preview",
"addresses", "monospaced", "autohtml", "autoimages", "actionbar",
"pull", "autoscroll", "swipenav", "autoexpand", "autoclose", "autonext", "collapse", "autoread", "automove",
"autoresize", "resize", "prefix_once", "autosend",
"notify_trash", "notify_archive", "notify_reply", "notify_flag", "notify_seen", "notify_preview", "light", "sound",
"badge", "subscriptions", "search_local", "english", "authentication", "paranoid", "updates", "debug",
"first", "why", "last_update_check", "app_support", "message_swipe", "message_select", "folder_actions", "folder_sync",
"edit_ref_confirmed", "show_html_confirmed", "show_images_confirmed", "print_html_confirmed", "show_organization", "style_toolbar"
};
@Override
@Nullable
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
setSubtitle(R.string.title_advanced);
setHasOptionsMenu(true);
View view = inflater.inflate(R.layout.fragment_options, container, false);
@ -139,31 +120,4 @@ public class FragmentOptions extends FragmentBase {
return POSITION_NONE; // always recreate fragment
}
}
@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 : ADVANCED_OPTIONS)
editor.remove(option);
editor.apply();
adapter.notifyDataSetChanged();
}
}

View File

@ -22,6 +22,9 @@ package eu.faircode.email;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
@ -34,7 +37,7 @@ import androidx.annotation.Nullable;
import androidx.appcompat.widget.SwitchCompat;
import androidx.preference.PreferenceManager;
public class FragmentOptionsBehavior extends FragmentBase {
public class FragmentOptionsBehavior extends FragmentBase implements SharedPreferences.OnSharedPreferenceChangeListener {
private SwitchCompat swPull;
private SwitchCompat swAutoScroll;
private SwitchCompat swSwipeNav;
@ -50,10 +53,16 @@ public class FragmentOptionsBehavior extends FragmentBase {
private SwitchCompat swPrefixOnce;
private SwitchCompat swAutoSend;
private final static String[] RESET_OPTIONS = new String[]{
"pull", "autoscroll", "swipenav", "autoexpand", "autoclose", "autonext",
"collapse", "autoread", "automove", "autoresize", "resize", "prefix_once", "autosend"
};
@Override
@Nullable
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
setSubtitle(R.string.title_advanced);
setHasOptionsMenu(true);
View view = inflater.inflate(R.layout.fragment_options_behavior, container, false);
@ -78,8 +87,6 @@ public class FragmentOptionsBehavior extends FragmentBase {
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
setOptions();
swPull.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
@ -180,9 +187,48 @@ public class FragmentOptionsBehavior extends FragmentBase {
}
});
setOptions();
PreferenceManager.getDefaultSharedPreferences(getContext()).registerOnSharedPreferenceChangeListener(this);
return view;
}
@Override
public void onDestroyView() {
PreferenceManager.getDefaultSharedPreferences(getContext()).unregisterOnSharedPreferenceChangeListener(this);
super.onDestroyView();
}
@Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
setOptions();
}
@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();
}
private void setOptions() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());

View File

@ -27,6 +27,9 @@ import android.net.NetworkCapabilities;
import android.net.NetworkRequest;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
@ -41,17 +44,22 @@ import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.Lifecycle;
import androidx.preference.PreferenceManager;
public class FragmentOptionsConnection extends FragmentBase {
public class FragmentOptionsConnection extends FragmentBase implements SharedPreferences.OnSharedPreferenceChangeListener {
private TextView tvConnectionType;
private TextView tvConnectionRoaming;
private SwitchCompat swMetered;
private Spinner spDownload;
private SwitchCompat swRoaming;
private final static String[] RESET_OPTIONS = new String[]{
"metered", "download", "roaming"
};
@Override
@Nullable
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
setSubtitle(R.string.title_advanced);
setHasOptionsMenu(true);
View view = inflater.inflate(R.layout.fragment_options_connection, container, false);
@ -67,8 +75,6 @@ public class FragmentOptionsConnection extends FragmentBase {
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
setOptions();
swMetered.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
@ -98,9 +104,23 @@ public class FragmentOptionsConnection extends FragmentBase {
}
});
setOptions();
PreferenceManager.getDefaultSharedPreferences(getContext()).registerOnSharedPreferenceChangeListener(this);
return view;
}
@Override
public void onDestroyView() {
PreferenceManager.getDefaultSharedPreferences(getContext()).unregisterOnSharedPreferenceChangeListener(this);
super.onDestroyView();
}
@Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
setOptions();
}
@Override
public void onResume() {
super.onResume();
@ -119,6 +139,31 @@ public class FragmentOptionsConnection extends FragmentBase {
super.onPause();
}
@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();
}
private void setOptions() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());

View File

@ -22,6 +22,9 @@ package eu.faircode.email;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
@ -33,7 +36,7 @@ import androidx.annotation.Nullable;
import androidx.appcompat.widget.SwitchCompat;
import androidx.preference.PreferenceManager;
public class FragmentOptionsDisplay extends FragmentBase {
public class FragmentOptionsDisplay extends FragmentBase implements SharedPreferences.OnSharedPreferenceChangeListener {
private Spinner spStartup;
private SwitchCompat swDate;
private SwitchCompat swThreading;
@ -50,10 +53,16 @@ public class FragmentOptionsDisplay extends FragmentBase {
private SwitchCompat swImages;
private SwitchCompat swActionbar;
private final static String[] RESET_OPTIONS = new String[]{
"startup", "date", "threading", "avatars", "identicons", "circular", "name_email", "subject_italic",
"flags", "preview", "addresses", "monospaced", "autohtml", "autoimages", "actionbar",
};
@Override
@Nullable
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
setSubtitle(R.string.title_advanced);
setHasOptionsMenu(true);
View view = inflater.inflate(R.layout.fragment_options_display, container, false);
@ -79,8 +88,6 @@ public class FragmentOptionsDisplay extends FragmentBase {
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
setOptions();
spStartup.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
@ -195,9 +202,48 @@ public class FragmentOptionsDisplay extends FragmentBase {
}
});
setOptions();
PreferenceManager.getDefaultSharedPreferences(getContext()).registerOnSharedPreferenceChangeListener(this);
return view;
}
@Override
public void onDestroyView() {
PreferenceManager.getDefaultSharedPreferences(getContext()).unregisterOnSharedPreferenceChangeListener(this);
super.onDestroyView();
}
@Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
setOptions();
}
@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();
}
private void setOptions() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());

View File

@ -27,6 +27,9 @@ import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
@ -52,10 +55,15 @@ public class FragmentOptionsGeneral extends FragmentBase implements SharedPrefer
private TextView tvScheduleStart;
private TextView tvScheduleEnd;
private final static String[] RESET_OPTIONS = new String[]{
"enabled", "poll_interval", "schedule", "schedule_start", "schedule_end"
};
@Override
@Nullable
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
setSubtitle(R.string.title_advanced);
setHasOptionsMenu(true);
View view = inflater.inflate(R.layout.fragment_options_general, container, false);
@ -71,10 +79,6 @@ public class FragmentOptionsGeneral extends FragmentBase implements SharedPrefer
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
setOptions();
// General
swEnabled.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
@ -149,7 +153,7 @@ public class FragmentOptionsGeneral extends FragmentBase implements SharedPrefer
}
});
setOptions();
PreferenceManager.getDefaultSharedPreferences(getContext()).registerOnSharedPreferenceChangeListener(this);
return view;
@ -161,6 +165,36 @@ public class FragmentOptionsGeneral extends FragmentBase implements SharedPrefer
super.onDestroyView();
}
@Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
setOptions();
}
@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();
}
private void setOptions() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
@ -226,16 +260,4 @@ public class FragmentOptionsGeneral extends FragmentBase implements SharedPrefer
ServiceSynchronize.reschedule(getContext());
}
}
@Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
if ("enabled".equals(key))
swEnabled.setChecked(prefs.getBoolean(key, true));
else if ("schedule".equals(key))
swSchedule.setChecked(prefs.getBoolean(key, false));
else if ("schedule_start".equals(key))
tvScheduleStart.setText(formatHour(getContext(), prefs.getInt(key, 0)));
else if ("schedule_end".equals(key))
tvScheduleEnd.setText(formatHour(getContext(), prefs.getInt(key, 0)));
}
}

View File

@ -25,6 +25,9 @@ import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
@ -53,10 +56,15 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
private Group grpSearchLocal;
private final static String[] RESET_OPTIONS = new String[]{
"badge", "subscriptions", "search_local", "english", "authentication", "paranoid", "updates", "debug"
};
@Override
@Nullable
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
setSubtitle(R.string.title_advanced);
setHasOptionsMenu(true);
View view = inflater.inflate(R.layout.fragment_options_misc, container, false);
@ -80,8 +88,6 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
setOptions();
swBadge.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
@ -160,6 +166,7 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
setLastCleanup(prefs.getLong("last_cleanup", -1));
setOptions();
PreferenceManager.getDefaultSharedPreferences(getContext()).registerOnSharedPreferenceChangeListener(this);
return view;
@ -171,6 +178,38 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
super.onDestroyView();
}
@Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
setOptions();
if ("last_cleanup".equals(key))
setLastCleanup(prefs.getLong(key, -1));
}
@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();
}
private void setOptions() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
@ -187,12 +226,6 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
grpSearchLocal.setVisibility(Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.M ? View.GONE : View.VISIBLE);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
if ("last_cleanup".equals(key))
setLastCleanup(prefs.getLong(key, -1));
}
private void setLastCleanup(long time) {
java.text.DateFormat df = SimpleDateFormat.getDateTimeInstance();
tvLastCleanup.setText(

View File

@ -26,6 +26,9 @@ import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
@ -41,7 +44,7 @@ import androidx.preference.PreferenceManager;
import static android.app.Activity.RESULT_OK;
public class FragmentOptionsNotifications extends FragmentBase {
public class FragmentOptionsNotifications extends FragmentBase implements SharedPreferences.OnSharedPreferenceChangeListener {
private SwitchCompat swNotifyPreview;
private CheckBox cbNotifyActionTrash;
private CheckBox cbNotifyActionArchive;
@ -53,10 +56,15 @@ public class FragmentOptionsNotifications extends FragmentBase {
private Group grpNotification;
private final static String[] RESET_OPTIONS = new String[]{
"notify_preview", "notify_trash", "notify_archive", "notify_reply", "notify_flag", "notify_seen", "light", "sound"
};
@Override
@Nullable
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
setSubtitle(R.string.title_advanced);
setHasOptionsMenu(true);
View view = inflater.inflate(R.layout.fragment_options_notifications, container, false);
@ -77,8 +85,6 @@ public class FragmentOptionsNotifications extends FragmentBase {
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
setOptions();
swNotifyPreview.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
@ -142,20 +148,48 @@ public class FragmentOptionsNotifications extends FragmentBase {
}
});
setOptions();
PreferenceManager.getDefaultSharedPreferences(getContext()).registerOnSharedPreferenceChangeListener(this);
return view;
}
private void setAction(CompoundButton cb, String key, boolean checked) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
if (Helper.isPro(getContext()))
prefs.edit().putBoolean(key, checked).apply();
else {
cb.setChecked(!checked);
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(getContext());
lbm.sendBroadcast(new Intent(ActivityView.ACTION_SHOW_PRO));
@Override
public void onDestroyView() {
PreferenceManager.getDefaultSharedPreferences(getContext()).unregisterOnSharedPreferenceChangeListener(this);
super.onDestroyView();
}
@Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
setOptions();
}
@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();
}
private void setOptions() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
@ -170,6 +204,17 @@ public class FragmentOptionsNotifications extends FragmentBase {
grpNotification.setVisibility(Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.O ? View.VISIBLE : View.GONE);
}
private void setAction(CompoundButton cb, String key, boolean checked) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
if (Helper.isPro(getContext()))
prefs.edit().putBoolean(key, checked).apply();
else {
cb.setChecked(!checked);
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(getContext());
lbm.sendBroadcast(new Intent(ActivityView.ACTION_SHOW_PRO));
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i("Result class=" + this.getClass().getSimpleName() +