Handle cached bluetooth device with stylus pref

- Checking whether a stylus has a tail button key is not possible when
  the device is cached, so assume that it does.
- Updated StylusDevicesControllerTest to use SwitchPreferenceCompat as
  there were unrelated tests that were failing. The actual test that
  would've caught the bug only started failing after the unrelated test
  failures were fixed.

Bug: 311334474
Test: atest StylusDevicesControllerTest
Flag: N/A
Change-Id: Iee2899bf0a77a70f4a4f76833f485d511d919c40
This commit is contained in:
Ajinkya Chalke
2023-11-20 15:46:27 +00:00
parent 145733011c
commit dcb5bc6da5
2 changed files with 16 additions and 8 deletions

View File

@@ -108,10 +108,15 @@ public class StylusDevicesController extends AbstractPreferenceController implem
return null;
}
boolean doesStylusSupportTailButton = mInputDevice.hasKeys(
KeyEvent.KEYCODE_STYLUS_BUTTON_TAIL)[0];
if (!doesStylusSupportTailButton) {
return null;
// Check if the connected stylus supports the tail button. A connected device is when input
// device is available (mInputDevice != null). For a cached device (mInputDevice == null)
// there isn't way to check if the device supports the button so assume it does.
if (mInputDevice != null) {
boolean doesStylusSupportTailButton =
mInputDevice.hasKeys(KeyEvent.KEYCODE_STYLUS_BUTTON_TAIL)[0];
if (!doesStylusSupportTailButton) {
return null;
}
}
Preference pref = preference == null ? new Preference(mContext) : preference;

View File

@@ -54,7 +54,7 @@ import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceManager;
import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference;
import androidx.preference.SwitchPreferenceCompat;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.R;
@@ -518,7 +518,8 @@ public class StylusDevicesControllerTest {
Settings.Secure.STYLUS_BUTTONS_ENABLED, 0);
showScreen(mController);
SwitchPreference buttonsPref = (SwitchPreference) mPreferenceContainer.getPreference(2);
SwitchPreferenceCompat buttonsPref =
(SwitchPreferenceCompat) mPreferenceContainer.getPreference(2);
assertThat(buttonsPref.isChecked()).isEqualTo(true);
}
@@ -529,7 +530,8 @@ public class StylusDevicesControllerTest {
Settings.Secure.STYLUS_BUTTONS_ENABLED, 1);
showScreen(mController);
SwitchPreference buttonsPref = (SwitchPreference) mPreferenceContainer.getPreference(2);
SwitchPreferenceCompat buttonsPref =
(SwitchPreferenceCompat) mPreferenceContainer.getPreference(2);
assertThat(buttonsPref.isChecked()).isEqualTo(false);
}
@@ -539,7 +541,8 @@ public class StylusDevicesControllerTest {
Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Secure.STYLUS_BUTTONS_ENABLED, 0);
showScreen(mController);
SwitchPreference buttonsPref = (SwitchPreference) mPreferenceContainer.getPreference(2);
SwitchPreferenceCompat buttonsPref =
(SwitchPreferenceCompat) mPreferenceContainer.getPreference(2);
buttonsPref.performClick();