androidx.preference.TwoStatePreference Java Examples

The following examples show how to use androidx.preference.TwoStatePreference. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SettingsFragment.java    From Kore with Apache License 2.0 6 votes vote down vote up
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
    switch (requestCode) {
        case Utils.PERMISSION_REQUEST_READ_PHONE_STATE:
            // If request is cancelled, the result arrays are empty.
            if ((grantResults.length == 0) ||
                (grantResults[0] != PackageManager.PERMISSION_GRANTED)) {
                Toast.makeText(getActivity(), R.string.read_phone_state_permission_denied, Toast.LENGTH_SHORT)
                     .show();
                TwoStatePreference pauseCallPreference =
                        (TwoStatePreference)findPreference(Settings.KEY_PREF_PAUSE_DURING_CALLS);
                pauseCallPreference.setChecked(false);
            }
            break;
    }
}
 
Example #2
Source File: TalkBackSelectorPreferencesActivity.java    From talkback with Apache License 2.0 6 votes vote down vote up
/**
 * Enables or disables the setting configuration preference category, depending on the on/off
 * state of the selector.
 */
private void enableOrDisableSelectorSettings(boolean enable) {
  PreferenceCategory settingsCategory =
      (PreferenceCategory)
          findPreference(getString(R.string.pref_category_selector_settings_configuration_key));
  if (settingsCategory == null) {
    return;
  }

  final int count = settingsCategory.getPreferenceCount();

  for (int i = 0; i < count; i++) {
    final Preference preference = settingsCategory.getPreference(i);

    if (preference instanceof TwoStatePreference) {
      TwoStatePreference switchPreference = (TwoStatePreference) preference;
      switchPreference.setEnabled(enable);
    }
  }
}
 
Example #3
Source File: TalkBackPreferencesActivity.java    From talkback with Apache License 2.0 6 votes vote down vote up
/**
 * In versions O and above, assign a default value for speaking passwords without headphones to
 * the system setting for speaking passwords out loud. This way, if the user already wants the
 * system to speak speak passwords out loud, the user will see no change and passwords will
 * continue to be spoken. In M and below, hide this preference.
 */
private void updateSpeakPasswordsPreference() {
  if (FeatureSupport.useSpeakPasswordsServicePref()) {
    // Read talkback speak-passwords preference, with default to system preference.
    boolean speakPassValue = SpeakPasswordsManager.getAlwaysSpeakPasswordsPref(context);
    // Update talkback preference display to match read value.
    TwoStatePreference prefSpeakPasswords =
        (TwoStatePreference)
            findPreferenceByResId(R.string.pref_speak_passwords_without_headphones);
    if (prefSpeakPasswords != null) {
      prefSpeakPasswords.setChecked(speakPassValue);
    }
  } else {
    PreferenceSettingsUtils.hidePreference(
        context, getPreferenceScreen(), R.string.pref_speak_passwords_without_headphones);
  }
}
 
Example #4
Source File: TalkBackPreferencesActivity.java    From talkback with Apache License 2.0 6 votes vote down vote up
private void updateDimingPreferenceStatus() {
  // Log an error if the device supports volume key shortcuts (i.e. those running Android N or
  // earlier) but the dim screen shortcut switch is not available. Don't exit the function
  // because we still want to set up the other switch.
  final TwoStatePreference dimShortcutPreference =
      (TwoStatePreference) findPreferenceByResId(R.string.pref_dim_volume_three_clicks_key);
  if (FeatureSupport.supportsVolumeKeyShortcuts() && dimShortcutPreference == null) {
    LogUtils.e(TAG, "Expected switch for dim screen shortcut, but switch is not present.");
  }

  final TalkBackService talkBack = TalkBackService.getInstance();
  if (talkBack == null || !DimScreenControllerApp.isSupportedbyPlatform(talkBack)) {
    LogUtils.i(
        TAG,
        "Either TalkBack could not be found, or the platform does not support screen dimming.");
    final PreferenceGroup category =
        (PreferenceGroup) findPreferenceByResId(R.string.pref_category_miscellaneous_key);
    if (category == null) {
      return;
    }
    if (dimShortcutPreference != null) {
      category.removePreference(dimShortcutPreference);
    }
    return;
  }
}
 
Example #5
Source File: TalkBackPreferencesActivity.java    From talkback with Apache License 2.0 6 votes vote down vote up
/** Update the audio focus if the activity is visible and the selector has changed the state. */
private void updateAudioFocusPreference() {
  final TwoStatePreference audioFocusPreference =
      (TwoStatePreference) findPreferenceByResId(R.string.pref_use_audio_focus_key);
  if (audioFocusPreference == null) {
    return;
  }

  // Make sure that we have the latest value of the audio focus preference before
  // continuing.
  boolean focusEnabled =
      SharedPreferencesUtils.getBooleanPref(
          prefs,
          getResources(),
          R.string.pref_use_audio_focus_key,
          R.bool.pref_use_audio_focus_default);

  audioFocusPreference.setChecked(focusEnabled);
}
 
Example #6
Source File: TalkBackPreferencesActivity.java    From talkback with Apache License 2.0 6 votes vote down vote up
/** Ensure that the vibration setting does not appear on devices without a vibrator. */
private void checkVibrationSupport() {
  Activity activity = getActivity();
  if (activity == null) {
    return;
  }

  final Vibrator vibrator = (Vibrator) activity.getSystemService(VIBRATOR_SERVICE);

  if (vibrator != null && vibrator.hasVibrator()) {
    return;
  }

  final PreferenceGroup category =
      (PreferenceGroup) findPreferenceByResId(R.string.pref_category_feedback_key);
  final TwoStatePreference prefVibration =
      (TwoStatePreference) findPreferenceByResId(R.string.pref_vibration_key);

  if (prefVibration != null) {
    prefVibration.setChecked(false);
    category.removePreference(prefVibration);
  }
}
 
Example #7
Source File: TalkBackPreferencesActivity.java    From talkback with Apache License 2.0 6 votes vote down vote up
/**
 * Ensure that the proximity sensor setting does not appear on devices without a proximity
 * sensor.
 */
private void checkProximitySupport() {
  Activity activity = getActivity();
  if (activity == null) {
    return;
  }

  final SensorManager manager = (SensorManager) activity.getSystemService(SENSOR_SERVICE);
  final Sensor proximity = manager.getDefaultSensor(Sensor.TYPE_PROXIMITY);

  if (proximity != null) {
    return;
  }

  final PreferenceGroup category =
      (PreferenceGroup) findPreferenceByResId(R.string.pref_category_when_to_speak_key);
  final TwoStatePreference prefProximity =
      (TwoStatePreference) findPreferenceByResId(R.string.pref_proximity_key);

  if (prefProximity != null) {
    prefProximity.setChecked(false);
    category.removePreference(prefProximity);
  }
}
 
Example #8
Source File: TalkBackSelectorPreferencesActivity.java    From talkback with Apache License 2.0 5 votes vote down vote up
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
  context = getActivity().getApplicationContext();
  prefs = SharedPreferencesUtils.getSharedPreferences(context);

  PreferenceSettingsUtils.addPreferencesFromResource(this, R.xml.selector_preferences);

  final TwoStatePreference selectorActivation =
      (TwoStatePreference) findPreference(getString(R.string.pref_selector_activation_key));
  if (selectorActivation != null) {
    selectorActivation.setOnPreferenceChangeListener(selectorActivationChangeListener);
    enableOrDisableSelectorSettings(selectorActivation.isChecked());
  }
}
 
Example #9
Source File: TalkBackPreferencesActivity.java    From talkback with Apache License 2.0 5 votes vote down vote up
private void updateTalkBackShortcutStatus() {
  final TwoStatePreference preference =
      (TwoStatePreference) findPreferenceByResId(R.string.pref_two_volume_long_press_key);
  if (preference == null) {
    return;
  }
  preference.setEnabled(TalkBackService.getInstance() != null || preference.isChecked());
}
 
Example #10
Source File: TalkBackDeveloperPreferencesActivity.java    From talkback with Apache License 2.0 5 votes vote down vote up
/** Assigns the appropriate intent to the touch exploration preference. */
private void initTouchExplorationPreference() {
  final TwoStatePreference prefTouchExploration =
      (TwoStatePreference)
          findPreference(getString(R.string.pref_explore_by_touch_reflect_key));
  if (prefTouchExploration == null) {
    return;
  }

  // Ensure that changes to the reflected preference's checked state never
  // trigger content observers.
  prefTouchExploration.setPersistent(false);

  // Synchronize the reflected state.
  updateTouchExplorationDisplay();

  // Set up listeners that will keep the state synchronized.
  prefTouchExploration.setOnPreferenceChangeListener(touchExplorationChangeListener);

  // Initialize preference dialog
  exploreByTouchDialog =
      new AlertDialog.Builder(this.getActivity())
          .setTitle(R.string.dialog_title_disable_exploration)
          .setMessage(R.string.dialog_message_disable_exploration)
          .setNegativeButton(android.R.string.cancel, null)
          .setOnCancelListener(null)
          .setPositiveButton(
              android.R.string.ok,
              (DialogInterface dialog, int which) -> {
                if (setTouchExplorationRequested(false)) {
                  prefTouchExploration.setChecked(false);
                }
              })
          .create();
}
 
Example #11
Source File: TalkBackDeveloperPreferencesActivity.java    From talkback with Apache License 2.0 5 votes vote down vote up
/**
 * Updates the preferences state to match the actual state of touch exploration. This is called
 * once when the preferences activity launches and again whenever the actual state of touch
 * exploration changes.
 */
private void updateTouchExplorationDisplay() {
  TwoStatePreference prefTouchExploration =
      (TwoStatePreference)
          findPreference(getString(R.string.pref_explore_by_touch_reflect_key));
  if (prefTouchExploration == null) {
    return;
  }

  Activity activity = getActivity();
  if (activity == null) {
    return;
  }

  ContentResolver resolver = activity.getContentResolver();
  Resources res = getResources();
  SharedPreferences prefs = SharedPreferencesUtils.getSharedPreferences(activity);

  boolean requestedState =
      SharedPreferencesUtils.getBooleanPref(
          prefs, res, R.string.pref_explore_by_touch_key, R.bool.pref_explore_by_touch_default);
  boolean reflectedState = prefTouchExploration.isChecked();
  boolean actualState =
      TalkBackService.isServiceActive() ? isTouchExplorationEnabled(resolver) : requestedState;

  // If touch exploration is actually off and we requested it on, the user
  // must have declined the "Enable touch exploration" dialog. Update the
  // requested value to reflect this.
  if (requestedState != actualState) {
    LogUtils.d(TAG, "Set touch exploration preference to reflect actual state %b", actualState);
    SharedPreferencesUtils.putBooleanPref(
        prefs, res, R.string.pref_explore_by_touch_key, actualState);
  }

  // Ensure that the check box preference reflects the requested state,
  // which was just synchronized to match the actual state.
  if (reflectedState != actualState) {
    prefTouchExploration.setChecked(actualState);
  }
}
 
Example #12
Source File: LockScreenPolicyFragment.java    From android-testdpc with Apache License 2.0 5 votes vote down vote up
/**
 * Set an initial value. Updates the summary to match.
 */
private void setup(String key, Object adminSetting) {
    final Preference pref = findPreference(key);
    final DpcPreferenceBase dpcPref = (DpcPreferenceBase) pref;

    // Disable preferences that don't apply to the parent profile
    dpcPref.setCustomConstraint(
            () -> Keys.NOT_APPLICABLE_TO_PARENT.contains(key) && isParentProfileInstance()
                    ? R.string.not_for_parent_profile
                    : NO_CUSTOM_CONSTRIANT
    );

    // We do not allow user to add trust agent config in pre-N devices in managed profile.
    if (Util.SDK_INT < VERSION_CODES.N && key.equals(Keys.SET_TRUST_AGENT_CONFIG)) {
        dpcPref.setAdminConstraint(DpcPreferenceHelper.ADMIN_DEVICE_OWNER);
        return;
    }

    // Set up initial state and possibly a descriptive summary of said state.
    if (pref instanceof EditTextPreference) {
        String stringValue = (adminSetting != null ? adminSetting.toString() : null);
        if (adminSetting instanceof Number && "0".equals(stringValue)) {
            stringValue = null;
        }
        ((EditTextPreference) pref).setText(stringValue);
        pref.setSummary(stringValue);
    } else if (pref instanceof TwoStatePreference) {
        ((TwoStatePreference) pref).setChecked((Boolean) adminSetting);
    }

    // Start listening for change events.
    pref.setOnPreferenceChangeListener(this);
    pref.setOnPreferenceClickListener(this);
}