Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit fc4155a0 authored by Peiyong Lin's avatar Peiyong Lin
Browse files

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
parent dc4f004c
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -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">
+0 −2
Original line number Diff line number Diff line
@@ -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>

+2 −28
Original line number Diff line number Diff line
@@ -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) {
+77 −0
Original line number Diff line number Diff line
@@ -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;
    }
}
+2 −0
Original line number Diff line number Diff line
@@ -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);
    }