From fc4155a02c4d58448a54810869928025b82bca93 Mon Sep 17 00:00:00 2001 From: Peiyong Lin Date: Fri, 21 Feb 2020 13:34:47 -0800 Subject: [PATCH] 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 --- res/values/strings.xml | 1 + res/xml/graphics_driver_settings.xml | 2 - ...GraphicsDriverAppPreferenceController.java | 30 +------- ...rEnableForAllAppsPreferenceController.java | 77 +++++++++++++++++++ ...bleForAllAppsPreferenceControllerTest.java | 2 + 5 files changed, 82 insertions(+), 30 deletions(-) diff --git a/res/values/strings.xml b/res/values/strings.xml index 0c9fa053242..95ca27ceb2c 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -11002,6 +11002,7 @@ @string/graphics_driver_app_preference_default @string/graphics_driver_app_preference_game_driver + @string/graphics_driver_app_preference_prerelease_driver diff --git a/res/xml/graphics_driver_settings.xml b/res/xml/graphics_driver_settings.xml index c72c8fb30ae..a1fa78c608f 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"> diff --git a/src/com/android/settings/development/graphicsdriver/GraphicsDriverAppPreferenceController.java b/src/com/android/settings/development/graphicsdriver/GraphicsDriverAppPreferenceController.java index 2013b459f92..b1a31fbaec6 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.pm.PackageManager; 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 @@ public class GraphicsDriverAppPreferenceController extends BasePreferenceControl 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 @@ public class GraphicsDriverAppPreferenceController extends BasePreferenceControl 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 @@ public class GraphicsDriverAppPreferenceController extends BasePreferenceControl 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 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 4baa99300db..29f10149446 100644 --- a/src/com/android/settings/development/graphicsdriver/GraphicsDriverEnableForAllAppsPreferenceController.java +++ b/src/com/android/settings/development/graphicsdriver/GraphicsDriverEnableForAllAppsPreferenceController.java @@ -18,10 +18,15 @@ package com.android.settings.development.graphicsdriver; 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.OnStart; 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 class GraphicsDriverEnableForAllAppsPreferenceController extends BasePref 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 @@ public class GraphicsDriverEnableForAllAppsPreferenceController extends BasePref private final String mPreferenceGameDriver; private final String mPreferencePrereleaseDriver; @VisibleForTesting + CharSequence[] mEntryList; + @VisibleForTesting GraphicsDriverContentObserver mGraphicsDriverContentObserver; private ListPreference mPreference; @@ -69,6 +83,7 @@ public class GraphicsDriverEnableForAllAppsPreferenceController extends BasePref 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 class GraphicsDriverEnableForAllAppsPreferenceController extends BasePref 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 class GraphicsDriverEnableForAllAppsPreferenceController extends BasePref 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 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 366a18d2ca6..920e9d1506f 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 @@ public class GraphicsDriverEnableForAllAppsPreferenceControllerTest { 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); }