diff --git a/src/com/android/settings/connecteddevice/stylus/StylusDevicesController.java b/src/com/android/settings/connecteddevice/stylus/StylusDevicesController.java index e04251a218e..a7e94e91ff1 100644 --- a/src/com/android/settings/connecteddevice/stylus/StylusDevicesController.java +++ b/src/com/android/settings/connecteddevice/stylus/StylusDevicesController.java @@ -27,6 +27,8 @@ import android.provider.Settings.Secure; import android.text.TextUtils; import android.util.Log; import android.view.InputDevice; +import android.view.inputmethod.InputMethodInfo; +import android.view.inputmethod.InputMethodManager; import androidx.annotation.Nullable; import androidx.annotation.VisibleForTesting; @@ -119,14 +121,15 @@ public class StylusDevicesController extends AbstractPreferenceController implem return pref; } - private SwitchPreference createHandwritingPreference() { - SwitchPreference pref = new SwitchPreference(mContext); + private SwitchPreference createOrUpdateHandwritingPreference(SwitchPreference preference) { + SwitchPreference pref = preference == null ? new SwitchPreference(mContext) : preference; pref.setKey(KEY_HANDWRITING); pref.setTitle(mContext.getString(R.string.stylus_textfield_handwriting)); pref.setIcon(R.drawable.ic_text_fields_alt); pref.setOnPreferenceClickListener(this); pref.setChecked(Settings.Global.getInt(mContext.getContentResolver(), Settings.Global.STYLUS_HANDWRITING_ENABLED, 0) == 1); + pref.setVisible(currentInputMethodSupportsHandwriting()); return pref; } @@ -157,6 +160,18 @@ public class StylusDevicesController extends AbstractPreferenceController implem Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.STYLUS_HANDWRITING_ENABLED, ((SwitchPreference) preference).isChecked() ? 1 : 0); + + if (((SwitchPreference) preference).isChecked()) { + InputMethodManager imm = mContext.getSystemService(InputMethodManager.class); + InputMethodInfo inputMethod = imm.getCurrentInputMethodInfo(); + if (inputMethod == null) break; + + Intent handwritingIntent = + inputMethod.createStylusHandwritingSettingsActivityIntent(); + if (handwritingIntent != null) { + mContext.startActivity(handwritingIntent); + } + } break; case KEY_IGNORE_BUTTON: Settings.Secure.putInt(mContext.getContentResolver(), @@ -194,11 +209,11 @@ public class StylusDevicesController extends AbstractPreferenceController implem mPreferencesContainer.addPreference(notesPref); } - Preference handwritingPref = mPreferencesContainer.findPreference(KEY_HANDWRITING); - // TODO(b/255732419): add proper InputMethodInfo conditional to show or hide - // InputMethodManager imm = mContext.getSystemService(InputMethodManager.class); - if (handwritingPref == null) { - mPreferencesContainer.addPreference(createHandwritingPreference()); + SwitchPreference currHandwritingPref = mPreferencesContainer.findPreference( + KEY_HANDWRITING); + Preference handwritingPref = createOrUpdateHandwritingPreference(currHandwritingPref); + if (currHandwritingPref == null) { + mPreferencesContainer.addPreference(handwritingPref); } Preference buttonPref = mPreferencesContainer.findPreference(KEY_IGNORE_BUTTON); @@ -207,6 +222,12 @@ public class StylusDevicesController extends AbstractPreferenceController implem } } + private boolean currentInputMethodSupportsHandwriting() { + InputMethodManager imm = mContext.getSystemService(InputMethodManager.class); + InputMethodInfo inputMethod = imm.getCurrentInputMethodInfo(); + return inputMethod != null && inputMethod.supportsStylusHandwriting(); + } + /** * Identifies whether a device is a stylus using the associated {@link InputDevice} or * {@link CachedBluetoothDevice}. 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 d81722fd5fc..cd6b4bd1260 100644 --- a/tests/robotests/src/com/android/settings/connecteddevice/stylus/StylusDevicesControllerTest.java +++ b/tests/robotests/src/com/android/settings/connecteddevice/stylus/StylusDevicesControllerTest.java @@ -21,7 +21,9 @@ import static com.google.common.truth.Truth.assertThat; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.any; import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -35,6 +37,7 @@ import android.os.UserHandle; import android.provider.Settings; import android.provider.Settings.Secure; import android.view.InputDevice; +import android.view.inputmethod.InputMethodInfo; import android.view.inputmethod.InputMethodManager; import androidx.preference.Preference; @@ -49,7 +52,6 @@ import com.android.settingslib.bluetooth.CachedBluetoothDevice; import com.android.settingslib.core.lifecycle.Lifecycle; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; @@ -73,6 +75,8 @@ public class StylusDevicesControllerTest { @Mock private InputMethodManager mImm; @Mock + private InputMethodInfo mInputMethodInfo; + @Mock private PackageManager mPm; @Mock private RoleManager mRm; @@ -99,6 +103,11 @@ public class StylusDevicesControllerTest { when(mContext.getSystemService(RoleManager.class)).thenReturn(mRm); doNothing().when(mContext).startActivity(any()); + when(mImm.getCurrentInputMethodInfo()).thenReturn(mInputMethodInfo); + when(mInputMethodInfo.createStylusHandwritingSettingsActivityIntent()) + .thenReturn(mock(Intent.class)); + when(mInputMethodInfo.supportsStylusHandwriting()).thenReturn(true); + when(mRm.getRoleHoldersAsUser(eq(RoleManager.ROLE_NOTES), any(UserHandle.class))) .thenReturn(Collections.singletonList(NOTES_PACKAGE_NAME)); when(mRm.isRoleAvailable(RoleManager.ROLE_NOTES)).thenReturn(true); @@ -205,24 +214,23 @@ public class StylusDevicesControllerTest { assertThat(mPreferenceContainer.getPreferenceCount()).isEqualTo(3); assertThat(defaultNotesPref.getTitle().toString()).isEqualTo( mContext.getString(R.string.stylus_default_notes_app)); + assertThat(defaultNotesPref.isVisible()).isTrue(); assertThat(handwritingPref.getTitle().toString()).isEqualTo( mContext.getString(R.string.stylus_textfield_handwriting)); + assertThat(handwritingPref.isVisible()).isTrue(); assertThat(buttonPref.getTitle().toString()).isEqualTo( mContext.getString(R.string.stylus_ignore_button)); + assertThat(buttonPref.isVisible()).isTrue(); } @Test - @Ignore // TODO(b/255732419): unignore when InputMethodInfo available - public void btStylusInputDevice_noHandwritingIme_showsSomePreferences() { - showScreen(mController); - Preference defaultNotesPref = mPreferenceContainer.getPreference(0); - Preference buttonPref = mPreferenceContainer.getPreference(1); + public void btStylusInputDevice_noHandwritingIme_handwritingPrefNotVisible() { + when(mInputMethodInfo.supportsStylusHandwriting()).thenReturn(false); - assertThat(mPreferenceContainer.getPreferenceCount()).isEqualTo(2); - assertThat(defaultNotesPref.getTitle().toString()).isEqualTo( - mContext.getString(R.string.stylus_default_notes_app)); - assertThat(buttonPref.getTitle().toString()).isEqualTo( - mContext.getString(R.string.stylus_ignore_button)); + showScreen(mController); + Preference handwritingPref = mPreferenceContainer.getPreference(1); + + assertThat(handwritingPref.isVisible()).isFalse(); } @Test @@ -312,6 +320,47 @@ public class StylusDevicesControllerTest { Settings.Global.STYLUS_HANDWRITING_ENABLED, -1)).isEqualTo(1); } + @Test + public void handwritingPreference_startsHandwritingSettingsOnClickIfChecked() { + Settings.Global.putInt(mContext.getContentResolver(), + Settings.Global.STYLUS_HANDWRITING_ENABLED, 0); + showScreen(mController); + SwitchPreference handwritingPref = (SwitchPreference) mPreferenceContainer.getPreference(1); + + handwritingPref.performClick(); + + verify(mInputMethodInfo).createStylusHandwritingSettingsActivityIntent(); + verify(mContext).startActivity(any()); + } + + @Test + public void handwritingPreference_doesNotStartHandwritingSettingsOnClickIfNotChecked() { + Settings.Global.putInt(mContext.getContentResolver(), + Settings.Global.STYLUS_HANDWRITING_ENABLED, 1); + showScreen(mController); + SwitchPreference handwritingPref = (SwitchPreference) mPreferenceContainer.getPreference(1); + + handwritingPref.performClick(); + + verify(mInputMethodInfo, times(0)).createStylusHandwritingSettingsActivityIntent(); + verify(mContext, times(0)).startActivity(any()); + } + + @Test + public void handwritingPreference_doesNotStartHandwritingSettingsIfNoIntent() { + when(mInputMethodInfo.createStylusHandwritingSettingsActivityIntent()) + .thenReturn(null); + Settings.Global.putInt(mContext.getContentResolver(), + Settings.Global.STYLUS_HANDWRITING_ENABLED, 1); + showScreen(mController); + SwitchPreference handwritingPref = (SwitchPreference) mPreferenceContainer.getPreference(1); + + handwritingPref.performClick(); + + verify(mInputMethodInfo, times(0)).createStylusHandwritingSettingsActivityIntent(); + verify(mContext, times(0)).startActivity(any()); + } + @Test public void buttonsPreference_checkedWhenFlagTrue() { Settings.Secure.putInt(mContext.getContentResolver(),