Use two-target preference for stylus handwriting toggle.
Bug: 294279890 Test: StylusDevicesControllerTest Change-Id: I8e83fb3a8bb76469c4bf1b99df5524930ebad086
This commit is contained in:
@@ -45,6 +45,7 @@ import androidx.preference.SwitchPreference;
|
|||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
import com.android.settings.dashboard.profileselector.ProfileSelectDialog;
|
import com.android.settings.dashboard.profileselector.ProfileSelectDialog;
|
||||||
import com.android.settings.dashboard.profileselector.UserAdapter;
|
import com.android.settings.dashboard.profileselector.UserAdapter;
|
||||||
|
import com.android.settingslib.PrimarySwitchPreference;
|
||||||
import com.android.settingslib.bluetooth.BluetoothUtils;
|
import com.android.settingslib.bluetooth.BluetoothUtils;
|
||||||
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
|
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
|
||||||
import com.android.settingslib.core.AbstractPreferenceController;
|
import com.android.settingslib.core.AbstractPreferenceController;
|
||||||
@@ -59,7 +60,8 @@ import java.util.List;
|
|||||||
* This class adds stylus preferences.
|
* This class adds stylus preferences.
|
||||||
*/
|
*/
|
||||||
public class StylusDevicesController extends AbstractPreferenceController implements
|
public class StylusDevicesController extends AbstractPreferenceController implements
|
||||||
Preference.OnPreferenceClickListener, LifecycleObserver, OnResume {
|
Preference.OnPreferenceClickListener, Preference.OnPreferenceChangeListener,
|
||||||
|
LifecycleObserver, OnResume {
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
static final String KEY_STYLUS = "device_stylus";
|
static final String KEY_STYLUS = "device_stylus";
|
||||||
@@ -138,11 +140,15 @@ public class StylusDevicesController extends AbstractPreferenceController implem
|
|||||||
return pref;
|
return pref;
|
||||||
}
|
}
|
||||||
|
|
||||||
private SwitchPreference createOrUpdateHandwritingPreference(SwitchPreference preference) {
|
private PrimarySwitchPreference createOrUpdateHandwritingPreference(
|
||||||
SwitchPreference pref = preference == null ? new SwitchPreference(mContext) : preference;
|
PrimarySwitchPreference preference) {
|
||||||
|
PrimarySwitchPreference pref = preference == null ? new PrimarySwitchPreference(mContext)
|
||||||
|
: preference;
|
||||||
pref.setKey(KEY_HANDWRITING);
|
pref.setKey(KEY_HANDWRITING);
|
||||||
pref.setTitle(mContext.getString(R.string.stylus_textfield_handwriting));
|
pref.setTitle(mContext.getString(R.string.stylus_textfield_handwriting));
|
||||||
pref.setIcon(R.drawable.ic_text_fields_alt);
|
pref.setIcon(R.drawable.ic_text_fields_alt);
|
||||||
|
// Using a two-target preference, clicking will send an intent and change will toggle.
|
||||||
|
pref.setOnPreferenceChangeListener(this);
|
||||||
pref.setOnPreferenceClickListener(this);
|
pref.setOnPreferenceClickListener(this);
|
||||||
pref.setChecked(Settings.Secure.getInt(mContext.getContentResolver(),
|
pref.setChecked(Settings.Secure.getInt(mContext.getContentResolver(),
|
||||||
Settings.Secure.STYLUS_HANDWRITING_ENABLED,
|
Settings.Secure.STYLUS_HANDWRITING_ENABLED,
|
||||||
@@ -165,7 +171,6 @@ public class StylusDevicesController extends AbstractPreferenceController implem
|
|||||||
@Override
|
@Override
|
||||||
public boolean onPreferenceClick(Preference preference) {
|
public boolean onPreferenceClick(Preference preference) {
|
||||||
String key = preference.getKey();
|
String key = preference.getKey();
|
||||||
|
|
||||||
switch (key) {
|
switch (key) {
|
||||||
case KEY_DEFAULT_NOTES:
|
case KEY_DEFAULT_NOTES:
|
||||||
PackageManager pm = mContext.getPackageManager();
|
PackageManager pm = mContext.getPackageManager();
|
||||||
@@ -181,21 +186,14 @@ public class StylusDevicesController extends AbstractPreferenceController implem
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case KEY_HANDWRITING:
|
case KEY_HANDWRITING:
|
||||||
Settings.Secure.putInt(mContext.getContentResolver(),
|
|
||||||
Settings.Secure.STYLUS_HANDWRITING_ENABLED,
|
|
||||||
((SwitchPreference) preference).isChecked() ? 1 : 0);
|
|
||||||
|
|
||||||
if (((SwitchPreference) preference).isChecked()) {
|
|
||||||
InputMethodManager imm = mContext.getSystemService(InputMethodManager.class);
|
InputMethodManager imm = mContext.getSystemService(InputMethodManager.class);
|
||||||
InputMethodInfo inputMethod = imm.getCurrentInputMethodInfo();
|
InputMethodInfo inputMethod = imm.getCurrentInputMethodInfo();
|
||||||
if (inputMethod == null) break;
|
if (inputMethod == null) break;
|
||||||
|
|
||||||
Intent handwritingIntent =
|
Intent handwritingIntent =
|
||||||
inputMethod.createStylusHandwritingSettingsActivityIntent();
|
inputMethod.createStylusHandwritingSettingsActivityIntent();
|
||||||
if (handwritingIntent != null) {
|
if (handwritingIntent != null) {
|
||||||
mContext.startActivity(handwritingIntent);
|
mContext.startActivity(handwritingIntent);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case KEY_IGNORE_BUTTON:
|
case KEY_IGNORE_BUTTON:
|
||||||
Settings.Secure.putInt(mContext.getContentResolver(),
|
Settings.Secure.putInt(mContext.getContentResolver(),
|
||||||
@@ -206,6 +204,19 @@ public class StylusDevicesController extends AbstractPreferenceController implem
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
||||||
|
String key = preference.getKey();
|
||||||
|
switch (key) {
|
||||||
|
case KEY_HANDWRITING:
|
||||||
|
Settings.Secure.putInt(mContext.getContentResolver(),
|
||||||
|
Settings.Secure.STYLUS_HANDWRITING_ENABLED,
|
||||||
|
(boolean) newValue ? 1 : 0);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final void displayPreference(PreferenceScreen screen) {
|
public final void displayPreference(PreferenceScreen screen) {
|
||||||
mPreferencesContainer = (PreferenceCategory) screen.findPreference(getPreferenceKey());
|
mPreferencesContainer = (PreferenceCategory) screen.findPreference(getPreferenceKey());
|
||||||
@@ -233,7 +244,7 @@ public class StylusDevicesController extends AbstractPreferenceController implem
|
|||||||
mPreferencesContainer.addPreference(notesPref);
|
mPreferencesContainer.addPreference(notesPref);
|
||||||
}
|
}
|
||||||
|
|
||||||
SwitchPreference currHandwritingPref = mPreferencesContainer.findPreference(
|
PrimarySwitchPreference currHandwritingPref = mPreferencesContainer.findPreference(
|
||||||
KEY_HANDWRITING);
|
KEY_HANDWRITING);
|
||||||
Preference handwritingPref = createOrUpdateHandwritingPreference(currHandwritingPref);
|
Preference handwritingPref = createOrUpdateHandwritingPreference(currHandwritingPref);
|
||||||
if (currHandwritingPref == null) {
|
if (currHandwritingPref == null) {
|
||||||
@@ -328,5 +339,4 @@ public class StylusDevicesController extends AbstractPreferenceController implem
|
|||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -57,6 +57,7 @@ import androidx.test.core.app.ApplicationProvider;
|
|||||||
|
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
import com.android.settings.dashboard.profileselector.UserAdapter;
|
import com.android.settings.dashboard.profileselector.UserAdapter;
|
||||||
|
import com.android.settingslib.PrimarySwitchPreference;
|
||||||
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
|
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
|
||||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||||
|
|
||||||
@@ -403,9 +404,10 @@ public class StylusDevicesControllerTest {
|
|||||||
Settings.Secure.STYLUS_HANDWRITING_ENABLED, 1);
|
Settings.Secure.STYLUS_HANDWRITING_ENABLED, 1);
|
||||||
|
|
||||||
showScreen(mController);
|
showScreen(mController);
|
||||||
SwitchPreference handwritingPref = (SwitchPreference) mPreferenceContainer.getPreference(1);
|
PrimarySwitchPreference handwritingPref =
|
||||||
|
(PrimarySwitchPreference) mPreferenceContainer.getPreference(1);
|
||||||
|
|
||||||
assertThat(handwritingPref.isChecked()).isEqualTo(true);
|
assertThat(handwritingPref.getCheckedState()).isEqualTo(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -414,9 +416,10 @@ public class StylusDevicesControllerTest {
|
|||||||
Settings.Secure.STYLUS_HANDWRITING_ENABLED, 0);
|
Settings.Secure.STYLUS_HANDWRITING_ENABLED, 0);
|
||||||
|
|
||||||
showScreen(mController);
|
showScreen(mController);
|
||||||
SwitchPreference handwritingPref = (SwitchPreference) mPreferenceContainer.getPreference(1);
|
PrimarySwitchPreference handwritingPref =
|
||||||
|
(PrimarySwitchPreference) mPreferenceContainer.getPreference(1);
|
||||||
|
|
||||||
assertThat(handwritingPref.isChecked()).isEqualTo(false);
|
assertThat(handwritingPref.getCheckedState()).isEqualTo(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -424,21 +427,20 @@ public class StylusDevicesControllerTest {
|
|||||||
Settings.Secure.putInt(mContext.getContentResolver(),
|
Settings.Secure.putInt(mContext.getContentResolver(),
|
||||||
Settings.Secure.STYLUS_HANDWRITING_ENABLED, 0);
|
Settings.Secure.STYLUS_HANDWRITING_ENABLED, 0);
|
||||||
showScreen(mController);
|
showScreen(mController);
|
||||||
SwitchPreference handwritingPref = (SwitchPreference) mPreferenceContainer.getPreference(1);
|
PrimarySwitchPreference handwritingPref =
|
||||||
|
(PrimarySwitchPreference) mPreferenceContainer.getPreference(1);
|
||||||
|
|
||||||
handwritingPref.performClick();
|
handwritingPref.callChangeListener(true);
|
||||||
|
|
||||||
assertThat(handwritingPref.isChecked()).isEqualTo(true);
|
|
||||||
assertThat(Settings.Secure.getInt(mContext.getContentResolver(),
|
assertThat(Settings.Secure.getInt(mContext.getContentResolver(),
|
||||||
Settings.Secure.STYLUS_HANDWRITING_ENABLED, -1)).isEqualTo(1);
|
Settings.Secure.STYLUS_HANDWRITING_ENABLED, -1)).isEqualTo(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void handwritingPreference_startsHandwritingSettingsOnClickIfChecked() {
|
public void handwritingPreference_startsHandwritingSettingsOnClick() {
|
||||||
Settings.Secure.putInt(mContext.getContentResolver(),
|
|
||||||
Settings.Secure.STYLUS_HANDWRITING_ENABLED, 0);
|
|
||||||
showScreen(mController);
|
showScreen(mController);
|
||||||
SwitchPreference handwritingPref = (SwitchPreference) mPreferenceContainer.getPreference(1);
|
PrimarySwitchPreference handwritingPref =
|
||||||
|
(PrimarySwitchPreference) mPreferenceContainer.getPreference(1);
|
||||||
|
|
||||||
handwritingPref.performClick();
|
handwritingPref.performClick();
|
||||||
|
|
||||||
@@ -447,11 +449,23 @@ public class StylusDevicesControllerTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void handwritingPreference_doesNotStartHandwritingSettingsOnClickIfNotChecked() {
|
public void handwritingPreference_doesNotStartHandwritingSettingsOnChange() {
|
||||||
Settings.Secure.putInt(mContext.getContentResolver(),
|
|
||||||
Settings.Secure.STYLUS_HANDWRITING_ENABLED, 1);
|
|
||||||
showScreen(mController);
|
showScreen(mController);
|
||||||
SwitchPreference handwritingPref = (SwitchPreference) mPreferenceContainer.getPreference(1);
|
PrimarySwitchPreference handwritingPref =
|
||||||
|
(PrimarySwitchPreference) mPreferenceContainer.getPreference(1);
|
||||||
|
|
||||||
|
handwritingPref.callChangeListener(true);
|
||||||
|
|
||||||
|
verify(mInputMethodInfo, times(0)).createStylusHandwritingSettingsActivityIntent();
|
||||||
|
verify(mContext, times(0)).startActivity(any());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void handwritingPreference_doesNotCreateIntentIfNoInputMethod() {
|
||||||
|
when(mImm.getCurrentInputMethodInfo()).thenReturn(null);
|
||||||
|
showScreen(mController);
|
||||||
|
PrimarySwitchPreference handwritingPref =
|
||||||
|
(PrimarySwitchPreference) mPreferenceContainer.getPreference(1);
|
||||||
|
|
||||||
handwritingPref.performClick();
|
handwritingPref.performClick();
|
||||||
|
|
||||||
@@ -463,14 +477,12 @@ public class StylusDevicesControllerTest {
|
|||||||
public void handwritingPreference_doesNotStartHandwritingSettingsIfNoIntent() {
|
public void handwritingPreference_doesNotStartHandwritingSettingsIfNoIntent() {
|
||||||
when(mInputMethodInfo.createStylusHandwritingSettingsActivityIntent())
|
when(mInputMethodInfo.createStylusHandwritingSettingsActivityIntent())
|
||||||
.thenReturn(null);
|
.thenReturn(null);
|
||||||
Settings.Secure.putInt(mContext.getContentResolver(),
|
|
||||||
Settings.Secure.STYLUS_HANDWRITING_ENABLED, 1);
|
|
||||||
showScreen(mController);
|
showScreen(mController);
|
||||||
SwitchPreference handwritingPref = (SwitchPreference) mPreferenceContainer.getPreference(1);
|
PrimarySwitchPreference handwritingPref =
|
||||||
|
(PrimarySwitchPreference) mPreferenceContainer.getPreference(1);
|
||||||
|
|
||||||
handwritingPref.performClick();
|
handwritingPref.performClick();
|
||||||
|
|
||||||
verify(mInputMethodInfo, times(0)).createStylusHandwritingSettingsActivityIntent();
|
|
||||||
verify(mContext, times(0)).startActivity(any());
|
verify(mContext, times(0)).startActivity(any());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user