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
This commit is contained in:
Peiyong Lin
2020-02-21 13:34:47 -08:00
parent dc4f004cc9
commit fc4155a02c
5 changed files with 82 additions and 30 deletions

View File

@@ -11002,6 +11002,7 @@
<string-array name="graphics_driver_all_apps_preference_values"> <string-array name="graphics_driver_all_apps_preference_values">
<item>@string/graphics_driver_app_preference_default</item> <item>@string/graphics_driver_app_preference_default</item>
<item>@string/graphics_driver_app_preference_game_driver</item> <item>@string/graphics_driver_app_preference_game_driver</item>
<item>@string/graphics_driver_app_preference_prerelease_driver</item>
</string-array> </string-array>
<!-- All the values of graphics driver for app preference [CHAR LIMIT=50] --> <!-- All the values of graphics driver for app preference [CHAR LIMIT=50] -->
<string-array name="graphics_driver_app_preference_values"> <string-array name="graphics_driver_app_preference_values">

View File

@@ -24,8 +24,6 @@
android:key="graphics_driver_all_apps_preference" android:key="graphics_driver_all_apps_preference"
android:title="@string/graphics_driver_all_apps_preference_title" android:title="@string/graphics_driver_all_apps_preference_title"
android:dialogTitle="@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"> settings:controller="com.android.settings.development.graphicsdriver.GraphicsDriverEnableForAllAppsPreferenceController">
</ListPreference> </ListPreference>

View File

@@ -26,9 +26,7 @@ import android.content.pm.PackageManager;
import android.content.res.Resources; import android.content.res.Resources;
import android.os.Handler; import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import android.os.SystemProperties;
import android.provider.Settings; import android.provider.Settings;
import android.text.TextUtils;
import androidx.annotation.VisibleForTesting; import androidx.annotation.VisibleForTesting;
import androidx.preference.ListPreference; import androidx.preference.ListPreference;
@@ -60,9 +58,6 @@ public class GraphicsDriverAppPreferenceController extends BasePreferenceControl
GraphicsDriverContentObserver.OnGraphicsDriverContentChangedListener, LifecycleObserver, GraphicsDriverContentObserver.OnGraphicsDriverContentChangedListener, LifecycleObserver,
OnStart, OnStop { 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 Context mContext;
private final ContentResolver mContentResolver; private final ContentResolver mContentResolver;
private final String mPreferenceTitle; private final String mPreferenceTitle;
@@ -98,7 +93,8 @@ public class GraphicsDriverAppPreferenceController extends BasePreferenceControl
mPreferencePrereleaseDriver = mPreferencePrereleaseDriver =
resources.getString(R.string.graphics_driver_app_preference_prerelease_driver); resources.getString(R.string.graphics_driver_app_preference_prerelease_driver);
mPreferenceSystem = resources.getString(R.string.graphics_driver_app_preference_system); 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. // TODO: Move this task to background if there's potential ANR/Jank.
// Update the UI when all the app infos are ready. // Update the UI when all the app infos are ready.
@@ -195,28 +191,6 @@ public class GraphicsDriverAppPreferenceController extends BasePreferenceControl
updateState(mPreferenceGroup); 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 // AppInfo class to achieve loading the application label only once
class AppInfo { class AppInfo {
AppInfo(PackageManager packageManager, ApplicationInfo applicationInfo) { AppInfo(PackageManager packageManager, ApplicationInfo applicationInfo) {

View File

@@ -18,10 +18,15 @@ package com.android.settings.development.graphicsdriver;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources; import android.content.res.Resources;
import android.os.Build;
import android.os.Handler; import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import android.os.SystemProperties;
import android.provider.Settings; import android.provider.Settings;
import android.text.TextUtils;
import androidx.annotation.VisibleForTesting; import androidx.annotation.VisibleForTesting;
import androidx.preference.ListPreference; 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.core.lifecycle.events.OnStop;
import com.android.settingslib.development.DevelopmentSettingsEnabler; 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. * 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_ALL_APPS = 1;
public static final int GAME_DRIVER_PRERELEASE_ALL_APPS = 2; public static final int GAME_DRIVER_PRERELEASE_ALL_APPS = 2;
public static final int GAME_DRIVER_OFF = 3; 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 Context mContext;
private final ContentResolver mContentResolver; private final ContentResolver mContentResolver;
@@ -54,6 +66,8 @@ public class GraphicsDriverEnableForAllAppsPreferenceController extends BasePref
private final String mPreferenceGameDriver; private final String mPreferenceGameDriver;
private final String mPreferencePrereleaseDriver; private final String mPreferencePrereleaseDriver;
@VisibleForTesting @VisibleForTesting
CharSequence[] mEntryList;
@VisibleForTesting
GraphicsDriverContentObserver mGraphicsDriverContentObserver; GraphicsDriverContentObserver mGraphicsDriverContentObserver;
private ListPreference mPreference; private ListPreference mPreference;
@@ -69,6 +83,7 @@ public class GraphicsDriverEnableForAllAppsPreferenceController extends BasePref
resources.getString(R.string.graphics_driver_app_preference_game_driver); resources.getString(R.string.graphics_driver_app_preference_game_driver);
mPreferencePrereleaseDriver = mPreferencePrereleaseDriver =
resources.getString(R.string.graphics_driver_app_preference_prerelease_driver); resources.getString(R.string.graphics_driver_app_preference_prerelease_driver);
mEntryList = constructEntryList(mContext, false);
mGraphicsDriverContentObserver = mGraphicsDriverContentObserver =
new GraphicsDriverContentObserver(new Handler(Looper.getMainLooper()), this); new GraphicsDriverContentObserver(new Handler(Looper.getMainLooper()), this);
} }
@@ -87,6 +102,8 @@ public class GraphicsDriverEnableForAllAppsPreferenceController extends BasePref
public void displayPreference(PreferenceScreen screen) { public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen); super.displayPreference(screen);
mPreference = screen.findPreference(getPreferenceKey()); mPreference = screen.findPreference(getPreferenceKey());
mPreference.setEntries(mEntryList);
mPreference.setEntryValues(mEntryList);
mPreference.setOnPreferenceChangeListener(this); mPreference.setOnPreferenceChangeListener(this);
} }
@@ -147,4 +164,64 @@ public class GraphicsDriverEnableForAllAppsPreferenceController extends BasePref
public void onGraphicsDriverContentChanged() { public void onGraphicsDriverContentChanged() {
updateState(mPreference); 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;
}
} }

View File

@@ -82,6 +82,8 @@ public class GraphicsDriverEnableForAllAppsPreferenceControllerTest {
mResolver, Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_DEFAULT); mResolver, Settings.Global.GAME_DRIVER_ALL_APPS, GAME_DRIVER_DEFAULT);
mController = new GraphicsDriverEnableForAllAppsPreferenceController(mContext, "testKey"); 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); when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
mController.displayPreference(mScreen); mController.displayPreference(mScreen);
} }