From 1754a88ac39e061dd36afd72f8a04e8d5e59751c Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Tue, 17 Nov 2015 00:58:22 +0100 Subject: [PATCH] inline-ttl: create ttl choice customization setting activity --- OpenKeychain/src/main/AndroidManifest.xml | 5 + .../keychain/Constants.java | 3 +- .../service/PassphraseCacheService.java | 3 +- .../keychain/ui/SettingsActivity.java | 43 ++-- .../keychain/ui/SettingsCacheTTLActivity.java | 82 ++++++ .../keychain/ui/SettingsCacheTTLFragment.java | 233 ++++++++++++++++++ .../keychain/ui/widget/CacheTTLSpinner.java | 10 +- .../keychain/util/Preferences.java | 75 ++++-- .../main/res/layout/settings_cache_ttl.xml | 71 ++++++ .../layout/settings_cache_ttl_fragment.xml | 7 + .../res/layout/settings_cache_ttl_item.xml | 36 +++ OpenKeychain/src/main/res/values/arrays.xml | 28 --- OpenKeychain/src/main/res/values/strings.xml | 12 +- .../main/res/xml/passphrase_preferences.xml | 6 +- 14 files changed, 541 insertions(+), 73 deletions(-) create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsCacheTTLActivity.java create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsCacheTTLFragment.java create mode 100644 OpenKeychain/src/main/res/layout/settings_cache_ttl.xml create mode 100644 OpenKeychain/src/main/res/layout/settings_cache_ttl_fragment.xml create mode 100644 OpenKeychain/src/main/res/layout/settings_cache_ttl_item.xml 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 @@ -466,6 +466,11 @@ android:configChanges="orientation|screenSize|keyboardHidden|keyboard" android:label="@string/title_key_server_preference" android:windowSoftInputMode="stateHidden" /> + + * + * 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 . + */ + +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 + * + * 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 . + */ + +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 { + + private final ArrayList 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 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 CACHE_TTL_NAMES; + public static final ArrayList CACHE_TTLS; + static { + HashMap 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 ttlTimes; + public int defaultTtl; + + public CacheTTLPrefs(Collection ttlStrings, int defaultTtl) { + this.defaultTtl = defaultTtl; + ttlTimes = new HashSet<>(); + for (String ttlString : ttlStrings) { + ttlTimes.add(Integer.parseInt(ttlString)); + } + } + + public static CacheTTLPrefs getDefault() { + ArrayList 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 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ 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 @@ + + + \ 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 @@ + + + + + + + + + + \ 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 @@ - - @string/choice_15secs - @string/choice_1min - @string/choice_3mins - @string/choice_5mins - @string/choice_10mins - @string/choice_20mins - @string/choice_40mins - @string/choice_1hour - @string/choice_2hours - @string/choice_4hours - @string/choice_8hours - @string/choice_forever - - - 15 - 60 - 180 - 300 - 600 - 1200 - 2400 - 3600 - 7200 - 14400 - 28800 - -1 - @string/pref_proxy_type_choice_http @string/pref_proxy_type_choice_socks 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 @@ "Settings" "Apps" "OpenPGP keyservers" + "Customize 'Remember' Choices" "Change Password" "Share fingerprint with…" "Share key with…" @@ -157,7 +158,7 @@ "Encryption algorithm" "Hash algorithm" "Encrypt with password" - "Remember time" + "Customize 'Remember' Choices" "Remember passwords by subkey" "Text compression" "File compression" @@ -1709,5 +1710,14 @@ "Edit Identities" "Edit Subkeys" "Search for\n'%s'" + "Five Minutes" + "One Hour" + "Three Hours" + "One Day" + "Three Days" + "Pick up to three" + "and one default" + "At least one item must be selected!" + "Can\'t select more than three items!" 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 @@ -