2014-11-04 14:19:48 +00:00
|
|
|
package org.thoughtcrime.securesms.preferences;
|
|
|
|
|
|
|
|
import android.app.AlertDialog;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.DialogInterface;
|
|
|
|
import android.content.Intent;
|
2015-02-13 23:06:50 +00:00
|
|
|
import android.content.res.TypedArray;
|
2014-11-04 14:19:48 +00:00
|
|
|
import android.os.Build;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.preference.CheckBoxPreference;
|
|
|
|
import android.preference.Preference;
|
|
|
|
import android.preference.PreferenceScreen;
|
|
|
|
import android.support.v4.preference.PreferenceFragment;
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
2014-12-13 00:07:02 +00:00
|
|
|
import com.doomonafireball.betterpickers.hmspicker.HmsPickerBuilder;
|
|
|
|
import com.doomonafireball.betterpickers.hmspicker.HmsPickerDialogFragment;
|
|
|
|
|
2014-11-04 14:19:48 +00:00
|
|
|
import org.thoughtcrime.securesms.ApplicationPreferencesActivity;
|
|
|
|
import org.thoughtcrime.securesms.PassphraseChangeActivity;
|
|
|
|
import org.thoughtcrime.securesms.R;
|
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecretUtil;
|
|
|
|
import org.thoughtcrime.securesms.service.KeyCachingService;
|
|
|
|
import org.thoughtcrime.securesms.util.Dialogs;
|
2015-01-30 06:37:01 +00:00
|
|
|
import org.thoughtcrime.securesms.util.ResUtil;
|
2014-11-04 14:19:48 +00:00
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
|
|
|
|
2014-12-13 00:07:02 +00:00
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
2014-11-04 14:19:48 +00:00
|
|
|
public class AppProtectionPreferenceFragment extends PreferenceFragment {
|
|
|
|
private CheckBoxPreference disablePassphrase;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle paramBundle) {
|
|
|
|
super.onCreate(paramBundle);
|
|
|
|
addPreferencesFromResource(R.xml.preferences_app_protection);
|
|
|
|
|
|
|
|
disablePassphrase = (CheckBoxPreference) this.findPreference("pref_enable_passphrase_temporary");
|
|
|
|
|
|
|
|
this.findPreference(TextSecurePreferences.CHANGE_PASSPHRASE_PREF)
|
2014-12-13 00:07:02 +00:00
|
|
|
.setOnPreferenceClickListener(new ChangePassphraseClickListener());
|
|
|
|
this.findPreference(TextSecurePreferences.PASSPHRASE_TIMEOUT_INTERVAL_PREF)
|
|
|
|
.setOnPreferenceClickListener(new PassphraseIntervalClickListener());
|
2014-11-04 14:19:48 +00:00
|
|
|
disablePassphrase
|
2014-12-13 00:07:02 +00:00
|
|
|
.setOnPreferenceChangeListener(new DisablePassphraseClickListener());
|
2014-11-04 14:19:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
((ApplicationPreferencesActivity) getActivity()).getSupportActionBar().setTitle(R.string.preferences__app_protection);
|
2014-12-13 00:07:02 +00:00
|
|
|
|
2014-11-04 14:19:48 +00:00
|
|
|
initializePlatformSpecificOptions();
|
2014-12-13 00:07:02 +00:00
|
|
|
initializeTimeoutSummary();
|
2014-11-04 14:19:48 +00:00
|
|
|
|
|
|
|
disablePassphrase.setChecked(!TextSecurePreferences.isPasswordDisabled(getActivity()));
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initializePlatformSpecificOptions() {
|
|
|
|
PreferenceScreen preferenceScreen = getPreferenceScreen();
|
|
|
|
Preference screenSecurityPreference = findPreference(TextSecurePreferences.SCREEN_SECURITY_PREF);
|
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH &&
|
|
|
|
screenSecurityPreference != null) {
|
|
|
|
preferenceScreen.removePreference(screenSecurityPreference);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-13 00:07:02 +00:00
|
|
|
private void initializeTimeoutSummary() {
|
|
|
|
int timeoutMinutes = TextSecurePreferences.getPassphraseTimeoutInterval(getActivity());
|
|
|
|
this.findPreference(TextSecurePreferences.PASSPHRASE_TIMEOUT_INTERVAL_PREF)
|
2014-12-13 00:37:06 +00:00
|
|
|
.setSummary(getString(R.string.AppProtectionPreferenceFragment_minutes, timeoutMinutes));
|
2014-12-13 00:07:02 +00:00
|
|
|
}
|
|
|
|
|
2014-11-04 14:19:48 +00:00
|
|
|
private class ChangePassphraseClickListener implements Preference.OnPreferenceClickListener {
|
|
|
|
@Override
|
|
|
|
public boolean onPreferenceClick(Preference preference) {
|
|
|
|
if (MasterSecretUtil.isPassphraseInitialized(getActivity())) {
|
|
|
|
startActivity(new Intent(getActivity(), PassphraseChangeActivity.class));
|
|
|
|
} else {
|
|
|
|
Toast.makeText(getActivity(),
|
|
|
|
R.string.ApplicationPreferenceActivity_you_havent_set_a_passphrase_yet,
|
|
|
|
Toast.LENGTH_LONG).show();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-13 00:07:02 +00:00
|
|
|
private class PassphraseIntervalClickListener implements Preference.OnPreferenceClickListener, HmsPickerDialogFragment.HmsPickerDialogHandler {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onPreferenceClick(Preference preference) {
|
2015-02-13 23:06:50 +00:00
|
|
|
int[] attributes = {R.attr.app_protect_timeout_picker_color};
|
|
|
|
TypedArray hmsStyle = getActivity().obtainStyledAttributes(attributes);
|
|
|
|
|
2014-12-13 00:07:02 +00:00
|
|
|
new HmsPickerBuilder().setFragmentManager(getFragmentManager())
|
2015-02-13 23:06:50 +00:00
|
|
|
.setStyleResId(hmsStyle.getResourceId(0, R.style.BetterPickersDialogFragment_Light))
|
2014-12-13 00:07:02 +00:00
|
|
|
.addHmsPickerDialogHandler(this)
|
|
|
|
.show();
|
2015-02-13 23:06:50 +00:00
|
|
|
|
|
|
|
hmsStyle.recycle();
|
|
|
|
|
2014-12-13 00:07:02 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDialogHmsSet(int reference, int hours, int minutes, int seconds) {
|
|
|
|
int timeoutMinutes = Math.max((int)TimeUnit.HOURS.toMinutes(hours) +
|
|
|
|
minutes +
|
|
|
|
(int)TimeUnit.SECONDS.toMinutes(seconds), 1);
|
|
|
|
|
|
|
|
TextSecurePreferences.setPassphraseTimeoutInterval(getActivity(), timeoutMinutes);
|
|
|
|
initializeTimeoutSummary();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-04 14:19:48 +00:00
|
|
|
private class DisablePassphraseClickListener implements Preference.OnPreferenceChangeListener {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onPreferenceChange(final Preference preference, Object newValue) {
|
|
|
|
if (((CheckBoxPreference)preference).isChecked()) {
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
|
|
|
|
builder.setTitle(R.string.ApplicationPreferencesActivity_disable_storage_encryption);
|
|
|
|
builder.setMessage(R.string.ApplicationPreferencesActivity_warning_this_will_disable_storage_encryption_for_all_messages);
|
2015-01-30 06:37:01 +00:00
|
|
|
builder.setIcon(ResUtil.getDrawable(getActivity(), R.attr.dialog_alert_icon));
|
2014-11-04 14:19:48 +00:00
|
|
|
builder.setPositiveButton(R.string.ApplicationPreferencesActivity_disable, new DialogInterface.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
MasterSecret masterSecret = getActivity().getIntent().getParcelableExtra("master_secret");
|
|
|
|
MasterSecretUtil.changeMasterSecretPassphrase(getActivity(),
|
|
|
|
masterSecret,
|
|
|
|
MasterSecretUtil.UNENCRYPTED_PASSPHRASE);
|
|
|
|
|
|
|
|
|
|
|
|
TextSecurePreferences.setPasswordDisabled(getActivity(), true);
|
|
|
|
((CheckBoxPreference)preference).setChecked(false);
|
|
|
|
|
|
|
|
Intent intent = new Intent(getActivity(), KeyCachingService.class);
|
|
|
|
intent.setAction(KeyCachingService.DISABLE_ACTION);
|
|
|
|
getActivity().startService(intent);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
builder.setNegativeButton(android.R.string.cancel, null);
|
|
|
|
builder.show();
|
|
|
|
} else {
|
|
|
|
Intent intent = new Intent(getActivity(), PassphraseChangeActivity.class);
|
|
|
|
startActivity(intent);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-03 02:04:54 +00:00
|
|
|
private static CharSequence getPassphraseSummary(Context context) {
|
|
|
|
final int passphraseResId = R.string.preferences__passphrase_summary;
|
|
|
|
final String onRes = context.getString(R.string.ApplicationPreferencesActivity_on);
|
|
|
|
final String offRes = context.getString(R.string.ApplicationPreferencesActivity_off);
|
|
|
|
|
|
|
|
if (TextSecurePreferences.isPasswordDisabled(context)) {
|
|
|
|
return context.getString(passphraseResId, offRes);
|
|
|
|
} else {
|
|
|
|
return context.getString(passphraseResId, onRes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static CharSequence getScreenSecuritySummary(Context context) {
|
|
|
|
final int screenSecurityResId = R.string.preferences__screen_security_summary;
|
|
|
|
final String onRes = context.getString(R.string.ApplicationPreferencesActivity_on);
|
|
|
|
final String offRes = context.getString(R.string.ApplicationPreferencesActivity_off);
|
2014-11-04 14:19:48 +00:00
|
|
|
|
2015-03-03 02:04:54 +00:00
|
|
|
if (TextSecurePreferences.isScreenSecurityEnabled(context)) {
|
|
|
|
return context.getString(screenSecurityResId, onRes);
|
|
|
|
} else {
|
|
|
|
return context.getString(screenSecurityResId, offRes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static CharSequence getSummary(Context context) {
|
|
|
|
return getPassphraseSummary(context) + ", " + getScreenSecuritySummary(context);
|
2014-11-04 14:19:48 +00:00
|
|
|
}
|
|
|
|
}
|