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

310 lines
12 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.app.ActivityManager;
import android.app.NotificationManager;
import android.content.Context;
2019-05-06 07:10:13 +00:00
import android.content.SharedPreferences;
2019-08-24 11:13:48 +00:00
import android.graphics.Paint;
2019-05-06 07:10:13 +00:00
import android.os.Bundle;
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;
2019-07-10 07:35:20 +00:00
import android.widget.Button;
2019-05-06 07:10:13 +00:00
import android.widget.CompoundButton;
import android.widget.TextView;
2019-07-10 07:35:20 +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-05-26 10:41:41 +00:00
import androidx.constraintlayout.widget.Group;
2019-09-10 06:53:07 +00:00
import androidx.lifecycle.Lifecycle;
2019-05-06 07:10:13 +00:00
import androidx.preference.PreferenceManager;
public class FragmentOptionsMisc extends FragmentBase implements SharedPreferences.OnSharedPreferenceChangeListener {
private SwitchCompat swEnglish;
2019-05-12 18:15:16 +00:00
private SwitchCompat swWatchdog;
2019-05-06 07:10:13 +00:00
private SwitchCompat swUpdates;
2019-08-24 10:30:33 +00:00
private SwitchCompat swExperiments;
2019-08-24 11:13:48 +00:00
private TextView tvExperimentsHint;
2019-05-10 06:53:45 +00:00
private SwitchCompat swCrashReports;
2019-05-06 07:10:13 +00:00
private SwitchCompat swDebug;
2019-10-05 07:37:57 +00:00
private Button btnReset;
2019-07-10 07:35:20 +00:00
private Button btnCleanup;
2019-05-06 07:10:13 +00:00
2019-06-15 19:25:49 +00:00
private TextView tvProcessors;
private TextView tvMemoryClass;
2019-10-19 19:53:19 +00:00
private TextView tvStorageSpace;
2019-05-06 07:10:13 +00:00
private TextView tvLastCleanup;
2019-05-26 10:41:41 +00:00
private TextView tvUuid;
private Group grpDebug;
2019-05-06 07:10:13 +00:00
2019-05-06 12:41:03 +00:00
private final static String[] RESET_OPTIONS = new String[]{
2019-10-20 08:22:21 +00:00
"english", "watchdog", "updates", "experiments", "crash_reports", "debug"
2019-05-06 12:41:03 +00:00
};
2019-05-09 05:32:48 +00:00
private final static String[] RESET_QUESTIONS = new String[]{
"welcome", "crash_reports_asked",
"html_always_images", "print_html_confirmed",
"identities_asked", "cc_bcc", "inline_image_hint", "compose_reference", "send_dialog"
2019-05-09 05:32:48 +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_misc, container, false);
// Get controls
swEnglish = view.findViewById(R.id.swEnglish);
2019-05-12 18:15:16 +00:00
swWatchdog = view.findViewById(R.id.swWatchdog);
2019-05-06 07:10:13 +00:00
swUpdates = view.findViewById(R.id.swUpdates);
2019-08-24 10:30:33 +00:00
swExperiments = view.findViewById(R.id.swExperiments);
2019-08-24 11:13:48 +00:00
tvExperimentsHint = view.findViewById(R.id.tvExperimentsHint);
2019-05-10 06:53:45 +00:00
swCrashReports = view.findViewById(R.id.swCrashReports);
2019-05-06 07:10:13 +00:00
swDebug = view.findViewById(R.id.swDebug);
2019-10-05 07:37:57 +00:00
btnReset = view.findViewById(R.id.btnReset);
2019-07-10 07:35:20 +00:00
btnCleanup = view.findViewById(R.id.btnCleanup);
2019-05-06 07:10:13 +00:00
2019-06-15 19:25:49 +00:00
tvProcessors = view.findViewById(R.id.tvProcessors);
tvMemoryClass = view.findViewById(R.id.tvMemoryClass);
2019-10-19 19:53:19 +00:00
tvStorageSpace = view.findViewById(R.id.tvStorageSpace);
2019-05-06 07:10:13 +00:00
tvLastCleanup = view.findViewById(R.id.tvLastCleanup);
2019-05-26 10:41:41 +00:00
tvUuid = view.findViewById(R.id.tvUuid);
grpDebug = view.findViewById(R.id.grpDebug);
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());
swEnglish.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("english", checked).commit(); // apply won't work here
2019-05-10 06:54:06 +00:00
restart();
2019-05-06 07:10:13 +00:00
}
});
2019-05-12 18:15:16 +00:00
swWatchdog.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("watchdog", checked).apply();
WorkerWatchdog.init(getContext());
}
});
2019-05-06 07:10:13 +00:00
swUpdates.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("updates", checked).apply();
if (!checked) {
NotificationManager nm = (NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE);
nm.cancel(Helper.NOTIFICATION_UPDATE);
}
2019-05-06 07:10:13 +00:00
}
});
2019-08-24 11:13:48 +00:00
tvExperimentsHint.setPaintFlags(tvExperimentsHint.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
tvExperimentsHint.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Helper.viewFAQ(getContext(), 125);
}
});
2019-08-24 10:30:33 +00:00
swExperiments.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("experiments", checked).apply();
}
});
2019-05-10 06:53:45 +00:00
swCrashReports.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
2019-07-31 06:43:50 +00:00
prefs.edit()
.remove("crash_reports_asked")
.putBoolean("crash_reports", checked)
.apply();
2019-08-12 11:17:55 +00:00
Log.setCrashReporting(checked);
2019-05-10 06:53:45 +00:00
}
});
2019-05-06 07:10:13 +00:00
swDebug.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("debug", checked).apply();
2019-05-29 06:51:08 +00:00
grpDebug.setVisibility(checked || BuildConfig.DEBUG ? View.VISIBLE : View.GONE);
2019-05-06 07:10:13 +00:00
ServiceSynchronize.reload(getContext(), "debug=" + checked);
}
});
2019-10-05 07:37:57 +00:00
btnReset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onResetQuestions();
}
});
2019-07-10 07:35:20 +00:00
btnCleanup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onCleanup();
}
});
2019-05-06 07:10:13 +00:00
setLastCleanup(prefs.getLong("last_cleanup", -1));
PreferenceManager.getDefaultSharedPreferences(getContext()).registerOnSharedPreferenceChangeListener(this);
return view;
}
@Override
public void onDestroyView() {
PreferenceManager.getDefaultSharedPreferences(getContext()).unregisterOnSharedPreferenceChangeListener(this);
super.onDestroyView();
}
2019-05-06 12:41:03 +00:00
@Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
2019-09-10 06:53:07 +00:00
if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED)) {
setOptions();
if ("last_cleanup".equals(key))
setLastCleanup(prefs.getLong(key, -1));
}
2019-05-06 12:41:03 +00:00
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
2019-10-05 07:37:57 +00:00
inflater.inflate(R.menu.menu_options, menu);
2019-05-06 12:41:03 +00:00
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_default:
2019-09-01 20:33:32 +00:00
onMenuDefault();
2019-05-09 05:32:48 +00:00
return true;
2019-05-06 12:41:03 +00:00
default:
return super.onOptionsItemSelected(item);
}
}
2019-09-01 20:33:32 +00:00
private void onMenuDefault() {
2019-05-06 12:41:03 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
SharedPreferences.Editor editor = prefs.edit();
2019-09-01 20:33:32 +00:00
for (String option : RESET_OPTIONS)
2019-05-06 12:41:03 +00:00
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-10-05 07:37:57 +00:00
private void onResetQuestions() {
2019-09-01 20:33:32 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
SharedPreferences.Editor editor = prefs.edit();
for (String option : RESET_QUESTIONS)
editor.remove(option);
for (String key : prefs.getAll().keySet())
2019-10-04 13:25:04 +00:00
if (key.endsWith(".show_full") || key.endsWith(".show_images"))
2019-09-01 20:33:32 +00:00
editor.remove(key);
editor.apply();
ToastEx.makeText(getContext(), R.string.title_setup_done, Toast.LENGTH_LONG).show();
}
2019-07-10 07:35:20 +00:00
private void onCleanup() {
new SimpleTask<Void>() {
@Override
protected void onPreExecute(Bundle args) {
btnCleanup.setEnabled(false);
2019-08-01 08:04:56 +00:00
ToastEx.makeText(getContext(), R.string.title_executing, Toast.LENGTH_LONG).show();
2019-07-10 07:35:20 +00:00
}
@Override
protected void onPostExecute(Bundle args) {
btnCleanup.setEnabled(true);
2019-08-13 16:22:52 +00:00
ToastEx.makeText(getContext(), R.string.title_completed, Toast.LENGTH_LONG).show();
2019-07-10 07:35:20 +00:00
}
@Override
protected Void onExecute(Context context, Bundle args) {
WorkerCleanup.cleanup(context, true);
return null;
}
2019-10-07 06:35:50 +00:00
@Override
protected void onExecuted(Bundle args, Void data) {
ServiceSynchronize.reschedule(getContext());
}
2019-07-10 07:35:20 +00:00
@Override
protected void onException(Bundle args, Throwable ex) {
2019-10-12 15:16:53 +00:00
Helper.unexpectedError(getParentFragmentManager(), ex);
2019-07-10 07:35:20 +00:00
}
}.execute(this, new Bundle(), "cleanup:run");
}
2019-05-06 07:10:13 +00:00
private void setOptions() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
swEnglish.setChecked(prefs.getBoolean("english", false));
2019-05-12 18:15:16 +00:00
swWatchdog.setChecked(prefs.getBoolean("watchdog", true));
2019-05-06 07:10:13 +00:00
swUpdates.setChecked(prefs.getBoolean("updates", true));
swUpdates.setVisibility(
2019-09-10 07:05:21 +00:00
Helper.isPlayStoreInstall() || !Helper.hasValidFingerprint(getContext())
? View.GONE : View.VISIBLE);
2019-08-24 10:30:33 +00:00
swExperiments.setChecked(prefs.getBoolean("experiments", false));
2019-05-10 06:53:45 +00:00
swCrashReports.setChecked(prefs.getBoolean("crash_reports", false));
2019-11-08 11:17:07 +00:00
tvUuid.setText(prefs.getString("uuid", null));
2019-05-06 07:10:13 +00:00
swDebug.setChecked(prefs.getBoolean("debug", false));
2019-06-15 19:25:49 +00:00
tvProcessors.setText(getString(R.string.title_advanced_processors, Runtime.getRuntime().availableProcessors()));
ActivityManager am = (ActivityManager) getContext().getSystemService(Context.ACTIVITY_SERVICE);
int class_mb = am.getMemoryClass();
tvMemoryClass.setText(getString(R.string.title_advanced_memory_class, class_mb + " MB"));
2019-10-19 19:53:19 +00:00
tvStorageSpace.setText(getString(R.string.title_advanced_storage_space,
Helper.humanReadableByteCount(Helper.getStorageSpace(), true)));
2019-05-29 06:51:08 +00:00
grpDebug.setVisibility(swDebug.isChecked() || BuildConfig.DEBUG ? View.VISIBLE : View.GONE);
2019-05-06 07:10:13 +00:00
}
private void setLastCleanup(long time) {
2019-07-15 19:28:25 +00:00
java.text.DateFormat DTF = Helper.getDateTimeInstance(getContext());
2019-05-06 07:10:13 +00:00
tvLastCleanup.setText(
getString(R.string.title_advanced_last_cleanup,
2019-07-15 19:28:25 +00:00
time < 0 ? "-" : DTF.format(time)));
2019-05-06 07:10:13 +00:00
}
}