From 59c60df9935ed4f6266ca85fb7bbe046fcaa7eb6 Mon Sep 17 00:00:00 2001 From: lynxnb Date: Thu, 14 Jul 2022 02:06:51 +0200 Subject: [PATCH] Add `GPU Driver Configuration` preference This preference launches `GpuDriverActivity` for managing custom gpu drivers. When the device has an incompatible GPU, the preference will be disabled and greyed out. --- .../skyline/preference/GpuDriverPreference.kt | 52 +++++++++++++++++++ app/src/main/res/values/strings.xml | 2 + app/src/main/res/xml/preferences.xml | 3 ++ 3 files changed, 57 insertions(+) create mode 100644 app/src/main/java/emu/skyline/preference/GpuDriverPreference.kt diff --git a/app/src/main/java/emu/skyline/preference/GpuDriverPreference.kt b/app/src/main/java/emu/skyline/preference/GpuDriverPreference.kt new file mode 100644 index 00000000..8624e196 --- /dev/null +++ b/app/src/main/java/emu/skyline/preference/GpuDriverPreference.kt @@ -0,0 +1,52 @@ +/* + * SPDX-License-Identifier: MPL-2.0 + * Copyright © 2020 Skyline Team and Contributors (https://github.com/skyline-emu/) + */ + +package emu.skyline.preference + +import android.content.Context +import android.content.Intent +import android.util.AttributeSet +import androidx.activity.ComponentActivity +import androidx.activity.result.contract.ActivityResultContracts +import androidx.preference.Preference +import androidx.preference.Preference.SummaryProvider +import androidx.preference.R +import emu.skyline.utils.GpuDriverHelper +import emu.skyline.utils.PreferenceSettings +import emu.skyline.R as SkylineR + +/** + * This preference is used to launch [GpuDriverActivity] using a preference + */ +class GpuDriverPreference @JvmOverloads constructor(context : Context, attrs : AttributeSet? = null, defStyleAttr : Int = R.attr.preferenceStyle) : Preference(context, attrs, defStyleAttr) { + private val driverCallback = (context as ComponentActivity).registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { + notifyChanged() + } + + init { + val supportsCustomDriverLoading = GpuDriverHelper.supportsCustomDriverLoading() + if (supportsCustomDriverLoading) { + summaryProvider = SummaryProvider { + sharedPreferences?.getString(key, PreferenceSettings.SYSTEM_GPU_DRIVER)?.let { + var driver = it + if (it == PreferenceSettings.SYSTEM_GPU_DRIVER) + driver = context.getString(SkylineR.string.system_driver) + + context.getString(SkylineR.string.gpu_driver_config_desc, driver) + } + } + } else { + isEnabled = false + summaryProvider = SummaryProvider { + context.getString(SkylineR.string.gpu_driver_config_desc_unsupported) + } + } + } + + /** + * This launches [GpuDriverActivity] on click to manage driver packages + */ + override fun onClick() = driverCallback.launch(Intent(context, GpuDriverActivity::class.java)) +} diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 68049d8b..a5aac3be 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -37,6 +37,8 @@ Performance Statistics will be shown in the top-left corner Log Level GPU Driver Configuration + Active driver: %1$s + Your GPU is not supported System Use Docked Mode diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index 5b07a1c4..db9314fe 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -40,6 +40,9 @@ app:key="log_level" app:title="@string/log_level" app:useSimpleSummaryProvider="true" /> +