Stylus: Introduce Show Stylus Hover Pointer Setting (2/2)

Currently, the stylus hover icon can only be shown when
config_enableStylusPointerIcon is set to true. This means that the
users do not have a choice of turning the feature on or off.

This CL introduces the capability to do the same. This CL adds the
toggle in the StylusDevicesController to be shown when stylus is
connected. The toggle is mapped to the STYLUS_POINTER_ENABLED secure
setting which InputManagerService listens to turn on and off the stylus
pointer icon.

Test: manual
Steps:
1. Change the value of config_enableStylusPointerIcon as true.
2. Build the images and flash it to a stylus supporting device.
3. After the device is booted, hover over the screen, the stylus
   pointer should appear.
4. Go to Settings > Connected Devices > Stylus and tap on
   "Show pointer while hovering" toggle.
5. Try hovering over the screen, check no pointer icon is shown
6. Repeat Step#4, check that stylus pointer appears.

Run
- make RunSettingsRoboTests \
      ROBOTEST_FILTER=StylusDevicesControllerTest
- atest SettingsProviderTest

Change-Id: Idfff9b4307370cc510a5f94bd57777c30c96e854
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 7291fad..c5d000b 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -12882,4 +12882,7 @@
 
     <!-- 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>
+
+    <!--Text for Stylus Pointer Icon preference -->
+    <string name="show_stylus_pointer_icon">Show pointer while hovering</string>
 </resources>
diff --git a/src/com/android/settings/connecteddevice/stylus/StylusDevicesController.java b/src/com/android/settings/connecteddevice/stylus/StylusDevicesController.java
index d8e8887..cd23103 100644
--- a/src/com/android/settings/connecteddevice/stylus/StylusDevicesController.java
+++ b/src/com/android/settings/connecteddevice/stylus/StylusDevicesController.java
@@ -24,6 +24,7 @@
 import android.content.pm.ApplicationInfo;
 import android.content.pm.PackageManager;
 import android.content.pm.UserInfo;
+import android.hardware.input.InputSettings;
 import android.os.Process;
 import android.os.UserHandle;
 import android.os.UserManager;
@@ -73,6 +74,8 @@
     static final String KEY_IGNORE_BUTTON = "ignore_button";
     @VisibleForTesting
     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";
 
@@ -181,6 +184,26 @@
         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
     public boolean onPreferenceClick(Preference preference) {
         String key = preference.getKey();
@@ -213,6 +236,11 @@
                         Secure.STYLUS_BUTTONS_ENABLED,
                         ((TwoStatePreference) preference).isChecked() ? 0 : 1);
                 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;
     }
@@ -268,6 +296,13 @@
         if (buttonPref == null) {
             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() {
diff --git a/tests/robotests/src/com/android/settings/connecteddevice/stylus/StylusDevicesControllerTest.java b/tests/robotests/src/com/android/settings/connecteddevice/stylus/StylusDevicesControllerTest.java
index a540d28..135be4b 100644
--- a/tests/robotests/src/com/android/settings/connecteddevice/stylus/StylusDevicesControllerTest.java
+++ b/tests/robotests/src/com/android/settings/connecteddevice/stylus/StylusDevicesControllerTest.java
@@ -221,7 +221,7 @@
 
         showScreen(controller);
 
-        assertThat(mPreferenceContainer.getPreferenceCount()).isEqualTo(3);
+        assertThat(mPreferenceContainer.getPreferenceCount()).isEqualTo(4);
     }
 
     @Test
@@ -249,11 +249,12 @@
     @Test
     public void btStylusInputDevice_showsAllPreferences() {
         showScreen(mController);
+
         Preference defaultNotesPref = mPreferenceContainer.getPreference(0);
         Preference handwritingPref = mPreferenceContainer.getPreference(1);
         Preference buttonPref = mPreferenceContainer.getPreference(2);
+        Preference stylusPointerIconPref = mPreferenceContainer.getPreference(3);
 
-        assertThat(mPreferenceContainer.getPreferenceCount()).isEqualTo(3);
         assertThat(defaultNotesPref.getTitle().toString()).isEqualTo(
                 mContext.getString(R.string.stylus_default_notes_app));
         assertThat(defaultNotesPref.isVisible()).isTrue();
@@ -263,6 +264,9 @@
         assertThat(buttonPref.getTitle().toString()).isEqualTo(
                 mContext.getString(R.string.stylus_ignore_button));
         assertThat(buttonPref.isVisible()).isTrue();
+        assertThat(stylusPointerIconPref.getTitle().toString()).isEqualTo(
+                mContext.getString(R.string.show_stylus_pointer_icon));
+        assertThat(stylusPointerIconPref.isVisible()).isTrue();
     }
 
     @Test
@@ -551,6 +555,46 @@
                 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) {
         controller.displayPreference(mScreen);
     }