@SmallTest
  @Feature({"Sync"})
  public void testSyncSwitchClearsServerAutofillCreditCards() throws Exception {
    setUpTestAccountAndSignIn();

    setPaymentsIntegrationEnabled(true);

    assertFalse("There should be no server cards", hasServerAutofillCreditCards());
    addServerAutofillCreditCard();
    assertTrue("There should be server cards", hasServerAutofillCreditCards());

    assertTrue(AndroidSyncSettings.isChromeSyncEnabled(mContext));
    SyncCustomizationFragment fragment = startSyncCustomizationFragment();
    assertDefaultSyncOnState(fragment);
    SwitchPreference syncSwitch = getSyncSwitch(fragment);
    assertTrue(syncSwitch.isChecked());
    assertTrue(AndroidSyncSettings.isChromeSyncEnabled(mContext));
    togglePreference(syncSwitch);
    assertFalse(syncSwitch.isChecked());
    assertFalse(AndroidSyncSettings.isChromeSyncEnabled(mContext));

    closeFragment(fragment);

    assertFalse("There should be no server cards remaining", hasServerAutofillCreditCards());
  }
  @Override
  public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
    if (preference == mTapToWake) {
      return mHardware.set(FEATURE_TAP_TO_WAKE, mTapToWake.isChecked());
    } else if (preference == mWakeWhenPluggedOrUnplugged) {
      Settings.Global.putInt(
          getContentResolver(),
          Settings.Global.WAKE_WHEN_PLUGGED_OR_UNPLUGGED,
          mWakeWhenPluggedOrUnplugged.isChecked() ? 1 : 0);
      return true;
    }

    return super.onPreferenceTreeClick(preferenceScreen, preference);
  }
 private void handleTogglePowerButtonEndsCallPreferenceClick() {
   Settings.Secure.putInt(
       getContentResolver(),
       Settings.Secure.INCALL_POWER_BUTTON_BEHAVIOR,
       (mTogglePowerButtonEndsCallPreference.isChecked()
           ? Settings.Secure.INCALL_POWER_BUTTON_BEHAVIOR_HANGUP
           : Settings.Secure.INCALL_POWER_BUTTON_BEHAVIOR_SCREEN_OFF));
 }
 private void handleToggleLargeTextPreferenceClick() {
   try {
     mCurConfig.fontScale = mToggleLargeTextPreference.isChecked() ? LARGE_FONT_SCALE : 1;
     ActivityManagerNative.getDefault().updatePersistentConfiguration(mCurConfig);
   } catch (RemoteException re) {
     /* ignore */
   }
 }
 private void updateReminderPreferences(SwitchPreference preference) {
   Log.d(TAG, "updateReminderPreferences() called");
   if (preference.isChecked()) {
     TimePickerFragment dialog = new TimePickerFragment();
     dialog.setPreference(preference);
     dialog.show(getFragmentManager(), "TimePickerFragment");
   }
 }
  @SmallTest
  @Feature({"Sync"})
  public void testSyncSwitch() throws Exception {
    setUpTestAccountAndSignIn();
    SyncTestUtil.waitForSyncActive();
    SyncCustomizationFragment fragment = startSyncCustomizationFragment();
    final SwitchPreference syncSwitch = getSyncSwitch(fragment);

    assertTrue(syncSwitch.isChecked());
    assertTrue(AndroidSyncSettings.isChromeSyncEnabled(mContext));
    togglePreference(syncSwitch);
    assertFalse(syncSwitch.isChecked());
    assertFalse(AndroidSyncSettings.isChromeSyncEnabled(mContext));
    togglePreference(syncSwitch);
    assertTrue(syncSwitch.isChecked());
    assertTrue(AndroidSyncSettings.isChromeSyncEnabled(mContext));
  }
 @Override
 public void onActivityResult(int requestCode, int resultCode, Intent data) {
   if (requestCode == REQUEST_CODE_EXIT_ECM) {
     Boolean isChoiceYes = data.getBooleanExtra(EXIT_ECM_RESULT, false);
     // Set Airplane mode based on the return value and checkbox state
     mAirplaneModeEnabler.setAirplaneModeInECM(isChoiceYes, mAirplaneModePreference.isChecked());
   }
   super.onActivityResult(requestCode, resultCode, data);
 }
  @Override
  public void onResume() {
    super.onResume();
    getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);

    SwitchPreference switchPreference = (SwitchPreference) findPreference("show_advanced_setting");
    if (switchPreference.isChecked()) {
      addPreferencesFromResource(R.xml.preference_advanced_setting);
    }
  }
  @SmallTest
  @Feature({"Sync"})
  public void testPaymentsIntegrationEnabledBySyncEverything() throws Exception {
    setUpTestAccountAndSignIn();
    setPaymentsIntegrationEnabled(false);
    disableDataType(ModelType.AUTOFILL);

    // Get the UI elements.
    SyncCustomizationFragment fragment = startSyncCustomizationFragment();
    SwitchPreference syncEverything = getSyncEverything(fragment);
    CheckBoxPreference syncAutofill =
        (CheckBoxPreference)
            fragment.findPreference(SyncCustomizationFragment.PREFERENCE_SYNC_AUTOFILL);
    CheckBoxPreference paymentsIntegration =
        (CheckBoxPreference)
            fragment.findPreference(SyncCustomizationFragment.PREFERENCE_PAYMENTS_INTEGRATION);

    // All three are unchecked and payments is disabled.
    assertFalse(syncEverything.isChecked());
    assertFalse(syncAutofill.isChecked());
    assertTrue(syncAutofill.isEnabled());
    assertFalse(paymentsIntegration.isChecked());
    assertFalse(paymentsIntegration.isEnabled());

    // All three are checked after toggling sync everything.
    togglePreference(syncEverything);
    assertTrue(syncEverything.isChecked());
    assertTrue(syncAutofill.isChecked());
    assertFalse(syncAutofill.isEnabled());
    assertTrue(paymentsIntegration.isChecked());
    assertFalse(paymentsIntegration.isEnabled());

    // Closing the fragment enabled payments integration.
    closeFragment(fragment);
    assertPaymentsIntegrationEnabled(true);
  }
 private void assertDefaultSyncOffState(SyncCustomizationFragment fragment) {
   assertFalse("The sync switch should be off.", getSyncSwitch(fragment).isChecked());
   assertTrue("The sync switch should be enabled.", getSyncSwitch(fragment).isEnabled());
   SwitchPreference syncEverything = getSyncEverything(fragment);
   assertTrue("The sync everything switch should be on.", syncEverything.isChecked());
   assertFalse("The sync everything switch should be disabled.", syncEverything.isEnabled());
   for (CheckBoxPreference dataType : getDataTypes(fragment).values()) {
     String key = dataType.getKey();
     assertTrue("Data type " + key + " should be checked.", dataType.isChecked());
     assertFalse("Data type " + key + " should be disabled.", dataType.isEnabled());
   }
   assertFalse("The encryption button should be disabled.", getEncryption(fragment).isEnabled());
   assertTrue(
       "The manage sync data button should be always enabled.",
       getManageData(fragment).isEnabled());
 }
  @Override
  public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
    if (preference instanceof SwitchPreference) {
      SwitchPreference action = (SwitchPreference) preference;
      mGlobalActionsMap.put(action.getKey(), action.isChecked());

      List<String> enabledActionsList = new ArrayList<String>();
      for (String actionKey : mGlobalActionsMap.keySet()) {
        Boolean checked = mGlobalActionsMap.get(actionKey);
        if (checked) {
          enabledActionsList.add(actionKey);
        }
      }
      setList(enabledActionsList);
      return true;
    }
    return super.onPreferenceTreeClick(preferenceScreen, preference);
  }
 private void handleToggleSpeakPasswordPreferenceClick() {
   Settings.Secure.putInt(
       getContentResolver(),
       Settings.Secure.ACCESSIBILITY_SPEAK_PASSWORD,
       mToggleSpeakPasswordPreference.isChecked() ? 1 : 0);
 }
 private void handleLockScreenRotationPreferenceClick() {
   RotationPolicy.setRotationLockForAccessibility(
       getActivity(), !mToggleLockScreenRotationPreference.isChecked());
 }
 private void handleToggleTextContrastPreferenceClick() {
   Settings.Secure.putInt(
       getContentResolver(),
       Settings.Secure.ACCESSIBILITY_HIGH_TEXT_CONTRAST_ENABLED,
       (mToggleHighTextContrastPreference.isChecked() ? 1 : 0));
 }