diff options
Diffstat (limited to 'OpenKeychain/src/main/java')
7 files changed, 409 insertions, 40 deletions
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() { |