Only show graphics driver option when the apk is available.
Previously we always show all options, this patch makes the dashboard
only shows the option when the driver apk is available.
Bug: b/148626177
Test: make RunSettingsRoboTests ROBOTEST_FILTER=GraphicsDriver
Change-Id: Ifde5929d826d5ab542e855aa334546dd744b138b
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 0c9fa05..95ca27c 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -11002,6 +11002,7 @@
<string-array name="graphics_driver_all_apps_preference_values">
<item>@string/graphics_driver_app_preference_default</item>
<item>@string/graphics_driver_app_preference_game_driver</item>
+ <item>@string/graphics_driver_app_preference_prerelease_driver</item>
</string-array>
<!-- All the values of graphics driver for app preference [CHAR LIMIT=50] -->
<string-array name="graphics_driver_app_preference_values">
diff --git a/res/xml/graphics_driver_settings.xml b/res/xml/graphics_driver_settings.xml
index c72c8fb..a1fa78c 100644
--- a/res/xml/graphics_driver_settings.xml
+++ b/res/xml/graphics_driver_settings.xml
@@ -24,8 +24,6 @@
android:key="graphics_driver_all_apps_preference"
android:title="@string/graphics_driver_all_apps_preference_title"
android:dialogTitle="@string/graphics_driver_all_apps_preference_title"
- android:entries="@array/graphics_driver_all_apps_preference_values"
- android:entryValues="@array/graphics_driver_all_apps_preference_values"
settings:controller="com.android.settings.development.graphicsdriver.GraphicsDriverEnableForAllAppsPreferenceController">
</ListPreference>
diff --git a/src/com/android/settings/development/graphicsdriver/GraphicsDriverAppPreferenceController.java b/src/com/android/settings/development/graphicsdriver/GraphicsDriverAppPreferenceController.java
index 2013b45..b1a31fb 100644
--- a/src/com/android/settings/development/graphicsdriver/GraphicsDriverAppPreferenceController.java
+++ b/src/com/android/settings/development/graphicsdriver/GraphicsDriverAppPreferenceController.java
@@ -26,9 +26,7 @@
import android.content.res.Resources;
import android.os.Handler;
import android.os.Looper;
-import android.os.SystemProperties;
import android.provider.Settings;
-import android.text.TextUtils;
import androidx.annotation.VisibleForTesting;
import androidx.preference.ListPreference;
@@ -60,9 +58,6 @@
GraphicsDriverContentObserver.OnGraphicsDriverContentChangedListener, LifecycleObserver,
OnStart, OnStop {
- private static final String PROPERTY_GFX_DRIVER_GAME = "ro.gfx.driver.0";
- private static final String PROPERTY_GFX_DRIVER_PRERELEASE = "ro.gfx.driver.1";
-
private final Context mContext;
private final ContentResolver mContentResolver;
private final String mPreferenceTitle;
@@ -98,7 +93,8 @@
mPreferencePrereleaseDriver =
resources.getString(R.string.graphics_driver_app_preference_prerelease_driver);
mPreferenceSystem = resources.getString(R.string.graphics_driver_app_preference_system);
- mEntryList = constructEntryList();
+ mEntryList = GraphicsDriverEnableForAllAppsPreferenceController.constructEntryList(
+ mContext, true);
// TODO: Move this task to background if there's potential ANR/Jank.
// Update the UI when all the app infos are ready.
@@ -195,28 +191,6 @@
updateState(mPreferenceGroup);
}
- /**
- * Constructs and returns a list of graphics driver choices.
- */
- public CharSequence[] constructEntryList() {
- final String prereleaseDriverPackageName =
- SystemProperties.get(PROPERTY_GFX_DRIVER_PRERELEASE);
- final String gameDriverPackageName = SystemProperties.get(PROPERTY_GFX_DRIVER_GAME);
-
- List<CharSequence> entryList = new ArrayList<>();
- entryList.add(mPreferenceDefault);
- if (!TextUtils.isEmpty(prereleaseDriverPackageName)) {
- entryList.add(mPreferencePrereleaseDriver);
- }
- if (!TextUtils.isEmpty(gameDriverPackageName)) {
- entryList.add(mPreferenceGameDriver);
- }
- entryList.add(mPreferenceSystem);
- CharSequence[] filteredEntryList = new CharSequence[entryList.size()];
- filteredEntryList = entryList.toArray(filteredEntryList);
- return filteredEntryList;
- }
-
// AppInfo class to achieve loading the application label only once
class AppInfo {
AppInfo(PackageManager packageManager, ApplicationInfo applicationInfo) {
diff --git a/src/com/android/settings/development/graphicsdriver/GraphicsDriverEnableForAllAppsPreferenceController.java b/src/com/android/settings/development/graphicsdriver/GraphicsDriverEnableForAllAppsPreferenceController.java
index 4baa993..29f1014 100644
--- a/src/com/android/settings/development/graphicsdriver/GraphicsDriverEnableForAllAppsPreferenceController.java
+++ b/src/com/android/settings/development/graphicsdriver/GraphicsDriverEnableForAllAppsPreferenceController.java
@@ -18,10 +18,15 @@
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.Build;
import android.os.Handler;
import android.os.Looper;
+import android.os.SystemProperties;
import android.provider.Settings;
+import android.text.TextUtils;
import androidx.annotation.VisibleForTesting;
import androidx.preference.ListPreference;
@@ -35,6 +40,11 @@
import com.android.settingslib.core.lifecycle.events.OnStop;
import com.android.settingslib.development.DevelopmentSettingsEnabler;
+import dalvik.system.VMRuntime;
+
+import java.util.ArrayList;
+import java.util.List;
+
/**
* Controller of global switch to enable Game Driver for all Apps.
*/
@@ -47,6 +57,8 @@
public static final int GAME_DRIVER_ALL_APPS = 1;
public static final int GAME_DRIVER_PRERELEASE_ALL_APPS = 2;
public static final int GAME_DRIVER_OFF = 3;
+ public static final String PROPERTY_GFX_DRIVER_GAME = "ro.gfx.driver.0";
+ public static final String PROPERTY_GFX_DRIVER_PRERELEASE = "ro.gfx.driver.1";
private final Context mContext;
private final ContentResolver mContentResolver;
@@ -54,6 +66,8 @@
private final String mPreferenceGameDriver;
private final String mPreferencePrereleaseDriver;
@VisibleForTesting
+ CharSequence[] mEntryList;
+ @VisibleForTesting
GraphicsDriverContentObserver mGraphicsDriverContentObserver;
private ListPreference mPreference;
@@ -69,6 +83,7 @@
resources.getString(R.string.graphics_driver_app_preference_game_driver);
mPreferencePrereleaseDriver =
resources.getString(R.string.graphics_driver_app_preference_prerelease_driver);
+ mEntryList = constructEntryList(mContext, false);
mGraphicsDriverContentObserver =
new GraphicsDriverContentObserver(new Handler(Looper.getMainLooper()), this);
}
@@ -87,6 +102,8 @@
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mPreference = screen.findPreference(getPreferenceKey());
+ mPreference.setEntries(mEntryList);
+ mPreference.setEntryValues(mEntryList);
mPreference.setOnPreferenceChangeListener(this);
}
@@ -147,4 +164,64 @@
public void onGraphicsDriverContentChanged() {
updateState(mPreference);
}
+
+ /**
+ * Constructs and returns a list of graphics driver choices.
+ */
+ public static CharSequence[] constructEntryList(Context context, boolean withSystem) {
+ final Resources resources = context.getResources();
+ final String prereleaseDriverPackageName =
+ SystemProperties.get(PROPERTY_GFX_DRIVER_PRERELEASE);
+ final String gameDriverPackageName = SystemProperties.get(PROPERTY_GFX_DRIVER_GAME);
+
+ List<CharSequence> entryList = new ArrayList<>();
+ entryList.add(resources.getString(R.string.graphics_driver_app_preference_default));
+ final PackageManager pm = context.getPackageManager();
+ if (!TextUtils.isEmpty(prereleaseDriverPackageName)
+ && hasDriverPackage(pm, prereleaseDriverPackageName)) {
+ entryList.add(resources.getString(
+ R.string.graphics_driver_app_preference_prerelease_driver));
+ }
+ if (!TextUtils.isEmpty(gameDriverPackageName)
+ && hasDriverPackage(pm, gameDriverPackageName)) {
+ entryList.add(resources.getString(R.string.graphics_driver_app_preference_game_driver));
+ }
+ if (withSystem) {
+ entryList.add(resources.getString(R.string.graphics_driver_app_preference_system));
+ }
+ CharSequence[] filteredEntryList = new CharSequence[entryList.size()];
+ filteredEntryList = entryList.toArray(filteredEntryList);
+ return filteredEntryList;
+ }
+
+ private static boolean hasDriverPackage(PackageManager pm, String driverPackageName) {
+ final ApplicationInfo driverAppInfo;
+ try {
+ driverAppInfo = pm.getApplicationInfo(driverPackageName,
+ PackageManager.MATCH_SYSTEM_ONLY);
+ } catch (PackageManager.NameNotFoundException e) {
+ return false;
+ }
+ if (driverAppInfo.targetSdkVersion < Build.VERSION_CODES.O) {
+ return false;
+ }
+ final String abi = chooseAbi(driverAppInfo);
+ if (abi == null) {
+ return false;
+ }
+ return true;
+ }
+
+ private static String chooseAbi(ApplicationInfo ai) {
+ final String isa = VMRuntime.getCurrentInstructionSet();
+ if (ai.primaryCpuAbi != null
+ && isa.equals(VMRuntime.getInstructionSet(ai.primaryCpuAbi))) {
+ return ai.primaryCpuAbi;
+ }
+ if (ai.secondaryCpuAbi != null
+ && isa.equals(VMRuntime.getInstructionSet(ai.secondaryCpuAbi))) {
+ return ai.secondaryCpuAbi;
+ }
+ return null;
+ }
}
diff --git a/tests/robotests/src/com/android/settings/development/graphicsdriver/GraphicsDriverEnableForAllAppsPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/development/graphicsdriver/GraphicsDriverEnableForAllAppsPreferenceControllerTest.java
index 366a18d..920e9d1 100644
--- a/tests/robotests/src/com/android/settings/development/graphicsdriver/GraphicsDriverEnableForAllAppsPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/development/graphicsdriver/GraphicsDriverEnableForAllAppsPreferenceControllerTest.java
@@ -82,6 +82,8 @@
mResolver, Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_DEFAULT);
mController = new GraphicsDriverEnableForAllAppsPreferenceController(mContext, "testKey");
+ mController.mEntryList = mContext.getResources().getStringArray(
+ R.array.graphics_driver_all_apps_preference_values);
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
mController.displayPreference(mScreen);
}