Search in sources :

Example 1 with TwoStatePreference

use of android.support.v7.preference.TwoStatePreference in project Resurrection_packages_apps_Settings by ResurrectionRemix.

the class SoundSettings method initVibrateWhenRinging.

// === Vibrate when ringing ===
private void initVibrateWhenRinging() {
    mVibrateWhenRinging = (TwoStatePreference) getPreferenceScreen().findPreference(KEY_VIBRATE_WHEN_RINGING);
    if (mVibrateWhenRinging == null) {
        Log.i(TAG, "Preference not found: " + KEY_VIBRATE_WHEN_RINGING);
        return;
    }
    if (!mVoiceCapable) {
        getPreferenceScreen().removePreference(mVibrateWhenRinging);
        mVibrateWhenRinging = null;
        return;
    }
    mVibrateWhenRinging.setPersistent(false);
    updateVibrateWhenRinging();
    mVibrateWhenRinging.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {

        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            final boolean val = (Boolean) newValue;
            return Settings.System.putInt(getContentResolver(), Settings.System.VIBRATE_WHEN_RINGING, val ? 1 : 0);
        }
    });
}
Also used : RestrictedPreference(com.android.settingslib.RestrictedPreference) TwoStatePreference(android.support.v7.preference.TwoStatePreference) Preference(android.support.v7.preference.Preference) RingtonePreference(com.android.settings.RingtonePreference) OnPreferenceChangeListener(android.support.v7.preference.Preference.OnPreferenceChangeListener)

Example 2 with TwoStatePreference

use of android.support.v7.preference.TwoStatePreference in project Resurrection_packages_apps_Settings by ResurrectionRemix.

the class InputMethodAndSubtypeEnabler method addInputMethodSubtypePreferences.

private void addInputMethodSubtypePreferences(final InputMethodInfo imi, final PreferenceScreen root) {
    final Context context = getPrefContext();
    final int subtypeCount = imi.getSubtypeCount();
    if (subtypeCount <= 1) {
        return;
    }
    final String imiId = imi.getId();
    final PreferenceCategory keyboardSettingsCategory = new PreferenceCategory(getPrefContext());
    root.addPreference(keyboardSettingsCategory);
    final PackageManager pm = getPackageManager();
    final CharSequence label = imi.loadLabel(pm);
    keyboardSettingsCategory.setTitle(label);
    keyboardSettingsCategory.setKey(imiId);
    // TODO: Use toggle Preference if images are ready.
    final TwoStatePreference autoSelectionPref = new SwitchWithNoTextPreference(getPrefContext());
    mAutoSelectionPrefsMap.put(imiId, autoSelectionPref);
    keyboardSettingsCategory.addPreference(autoSelectionPref);
    autoSelectionPref.setOnPreferenceChangeListener(this);
    final PreferenceCategory activeInputMethodsCategory = new PreferenceCategory(getPrefContext());
    activeInputMethodsCategory.setTitle(R.string.active_input_method_subtypes);
    root.addPreference(activeInputMethodsCategory);
    CharSequence autoSubtypeLabel = null;
    final ArrayList<Preference> subtypePreferences = new ArrayList<>();
    for (int index = 0; index < subtypeCount; ++index) {
        final InputMethodSubtype subtype = imi.getSubtypeAt(index);
        if (subtype.overridesImplicitlyEnabledSubtype()) {
            if (autoSubtypeLabel == null) {
                autoSubtypeLabel = InputMethodAndSubtypeUtil.getSubtypeLocaleNameAsSentence(subtype, context, imi);
            }
        } else {
            final Preference subtypePref = new InputMethodSubtypePreference(context, subtype, imi);
            subtypePreferences.add(subtypePref);
        }
    }
    Collections.sort(subtypePreferences, new Comparator<Preference>() {

        @Override
        public int compare(final Preference lhs, final Preference rhs) {
            if (lhs instanceof InputMethodSubtypePreference) {
                return ((InputMethodSubtypePreference) lhs).compareTo(rhs, mCollator);
            }
            return lhs.compareTo(rhs);
        }
    });
    final int prefCount = subtypePreferences.size();
    for (int index = 0; index < prefCount; ++index) {
        final Preference pref = subtypePreferences.get(index);
        activeInputMethodsCategory.addPreference(pref);
        pref.setOnPreferenceChangeListener(this);
        InputMethodAndSubtypeUtil.removeUnnecessaryNonPersistentPreference(pref);
    }
    mInputMethodAndSubtypePrefsMap.put(imiId, subtypePreferences);
    if (TextUtils.isEmpty(autoSubtypeLabel)) {
        autoSelectionPref.setTitle(R.string.use_system_language_to_select_input_method_subtypes);
    } else {
        autoSelectionPref.setTitle(autoSubtypeLabel);
    }
}
Also used : Context(android.content.Context) InputMethodSubtype(android.view.inputmethod.InputMethodSubtype) TwoStatePreference(android.support.v7.preference.TwoStatePreference) ArrayList(java.util.ArrayList) PackageManager(android.content.pm.PackageManager) PreferenceCategory(android.support.v7.preference.PreferenceCategory) TwoStatePreference(android.support.v7.preference.TwoStatePreference) Preference(android.support.v7.preference.Preference)

Example 3 with TwoStatePreference

use of android.support.v7.preference.TwoStatePreference in project Resurrection_packages_apps_Settings by ResurrectionRemix.

the class InputMethodAndSubtypeEnabler method setAutoSelectionSubtypesEnabled.

private void setAutoSelectionSubtypesEnabled(final String imiId, final boolean autoSelectionEnabled) {
    final TwoStatePreference autoSelectionPref = mAutoSelectionPrefsMap.get(imiId);
    if (autoSelectionPref == null) {
        return;
    }
    autoSelectionPref.setChecked(autoSelectionEnabled);
    final List<Preference> subtypePrefs = mInputMethodAndSubtypePrefsMap.get(imiId);
    for (final Preference pref : subtypePrefs) {
        if (pref instanceof TwoStatePreference) {
            // When autoSelectionEnabled is true, all subtype prefs need to be disabled with
            // implicitly checked subtypes. In case of false, all subtype prefs need to be
            // enabled.
            pref.setEnabled(!autoSelectionEnabled);
            if (autoSelectionEnabled) {
                ((TwoStatePreference) pref).setChecked(false);
            }
        }
    }
    if (autoSelectionEnabled) {
        InputMethodAndSubtypeUtil.saveInputMethodSubtypeList(this, getContentResolver(), mInputMethodInfoList, mHaveHardKeyboard);
        updateImplicitlyEnabledSubtypes(imiId, true);
    }
}
Also used : TwoStatePreference(android.support.v7.preference.TwoStatePreference) TwoStatePreference(android.support.v7.preference.TwoStatePreference) Preference(android.support.v7.preference.Preference)

Example 4 with TwoStatePreference

use of android.support.v7.preference.TwoStatePreference in project Resurrection_packages_apps_Settings by ResurrectionRemix.

the class InputMethodAndSubtypeEnabler method onPreferenceChange.

@Override
public boolean onPreferenceChange(final Preference pref, final Object newValue) {
    if (!(newValue instanceof Boolean)) {
        // Invoke default behavior.
        return true;
    }
    final boolean isChecking = (Boolean) newValue;
    for (final String imiId : mAutoSelectionPrefsMap.keySet()) {
        // An auto select subtype preference is changing.
        if (mAutoSelectionPrefsMap.get(imiId) == pref) {
            final TwoStatePreference autoSelectionPref = (TwoStatePreference) pref;
            autoSelectionPref.setChecked(isChecking);
            // Enable or disable subtypes depending on the auto selection preference.
            setAutoSelectionSubtypesEnabled(imiId, autoSelectionPref.isChecked());
            return false;
        }
    }
    // A subtype preference is changing.
    if (pref instanceof InputMethodSubtypePreference) {
        final InputMethodSubtypePreference subtypePref = (InputMethodSubtypePreference) pref;
        subtypePref.setChecked(isChecking);
        if (!subtypePref.isChecked()) {
            // It takes care of the case where no subtypes are explicitly enabled then the auto
            // selection preference is going to be checked.
            updateAutoSelectionPreferences();
        }
        return false;
    }
    // Invoke default behavior.
    return true;
}
Also used : TwoStatePreference(android.support.v7.preference.TwoStatePreference)

Example 5 with TwoStatePreference

use of android.support.v7.preference.TwoStatePreference in project Resurrection_packages_apps_Settings by ResurrectionRemix.

the class InputMethodAndSubtypeUtil method saveInputMethodSubtypeList.

static void saveInputMethodSubtypeList(SettingsPreferenceFragment context, ContentResolver resolver, List<InputMethodInfo> inputMethodInfos, boolean hasHardKeyboard) {
    String currentInputMethodId = Settings.Secure.getString(resolver, Settings.Secure.DEFAULT_INPUT_METHOD);
    final int selectedInputMethodSubtype = getInputMethodSubtypeSelected(resolver);
    final HashMap<String, HashSet<String>> enabledIMEsAndSubtypesMap = getEnabledInputMethodsAndSubtypeList(resolver);
    final HashSet<String> disabledSystemIMEs = getDisabledSystemIMEs(resolver);
    boolean needsToResetSelectedSubtype = false;
    for (final InputMethodInfo imi : inputMethodInfos) {
        final String imiId = imi.getId();
        final Preference pref = context.findPreference(imiId);
        if (pref == null) {
            continue;
        }
        // In the choose input method screen or in the subtype enabler screen,
        // <code>pref</code> is an instance of TwoStatePreference.
        final boolean isImeChecked = (pref instanceof TwoStatePreference) ? ((TwoStatePreference) pref).isChecked() : enabledIMEsAndSubtypesMap.containsKey(imiId);
        final boolean isCurrentInputMethod = imiId.equals(currentInputMethodId);
        final boolean systemIme = InputMethodUtils.isSystemIme(imi);
        if ((!hasHardKeyboard && InputMethodSettingValuesWrapper.getInstance(context.getActivity()).isAlwaysCheckedIme(imi, context.getActivity())) || isImeChecked) {
            if (!enabledIMEsAndSubtypesMap.containsKey(imiId)) {
                // imiId has just been enabled
                enabledIMEsAndSubtypesMap.put(imiId, new HashSet<String>());
            }
            final HashSet<String> subtypesSet = enabledIMEsAndSubtypesMap.get(imiId);
            boolean subtypePrefFound = false;
            final int subtypeCount = imi.getSubtypeCount();
            for (int i = 0; i < subtypeCount; ++i) {
                final InputMethodSubtype subtype = imi.getSubtypeAt(i);
                final String subtypeHashCodeStr = String.valueOf(subtype.hashCode());
                final TwoStatePreference subtypePref = (TwoStatePreference) context.findPreference(imiId + subtypeHashCodeStr);
                // In the Configure input method screen which does not have subtype preferences.
                if (subtypePref == null) {
                    continue;
                }
                if (!subtypePrefFound) {
                    // Once subtype preference is found, subtypeSet needs to be cleared.
                    // Because of system change, hashCode value could have been changed.
                    subtypesSet.clear();
                    // If selected subtype preference is disabled, needs to reset.
                    needsToResetSelectedSubtype = true;
                    subtypePrefFound = true;
                }
                // subtypes can be saved here.
                if (subtypePref.isEnabled() && subtypePref.isChecked()) {
                    subtypesSet.add(subtypeHashCodeStr);
                    if (isCurrentInputMethod) {
                        if (selectedInputMethodSubtype == subtype.hashCode()) {
                            // Selected subtype is still enabled, there is no need to reset
                            // selected subtype.
                            needsToResetSelectedSubtype = false;
                        }
                    }
                } else {
                    subtypesSet.remove(subtypeHashCodeStr);
                }
            }
        } else {
            enabledIMEsAndSubtypesMap.remove(imiId);
            if (isCurrentInputMethod) {
                // find the applicable IME from the history and the system locale.
                if (DEBUG) {
                    Log.d(TAG, "Current IME was uninstalled or disabled.");
                }
                currentInputMethodId = null;
            }
        }
        // doesn't get enabled automatically on any changes to the package list
        if (systemIme && hasHardKeyboard) {
            if (disabledSystemIMEs.contains(imiId)) {
                if (isImeChecked) {
                    disabledSystemIMEs.remove(imiId);
                }
            } else {
                if (!isImeChecked) {
                    disabledSystemIMEs.add(imiId);
                }
            }
        }
    }
    final String enabledIMEsAndSubtypesString = buildInputMethodsAndSubtypesString(enabledIMEsAndSubtypesMap);
    final String disabledSystemIMEsString = buildInputMethodsString(disabledSystemIMEs);
    if (DEBUG) {
        Log.d(TAG, "--- Save enabled inputmethod settings. :" + enabledIMEsAndSubtypesString);
        Log.d(TAG, "--- Save disabled system inputmethod settings. :" + disabledSystemIMEsString);
        Log.d(TAG, "--- Save default inputmethod settings. :" + currentInputMethodId);
        Log.d(TAG, "--- Needs to reset the selected subtype :" + needsToResetSelectedSubtype);
        Log.d(TAG, "--- Subtype is selected :" + isInputMethodSubtypeSelected(resolver));
    }
    // We should reset the selected input method's subtype.
    if (needsToResetSelectedSubtype || !isInputMethodSubtypeSelected(resolver)) {
        if (DEBUG) {
            Log.d(TAG, "--- Reset inputmethod subtype because it's not defined.");
        }
        putSelectedInputMethodSubtype(resolver, NOT_A_SUBTYPE_ID);
    }
    Settings.Secure.putString(resolver, Settings.Secure.ENABLED_INPUT_METHODS, enabledIMEsAndSubtypesString);
    if (disabledSystemIMEsString.length() > 0) {
        Settings.Secure.putString(resolver, Settings.Secure.DISABLED_SYSTEM_INPUT_METHODS, disabledSystemIMEsString);
    }
    // If the current input method is unset, InputMethodManagerService will find the applicable
    // IME from the history and the system locale.
    Settings.Secure.putString(resolver, Settings.Secure.DEFAULT_INPUT_METHOD, currentInputMethodId != null ? currentInputMethodId : "");
}
Also used : InputMethodSubtype(android.view.inputmethod.InputMethodSubtype) TwoStatePreference(android.support.v7.preference.TwoStatePreference) TwoStatePreference(android.support.v7.preference.TwoStatePreference) Preference(android.support.v7.preference.Preference) InputMethodInfo(android.view.inputmethod.InputMethodInfo) HashSet(java.util.HashSet)

Aggregations

TwoStatePreference (android.support.v7.preference.TwoStatePreference)94 Context (android.content.Context)50 Test (org.junit.Test)48 Preference (android.support.v7.preference.Preference)26 ComponentName (android.content.ComponentName)7 OnPreferenceChangeListener (android.support.v7.preference.Preference.OnPreferenceChangeListener)7 DropDownPreference (android.support.v7.preference.DropDownPreference)6 DefaultRingtonePreference (com.android.settings.DefaultRingtonePreference)6 Calendar (java.util.Calendar)6 Date (java.util.Date)6 InputMethodInfo (android.view.inputmethod.InputMethodInfo)5 ConfigKey (cx.ring.model.ConfigKey)5 InputMethodSubtype (android.view.inputmethod.InputMethodSubtype)4 EditTextPreference (android.support.v7.preference.EditTextPreference)3 PreferenceScreen (android.support.v7.preference.PreferenceScreen)3 File (java.io.File)3 PackageManager (android.content.pm.PackageManager)2 CheckBoxPreference (android.support.v7.preference.CheckBoxPreference)2 ListPreference (android.support.v7.preference.ListPreference)2 SwitchPreferenceCompat (android.support.v7.preference.SwitchPreferenceCompat)2