Merge "Stylus: Introduce Show Stylus Hover Pointer Setting (2/2)" into main

This commit is contained in:
Prabir Pradhan
2024-02-07 22:49:21 +00:00
committed by Android (Google) Code Review
3 changed files with 84 additions and 2 deletions

View File

@@ -12882,4 +12882,7 @@
<!-- Authority of the content provider that support methods restartPhoneProcess and restartRild. Will be overlaid by OEM.--> <!-- Authority of the content provider that support methods restartPhoneProcess and restartRild. Will be overlaid by OEM.-->
<string name="reset_telephony_stack_content_provider_authority" translatable="false"></string> <string name="reset_telephony_stack_content_provider_authority" translatable="false"></string>
<!--Text for Stylus Pointer Icon preference -->
<string name="show_stylus_pointer_icon">Show pointer while hovering</string>
</resources> </resources>

View File

@@ -24,6 +24,7 @@ import android.content.Intent;
import android.content.pm.ApplicationInfo; import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.pm.UserInfo; import android.content.pm.UserInfo;
import android.hardware.input.InputSettings;
import android.os.Process; import android.os.Process;
import android.os.UserHandle; import android.os.UserHandle;
import android.os.UserManager; import android.os.UserManager;
@@ -73,6 +74,8 @@ public class StylusDevicesController extends AbstractPreferenceController implem
static final String KEY_IGNORE_BUTTON = "ignore_button"; static final String KEY_IGNORE_BUTTON = "ignore_button";
@VisibleForTesting @VisibleForTesting
static final String KEY_DEFAULT_NOTES = "default_notes"; static final String KEY_DEFAULT_NOTES = "default_notes";
@VisibleForTesting
static final String KEY_SHOW_STYLUS_POINTER_ICON = "show_stylus_pointer_icon";
private static final String TAG = "StylusDevicesController"; private static final String TAG = "StylusDevicesController";
@@ -181,6 +184,26 @@ public class StylusDevicesController extends AbstractPreferenceController implem
return pref; return pref;
} }
@Nullable
private SwitchPreferenceCompat createShowStylusPointerIconPreference(
SwitchPreferenceCompat preference) {
if (!mContext.getResources()
.getBoolean(com.android.internal.R.bool.config_enableStylusPointerIcon)) {
// If the config is not enabled, no need to show the preference to user
return null;
}
SwitchPreferenceCompat pref = preference == null ? new SwitchPreferenceCompat(mContext)
: preference;
pref.setKey(KEY_SHOW_STYLUS_POINTER_ICON);
pref.setTitle(mContext.getString(R.string.show_stylus_pointer_icon));
pref.setIcon(R.drawable.ic_stylus);
pref.setOnPreferenceClickListener(this);
pref.setChecked(Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Secure.STYLUS_POINTER_ICON_ENABLED,
InputSettings.DEFAULT_STYLUS_POINTER_ICON_ENABLED) == 1);
return pref;
}
@Override @Override
public boolean onPreferenceClick(Preference preference) { public boolean onPreferenceClick(Preference preference) {
String key = preference.getKey(); String key = preference.getKey();
@@ -213,6 +236,11 @@ public class StylusDevicesController extends AbstractPreferenceController implem
Secure.STYLUS_BUTTONS_ENABLED, Secure.STYLUS_BUTTONS_ENABLED,
((TwoStatePreference) preference).isChecked() ? 0 : 1); ((TwoStatePreference) preference).isChecked() ? 0 : 1);
break; break;
case KEY_SHOW_STYLUS_POINTER_ICON:
Settings.Secure.putInt(mContext.getContentResolver(),
Secure.STYLUS_POINTER_ICON_ENABLED,
((SwitchPreferenceCompat) preference).isChecked() ? 1 : 0);
break;
} }
return true; return true;
} }
@@ -268,6 +296,13 @@ public class StylusDevicesController extends AbstractPreferenceController implem
if (buttonPref == null) { if (buttonPref == null) {
mPreferencesContainer.addPreference(createButtonPressPreference()); mPreferencesContainer.addPreference(createButtonPressPreference());
} }
SwitchPreferenceCompat currShowStylusPointerIconPref = mPreferencesContainer
.findPreference(KEY_SHOW_STYLUS_POINTER_ICON);
Preference showStylusPointerIconPref =
createShowStylusPointerIconPreference(currShowStylusPointerIconPref);
if (currShowStylusPointerIconPref == null && showStylusPointerIconPref != null) {
mPreferencesContainer.addPreference(showStylusPointerIconPref);
}
} }
private boolean currentInputMethodSupportsHandwriting() { private boolean currentInputMethodSupportsHandwriting() {

View File

@@ -221,7 +221,7 @@ public class StylusDevicesControllerTest {
showScreen(controller); showScreen(controller);
assertThat(mPreferenceContainer.getPreferenceCount()).isEqualTo(3); assertThat(mPreferenceContainer.getPreferenceCount()).isEqualTo(4);
} }
@Test @Test
@@ -249,11 +249,12 @@ public class StylusDevicesControllerTest {
@Test @Test
public void btStylusInputDevice_showsAllPreferences() { public void btStylusInputDevice_showsAllPreferences() {
showScreen(mController); showScreen(mController);
Preference defaultNotesPref = mPreferenceContainer.getPreference(0); Preference defaultNotesPref = mPreferenceContainer.getPreference(0);
Preference handwritingPref = mPreferenceContainer.getPreference(1); Preference handwritingPref = mPreferenceContainer.getPreference(1);
Preference buttonPref = mPreferenceContainer.getPreference(2); Preference buttonPref = mPreferenceContainer.getPreference(2);
Preference stylusPointerIconPref = mPreferenceContainer.getPreference(3);
assertThat(mPreferenceContainer.getPreferenceCount()).isEqualTo(3);
assertThat(defaultNotesPref.getTitle().toString()).isEqualTo( assertThat(defaultNotesPref.getTitle().toString()).isEqualTo(
mContext.getString(R.string.stylus_default_notes_app)); mContext.getString(R.string.stylus_default_notes_app));
assertThat(defaultNotesPref.isVisible()).isTrue(); assertThat(defaultNotesPref.isVisible()).isTrue();
@@ -263,6 +264,9 @@ public class StylusDevicesControllerTest {
assertThat(buttonPref.getTitle().toString()).isEqualTo( assertThat(buttonPref.getTitle().toString()).isEqualTo(
mContext.getString(R.string.stylus_ignore_button)); mContext.getString(R.string.stylus_ignore_button));
assertThat(buttonPref.isVisible()).isTrue(); assertThat(buttonPref.isVisible()).isTrue();
assertThat(stylusPointerIconPref.getTitle().toString()).isEqualTo(
mContext.getString(R.string.show_stylus_pointer_icon));
assertThat(stylusPointerIconPref.isVisible()).isTrue();
} }
@Test @Test
@@ -551,6 +555,46 @@ public class StylusDevicesControllerTest {
Secure.STYLUS_BUTTONS_ENABLED, -1)).isEqualTo(1); Secure.STYLUS_BUTTONS_ENABLED, -1)).isEqualTo(1);
} }
@Test
public void stylusPointerIconPreference_checkedWhenFlagTrue() {
Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Secure.STYLUS_POINTER_ICON_ENABLED, 1);
showScreen(mController);
SwitchPreferenceCompat stylusPointerIconPref =
(SwitchPreferenceCompat) mPreferenceContainer.getPreference(3);
assertThat(stylusPointerIconPref.isChecked()).isEqualTo(true);
}
@Test
public void stylusPointerIconPreference_uncheckedWhenFlagFalse() {
Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Secure.STYLUS_POINTER_ICON_ENABLED, 0);
showScreen(mController);
SwitchPreferenceCompat stylusPointerIconPref =
(SwitchPreferenceCompat) mPreferenceContainer.getPreference(3);
assertThat(stylusPointerIconPref.isChecked()).isEqualTo(false);
}
@Test
public void stylusPointerIconPreference_updatesFlagOnClick() {
Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Secure.STYLUS_POINTER_ICON_ENABLED, 0);
showScreen(mController);
SwitchPreferenceCompat stylusPointerIconPref =
(SwitchPreferenceCompat) mPreferenceContainer.getPreference(3);
stylusPointerIconPref.performClick();
assertThat(stylusPointerIconPref.isChecked()).isEqualTo(true);
assertThat(Settings.Secure.getInt(mContext.getContentResolver(),
Secure.STYLUS_POINTER_ICON_ENABLED, -1)).isEqualTo(1);
}
private void showScreen(StylusDevicesController controller) { private void showScreen(StylusDevicesController controller) {
controller.displayPreference(mScreen); controller.displayPreference(mScreen);
} }