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

474 lines
21 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.
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/>.
2021-01-01 07:56:36 +00:00
Copyright 2018-2021 by Marcel Bokhorst (M66B)
*/
import android.content.Intent;
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.Spinner;
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;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.preference.PreferenceManager;
public class FragmentOptionsSend extends FragmentBase implements SharedPreferences.OnSharedPreferenceChangeListener {
private SwitchCompat swKeyboard;
private SwitchCompat swKeyboardNoFullscreen;
private SwitchCompat swSuggestNames;
private SwitchCompat swSuggestSent;
private SwitchCompat swSuggestReceived;
private SwitchCompat swSuggestFrequently;
private Button btnLocalContacts;
2020-03-15 10:40:04 +00:00
private SwitchCompat swSendReminders;
private Spinner spSendDelayed;
2020-07-24 08:15:13 +00:00
private Spinner spComposeFont;
private SwitchCompat swPrefixOnce;
private SwitchCompat swSeparateReply;
2019-11-07 07:56:54 +00:00
private SwitchCompat swExtendedReply;
2020-11-20 13:35:07 +00:00
private SwitchCompat swWriteBelow;
2019-11-07 10:15:02 +00:00
private SwitchCompat swQuoteReply;
2020-11-14 08:36:11 +00:00
private SwitchCompat swQuoteLimit;
2020-03-25 12:32:11 +00:00
private SwitchCompat swResizeReply;
2020-02-16 12:29:19 +00:00
private Spinner spSignatureLocation;
private SwitchCompat swSignatureReply;
private SwitchCompat swSignatureForward;
2021-02-07 09:30:26 +00:00
private Button btnEditSignature;
2020-07-17 09:23:45 +00:00
private SwitchCompat swDiscardDelete;
2020-03-15 10:40:04 +00:00
2021-03-24 06:30:50 +00:00
private SwitchCompat swAutoLink;
2020-03-15 10:40:04 +00:00
private SwitchCompat swPlainOnly;
private SwitchCompat swFormatFlowed;
private SwitchCompat swUsenetSignature;
private SwitchCompat swRemoveSignatures;
private SwitchCompat swReceipt;
2020-03-07 07:20:55 +00:00
private Spinner spReceiptType;
private SwitchCompat swLookupMx;
private final static String[] RESET_OPTIONS = new String[]{
"keyboard", "keyboard_no_fullscreen",
"suggest_names", "suggest_sent", "suggested_received", "suggest_frequently",
"send_reminders", "send_delayed",
2020-11-20 13:35:07 +00:00
"compose_font", "prefix_once", "separate_reply", "extended_reply", "write_below", "quote_reply", "quote_limit", "resize_reply",
"signature_location", "signature_reply", "signature_forward",
2020-07-17 09:23:45 +00:00
"discard_delete",
2021-03-24 06:30:50 +00:00
"auto_link", "plain_only", "format_flowed", "usenet_signature", "remove_signatures",
2020-03-15 10:40:04 +00:00
"receipt_default", "receipt_type", "lookup_mx"
};
@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);
setHasOptionsMenu(true);
View view = inflater.inflate(R.layout.fragment_options_send, container, false);
// Get controls
swKeyboard = view.findViewById(R.id.swKeyboard);
swKeyboardNoFullscreen = view.findViewById(R.id.swKeyboardNoFullscreen);
swSuggestNames = view.findViewById(R.id.swSuggestNames);
swSuggestSent = view.findViewById(R.id.swSuggestSent);
swSuggestReceived = view.findViewById(R.id.swSuggestReceived);
swSuggestFrequently = view.findViewById(R.id.swSuggestFrequently);
btnLocalContacts = view.findViewById(R.id.btnLocalContacts);
2020-03-15 10:40:04 +00:00
swSendReminders = view.findViewById(R.id.swSendReminders);
spSendDelayed = view.findViewById(R.id.spSendDelayed);
2020-07-24 08:15:13 +00:00
spComposeFont = view.findViewById(R.id.spComposeFont);
2020-03-15 10:40:04 +00:00
swPrefixOnce = view.findViewById(R.id.swPrefixOnce);
swSeparateReply = view.findViewById(R.id.swSeparateReply);
2019-11-07 07:56:54 +00:00
swExtendedReply = view.findViewById(R.id.swExtendedReply);
2020-11-20 13:35:07 +00:00
swWriteBelow = view.findViewById(R.id.swWriteBelow);
2019-11-07 10:15:02 +00:00
swQuoteReply = view.findViewById(R.id.swQuoteReply);
2020-11-14 08:36:11 +00:00
swQuoteLimit = view.findViewById(R.id.swQuoteLimit);
2020-03-25 12:32:11 +00:00
swResizeReply = view.findViewById(R.id.swResizeReply);
2020-02-16 12:29:19 +00:00
spSignatureLocation = view.findViewById(R.id.spSignatureLocation);
swSignatureReply = view.findViewById(R.id.swSignatureReply);
swSignatureForward = view.findViewById(R.id.swSignatureForward);
2021-02-07 09:30:26 +00:00
btnEditSignature = view.findViewById(R.id.btnEditSignature);
2020-07-17 09:23:45 +00:00
swDiscardDelete = view.findViewById(R.id.swDiscardDelete);
2020-03-15 10:40:04 +00:00
2021-03-24 06:30:50 +00:00
swAutoLink = view.findViewById(R.id.swAutoLink);
2020-03-15 10:40:04 +00:00
swPlainOnly = view.findViewById(R.id.swPlainOnly);
swFormatFlowed = view.findViewById(R.id.swFormatFlowed);
swUsenetSignature = view.findViewById(R.id.swUsenetSignature);
swRemoveSignatures = view.findViewById(R.id.swRemoveSignatures);
swReceipt = view.findViewById(R.id.swReceipt);
2020-03-07 07:20:55 +00:00
spReceiptType = view.findViewById(R.id.spReceiptType);
swLookupMx = view.findViewById(R.id.swLookupMx);
setOptions();
// Wire controls
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
swKeyboard.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("keyboard", checked).apply();
}
});
swKeyboardNoFullscreen.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("keyboard_no_fullscreen", checked).apply();
}
});
swSuggestNames.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("suggest_names", checked).apply();
}
});
swSuggestSent.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("suggest_sent", checked).apply();
2020-03-30 12:15:29 +00:00
swSuggestFrequently.setEnabled(swSuggestSent.isChecked() || swSuggestReceived.isChecked());
}
});
swSuggestReceived.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("suggest_received", checked).apply();
2020-03-30 12:15:29 +00:00
swSuggestFrequently.setEnabled(swSuggestSent.isChecked() || swSuggestReceived.isChecked());
}
});
swSuggestFrequently.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("suggest_frequently", checked).apply();
}
});
btnLocalContacts.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(getContext());
lbm.sendBroadcast(new Intent(ActivitySetup.ACTION_MANAGE_LOCAL_CONTACTS));
}
});
2020-03-15 10:40:04 +00:00
swSendReminders.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
2020-03-15 10:40:04 +00:00
prefs.edit().putBoolean("send_reminders", checked).apply();
}
});
2020-03-15 10:40:04 +00:00
spSendDelayed.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
2019-11-07 07:56:54 +00:00
@Override
2020-03-15 10:40:04 +00:00
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
int[] values = getResources().getIntArray(R.array.sendDelayedValues);
prefs.edit().putInt("send_delayed", values[position]).apply();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
prefs.edit().remove("send_delayed").apply();
2019-11-07 07:56:54 +00:00
}
});
2020-07-24 08:15:13 +00:00
spComposeFont.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
String[] values = getResources().getStringArray(R.array.fontNameValues);
String value = values[position];
boolean monospaced = prefs.getBoolean("monospaced", false);
if (value.equals(monospaced ? "monospace" : "sans-serif"))
prefs.edit().remove("compose_font").apply();
else
prefs.edit().putString("compose_font", values[position]).apply();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
prefs.edit().remove("compose_font").apply();
}
});
2020-03-15 10:40:04 +00:00
swPrefixOnce.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
2019-11-07 10:15:02 +00:00
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
2020-03-15 10:40:04 +00:00
prefs.edit().putBoolean("prefix_once", checked).apply();
2019-11-07 10:15:02 +00:00
}
});
swSeparateReply.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("separate_reply", checked).apply();
}
});
swSeparateReply.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("separate_reply", checked).apply();
}
});
2020-03-15 10:40:04 +00:00
swExtendedReply.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
2020-01-27 18:19:25 +00:00
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
2020-03-15 10:40:04 +00:00
prefs.edit().putBoolean("extended_reply", checked).apply();
2020-01-27 18:19:25 +00:00
}
});
2020-11-20 13:35:07 +00:00
swWriteBelow.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("write_below", checked).apply();
}
});
2020-03-15 10:40:04 +00:00
swQuoteReply.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
2020-03-07 18:02:46 +00:00
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
2020-03-15 10:40:04 +00:00
prefs.edit().putBoolean("quote_reply", checked).apply();
2020-11-14 08:36:11 +00:00
}
});
swQuoteLimit.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("quote_limit", checked).apply();
2020-03-07 18:02:46 +00:00
}
});
2020-03-25 12:32:11 +00:00
swResizeReply.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
2019-05-09 16:04:18 +00:00
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
2020-03-25 12:32:11 +00:00
prefs.edit().putBoolean("resize_reply", checked).apply();
}
});
2020-03-25 12:32:11 +00:00
spSignatureLocation.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
2019-05-09 16:04:18 +00:00
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
2020-03-25 12:32:11 +00:00
prefs.edit().putInt("signature_location", position).apply();
2019-05-09 16:04:18 +00:00
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
2020-03-25 12:32:11 +00:00
prefs.edit().remove("signature_location").apply();
2019-05-09 16:04:18 +00:00
}
});
swSignatureReply.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("signature_reply", checked).apply();
}
});
swSignatureForward.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("signature_forward", checked).apply();
}
});
2021-02-07 09:30:26 +00:00
btnEditSignature.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(v.getContext());
lbm.sendBroadcast(new Intent(ActivitySetup.ACTION_VIEW_IDENTITIES));
}
});
2020-07-17 09:23:45 +00:00
swDiscardDelete.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("discard_delete", checked).apply();
}
});
2021-03-24 06:30:50 +00:00
swAutoLink.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("auto_link", checked).apply();
}
});
2020-03-15 10:40:04 +00:00
swPlainOnly.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
2020-03-15 10:40:04 +00:00
prefs.edit().putBoolean("plain_only", checked).apply();
}
});
2020-03-15 10:40:04 +00:00
swFormatFlowed.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
2020-03-07 07:20:55 +00:00
@Override
2020-03-15 10:40:04 +00:00
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("format_flowed", checked).apply();
2020-03-07 07:20:55 +00:00
}
2020-03-15 10:40:04 +00:00
});
2020-03-07 07:20:55 +00:00
2020-03-15 10:40:04 +00:00
swUsenetSignature.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
2020-03-07 07:20:55 +00:00
@Override
2020-03-15 10:40:04 +00:00
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("usenet_signature", checked).apply();
2020-03-07 07:20:55 +00:00
}
});
2020-03-15 10:40:04 +00:00
swRemoveSignatures.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
2020-01-18 09:54:19 +00:00
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
2020-03-15 10:40:04 +00:00
prefs.edit().putBoolean("remove_signatures", checked).apply();
2020-01-18 09:54:19 +00:00
}
});
2020-03-15 10:40:04 +00:00
swReceipt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
2020-03-15 10:40:04 +00:00
prefs.edit().putBoolean("receipt_default", checked).apply();
}
});
2020-03-15 10:40:04 +00:00
spReceiptType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
2020-03-15 10:40:04 +00:00
prefs.edit().putInt("receipt_type", position).apply();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
2020-03-15 10:40:04 +00:00
prefs.edit().remove("receipt_type").apply();
}
});
swLookupMx.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("lookup_mx", 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) {
2019-09-10 06:53:07 +00:00
if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED))
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) {
2021-02-05 10:15:02 +00:00
if (item.getItemId() == R.id.menu_default) {
2021-03-21 13:27:00 +00:00
FragmentOptions.reset(getContext(), RESET_OPTIONS);
2021-02-05 10:15:02 +00:00
return true;
}
2021-02-05 10:15:02 +00:00
return super.onOptionsItemSelected(item);
}
private void setOptions() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
swKeyboard.setChecked(prefs.getBoolean("keyboard", true));
swKeyboardNoFullscreen.setChecked(prefs.getBoolean("keyboard_no_fullscreen", false));
swSuggestNames.setChecked(prefs.getBoolean("suggest_names", true));
swSuggestSent.setChecked(prefs.getBoolean("suggest_sent", true));
swSuggestReceived.setChecked(prefs.getBoolean("suggest_received", false));
swSuggestFrequently.setChecked(prefs.getBoolean("suggest_frequently", false));
2020-03-30 12:15:29 +00:00
swSuggestFrequently.setEnabled(swSuggestSent.isChecked() || swSuggestReceived.isChecked());
2020-03-15 10:40:04 +00:00
swSendReminders.setChecked(prefs.getBoolean("send_reminders", true));
int send_delayed = prefs.getInt("send_delayed", 0);
int[] sendDelayedValues = getResources().getIntArray(R.array.sendDelayedValues);
for (int pos = 0; pos < sendDelayedValues.length; pos++)
if (sendDelayedValues[pos] == send_delayed) {
spSendDelayed.setSelection(pos);
break;
}
2020-07-24 08:15:13 +00:00
boolean monospaced = prefs.getBoolean("monospaced", false);
String compose_font = prefs.getString("compose_font", monospaced ? "monospace" : "sans-serif");
String[] fontNameValues = getResources().getStringArray(R.array.fontNameValues);
for (int pos = 0; pos < fontNameValues.length; pos++)
if (fontNameValues[pos].equals(compose_font)) {
spComposeFont.setSelection(pos);
break;
}
2019-08-03 12:39:26 +00:00
swPrefixOnce.setChecked(prefs.getBoolean("prefix_once", true));
swSeparateReply.setChecked(prefs.getBoolean("separate_reply", false));
2019-11-07 07:56:54 +00:00
swExtendedReply.setChecked(prefs.getBoolean("extended_reply", false));
2020-11-20 13:35:07 +00:00
swWriteBelow.setChecked(prefs.getBoolean("write_below", false));
2019-11-07 10:15:02 +00:00
swQuoteReply.setChecked(prefs.getBoolean("quote_reply", true));
2020-11-15 07:53:39 +00:00
swQuoteLimit.setChecked(prefs.getBoolean("quote_limit", true));
2020-03-25 12:32:11 +00:00
swResizeReply.setChecked(prefs.getBoolean("resize_reply", true));
2020-02-16 12:29:19 +00:00
int signature_location = prefs.getInt("signature_location", 1);
spSignatureLocation.setSelection(signature_location);
swSignatureReply.setChecked(prefs.getBoolean("signature_reply", true));
swSignatureForward.setChecked(prefs.getBoolean("signature_forward", true));
2020-07-17 09:23:45 +00:00
swDiscardDelete.setChecked(prefs.getBoolean("discard_delete", false));
2021-03-24 06:30:50 +00:00
swAutoLink.setChecked(prefs.getBoolean("auto_link", false));
2020-03-15 10:40:04 +00:00
swPlainOnly.setChecked(prefs.getBoolean("plain_only", false));
swFormatFlowed.setChecked(prefs.getBoolean("format_flowed", false));
swUsenetSignature.setChecked(prefs.getBoolean("usenet_signature", false));
swRemoveSignatures.setChecked(prefs.getBoolean("remove_signatures", false));
swReceipt.setChecked(prefs.getBoolean("receipt_default", false));
2020-03-07 07:20:55 +00:00
int receipt_type = prefs.getInt("receipt_type", 2);
spReceiptType.setSelection(receipt_type);
2020-03-15 10:40:04 +00:00
swLookupMx.setChecked(prefs.getBoolean("lookup_mx", false));
}
}