mirror of https://github.com/M66B/FairEmail.git
336 lines
13 KiB
Java
336 lines
13 KiB
Java
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.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;
|
|
import android.widget.Button;
|
|
import android.widget.CompoundButton;
|
|
import android.widget.RadioGroup;
|
|
import android.widget.Spinner;
|
|
|
|
import androidx.annotation.NonNull;
|
|
import androidx.annotation.Nullable;
|
|
import androidx.appcompat.widget.SwitchCompat;
|
|
import androidx.preference.PreferenceManager;
|
|
|
|
public class FragmentOptionsDisplay extends FragmentBase implements SharedPreferences.OnSharedPreferenceChangeListener {
|
|
private Button btnTheme;
|
|
private Spinner spStartup;
|
|
private SwitchCompat swDate;
|
|
private SwitchCompat swThreading;
|
|
private SwitchCompat swAvatars;
|
|
private SwitchCompat swIdenticons;
|
|
private SwitchCompat swCircular;
|
|
private SwitchCompat swNameEmail;
|
|
private SwitchCompat swSubjectItalic;
|
|
private SwitchCompat swFlags;
|
|
private SwitchCompat swPreview;
|
|
private SwitchCompat swAddresses;
|
|
private SwitchCompat swAttachmentsAlt;
|
|
private SwitchCompat swMonospaced;
|
|
private SwitchCompat swImages;
|
|
private SwitchCompat swActionbar;
|
|
|
|
private final static String[] RESET_OPTIONS = new String[]{
|
|
"theme", "startup", "date", "threading", "avatars", "identicons", "circular", "name_email", "subject_italic",
|
|
"flags", "preview", "addresses", "attachments_alt", "monospaced", "autoimages", "actionbar",
|
|
};
|
|
|
|
@Override
|
|
@Nullable
|
|
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
|
setSubtitle(R.string.title_setup);
|
|
setHasOptionsMenu(true);
|
|
|
|
View view = inflater.inflate(R.layout.fragment_options_display, container, false);
|
|
|
|
// Get controls
|
|
|
|
btnTheme = view.findViewById(R.id.btnTheme);
|
|
spStartup = view.findViewById(R.id.spStartup);
|
|
swDate = view.findViewById(R.id.swDate);
|
|
swThreading = view.findViewById(R.id.swThreading);
|
|
swAvatars = view.findViewById(R.id.swAvatars);
|
|
swIdenticons = view.findViewById(R.id.swIdenticons);
|
|
swCircular = view.findViewById(R.id.swCircular);
|
|
swNameEmail = view.findViewById(R.id.swNameEmail);
|
|
swSubjectItalic = view.findViewById(R.id.swSubjectItalic);
|
|
swFlags = view.findViewById(R.id.swFlags);
|
|
swPreview = view.findViewById(R.id.swPreview);
|
|
swAddresses = view.findViewById(R.id.swAddresses);
|
|
swAttachmentsAlt = view.findViewById(R.id.swAttachmentsAlt);
|
|
swMonospaced = view.findViewById(R.id.swMonospaced);
|
|
swImages = view.findViewById(R.id.swImages);
|
|
swActionbar = view.findViewById(R.id.swActionbar);
|
|
|
|
setOptions();
|
|
|
|
// Wire controls
|
|
|
|
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
|
|
|
|
btnTheme.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View view) {
|
|
onSelectTheme();
|
|
}
|
|
});
|
|
|
|
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();
|
|
}
|
|
});
|
|
|
|
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();
|
|
ContactInfo.clearCache();
|
|
}
|
|
});
|
|
|
|
swIdenticons.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
|
|
@Override
|
|
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
|
|
prefs.edit().putBoolean("identicons", checked).apply();
|
|
ContactInfo.clearCache();
|
|
}
|
|
});
|
|
|
|
swCircular.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
|
|
@Override
|
|
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
|
|
prefs.edit().putBoolean("circular", checked).apply();
|
|
ContactInfo.clearCache();
|
|
}
|
|
});
|
|
|
|
swNameEmail.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
|
|
@Override
|
|
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
|
|
prefs.edit().putBoolean("name_email", checked).apply();
|
|
}
|
|
});
|
|
|
|
swSubjectItalic.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
|
|
@Override
|
|
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
|
|
prefs.edit().putBoolean("subject_italic", checked).apply();
|
|
}
|
|
});
|
|
|
|
swFlags.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
|
|
@Override
|
|
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
|
|
prefs.edit().putBoolean("flags", checked).apply();
|
|
}
|
|
});
|
|
|
|
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();
|
|
}
|
|
});
|
|
|
|
swAttachmentsAlt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
|
|
@Override
|
|
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
|
|
prefs.edit().putBoolean("attachments_alt", checked).apply();
|
|
}
|
|
});
|
|
|
|
swMonospaced.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
|
|
@Override
|
|
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
|
|
prefs.edit().putBoolean("monospaced", checked).apply();
|
|
}
|
|
});
|
|
|
|
swImages.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
|
|
@Override
|
|
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
|
|
prefs.edit().putBoolean("autoimages", checked).apply();
|
|
}
|
|
});
|
|
|
|
swActionbar.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
|
|
@Override
|
|
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
|
|
prefs.edit().putBoolean("actionbar", checked).apply();
|
|
}
|
|
});
|
|
|
|
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 onSelectTheme() {
|
|
View dview = LayoutInflater.from(getContext()).inflate(R.layout.dialog_theme, null);
|
|
final RadioGroup rgTheme = dview.findViewById(R.id.rgTheme);
|
|
|
|
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
|
|
String theme = prefs.getString("theme", "light");
|
|
|
|
switch (theme) {
|
|
case "dark":
|
|
rgTheme.check(R.id.rbThemeDark);
|
|
break;
|
|
case "black":
|
|
rgTheme.check(R.id.rbThemeBlack);
|
|
break;
|
|
case "system":
|
|
rgTheme.check(R.id.rbThemeSystem);
|
|
break;
|
|
default:
|
|
rgTheme.check(R.id.rbThemeLight);
|
|
}
|
|
|
|
rgTheme.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
|
|
@Override
|
|
public void onCheckedChanged(RadioGroup group, int checkedId) {
|
|
getActivity().getIntent().putExtra("tab", "display");
|
|
|
|
switch (checkedId) {
|
|
case R.id.rbThemeLight:
|
|
prefs.edit().putString("theme", "light").apply();
|
|
break;
|
|
case R.id.rbThemeDark:
|
|
prefs.edit().putString("theme", "dark").apply();
|
|
break;
|
|
case R.id.rbThemeBlack:
|
|
prefs.edit().putString("theme", "black").apply();
|
|
break;
|
|
case R.id.rbThemeSystem:
|
|
prefs.edit().putString("theme", "system").apply();
|
|
break;
|
|
}
|
|
}
|
|
});
|
|
|
|
new DialogBuilderLifecycle(getContext(), getViewLifecycleOwner())
|
|
.setView(dview)
|
|
.show();
|
|
}
|
|
|
|
private void setOptions() {
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
|
|
|
|
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;
|
|
}
|
|
|
|
swDate.setChecked(prefs.getBoolean("date", true));
|
|
swThreading.setChecked(prefs.getBoolean("threading", true));
|
|
swAvatars.setChecked(prefs.getBoolean("avatars", true));
|
|
swIdenticons.setChecked(prefs.getBoolean("identicons", false));
|
|
swCircular.setChecked(prefs.getBoolean("circular", true));
|
|
swNameEmail.setChecked(prefs.getBoolean("name_email", !compact));
|
|
swSubjectItalic.setChecked(prefs.getBoolean("subject_italic", true));
|
|
swFlags.setChecked(prefs.getBoolean("flags", true));
|
|
swPreview.setChecked(prefs.getBoolean("preview", false));
|
|
swAddresses.setChecked(prefs.getBoolean("addresses", false));
|
|
swAttachmentsAlt.setChecked(prefs.getBoolean("attachments_alt", false));
|
|
swMonospaced.setChecked(prefs.getBoolean("monospaced", false));
|
|
swImages.setChecked(prefs.getBoolean("autoimages", false));
|
|
swActionbar.setChecked(prefs.getBoolean("actionbar", true));
|
|
}
|
|
}
|