From a2754c515bcc94ff4738c98e7e68a1d621660946 Mon Sep 17 00:00:00 2001 From: Yiwei Zhang Date: Fri, 28 Dec 2018 11:38:11 +0800 Subject: [PATCH 1/8] GUP: Add stub UI Bug: 119221883 Test: make RunSettingsRoboTests ROBOTEST_FILTER=GupDashboardTest Change-Id: I1f7cc66790e1d1e1de5113e28a055ec5d1679013 Merged-In: I1f7cc66790e1d1e1de5113e28a055ec5d1679013 --- res/values/strings.xml | 5 ++ res/xml/development_settings.xml | 6 +++ res/xml/gup_settings.xml | 20 +++++++ .../development/gup/GupDashboard.java | 50 +++++++++++++++++ ...randfather_not_implementing_index_provider | 1 + .../development/gup/GupDashboardTest.java | 54 +++++++++++++++++++ 6 files changed, 136 insertions(+) create mode 100644 res/xml/gup_settings.xml create mode 100644 src/com/android/settings/development/gup/GupDashboard.java create mode 100644 tests/robotests/src/com/android/settings/development/gup/GupDashboardTest.java diff --git a/res/values/strings.xml b/res/values/strings.xml index 62c36c2e409..83a55ec5bef 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -9989,6 +9989,11 @@ %1$s + + Game Update Packages Preferences + + Modify Game Update Packages settings + diff --git a/res/xml/development_settings.xml b/res/xml/development_settings.xml index 7a98f13c888..8fea51e0aaa 100644 --- a/res/xml/development_settings.xml +++ b/res/xml/development_settings.xml @@ -197,6 +197,12 @@ android:title="@string/enable_gpu_debug_layers" android:summary="@string/enable_gpu_debug_layers_summary" /> + + + + + diff --git a/src/com/android/settings/development/gup/GupDashboard.java b/src/com/android/settings/development/gup/GupDashboard.java new file mode 100644 index 00000000000..674a0a90d0e --- /dev/null +++ b/src/com/android/settings/development/gup/GupDashboard.java @@ -0,0 +1,50 @@ +/* + * Copyright 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings.development.gup; + +import android.content.Context; + +import com.android.internal.logging.nano.MetricsProto; +import com.android.settings.R; +import com.android.settings.dashboard.DashboardFragment; +import com.android.settingslib.core.AbstractPreferenceController; + +import java.util.List; + +public class GupDashboard extends DashboardFragment { + private static final String TAG = "GupDashboard"; + + @Override + public int getMetricsCategory() { + return MetricsProto.MetricsEvent.SETTINGS_GUP_DASHBOARD; + } + + @Override + protected String getLogTag() { + return TAG; + } + + @Override + protected int getPreferenceScreenResId() { + return R.xml.gup_settings; + } + + @Override + public int getHelpResource() { + return 0; + } +} diff --git a/tests/robotests/assets/grandfather_not_implementing_index_provider b/tests/robotests/assets/grandfather_not_implementing_index_provider index 39e63d4baa7..2c515d7a825 100644 --- a/tests/robotests/assets/grandfather_not_implementing_index_provider +++ b/tests/robotests/assets/grandfather_not_implementing_index_provider @@ -6,6 +6,7 @@ com.android.settings.accounts.AccountDetailDashboardFragment com.android.settings.fuelgauge.PowerUsageAnomalyDetails com.android.settings.fuelgauge.AdvancedPowerUsageDetail com.android.settings.development.featureflags.FeatureFlagsDashboard +com.android.settings.development.gup.GupDashboard com.android.settings.development.qstile.DevelopmentTileConfigFragment com.android.settings.deviceinfo.StorageProfileFragment com.android.settings.notification.ChannelNotificationSettings diff --git a/tests/robotests/src/com/android/settings/development/gup/GupDashboardTest.java b/tests/robotests/src/com/android/settings/development/gup/GupDashboardTest.java new file mode 100644 index 00000000000..17278ef08ea --- /dev/null +++ b/tests/robotests/src/com/android/settings/development/gup/GupDashboardTest.java @@ -0,0 +1,54 @@ +/* + * Copyright 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings.development.gup; + +import static com.google.common.truth.Truth.assertThat; + +import com.android.internal.logging.nano.MetricsProto; +import com.android.settings.R; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; + +@RunWith(RobolectricTestRunner.class) +public class GupDashboardTest { + private GupDashboard mDashboard; + + @Before + public void setUp() { + mDashboard = new GupDashboard(); + } + + @Test + public void getHelpResource_shouldReturn0() { + assertThat(mDashboard.getHelpResource()).isEqualTo(0); + } + + @Test + public void getMetricesCategory_shouldReturnGupDashboard() { + assertThat(mDashboard.getMetricsCategory()) + .isEqualTo(MetricsProto.MetricsEvent.SETTINGS_GUP_DASHBOARD); + } + + @Test + public void getPreferenceScreen_shouldReturnGupSettings() { + assertThat(mDashboard.getPreferenceScreenResId()) + .isEqualTo(R.xml.gup_settings); + } +} From dc62028e85a7142755e2fd5641d46c7b26862826 Mon Sep 17 00:00:00 2001 From: Yiwei Zhang Date: Sat, 29 Dec 2018 08:52:31 +0800 Subject: [PATCH 2/8] GUP: Display a list of Apps and dialogs 1) Add preference controller for gup dashboard and add list preferences for each entry of the apps 2) Add gup dashboard page to search index 3) Add comprehensive tests for GupPreferenceController Bug: 119221883 Test: make RunSettingsRoboTests Change-Id: Ide4934c0dd3901532723e77e74663e5a7b639026 Merged-In: Ide4934c0dd3901532723e77e74663e5a7b639026 --- res/values/strings.xml | 21 +- res/xml/development_settings.xml | 5 - res/xml/gup_settings.xml | 12 +- ...evelopmentOptionsActivityRequestCodes.java | 2 - .../DevelopmentSettingsDashboardFragment.java | 1 - ...tePackageDevOptInPreferenceController.java | 121 ---------- .../gup/GupPreferenceController.java | 206 +++++++++++++++++ ...ckageDevOptInPreferenceControllerTest.java | 135 ----------- .../gup/GupPreferenceControllerTest.java | 213 ++++++++++++++++++ 9 files changed, 444 insertions(+), 272 deletions(-) delete mode 100644 src/com/android/settings/development/GameUpdatePackageDevOptInPreferenceController.java create mode 100644 src/com/android/settings/development/gup/GupPreferenceController.java delete mode 100644 tests/robotests/src/com/android/settings/development/GameUpdatePackageDevOptInPreferenceControllerTest.java create mode 100644 tests/robotests/src/com/android/settings/development/gup/GupPreferenceControllerTest.java diff --git a/res/values/strings.xml b/res/values/strings.xml index 83a55ec5bef..915cd09df65 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -9982,17 +9982,24 @@ show both names, with the directory name wrapped in parenthesis --> %1$s (%2$s) - - Use Game Update Package - - No selected app - - %1$s - Game Update Packages Preferences Modify Game Update Packages settings + + Select Graphics Driver + + Default + + Game Update Packages + + Native Graphics Driver + + + @string/gup_app_preference_default + @string/gup_app_preference_gup + @string/gup_app_preference_native + diff --git a/res/xml/development_settings.xml b/res/xml/development_settings.xml index 8fea51e0aaa..b1044bb1e1c 100644 --- a/res/xml/development_settings.xml +++ b/res/xml/development_settings.xml @@ -431,11 +431,6 @@ android:summary="%s" android:title="@string/simulate_color_space" /> - - + xmlns:settings="http://schemas.android.com/apk/res-auto" + android:key="gup_settings" + android:title="@string/gup_dashboard_title"> + + + + + diff --git a/src/com/android/settings/development/DevelopmentOptionsActivityRequestCodes.java b/src/com/android/settings/development/DevelopmentOptionsActivityRequestCodes.java index a7bce1451f4..b7b27591df9 100644 --- a/src/com/android/settings/development/DevelopmentOptionsActivityRequestCodes.java +++ b/src/com/android/settings/development/DevelopmentOptionsActivityRequestCodes.java @@ -25,6 +25,4 @@ public interface DevelopmentOptionsActivityRequestCodes { int REQUEST_CODE_DEBUG_APP = 1; int REQUEST_MOCK_LOCATION_APP = 2; - - int REQUEST_CODE_GUP_DEV_OPT_IN_APPS = 6; } diff --git a/src/com/android/settings/development/DevelopmentSettingsDashboardFragment.java b/src/com/android/settings/development/DevelopmentSettingsDashboardFragment.java index 575e8fe1ca2..8518c7446eb 100644 --- a/src/com/android/settings/development/DevelopmentSettingsDashboardFragment.java +++ b/src/com/android/settings/development/DevelopmentSettingsDashboardFragment.java @@ -400,7 +400,6 @@ public class DevelopmentSettingsDashboardFragment extends RestrictedDashboardFra controllers.add(new SelectDebugAppPreferenceController(context, fragment)); controllers.add(new WaitForDebuggerPreferenceController(context)); controllers.add(new EnableGpuDebugLayersPreferenceController(context)); - controllers.add(new GameUpdatePackageDevOptInPreferenceController(context, fragment)); controllers.add(new VerifyAppsOverUsbPreferenceController(context)); controllers.add(new LogdSizePreferenceController(context)); controllers.add(new LogPersistPreferenceController(context, fragment, lifecycle)); diff --git a/src/com/android/settings/development/GameUpdatePackageDevOptInPreferenceController.java b/src/com/android/settings/development/GameUpdatePackageDevOptInPreferenceController.java deleted file mode 100644 index be2c7a5dac8..00000000000 --- a/src/com/android/settings/development/GameUpdatePackageDevOptInPreferenceController.java +++ /dev/null @@ -1,121 +0,0 @@ -/* - * Copyright 2018 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.settings.development; - -import static com.android.settings.development.DevelopmentOptionsActivityRequestCodes - .REQUEST_CODE_GUP_DEV_OPT_IN_APPS; - -import android.app.Activity; -import android.content.Context; -import android.content.Intent; -import android.content.pm.ApplicationInfo; -import android.content.pm.PackageManager; -import android.provider.Settings; -import androidx.annotation.VisibleForTesting; -import androidx.preference.Preference; - -import com.android.settings.R; -import com.android.settings.core.PreferenceControllerMixin; -import com.android.settingslib.development.DeveloperOptionsPreferenceController; - -// TODO(b/119221883): Need to override isAvailable() to return false when updatable graphics driver is not supported. -public class GameUpdatePackageDevOptInPreferenceController - extends DeveloperOptionsPreferenceController - implements PreferenceControllerMixin, OnActivityResultListener { - - private static final String GUP_DEV_OPT_IN_APP_KEY = "gup_dev_opt_in_app"; - - private final DevelopmentSettingsDashboardFragment mFragment; - private final PackageManager mPackageManager; - - public GameUpdatePackageDevOptInPreferenceController(Context context, - DevelopmentSettingsDashboardFragment fragment) { - super(context); - mFragment = fragment; - mPackageManager = mContext.getPackageManager(); - } - - @Override - public String getPreferenceKey() { - return GUP_DEV_OPT_IN_APP_KEY; - } - - @Override - public boolean handlePreferenceTreeClick(Preference preference) { - if (GUP_DEV_OPT_IN_APP_KEY.equals(preference.getKey())) { - // pass it on to settings - final Intent intent = getActivityStartIntent(); - mFragment.startActivityForResult(intent, REQUEST_CODE_GUP_DEV_OPT_IN_APPS); - return true; - } - return false; - } - - @Override - public void updateState(Preference preference) { - updatePreferenceSummary(); - } - - @Override - public boolean onActivityResult(int requestCode, int resultCode, Intent data) { - if (requestCode != REQUEST_CODE_GUP_DEV_OPT_IN_APPS - || resultCode != Activity.RESULT_OK) { - return false; - } - Settings.Global.putString(mContext.getContentResolver(), - Settings.Global.GUP_DEV_OPT_IN_APPS, data.getAction()); - updatePreferenceSummary(); - return true; - } - - @Override - protected void onDeveloperOptionsSwitchDisabled() { - super.onDeveloperOptionsSwitchDisabled(); - mPreference.setSummary(mContext.getResources().getString( - R.string.gup_dev_opt_in_app_not_set)); - } - - @VisibleForTesting - Intent getActivityStartIntent() { - Intent intent = new Intent(mContext, AppPicker.class); - intent.putExtra(AppPicker.EXTRA_DEBUGGABLE, true /* value */); - return intent; - } - - private void updatePreferenceSummary() { - final String optInApp = Settings.Global.getString( - mContext.getContentResolver(), Settings.Global.GUP_DEV_OPT_IN_APPS); - if (optInApp != null && !optInApp.isEmpty()) { - mPreference.setSummary(mContext.getResources().getString( - R.string.gup_dev_opt_in_app_set, getAppLabel(optInApp))); - } else { - mPreference.setSummary(mContext.getResources().getString( - R.string.gup_dev_opt_in_app_not_set)); - } - } - - private String getAppLabel(String applicationPackageName) { - try { - final ApplicationInfo ai = mPackageManager.getApplicationInfo(applicationPackageName, - PackageManager.GET_DISABLED_COMPONENTS); - final CharSequence lab = mPackageManager.getApplicationLabel(ai); - return lab != null ? lab.toString() : applicationPackageName; - } catch (PackageManager.NameNotFoundException e) { - return applicationPackageName; - } - } -} diff --git a/src/com/android/settings/development/gup/GupPreferenceController.java b/src/com/android/settings/development/gup/GupPreferenceController.java new file mode 100644 index 00000000000..762314474ef --- /dev/null +++ b/src/com/android/settings/development/gup/GupPreferenceController.java @@ -0,0 +1,206 @@ +/* + * Copyright 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings.development.gup; + +import android.content.ContentResolver; +import android.content.Context; +import android.content.pm.ApplicationInfo; +import android.content.pm.PackageManager; +import android.content.res.Resources; +import android.provider.Settings; + +import androidx.annotation.VisibleForTesting; +import androidx.preference.ListPreference; +import androidx.preference.Preference; +import androidx.preference.PreferenceGroup; +import androidx.preference.PreferenceScreen; + +import com.android.settings.R; +import com.android.settings.core.BasePreferenceController; +import com.android.settingslib.development.DevelopmentSettingsEnabler; + +import java.text.Collator; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class GupPreferenceController + extends BasePreferenceController implements Preference.OnPreferenceChangeListener { + private final CharSequence[] mEntryList; + private final String mPreferenceTitle; + private final String mPreferenceDefault; + private final String mPreferenceGup; + private final String mPreferenceNative; + + private final List mAppInfos; + private final Set mDevOptInApps; + private final Set mDevOptOutApps; + + public GupPreferenceController(Context context, String key) { + super(context, key); + + final Resources resources = context.getResources(); + mEntryList = resources.getStringArray(R.array.gup_app_preference_values); + mPreferenceTitle = resources.getString(R.string.gup_app_preference_title); + mPreferenceDefault = resources.getString(R.string.gup_app_preference_default); + mPreferenceGup = resources.getString(R.string.gup_app_preference_gup); + mPreferenceNative = resources.getString(R.string.gup_app_preference_native); + + // TODO: Move this task to background if there's potential ANR/Jank. + // Update the UI when all the app infos are ready. + mAppInfos = getAppInfos(context); + + final ContentResolver contentResolver = context.getContentResolver(); + mDevOptInApps = + getGlobalSettingsString(contentResolver, Settings.Global.GUP_DEV_OPT_IN_APPS); + mDevOptOutApps = + getGlobalSettingsString(contentResolver, Settings.Global.GUP_DEV_OPT_OUT_APPS); + } + + @Override + public int getAvailabilityStatus() { + return DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(mContext) + ? AVAILABLE + : DISABLED_DEPENDENT_SETTING; + } + + @Override + public void displayPreference(PreferenceScreen screen) { + super.displayPreference(screen); + final PreferenceGroup preferenceGroup = + (PreferenceGroup) screen.findPreference(getPreferenceKey()); + if (preferenceGroup == null) { + return; + } + + for (AppInfo appInfo : mAppInfos) { + preferenceGroup.addPreference( + createListPreference(appInfo.info.packageName, appInfo.label)); + } + } + + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + final ListPreference listPref = (ListPreference) preference; + final String value = newValue.toString(); + final String packageName = preference.getKey(); + + // When user choose a new preference, update both Sets for + // opt-in and opt-out apps. Then set the new summary text. + if (value.equals(mPreferenceNative)) { + mDevOptInApps.remove(packageName); + mDevOptOutApps.add(packageName); + listPref.setSummary(mPreferenceNative); + } else if (value.equals(mPreferenceGup)) { + mDevOptInApps.add(packageName); + mDevOptOutApps.remove(packageName); + listPref.setSummary(mPreferenceGup); + } else { + mDevOptInApps.remove(packageName); + mDevOptOutApps.remove(packageName); + listPref.setSummary(mPreferenceDefault); + } + + // Push the updated Sets for opt-in and opt-out apps to + // corresponding Settings.Global.GUP_DEV_OPT_(IN|OUT)_APPS + Settings.Global.putString(mContext.getContentResolver(), + Settings.Global.GUP_DEV_OPT_IN_APPS, String.join(",", mDevOptInApps)); + Settings.Global.putString(mContext.getContentResolver(), + Settings.Global.GUP_DEV_OPT_OUT_APPS, String.join(",", mDevOptOutApps)); + + return true; + } + + // AppInfo class to achieve loading the application label only once + class AppInfo { + AppInfo(PackageManager packageManager, ApplicationInfo applicationInfo) { + info = applicationInfo; + label = packageManager.getApplicationLabel(applicationInfo).toString(); + } + final ApplicationInfo info; + final String label; + } + + // List of non-system packages that are installed for the current user. + private List getAppInfos(Context context) { + final PackageManager packageManager = context.getPackageManager(); + final List applicationInfos = + packageManager.getInstalledApplications(0 /* flags */); + + final List appInfos = new ArrayList<>(); + for (ApplicationInfo applicationInfo : applicationInfos) { + if ((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) { + appInfos.add(new AppInfo(packageManager, applicationInfo)); + } + } + + Collections.sort(appInfos, appInfoComparator); + + return appInfos; + } + + // Parse the raw comma separated package names into a String Set + private Set getGlobalSettingsString(ContentResolver contentResolver, String name) { + final String settingsValue = Settings.Global.getString(contentResolver, name); + if (settingsValue == null) { + return new HashSet<>(); + } + + final Set valueSet = new HashSet<>(Arrays.asList(settingsValue.split(","))); + valueSet.remove(""); + + return valueSet; + } + + private final Comparator appInfoComparator = new Comparator() { + public final int compare(AppInfo a, AppInfo b) { + return Collator.getInstance().compare(a.label, b.label); + } + }; + + @VisibleForTesting + protected ListPreference createListPreference(String packageName, String appName) { + final ListPreference listPreference = new ListPreference(mContext); + + listPreference.setKey(packageName); + listPreference.setTitle(appName); + listPreference.setDialogTitle(mPreferenceTitle); + listPreference.setEntries(mEntryList); + listPreference.setEntryValues(mEntryList); + + // Initialize preference default and summary with the opt in/out choices + // from Settings.Global.GUP_DEV_OPT_(IN|OUT)_APPS + if (mDevOptOutApps.contains(packageName)) { + listPreference.setValue(mPreferenceNative); + listPreference.setSummary(mPreferenceNative); + } else if (mDevOptInApps.contains(packageName)) { + listPreference.setValue(mPreferenceGup); + listPreference.setSummary(mPreferenceGup); + } else { + listPreference.setValue(mPreferenceDefault); + listPreference.setSummary(mPreferenceDefault); + } + + listPreference.setOnPreferenceChangeListener(this); + + return listPreference; + } +} diff --git a/tests/robotests/src/com/android/settings/development/GameUpdatePackageDevOptInPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/development/GameUpdatePackageDevOptInPreferenceControllerTest.java deleted file mode 100644 index 84fa525d70b..00000000000 --- a/tests/robotests/src/com/android/settings/development/GameUpdatePackageDevOptInPreferenceControllerTest.java +++ /dev/null @@ -1,135 +0,0 @@ -/* - * Copyright 2018 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.settings.development; - -import static com.android.settings.development.DevelopmentOptionsActivityRequestCodes.REQUEST_CODE_GUP_DEV_OPT_IN_APPS; -import static com.google.common.truth.Truth.assertThat; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import android.app.Activity; -import android.content.ContentResolver; -import android.content.Context; -import android.content.Intent; -import android.content.pm.PackageManager; -import android.provider.Settings; - -import androidx.preference.Preference; -import androidx.preference.PreferenceScreen; - -import com.android.settings.R; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import org.robolectric.RobolectricTestRunner; -import org.robolectric.RuntimeEnvironment; -import org.robolectric.util.ReflectionHelpers; - -@RunWith(RobolectricTestRunner.class) -public class GameUpdatePackageDevOptInPreferenceControllerTest { - - @Mock - private PreferenceScreen mPreferenceScreen; - @Mock - private DevelopmentSettingsDashboardFragment mFragment; - - private Context mContext; - private Preference mPreference; - private GameUpdatePackageDevOptInPreferenceController mController; - - @Before - public void setup() { - MockitoAnnotations.initMocks(this); - mContext = RuntimeEnvironment.application; - mController = spy(new GameUpdatePackageDevOptInPreferenceController(mContext, mFragment)); - mPreference = new Preference(mContext); - mPreference.setKey(mController.getPreferenceKey()); - - when(mPreferenceScreen.findPreference(mController.getPreferenceKey())) - .thenReturn(mPreference); - mController.displayPreference(mPreferenceScreen); - } - - @Test - public void handlePreferenceTreeClick_preferenceClicked_launchActivity() { - final Intent activityStartIntent = new Intent(mContext, AppPicker.class); - final String preferenceKey = mController.getPreferenceKey(); - doReturn(activityStartIntent).when(mController).getActivityStartIntent(); - mController.handlePreferenceTreeClick(mPreference); - - verify(mFragment).startActivityForResult(activityStartIntent, - REQUEST_CODE_GUP_DEV_OPT_IN_APPS); - } - - @Test - public void updateState_foobarAppSelected_shouldUpdateSummaryWithGUPDevOptInAppLabel() { - final String selectedApp = "foobar"; - final ContentResolver contentResolver = mContext.getContentResolver(); - Settings.Global.putString(contentResolver, - Settings.Global.GUP_DEV_OPT_IN_APPS, selectedApp); - mController.updateState(mPreference); - - assertThat(mPreference.getSummary()).isEqualTo( - mContext.getString(R.string.gup_dev_opt_in_app_set, selectedApp)); - } - - @Test - public void updateState_noAppSelected_shouldUpdateSummaryWithNoAppSelected() { - final String selectedApp = null; - final ContentResolver contentResolver = mContext.getContentResolver(); - Settings.Global.putString(contentResolver, - Settings.Global.GUP_DEV_OPT_IN_APPS, selectedApp); - mController.updateState(mPreference); - - assertThat(mPreference.getSummary()).isEqualTo( - mContext.getString(R.string.gup_dev_opt_in_app_not_set)); - } - - @Test - public void onActivityResult_foobarAppSelected_shouldUpdateSummaryWithGUPDevOptInLabel() { - Intent activityResultIntent = new Intent(mContext, AppPicker.class); - final String appLabel = "foobar"; - activityResultIntent.setAction(appLabel); - final boolean result = mController - .onActivityResult(REQUEST_CODE_GUP_DEV_OPT_IN_APPS, Activity.RESULT_OK, - activityResultIntent); - - assertThat(result).isTrue(); - assertThat(mPreference.getSummary()).isEqualTo( - mContext.getString(R.string.gup_dev_opt_in_app_set, appLabel)); - } - - @Test - public void onActivityResult_badRequestCode_shouldReturnFalse() { - assertThat(mController.onActivityResult( - -1 /* requestCode */, -1 /* resultCode */, null /* intent */)).isFalse(); - } - - @Test - public void onDeveloperOptionsSwitchDisabled_shouldDisablePreference() { - mController.onDeveloperOptionsSwitchDisabled(); - - assertThat(mPreference.isEnabled()).isFalse(); - assertThat(mPreference.getSummary()).isEqualTo( - mContext.getString(R.string.gup_dev_opt_in_app_not_set)); - } -} diff --git a/tests/robotests/src/com/android/settings/development/gup/GupPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/development/gup/GupPreferenceControllerTest.java new file mode 100644 index 00000000000..eedffa5f5a5 --- /dev/null +++ b/tests/robotests/src/com/android/settings/development/gup/GupPreferenceControllerTest.java @@ -0,0 +1,213 @@ +/* + * Copyright 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings.development.gup; + +import static com.android.settings.testutils.ApplicationTestUtils.buildInfo; +import static com.google.common.truth.Truth.assertThat; + +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; + +import android.content.ContentResolver; +import android.content.Context; +import android.content.pm.ApplicationInfo; +import android.content.pm.PackageManager; +import android.provider.Settings; + +import androidx.preference.ListPreference; +import androidx.preference.PreferenceCategory; +import androidx.preference.PreferenceGroup; +import androidx.preference.PreferenceManager; +import androidx.preference.PreferenceScreen; + +import com.android.settings.R; + +import java.util.Arrays; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.RuntimeEnvironment; + +@RunWith(RobolectricTestRunner.class) +public class GupPreferenceControllerTest { + private static final int DEFAULT = 0; + private static final int GUP = 1; + private static final int NATIVE = 2; + private static final String TEST_APP_NAME = "testApp"; + private static final String TEST_PKG_NAME = "testPkg"; + + // Pre-installed Apps in the Mock PackageManager + private static final String APP_1 = "app1"; + private static final String APP_2 = "app2"; + private static final String APP_3 = "app3"; + + @Mock + private PackageManager mPackageManager; + @Mock + private PreferenceScreen mScreen; + + private Context mContext; + private PreferenceGroup mGroup; + private PreferenceManager mPreferenceManager; + private ContentResolver mResolver; + private GupPreferenceController mController; + private CharSequence[] mValueList; + private String mDialogTitle; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + mContext = spy(RuntimeEnvironment.application); + mResolver = mContext.getContentResolver(); + mValueList = mContext.getResources().getStringArray(R.array.gup_app_preference_values); + mDialogTitle = mContext.getResources().getString(R.string.gup_app_preference_title); + } + + @Test + public void displayPreference_shouldAddTwoPreferencesAndSortAscendingly() { + mockPackageManager(); + loadDefaultConfig(); + + // Only non-system app has preference + assertThat(mGroup.getPreferenceCount()).isEqualTo(2); + assertThat(mGroup.getPreference(0).getKey()).isEqualTo(APP_1); + assertThat(mGroup.getPreference(1).getKey()).isEqualTo(APP_3); + } + + @Test + public void createPreference_configDefault_shouldSetDefaultAttributes() { + loadDefaultConfig(); + final ListPreference preference = + mController.createListPreference(TEST_PKG_NAME, TEST_APP_NAME); + + assertThat(preference.getKey()).isEqualTo(TEST_PKG_NAME); + assertThat(preference.getTitle()).isEqualTo(TEST_APP_NAME); + assertThat(preference.getDialogTitle()).isEqualTo(mDialogTitle); + assertThat(preference.getEntries()).isEqualTo(mValueList); + assertThat(preference.getEntryValues()).isEqualTo(mValueList); + assertThat(preference.getEntry()).isEqualTo(mValueList[DEFAULT]); + assertThat(preference.getValue()).isEqualTo(mValueList[DEFAULT]); + assertThat(preference.getSummary()).isEqualTo(mValueList[DEFAULT]); + } + + @Test + public void createPreference_configGup_shouldSetGupAttributes() { + loadConfig(TEST_PKG_NAME, ""); + final ListPreference preference = + mController.createListPreference(TEST_PKG_NAME, TEST_APP_NAME); + + assertThat(preference.getKey()).isEqualTo(TEST_PKG_NAME); + assertThat(preference.getTitle()).isEqualTo(TEST_APP_NAME); + assertThat(preference.getDialogTitle()).isEqualTo(mDialogTitle); + assertThat(preference.getEntries()).isEqualTo(mValueList); + assertThat(preference.getEntryValues()).isEqualTo(mValueList); + assertThat(preference.getEntry()).isEqualTo(mValueList[GUP]); + assertThat(preference.getValue()).isEqualTo(mValueList[GUP]); + assertThat(preference.getSummary()).isEqualTo(mValueList[GUP]); + } + + @Test + public void createPreference_configNative_shouldSetNativeAttributes() { + loadConfig("", TEST_PKG_NAME); + final ListPreference preference = + mController.createListPreference(TEST_PKG_NAME, TEST_APP_NAME); + + assertThat(preference.getKey()).isEqualTo(TEST_PKG_NAME); + assertThat(preference.getTitle()).isEqualTo(TEST_APP_NAME); + assertThat(preference.getDialogTitle()).isEqualTo(mDialogTitle); + assertThat(preference.getEntries()).isEqualTo(mValueList); + assertThat(preference.getEntryValues()).isEqualTo(mValueList); + assertThat(preference.getEntry()).isEqualTo(mValueList[NATIVE]); + assertThat(preference.getValue()).isEqualTo(mValueList[NATIVE]); + assertThat(preference.getSummary()).isEqualTo(mValueList[NATIVE]); + } + + @Test + public void onPreferenceChange_selectDefault_shouldUpdateAttributesAndSettingsGlobal() { + loadDefaultConfig(); + final ListPreference preference = + mController.createListPreference(TEST_PKG_NAME, TEST_APP_NAME); + mController.onPreferenceChange(preference, mValueList[DEFAULT]); + + assertThat(preference.getSummary()).isEqualTo(mValueList[DEFAULT]); + assertThat(Settings.Global.getString(mResolver, Settings.Global.GUP_DEV_OPT_IN_APPS)) + .isEqualTo(""); + assertThat(Settings.Global.getString(mResolver, Settings.Global.GUP_DEV_OPT_OUT_APPS)) + .isEqualTo(""); + } + + @Test + public void onPreferenceChange_selectGup_shouldUpdateAttributesAndSettingsGlobal() { + loadDefaultConfig(); + final ListPreference preference = + mController.createListPreference(TEST_PKG_NAME, TEST_APP_NAME); + mController.onPreferenceChange(preference, mValueList[GUP]); + + assertThat(preference.getSummary()).isEqualTo(mValueList[GUP]); + assertThat(Settings.Global.getString(mResolver, Settings.Global.GUP_DEV_OPT_IN_APPS)) + .isEqualTo(TEST_PKG_NAME); + assertThat(Settings.Global.getString(mResolver, Settings.Global.GUP_DEV_OPT_OUT_APPS)) + .isEqualTo(""); + } + + @Test + public void onPreferenceChange_selectNative_shouldUpdateAttributesAndSettingsGlobal() { + loadDefaultConfig(); + final ListPreference preference = + mController.createListPreference(TEST_PKG_NAME, TEST_APP_NAME); + mController.onPreferenceChange(preference, mValueList[NATIVE]); + + assertThat(preference.getSummary()).isEqualTo(mValueList[NATIVE]); + assertThat(Settings.Global.getString(mResolver, Settings.Global.GUP_DEV_OPT_IN_APPS)) + .isEqualTo(""); + assertThat(Settings.Global.getString(mResolver, Settings.Global.GUP_DEV_OPT_OUT_APPS)) + .isEqualTo(TEST_PKG_NAME); + } + + private void mockPackageManager() { + final int uid = mContext.getUserId(); + final ApplicationInfo app1 = buildInfo(uid, APP_1, 0 /* flags */, 0 /* targetSdkVersion */); + final ApplicationInfo app2 = + buildInfo(uid, APP_2, ApplicationInfo.FLAG_SYSTEM, 0 /* targetSdkVersion */); + final ApplicationInfo app3 = buildInfo(uid, APP_3, 0 /* flags */, 0 /* targetSdkVersion */); + + when(mPackageManager.getInstalledApplications(0 /* flags */)) + .thenReturn(Arrays.asList(app3, app2, app1)); + when(mPackageManager.getApplicationLabel(app1)).thenReturn(APP_1); + when(mPackageManager.getApplicationLabel(app2)).thenReturn(APP_2); + when(mPackageManager.getApplicationLabel(app3)).thenReturn(APP_3); + when(mContext.getPackageManager()).thenReturn(mPackageManager); + } + + private void loadDefaultConfig() { loadConfig("", ""); } + + private void loadConfig(String optIn, String optOut) { + Settings.Global.putString(mResolver, Settings.Global.GUP_DEV_OPT_IN_APPS, optIn); + Settings.Global.putString(mResolver, Settings.Global.GUP_DEV_OPT_OUT_APPS, optOut); + + mController = new GupPreferenceController(mContext, "testKey"); + mGroup = spy(new PreferenceCategory(mContext)); + final PreferenceManager preferenceManager = new PreferenceManager(mContext); + when(mGroup.getPreferenceManager()).thenReturn(preferenceManager); + when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mGroup); + mController.displayPreference(mScreen); + } +} From d0bbbfeeea621b16cf2fa593458c6230390f69a0 Mon Sep 17 00:00:00 2001 From: Yiwei Zhang Date: Fri, 11 Jan 2019 04:40:04 +0800 Subject: [PATCH 3/8] GUP: Fixed some typos and update some values Bug: 119221883 Test: make RunSettingsRoboTests Change-Id: I7b1e42cd3a823a72bcd6d61b26c4954c237ff6d0 Merged-In: I7b1e42cd3a823a72bcd6d61b26c4954c237ff6d0 --- res/values/strings.xml | 24 +++++++++---------- .../gup/GupPreferenceController.java | 15 ++++++------ .../gup/GupPreferenceControllerTest.java | 22 ++++++++++------- 3 files changed, 33 insertions(+), 28 deletions(-) diff --git a/res/values/strings.xml b/res/values/strings.xml index 915cd09df65..73f011b6c0c 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -9982,23 +9982,23 @@ show both names, with the directory name wrapped in parenthesis --> %1$s (%2$s) - - Game Update Packages Preferences - - Modify Game Update Packages settings - + + Game Update Package Preferences + + Modify Game Update Package settings + Select Graphics Driver - + Default - - Game Update Packages - - Native Graphics Driver - + + Game Update Package + + System Graphics Driver + @string/gup_app_preference_default @string/gup_app_preference_gup - @string/gup_app_preference_native + @string/gup_app_preference_system diff --git a/src/com/android/settings/development/gup/GupPreferenceController.java b/src/com/android/settings/development/gup/GupPreferenceController.java index 762314474ef..d4cd2f1284e 100644 --- a/src/com/android/settings/development/gup/GupPreferenceController.java +++ b/src/com/android/settings/development/gup/GupPreferenceController.java @@ -48,7 +48,7 @@ public class GupPreferenceController private final String mPreferenceTitle; private final String mPreferenceDefault; private final String mPreferenceGup; - private final String mPreferenceNative; + private final String mPreferenceSystem; private final List mAppInfos; private final Set mDevOptInApps; @@ -62,7 +62,7 @@ public class GupPreferenceController mPreferenceTitle = resources.getString(R.string.gup_app_preference_title); mPreferenceDefault = resources.getString(R.string.gup_app_preference_default); mPreferenceGup = resources.getString(R.string.gup_app_preference_gup); - mPreferenceNative = resources.getString(R.string.gup_app_preference_native); + mPreferenceSystem = resources.getString(R.string.gup_app_preference_system); // TODO: Move this task to background if there's potential ANR/Jank. // Update the UI when all the app infos are ready. @@ -105,19 +105,18 @@ public class GupPreferenceController // When user choose a new preference, update both Sets for // opt-in and opt-out apps. Then set the new summary text. - if (value.equals(mPreferenceNative)) { + if (value.equals(mPreferenceSystem)) { mDevOptInApps.remove(packageName); mDevOptOutApps.add(packageName); - listPref.setSummary(mPreferenceNative); } else if (value.equals(mPreferenceGup)) { mDevOptInApps.add(packageName); mDevOptOutApps.remove(packageName); - listPref.setSummary(mPreferenceGup); } else { mDevOptInApps.remove(packageName); mDevOptOutApps.remove(packageName); - listPref.setSummary(mPreferenceDefault); } + listPref.setValue(value); + listPref.setSummary(value); // Push the updated Sets for opt-in and opt-out apps to // corresponding Settings.Global.GUP_DEV_OPT_(IN|OUT)_APPS @@ -189,8 +188,8 @@ public class GupPreferenceController // Initialize preference default and summary with the opt in/out choices // from Settings.Global.GUP_DEV_OPT_(IN|OUT)_APPS if (mDevOptOutApps.contains(packageName)) { - listPreference.setValue(mPreferenceNative); - listPreference.setSummary(mPreferenceNative); + listPreference.setValue(mPreferenceSystem); + listPreference.setSummary(mPreferenceSystem); } else if (mDevOptInApps.contains(packageName)) { listPreference.setValue(mPreferenceGup); listPreference.setSummary(mPreferenceGup); diff --git a/tests/robotests/src/com/android/settings/development/gup/GupPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/development/gup/GupPreferenceControllerTest.java index eedffa5f5a5..0ac14ef007c 100644 --- a/tests/robotests/src/com/android/settings/development/gup/GupPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/development/gup/GupPreferenceControllerTest.java @@ -50,7 +50,7 @@ import org.robolectric.RuntimeEnvironment; public class GupPreferenceControllerTest { private static final int DEFAULT = 0; private static final int GUP = 1; - private static final int NATIVE = 2; + private static final int SYSTEM = 2; private static final String TEST_APP_NAME = "testApp"; private static final String TEST_PKG_NAME = "testPkg"; @@ -125,7 +125,7 @@ public class GupPreferenceControllerTest { } @Test - public void createPreference_configNative_shouldSetNativeAttributes() { + public void createPreference_configSystem_shouldSetSystemAttributes() { loadConfig("", TEST_PKG_NAME); final ListPreference preference = mController.createListPreference(TEST_PKG_NAME, TEST_APP_NAME); @@ -135,9 +135,9 @@ public class GupPreferenceControllerTest { assertThat(preference.getDialogTitle()).isEqualTo(mDialogTitle); assertThat(preference.getEntries()).isEqualTo(mValueList); assertThat(preference.getEntryValues()).isEqualTo(mValueList); - assertThat(preference.getEntry()).isEqualTo(mValueList[NATIVE]); - assertThat(preference.getValue()).isEqualTo(mValueList[NATIVE]); - assertThat(preference.getSummary()).isEqualTo(mValueList[NATIVE]); + assertThat(preference.getEntry()).isEqualTo(mValueList[SYSTEM]); + assertThat(preference.getValue()).isEqualTo(mValueList[SYSTEM]); + assertThat(preference.getSummary()).isEqualTo(mValueList[SYSTEM]); } @Test @@ -147,6 +147,8 @@ public class GupPreferenceControllerTest { mController.createListPreference(TEST_PKG_NAME, TEST_APP_NAME); mController.onPreferenceChange(preference, mValueList[DEFAULT]); + assertThat(preference.getEntry()).isEqualTo(mValueList[DEFAULT]); + assertThat(preference.getValue()).isEqualTo(mValueList[DEFAULT]); assertThat(preference.getSummary()).isEqualTo(mValueList[DEFAULT]); assertThat(Settings.Global.getString(mResolver, Settings.Global.GUP_DEV_OPT_IN_APPS)) .isEqualTo(""); @@ -161,6 +163,8 @@ public class GupPreferenceControllerTest { mController.createListPreference(TEST_PKG_NAME, TEST_APP_NAME); mController.onPreferenceChange(preference, mValueList[GUP]); + assertThat(preference.getEntry()).isEqualTo(mValueList[GUP]); + assertThat(preference.getValue()).isEqualTo(mValueList[GUP]); assertThat(preference.getSummary()).isEqualTo(mValueList[GUP]); assertThat(Settings.Global.getString(mResolver, Settings.Global.GUP_DEV_OPT_IN_APPS)) .isEqualTo(TEST_PKG_NAME); @@ -169,13 +173,15 @@ public class GupPreferenceControllerTest { } @Test - public void onPreferenceChange_selectNative_shouldUpdateAttributesAndSettingsGlobal() { + public void onPreferenceChange_selectSystem_shouldUpdateAttributesAndSettingsGlobal() { loadDefaultConfig(); final ListPreference preference = mController.createListPreference(TEST_PKG_NAME, TEST_APP_NAME); - mController.onPreferenceChange(preference, mValueList[NATIVE]); + mController.onPreferenceChange(preference, mValueList[SYSTEM]); - assertThat(preference.getSummary()).isEqualTo(mValueList[NATIVE]); + assertThat(preference.getEntry()).isEqualTo(mValueList[SYSTEM]); + assertThat(preference.getValue()).isEqualTo(mValueList[SYSTEM]); + assertThat(preference.getSummary()).isEqualTo(mValueList[SYSTEM]); assertThat(Settings.Global.getString(mResolver, Settings.Global.GUP_DEV_OPT_IN_APPS)) .isEqualTo(""); assertThat(Settings.Global.getString(mResolver, Settings.Global.GUP_DEV_OPT_OUT_APPS)) From 75548c4a3336c71c929d189a527b51e60d2db32b Mon Sep 17 00:00:00 2001 From: Yiwei Zhang Date: Fri, 11 Jan 2019 08:50:25 +0800 Subject: [PATCH 4/8] GUP: Add global switch for all apps Bug: 119221883 Test: make RunSettingsRoboTests Change-Id: Iebf6c5eceb323d03332169eeb0a4a0d1f9c2c493 Merged-In: Iebf6c5eceb323d03332169eeb0a4a0d1f9c2c493 --- res/values/strings.xml | 2 + res/xml/gup_settings.xml | 6 ++ ...pEnableForAllAppsPreferenceController.java | 72 +++++++++++++++ ...bleForAllAppsPreferenceControllerTest.java | 88 +++++++++++++++++++ 4 files changed, 168 insertions(+) create mode 100644 src/com/android/settings/development/gup/GupEnableForAllAppsPreferenceController.java create mode 100644 tests/robotests/src/com/android/settings/development/gup/GupEnableForAllAppsPreferenceControllerTest.java diff --git a/res/values/strings.xml b/res/values/strings.xml index 73f011b6c0c..b2a2e9ea79a 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -9986,6 +9986,8 @@ Game Update Package Preferences Modify Game Update Package settings + + Enable for all apps Select Graphics Driver diff --git a/res/xml/gup_settings.xml b/res/xml/gup_settings.xml index 43ba39b331e..09bca712f36 100644 --- a/res/xml/gup_settings.xml +++ b/res/xml/gup_settings.xml @@ -21,6 +21,12 @@ android:key="gup_settings" android:title="@string/gup_dashboard_title"> + + + Date: Sat, 19 Jan 2019 16:45:20 +0800 Subject: [PATCH 5/8] Game Driver: Add SwitchBar to control GUP feature Uncheck the global switch will hide the preference controllers and force all apps to use system graphics driver. This change also add a content observer to notify all the preference controllers of settings global changes. Bug: 119221883 Test: make RunSettingsRoboTests Change-Id: Ice9ded17c759791a3728c552f79881e2215ac081 Merged-In: Ice9ded17c759791a3728c552f79881e2215ac081 --- .../gup/GameDriverContentObserver.java | 56 ++++++++ .../development/gup/GupDashboard.java | 20 ++- ...pEnableForAllAppsPreferenceController.java | 57 ++++++-- .../gup/GupGlobalSwitchBarController.java | 98 ++++++++++++++ .../gup/GupPreferenceController.java | 90 ++++++++++--- .../gup/GameDriverContentObserverTest.java | 70 ++++++++++ ...bleForAllAppsPreferenceControllerTest.java | 26 +++- .../gup/GupGlobalSwitchBarControllerTest.java | 124 ++++++++++++++++++ .../gup/GupPreferenceControllerTest.java | 34 ++++- 9 files changed, 536 insertions(+), 39 deletions(-) create mode 100644 src/com/android/settings/development/gup/GameDriverContentObserver.java create mode 100644 src/com/android/settings/development/gup/GupGlobalSwitchBarController.java create mode 100644 tests/robotests/src/com/android/settings/development/gup/GameDriverContentObserverTest.java create mode 100644 tests/robotests/src/com/android/settings/development/gup/GupGlobalSwitchBarControllerTest.java diff --git a/src/com/android/settings/development/gup/GameDriverContentObserver.java b/src/com/android/settings/development/gup/GameDriverContentObserver.java new file mode 100644 index 00000000000..92602134c0f --- /dev/null +++ b/src/com/android/settings/development/gup/GameDriverContentObserver.java @@ -0,0 +1,56 @@ +/* + * Copyright 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings.development.gup; + +import android.content.ContentResolver; +import android.database.ContentObserver; +import android.os.Handler; +import android.provider.Settings; + +import androidx.annotation.VisibleForTesting; + +/** + * Helper class to observe Game Driver settings global change. + */ +public class GameDriverContentObserver extends ContentObserver { + interface OnGameDriverContentChangedListener { + void onGameDriverContentChanged(); + } + + @VisibleForTesting + OnGameDriverContentChangedListener mListener; + + public GameDriverContentObserver(Handler handler, OnGameDriverContentChangedListener listener) { + super(handler); + mListener = listener; + } + + @Override + public void onChange(boolean selfChange) { + super.onChange(selfChange); + mListener.onGameDriverContentChanged(); + } + + public void register(ContentResolver contentResolver) { + contentResolver.registerContentObserver( + Settings.Global.getUriFor(Settings.Global.GUP_DEV_ALL_APPS), false, this); + } + + public void unregister(ContentResolver contentResolver) { + contentResolver.unregisterContentObserver(this); + } +} diff --git a/src/com/android/settings/development/gup/GupDashboard.java b/src/com/android/settings/development/gup/GupDashboard.java index 674a0a90d0e..392de47bd73 100644 --- a/src/com/android/settings/development/gup/GupDashboard.java +++ b/src/com/android/settings/development/gup/GupDashboard.java @@ -17,14 +17,20 @@ package com.android.settings.development.gup; import android.content.Context; +import android.os.Bundle; import com.android.internal.logging.nano.MetricsProto; import com.android.settings.R; +import com.android.settings.SettingsActivity; import com.android.settings.dashboard.DashboardFragment; -import com.android.settingslib.core.AbstractPreferenceController; +import com.android.settings.widget.SwitchBar; +import com.android.settings.widget.SwitchBarController; import java.util.List; +/** + * Dashboard for Game Driver preferences. + */ public class GupDashboard extends DashboardFragment { private static final String TAG = "GupDashboard"; @@ -47,4 +53,16 @@ public class GupDashboard extends DashboardFragment { public int getHelpResource() { return 0; } + + @Override + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + + final SettingsActivity activity = (SettingsActivity) getActivity(); + final SwitchBar switchBar = activity.getSwitchBar(); + final GupGlobalSwitchBarController switchBarController = + new GupGlobalSwitchBarController(activity, new SwitchBarController(switchBar)); + getLifecycle().addObserver(switchBarController); + switchBar.show(); + } } diff --git a/src/com/android/settings/development/gup/GupEnableForAllAppsPreferenceController.java b/src/com/android/settings/development/gup/GupEnableForAllAppsPreferenceController.java index 3c2d8b88b15..b71e9f334eb 100644 --- a/src/com/android/settings/development/gup/GupEnableForAllAppsPreferenceController.java +++ b/src/com/android/settings/development/gup/GupEnableForAllAppsPreferenceController.java @@ -18,55 +18,92 @@ package com.android.settings.development.gup; import android.content.ContentResolver; import android.content.Context; +import android.os.Handler; +import android.os.Looper; import android.provider.Settings; +import androidx.annotation.VisibleForTesting; import androidx.preference.Preference; import androidx.preference.PreferenceScreen; import androidx.preference.SwitchPreference; import com.android.settings.core.BasePreferenceController; +import com.android.settingslib.core.lifecycle.LifecycleObserver; +import com.android.settingslib.core.lifecycle.events.OnStart; +import com.android.settingslib.core.lifecycle.events.OnStop; import com.android.settingslib.development.DevelopmentSettingsEnabler; -public class GupEnableForAllAppsPreferenceController - extends BasePreferenceController implements Preference.OnPreferenceChangeListener { +/** + * Controller of global switch to enable Game Driver for all Apps. + */ +public class GupEnableForAllAppsPreferenceController extends BasePreferenceController + implements Preference.OnPreferenceChangeListener, + GameDriverContentObserver.OnGameDriverContentChangedListener, LifecycleObserver, + OnStart, OnStop { public static final int GUP_DEFAULT = 0; public static final int GUP_ALL_APPS = 1; + public static final int GUP_OFF = 2; + private final Context mContext; private final ContentResolver mContentResolver; + @VisibleForTesting + GameDriverContentObserver mGameDriverContentObserver; + + private SwitchPreference mPreference; public GupEnableForAllAppsPreferenceController(Context context, String key) { super(context, key); + mContext = context; mContentResolver = context.getContentResolver(); + mGameDriverContentObserver = + new GameDriverContentObserver(new Handler(Looper.getMainLooper()), this); } @Override public int getAvailabilityStatus() { return DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(mContext) + && (Settings.Global.getInt( + mContentResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_DEFAULT) + != GUP_OFF) ? AVAILABLE - : DISABLED_DEPENDENT_SETTING; + : CONDITIONALLY_UNAVAILABLE; } @Override public void displayPreference(PreferenceScreen screen) { super.displayPreference(screen); - final SwitchPreference switchPreference = - (SwitchPreference) screen.findPreference(getPreferenceKey()); - if (switchPreference == null) { - return; - } + mPreference = (SwitchPreference) screen.findPreference(getPreferenceKey()); + } + @Override + public void onStart() { + mGameDriverContentObserver.register(mContentResolver); + } + + @Override + public void onStop() { + mGameDriverContentObserver.unregister(mContentResolver); + } + + @Override + public void updateState(Preference preference) { + final SwitchPreference switchPreference = (SwitchPreference) preference; + switchPreference.setVisible(isAvailable()); switchPreference.setChecked(Settings.Global.getInt(mContentResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_DEFAULT) == GUP_ALL_APPS); - switchPreference.setOnPreferenceChangeListener(this); } @Override public boolean onPreferenceChange(Preference preference, Object newValue) { - // When developer option is present, always overwrite GUP_DEV_ALL_APPS. Settings.Global.putInt(mContentResolver, Settings.Global.GUP_DEV_ALL_APPS, (boolean) newValue ? GUP_ALL_APPS : GUP_DEFAULT); return true; } + + @Override + public void onGameDriverContentChanged() { + updateState(mPreference); + } } diff --git a/src/com/android/settings/development/gup/GupGlobalSwitchBarController.java b/src/com/android/settings/development/gup/GupGlobalSwitchBarController.java new file mode 100644 index 00000000000..c6bdf9dd332 --- /dev/null +++ b/src/com/android/settings/development/gup/GupGlobalSwitchBarController.java @@ -0,0 +1,98 @@ +/* + * Copyright 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings.development.gup; + +import static com.android.settings.development.gup.GupEnableForAllAppsPreferenceController.GUP_ALL_APPS; +import static com.android.settings.development.gup.GupEnableForAllAppsPreferenceController.GUP_DEFAULT; +import static com.android.settings.development.gup.GupEnableForAllAppsPreferenceController.GUP_OFF; + +import android.content.ContentResolver; +import android.content.Context; +import android.os.Handler; +import android.os.Looper; +import android.provider.Settings; + +import androidx.annotation.VisibleForTesting; + +import com.android.settings.widget.SwitchWidgetController; +import com.android.settingslib.core.lifecycle.LifecycleObserver; +import com.android.settingslib.core.lifecycle.events.OnStart; +import com.android.settingslib.core.lifecycle.events.OnStop; +import com.android.settingslib.development.DevelopmentSettingsEnabler; + +/** + * Controller of global switch bar used to fully turn off Game Driver. + */ +public class GupGlobalSwitchBarController + implements SwitchWidgetController.OnSwitchChangeListener, + GameDriverContentObserver.OnGameDriverContentChangedListener, LifecycleObserver, + OnStart, OnStop { + private final Context mContext; + private final ContentResolver mContentResolver; + @VisibleForTesting + SwitchWidgetController mSwitchWidgetController; + @VisibleForTesting + GameDriverContentObserver mGameDriverContentObserver; + + GupGlobalSwitchBarController(Context context, SwitchWidgetController switchWidgetController) { + mContext = context; + mContentResolver = context.getContentResolver(); + mGameDriverContentObserver = + new GameDriverContentObserver(new Handler(Looper.getMainLooper()), this); + mSwitchWidgetController = switchWidgetController; + mSwitchWidgetController.setEnabled( + DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(context)); + mSwitchWidgetController.setChecked(Settings.Global.getInt(mContentResolver, + Settings.Global.GUP_DEV_ALL_APPS, GUP_DEFAULT) + != GUP_OFF); + mSwitchWidgetController.setListener(this); + } + + @Override + public void onStart() { + mSwitchWidgetController.startListening(); + mGameDriverContentObserver.register(mContentResolver); + } + + @Override + public void onStop() { + mSwitchWidgetController.stopListening(); + mGameDriverContentObserver.unregister(mContentResolver); + } + + @Override + public boolean onSwitchToggled(boolean isChecked) { + if (!isChecked) { + Settings.Global.putInt(mContentResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_OFF); + return true; + } + + if (Settings.Global.getInt(mContentResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_DEFAULT) + != GUP_ALL_APPS) { + Settings.Global.putInt(mContentResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_DEFAULT); + } + + return true; + } + + @Override + public void onGameDriverContentChanged() { + mSwitchWidgetController.setChecked(Settings.Global.getInt(mContentResolver, + Settings.Global.GUP_DEV_ALL_APPS, GUP_DEFAULT) + != GUP_OFF); + } +} diff --git a/src/com/android/settings/development/gup/GupPreferenceController.java b/src/com/android/settings/development/gup/GupPreferenceController.java index d4cd2f1284e..8fed52cc638 100644 --- a/src/com/android/settings/development/gup/GupPreferenceController.java +++ b/src/com/android/settings/development/gup/GupPreferenceController.java @@ -16,11 +16,16 @@ package com.android.settings.development.gup; +import static com.android.settings.development.gup.GupEnableForAllAppsPreferenceController.GUP_DEFAULT; +import static com.android.settings.development.gup.GupEnableForAllAppsPreferenceController.GUP_OFF; + import android.content.ContentResolver; import android.content.Context; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.content.res.Resources; +import android.os.Handler; +import android.os.Looper; import android.provider.Settings; import androidx.annotation.VisibleForTesting; @@ -31,6 +36,9 @@ import androidx.preference.PreferenceScreen; import com.android.settings.R; import com.android.settings.core.BasePreferenceController; +import com.android.settingslib.core.lifecycle.LifecycleObserver; +import com.android.settingslib.core.lifecycle.events.OnStart; +import com.android.settingslib.core.lifecycle.events.OnStop; import com.android.settingslib.development.DevelopmentSettingsEnabler; import java.text.Collator; @@ -42,21 +50,37 @@ import java.util.HashSet; import java.util.List; import java.util.Set; -public class GupPreferenceController - extends BasePreferenceController implements Preference.OnPreferenceChangeListener { +/** + * Controller of all the per App based list preferences. + */ +public class GupPreferenceController extends BasePreferenceController + implements Preference.OnPreferenceChangeListener, + GameDriverContentObserver.OnGameDriverContentChangedListener, LifecycleObserver, + OnStart, OnStop { + private final Context mContext; + private final ContentResolver mContentResolver; private final CharSequence[] mEntryList; private final String mPreferenceTitle; private final String mPreferenceDefault; private final String mPreferenceGup; private final String mPreferenceSystem; + @VisibleForTesting + GameDriverContentObserver mGameDriverContentObserver; private final List mAppInfos; private final Set mDevOptInApps; private final Set mDevOptOutApps; + private PreferenceGroup mPreferenceGroup; + public GupPreferenceController(Context context, String key) { super(context, key); + mContext = context; + mContentResolver = context.getContentResolver(); + mGameDriverContentObserver = + new GameDriverContentObserver(new Handler(Looper.getMainLooper()), this); + final Resources resources = context.getResources(); mEntryList = resources.getStringArray(R.array.gup_app_preference_values); mPreferenceTitle = resources.getString(R.string.gup_app_preference_title); @@ -68,35 +92,57 @@ public class GupPreferenceController // Update the UI when all the app infos are ready. mAppInfos = getAppInfos(context); - final ContentResolver contentResolver = context.getContentResolver(); mDevOptInApps = - getGlobalSettingsString(contentResolver, Settings.Global.GUP_DEV_OPT_IN_APPS); + getGlobalSettingsString(mContentResolver, Settings.Global.GUP_DEV_OPT_IN_APPS); mDevOptOutApps = - getGlobalSettingsString(contentResolver, Settings.Global.GUP_DEV_OPT_OUT_APPS); + getGlobalSettingsString(mContentResolver, Settings.Global.GUP_DEV_OPT_OUT_APPS); } @Override public int getAvailabilityStatus() { return DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(mContext) + && (Settings.Global.getInt( + mContentResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_DEFAULT) + != GUP_OFF) ? AVAILABLE - : DISABLED_DEPENDENT_SETTING; + : CONDITIONALLY_UNAVAILABLE; } @Override public void displayPreference(PreferenceScreen screen) { super.displayPreference(screen); - final PreferenceGroup preferenceGroup = - (PreferenceGroup) screen.findPreference(getPreferenceKey()); - if (preferenceGroup == null) { - return; - } + mPreferenceGroup = (PreferenceGroup) screen.findPreference(getPreferenceKey()); + final Context context = mPreferenceGroup.getContext(); for (AppInfo appInfo : mAppInfos) { - preferenceGroup.addPreference( - createListPreference(appInfo.info.packageName, appInfo.label)); + mPreferenceGroup.addPreference( + createListPreference(context, appInfo.info.packageName, appInfo.label)); } } + @Override + public void onStart() { + mGameDriverContentObserver.register(mContentResolver); + } + + @Override + public void onStop() { + mGameDriverContentObserver.unregister(mContentResolver); + } + + @Override + public void updateState(Preference preference) { + // This is a workaround, because PreferenceGroup.setVisible is not applied to the + // preferences inside the group. + final boolean isGroupAvailable = isAvailable(); + final PreferenceGroup group = (PreferenceGroup) preference; + for (int idx = 0; idx < group.getPreferenceCount(); idx++) { + final Preference pref = group.getPreference(idx); + pref.setVisible(isGroupAvailable); + } + preference.setVisible(isGroupAvailable); + } + @Override public boolean onPreferenceChange(Preference preference, Object newValue) { final ListPreference listPref = (ListPreference) preference; @@ -120,14 +166,19 @@ public class GupPreferenceController // Push the updated Sets for opt-in and opt-out apps to // corresponding Settings.Global.GUP_DEV_OPT_(IN|OUT)_APPS - Settings.Global.putString(mContext.getContentResolver(), - Settings.Global.GUP_DEV_OPT_IN_APPS, String.join(",", mDevOptInApps)); - Settings.Global.putString(mContext.getContentResolver(), - Settings.Global.GUP_DEV_OPT_OUT_APPS, String.join(",", mDevOptOutApps)); + Settings.Global.putString(mContentResolver, Settings.Global.GUP_DEV_OPT_IN_APPS, + String.join(",", mDevOptInApps)); + Settings.Global.putString(mContentResolver, Settings.Global.GUP_DEV_OPT_OUT_APPS, + String.join(",", mDevOptOutApps)); return true; } + @Override + public void onGameDriverContentChanged() { + updateState(mPreferenceGroup); + } + // AppInfo class to achieve loading the application label only once class AppInfo { AppInfo(PackageManager packageManager, ApplicationInfo applicationInfo) { @@ -176,8 +227,9 @@ public class GupPreferenceController }; @VisibleForTesting - protected ListPreference createListPreference(String packageName, String appName) { - final ListPreference listPreference = new ListPreference(mContext); + protected ListPreference createListPreference( + Context context, String packageName, String appName) { + final ListPreference listPreference = new ListPreference(context); listPreference.setKey(packageName); listPreference.setTitle(appName); diff --git a/tests/robotests/src/com/android/settings/development/gup/GameDriverContentObserverTest.java b/tests/robotests/src/com/android/settings/development/gup/GameDriverContentObserverTest.java new file mode 100644 index 00000000000..6939ac9e2de --- /dev/null +++ b/tests/robotests/src/com/android/settings/development/gup/GameDriverContentObserverTest.java @@ -0,0 +1,70 @@ +/* + * Copyright 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings.development.gup; + +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; + +import android.content.ContentResolver; +import android.provider.Settings; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.RuntimeEnvironment; + +@RunWith(RobolectricTestRunner.class) +public class GameDriverContentObserverTest { + @Mock + private ContentResolver mResolver; + @Mock + private GameDriverContentObserver.OnGameDriverContentChangedListener mListener; + + private GameDriverContentObserver mGameDriverContentObserver; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + mGameDriverContentObserver = spy(new GameDriverContentObserver(null, null)); + } + + @Test + public void onChange_shouldCallListener() { + mGameDriverContentObserver.mListener = mListener; + mGameDriverContentObserver.onChange(true); + + verify(mListener).onGameDriverContentChanged(); + } + + @Test + public void register_shouldRegisterContentObserver() { + mGameDriverContentObserver.register(mResolver); + + verify(mResolver).registerContentObserver( + Settings.Global.getUriFor(Settings.Global.GUP_DEV_ALL_APPS), false, + mGameDriverContentObserver); + } + + @Test + public void unregister_shouldUnregisterContentObserver() { + mGameDriverContentObserver.unregister(mResolver); + + verify(mResolver).unregisterContentObserver(mGameDriverContentObserver); + } +} diff --git a/tests/robotests/src/com/android/settings/development/gup/GupEnableForAllAppsPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/development/gup/GupEnableForAllAppsPreferenceControllerTest.java index 4dbdd35e17f..cd8c7108106 100644 --- a/tests/robotests/src/com/android/settings/development/gup/GupEnableForAllAppsPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/development/gup/GupEnableForAllAppsPreferenceControllerTest.java @@ -20,6 +20,7 @@ import static com.android.settings.development.gup.GupEnableForAllAppsPreference import static com.android.settings.development.gup.GupEnableForAllAppsPreferenceController.GUP_DEFAULT; import static com.google.common.truth.Truth.assertThat; +import static org.mockito.Mockito.atLeastOnce; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -44,6 +45,8 @@ public class GupEnableForAllAppsPreferenceControllerTest { private PreferenceScreen mScreen; @Mock private SwitchPreference mPreference; + @Mock + private GameDriverContentObserver mGameDriverContentObserver; private Context mContext; private ContentResolver mResolver; @@ -56,20 +59,38 @@ public class GupEnableForAllAppsPreferenceControllerTest { mResolver = mContext.getContentResolver(); mController = new GupEnableForAllAppsPreferenceController(mContext, "testKey"); when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); + mController.displayPreference(mScreen); + + Settings.Global.putInt(mResolver, Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 1); } @Test public void displayPreference_shouldAddSwitchPreference() { Settings.Global.putInt(mResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_DEFAULT); - mController.displayPreference(mScreen); + mController.updateState(mPreference); verify(mPreference).setChecked(false); } + @Test + public void onStart_shouldRegister() { + mController.mGameDriverContentObserver = mGameDriverContentObserver; + mController.onStart(); + + verify(mGameDriverContentObserver).register(mResolver); + } + + @Test + public void onStop_shouldUnregister() { + mController.mGameDriverContentObserver = mGameDriverContentObserver; + mController.onStop(); + + verify(mGameDriverContentObserver).unregister(mResolver); + } + @Test public void onPreferenceChange_check_shouldUpdateSettingsGlobal() { Settings.Global.putInt(mResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_DEFAULT); - mController.displayPreference(mScreen); mController.onPreferenceChange(mPreference, true); assertThat(Settings.Global.getInt(mResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_DEFAULT)) @@ -79,7 +100,6 @@ public class GupEnableForAllAppsPreferenceControllerTest { @Test public void onPreferenceChange_uncheck_shouldUpdateSettingsGlobal() { Settings.Global.putInt(mResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_ALL_APPS); - mController.displayPreference(mScreen); mController.onPreferenceChange(mPreference, false); assertThat(Settings.Global.getInt(mResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_DEFAULT)) diff --git a/tests/robotests/src/com/android/settings/development/gup/GupGlobalSwitchBarControllerTest.java b/tests/robotests/src/com/android/settings/development/gup/GupGlobalSwitchBarControllerTest.java new file mode 100644 index 00000000000..0d3ed481fc8 --- /dev/null +++ b/tests/robotests/src/com/android/settings/development/gup/GupGlobalSwitchBarControllerTest.java @@ -0,0 +1,124 @@ +/* + * Copyright 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings.development.gup; + +import static com.android.settings.development.gup.GupEnableForAllAppsPreferenceController.GUP_DEFAULT; +import static com.android.settings.development.gup.GupEnableForAllAppsPreferenceController.GUP_OFF; +import static com.google.common.truth.Truth.assertThat; + +import static org.mockito.Mockito.verify; + +import android.content.ContentResolver; +import android.content.Context; +import android.provider.Settings; + +import com.android.settings.widget.SwitchBar; +import com.android.settings.widget.SwitchBarController; +import com.android.settings.widget.SwitchWidgetController; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.RuntimeEnvironment; + +@RunWith(RobolectricTestRunner.class) +public class GupGlobalSwitchBarControllerTest { + @Mock + private SwitchBar mSwitchBar; + @Mock + private SwitchWidgetController mSwitchWidgetController; + @Mock + private GameDriverContentObserver mGameDriverContentObserver; + + private Context mContext; + private ContentResolver mResolver; + private GupGlobalSwitchBarController mController; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + mContext = RuntimeEnvironment.application; + mResolver = mContext.getContentResolver(); + } + + @Test + public void constructor_gupOn_shouldCheckSwitchBar() { + Settings.Global.putInt(mResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_DEFAULT); + mController = + new GupGlobalSwitchBarController(mContext, new SwitchBarController(mSwitchBar)); + + verify(mSwitchBar).setChecked(true); + } + + @Test + public void constructor_gupOff_shouldUncheckSwitchBar() { + Settings.Global.putInt(mResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_OFF); + mController = + new GupGlobalSwitchBarController(mContext, new SwitchBarController(mSwitchBar)); + + verify(mSwitchBar).setChecked(false); + } + + @Test + public void onStart_shouldStartListeningAndRegister() { + mController = + new GupGlobalSwitchBarController(mContext, new SwitchBarController(mSwitchBar)); + mController.mSwitchWidgetController = mSwitchWidgetController; + mController.mGameDriverContentObserver = mGameDriverContentObserver; + mController.onStart(); + + verify(mSwitchWidgetController).startListening(); + verify(mGameDriverContentObserver).register(mResolver); + } + + @Test + public void onStop_shouldStopListeningAndUnregister() { + mController = + new GupGlobalSwitchBarController(mContext, new SwitchBarController(mSwitchBar)); + mController.mSwitchWidgetController = mSwitchWidgetController; + mController.mGameDriverContentObserver = mGameDriverContentObserver; + mController.onStop(); + + verify(mSwitchWidgetController).stopListening(); + verify(mGameDriverContentObserver).unregister(mResolver); + } + + @Test + public void onSwitchToggled_checked_shouldTurnOnGup() { + Settings.Global.putInt(mResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_OFF); + mController = + new GupGlobalSwitchBarController(mContext, new SwitchBarController(mSwitchBar)); + mController.onSwitchToggled(true); + + assertThat(Settings.Global.getInt(mResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_DEFAULT)) + .isEqualTo(GUP_DEFAULT); + } + + @Test + public void onSwitchToggled_unchecked_shouldTurnOffGup() { + Settings.Global.putInt(mResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_DEFAULT); + mController = + new GupGlobalSwitchBarController(mContext, new SwitchBarController(mSwitchBar)); + mController.onSwitchToggled(false); + + assertThat(Settings.Global.getInt(mResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_DEFAULT)) + .isEqualTo(GUP_OFF); + } +} diff --git a/tests/robotests/src/com/android/settings/development/gup/GupPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/development/gup/GupPreferenceControllerTest.java index 0ac14ef007c..8055c94ae3f 100644 --- a/tests/robotests/src/com/android/settings/development/gup/GupPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/development/gup/GupPreferenceControllerTest.java @@ -20,6 +20,7 @@ import static com.android.settings.testutils.ApplicationTestUtils.buildInfo; import static com.google.common.truth.Truth.assertThat; import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import android.content.ContentResolver; @@ -63,6 +64,8 @@ public class GupPreferenceControllerTest { private PackageManager mPackageManager; @Mock private PreferenceScreen mScreen; + @Mock + private GameDriverContentObserver mGameDriverContentObserver; private Context mContext; private PreferenceGroup mGroup; @@ -92,11 +95,29 @@ public class GupPreferenceControllerTest { assertThat(mGroup.getPreference(1).getKey()).isEqualTo(APP_3); } + @Test + public void onStart_shouldRegister() { + loadDefaultConfig(); + mController.mGameDriverContentObserver = mGameDriverContentObserver; + mController.onStart(); + + verify(mGameDriverContentObserver).register(mResolver); + } + + @Test + public void onStop_shouldUnregister() { + loadDefaultConfig(); + mController.mGameDriverContentObserver = mGameDriverContentObserver; + mController.onStop(); + + verify(mGameDriverContentObserver).unregister(mResolver); + } + @Test public void createPreference_configDefault_shouldSetDefaultAttributes() { loadDefaultConfig(); final ListPreference preference = - mController.createListPreference(TEST_PKG_NAME, TEST_APP_NAME); + mController.createListPreference(mContext, TEST_PKG_NAME, TEST_APP_NAME); assertThat(preference.getKey()).isEqualTo(TEST_PKG_NAME); assertThat(preference.getTitle()).isEqualTo(TEST_APP_NAME); @@ -112,7 +133,7 @@ public class GupPreferenceControllerTest { public void createPreference_configGup_shouldSetGupAttributes() { loadConfig(TEST_PKG_NAME, ""); final ListPreference preference = - mController.createListPreference(TEST_PKG_NAME, TEST_APP_NAME); + mController.createListPreference(mContext, TEST_PKG_NAME, TEST_APP_NAME); assertThat(preference.getKey()).isEqualTo(TEST_PKG_NAME); assertThat(preference.getTitle()).isEqualTo(TEST_APP_NAME); @@ -128,7 +149,7 @@ public class GupPreferenceControllerTest { public void createPreference_configSystem_shouldSetSystemAttributes() { loadConfig("", TEST_PKG_NAME); final ListPreference preference = - mController.createListPreference(TEST_PKG_NAME, TEST_APP_NAME); + mController.createListPreference(mContext, TEST_PKG_NAME, TEST_APP_NAME); assertThat(preference.getKey()).isEqualTo(TEST_PKG_NAME); assertThat(preference.getTitle()).isEqualTo(TEST_APP_NAME); @@ -144,7 +165,7 @@ public class GupPreferenceControllerTest { public void onPreferenceChange_selectDefault_shouldUpdateAttributesAndSettingsGlobal() { loadDefaultConfig(); final ListPreference preference = - mController.createListPreference(TEST_PKG_NAME, TEST_APP_NAME); + mController.createListPreference(mContext, TEST_PKG_NAME, TEST_APP_NAME); mController.onPreferenceChange(preference, mValueList[DEFAULT]); assertThat(preference.getEntry()).isEqualTo(mValueList[DEFAULT]); @@ -160,7 +181,7 @@ public class GupPreferenceControllerTest { public void onPreferenceChange_selectGup_shouldUpdateAttributesAndSettingsGlobal() { loadDefaultConfig(); final ListPreference preference = - mController.createListPreference(TEST_PKG_NAME, TEST_APP_NAME); + mController.createListPreference(mContext, TEST_PKG_NAME, TEST_APP_NAME); mController.onPreferenceChange(preference, mValueList[GUP]); assertThat(preference.getEntry()).isEqualTo(mValueList[GUP]); @@ -176,7 +197,7 @@ public class GupPreferenceControllerTest { public void onPreferenceChange_selectSystem_shouldUpdateAttributesAndSettingsGlobal() { loadDefaultConfig(); final ListPreference preference = - mController.createListPreference(TEST_PKG_NAME, TEST_APP_NAME); + mController.createListPreference(mContext, TEST_PKG_NAME, TEST_APP_NAME); mController.onPreferenceChange(preference, mValueList[SYSTEM]); assertThat(preference.getEntry()).isEqualTo(mValueList[SYSTEM]); @@ -212,6 +233,7 @@ public class GupPreferenceControllerTest { mController = new GupPreferenceController(mContext, "testKey"); mGroup = spy(new PreferenceCategory(mContext)); final PreferenceManager preferenceManager = new PreferenceManager(mContext); + when(mGroup.getContext()).thenReturn(mContext); when(mGroup.getPreferenceManager()).thenReturn(preferenceManager); when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mGroup); mController.displayPreference(mScreen); From 34986f5ba5d47a5eaa6a6bc4be89258ac572cb5e Mon Sep 17 00:00:00 2001 From: Yiwei Zhang Date: Wed, 23 Jan 2019 12:40:42 -0800 Subject: [PATCH 6/8] Game Driver: Add footer to the dashboard This change adds a footer to the current dashboard to show some descriptions when the whole module is turned off. Bug: 119221883 Test: make RunSettingsRoboTests Change-Id: I9788d4c64b06deb099a43e8db7eecab20a85f494 Merged-In: I9788d4c64b06deb099a43e8db7eecab20a85f494 --- res/values/strings.xml | 2 + res/xml/gup_settings.xml | 7 + .../GameDriverFooterPreferenceController.java | 91 +++++++++++++ ...eDriverFooterPreferenceControllerTest.java | 120 ++++++++++++++++++ 4 files changed, 220 insertions(+) create mode 100644 src/com/android/settings/development/gup/GameDriverFooterPreferenceController.java create mode 100644 tests/robotests/src/com/android/settings/development/gup/GameDriverFooterPreferenceControllerTest.java diff --git a/res/values/strings.xml b/res/values/strings.xml index b2a2e9ea79a..ff3d03cb2d5 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -9986,6 +9986,8 @@ Game Update Package Preferences Modify Game Update Package settings + + When Game Driver is turned on, you can pick to use the updated graphics driver for Apps installed on the device. Enable for all apps diff --git a/res/xml/gup_settings.xml b/res/xml/gup_settings.xml index 09bca712f36..8b09bd27ed2 100644 --- a/res/xml/gup_settings.xml +++ b/res/xml/gup_settings.xml @@ -33,4 +33,11 @@ settings:controller="com.android.settings.development.gup.GupPreferenceController"> + + + diff --git a/src/com/android/settings/development/gup/GameDriverFooterPreferenceController.java b/src/com/android/settings/development/gup/GameDriverFooterPreferenceController.java new file mode 100644 index 00000000000..332c1597cf8 --- /dev/null +++ b/src/com/android/settings/development/gup/GameDriverFooterPreferenceController.java @@ -0,0 +1,91 @@ +/* + * Copyright 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings.development.gup; + +import static com.android.settings.development.gup.GupEnableForAllAppsPreferenceController.GUP_DEFAULT; +import static com.android.settings.development.gup.GupEnableForAllAppsPreferenceController.GUP_OFF; + +import android.content.ContentResolver; +import android.content.Context; +import android.os.Handler; +import android.os.Looper; +import android.provider.Settings; + +import androidx.annotation.VisibleForTesting; +import androidx.preference.Preference; +import androidx.preference.PreferenceScreen; + +import com.android.settings.core.BasePreferenceController; +import com.android.settingslib.core.lifecycle.LifecycleObserver; +import com.android.settingslib.core.lifecycle.events.OnStart; +import com.android.settingslib.core.lifecycle.events.OnStop; +import com.android.settingslib.widget.FooterPreference; + +/** + * Controller of footer preference for Game Driver. + */ +public class GameDriverFooterPreferenceController extends BasePreferenceController + implements GameDriverContentObserver.OnGameDriverContentChangedListener, LifecycleObserver, + OnStart, OnStop { + private final ContentResolver mContentResolver; + @VisibleForTesting + GameDriverContentObserver mGameDriverContentObserver; + + private FooterPreference mPreference; + + public GameDriverFooterPreferenceController(Context context) { + super(context, FooterPreference.KEY_FOOTER); + mContentResolver = context.getContentResolver(); + mGameDriverContentObserver = + new GameDriverContentObserver(new Handler(Looper.getMainLooper()), this); + } + + @Override + public int getAvailabilityStatus() { + return Settings.Global.getInt( + mContentResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_DEFAULT) + == GUP_OFF + ? AVAILABLE + : CONDITIONALLY_UNAVAILABLE; + } + + @Override + public void displayPreference(PreferenceScreen screen) { + super.displayPreference(screen); + mPreference = (FooterPreference) screen.findPreference(getPreferenceKey()); + } + + @Override + public void onStart() { + mGameDriverContentObserver.register(mContentResolver); + } + + @Override + public void onStop() { + mGameDriverContentObserver.unregister(mContentResolver); + } + + @Override + public void updateState(Preference preference) { + preference.setVisible(isAvailable()); + } + + @Override + public void onGameDriverContentChanged() { + updateState(mPreference); + } +} diff --git a/tests/robotests/src/com/android/settings/development/gup/GameDriverFooterPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/development/gup/GameDriverFooterPreferenceControllerTest.java new file mode 100644 index 00000000000..17ddbda90a4 --- /dev/null +++ b/tests/robotests/src/com/android/settings/development/gup/GameDriverFooterPreferenceControllerTest.java @@ -0,0 +1,120 @@ +/* + * Copyright 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings.development.gup; + +import static com.android.settings.core.BasePreferenceController.AVAILABLE; +import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_UNAVAILABLE; +import static com.android.settings.development.gup.GupEnableForAllAppsPreferenceController.GUP_ALL_APPS; +import static com.android.settings.development.gup.GupEnableForAllAppsPreferenceController.GUP_DEFAULT; +import static com.android.settings.development.gup.GupEnableForAllAppsPreferenceController.GUP_OFF; +import static com.google.common.truth.Truth.assertThat; + +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import android.content.ContentResolver; +import android.content.Context; +import android.provider.Settings; + +import androidx.preference.PreferenceScreen; + +import com.android.settingslib.widget.FooterPreference; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.RuntimeEnvironment; + +@RunWith(RobolectricTestRunner.class) +public class GameDriverFooterPreferenceControllerTest { + @Mock + private PreferenceScreen mScreen; + @Mock + private FooterPreference mPreference; + @Mock + private GameDriverContentObserver mGameDriverContentObserver; + + private Context mContext; + private ContentResolver mResolver; + private GameDriverFooterPreferenceController mController; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + mContext = RuntimeEnvironment.application; + mResolver = mContext.getContentResolver(); + mController = spy(new GameDriverFooterPreferenceController(mContext)); + when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); + } + + @Test + public void getAvailabilityStatus_gameDriverOff_availableUnsearchable() { + Settings.Global.putInt(mResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_OFF); + + assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE); + } + + @Test + public void getAvailabilityStatus_gameDriverDefault_conditionallyUnavailable() { + Settings.Global.putInt(mResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_DEFAULT); + + assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE); + } + + @Test + public void getAvailabilityStatus_gameDriverAllApps_conditionallyUnavailable() { + Settings.Global.putInt(mResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_ALL_APPS); + + assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE); + } + + @Test + public void onStart_shouldRegister() { + mController.mGameDriverContentObserver = mGameDriverContentObserver; + mController.onStart(); + + verify(mGameDriverContentObserver).register(mResolver); + } + + @Test + public void onStop_shouldUnregister() { + mController.mGameDriverContentObserver = mGameDriverContentObserver; + mController.onStop(); + + verify(mGameDriverContentObserver).unregister(mResolver); + } + + @Test + public void updateState_available_visible() { + when(mController.getAvailabilityStatus()).thenReturn(AVAILABLE); + mController.updateState(mPreference); + + verify(mPreference).setVisible(true); + } + + @Test + public void updateState_unavailable_invisible() { + when(mController.getAvailabilityStatus()).thenReturn(CONDITIONALLY_UNAVAILABLE); + mController.updateState(mPreference); + + verify(mPreference).setVisible(false); + } +} From 046b9a8ca236d229c265f280cbb59a18d0eff294 Mon Sep 17 00:00:00 2001 From: Yiwei Zhang Date: Thu, 24 Jan 2019 14:04:58 -0800 Subject: [PATCH 7/8] Game Driver: rename GUP to Game Driver Bug: 119221883 Test: make RunSettingsRoboTests Change-Id: Ia7b9e3978ad96436a66843e6b5d1bd1e15f367c9 Merged-In: Ia7b9e3978ad96436a66843e6b5d1bd1e15f367c9 --- res/values/strings.xml | 40 ++++++------ res/xml/development_settings.xml | 8 +-- ..._settings.xml => game_driver_settings.xml} | 18 +++--- .../GameDriverAppPreferenceController.java} | 48 +++++++------- .../GameDriverContentObserver.java | 5 +- .../GameDriverDashboard.java} | 16 ++--- ...EnableForAllAppsPreferenceController.java} | 30 ++++----- .../GameDriverFooterPreferenceController.java | 11 ++-- .../GameDriverGlobalSwitchBarController.java} | 39 +++++++----- ...randfather_not_implementing_index_provider | 2 +- ...ameDriverAppPreferenceControllerTest.java} | 50 ++++++++------- .../GameDriverContentObserverTest.java | 6 +- .../GameDriverDashboardTest.java} | 19 +++--- ...leForAllAppsPreferenceControllerTest.java} | 32 ++++++---- ...eDriverFooterPreferenceControllerTest.java | 17 ++--- ...eDriverGlobalSwitchBarControllerTest.java} | 63 ++++++++++--------- 16 files changed, 219 insertions(+), 185 deletions(-) rename res/xml/{gup_settings.xml => game_driver_settings.xml} (63%) rename src/com/android/settings/development/{gup/GupPreferenceController.java => gamedriver/GameDriverAppPreferenceController.java} (84%) rename src/com/android/settings/development/{gup => gamedriver}/GameDriverContentObserver.java (91%) rename src/com/android/settings/development/{gup/GupDashboard.java => gamedriver/GameDriverDashboard.java} (77%) rename src/com/android/settings/development/{gup/GupEnableForAllAppsPreferenceController.java => gamedriver/GameDriverEnableForAllAppsPreferenceController.java} (77%) rename src/com/android/settings/development/{gup => gamedriver}/GameDriverFooterPreferenceController.java (86%) rename src/com/android/settings/development/{gup/GupGlobalSwitchBarController.java => gamedriver/GameDriverGlobalSwitchBarController.java} (64%) rename tests/robotests/src/com/android/settings/development/{gup/GupPreferenceControllerTest.java => gamedriver/GameDriverAppPreferenceControllerTest.java} (88%) rename tests/robotests/src/com/android/settings/development/{gup => gamedriver}/GameDriverContentObserverTest.java (93%) rename tests/robotests/src/com/android/settings/development/{gup/GupDashboardTest.java => gamedriver/GameDriverDashboardTest.java} (69%) rename tests/robotests/src/com/android/settings/development/{gup/GupEnableForAllAppsPreferenceControllerTest.java => gamedriver/GameDriverEnableForAllAppsPreferenceControllerTest.java} (69%) rename tests/robotests/src/com/android/settings/development/{gup => gamedriver}/GameDriverFooterPreferenceControllerTest.java (82%) rename tests/robotests/src/com/android/settings/development/{gup/GupGlobalSwitchBarControllerTest.java => gamedriver/GameDriverGlobalSwitchBarControllerTest.java} (54%) diff --git a/res/values/strings.xml b/res/values/strings.xml index ff3d03cb2d5..b9ff512639b 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -9982,27 +9982,27 @@ show both names, with the directory name wrapped in parenthesis --> %1$s (%2$s) - - Game Update Package Preferences - - Modify Game Update Package settings - + + Game Driver Preferences + + Modify Game Driver settings + When Game Driver is turned on, you can pick to use the updated graphics driver for Apps installed on the device. - - Enable for all apps - - Select Graphics Driver - - Default - - Game Update Package - - System Graphics Driver - - - @string/gup_app_preference_default - @string/gup_app_preference_gup - @string/gup_app_preference_system + + Enable for all apps + + Select Graphics Driver + + Default + + Game Driver + + System Graphics Driver + + + @string/game_driver_app_preference_default + @string/game_driver_app_preference_game_driver + @string/game_driver_app_preference_system diff --git a/res/xml/development_settings.xml b/res/xml/development_settings.xml index b1044bb1e1c..cde55433ab1 100644 --- a/res/xml/development_settings.xml +++ b/res/xml/development_settings.xml @@ -198,10 +198,10 @@ android:summary="@string/enable_gpu_debug_layers_summary" /> + android:key="game_driver_dashboard" + android:title="@string/game_driver_dashboard_title" + android:summary="@string/game_driver_dashboard_summary" + android:fragment="com.android.settings.development.gamedriver.GameDriverDashboard" /> diff --git a/res/xml/gup_settings.xml b/res/xml/game_driver_settings.xml similarity index 63% rename from res/xml/gup_settings.xml rename to res/xml/game_driver_settings.xml index 8b09bd27ed2..a04724a786f 100644 --- a/res/xml/gup_settings.xml +++ b/res/xml/game_driver_settings.xml @@ -18,26 +18,26 @@ + android:key="game_driver_settings" + android:title="@string/game_driver_dashboard_title"> + android:key="game_driver_all_apps_preference" + android:title="@string/game_driver_all_apps_preference_title" + settings:controller="com.android.settings.development.gamedriver.GameDriverEnableForAllAppsPreferenceController"> + android:key="game_driver_category" + android:title="@string/game_driver_app_preference_title" + settings:controller="com.android.settings.development.gamedriver.GameDriverAppPreferenceController"> + settings:controller="com.android.settings.development.gamedriver.GameDriverFooterPreferenceController"> diff --git a/src/com/android/settings/development/gup/GupPreferenceController.java b/src/com/android/settings/development/gamedriver/GameDriverAppPreferenceController.java similarity index 84% rename from src/com/android/settings/development/gup/GupPreferenceController.java rename to src/com/android/settings/development/gamedriver/GameDriverAppPreferenceController.java index 8fed52cc638..659310c2580 100644 --- a/src/com/android/settings/development/gup/GupPreferenceController.java +++ b/src/com/android/settings/development/gamedriver/GameDriverAppPreferenceController.java @@ -14,10 +14,10 @@ * limitations under the License. */ -package com.android.settings.development.gup; +package com.android.settings.development.gamedriver; -import static com.android.settings.development.gup.GupEnableForAllAppsPreferenceController.GUP_DEFAULT; -import static com.android.settings.development.gup.GupEnableForAllAppsPreferenceController.GUP_OFF; +import static com.android.settings.development.gamedriver.GameDriverEnableForAllAppsPreferenceController.GAME_DRIVER_DEFAULT; +import static com.android.settings.development.gamedriver.GameDriverEnableForAllAppsPreferenceController.GAME_DRIVER_OFF; import android.content.ContentResolver; import android.content.Context; @@ -53,16 +53,17 @@ import java.util.Set; /** * Controller of all the per App based list preferences. */ -public class GupPreferenceController extends BasePreferenceController +public class GameDriverAppPreferenceController extends BasePreferenceController implements Preference.OnPreferenceChangeListener, GameDriverContentObserver.OnGameDriverContentChangedListener, LifecycleObserver, OnStart, OnStop { + private final Context mContext; private final ContentResolver mContentResolver; private final CharSequence[] mEntryList; private final String mPreferenceTitle; private final String mPreferenceDefault; - private final String mPreferenceGup; + private final String mPreferenceGameDriver; private final String mPreferenceSystem; @VisibleForTesting GameDriverContentObserver mGameDriverContentObserver; @@ -73,7 +74,7 @@ public class GupPreferenceController extends BasePreferenceController private PreferenceGroup mPreferenceGroup; - public GupPreferenceController(Context context, String key) { + public GameDriverAppPreferenceController(Context context, String key) { super(context, key); mContext = context; @@ -82,28 +83,29 @@ public class GupPreferenceController extends BasePreferenceController new GameDriverContentObserver(new Handler(Looper.getMainLooper()), this); final Resources resources = context.getResources(); - mEntryList = resources.getStringArray(R.array.gup_app_preference_values); - mPreferenceTitle = resources.getString(R.string.gup_app_preference_title); - mPreferenceDefault = resources.getString(R.string.gup_app_preference_default); - mPreferenceGup = resources.getString(R.string.gup_app_preference_gup); - mPreferenceSystem = resources.getString(R.string.gup_app_preference_system); + mEntryList = resources.getStringArray(R.array.game_driver_app_preference_values); + mPreferenceTitle = resources.getString(R.string.game_driver_app_preference_title); + mPreferenceDefault = resources.getString(R.string.game_driver_app_preference_default); + mPreferenceGameDriver = + resources.getString(R.string.game_driver_app_preference_game_driver); + mPreferenceSystem = resources.getString(R.string.game_driver_app_preference_system); // TODO: Move this task to background if there's potential ANR/Jank. // Update the UI when all the app infos are ready. mAppInfos = getAppInfos(context); mDevOptInApps = - getGlobalSettingsString(mContentResolver, Settings.Global.GUP_DEV_OPT_IN_APPS); + getGlobalSettingsString(mContentResolver, Settings.Global.GAME_DRIVER_OPT_IN_APPS); mDevOptOutApps = - getGlobalSettingsString(mContentResolver, Settings.Global.GUP_DEV_OPT_OUT_APPS); + getGlobalSettingsString(mContentResolver, Settings.Global.GAME_DRIVER_OPT_OUT_APPS); } @Override public int getAvailabilityStatus() { return DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(mContext) - && (Settings.Global.getInt( - mContentResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_DEFAULT) - != GUP_OFF) + && (Settings.Global.getInt(mContentResolver, + Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_DEFAULT) + != GAME_DRIVER_OFF) ? AVAILABLE : CONDITIONALLY_UNAVAILABLE; } @@ -154,7 +156,7 @@ public class GupPreferenceController extends BasePreferenceController if (value.equals(mPreferenceSystem)) { mDevOptInApps.remove(packageName); mDevOptOutApps.add(packageName); - } else if (value.equals(mPreferenceGup)) { + } else if (value.equals(mPreferenceGameDriver)) { mDevOptInApps.add(packageName); mDevOptOutApps.remove(packageName); } else { @@ -165,10 +167,10 @@ public class GupPreferenceController extends BasePreferenceController listPref.setSummary(value); // Push the updated Sets for opt-in and opt-out apps to - // corresponding Settings.Global.GUP_DEV_OPT_(IN|OUT)_APPS - Settings.Global.putString(mContentResolver, Settings.Global.GUP_DEV_OPT_IN_APPS, + // corresponding Settings.Global.GAME_DRIVER_OPT_(IN|OUT)_APPS + Settings.Global.putString(mContentResolver, Settings.Global.GAME_DRIVER_OPT_IN_APPS, String.join(",", mDevOptInApps)); - Settings.Global.putString(mContentResolver, Settings.Global.GUP_DEV_OPT_OUT_APPS, + Settings.Global.putString(mContentResolver, Settings.Global.GAME_DRIVER_OPT_OUT_APPS, String.join(",", mDevOptOutApps)); return true; @@ -238,13 +240,13 @@ public class GupPreferenceController extends BasePreferenceController listPreference.setEntryValues(mEntryList); // Initialize preference default and summary with the opt in/out choices - // from Settings.Global.GUP_DEV_OPT_(IN|OUT)_APPS + // from Settings.Global.GAME_DRIVER_OPT_(IN|OUT)_APPS if (mDevOptOutApps.contains(packageName)) { listPreference.setValue(mPreferenceSystem); listPreference.setSummary(mPreferenceSystem); } else if (mDevOptInApps.contains(packageName)) { - listPreference.setValue(mPreferenceGup); - listPreference.setSummary(mPreferenceGup); + listPreference.setValue(mPreferenceGameDriver); + listPreference.setSummary(mPreferenceGameDriver); } else { listPreference.setValue(mPreferenceDefault); listPreference.setSummary(mPreferenceDefault); diff --git a/src/com/android/settings/development/gup/GameDriverContentObserver.java b/src/com/android/settings/development/gamedriver/GameDriverContentObserver.java similarity index 91% rename from src/com/android/settings/development/gup/GameDriverContentObserver.java rename to src/com/android/settings/development/gamedriver/GameDriverContentObserver.java index 92602134c0f..e31e04640b3 100644 --- a/src/com/android/settings/development/gup/GameDriverContentObserver.java +++ b/src/com/android/settings/development/gamedriver/GameDriverContentObserver.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.android.settings.development.gup; +package com.android.settings.development.gamedriver; import android.content.ContentResolver; import android.database.ContentObserver; @@ -27,6 +27,7 @@ import androidx.annotation.VisibleForTesting; * Helper class to observe Game Driver settings global change. */ public class GameDriverContentObserver extends ContentObserver { + interface OnGameDriverContentChangedListener { void onGameDriverContentChanged(); } @@ -47,7 +48,7 @@ public class GameDriverContentObserver extends ContentObserver { public void register(ContentResolver contentResolver) { contentResolver.registerContentObserver( - Settings.Global.getUriFor(Settings.Global.GUP_DEV_ALL_APPS), false, this); + Settings.Global.getUriFor(Settings.Global.GAME_DRIVER_ALL_APPS), false, this); } public void unregister(ContentResolver contentResolver) { diff --git a/src/com/android/settings/development/gup/GupDashboard.java b/src/com/android/settings/development/gamedriver/GameDriverDashboard.java similarity index 77% rename from src/com/android/settings/development/gup/GupDashboard.java rename to src/com/android/settings/development/gamedriver/GameDriverDashboard.java index 392de47bd73..b79af719996 100644 --- a/src/com/android/settings/development/gup/GupDashboard.java +++ b/src/com/android/settings/development/gamedriver/GameDriverDashboard.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.android.settings.development.gup; +package com.android.settings.development.gamedriver; import android.content.Context; import android.os.Bundle; @@ -31,12 +31,13 @@ import java.util.List; /** * Dashboard for Game Driver preferences. */ -public class GupDashboard extends DashboardFragment { - private static final String TAG = "GupDashboard"; +public class GameDriverDashboard extends DashboardFragment { + + private static final String TAG = "GameDriverDashboard"; @Override public int getMetricsCategory() { - return MetricsProto.MetricsEvent.SETTINGS_GUP_DASHBOARD; + return MetricsProto.MetricsEvent.SETTINGS_GAME_DRIVER_DASHBOARD; } @Override @@ -46,7 +47,7 @@ public class GupDashboard extends DashboardFragment { @Override protected int getPreferenceScreenResId() { - return R.xml.gup_settings; + return R.xml.game_driver_settings; } @Override @@ -60,8 +61,9 @@ public class GupDashboard extends DashboardFragment { final SettingsActivity activity = (SettingsActivity) getActivity(); final SwitchBar switchBar = activity.getSwitchBar(); - final GupGlobalSwitchBarController switchBarController = - new GupGlobalSwitchBarController(activity, new SwitchBarController(switchBar)); + final GameDriverGlobalSwitchBarController switchBarController = + new GameDriverGlobalSwitchBarController( + activity, new SwitchBarController(switchBar)); getLifecycle().addObserver(switchBarController); switchBar.show(); } diff --git a/src/com/android/settings/development/gup/GupEnableForAllAppsPreferenceController.java b/src/com/android/settings/development/gamedriver/GameDriverEnableForAllAppsPreferenceController.java similarity index 77% rename from src/com/android/settings/development/gup/GupEnableForAllAppsPreferenceController.java rename to src/com/android/settings/development/gamedriver/GameDriverEnableForAllAppsPreferenceController.java index b71e9f334eb..1afe5d6f4d6 100644 --- a/src/com/android/settings/development/gup/GupEnableForAllAppsPreferenceController.java +++ b/src/com/android/settings/development/gamedriver/GameDriverEnableForAllAppsPreferenceController.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.android.settings.development.gup; +package com.android.settings.development.gamedriver; import android.content.ContentResolver; import android.content.Context; @@ -36,13 +36,14 @@ import com.android.settingslib.development.DevelopmentSettingsEnabler; /** * Controller of global switch to enable Game Driver for all Apps. */ -public class GupEnableForAllAppsPreferenceController extends BasePreferenceController +public class GameDriverEnableForAllAppsPreferenceController extends BasePreferenceController implements Preference.OnPreferenceChangeListener, GameDriverContentObserver.OnGameDriverContentChangedListener, LifecycleObserver, OnStart, OnStop { - public static final int GUP_DEFAULT = 0; - public static final int GUP_ALL_APPS = 1; - public static final int GUP_OFF = 2; + + public static final int GAME_DRIVER_DEFAULT = 0; + public static final int GAME_DRIVER_ALL_APPS = 1; + public static final int GAME_DRIVER_OFF = 2; private final Context mContext; private final ContentResolver mContentResolver; @@ -51,7 +52,7 @@ public class GupEnableForAllAppsPreferenceController extends BasePreferenceContr private SwitchPreference mPreference; - public GupEnableForAllAppsPreferenceController(Context context, String key) { + public GameDriverEnableForAllAppsPreferenceController(Context context, String key) { super(context, key); mContext = context; mContentResolver = context.getContentResolver(); @@ -62,9 +63,9 @@ public class GupEnableForAllAppsPreferenceController extends BasePreferenceContr @Override public int getAvailabilityStatus() { return DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(mContext) - && (Settings.Global.getInt( - mContentResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_DEFAULT) - != GUP_OFF) + && (Settings.Global.getInt(mContentResolver, + Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_DEFAULT) + != GAME_DRIVER_OFF) ? AVAILABLE : CONDITIONALLY_UNAVAILABLE; } @@ -89,15 +90,16 @@ public class GupEnableForAllAppsPreferenceController extends BasePreferenceContr public void updateState(Preference preference) { final SwitchPreference switchPreference = (SwitchPreference) preference; switchPreference.setVisible(isAvailable()); - switchPreference.setChecked(Settings.Global.getInt(mContentResolver, - Settings.Global.GUP_DEV_ALL_APPS, GUP_DEFAULT) - == GUP_ALL_APPS); + switchPreference.setChecked( + Settings.Global.getInt( + mContentResolver, Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_DEFAULT) + == GAME_DRIVER_ALL_APPS); } @Override public boolean onPreferenceChange(Preference preference, Object newValue) { - Settings.Global.putInt(mContentResolver, Settings.Global.GUP_DEV_ALL_APPS, - (boolean) newValue ? GUP_ALL_APPS : GUP_DEFAULT); + Settings.Global.putInt(mContentResolver, Settings.Global.GAME_DRIVER_ALL_APPS, + (boolean) newValue ? GAME_DRIVER_ALL_APPS : GAME_DRIVER_DEFAULT); return true; } diff --git a/src/com/android/settings/development/gup/GameDriverFooterPreferenceController.java b/src/com/android/settings/development/gamedriver/GameDriverFooterPreferenceController.java similarity index 86% rename from src/com/android/settings/development/gup/GameDriverFooterPreferenceController.java rename to src/com/android/settings/development/gamedriver/GameDriverFooterPreferenceController.java index 332c1597cf8..bacbf95fa3b 100644 --- a/src/com/android/settings/development/gup/GameDriverFooterPreferenceController.java +++ b/src/com/android/settings/development/gamedriver/GameDriverFooterPreferenceController.java @@ -14,10 +14,10 @@ * limitations under the License. */ -package com.android.settings.development.gup; +package com.android.settings.development.gamedriver; -import static com.android.settings.development.gup.GupEnableForAllAppsPreferenceController.GUP_DEFAULT; -import static com.android.settings.development.gup.GupEnableForAllAppsPreferenceController.GUP_OFF; +import static com.android.settings.development.gamedriver.GameDriverEnableForAllAppsPreferenceController.GAME_DRIVER_DEFAULT; +import static com.android.settings.development.gamedriver.GameDriverEnableForAllAppsPreferenceController.GAME_DRIVER_OFF; import android.content.ContentResolver; import android.content.Context; @@ -41,6 +41,7 @@ import com.android.settingslib.widget.FooterPreference; public class GameDriverFooterPreferenceController extends BasePreferenceController implements GameDriverContentObserver.OnGameDriverContentChangedListener, LifecycleObserver, OnStart, OnStop { + private final ContentResolver mContentResolver; @VisibleForTesting GameDriverContentObserver mGameDriverContentObserver; @@ -57,8 +58,8 @@ public class GameDriverFooterPreferenceController extends BasePreferenceControll @Override public int getAvailabilityStatus() { return Settings.Global.getInt( - mContentResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_DEFAULT) - == GUP_OFF + mContentResolver, Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_DEFAULT) + == GAME_DRIVER_OFF ? AVAILABLE : CONDITIONALLY_UNAVAILABLE; } diff --git a/src/com/android/settings/development/gup/GupGlobalSwitchBarController.java b/src/com/android/settings/development/gamedriver/GameDriverGlobalSwitchBarController.java similarity index 64% rename from src/com/android/settings/development/gup/GupGlobalSwitchBarController.java rename to src/com/android/settings/development/gamedriver/GameDriverGlobalSwitchBarController.java index c6bdf9dd332..125d95b4cf6 100644 --- a/src/com/android/settings/development/gup/GupGlobalSwitchBarController.java +++ b/src/com/android/settings/development/gamedriver/GameDriverGlobalSwitchBarController.java @@ -14,11 +14,11 @@ * limitations under the License. */ -package com.android.settings.development.gup; +package com.android.settings.development.gamedriver; -import static com.android.settings.development.gup.GupEnableForAllAppsPreferenceController.GUP_ALL_APPS; -import static com.android.settings.development.gup.GupEnableForAllAppsPreferenceController.GUP_DEFAULT; -import static com.android.settings.development.gup.GupEnableForAllAppsPreferenceController.GUP_OFF; +import static com.android.settings.development.gamedriver.GameDriverEnableForAllAppsPreferenceController.GAME_DRIVER_ALL_APPS; +import static com.android.settings.development.gamedriver.GameDriverEnableForAllAppsPreferenceController.GAME_DRIVER_DEFAULT; +import static com.android.settings.development.gamedriver.GameDriverEnableForAllAppsPreferenceController.GAME_DRIVER_OFF; import android.content.ContentResolver; import android.content.Context; @@ -37,10 +37,11 @@ import com.android.settingslib.development.DevelopmentSettingsEnabler; /** * Controller of global switch bar used to fully turn off Game Driver. */ -public class GupGlobalSwitchBarController +public class GameDriverGlobalSwitchBarController implements SwitchWidgetController.OnSwitchChangeListener, GameDriverContentObserver.OnGameDriverContentChangedListener, LifecycleObserver, OnStart, OnStop { + private final Context mContext; private final ContentResolver mContentResolver; @VisibleForTesting @@ -48,7 +49,8 @@ public class GupGlobalSwitchBarController @VisibleForTesting GameDriverContentObserver mGameDriverContentObserver; - GupGlobalSwitchBarController(Context context, SwitchWidgetController switchWidgetController) { + GameDriverGlobalSwitchBarController( + Context context, SwitchWidgetController switchWidgetController) { mContext = context; mContentResolver = context.getContentResolver(); mGameDriverContentObserver = @@ -56,9 +58,10 @@ public class GupGlobalSwitchBarController mSwitchWidgetController = switchWidgetController; mSwitchWidgetController.setEnabled( DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(context)); - mSwitchWidgetController.setChecked(Settings.Global.getInt(mContentResolver, - Settings.Global.GUP_DEV_ALL_APPS, GUP_DEFAULT) - != GUP_OFF); + mSwitchWidgetController.setChecked( + Settings.Global.getInt( + mContentResolver, Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_DEFAULT) + != GAME_DRIVER_OFF); mSwitchWidgetController.setListener(this); } @@ -77,13 +80,16 @@ public class GupGlobalSwitchBarController @Override public boolean onSwitchToggled(boolean isChecked) { if (!isChecked) { - Settings.Global.putInt(mContentResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_OFF); + Settings.Global.putInt( + mContentResolver, Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_OFF); return true; } - if (Settings.Global.getInt(mContentResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_DEFAULT) - != GUP_ALL_APPS) { - Settings.Global.putInt(mContentResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_DEFAULT); + if (Settings.Global.getInt( + mContentResolver, Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_DEFAULT) + != GAME_DRIVER_ALL_APPS) { + Settings.Global.putInt( + mContentResolver, Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_DEFAULT); } return true; @@ -91,8 +97,9 @@ public class GupGlobalSwitchBarController @Override public void onGameDriverContentChanged() { - mSwitchWidgetController.setChecked(Settings.Global.getInt(mContentResolver, - Settings.Global.GUP_DEV_ALL_APPS, GUP_DEFAULT) - != GUP_OFF); + mSwitchWidgetController.setChecked( + Settings.Global.getInt( + mContentResolver, Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_DEFAULT) + != GAME_DRIVER_OFF); } } diff --git a/tests/robotests/assets/grandfather_not_implementing_index_provider b/tests/robotests/assets/grandfather_not_implementing_index_provider index 2c515d7a825..ffdaca8bda6 100644 --- a/tests/robotests/assets/grandfather_not_implementing_index_provider +++ b/tests/robotests/assets/grandfather_not_implementing_index_provider @@ -6,7 +6,7 @@ com.android.settings.accounts.AccountDetailDashboardFragment com.android.settings.fuelgauge.PowerUsageAnomalyDetails com.android.settings.fuelgauge.AdvancedPowerUsageDetail com.android.settings.development.featureflags.FeatureFlagsDashboard -com.android.settings.development.gup.GupDashboard +com.android.settings.development.gamedriver.GameDriverDashboard com.android.settings.development.qstile.DevelopmentTileConfigFragment com.android.settings.deviceinfo.StorageProfileFragment com.android.settings.notification.ChannelNotificationSettings diff --git a/tests/robotests/src/com/android/settings/development/gup/GupPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/development/gamedriver/GameDriverAppPreferenceControllerTest.java similarity index 88% rename from tests/robotests/src/com/android/settings/development/gup/GupPreferenceControllerTest.java rename to tests/robotests/src/com/android/settings/development/gamedriver/GameDriverAppPreferenceControllerTest.java index 8055c94ae3f..5906cbc74f2 100644 --- a/tests/robotests/src/com/android/settings/development/gup/GupPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/development/gamedriver/GameDriverAppPreferenceControllerTest.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.android.settings.development.gup; +package com.android.settings.development.gamedriver; import static com.android.settings.testutils.ApplicationTestUtils.buildInfo; import static com.google.common.truth.Truth.assertThat; @@ -48,9 +48,10 @@ import org.robolectric.RobolectricTestRunner; import org.robolectric.RuntimeEnvironment; @RunWith(RobolectricTestRunner.class) -public class GupPreferenceControllerTest { +public class GameDriverAppPreferenceControllerTest { + private static final int DEFAULT = 0; - private static final int GUP = 1; + private static final int GAME_DRIVER = 1; private static final int SYSTEM = 2; private static final String TEST_APP_NAME = "testApp"; private static final String TEST_PKG_NAME = "testPkg"; @@ -71,7 +72,7 @@ public class GupPreferenceControllerTest { private PreferenceGroup mGroup; private PreferenceManager mPreferenceManager; private ContentResolver mResolver; - private GupPreferenceController mController; + private GameDriverAppPreferenceController mController; private CharSequence[] mValueList; private String mDialogTitle; @@ -80,8 +81,9 @@ public class GupPreferenceControllerTest { MockitoAnnotations.initMocks(this); mContext = spy(RuntimeEnvironment.application); mResolver = mContext.getContentResolver(); - mValueList = mContext.getResources().getStringArray(R.array.gup_app_preference_values); - mDialogTitle = mContext.getResources().getString(R.string.gup_app_preference_title); + mValueList = + mContext.getResources().getStringArray(R.array.game_driver_app_preference_values); + mDialogTitle = mContext.getResources().getString(R.string.game_driver_app_preference_title); } @Test @@ -130,7 +132,7 @@ public class GupPreferenceControllerTest { } @Test - public void createPreference_configGup_shouldSetGupAttributes() { + public void createPreference_configGAME_DRIVER_shouldSetGameDriverAttributes() { loadConfig(TEST_PKG_NAME, ""); final ListPreference preference = mController.createListPreference(mContext, TEST_PKG_NAME, TEST_APP_NAME); @@ -140,9 +142,9 @@ public class GupPreferenceControllerTest { assertThat(preference.getDialogTitle()).isEqualTo(mDialogTitle); assertThat(preference.getEntries()).isEqualTo(mValueList); assertThat(preference.getEntryValues()).isEqualTo(mValueList); - assertThat(preference.getEntry()).isEqualTo(mValueList[GUP]); - assertThat(preference.getValue()).isEqualTo(mValueList[GUP]); - assertThat(preference.getSummary()).isEqualTo(mValueList[GUP]); + assertThat(preference.getEntry()).isEqualTo(mValueList[GAME_DRIVER]); + assertThat(preference.getValue()).isEqualTo(mValueList[GAME_DRIVER]); + assertThat(preference.getSummary()).isEqualTo(mValueList[GAME_DRIVER]); } @Test @@ -171,25 +173,25 @@ public class GupPreferenceControllerTest { assertThat(preference.getEntry()).isEqualTo(mValueList[DEFAULT]); assertThat(preference.getValue()).isEqualTo(mValueList[DEFAULT]); assertThat(preference.getSummary()).isEqualTo(mValueList[DEFAULT]); - assertThat(Settings.Global.getString(mResolver, Settings.Global.GUP_DEV_OPT_IN_APPS)) + assertThat(Settings.Global.getString(mResolver, Settings.Global.GAME_DRIVER_OPT_IN_APPS)) .isEqualTo(""); - assertThat(Settings.Global.getString(mResolver, Settings.Global.GUP_DEV_OPT_OUT_APPS)) + assertThat(Settings.Global.getString(mResolver, Settings.Global.GAME_DRIVER_OPT_OUT_APPS)) .isEqualTo(""); } @Test - public void onPreferenceChange_selectGup_shouldUpdateAttributesAndSettingsGlobal() { + public void onPreferenceChange_selectGAME_DRIVER_shouldUpdateAttributesAndSettingsGlobal() { loadDefaultConfig(); final ListPreference preference = mController.createListPreference(mContext, TEST_PKG_NAME, TEST_APP_NAME); - mController.onPreferenceChange(preference, mValueList[GUP]); + mController.onPreferenceChange(preference, mValueList[GAME_DRIVER]); - assertThat(preference.getEntry()).isEqualTo(mValueList[GUP]); - assertThat(preference.getValue()).isEqualTo(mValueList[GUP]); - assertThat(preference.getSummary()).isEqualTo(mValueList[GUP]); - assertThat(Settings.Global.getString(mResolver, Settings.Global.GUP_DEV_OPT_IN_APPS)) + assertThat(preference.getEntry()).isEqualTo(mValueList[GAME_DRIVER]); + assertThat(preference.getValue()).isEqualTo(mValueList[GAME_DRIVER]); + assertThat(preference.getSummary()).isEqualTo(mValueList[GAME_DRIVER]); + assertThat(Settings.Global.getString(mResolver, Settings.Global.GAME_DRIVER_OPT_IN_APPS)) .isEqualTo(TEST_PKG_NAME); - assertThat(Settings.Global.getString(mResolver, Settings.Global.GUP_DEV_OPT_OUT_APPS)) + assertThat(Settings.Global.getString(mResolver, Settings.Global.GAME_DRIVER_OPT_OUT_APPS)) .isEqualTo(""); } @@ -203,9 +205,9 @@ public class GupPreferenceControllerTest { assertThat(preference.getEntry()).isEqualTo(mValueList[SYSTEM]); assertThat(preference.getValue()).isEqualTo(mValueList[SYSTEM]); assertThat(preference.getSummary()).isEqualTo(mValueList[SYSTEM]); - assertThat(Settings.Global.getString(mResolver, Settings.Global.GUP_DEV_OPT_IN_APPS)) + assertThat(Settings.Global.getString(mResolver, Settings.Global.GAME_DRIVER_OPT_IN_APPS)) .isEqualTo(""); - assertThat(Settings.Global.getString(mResolver, Settings.Global.GUP_DEV_OPT_OUT_APPS)) + assertThat(Settings.Global.getString(mResolver, Settings.Global.GAME_DRIVER_OPT_OUT_APPS)) .isEqualTo(TEST_PKG_NAME); } @@ -227,10 +229,10 @@ public class GupPreferenceControllerTest { private void loadDefaultConfig() { loadConfig("", ""); } private void loadConfig(String optIn, String optOut) { - Settings.Global.putString(mResolver, Settings.Global.GUP_DEV_OPT_IN_APPS, optIn); - Settings.Global.putString(mResolver, Settings.Global.GUP_DEV_OPT_OUT_APPS, optOut); + Settings.Global.putString(mResolver, Settings.Global.GAME_DRIVER_OPT_IN_APPS, optIn); + Settings.Global.putString(mResolver, Settings.Global.GAME_DRIVER_OPT_OUT_APPS, optOut); - mController = new GupPreferenceController(mContext, "testKey"); + mController = new GameDriverAppPreferenceController(mContext, "testKey"); mGroup = spy(new PreferenceCategory(mContext)); final PreferenceManager preferenceManager = new PreferenceManager(mContext); when(mGroup.getContext()).thenReturn(mContext); diff --git a/tests/robotests/src/com/android/settings/development/gup/GameDriverContentObserverTest.java b/tests/robotests/src/com/android/settings/development/gamedriver/GameDriverContentObserverTest.java similarity index 93% rename from tests/robotests/src/com/android/settings/development/gup/GameDriverContentObserverTest.java rename to tests/robotests/src/com/android/settings/development/gamedriver/GameDriverContentObserverTest.java index 6939ac9e2de..caaf896f5fc 100644 --- a/tests/robotests/src/com/android/settings/development/gup/GameDriverContentObserverTest.java +++ b/tests/robotests/src/com/android/settings/development/gamedriver/GameDriverContentObserverTest.java @@ -14,13 +14,14 @@ * limitations under the License. */ -package com.android.settings.development.gup; +package com.android.settings.development.gamedriver; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; import android.content.ContentResolver; import android.provider.Settings; + import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -31,6 +32,7 @@ import org.robolectric.RuntimeEnvironment; @RunWith(RobolectricTestRunner.class) public class GameDriverContentObserverTest { + @Mock private ContentResolver mResolver; @Mock @@ -57,7 +59,7 @@ public class GameDriverContentObserverTest { mGameDriverContentObserver.register(mResolver); verify(mResolver).registerContentObserver( - Settings.Global.getUriFor(Settings.Global.GUP_DEV_ALL_APPS), false, + Settings.Global.getUriFor(Settings.Global.GAME_DRIVER_ALL_APPS), false, mGameDriverContentObserver); } diff --git a/tests/robotests/src/com/android/settings/development/gup/GupDashboardTest.java b/tests/robotests/src/com/android/settings/development/gamedriver/GameDriverDashboardTest.java similarity index 69% rename from tests/robotests/src/com/android/settings/development/gup/GupDashboardTest.java rename to tests/robotests/src/com/android/settings/development/gamedriver/GameDriverDashboardTest.java index 17278ef08ea..29e74148544 100644 --- a/tests/robotests/src/com/android/settings/development/gup/GupDashboardTest.java +++ b/tests/robotests/src/com/android/settings/development/gamedriver/GameDriverDashboardTest.java @@ -14,11 +14,12 @@ * limitations under the License. */ -package com.android.settings.development.gup; +package com.android.settings.development.gamedriver; import static com.google.common.truth.Truth.assertThat; import com.android.internal.logging.nano.MetricsProto; + import com.android.settings.R; import org.junit.Before; @@ -27,12 +28,13 @@ import org.junit.runner.RunWith; import org.robolectric.RobolectricTestRunner; @RunWith(RobolectricTestRunner.class) -public class GupDashboardTest { - private GupDashboard mDashboard; +public class GameDriverDashboardTest { + + private GameDriverDashboard mDashboard; @Before public void setUp() { - mDashboard = new GupDashboard(); + mDashboard = new GameDriverDashboard(); } @Test @@ -41,14 +43,13 @@ public class GupDashboardTest { } @Test - public void getMetricesCategory_shouldReturnGupDashboard() { + public void getMetricesCategory_shouldReturnGameDriverDashboard() { assertThat(mDashboard.getMetricsCategory()) - .isEqualTo(MetricsProto.MetricsEvent.SETTINGS_GUP_DASHBOARD); + .isEqualTo(MetricsProto.MetricsEvent.SETTINGS_GAME_DRIVER_DASHBOARD); } @Test - public void getPreferenceScreen_shouldReturnGupSettings() { - assertThat(mDashboard.getPreferenceScreenResId()) - .isEqualTo(R.xml.gup_settings); + public void getPreferenceScreen_shouldReturnGameDriverSettings() { + assertThat(mDashboard.getPreferenceScreenResId()).isEqualTo(R.xml.game_driver_settings); } } diff --git a/tests/robotests/src/com/android/settings/development/gup/GupEnableForAllAppsPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/development/gamedriver/GameDriverEnableForAllAppsPreferenceControllerTest.java similarity index 69% rename from tests/robotests/src/com/android/settings/development/gup/GupEnableForAllAppsPreferenceControllerTest.java rename to tests/robotests/src/com/android/settings/development/gamedriver/GameDriverEnableForAllAppsPreferenceControllerTest.java index cd8c7108106..4ee702c5ff0 100644 --- a/tests/robotests/src/com/android/settings/development/gup/GupEnableForAllAppsPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/development/gamedriver/GameDriverEnableForAllAppsPreferenceControllerTest.java @@ -14,10 +14,10 @@ * limitations under the License. */ -package com.android.settings.development.gup; +package com.android.settings.development.gamedriver; -import static com.android.settings.development.gup.GupEnableForAllAppsPreferenceController.GUP_ALL_APPS; -import static com.android.settings.development.gup.GupEnableForAllAppsPreferenceController.GUP_DEFAULT; +import static com.android.settings.development.gamedriver.GameDriverEnableForAllAppsPreferenceController.GAME_DRIVER_ALL_APPS; +import static com.android.settings.development.gamedriver.GameDriverEnableForAllAppsPreferenceController.GAME_DRIVER_DEFAULT; import static com.google.common.truth.Truth.assertThat; import static org.mockito.Mockito.atLeastOnce; @@ -40,7 +40,8 @@ import org.robolectric.RobolectricTestRunner; import org.robolectric.RuntimeEnvironment; @RunWith(RobolectricTestRunner.class) -public class GupEnableForAllAppsPreferenceControllerTest { +public class GameDriverEnableForAllAppsPreferenceControllerTest { + @Mock private PreferenceScreen mScreen; @Mock @@ -50,14 +51,14 @@ public class GupEnableForAllAppsPreferenceControllerTest { private Context mContext; private ContentResolver mResolver; - private GupEnableForAllAppsPreferenceController mController; + private GameDriverEnableForAllAppsPreferenceController mController; @Before public void setUp() { MockitoAnnotations.initMocks(this); mContext = RuntimeEnvironment.application; mResolver = mContext.getContentResolver(); - mController = new GupEnableForAllAppsPreferenceController(mContext, "testKey"); + mController = new GameDriverEnableForAllAppsPreferenceController(mContext, "testKey"); when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); mController.displayPreference(mScreen); @@ -66,7 +67,8 @@ public class GupEnableForAllAppsPreferenceControllerTest { @Test public void displayPreference_shouldAddSwitchPreference() { - Settings.Global.putInt(mResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_DEFAULT); + Settings.Global.putInt( + mResolver, Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_DEFAULT); mController.updateState(mPreference); verify(mPreference).setChecked(false); @@ -90,19 +92,23 @@ public class GupEnableForAllAppsPreferenceControllerTest { @Test public void onPreferenceChange_check_shouldUpdateSettingsGlobal() { - Settings.Global.putInt(mResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_DEFAULT); + Settings.Global.putInt( + mResolver, Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_DEFAULT); mController.onPreferenceChange(mPreference, true); - assertThat(Settings.Global.getInt(mResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_DEFAULT)) - .isEqualTo(GUP_ALL_APPS); + assertThat(Settings.Global.getInt( + mResolver, Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_DEFAULT)) + .isEqualTo(GAME_DRIVER_ALL_APPS); } @Test public void onPreferenceChange_uncheck_shouldUpdateSettingsGlobal() { - Settings.Global.putInt(mResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_ALL_APPS); + Settings.Global.putInt( + mResolver, Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_ALL_APPS); mController.onPreferenceChange(mPreference, false); - assertThat(Settings.Global.getInt(mResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_DEFAULT)) - .isEqualTo(GUP_DEFAULT); + assertThat(Settings.Global.getInt( + mResolver, Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_DEFAULT)) + .isEqualTo(GAME_DRIVER_DEFAULT); } } diff --git a/tests/robotests/src/com/android/settings/development/gup/GameDriverFooterPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/development/gamedriver/GameDriverFooterPreferenceControllerTest.java similarity index 82% rename from tests/robotests/src/com/android/settings/development/gup/GameDriverFooterPreferenceControllerTest.java rename to tests/robotests/src/com/android/settings/development/gamedriver/GameDriverFooterPreferenceControllerTest.java index 17ddbda90a4..19676ed8b6a 100644 --- a/tests/robotests/src/com/android/settings/development/gup/GameDriverFooterPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/development/gamedriver/GameDriverFooterPreferenceControllerTest.java @@ -14,13 +14,13 @@ * limitations under the License. */ -package com.android.settings.development.gup; +package com.android.settings.development.gamedriver; import static com.android.settings.core.BasePreferenceController.AVAILABLE; import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_UNAVAILABLE; -import static com.android.settings.development.gup.GupEnableForAllAppsPreferenceController.GUP_ALL_APPS; -import static com.android.settings.development.gup.GupEnableForAllAppsPreferenceController.GUP_DEFAULT; -import static com.android.settings.development.gup.GupEnableForAllAppsPreferenceController.GUP_OFF; +import static com.android.settings.development.gamedriver.GameDriverEnableForAllAppsPreferenceController.GAME_DRIVER_ALL_APPS; +import static com.android.settings.development.gamedriver.GameDriverEnableForAllAppsPreferenceController.GAME_DRIVER_DEFAULT; +import static com.android.settings.development.gamedriver.GameDriverEnableForAllAppsPreferenceController.GAME_DRIVER_OFF; import static com.google.common.truth.Truth.assertThat; import static org.mockito.Mockito.spy; @@ -45,6 +45,7 @@ import org.robolectric.RuntimeEnvironment; @RunWith(RobolectricTestRunner.class) public class GameDriverFooterPreferenceControllerTest { + @Mock private PreferenceScreen mScreen; @Mock @@ -67,21 +68,23 @@ public class GameDriverFooterPreferenceControllerTest { @Test public void getAvailabilityStatus_gameDriverOff_availableUnsearchable() { - Settings.Global.putInt(mResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_OFF); + Settings.Global.putInt(mResolver, Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_OFF); assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE); } @Test public void getAvailabilityStatus_gameDriverDefault_conditionallyUnavailable() { - Settings.Global.putInt(mResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_DEFAULT); + Settings.Global.putInt( + mResolver, Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_DEFAULT); assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE); } @Test public void getAvailabilityStatus_gameDriverAllApps_conditionallyUnavailable() { - Settings.Global.putInt(mResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_ALL_APPS); + Settings.Global.putInt( + mResolver, Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_ALL_APPS); assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE); } diff --git a/tests/robotests/src/com/android/settings/development/gup/GupGlobalSwitchBarControllerTest.java b/tests/robotests/src/com/android/settings/development/gamedriver/GameDriverGlobalSwitchBarControllerTest.java similarity index 54% rename from tests/robotests/src/com/android/settings/development/gup/GupGlobalSwitchBarControllerTest.java rename to tests/robotests/src/com/android/settings/development/gamedriver/GameDriverGlobalSwitchBarControllerTest.java index 0d3ed481fc8..f0a302f2fb5 100644 --- a/tests/robotests/src/com/android/settings/development/gup/GupGlobalSwitchBarControllerTest.java +++ b/tests/robotests/src/com/android/settings/development/gamedriver/GameDriverGlobalSwitchBarControllerTest.java @@ -14,10 +14,10 @@ * limitations under the License. */ -package com.android.settings.development.gup; +package com.android.settings.development.gamedriver; -import static com.android.settings.development.gup.GupEnableForAllAppsPreferenceController.GUP_DEFAULT; -import static com.android.settings.development.gup.GupEnableForAllAppsPreferenceController.GUP_OFF; +import static com.android.settings.development.gamedriver.GameDriverEnableForAllAppsPreferenceController.GAME_DRIVER_DEFAULT; +import static com.android.settings.development.gamedriver.GameDriverEnableForAllAppsPreferenceController.GAME_DRIVER_OFF; import static com.google.common.truth.Truth.assertThat; import static org.mockito.Mockito.verify; @@ -39,7 +39,8 @@ import org.robolectric.RobolectricTestRunner; import org.robolectric.RuntimeEnvironment; @RunWith(RobolectricTestRunner.class) -public class GupGlobalSwitchBarControllerTest { +public class GameDriverGlobalSwitchBarControllerTest { + @Mock private SwitchBar mSwitchBar; @Mock @@ -49,7 +50,7 @@ public class GupGlobalSwitchBarControllerTest { private Context mContext; private ContentResolver mResolver; - private GupGlobalSwitchBarController mController; + private GameDriverGlobalSwitchBarController mController; @Before public void setUp() { @@ -59,27 +60,28 @@ public class GupGlobalSwitchBarControllerTest { } @Test - public void constructor_gupOn_shouldCheckSwitchBar() { - Settings.Global.putInt(mResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_DEFAULT); - mController = - new GupGlobalSwitchBarController(mContext, new SwitchBarController(mSwitchBar)); + public void constructor_gameDriverOn_shouldCheckSwitchBar() { + Settings.Global.putInt( + mResolver, Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_DEFAULT); + mController = new GameDriverGlobalSwitchBarController( + mContext, new SwitchBarController(mSwitchBar)); verify(mSwitchBar).setChecked(true); } @Test - public void constructor_gupOff_shouldUncheckSwitchBar() { - Settings.Global.putInt(mResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_OFF); - mController = - new GupGlobalSwitchBarController(mContext, new SwitchBarController(mSwitchBar)); + public void constructor_gameDriverOff_shouldUncheckSwitchBar() { + Settings.Global.putInt(mResolver, Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_OFF); + mController = new GameDriverGlobalSwitchBarController( + mContext, new SwitchBarController(mSwitchBar)); verify(mSwitchBar).setChecked(false); } @Test public void onStart_shouldStartListeningAndRegister() { - mController = - new GupGlobalSwitchBarController(mContext, new SwitchBarController(mSwitchBar)); + mController = new GameDriverGlobalSwitchBarController( + mContext, new SwitchBarController(mSwitchBar)); mController.mSwitchWidgetController = mSwitchWidgetController; mController.mGameDriverContentObserver = mGameDriverContentObserver; mController.onStart(); @@ -90,8 +92,8 @@ public class GupGlobalSwitchBarControllerTest { @Test public void onStop_shouldStopListeningAndUnregister() { - mController = - new GupGlobalSwitchBarController(mContext, new SwitchBarController(mSwitchBar)); + mController = new GameDriverGlobalSwitchBarController( + mContext, new SwitchBarController(mSwitchBar)); mController.mSwitchWidgetController = mSwitchWidgetController; mController.mGameDriverContentObserver = mGameDriverContentObserver; mController.onStop(); @@ -101,24 +103,27 @@ public class GupGlobalSwitchBarControllerTest { } @Test - public void onSwitchToggled_checked_shouldTurnOnGup() { - Settings.Global.putInt(mResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_OFF); - mController = - new GupGlobalSwitchBarController(mContext, new SwitchBarController(mSwitchBar)); + public void onSwitchToggled_checked_shouldTurnOnGameDriver() { + Settings.Global.putInt(mResolver, Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_OFF); + mController = new GameDriverGlobalSwitchBarController( + mContext, new SwitchBarController(mSwitchBar)); mController.onSwitchToggled(true); - assertThat(Settings.Global.getInt(mResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_DEFAULT)) - .isEqualTo(GUP_DEFAULT); + assertThat(Settings.Global.getInt( + mResolver, Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_DEFAULT)) + .isEqualTo(GAME_DRIVER_DEFAULT); } @Test - public void onSwitchToggled_unchecked_shouldTurnOffGup() { - Settings.Global.putInt(mResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_DEFAULT); - mController = - new GupGlobalSwitchBarController(mContext, new SwitchBarController(mSwitchBar)); + public void onSwitchToggled_unchecked_shouldTurnOffGameDriver() { + Settings.Global.putInt( + mResolver, Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_DEFAULT); + mController = new GameDriverGlobalSwitchBarController( + mContext, new SwitchBarController(mSwitchBar)); mController.onSwitchToggled(false); - assertThat(Settings.Global.getInt(mResolver, Settings.Global.GUP_DEV_ALL_APPS, GUP_DEFAULT)) - .isEqualTo(GUP_OFF); + assertThat(Settings.Global.getInt( + mResolver, Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_DEFAULT)) + .isEqualTo(GAME_DRIVER_OFF); } } From 9e758e1409a5437749567fe7427995a89048e6e9 Mon Sep 17 00:00:00 2001 From: Yiwei Zhang Date: Thu, 31 Jan 2019 18:25:00 -0800 Subject: [PATCH 8/8] Game Driver: Fix EnableForAllApps switch and test Bug: 123707483 Bug: 119221883 Test: Build, flash, boot and make RunSettingsRoboTests Change-Id: Iffbe3355b37763d4fa71b96271c1ba448fb6cdc5 Merged-In: Iffbe3355b37763d4fa71b96271c1ba448fb6cdc5 --- ...erEnableForAllAppsPreferenceController.java | 15 ++++++++++++++- .../GameDriverGlobalSwitchBarController.java | 18 ++++++++++-------- ...ableForAllAppsPreferenceControllerTest.java | 7 +++++-- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/src/com/android/settings/development/gamedriver/GameDriverEnableForAllAppsPreferenceController.java b/src/com/android/settings/development/gamedriver/GameDriverEnableForAllAppsPreferenceController.java index 1afe5d6f4d6..7f9b74b80b0 100644 --- a/src/com/android/settings/development/gamedriver/GameDriverEnableForAllAppsPreferenceController.java +++ b/src/com/android/settings/development/gamedriver/GameDriverEnableForAllAppsPreferenceController.java @@ -74,6 +74,7 @@ public class GameDriverEnableForAllAppsPreferenceController extends BasePreferen public void displayPreference(PreferenceScreen screen) { super.displayPreference(screen); mPreference = (SwitchPreference) screen.findPreference(getPreferenceKey()); + mPreference.setOnPreferenceChangeListener(this); } @Override @@ -98,8 +99,20 @@ public class GameDriverEnableForAllAppsPreferenceController extends BasePreferen @Override public boolean onPreferenceChange(Preference preference, Object newValue) { + final boolean isChecked = (boolean) newValue; + final int gameDriver = Settings.Global.getInt( + mContentResolver, Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_DEFAULT); + + if (isChecked && gameDriver == GAME_DRIVER_ALL_APPS) { + return true; + } + + if (!isChecked && (gameDriver == GAME_DRIVER_DEFAULT || gameDriver == GAME_DRIVER_OFF)) { + return true; + } + Settings.Global.putInt(mContentResolver, Settings.Global.GAME_DRIVER_ALL_APPS, - (boolean) newValue ? GAME_DRIVER_ALL_APPS : GAME_DRIVER_DEFAULT); + isChecked ? GAME_DRIVER_ALL_APPS : GAME_DRIVER_DEFAULT); return true; } diff --git a/src/com/android/settings/development/gamedriver/GameDriverGlobalSwitchBarController.java b/src/com/android/settings/development/gamedriver/GameDriverGlobalSwitchBarController.java index 125d95b4cf6..d84c28f3f5b 100644 --- a/src/com/android/settings/development/gamedriver/GameDriverGlobalSwitchBarController.java +++ b/src/com/android/settings/development/gamedriver/GameDriverGlobalSwitchBarController.java @@ -79,19 +79,21 @@ public class GameDriverGlobalSwitchBarController @Override public boolean onSwitchToggled(boolean isChecked) { - if (!isChecked) { - Settings.Global.putInt( - mContentResolver, Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_OFF); + final int gameDriver = Settings.Global.getInt( + mContentResolver, Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_DEFAULT); + + if (isChecked + && (gameDriver == GAME_DRIVER_DEFAULT || gameDriver == GAME_DRIVER_ALL_APPS)) { return true; } - if (Settings.Global.getInt( - mContentResolver, Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_DEFAULT) - != GAME_DRIVER_ALL_APPS) { - Settings.Global.putInt( - mContentResolver, Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_DEFAULT); + if (!isChecked && gameDriver == GAME_DRIVER_OFF) { + return true; } + Settings.Global.putInt(mContentResolver, Settings.Global.GAME_DRIVER_ALL_APPS, + isChecked ? GAME_DRIVER_DEFAULT : GAME_DRIVER_OFF); + return true; } diff --git a/tests/robotests/src/com/android/settings/development/gamedriver/GameDriverEnableForAllAppsPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/development/gamedriver/GameDriverEnableForAllAppsPreferenceControllerTest.java index 4ee702c5ff0..8f4c88ab7ce 100644 --- a/tests/robotests/src/com/android/settings/development/gamedriver/GameDriverEnableForAllAppsPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/development/gamedriver/GameDriverEnableForAllAppsPreferenceControllerTest.java @@ -58,11 +58,14 @@ public class GameDriverEnableForAllAppsPreferenceControllerTest { MockitoAnnotations.initMocks(this); mContext = RuntimeEnvironment.application; mResolver = mContext.getContentResolver(); + + Settings.Global.putInt(mResolver, Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 1); + Settings.Global.putInt( + mResolver, Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_DEFAULT); + mController = new GameDriverEnableForAllAppsPreferenceController(mContext, "testKey"); when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); mController.displayPreference(mScreen); - - Settings.Global.putInt(mResolver, Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 1); } @Test