From 31f51a39743e94afc69e36064f379957b487e72c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Wed, 13 Mar 2024 23:01:58 +0800 Subject: [PATCH] Fix theme --- app/src/main/AndroidManifest.xml | 4 +-- .../io/nekohasekai/sfa/ui/MainActivity.kt | 18 ++--------- .../sfa/ui/main/DashboardFragment.kt | 19 ++++++++---- .../sfa/ui/shared/AbstractActivity.kt | 12 +++++-- .../io/nekohasekai/sfa/utils/MIUIUtils.kt | 31 +++++++++++++++++++ app/src/main/res/values-night/themes.xml | 11 +++++++ app/src/main/res/values/themes.xml | 10 +++++- 7 files changed, 78 insertions(+), 27 deletions(-) create mode 100644 app/src/main/java/io/nekohasekai/sfa/utils/MIUIUtils.kt create mode 100644 app/src/main/res/values-night/themes.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 4ebba4e..aeda923 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -26,7 +26,7 @@ android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" - android:theme="@style/Theme.Material3.DayNight" + android:theme="@style/AppTheme" tools:targetApi="31"> - tab.setText(Page.values()[position].titleRes) - }.apply { attach() } activity.serviceStatus.observe(viewLifecycleOwner) { when (it) { Status.Stopped -> { @@ -83,6 +77,19 @@ class DashboardFragment : Fragment(R.layout.fragment_dashboard) { } } + override fun onStart() { + super.onStart() + val activity = activity ?: return + val binding = binding ?: return + if (mediator != null) return + mediator = TabLayoutMediator( + activity.binding.dashboardTabLayout, + binding.dashboardPager + ) { tab, position -> + tab.setText(Page.values()[position].titleRes) + }.apply { attach() } + } + override fun onDestroyView() { super.onDestroyView() mediator?.detach() diff --git a/app/src/main/java/io/nekohasekai/sfa/ui/shared/AbstractActivity.kt b/app/src/main/java/io/nekohasekai/sfa/ui/shared/AbstractActivity.kt index 9d36131..82231cc 100644 --- a/app/src/main/java/io/nekohasekai/sfa/ui/shared/AbstractActivity.kt +++ b/app/src/main/java/io/nekohasekai/sfa/ui/shared/AbstractActivity.kt @@ -2,12 +2,13 @@ package io.nekohasekai.sfa.ui.shared import android.os.Bundle import android.view.MenuItem +import android.view.WindowManager import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.content.res.AppCompatResources import com.google.android.material.color.DynamicColors import io.nekohasekai.sfa.R import io.nekohasekai.sfa.ktx.getAttrColor - +import io.nekohasekai.sfa.utils.MIUIUtils abstract class AbstractActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { @@ -20,9 +21,14 @@ abstract class AbstractActivity : AppCompatActivity() { window.statusBarColor = colorSurfaceContainer window.navigationBarColor = colorSurfaceContainer + // MIUI overrides colorSurfaceContainer to colorSurface without below flags + @Suppress("DEPRECATION") if (MIUIUtils.isMIUI) { + window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS) + window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION) + } + supportActionBar?.setHomeAsUpIndicator(AppCompatResources.getDrawable( - this@AbstractActivity, - R.drawable.ic_arrow_back_24 + this@AbstractActivity, R.drawable.ic_arrow_back_24 )!!.apply { setTint(getAttrColor(com.google.android.material.R.attr.colorOnSurface)) }) diff --git a/app/src/main/java/io/nekohasekai/sfa/utils/MIUIUtils.kt b/app/src/main/java/io/nekohasekai/sfa/utils/MIUIUtils.kt new file mode 100644 index 0000000..37eae60 --- /dev/null +++ b/app/src/main/java/io/nekohasekai/sfa/utils/MIUIUtils.kt @@ -0,0 +1,31 @@ +package io.nekohasekai.sfa.utils + +import android.annotation.SuppressLint +import android.content.Context +import android.content.Intent +import android.os.Process + +object MIUIUtils { + + val isMIUI by lazy { + !getSystemProperty("ro.miui.ui.version.name").isNullOrBlank() + } + + @SuppressLint("PrivateApi") + fun getSystemProperty(key: String?): String? { + try { + return Class.forName("android.os.SystemProperties").getMethod("get", String::class.java) + .invoke(null, key) as String + } catch (ignored: Exception) { + } + return null + } + + fun openPermissionSettings(context: Context) { + val intent = Intent("miui.intent.action.APP_PERM_EDITOR") + intent.putExtra("extra_package_uid", Process.myUid()) + intent.putExtra("extra_pkgname", context.packageName) + context.startActivity(intent) + } + +} \ No newline at end of file diff --git a/app/src/main/res/values-night/themes.xml b/app/src/main/res/values-night/themes.xml new file mode 100644 index 0000000..586b9ee --- /dev/null +++ b/app/src/main/res/values-night/themes.xml @@ -0,0 +1,11 @@ + + + + + + + diff --git a/app/src/main/res/values/themes.xml b/app/src/main/res/values/themes.xml index fe6a154..3560796 100644 --- a/app/src/main/res/values/themes.xml +++ b/app/src/main/res/values/themes.xml @@ -1,4 +1,12 @@ - + + + + +