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

309 lines
12 KiB
Java
Raw Normal View History

2019-10-05 12:52:40 +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)
*/
2019-10-20 10:17:04 +00:00
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
2019-10-05 12:52:40 +00:00
import android.content.SharedPreferences;
import android.os.Bundle;
2019-10-20 10:17:04 +00:00
import android.os.Handler;
import android.text.TextUtils;
2019-11-01 09:50:32 +00:00
import android.view.KeyEvent;
2019-10-05 12:52:40 +00:00
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
2019-11-01 09:50:32 +00:00
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
2019-10-20 08:22:21 +00:00
import android.widget.AdapterView;
2019-10-20 10:17:04 +00:00
import android.widget.Button;
2019-10-05 12:52:40 +00:00
import android.widget.CompoundButton;
2019-10-20 10:17:04 +00:00
import android.widget.EditText;
2019-10-20 08:22:21 +00:00
import android.widget.Spinner;
2019-11-01 09:50:32 +00:00
import android.widget.TextView;
2019-10-05 12:52:40 +00:00
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
2019-10-20 10:17:04 +00:00
import androidx.appcompat.app.AlertDialog;
2019-10-05 12:52:40 +00:00
import androidx.appcompat.widget.SwitchCompat;
import androidx.lifecycle.Lifecycle;
import androidx.preference.PreferenceManager;
public class FragmentOptionsPrivacy extends FragmentBase implements SharedPreferences.OnSharedPreferenceChangeListener {
private SwitchCompat swDisableTracking;
2019-10-16 09:05:38 +00:00
private SwitchCompat swDisplayHidden;
2019-10-29 18:31:48 +00:00
private SwitchCompat swAutoDecrypt;
2019-10-05 12:52:40 +00:00
private SwitchCompat swNoHistory;
private Button btnBiometrics;
2019-10-20 08:22:21 +00:00
private Spinner spBiometricsTimeout;
2019-10-20 10:17:04 +00:00
private Button btnPin;
2019-10-05 12:52:40 +00:00
private final static String[] RESET_OPTIONS = new String[]{
2019-11-06 09:40:50 +00:00
"disable_tracking", "display_hidden", "auto_decrypt", "no_history", "biometrics", "pin", "biometrics_timeout"
2019-10-05 12:52:40 +00:00
};
@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_privacy, container, false);
// Get controls
swDisableTracking = view.findViewById(R.id.swDisableTracking);
2019-10-16 09:05:38 +00:00
swDisplayHidden = view.findViewById(R.id.swDisplayHidden);
2019-10-29 18:31:48 +00:00
swAutoDecrypt = view.findViewById(R.id.swAutoDecrypt);
2019-10-05 12:52:40 +00:00
swNoHistory = view.findViewById(R.id.swNoHistory);
btnBiometrics = view.findViewById(R.id.btnBiometrics);
2019-10-20 08:22:21 +00:00
spBiometricsTimeout = view.findViewById(R.id.spBiometricsTimeout);
2019-10-20 10:17:04 +00:00
btnPin = view.findViewById(R.id.btnPin);
2019-10-05 12:52:40 +00:00
setOptions();
// Wire controls
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
swDisableTracking.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("disable_tracking", checked).apply();
}
});
2019-10-16 09:05:38 +00:00
swDisplayHidden.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("display_hidden", checked).apply();
}
});
2019-10-29 18:31:48 +00:00
swAutoDecrypt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("auto_decrypt", checked).apply();
}
});
2019-10-05 12:52:40 +00:00
swNoHistory.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
2019-10-05 14:09:52 +00:00
prefs.edit().putBoolean("no_history", checked).commit(); // apply won't work here
restart();
2019-10-05 12:52:40 +00:00
}
});
btnBiometrics.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final boolean biometrics = prefs.getBoolean("biometrics", false);
Helper.authenticate(getActivity(), biometrics, new Runnable() {
@Override
public void run() {
boolean pro = ActivityBilling.isPro(getContext());
if (pro) {
prefs.edit().putBoolean("biometrics", !biometrics).apply();
btnBiometrics.setText(biometrics
? R.string.title_setup_biometrics_disable
: R.string.title_setup_biometrics_enable);
} else
startActivity(new Intent(getContext(), ActivityBilling.class));
}
}, new Runnable() {
@Override
public void run() {
// Do nothing
}
});
}
});
btnPin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentDialogPin fragment = new FragmentDialogPin();
fragment.show(getParentFragmentManager(), "pin");
}
});
2019-10-20 08:22:21 +00:00
spBiometricsTimeout.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
int[] values = getResources().getIntArray(R.array.biometricsTimeoutValues);
prefs.edit().putInt("biometrics_timeout", values[position]).apply();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
prefs.edit().remove("biometrics_timeout").apply();
}
});
2019-10-05 12:52:40 +00:00
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) {
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) {
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();
ToastEx.makeText(getContext(), R.string.title_setup_done, Toast.LENGTH_LONG).show();
}
private void setOptions() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
swDisableTracking.setChecked(prefs.getBoolean("disable_tracking", true));
2019-10-16 09:05:38 +00:00
swDisplayHidden.setChecked(prefs.getBoolean("display_hidden", false));
2019-10-29 18:31:48 +00:00
swAutoDecrypt.setChecked(prefs.getBoolean("auto_decrypt", false));
2019-10-05 12:52:40 +00:00
swNoHistory.setChecked(prefs.getBoolean("no_history", false));
2019-10-20 08:22:21 +00:00
2019-11-06 09:40:50 +00:00
boolean biometrics = prefs.getBoolean("biometrics", false);
btnBiometrics.setText(biometrics
? R.string.title_setup_biometrics_disable
: R.string.title_setup_biometrics_enable);
btnBiometrics.setEnabled(Helper.canAuthenticate(getContext()));
2019-10-20 08:22:21 +00:00
int biometrics_timeout = prefs.getInt("biometrics_timeout", 2);
int[] biometricTimeoutValues = getResources().getIntArray(R.array.biometricsTimeoutValues);
for (int pos = 0; pos < biometricTimeoutValues.length; pos++)
if (biometricTimeoutValues[pos] == biometrics_timeout) {
spBiometricsTimeout.setSelection(pos);
break;
}
2019-10-05 12:52:40 +00:00
}
2019-10-20 10:17:04 +00:00
public static class FragmentDialogPin extends FragmentDialogBase {
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
final View dview = LayoutInflater.from(getContext()).inflate(R.layout.dialog_pin_set, null);
final EditText etPin = dview.findViewById(R.id.etPin);
2019-11-06 09:40:50 +00:00
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
AlertDialog.Builder builder = new AlertDialog.Builder(getContext())
2019-10-20 10:17:04 +00:00
.setView(dview)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String pin = etPin.getText().toString();
if (TextUtils.isEmpty(pin))
prefs.edit().remove("pin").apply();
2019-11-03 09:37:11 +00:00
else {
boolean pro = ActivityBilling.isPro(getContext());
2019-11-06 09:40:50 +00:00
if (pro) {
Helper.setAuthenticated(getContext());
2019-11-03 09:37:11 +00:00
prefs.edit().putString("pin", pin).apply();
2019-11-06 09:40:50 +00:00
} else
2019-11-03 09:37:11 +00:00
startActivity(new Intent(getContext(), ActivityBilling.class));
}
2019-10-20 10:17:04 +00:00
}
})
2019-11-06 09:40:50 +00:00
.setNegativeButton(android.R.string.cancel, null);
String pin = prefs.getString("pin", null);
if (!TextUtils.isEmpty(pin))
builder.setNeutralButton(R.string.title_reset, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
prefs.edit().remove("pin").apply();
}
});
final Dialog dialog = builder.create();
2019-11-01 09:50:32 +00:00
etPin.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
((AlertDialog) getDialog()).getButton(DialogInterface.BUTTON_POSITIVE).performClick();
return true;
} else
return false;
}
});
etPin.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus)
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
});
new Handler().post(new Runnable() {
@Override
public void run() {
etPin.requestFocus();
}
});
new Handler().post(new Runnable() {
@Override
public void run() {
etPin.requestFocus();
}
});
return dialog;
2019-10-20 10:17:04 +00:00
}
}
2019-10-05 12:52:40 +00:00
}