aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVincent Breitmoser <valodim@mugenguild.com>2015-11-17 00:58:22 +0100
committerVincent Breitmoser <valodim@mugenguild.com>2016-01-06 16:24:12 +0100
commit1754a88ac39e061dd36afd72f8a04e8d5e59751c (patch)
tree7a1172068f988bfe8f81bc3a8cff4cf5d9a31ac6
parent31cc083163c634ac1f11d7aa22c679cda24ce892 (diff)
downloadopen-keychain-1754a88ac39e061dd36afd72f8a04e8d5e59751c.tar.gz
open-keychain-1754a88ac39e061dd36afd72f8a04e8d5e59751c.tar.bz2
open-keychain-1754a88ac39e061dd36afd72f8a04e8d5e59751c.zip
inline-ttl: create ttl choice customization setting activity
-rw-r--r--OpenKeychain/src/main/AndroidManifest.xml5
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/Constants.java3
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java3
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java43
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsCacheTTLActivity.java82
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsCacheTTLFragment.java233
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/CacheTTLSpinner.java10
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Preferences.java75
-rw-r--r--OpenKeychain/src/main/res/layout/settings_cache_ttl.xml71
-rw-r--r--OpenKeychain/src/main/res/layout/settings_cache_ttl_fragment.xml7
-rw-r--r--OpenKeychain/src/main/res/layout/settings_cache_ttl_item.xml36
-rw-r--r--OpenKeychain/src/main/res/values/arrays.xml28
-rw-r--r--OpenKeychain/src/main/res/values/strings.xml12
-rw-r--r--OpenKeychain/src/main/res/xml/passphrase_preferences.xml6
14 files changed, 541 insertions, 73 deletions
diff --git a/OpenKeychain/src/main/AndroidManifest.xml b/OpenKeychain/src/main/AndroidManifest.xml
index b9c8f8a2b..945d7164c 100644
--- a/OpenKeychain/src/main/AndroidManifest.xml
+++ b/OpenKeychain/src/main/AndroidManifest.xml
@@ -467,6 +467,11 @@
android:label="@string/title_key_server_preference"
android:windowSoftInputMode="stateHidden" />
<activity
+ android:name=".ui.SettingsCacheTTLActivity"
+ android:configChanges="orientation|screenSize|keyboardHidden|keyboard"
+ android:label="@string/title_cache_ttl_preference"
+ android:windowSoftInputMode="stateHidden" />
+ <activity
android:name=".ui.BackupActivity"
android:configChanges="keyboardHidden|keyboard"
android:label="@string/title_backup">
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/Constants.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/Constants.java
index 2de1cb38b..7bac8aa97 100644
--- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/Constants.java
+++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/Constants.java
@@ -95,7 +95,8 @@ public final class Constants {
}
public static final class Pref {
- public static final String PASSPHRASE_CACHE_TTL = "passphraseCacheTtl";
+ public static final String PASSPHRASE_CACHE_TTLS = "passphraseCacheTtls";
+ public static final String PASSPHRASE_CACHE_DEFAULT = "passphraseCacheDefault";
public static final String PASSPHRASE_CACHE_SUBS = "passphraseCacheSubs";
public static final String LANGUAGE = "language";
public static final String KEY_SERVERS = "keyServers";
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java
index d4f4998a5..6479c01ad 100644
--- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java
+++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java
@@ -238,8 +238,7 @@ public class PassphraseCacheService extends Service {
return null;
}
addCachedPassphrase(this, Constants.key.symmetric, Constants.key.symmetric,
- cachedPassphrase.getPassphrase(), getString(R.string.passp_cache_notif_pwd),
- Preferences.getPreferences(getBaseContext()).getPassphraseCacheTtl());
+ cachedPassphrase.getPassphrase(), getString(R.string.passp_cache_notif_pwd), 180);
return cachedPassphrase.getPassphrase();
}
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java
index cd754d60e..7666a230a 100644
--- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java
+++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java
@@ -18,6 +18,9 @@
package org.sufficientlysecure.keychain.ui;
+
+import java.util.List;
+
import android.Manifest;
import android.accounts.Account;
import android.accounts.AccountManager;
@@ -29,6 +32,7 @@ import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
+import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
@@ -49,13 +53,10 @@ import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.compatibility.AppCompatPreferenceActivity;
import org.sufficientlysecure.keychain.ui.util.Notify;
import org.sufficientlysecure.keychain.ui.util.ThemeChanger;
-import org.sufficientlysecure.keychain.ui.widget.IntegerListPreference;
import org.sufficientlysecure.keychain.util.Log;
import org.sufficientlysecure.keychain.util.Preferences;
import org.sufficientlysecure.keychain.util.orbot.OrbotHelper;
-import java.util.List;
-
public class SettingsActivity extends AppCompatPreferenceActivity {
public static final int REQUEST_CODE_KEYSERVER_PREF = 0x00007005;
@@ -103,6 +104,7 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
Toolbar toolbar = (Toolbar) toolbarContainer.findViewById(R.id.toolbar);
toolbar.setTitle(R.string.title_preferences);
+ // noinspection deprecation, TODO use alternative in API level 21
toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_arrow_back_white_24dp));
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
@@ -195,23 +197,19 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.passphrase_preferences);
- initializePassphraseCacheTtl(
- (IntegerListPreference) findPreference(Constants.Pref.PASSPHRASE_CACHE_TTL));
- }
-
- private static void initializePassphraseCacheTtl(
- final IntegerListPreference passphraseCacheTtl) {
- passphraseCacheTtl.setValue("" + sPreferences.getPassphraseCacheTtl());
- passphraseCacheTtl.setSummary(passphraseCacheTtl.getEntry());
- passphraseCacheTtl
- .setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
- public boolean onPreferenceChange(Preference preference, Object newValue) {
- passphraseCacheTtl.setValue(newValue.toString());
- passphraseCacheTtl.setSummary(passphraseCacheTtl.getEntry());
- sPreferences.setPassphraseCacheTtl(Integer.parseInt(newValue.toString()));
+ findPreference(Constants.Pref.PASSPHRASE_CACHE_TTLS)
+ .setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
+ public boolean onPreferenceClick(Preference preference) {
+ Intent intent = new Intent(getActivity(), SettingsCacheTTLActivity.class);
+ intent.putExtra(SettingsCacheTTLActivity.EXTRA_TTL_PREF,
+ sPreferences.getPassphraseCacheTtl());
+ startActivity(intent);
return false;
}
});
+
+ initializePassphraseCacheSubs(
+ (CheckBoxPreference) findPreference(Constants.Pref.PASSPHRASE_CACHE_SUBS));
}
}
@@ -580,4 +578,15 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
|| ExperimentalPrefsFragment.class.getName().equals(fragmentName)
|| super.isValidFragment(fragmentName);
}
+
+ private static void initializePassphraseCacheSubs(final CheckBoxPreference mPassphraseCacheSubs) {
+ mPassphraseCacheSubs.setChecked(sPreferences.getPassphraseCacheSubs());
+ mPassphraseCacheSubs.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
+ public boolean onPreferenceChange(Preference preference, Object newValue) {
+ mPassphraseCacheSubs.setChecked((Boolean) newValue);
+ sPreferences.setPassphraseCacheSubs((Boolean) newValue);
+ return false;
+ }
+ });
+ }
}
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsCacheTTLActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsCacheTTLActivity.java
new file mode 100644
index 000000000..6c3d0fd1c
--- /dev/null
+++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsCacheTTLActivity.java
@@ -0,0 +1,82 @@
+/*
+ * Copyright (C) 2015 Vincent Breitmoser <v.breitmoser@my.amazin.horse>
+ *
+ * This program 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.
+ *
+ * This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package org.sufficientlysecure.keychain.ui;
+
+import java.util.ArrayList;
+
+import android.content.Intent;
+import android.os.Bundle;
+import android.view.MenuItem;
+import android.view.View;
+import android.view.View.OnClickListener;
+
+import org.sufficientlysecure.keychain.R;
+import org.sufficientlysecure.keychain.ui.base.BaseActivity;
+import org.sufficientlysecure.keychain.util.Preferences.CacheTTLPrefs;
+
+
+public class SettingsCacheTTLActivity extends BaseActivity {
+
+ public static final String EXTRA_TTL_PREF = "ttl_pref";
+
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ Intent intent = getIntent();
+ CacheTTLPrefs ttlPrefs = (CacheTTLPrefs) intent.getSerializableExtra(EXTRA_TTL_PREF);
+ loadFragment(savedInstanceState, ttlPrefs);
+
+ getSupportActionBar().setDisplayHomeAsUpEnabled(true);
+
+ }
+
+ @Override
+ public boolean onOptionsItemSelected(MenuItem item) {
+ switch (item.getItemId()) {
+ case android.R.id.home:
+ finish();
+ return true;
+ }
+ return super.onOptionsItemSelected(item);
+ }
+
+ @Override
+ protected void initLayout() {
+ setContentView(R.layout.settings_cache_ttl);
+ }
+
+ private void loadFragment(Bundle savedInstanceState, CacheTTLPrefs ttlPrefs) {
+ // However, if we're being restored from a previous state,
+ // then we don't need to do anything and should return or else
+ // we could end up with overlapping fragments.
+ if (savedInstanceState != null) {
+ return;
+ }
+
+ SettingsCacheTTLFragment fragment = SettingsCacheTTLFragment.newInstance(ttlPrefs);
+
+ // Add the fragment to the 'fragment_container' FrameLayout
+ // NOTE: We use commitAllowingStateLoss() to prevent weird crashes!
+ getSupportFragmentManager().beginTransaction()
+ .replace(R.id.settings_cache_ttl_fragment, fragment)
+ .commitAllowingStateLoss();
+ // do it immediately!
+ getSupportFragmentManager().executePendingTransactions();
+ }
+}
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsCacheTTLFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsCacheTTLFragment.java
new file mode 100644
index 000000000..b383082ed
--- /dev/null
+++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsCacheTTLFragment.java
@@ -0,0 +1,233 @@
+/*
+ * Copyright (C) 2015 Vincent Breitmoser <v.breitmoser@my.amazin.horse>
+ *
+ * This program 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.
+ *
+ * This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package org.sufficientlysecure.keychain.ui;
+
+
+import java.util.ArrayList;
+import java.util.Collections;
+
+import android.os.Bundle;
+import android.support.v4.app.Fragment;
+import android.support.v7.widget.LinearLayoutManager;
+import android.support.v7.widget.RecyclerView;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.View.OnClickListener;
+import android.view.ViewGroup;
+import android.widget.CheckBox;
+import android.widget.RadioButton;
+import android.widget.TextView;
+
+import org.sufficientlysecure.keychain.R;
+import org.sufficientlysecure.keychain.ui.util.Notify;
+import org.sufficientlysecure.keychain.ui.util.Notify.Style;
+import org.sufficientlysecure.keychain.ui.util.recyclerview.DividerItemDecoration;
+import org.sufficientlysecure.keychain.util.Preferences.CacheTTLPrefs;
+
+
+public class SettingsCacheTTLFragment extends Fragment {
+
+ public static final String ARG_TTL_PREFS = "ttl_prefs";
+
+ private CacheTTLListAdapter mAdapter;
+
+ public static SettingsCacheTTLFragment newInstance(CacheTTLPrefs ttlPrefs) {
+ Bundle args = new Bundle();
+ args.putSerializable(ARG_TTL_PREFS, ttlPrefs);
+
+ SettingsCacheTTLFragment fragment = new SettingsCacheTTLFragment();
+ fragment.setArguments(args);
+
+ return fragment;
+ }
+
+ @Override
+ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
+ savedInstanceState) {
+
+ return inflater.inflate(R.layout.settings_cache_ttl_fragment, null);
+ }
+
+ @Override
+ public void onViewCreated(View view, Bundle savedInstanceState) {
+ super.onViewCreated(view, savedInstanceState);
+
+ CacheTTLPrefs prefs = (CacheTTLPrefs) getArguments().getSerializable(ARG_TTL_PREFS);
+
+ mAdapter = new CacheTTLListAdapter(prefs);
+
+ RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.cache_ttl_recycler_view);
+ recyclerView.setHasFixedSize(true);
+ recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
+ recyclerView.setAdapter(mAdapter);
+ recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
+
+
+ }
+
+ private void saveKeyserverList() {
+ // Preferences.getPreferences(getActivity()).setKeyServers(servers);
+ }
+
+ public class CacheTTLListAdapter extends RecyclerView.Adapter<CacheTTLListAdapter.ViewHolder> {
+
+ private final ArrayList<Boolean> mPositionIsChecked;
+ private int mDefaultPosition;
+
+ public CacheTTLListAdapter(CacheTTLPrefs prefs) {
+ this.mPositionIsChecked = new ArrayList<>();
+ for (int ttlTime : CacheTTLPrefs.CACHE_TTLS) {
+ mPositionIsChecked.add(prefs.ttlTimes.contains(ttlTime));
+ if (ttlTime == prefs.defaultTtl) {
+ mDefaultPosition = mPositionIsChecked.size() -1;
+ }
+ }
+
+ }
+
+ @Override
+ public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
+ View view = LayoutInflater.from(parent.getContext())
+ .inflate(R.layout.settings_cache_ttl_item, parent, false);
+ return new ViewHolder(view);
+ }
+
+ @Override
+ public void onBindViewHolder(final ViewHolder holder, int position) {
+ holder.bind(position);
+ }
+
+ @Override
+ public int getItemCount() {
+ return mPositionIsChecked.size();
+ }
+
+ public class ViewHolder extends RecyclerView.ViewHolder {
+
+ CheckBox mChecked;
+ TextView mTitle;
+ RadioButton mIsDefault;
+
+ public ViewHolder(View itemView) {
+ super(itemView);
+ mChecked = (CheckBox) itemView.findViewById(R.id.ttl_selected);
+ mTitle = (TextView) itemView.findViewById(R.id.ttl_title);
+ mIsDefault = (RadioButton) itemView.findViewById(R.id.ttl_default);
+
+ itemView.setOnClickListener(new OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ mChecked.performClick();
+ }
+ });
+ }
+
+ public void bind(final int position) {
+
+ int ttl = CacheTTLPrefs.CACHE_TTLS.get(position);
+ boolean isChecked = mPositionIsChecked.get(position);
+ boolean isDefault = position == mDefaultPosition;
+
+ mTitle.setText(CacheTTLPrefs.CACHE_TTL_NAMES.get(ttl));
+ mChecked.setChecked(isChecked);
+ mIsDefault.setEnabled(isChecked);
+ mIsDefault.setChecked(isDefault);
+
+ mChecked.setOnClickListener(new OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ setTtlChecked(position);
+ }
+ });
+
+ mIsDefault.setOnClickListener(!isChecked ? null : new OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ setDefault(position);
+ }
+ });
+
+ }
+
+ private void setTtlChecked(int position) {
+ boolean isChecked = mPositionIsChecked.get(position);
+ int checkedItems = countCheckedItems();
+
+ boolean isLastChecked = isChecked && checkedItems == 1;
+ boolean isOneTooMany = !isChecked && checkedItems >= 3;
+ if (isLastChecked) {
+ Notify.create(getActivity(), R.string.settings_cache_ttl_at_least_one, Style.ERROR).show();
+ } else if (isOneTooMany) {
+ Notify.create(getActivity(), R.string.settings_cache_ttl_max_three, Style.ERROR).show();
+ } else {
+ mPositionIsChecked.set(position, !isChecked);
+ repositionDefault();
+ }
+ notifyItemChanged(position);
+ }
+
+ private void repositionDefault() {
+ boolean defaultPositionIsChecked = mPositionIsChecked.get(mDefaultPosition);
+ if (defaultPositionIsChecked) {
+ return;
+ }
+
+ // prefer moving default up
+ int i = mDefaultPosition;
+ while (--i >= 0) {
+ if (mPositionIsChecked.get(i)) {
+ setDefault(i);
+ return;
+ }
+ }
+
+ // if that didn't work, move it down
+ i = mDefaultPosition;
+ while (++i < mPositionIsChecked.size()) {
+ if (mPositionIsChecked.get(i)) {
+ setDefault(i);
+ return;
+ }
+ }
+
+ // we should never get here - if we do, leave default as is (there is a sanity check in the
+ // set preference method, so no biggie)
+
+ }
+
+ private void setDefault(int position) {
+ int previousDefaultPosition = mDefaultPosition;
+ mDefaultPosition = position;
+ notifyItemChanged(previousDefaultPosition);
+ notifyItemChanged(mDefaultPosition);
+ }
+
+ private int countCheckedItems() {
+ int result = 0;
+ for (boolean isChecked : mPositionIsChecked) {
+ if (isChecked) {
+ result += 1;
+ }
+ }
+ return result;
+ }
+
+ }
+
+ }
+}
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/CacheTTLSpinner.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/CacheTTLSpinner.java
index 11f6fafcd..78d8e6c82 100644
--- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/CacheTTLSpinner.java
+++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/CacheTTLSpinner.java
@@ -58,11 +58,11 @@ public class CacheTTLSpinner extends AppCompatSpinner {
private void initView() {
MatrixCursor cursor = new MatrixCursor(new String[] { "_id", "TTL", "description" }, 5);
- cursor.addRow(new Object[] { 0, 60*5, "Five Minutes" });
- cursor.addRow(new Object[] { 1, 60*60, "One Hour" });
- cursor.addRow(new Object[] { 2, 60*60*3, "Three Hours" });
- cursor.addRow(new Object[] { 3, 60*60*24, "One Day" });
- cursor.addRow(new Object[] { 4, 60*60*24*3, "Three Days" });
+ cursor.addRow(new Object[] { 0, 60*5, getContext().getString(R.string.cache_ttl_five_minutes) });
+ cursor.addRow(new Object[] { 1, 60*60, getContext().getString(R.string.cache_ttl_one_hour) });
+ cursor.addRow(new Object[] { 2, 60*60*3, getContext().getString(R.string.cache_ttl_three_hours) });
+ cursor.addRow(new Object[] { 3, 60*60*24, getContext().getString(R.string.cache_ttl_one_day) });
+ cursor.addRow(new Object[] { 4, 60*60*24*3, getContext().getString(R.string.cache_ttl_three_days) });
setAdapter(new SimpleCursorAdapter(getContext(), R.layout.simple_item, cursor,
new String[] { "description" },
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Preferences.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Preferences.java
index ce81bbcac..2b3c3350a 100644
--- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Preferences.java
+++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Preferences.java
@@ -18,9 +18,22 @@
package org.sufficientlysecure.keychain.util;
+
+import java.io.Serializable;
+import java.net.Proxy;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.ListIterator;
+import java.util.Map;
+import java.util.Set;
+import java.util.Vector;
+
import android.content.Context;
import android.content.SharedPreferences;
-
import android.os.Parcel;
import android.os.Parcelable;
import android.preference.PreferenceManager;
@@ -28,14 +41,9 @@ import android.support.annotation.NonNull;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.Constants.Pref;
+import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.service.KeyserverSyncAdapterService;
-import java.net.Proxy;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.ListIterator;
-import java.util.Vector;
-
/**
* Singleton Implementation of a Preference Helper
*/
@@ -90,19 +98,18 @@ public class Preferences {
editor.commit();
}
- public long getPassphraseCacheTtl() {
- int ttl = mSharedPreferences.getInt(Constants.Pref.PASSPHRASE_CACHE_TTL, 180);
- // fix the value if it was set to "never" in previous versions, which currently is not
- // supported
- if (ttl == 0) {
- ttl = 180;
+ public CacheTTLPrefs getPassphraseCacheTtl() {
+ Set<String> pref = mSharedPreferences.getStringSet(Constants.Pref.PASSPHRASE_CACHE_TTLS, null);
+ if (pref == null) {
+ return CacheTTLPrefs.getDefault();
}
- return (long) ttl;
+ int def = mSharedPreferences.getInt(Pref.PASSPHRASE_CACHE_DEFAULT, 0);
+ return new CacheTTLPrefs(pref, def);
}
public void setPassphraseCacheTtl(int value) {
SharedPreferences.Editor editor = mSharedPreferences.edit();
- editor.putInt(Constants.Pref.PASSPHRASE_CACHE_TTL, value);
+ editor.putInt(Constants.Pref.PASSPHRASE_CACHE_TTLS, value);
editor.commit();
}
@@ -308,6 +315,44 @@ public class Preferences {
}
+ public static class CacheTTLPrefs implements Serializable {
+ public static final Map<Integer,Integer> CACHE_TTL_NAMES;
+ public static final ArrayList<Integer> CACHE_TTLS;
+ static {
+ HashMap<Integer,Integer> cacheTtlNames = new HashMap<>();
+ cacheTtlNames.put(60 * 5, R.string.cache_ttl_five_minutes);
+ cacheTtlNames.put(60 * 60, R.string.cache_ttl_one_hour);
+ cacheTtlNames.put(60 * 60 * 3, R.string.cache_ttl_three_hours);
+ cacheTtlNames.put(60 * 60 * 24, R.string.cache_ttl_one_day);
+ cacheTtlNames.put(60 * 60 * 24 * 3, R.string.cache_ttl_three_days);
+ CACHE_TTL_NAMES = Collections.unmodifiableMap(cacheTtlNames);
+
+ CACHE_TTLS = new ArrayList<>(CacheTTLPrefs.CACHE_TTL_NAMES.keySet());
+ Collections.sort(CACHE_TTLS);
+ }
+
+
+ public HashSet<Integer> ttlTimes;
+ public int defaultTtl;
+
+ public CacheTTLPrefs(Collection<String> ttlStrings, int defaultTtl) {
+ this.defaultTtl = defaultTtl;
+ ttlTimes = new HashSet<>();
+ for (String ttlString : ttlStrings) {
+ ttlTimes.add(Integer.parseInt(ttlString));
+ }
+ }
+
+ public static CacheTTLPrefs getDefault() {
+ ArrayList<String> ttlStrings = new ArrayList<>();
+ ttlStrings.add(Integer.toString(60 * 5));
+ ttlStrings.add(Integer.toString(60 * 60));
+ ttlStrings.add(Integer.toString(60 * 60 * 24));
+ return new CacheTTLPrefs(ttlStrings, 60 * 5);
+ }
+
+ }
+
// cloud prefs
public CloudSearchPrefs getCloudSearchPrefs() {
diff --git a/OpenKeychain/src/main/res/layout/settings_cache_ttl.xml b/OpenKeychain/src/main/res/layout/settings_cache_ttl.xml
new file mode 100644
index 000000000..2b0a3d7eb
--- /dev/null
+++ b/OpenKeychain/src/main/res/layout/settings_cache_ttl.xml
@@ -0,0 +1,71 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ android:orientation="vertical">
+
+ <include
+ android:id="@+id/toolbar_include"
+ layout="@layout/toolbar_standalone" />
+
+ <LinearLayout
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:orientation="horizontal">
+
+ <TextView
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_margin="8dp"
+ android:textAppearance="?android:attr/textAppearanceMedium"
+ android:text="@string/settings_cache_select_three"/>
+
+ <Space
+ android:layout_width="0dp"
+ android:layout_height="wrap_content"
+ android:layout_weight="1" />
+
+ <TextView
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_margin="8dp"
+ android:textAppearance="?android:attr/textAppearanceMedium"
+ android:text="@string/setting_cache_select_default"/>
+
+ </LinearLayout>
+
+ <LinearLayout
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:orientation="horizontal">
+
+ <ImageView
+ android:layout_width="wrap_content"
+ android:layout_height="match_parent"
+ android:layout_marginLeft="12dp"
+ android:src="@drawable/ic_expand_more_black_24dp"
+ />
+
+ <Space
+ android:layout_width="0dp"
+ android:layout_height="wrap_content"
+ android:layout_weight="1" />
+
+ <ImageView
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_marginRight="12dp"
+ android:src="@drawable/ic_expand_more_black_24dp"
+ />
+
+ </LinearLayout>
+
+ <View style="@style/Divider"/>
+
+ <FrameLayout
+ android:id="@+id/settings_cache_ttl_fragment"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ android:orientation="vertical" />
+
+</LinearLayout> \ No newline at end of file
diff --git a/OpenKeychain/src/main/res/layout/settings_cache_ttl_fragment.xml b/OpenKeychain/src/main/res/layout/settings_cache_ttl_fragment.xml
new file mode 100644
index 000000000..4a34bc5bc
--- /dev/null
+++ b/OpenKeychain/src/main/res/layout/settings_cache_ttl_fragment.xml
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<android.support.v7.widget.RecyclerView
+ xmlns:android="http://schemas.android.com/apk/res/android"
+ android:id="@+id/cache_ttl_recycler_view"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent" /> \ No newline at end of file
diff --git a/OpenKeychain/src/main/res/layout/settings_cache_ttl_item.xml b/OpenKeychain/src/main/res/layout/settings_cache_ttl_item.xml
new file mode 100644
index 000000000..039f07b24
--- /dev/null
+++ b/OpenKeychain/src/main/res/layout/settings_cache_ttl_item.xml
@@ -0,0 +1,36 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout
+ xmlns:android="http://schemas.android.com/apk/res/android"
+ xmlns:tools="http://schemas.android.com/tools"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:minHeight="?listPreferredItemHeight"
+ android:orientation="horizontal"
+ android:gravity="center_vertical"
+ android:background="?selectableItemBackground">
+
+ <CheckBox
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:id="@+id/ttl_selected"
+ android:layout_margin="8dp"
+ />
+
+ <TextView
+ android:layout_width="0dp"
+ android:layout_height="wrap_content"
+ android:id="@+id/ttl_title"
+ android:layout_weight="1"
+ android:textAppearance="?android:attr/textAppearanceMedium"
+ android:textStyle="bold"
+ tools:text="One Hour"
+ />
+
+ <RadioButton
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:id="@+id/ttl_default"
+ android:padding="4dp"
+ />
+
+</LinearLayout> \ No newline at end of file
diff --git a/OpenKeychain/src/main/res/values/arrays.xml b/OpenKeychain/src/main/res/values/arrays.xml
index 393a1e091..584d773b7 100644
--- a/OpenKeychain/src/main/res/values/arrays.xml
+++ b/OpenKeychain/src/main/res/values/arrays.xml
@@ -1,34 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
- <string-array name="passphrase_cache_ttl_entries" translatable="false">
- <item>@string/choice_15secs</item>
- <item>@string/choice_1min</item>
- <item>@string/choice_3mins</item>
- <item>@string/choice_5mins</item>
- <item>@string/choice_10mins</item>
- <item>@string/choice_20mins</item>
- <item>@string/choice_40mins</item>
- <item>@string/choice_1hour</item>
- <item>@string/choice_2hours</item>
- <item>@string/choice_4hours</item>
- <item>@string/choice_8hours</item>
- <item>@string/choice_forever</item>
- </string-array>
- <string-array name="passphrase_cache_ttl_values" translatable="false">
- <item>15</item>
- <item>60</item>
- <item>180</item>
- <item>300</item>
- <item>600</item>
- <item>1200</item>
- <item>2400</item>
- <item>3600</item>
- <item>7200</item>
- <item>14400</item>
- <item>28800</item>
- <item>-1</item>
- </string-array>
<string-array name="pref_proxy_type_entries" translatable="false">
<item>@string/pref_proxy_type_choice_http</item>
<item>@string/pref_proxy_type_choice_socks</item>
diff --git a/OpenKeychain/src/main/res/values/strings.xml b/OpenKeychain/src/main/res/values/strings.xml
index afda3c3c3..076ff0736 100644
--- a/OpenKeychain/src/main/res/values/strings.xml
+++ b/OpenKeychain/src/main/res/values/strings.xml
@@ -18,6 +18,7 @@
<string name="title_preferences">"Settings"</string>
<string name="title_api_registered_apps">"Apps"</string>
<string name="title_key_server_preference">"OpenPGP keyservers"</string>
+ <string name="title_cache_ttl_preference">"Customize 'Remember' Choices"</string>
<string name="title_change_passphrase">"Change Password"</string>
<string name="title_share_fingerprint_with">"Share fingerprint with…"</string>
<string name="title_share_key">"Share key with…"</string>
@@ -157,7 +158,7 @@
<string name="label_encryption_algorithm">"Encryption algorithm"</string>
<string name="label_hash_algorithm">"Hash algorithm"</string>
<string name="label_symmetric">"Encrypt with password"</string>
- <string name="label_passphrase_cache_ttl">"Remember time"</string>
+ <string name="label_passphrase_cache_ttl">"Customize 'Remember' Choices"</string>
<string name="label_passphrase_cache_subs">"Remember passwords by subkey"</string>
<string name="label_message_compression">"Text compression"</string>
<string name="label_file_compression">"File compression"</string>
@@ -1709,5 +1710,14 @@
<string name="title_edit_identities">"Edit Identities"</string>
<string name="title_edit_subkeys">"Edit Subkeys"</string>
<string name="btn_search_for_query">"Search for\n'%s'"</string>
+ <string name="cache_ttl_five_minutes">"Five Minutes"</string>
+ <string name="cache_ttl_one_hour">"One Hour"</string>
+ <string name="cache_ttl_three_hours">"Three Hours"</string>
+ <string name="cache_ttl_one_day">"One Day"</string>
+ <string name="cache_ttl_three_days">"Three Days"</string>
+ <string name="settings_cache_select_three">"Pick up to three"</string>
+ <string name="setting_cache_select_default">"and one default"</string>
+ <string name="settings_cache_ttl_at_least_one">"At least one item must be selected!"</string>
+ <string name="settings_cache_ttl_max_three">"Can\'t select more than three items!"</string>
</resources>
diff --git a/OpenKeychain/src/main/res/xml/passphrase_preferences.xml b/OpenKeychain/src/main/res/xml/passphrase_preferences.xml
index 75f293c43..25bdf3a3d 100644
--- a/OpenKeychain/src/main/res/xml/passphrase_preferences.xml
+++ b/OpenKeychain/src/main/res/xml/passphrase_preferences.xml
@@ -1,8 +1,6 @@
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
- <org.sufficientlysecure.keychain.ui.widget.IntegerListPreference
- android:entries="@array/passphrase_cache_ttl_entries"
- android:entryValues="@array/passphrase_cache_ttl_values"
- android:key="passphraseCacheTtl"
+ <PreferenceScreen
+ android:key="passphraseCacheTtls"
android:persistent="false"
android:title="@string/label_passphrase_cache_ttl" />
<CheckBoxPreference