Fix a incorrect class cast in GameControllerPreferenceCtrl
Also convert the controller to TogglePrefCtrl, and register it in xml. Change-Id: Ifbc95364b47690117b2875cba1cdc4761ad205be Bug: 71972185 Test: make RunSettingsRoboTests
This commit is contained in:
@@ -82,15 +82,10 @@
|
||||
|
||||
</PreferenceCategory>
|
||||
|
||||
<PreferenceCategory
|
||||
android:key="game_controller_settings_category"
|
||||
android:title="@string/game_controller_settings_category">
|
||||
|
||||
<SwitchPreference
|
||||
android:key="vibrate_input_devices"
|
||||
android:title="@string/vibrate_input_devices"
|
||||
android:summary="@string/vibrate_input_devices_summary" />
|
||||
|
||||
</PreferenceCategory>
|
||||
android:summary="@string/vibrate_input_devices_summary"
|
||||
settings:controller="com.android.settings.inputmethod.GameControllerPreferenceController" />
|
||||
|
||||
</PreferenceScreen>
|
||||
|
@@ -26,31 +26,23 @@ import androidx.preference.PreferenceScreen;
|
||||
import android.text.TextUtils;
|
||||
import android.view.InputDevice;
|
||||
|
||||
import com.android.settings.core.PreferenceControllerMixin;
|
||||
import com.android.settings.R;
|
||||
import com.android.settingslib.core.AbstractPreferenceController;
|
||||
import com.android.settings.core.PreferenceControllerMixin;
|
||||
import com.android.settings.core.TogglePreferenceController;
|
||||
import com.android.settingslib.core.lifecycle.LifecycleObserver;
|
||||
import com.android.settingslib.core.lifecycle.events.OnPause;
|
||||
import com.android.settingslib.core.lifecycle.events.OnResume;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class GameControllerPreferenceController extends AbstractPreferenceController
|
||||
public class GameControllerPreferenceController extends TogglePreferenceController
|
||||
implements PreferenceControllerMixin, InputManager.InputDeviceListener, LifecycleObserver,
|
||||
OnResume, OnPause {
|
||||
|
||||
@VisibleForTesting
|
||||
static final String PREF_KEY = "vibrate_input_devices";
|
||||
private static final String CATEGORY_KEY = "game_controller_settings_category";
|
||||
|
||||
private final InputManager mIm;
|
||||
|
||||
private PreferenceScreen mScreen;
|
||||
private Preference mCategory;
|
||||
private Preference mPreference;
|
||||
|
||||
public GameControllerPreferenceController(Context context) {
|
||||
super(context);
|
||||
public GameControllerPreferenceController(Context context, String key) {
|
||||
super(context, key);
|
||||
mIm = (InputManager) context.getSystemService(Context.INPUT_SERVICE);
|
||||
}
|
||||
|
||||
@@ -67,85 +59,61 @@ public class GameControllerPreferenceController extends AbstractPreferenceContro
|
||||
@Override
|
||||
public void displayPreference(PreferenceScreen screen) {
|
||||
super.displayPreference(screen);
|
||||
mScreen = screen;
|
||||
mCategory = screen.findPreference(CATEGORY_KEY);
|
||||
mPreference = screen.findPreference(PREF_KEY);
|
||||
mPreference = screen.findPreference(getPreferenceKey());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAvailable() {
|
||||
@AvailabilityStatus
|
||||
public int getAvailabilityStatus() {
|
||||
// If device explicitly wants to hide this, return early.
|
||||
if (!mContext.getResources().getBoolean(R.bool.config_show_vibrate_input_devices)) {
|
||||
return false;
|
||||
return DISABLED_UNSUPPORTED;
|
||||
}
|
||||
|
||||
final int[] devices = mIm.getInputDeviceIds();
|
||||
for (int deviceId : devices) {
|
||||
InputDevice device = mIm.getInputDevice(deviceId);
|
||||
if (device != null && !device.isVirtual() && device.getVibrator().hasVibrator()) {
|
||||
return true;
|
||||
return AVAILABLE;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handlePreferenceTreeClick(Preference preference) {
|
||||
if (TextUtils.equals(PREF_KEY, preference.getKey())) {
|
||||
Settings.System.putInt(mContext.getContentResolver(),
|
||||
Settings.System.VIBRATE_INPUT_DEVICES,
|
||||
((SwitchPreference) preference).isChecked() ? 1 : 0);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPreferenceKey() {
|
||||
return CATEGORY_KEY;
|
||||
return DISABLED_UNSUPPORTED;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateState(Preference preference) {
|
||||
super.updateState(preference);
|
||||
if (preference == null) {
|
||||
return;
|
||||
}
|
||||
((SwitchPreference) preference).setChecked(Settings.System.getInt(
|
||||
mContext.getContentResolver(),
|
||||
Settings.System.VIBRATE_INPUT_DEVICES, 1) > 0);
|
||||
mPreference.setVisible(isAvailable());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateNonIndexableKeys(List<String> keys) {
|
||||
if (!isAvailable()) {
|
||||
keys.add(CATEGORY_KEY);
|
||||
keys.add(PREF_KEY);
|
||||
public boolean isChecked() {
|
||||
return Settings.System.getInt(
|
||||
mContext.getContentResolver(),
|
||||
Settings.System.VIBRATE_INPUT_DEVICES, 1) > 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean setChecked(boolean isChecked) {
|
||||
return Settings.System.putInt(mContext.getContentResolver(),
|
||||
Settings.System.VIBRATE_INPUT_DEVICES, isChecked ? 1 : 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onInputDeviceAdded(int deviceId) {
|
||||
updateGameControllers();
|
||||
updateState(mPreference);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onInputDeviceRemoved(int deviceId) {
|
||||
updateGameControllers();
|
||||
updateState(mPreference);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onInputDeviceChanged(int deviceId) {
|
||||
updateGameControllers();
|
||||
}
|
||||
|
||||
private void updateGameControllers() {
|
||||
if (isAvailable()) {
|
||||
mScreen.addPreference(mCategory);
|
||||
updateState(mPreference);
|
||||
} else {
|
||||
if (mCategory != null) {
|
||||
mScreen.removePreference(mCategory);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -35,7 +35,6 @@ import com.android.settings.R;
|
||||
import com.android.settings.applications.defaultapps.DefaultAutofillPreferenceController;
|
||||
import com.android.settings.dashboard.DashboardFragment;
|
||||
import com.android.settings.dashboard.SummaryLoader;
|
||||
import com.android.settings.inputmethod.GameControllerPreferenceController;
|
||||
import com.android.settings.inputmethod.PhysicalKeyboardPreferenceController;
|
||||
import com.android.settings.inputmethod.SpellCheckerPreferenceController;
|
||||
import com.android.settings.inputmethod.VirtualKeyboardPreferenceController;
|
||||
@@ -126,17 +125,6 @@ public class LanguageAndInputSettings extends DashboardFragment {
|
||||
controllers.add(new DefaultAutofillPreferenceController(context));
|
||||
controllers.add(new UserDictionaryPreferenceController(context));
|
||||
|
||||
// Game Controller
|
||||
final GameControllerPreferenceController gameControllerPreferenceController
|
||||
= new GameControllerPreferenceController(context);
|
||||
if (lifecycle != null) {
|
||||
lifecycle.addObserver(gameControllerPreferenceController);
|
||||
}
|
||||
controllers.add(gameControllerPreferenceController);
|
||||
controllers.add(new PreferenceCategoryController(context,
|
||||
KEY_GAME_CONTROLLER_CATEGORY).setChildren(
|
||||
Arrays.asList(gameControllerPreferenceController)));
|
||||
|
||||
return controllers;
|
||||
}
|
||||
|
||||
|
@@ -16,6 +16,9 @@
|
||||
|
||||
package com.android.settings.inputmethod;
|
||||
|
||||
import static com.android.settings.core.BasePreferenceController.AVAILABLE;
|
||||
import static com.android.settings.core.BasePreferenceController.DISABLED_FOR_USER;
|
||||
import static com.android.settings.core.BasePreferenceController.DISABLED_UNSUPPORTED;
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.spy;
|
||||
@@ -37,10 +40,6 @@ import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
public class GameControllerPreferenceControllerTest {
|
||||
|
||||
@@ -57,7 +56,7 @@ public class GameControllerPreferenceControllerTest {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mContext = spy(RuntimeEnvironment.application);
|
||||
when(mContext.getSystemService(Context.INPUT_SERVICE)).thenReturn(mInputManager);
|
||||
mController = new GameControllerPreferenceController(mContext);
|
||||
mController = new GameControllerPreferenceController(mContext, "test_key");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -75,59 +74,59 @@ public class GameControllerPreferenceControllerTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsAvailable_hasDeviceWithVibrator_shouldReturnTrue() {
|
||||
public void getAvailabilityStatus_hasDeviceWithVibrator_shouldReturnAvailable() {
|
||||
when(mInputManager.getInputDeviceIds()).thenReturn(new int[] {1});
|
||||
when(mInputManager.getInputDevice(1)).thenReturn(mInputDevice);
|
||||
when(mInputDevice.isVirtual()).thenReturn(false);
|
||||
when(mInputDevice.getVibrator().hasVibrator()).thenReturn(true);
|
||||
|
||||
assertThat(mController.isAvailable()).isTrue();
|
||||
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsAvailable_hasNoVibratingDevice_shouldReturnFalse() {
|
||||
public void getAvailabilityStatus_hasNoVibratingDevice_shouldReturnDisabled() {
|
||||
when(mInputManager.getInputDeviceIds()).thenReturn(new int[] {1});
|
||||
when(mInputManager.getInputDevice(1)).thenReturn(mInputDevice);
|
||||
when(mInputDevice.isVirtual()).thenReturn(false);
|
||||
when(mInputDevice.getVibrator().hasVibrator()).thenReturn(false);
|
||||
|
||||
assertThat(mController.isAvailable()).isFalse();
|
||||
assertThat(mController.getAvailabilityStatus()).isEqualTo(DISABLED_UNSUPPORTED);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsAvailable_hasNoPhysicalDevice_shouldReturnFalse() {
|
||||
public void getAvailabilityStatus_hasNoPhysicalDevice_shouldReturnDisabled() {
|
||||
when(mInputManager.getInputDeviceIds()).thenReturn(new int[] {1});
|
||||
when(mInputManager.getInputDevice(1)).thenReturn(mInputDevice);
|
||||
when(mInputDevice.isVirtual()).thenReturn(true);
|
||||
|
||||
assertThat(mController.isAvailable()).isFalse();
|
||||
assertThat(mController.getAvailabilityStatus()).isEqualTo(DISABLED_UNSUPPORTED);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsAvailable_hasNoDevice_shouldReturnFalse() {
|
||||
public void getAvailabilityStatus_hasNoDevice_shouldReturnDisabled() {
|
||||
when(mInputManager.getInputDeviceIds()).thenReturn(new int[] {});
|
||||
|
||||
assertThat(mController.isAvailable()).isFalse();
|
||||
assertThat(mController.getAvailabilityStatus()).isEqualTo(DISABLED_UNSUPPORTED);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Config(qualifiers = "mcc999")
|
||||
public void testIsAvailable_ifDisabled_shouldReturnFalse() {
|
||||
mController = new GameControllerPreferenceController(mContext);
|
||||
public void getAvailabilityStatus_ifDisabled_shouldReturnDisabled() {
|
||||
mController = new GameControllerPreferenceController(mContext, "testkey");
|
||||
|
||||
assertThat(mController.isAvailable()).isFalse();
|
||||
assertThat(mController.getAvailabilityStatus()).isEqualTo(DISABLED_UNSUPPORTED);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateNonIndexableKeys_shouldIncludeCategoryAndPrefKeys() {
|
||||
when(mInputManager.getInputDeviceIds()).thenReturn(new int[]{});
|
||||
public void setChecked_toEnabled_shouldSetToSettingsProvider() {
|
||||
mController.setChecked(true);
|
||||
assertThat(mController.isChecked()).isTrue();
|
||||
}
|
||||
|
||||
final List<String> nonIndexables = new ArrayList<>();
|
||||
mController.updateNonIndexableKeys(nonIndexables);
|
||||
|
||||
assertThat(mController.isAvailable()).isFalse();
|
||||
assertThat(nonIndexables).containsExactlyElementsIn(Arrays.asList(
|
||||
GameControllerPreferenceController.PREF_KEY,
|
||||
mController.getPreferenceKey()));
|
||||
@Test
|
||||
public void setChecked_toDisabled_shouldSetToSettingsProvider() {
|
||||
mController.setChecked(true);
|
||||
mController.setChecked(false);
|
||||
assertThat(mController.isChecked()).isFalse();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user