From ae15b5dd0d7d57746c06a7a8c15e5d9dd14c858e Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Tue, 5 Jan 2016 01:20:28 +0100 Subject: inline-ttl: remove "default" setting --- .../keychain/ui/SettingsCacheTTLFragment.java | 63 +--------------------- .../keychain/ui/widget/CacheTTLSpinner.java | 11 +--- .../keychain/util/Preferences.java | 11 ++-- 3 files changed, 6 insertions(+), 79 deletions(-) (limited to 'OpenKeychain/src/main/java') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsCacheTTLFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsCacheTTLFragment.java index 5faed81ec..1c769e7a2 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsCacheTTLFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsCacheTTLFragment.java @@ -30,7 +30,6 @@ 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; @@ -93,31 +92,23 @@ public class SettingsCacheTTLFragment extends Fragment { 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; - } } } public CacheTTLPrefs getPrefs() { ArrayList ttls = new ArrayList<>(); - int defaultTtl = 0; for (int i = 0; i < mPositionIsChecked.size(); i++) { if (mPositionIsChecked.get(i)) { ttls.add(Integer.toString(CacheTTLPrefs.CACHE_TTLS.get(i))); - if (i == mDefaultPosition) { - defaultTtl = CacheTTLPrefs.CACHE_TTLS.get(i); - } } } - return new CacheTTLPrefs(ttls, defaultTtl); + return new CacheTTLPrefs(ttls); } @Override @@ -141,13 +132,11 @@ public class SettingsCacheTTLFragment extends Fragment { 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 @@ -161,17 +150,12 @@ public class SettingsCacheTTLFragment extends Fragment { int ttl = CacheTTLPrefs.CACHE_TTLS.get(position); boolean isChecked = mPositionIsChecked.get(position); - boolean isDefault = position == mDefaultPosition; mTitle.setText(CacheTTLPrefs.CACHE_TTL_NAMES.get(ttl)); // avoid some ui flicker by skipping unnecessary updates if (mChecked.isChecked() != isChecked) { mChecked.setChecked(isChecked); } - if (mIsDefault.isChecked() != isDefault) { - mIsDefault.setChecked(isDefault); - } - mIsDefault.setEnabled(isChecked); mChecked.setOnClickListener(new OnClickListener() { @Override @@ -181,14 +165,6 @@ public class SettingsCacheTTLFragment extends Fragment { } }); - mIsDefault.setOnClickListener(!isChecked ? null : new OnClickListener() { - @Override - public void onClick(View v) { - setTtlDefault(position); - savePreference(); - } - }); - } private void setTtlChecked(int position) { @@ -203,47 +179,10 @@ public class SettingsCacheTTLFragment extends Fragment { 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)) { - setTtlDefault(i); - return; - } - } - - // if that didn't work, move it down - i = mDefaultPosition; - while (++i < mPositionIsChecked.size()) { - if (mPositionIsChecked.get(i)) { - setTtlDefault(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 setTtlDefault(int position) { - int previousDefaultPosition = mDefaultPosition; - mDefaultPosition = position; - notifyItemChanged(previousDefaultPosition); - notifyItemChanged(mDefaultPosition); - } - private int countCheckedItems() { int result = 0; for (boolean isChecked : mPositionIsChecked) { 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 07149ec93..efaabbad2 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 @@ -46,23 +46,16 @@ public class CacheTTLSpinner extends AppCompatSpinner { CacheTTLPrefs prefs = Preferences.getPreferences(context).getPassphraseCacheTtl(); MatrixCursor cursor = new MatrixCursor(new String[] { "_id", "TTL", "description" }, 5); - int i = 0, defaultPosition = 0; + int i = 0; for (int ttl : CacheTTLPrefs.CACHE_TTLS) { if ( ! prefs.ttlTimes.contains(ttl)) { continue; } - if (ttl == prefs.defaultTtl) { - defaultPosition = i; - } cursor.addRow(new Object[] { i++, ttl, getContext().getString(CacheTTLPrefs.CACHE_TTL_NAMES.get(ttl)) }); } setAdapter(new SimpleCursorAdapter(getContext(), R.layout.simple_item, cursor, - new String[] { "description" }, - new int[] { R.id.simple_item_text }, - 0)); - - setSelection(defaultPosition); + new String[] { "description" }, new int[] { R.id.simple_item_text }, 0)); } public int getSelectedTimeToLive() { 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 6e8c8c651..75a7dcaba 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Preferences.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Preferences.java @@ -307,14 +307,12 @@ public class Preferences { if (pref == null) { return CacheTTLPrefs.getDefault(); } - int def = mSharedPreferences.getInt(Pref.PASSPHRASE_CACHE_DEFAULT, 300); - return new CacheTTLPrefs(pref, def); + return new CacheTTLPrefs(pref); } public void setPassphraseCacheTtl(CacheTTLPrefs prefs) { SharedPreferences.Editor editor = mSharedPreferences.edit(); editor.putStringSet(Constants.Pref.PASSPHRASE_CACHE_TTLS, prefs.getStringSet()); - editor.putInt(Pref.PASSPHRASE_CACHE_DEFAULT, prefs.defaultTtl); editor.commit(); } @@ -336,10 +334,8 @@ public class Preferences { } public HashSet ttlTimes; - public int defaultTtl; - public CacheTTLPrefs(Collection ttlStrings, int defaultTtl) { - this.defaultTtl = defaultTtl; + public CacheTTLPrefs(Collection ttlStrings) { ttlTimes = new HashSet<>(); for (String ttlString : ttlStrings) { ttlTimes.add(Integer.parseInt(ttlString)); @@ -356,11 +352,10 @@ public class Preferences { public static CacheTTLPrefs getDefault() { ArrayList ttlStrings = new ArrayList<>(); - ttlStrings.add(Integer.toString(0)); ttlStrings.add(Integer.toString(60 * 5)); ttlStrings.add(Integer.toString(60 * 60)); ttlStrings.add(Integer.toString(60 * 60 * 24)); - return new CacheTTLPrefs(ttlStrings, 0); + return new CacheTTLPrefs(ttlStrings); } } -- cgit v1.2.3