inline-ttl: remove "default" setting

This commit is contained in:
Vincent Breitmoser
2016-01-05 01:20:28 +01:00
parent f2ef65ac37
commit ae15b5dd0d
6 changed files with 7 additions and 126 deletions

View File

@@ -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<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;
}
}
}
public CacheTTLPrefs getPrefs() {
ArrayList<String> 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) {

View File

@@ -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() {

View File

@@ -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<Integer> ttlTimes;
public int defaultTtl;
public CacheTTLPrefs(Collection<String> ttlStrings, int defaultTtl) {
this.defaultTtl = defaultTtl;
public CacheTTLPrefs(Collection<String> 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<String> 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);
}
}