|
| 1 | +package org.wordpress.passcodelock; |
| 2 | + |
| 3 | +import android.content.Intent; |
| 4 | +import android.os.Bundle; |
| 5 | +import android.support.v7.preference.Preference; |
| 6 | +import android.support.v7.preference.PreferenceFragmentCompat; |
| 7 | + |
| 8 | +public class PasscodePreferenceFragmentCompat extends PreferenceFragmentCompat |
| 9 | + implements Preference.OnPreferenceClickListener { |
| 10 | + public static final String KEY_SHOULD_INFLATE = "should-inflate"; |
| 11 | + public static final int ENABLE_PASSLOCK = 0; |
| 12 | + public static final int DISABLE_PASSLOCK = 1; |
| 13 | + public static final int CHANGE_PASSWORD = 2; |
| 14 | + |
| 15 | + private Preference mTogglePasscodePreference; |
| 16 | + private Preference mChangePasscodePreference; |
| 17 | + |
| 18 | + @Override |
| 19 | + public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { |
| 20 | + Bundle args = getArguments(); |
| 21 | + |
| 22 | + if (args != null && args.getBoolean(KEY_SHOULD_INFLATE, true)) { |
| 23 | + addPreferencesFromResource(R.xml.passcode_preferences); |
| 24 | + mTogglePasscodePreference = findPreference(getString(R.string.pref_key_passcode_toggle)); |
| 25 | + mChangePasscodePreference = findPreference(getString(R.string.pref_key_change_passcode)); |
| 26 | + } |
| 27 | + |
| 28 | + refreshPreferenceState(); |
| 29 | + } |
| 30 | + |
| 31 | + @Override |
| 32 | + public boolean onPreferenceClick(Preference preference) { |
| 33 | + String preferenceKey = preference.getKey() != null ? preference.getKey() : ""; |
| 34 | + |
| 35 | + if (preferenceKey.equals(getString(R.string.pref_key_passcode_toggle))) { |
| 36 | + return handlePasscodeToggleClick(); |
| 37 | + } else if (preferenceKey.equals(getString(R.string.pref_key_change_passcode))) { |
| 38 | + return handleChangePasscodeClick(); |
| 39 | + } |
| 40 | + |
| 41 | + return false; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * When the preferences are nested in a parent apps xml layout the inflated preferences will |
| 46 | + * need to be set. |
| 47 | + */ |
| 48 | + public void setPreferences(Preference togglePreference, Preference changePreference) { |
| 49 | + mTogglePasscodePreference = togglePreference; |
| 50 | + mChangePasscodePreference = changePreference; |
| 51 | + |
| 52 | + refreshPreferenceState(); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Called when user requests to turn the passlock on or off. |
| 57 | + * |
| 58 | + * @return |
| 59 | + * always true to indicate that the request was handled |
| 60 | + */ |
| 61 | + private boolean handlePasscodeToggleClick() { |
| 62 | + int type = AppLockManager.getInstance().getAppLock().isPasswordLocked() |
| 63 | + ? DISABLE_PASSLOCK : ENABLE_PASSLOCK; |
| 64 | + Intent i = new Intent(getActivity(), PasscodeManagePasswordActivity.class); |
| 65 | + i.putExtra(PasscodeManagePasswordActivity.KEY_TYPE, type); |
| 66 | + startActivityForResult(i, type); |
| 67 | + |
| 68 | + return true; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Called when user requests to change passcode. |
| 73 | + * |
| 74 | + * @return |
| 75 | + * always true to indicate that the request was handled |
| 76 | + */ |
| 77 | + private boolean handleChangePasscodeClick() { |
| 78 | + Intent i = new Intent(getActivity(), PasscodeManagePasswordActivity.class); |
| 79 | + i.putExtra(PasscodeManagePasswordActivity.KEY_TYPE, CHANGE_PASSWORD); |
| 80 | + i.putExtra(AbstractPasscodeKeyboardActivity.KEY_MESSAGE, |
| 81 | + getString(R.string.passcode_enter_old_passcode)); |
| 82 | + startActivityForResult(i, CHANGE_PASSWORD); |
| 83 | + |
| 84 | + return true; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Helper method to setup preference properties |
| 89 | + */ |
| 90 | + private void refreshPreferenceState() { |
| 91 | + if (mTogglePasscodePreference != null && mChangePasscodePreference != null) { |
| 92 | + mTogglePasscodePreference.setOnPreferenceClickListener(this); |
| 93 | + mChangePasscodePreference.setOnPreferenceClickListener(this); |
| 94 | + |
| 95 | + if (AppLockManager.getInstance().getAppLock().isPasswordLocked()) { |
| 96 | + mTogglePasscodePreference.setTitle(R.string.passcode_turn_off); |
| 97 | + mChangePasscodePreference.setEnabled(true); |
| 98 | + } else { |
| 99 | + mTogglePasscodePreference.setTitle(R.string.passcode_turn_on); |
| 100 | + mChangePasscodePreference.setEnabled(false); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments