Files
FairEmail/app/src/main/java/eu/faircode/email/FragmentOptions.java

779 lines
33 KiB
Java
Raw Normal View History

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.
2018-10-29 10:46:49 +00:00
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
2018-10-29 10:46:49 +00:00
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
2018-12-31 08:04:33 +00:00
Copyright 2018-2019 by Marcel Bokhorst (M66B)
*/
2019-02-14 12:28:03 +00:00
import android.app.Dialog;
import android.app.TimePickerDialog;
2018-11-04 15:34:30 +00:00
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.RingtoneManager;
2018-11-04 15:34:30 +00:00
import android.net.ConnectivityManager;
2018-12-10 13:36:05 +01:00
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkRequest;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
2019-02-14 12:28:03 +00:00
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;
2018-11-04 14:37:52 +00:00
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CompoundButton;
2018-11-04 14:37:52 +00:00
import android.widget.Spinner;
2019-01-12 08:21:42 +00:00
import android.widget.TextView;
2019-02-14 12:28:03 +00:00
import android.widget.TimePicker;
2019-02-14 12:28:03 +00:00
import java.text.SimpleDateFormat;
import java.util.Calendar;
2019-02-26 11:05:21 +01:00
import java.util.Objects;
2019-01-22 16:59:09 +00:00
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.SwitchCompat;
import androidx.constraintlayout.widget.Group;
2019-02-14 12:28:03 +00:00
import androidx.fragment.app.DialogFragment;
2019-01-12 14:41:59 +00:00
import androidx.fragment.app.FragmentActivity;
2019-02-14 19:42:34 +00:00
import androidx.fragment.app.FragmentTransaction;
2019-01-20 17:41:47 +00:00
import androidx.lifecycle.Lifecycle;
2019-03-15 13:54:25 +00:00
import androidx.preference.PreferenceManager;
import static android.app.Activity.RESULT_OK;
2019-01-15 17:41:55 +00:00
public class FragmentOptions extends FragmentBase implements SharedPreferences.OnSharedPreferenceChangeListener {
private SwitchCompat swEnabled;
2019-02-14 13:28:14 +00:00
private SwitchCompat swSchedule;
2019-02-14 12:28:03 +00:00
private TextView tvScheduleStart;
private TextView tvScheduleEnd;
2018-12-11 19:01:14 +01:00
2019-01-12 08:21:42 +00:00
private TextView tvConnectionType;
private SwitchCompat swMetered;
2018-12-11 19:01:14 +01:00
private Spinner spDownload;
private Spinner spStartup;
2019-01-21 18:12:22 +00:00
private SwitchCompat swDate;
private SwitchCompat swThreading;
private SwitchCompat swAvatars;
2018-11-03 16:34:35 +00:00
private SwitchCompat swIdenticons;
private SwitchCompat swNameEmail;
2019-03-09 07:36:45 +00:00
private SwitchCompat swSubjectItalic;
2019-03-02 15:54:38 +00:00
private SwitchCompat swFlags;
2018-11-04 12:42:41 +00:00
private SwitchCompat swPreview;
private SwitchCompat swAddresses;
private SwitchCompat swMonospaced;
private SwitchCompat swHtml;
private SwitchCompat swImages;
2019-01-30 19:35:36 +00:00
private SwitchCompat swActionbar;
2018-12-11 19:01:14 +01:00
private SwitchCompat swPull;
private SwitchCompat swSwipeNav;
2019-01-30 19:35:36 +00:00
private SwitchCompat swAutoExpand;
private SwitchCompat swAutoClose;
private SwitchCompat swAutoNext;
private SwitchCompat swCollapse;
private SwitchCompat swAutoRead;
private SwitchCompat swAutoMove;
private SwitchCompat swAutoResize;
2019-02-13 08:53:16 +00:00
private SwitchCompat swSender;
private SwitchCompat swAutoSend;
private SwitchCompat swBadge;
private SwitchCompat swNotifyPreview;
2019-02-18 09:49:59 +00:00
private SwitchCompat swSearchLocal;
private SwitchCompat swLight;
private Button btnSound;
2018-12-11 19:01:14 +01:00
private SwitchCompat swParanoid;
private SwitchCompat swEnglish;
2019-02-14 12:28:03 +00:00
private SwitchCompat swUpdates;
private SwitchCompat swDebug;
private Group grpSearchLocal;
private Group grpNotification;
2019-01-21 18:12:22 +00:00
static String[] OPTIONS_RESTART = new String[]{
"startup", "date", "threading", "avatars", "identicons", "name_email", "subject_italic", "flags", "preview",
"addresses", "monospaced", "autohtml", "autoimages", "actionbar",
2019-02-06 10:59:10 +00:00
"pull", "swipenav", "autoexpand", "autoclose", "autonext",
2019-01-30 19:35:36 +00:00
"debug"
2019-01-21 18:12:22 +00:00
};
private final static String[] ADVANCED_OPTIONS = new String[]{
2019-02-14 12:28:03 +00:00
"enabled", "schedule_start", "schedule_end",
"metered", "download",
"startup", "date", "threading", "avatars", "identicons", "name_email", "subject_italic", "flags", "preview",
"addresses", "monospaced", "autohtml", "autoimages", "actionbar",
2019-02-13 08:53:16 +00:00
"pull", "swipenav", "autoexpand", "autoclose", "autonext", "collapse", "autoread", "automove",
"autoresize", "sender", "autosend",
2019-02-18 09:49:59 +00:00
"notify_preview", "search_local", "light", "sound",
2019-03-30 10:18:52 +00:00
"paranoid", "english", "updates", "debug",
2019-01-30 19:35:36 +00:00
"first", "why", "last_update_check", "app_support", "message_swipe", "message_select", "folder_actions", "folder_sync",
2019-03-26 18:26:21 +00:00
"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);
2018-12-10 13:36:05 +01:00
setHasOptionsMenu(true);
View view = inflater.inflate(R.layout.fragment_options, container, false);
// Get controls
swEnabled = view.findViewById(R.id.swEnabled);
2019-02-14 13:28:14 +00:00
swSchedule = view.findViewById(R.id.swSchedule);
2019-02-14 12:28:03 +00:00
tvScheduleStart = view.findViewById(R.id.tvScheduleStart);
tvScheduleEnd = view.findViewById(R.id.tvScheduleEnd);
2018-12-11 19:01:14 +01:00
2019-01-12 08:21:42 +00:00
tvConnectionType = view.findViewById(R.id.tvConnectionType);
swMetered = view.findViewById(R.id.swMetered);
2018-12-11 19:01:14 +01:00
spDownload = view.findViewById(R.id.spDownload);
spStartup = view.findViewById(R.id.spStartup);
2019-01-21 18:12:22 +00:00
swDate = view.findViewById(R.id.swDate);
swThreading = view.findViewById(R.id.swThreading);
swAvatars = view.findViewById(R.id.swAvatars);
2018-11-03 16:34:35 +00:00
swIdenticons = view.findViewById(R.id.swIdenticons);
swNameEmail = view.findViewById(R.id.swNameEmail);
2019-03-09 07:36:45 +00:00
swSubjectItalic = view.findViewById(R.id.swSubjectItalic);
2019-03-02 15:54:38 +00:00
swFlags = view.findViewById(R.id.swFlags);
2018-11-04 12:42:41 +00:00
swPreview = view.findViewById(R.id.swPreview);
swAddresses = view.findViewById(R.id.swAddresses);
swMonospaced = view.findViewById(R.id.swMonospaced);
swHtml = view.findViewById(R.id.swHtml);
swImages = view.findViewById(R.id.swImages);
2019-01-30 19:35:36 +00:00
swActionbar = view.findViewById(R.id.swActionbar);
2018-12-11 19:01:14 +01:00
swPull = view.findViewById(R.id.swPull);
swSwipeNav = view.findViewById(R.id.swSwipeNav);
2019-01-30 19:35:36 +00:00
swAutoExpand = view.findViewById(R.id.swAutoExpand);
swAutoClose = view.findViewById(R.id.swAutoClose);
swAutoNext = view.findViewById(R.id.swAutoNext);
swCollapse = view.findViewById(R.id.swCollapse);
swAutoRead = view.findViewById(R.id.swAutoRead);
swAutoMove = view.findViewById(R.id.swAutoMove);
swAutoResize = view.findViewById(R.id.swAutoResize);
2019-02-13 08:53:16 +00:00
swSender = view.findViewById(R.id.swSender);
swAutoSend = view.findViewById(R.id.swAutoSend);
swBadge = view.findViewById(R.id.swBadge);
swNotifyPreview = view.findViewById(R.id.swNotifyPreview);
2019-02-18 09:49:59 +00:00
swSearchLocal = view.findViewById(R.id.swSearchLocal);
swLight = view.findViewById(R.id.swLight);
btnSound = view.findViewById(R.id.btnSound);
2018-12-11 19:01:14 +01:00
swParanoid = view.findViewById(R.id.swParanoid);
swEnglish = view.findViewById(R.id.swEnglish);
2019-02-14 12:28:03 +00:00
swUpdates = view.findViewById(R.id.swUpdates);
swDebug = view.findViewById(R.id.swDebug);
grpSearchLocal = view.findViewById(R.id.grpSearchLocal);
grpNotification = view.findViewById(R.id.grpNotification);
// Wire controls
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
setOptions();
swEnabled.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("enabled", checked).apply();
ServiceSynchronize.reload(getContext(), "enabled=" + checked);
}
});
2019-02-14 13:28:14 +00:00
swSchedule.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
2019-02-14 19:42:34 +00:00
if (checked) {
if (Helper.isPro(getContext())) {
prefs.edit().putBoolean("schedule", true).apply();
2019-02-28 07:55:08 +00:00
ServiceSynchronize.reschedule(getContext());
2019-02-14 19:42:34 +00:00
} else {
swSchedule.setChecked(false);
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, new FragmentPro()).addToBackStack("pro");
fragmentTransaction.commit();
}
} else {
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("schedule", false);
editor.putBoolean("enabled", true);
editor.apply();
2019-02-14 13:28:14 +00:00
ServiceSynchronize.reload(getContext(), "schedule=" + checked);
}
}
});
2019-02-14 12:28:03 +00:00
tvScheduleStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle args = new Bundle();
args.putBoolean("start", true);
DialogFragment timePicker = new TimePickerFragment();
timePicker.setArguments(args);
timePicker.show(getFragmentManager(), "timePicker");
}
});
tvScheduleEnd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle args = new Bundle();
args.putBoolean("start", false);
DialogFragment timePicker = new TimePickerFragment();
timePicker.setArguments(args);
timePicker.show(getFragmentManager(), "timePicker");
}
});
swParanoid.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("paranoid", checked).apply();
}
});
swEnglish.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("english", checked).commit(); // apply won't work here
Intent intent = new Intent(getContext(), ActivityMain.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
Runtime.getRuntime().exit(0);
}
});
2018-12-28 07:47:51 +00:00
swUpdates.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("updates", checked).apply();
}
});
swMetered.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("metered", checked).apply();
ServiceSynchronize.reload(getContext(), "metered=" + checked);
}
});
2018-12-11 19:01:14 +01:00
spDownload.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
2019-01-22 16:59:09 +00:00
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
Integer prev = (Integer) adapterView.getTag();
2019-02-26 11:05:21 +01:00
if (!Objects.equals(prev, position)) {
2019-01-22 16:59:09 +00:00
adapterView.setTag(position);
int[] values = getResources().getIntArray(R.array.downloadValues);
prefs.edit().putInt("download", values[position]).apply();
}
2018-12-11 19:01:14 +01:00
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
prefs.edit().remove("download").apply();
}
});
spStartup.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
String[] values = getResources().getStringArray(R.array.startupValues);
prefs.edit().putString("startup", values[position]).apply();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
prefs.edit().remove("startup").apply();
}
});
2019-01-21 18:12:22 +00:00
swDate.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("date", checked).apply();
}
});
swThreading.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("threading", checked).apply();
}
});
swAvatars.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("avatars", checked).apply();
2019-01-26 09:58:37 +00:00
ContactInfo.clearCache();
}
});
2018-11-03 16:34:35 +00:00
swIdenticons.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("identicons", checked).apply();
2019-01-26 09:58:37 +00:00
ContactInfo.clearCache();
2018-11-03 16:34:35 +00:00
}
});
swNameEmail.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("name_email", checked).apply();
}
});
2019-03-09 07:36:45 +00:00
swSubjectItalic.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("subject_italic", checked).apply();
}
});
2019-03-02 15:54:38 +00:00
swFlags.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("flags", checked).apply();
}
});
2018-11-04 12:42:41 +00:00
swPreview.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("preview", checked).apply();
}
});
swAddresses.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("addresses", checked).apply();
}
});
swMonospaced.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("monospaced", checked).apply();
}
});
swHtml.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("autohtml", checked).apply();
}
});
swImages.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("autoimages", checked).apply();
}
});
2019-01-30 19:35:36 +00:00
swActionbar.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("actionbar", checked).apply();
}
});
swPull.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("pull", checked).apply();
}
});
swSwipeNav.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("swipenav", checked).apply();
}
});
2019-01-30 19:35:36 +00:00
swAutoExpand.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
2019-01-30 19:35:36 +00:00
prefs.edit().putBoolean("autoexpand", checked).apply();
}
});
swAutoClose.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("autoclose", checked).apply();
swAutoNext.setEnabled(!checked);
}
});
swAutoNext.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("autonext", checked).apply();
2019-01-11 17:16:48 +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();
}
});
2019-02-13 08:53:16 +00:00
swAutoResize.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
2018-11-09 07:22:44 +00:00
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
2019-02-13 08:53:16 +00:00
prefs.edit().putBoolean("autoresize", checked).apply();
2018-11-09 07:22:44 +00:00
}
});
2019-02-13 08:53:16 +00:00
swSender.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
2019-02-13 08:53:16 +00:00
prefs.edit().putBoolean("sender", checked).apply();
}
});
swAutoSend.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("autosend", !checked).apply();
}
});
swBadge.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("badge", checked).apply();
ServiceSynchronize.reload(getContext(), "badge");
}
});
swNotifyPreview.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("notify_preview", checked).apply();
}
});
2019-02-18 09:49:59 +00:00
swSearchLocal.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("search_local", checked).apply();
}
});
swLight.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("light", checked).apply();
}
});
btnSound.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String sound = prefs.getString("sound", null);
Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, getString(R.string.title_advanced_sound));
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, sound == null ? null : Uri.parse(sound));
startActivityForResult(Helper.getChooser(getContext(), intent), ActivitySetup.REQUEST_SOUND);
}
2018-11-09 07:22:44 +00:00
});
swDebug.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("debug", checked).apply();
ServiceSynchronize.reload(getContext(), "debug=" + checked);
}
});
2018-11-10 10:45:03 +00:00
PreferenceManager.getDefaultSharedPreferences(getContext()).registerOnSharedPreferenceChangeListener(this);
return view;
}
2018-11-10 10:45:03 +00:00
2018-12-10 13:36:05 +01:00
@Override
public void onResume() {
super.onResume();
ConnectivityManager cm = (ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkRequest.Builder builder = new NetworkRequest.Builder();
builder.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
cm.registerNetworkCallback(builder.build(), networkCallback);
}
@Override
public void onPause() {
ConnectivityManager cm = (ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
cm.unregisterNetworkCallback(networkCallback);
super.onPause();
}
@Override
public void onDestroyView() {
PreferenceManager.getDefaultSharedPreferences(getContext()).unregisterOnSharedPreferenceChangeListener(this);
super.onDestroyView();
}
@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();
setOptions();
}
private void setOptions() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
swEnabled.setChecked(prefs.getBoolean("enabled", true));
2019-02-14 13:28:14 +00:00
swSchedule.setChecked(prefs.getBoolean("schedule", false));
2019-02-14 12:28:03 +00:00
tvScheduleStart.setText(formatHour(prefs.getInt("schedule_start", 0)));
tvScheduleEnd.setText(formatHour(prefs.getInt("schedule_end", 0)));
swMetered.setChecked(prefs.getBoolean("metered", true));
int download = prefs.getInt("download", MessageHelper.DEFAULT_ATTACHMENT_DOWNLOAD_SIZE);
int[] downloadValues = getResources().getIntArray(R.array.downloadValues);
for (int pos = 0; pos < downloadValues.length; pos++)
if (downloadValues[pos] == download) {
2019-01-22 16:59:09 +00:00
spDownload.setTag(pos);
spDownload.setSelection(pos);
break;
}
boolean compact = prefs.getBoolean("compact", false);
String startup = prefs.getString("startup", "unified");
String[] startupValues = getResources().getStringArray(R.array.startupValues);
for (int pos = 0; pos < startupValues.length; pos++)
if (startupValues[pos].equals(startup)) {
spStartup.setSelection(pos);
break;
}
2019-01-21 18:12:22 +00:00
swDate.setChecked(prefs.getBoolean("date", true));
swThreading.setChecked(prefs.getBoolean("threading", true));
swAvatars.setChecked(prefs.getBoolean("avatars", true));
swIdenticons.setChecked(prefs.getBoolean("identicons", false));
swNameEmail.setChecked(prefs.getBoolean("name_email", !compact));
2019-03-09 07:36:45 +00:00
swSubjectItalic.setChecked(prefs.getBoolean("subject_italic", true));
2019-03-02 15:54:38 +00:00
swFlags.setChecked(prefs.getBoolean("flags", true));
swPreview.setChecked(prefs.getBoolean("preview", false));
swAddresses.setChecked(prefs.getBoolean("addresses", true));
2019-03-27 09:22:27 +00:00
swMonospaced.setChecked(prefs.getBoolean("monospaced", false));
swHtml.setChecked(prefs.getBoolean("autohtml", false));
swImages.setChecked(prefs.getBoolean("autoimages", false));
2019-01-30 19:35:36 +00:00
swActionbar.setChecked(prefs.getBoolean("actionbar", true));
swPull.setChecked(prefs.getBoolean("pull", true));
swSwipeNav.setChecked(prefs.getBoolean("swipenav", true));
2019-01-30 19:35:36 +00:00
swAutoExpand.setChecked(prefs.getBoolean("autoexpand", true));
swAutoClose.setChecked(prefs.getBoolean("autoclose", true));
swAutoNext.setChecked(prefs.getBoolean("autonext", false));
swAutoNext.setEnabled(!swAutoClose.isChecked());
swCollapse.setChecked(prefs.getBoolean("collapse", false));
swAutoRead.setChecked(prefs.getBoolean("autoread", false));
swAutoMove.setChecked(!prefs.getBoolean("automove", false));
swAutoResize.setChecked(prefs.getBoolean("autoresize", true));
2019-02-13 08:53:16 +00:00
swSender.setChecked(prefs.getBoolean("sender", false));
swAutoSend.setChecked(!prefs.getBoolean("autosend", false));
swBadge.setChecked(prefs.getBoolean("badge", true));
swNotifyPreview.setChecked(prefs.getBoolean("notify_preview", true));
swNotifyPreview.setEnabled(Helper.isPro(getContext()));
2019-02-18 09:49:59 +00:00
swSearchLocal.setChecked(prefs.getBoolean("search_local", false));
swLight.setChecked(prefs.getBoolean("light", false));
swParanoid.setChecked(prefs.getBoolean("paranoid", true));
swEnglish.setChecked(prefs.getBoolean("english", false));
2019-02-14 12:28:03 +00:00
swUpdates.setChecked(prefs.getBoolean("updates", true));
swUpdates.setVisibility(Helper.isPlayStoreInstall(getContext()) ? View.GONE : View.VISIBLE);
swDebug.setChecked(prefs.getBoolean("debug", false));
grpSearchLocal.setVisibility(Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.M ? View.GONE : View.VISIBLE);
grpNotification.setVisibility(Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.O ? View.VISIBLE : View.GONE);
}
2019-02-14 12:28:03 +00:00
private String formatHour(int minutes) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, minutes / 60);
cal.set(Calendar.MINUTE, minutes % 60);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return SimpleDateFormat.getTimeInstance(SimpleDateFormat.SHORT).format(cal.getTime());
}
public static class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Bundle args = getArguments();
boolean start = args.getBoolean("start");
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
int minutes = prefs.getInt("schedule_" + (start ? "start" : "end"), 0);
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, minutes / 60);
cal.set(Calendar.MINUTE, minutes % 60);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return new TimePickerDialog(getActivity(), this,
cal.get(Calendar.HOUR_OF_DAY),
cal.get(Calendar.MINUTE),
DateFormat.is24HourFormat(getActivity()));
}
public void onTimeSet(TimePicker view, int hour, int minute) {
Bundle args = getArguments();
boolean start = args.getBoolean("start");
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
2019-02-14 13:28:14 +00:00
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("schedule_" + (start ? "start" : "end"), hour * 60 + minute);
editor.putBoolean("schedule", true);
editor.apply();
2019-02-14 12:28:03 +00:00
2019-02-28 07:55:08 +00:00
ServiceSynchronize.reschedule(getContext());
2019-02-14 12:28:03 +00:00
}
}
2018-12-10 13:36:05 +01:00
private ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
2019-01-12 08:21:42 +00:00
showConnectionType();
2018-12-10 13:36:05 +01:00
}
@Override
public void onCapabilitiesChanged(Network network, NetworkCapabilities networkCapabilities) {
2019-01-12 08:21:42 +00:00
showConnectionType();
2018-12-10 13:36:05 +01:00
}
@Override
public void onLost(Network network) {
2019-01-12 08:21:42 +00:00
showConnectionType();
2018-12-10 13:36:05 +01:00
}
};
2019-02-14 13:28:14 +00:00
private void showConnectionType() {
2019-01-12 14:41:59 +00:00
FragmentActivity activity = getActivity();
if (activity == null)
return;
2019-01-02 18:43:51 +01:00
2019-01-12 14:41:59 +00:00
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
2019-01-20 17:41:47 +00:00
if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED)) {
2019-03-16 13:12:31 +00:00
Helper.NetworkState networkState = Helper.getNetworkState(getContext());
2019-01-12 14:41:59 +00:00
2019-03-16 13:12:31 +00:00
tvConnectionType.setText(networkState.isUnmetered() ? R.string.title_legend_unmetered : R.string.title_legend_metered);
tvConnectionType.setVisibility(networkState.isConnected() ? View.VISIBLE : View.GONE);
2019-01-20 17:41:47 +00:00
}
2019-01-12 14:41:59 +00:00
}
});
2018-12-10 13:36:05 +01:00
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
2018-12-24 12:27:45 +00:00
Log.i("Result class=" + this.getClass().getSimpleName() +
" request=" + requestCode + " result=" + resultCode + " data=" + data);
if (requestCode == ActivitySetup.REQUEST_SOUND)
if (resultCode == RESULT_OK) {
Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
2018-12-24 12:27:45 +00:00
Log.i("Selected ringtone=" + uri);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
if (uri == null)
prefs.edit().remove("sound").apply();
else
prefs.edit().putString("sound", uri.toString()).apply();
}
}
2018-11-10 10:45:03 +00:00
@Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
if ("enabled".equals(key))
swEnabled.setChecked(prefs.getBoolean(key, true));
2019-02-14 13:28:14 +00:00
else if ("schedule".equals(key))
swSchedule.setChecked(prefs.getBoolean(key, false));
2019-02-14 12:28:03 +00:00
else if ("schedule_start".equals(key))
tvScheduleStart.setText(formatHour(prefs.getInt(key, 0)));
else if ("schedule_end".equals(key))
tvScheduleEnd.setText(formatHour(prefs.getInt(key, 0)));
2018-11-10 10:45:03 +00:00
}
}