Merge "Only show graphics driver option when the apk is available." into rvc-dev

This commit is contained in:
TreeHugger Robot
2020-03-01 04:42:02 +00:00
committed by Android (Google) Code Review
5 changed files with 82 additions and 30 deletions

View File

@@ -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">

View File

@@ -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>

View File

@@ -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<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) {

View File

@@ -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<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);
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);
}