Merge "Show Scribe toggle in Stylus settings only if supported by current IME."

This commit is contained in:
Vania Januar
2023-01-23 13:30:42 +00:00
committed by Android (Google) Code Review
2 changed files with 88 additions and 18 deletions

View File

@@ -27,6 +27,8 @@ import android.provider.Settings.Secure;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import android.view.InputDevice; import android.view.InputDevice;
import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodManager;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting; import androidx.annotation.VisibleForTesting;
@@ -119,14 +121,15 @@ public class StylusDevicesController extends AbstractPreferenceController implem
return pref; return pref;
} }
private SwitchPreference createHandwritingPreference() { private SwitchPreference createOrUpdateHandwritingPreference(SwitchPreference preference) {
SwitchPreference pref = new SwitchPreference(mContext); SwitchPreference pref = preference == null ? new SwitchPreference(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);
pref.setOnPreferenceClickListener(this); pref.setOnPreferenceClickListener(this);
pref.setChecked(Settings.Global.getInt(mContext.getContentResolver(), pref.setChecked(Settings.Global.getInt(mContext.getContentResolver(),
Settings.Global.STYLUS_HANDWRITING_ENABLED, 0) == 1); Settings.Global.STYLUS_HANDWRITING_ENABLED, 0) == 1);
pref.setVisible(currentInputMethodSupportsHandwriting());
return pref; return pref;
} }
@@ -157,6 +160,18 @@ public class StylusDevicesController extends AbstractPreferenceController implem
Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.putInt(mContext.getContentResolver(),
Settings.Global.STYLUS_HANDWRITING_ENABLED, Settings.Global.STYLUS_HANDWRITING_ENABLED,
((SwitchPreference) preference).isChecked() ? 1 : 0); ((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; break;
case KEY_IGNORE_BUTTON: case KEY_IGNORE_BUTTON:
Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.putInt(mContext.getContentResolver(),
@@ -194,11 +209,11 @@ public class StylusDevicesController extends AbstractPreferenceController implem
mPreferencesContainer.addPreference(notesPref); mPreferencesContainer.addPreference(notesPref);
} }
Preference handwritingPref = mPreferencesContainer.findPreference(KEY_HANDWRITING); SwitchPreference currHandwritingPref = mPreferencesContainer.findPreference(
// TODO(b/255732419): add proper InputMethodInfo conditional to show or hide KEY_HANDWRITING);
// InputMethodManager imm = mContext.getSystemService(InputMethodManager.class); Preference handwritingPref = createOrUpdateHandwritingPreference(currHandwritingPref);
if (handwritingPref == null) { if (currHandwritingPref == null) {
mPreferencesContainer.addPreference(createHandwritingPreference()); mPreferencesContainer.addPreference(handwritingPref);
} }
Preference buttonPref = mPreferencesContainer.findPreference(KEY_IGNORE_BUTTON); 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 * Identifies whether a device is a stylus using the associated {@link InputDevice} or
* {@link CachedBluetoothDevice}. * {@link CachedBluetoothDevice}.

View File

@@ -21,7 +21,9 @@ import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.any; import static org.mockito.Mockito.any;
import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy; import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@@ -35,6 +37,7 @@ import android.os.UserHandle;
import android.provider.Settings; import android.provider.Settings;
import android.provider.Settings.Secure; import android.provider.Settings.Secure;
import android.view.InputDevice; import android.view.InputDevice;
import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodManager; import android.view.inputmethod.InputMethodManager;
import androidx.preference.Preference; import androidx.preference.Preference;
@@ -49,7 +52,6 @@ import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.core.lifecycle.Lifecycle; import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before; import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor; import org.mockito.ArgumentCaptor;
@@ -73,6 +75,8 @@ public class StylusDevicesControllerTest {
@Mock @Mock
private InputMethodManager mImm; private InputMethodManager mImm;
@Mock @Mock
private InputMethodInfo mInputMethodInfo;
@Mock
private PackageManager mPm; private PackageManager mPm;
@Mock @Mock
private RoleManager mRm; private RoleManager mRm;
@@ -99,6 +103,11 @@ public class StylusDevicesControllerTest {
when(mContext.getSystemService(RoleManager.class)).thenReturn(mRm); when(mContext.getSystemService(RoleManager.class)).thenReturn(mRm);
doNothing().when(mContext).startActivity(any()); 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))) when(mRm.getRoleHoldersAsUser(eq(RoleManager.ROLE_NOTES), any(UserHandle.class)))
.thenReturn(Collections.singletonList(NOTES_PACKAGE_NAME)); .thenReturn(Collections.singletonList(NOTES_PACKAGE_NAME));
when(mRm.isRoleAvailable(RoleManager.ROLE_NOTES)).thenReturn(true); when(mRm.isRoleAvailable(RoleManager.ROLE_NOTES)).thenReturn(true);
@@ -205,24 +214,23 @@ public class StylusDevicesControllerTest {
assertThat(mPreferenceContainer.getPreferenceCount()).isEqualTo(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(handwritingPref.getTitle().toString()).isEqualTo( assertThat(handwritingPref.getTitle().toString()).isEqualTo(
mContext.getString(R.string.stylus_textfield_handwriting)); mContext.getString(R.string.stylus_textfield_handwriting));
assertThat(handwritingPref.isVisible()).isTrue();
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();
} }
@Test @Test
@Ignore // TODO(b/255732419): unignore when InputMethodInfo available public void btStylusInputDevice_noHandwritingIme_handwritingPrefNotVisible() {
public void btStylusInputDevice_noHandwritingIme_showsSomePreferences() { when(mInputMethodInfo.supportsStylusHandwriting()).thenReturn(false);
showScreen(mController);
Preference defaultNotesPref = mPreferenceContainer.getPreference(0);
Preference buttonPref = mPreferenceContainer.getPreference(1);
assertThat(mPreferenceContainer.getPreferenceCount()).isEqualTo(2); showScreen(mController);
assertThat(defaultNotesPref.getTitle().toString()).isEqualTo( Preference handwritingPref = mPreferenceContainer.getPreference(1);
mContext.getString(R.string.stylus_default_notes_app));
assertThat(buttonPref.getTitle().toString()).isEqualTo( assertThat(handwritingPref.isVisible()).isFalse();
mContext.getString(R.string.stylus_ignore_button));
} }
@Test @Test
@@ -312,6 +320,47 @@ public class StylusDevicesControllerTest {
Settings.Global.STYLUS_HANDWRITING_ENABLED, -1)).isEqualTo(1); 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 @Test
public void buttonsPreference_checkedWhenFlagTrue() { public void buttonsPreference_checkedWhenFlagTrue() {
Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.putInt(mContext.getContentResolver(),