OSDN Git Service

Fix a incorrect class cast in GameControllerPreferenceCtrl
authorFan Zhang <zhfan@google.com>
Thu, 12 Apr 2018 05:14:57 +0000 (13:14 +0800)
committerFan Zhang <zhfan@google.com>
Wed, 25 Apr 2018 16:08:24 +0000 (09:08 -0700)
Also convert the controller to TogglePrefCtrl, and register it in xml.

Change-Id: Ifbc95364b47690117b2875cba1cdc4761ad205be
Merged-In: Ifbc95364b47690117b2875cba1cdc4761ad205be
Fixes: 71972185
Test: make RunSettingsRoboTests

res/xml/language_and_input.xml
src/com/android/settings/inputmethod/GameControllerPreferenceController.java
src/com/android/settings/language/LanguageAndInputSettings.java
tests/robotests/src/com/android/settings/inputmethod/GameControllerPreferenceControllerTest.java

index d7fad7e..747d1b4 100644 (file)
     xmlns:settings="http://schemas.android.com/apk/res-auto"
     android:key="language_and_input_settings_screen"
     android:title="@string/language_settings"
-    settings:initialExpandedChildrenCount="3" >
+    settings:initialExpandedChildrenCount="3">
 
     <Preference
         android:key="phone_language"
         android:title="@string/phone_language"
         android:icon="@drawable/ic_translate_24dp"
-        android:fragment="com.android.settings.localepicker.LocaleListEditor"/>
+        android:fragment="com.android.settings.localepicker.LocaleListEditor" />
 
     <PreferenceCategory
         android:key="keyboards_category"
         <Preference
             android:key="virtual_keyboard_pref"
             android:title="@string/virtual_keyboard_category"
-            android:fragment="com.android.settings.inputmethod.VirtualKeyboardFragment"/>
+            android:fragment="com.android.settings.inputmethod.VirtualKeyboardFragment" />
         <Preference
             android:key="physical_keyboard_pref"
             android:title="@string/physical_keyboard_title"
             android:summary="@string/summary_placeholder"
-            android:fragment="com.android.settings.inputmethod.PhysicalKeyboardFragment"/>
+            android:fragment="com.android.settings.inputmethod.PhysicalKeyboardFragment" />
     </PreferenceCategory>
 
     <PreferenceCategory
@@ -52,7 +52,7 @@
             android:key="spellcheckers_settings"
             android:title="@string/spellcheckers_settings_title"
             android:persistent="false"
-            android:fragment="com.android.settings.inputmethod.SpellCheckersSettings"/>
+            android:fragment="com.android.settings.inputmethod.SpellCheckersSettings" />
 
         <com.android.settings.widget.GearPreference
             android:key="default_autofill"
@@ -63,7 +63,7 @@
         <!-- User dictionary preference title and fragment will be set programmatically. -->
         <Preference
             android:key="key_user_dictionary_settings"
-            android:title="@string/user_dict_settings_title"/>
+            android:title="@string/user_dict_settings_title" />
     </PreferenceCategory>
 
     <PreferenceCategory
         <com.android.settings.PointerSpeedPreference
             android:key="pointer_speed"
             android:title="@string/pointer_speed"
-            android:dialogTitle="@string/pointer_speed"/>
+            android:dialogTitle="@string/pointer_speed" />
 
         <Preference
             android:key="tts_settings_summary"
             android:title="@string/tts_settings_title"
-            android:fragment="com.android.settings.tts.TextToSpeechSettings"/>
+            android:fragment="com.android.settings.tts.TextToSpeechSettings" />
 
     </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>
+    <SwitchPreference
+        android:key="vibrate_input_devices"
+        android:title="@string/vibrate_input_devices"
+        android:summary="@string/vibrate_input_devices_summary"
+        settings:controller="com.android.settings.inputmethod.GameControllerPreferenceController" />
 
 </PreferenceScreen>
index c4e998a..85aef63 100644 (file)
@@ -19,38 +19,27 @@ package com.android.settings.inputmethod;
 import android.content.Context;
 import android.hardware.input.InputManager;
 import android.provider.Settings;
-import android.support.annotation.VisibleForTesting;
-import android.support.v14.preference.SwitchPreference;
 import android.support.v7.preference.Preference;
 import android.support.v7.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 +56,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(
+        mPreference.setVisible(isAvailable());
+    }
+
+    @Override
+    public boolean isChecked() {
+        return Settings.System.getInt(
                 mContext.getContentResolver(),
-                Settings.System.VIBRATE_INPUT_DEVICES, 1) > 0);
+                Settings.System.VIBRATE_INPUT_DEVICES, 1) > 0;
     }
 
     @Override
-    public void updateNonIndexableKeys(List<String> keys) {
-        if (!isAvailable()) {
-            keys.add(CATEGORY_KEY);
-            keys.add(PREF_KEY);
-        }
+    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);
-            }
-        }
+        updateState(mPreference);
     }
 }
index 126f3c8..73cfe27 100644 (file)
@@ -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;
@@ -124,17 +123,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;
     }
 
index bc4eca8..61283fa 100644 (file)
@@ -16,6 +16,8 @@
 
 package com.android.settings.inputmethod;
 
+import static com.android.settings.core.BasePreferenceController.AVAILABLE;
+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 +39,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 +55,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 +73,59 @@ public class GameControllerPreferenceControllerTest {
     }
 
     @Test
-    public void testIsAvailable_hasDeviceWithVibrator_shouldReturnTrue() {
-        when(mInputManager.getInputDeviceIds()).thenReturn(new int[]{1});
+    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() {
-        when(mInputManager.getInputDeviceIds()).thenReturn(new int[]{1});
+    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() {
-        when(mInputManager.getInputDeviceIds()).thenReturn(new int[]{1});
+    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() {
-        when(mInputManager.getInputDeviceIds()).thenReturn(new int[]{});
+    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[]{});
-
-        final List<String> nonIndexables = new ArrayList<>();
-        mController.updateNonIndexableKeys(nonIndexables);
+    public void setChecked_toEnabled_shouldSetToSettingsProvider() {
+        mController.setChecked(true);
+        assertThat(mController.isChecked()).isTrue();
+    }
 
-        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();
     }
 }