From 509dc64003a721e75a85dde2767631ac47a31b24 Mon Sep 17 00:00:00 2001 From: LY <51789698+Young-Lord@users.noreply.github.com> Date: Wed, 27 Dec 2023 07:01:29 +0800 Subject: [PATCH] bug catch --- app/build.gradle | 2 +- .../java/moe/lyniko/hiderecent/ui/HomeView.kt | 22 +++++++++++ .../moe/lyniko/hiderecent/utils/AppUtils.kt | 37 +++++++++++++++++-- .../hiderecent/utils/PreferenceUtils.kt | 11 ++++-- app/src/main/res/values-zh-rCN/strings.xml | 7 +++- app/src/main/res/values/strings.xml | 3 ++ 6 files changed, 71 insertions(+), 11 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index c12f665..eb4d288 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -72,4 +72,4 @@ dependencies { debugImplementation 'androidx.compose.ui:ui-tooling' debugImplementation 'androidx.compose.ui:ui-test-manifest' -} \ No newline at end of file +} diff --git a/app/src/main/java/moe/lyniko/hiderecent/ui/HomeView.kt b/app/src/main/java/moe/lyniko/hiderecent/ui/HomeView.kt index 014fba6..dd4d236 100644 --- a/app/src/main/java/moe/lyniko/hiderecent/ui/HomeView.kt +++ b/app/src/main/java/moe/lyniko/hiderecent/ui/HomeView.kt @@ -165,6 +165,28 @@ fun HomeView() { "Wrong user id: ${getIdByUserHandle(userHandle)}" ) } + if (! appUtils.isActivitiesFetched()) { + LaunchedEffect(snackbarHostState) { + scope.launch { + snackbarHostState.showSnackbar( + context.getString(R.string.activity_not_fetched), + actionLabel = context.getString(R.string.dismiss_notification), + duration = SnackbarDuration.Indefinite + ) + } + } + } + if (appUtils.parsedApps.isEmpty()) { + LaunchedEffect(snackbarHostState) { + scope.launch { + snackbarHostState.showSnackbar( + context.getString(R.string.apps_not_fetched), + actionLabel = context.getString(R.string.dismiss_notification), + duration = SnackbarDuration.Indefinite + ) + } + } + } } } diff --git a/app/src/main/java/moe/lyniko/hiderecent/utils/AppUtils.kt b/app/src/main/java/moe/lyniko/hiderecent/utils/AppUtils.kt index 8d30b18..148c168 100644 --- a/app/src/main/java/moe/lyniko/hiderecent/utils/AppUtils.kt +++ b/app/src/main/java/moe/lyniko/hiderecent/utils/AppUtils.kt @@ -25,12 +25,38 @@ class AppUtils( instance ?: AppUtils(context).also { instance = it } } } + // the package manager private val packageManager: PackageManager = context.packageManager - private val getInstalledPackagesFlags: Int = PackageManager.GET_ACTIVITIES or PackageManager.GET_META_DATA + private val baseGetInstalledPackagesFlags: Int = + PackageManager.GET_ACTIVITIES or PackageManager.GET_META_DATA + private var getInstalledPackagesFlags: Int = baseGetInstalledPackagesFlags - // a list for all the apps, lazy init private val allApps: List by lazy { + // catch random error caused by MIUI or something else + try { + allAppsNoCatch + } catch (e: Exception) { + e.printStackTrace() + // remove GET_ACTIVITIES flag + getInstalledPackagesFlags = + baseGetInstalledPackagesFlags or (baseGetInstalledPackagesFlags xor PackageManager.GET_ACTIVITIES) + try{ + allAppsNoCatch + } + catch(e: Exception) { + e.printStackTrace() + listOf() + } + } + } + + fun isActivitiesFetched(): Boolean { + return (PackageManager.GET_ACTIVITIES and getInstalledPackagesFlags) != 0 + } + + // a list for all the apps, lazy init + private val allAppsNoCatch: List by lazy { // get all the apps if ( PreferenceUtils.getInstance(context).managerPref.getBoolean( @@ -63,7 +89,10 @@ class AppUtils( } @SuppressLint("PrivateApi") - private fun getInstalledPackagesAsUser(@Suppress("SameParameterValue") flags: Int, userId: Int): List { + private fun getInstalledPackagesAsUser( + @Suppress("SameParameterValue") flags: Int, + userId: Int + ): List { // fuck android. // https://www.xda-developers.com/implementing-shizuku/ // Previous version: https://github.com/Young-Lord/hideRecent/commit/8f956002e1edbb95e2e3e945c28ec1a716596347 @@ -105,7 +134,7 @@ class AppUtils( // get all the apps val result = ArrayList() allApps.forEach { - if (result.find { pkg -> pkg.packageName == it.packageName } == null && ! it.activities.isNullOrEmpty()) { + if (result.find { pkg -> pkg.packageName == it.packageName } == null && (isActivitiesFetched() && !it.activities.isNullOrEmpty())) { // filter for multi-user and filter those without activities result.add(it) } diff --git a/app/src/main/java/moe/lyniko/hiderecent/utils/PreferenceUtils.kt b/app/src/main/java/moe/lyniko/hiderecent/utils/PreferenceUtils.kt index aa8c8d7..68952dc 100644 --- a/app/src/main/java/moe/lyniko/hiderecent/utils/PreferenceUtils.kt +++ b/app/src/main/java/moe/lyniko/hiderecent/utils/PreferenceUtils.kt @@ -6,11 +6,11 @@ import android.content.SharedPreferences import moe.lyniko.hiderecent.MyApplication import moe.lyniko.hiderecent.R +@SuppressLint("WorldReadableFiles") class PreferenceUtils( // init context on constructor context: Context ) { // ------ 1. get several SharedPreferences (funcPref is the only accessible during Xposed inject) ------ - @SuppressLint("WorldReadableFiles") private var funcPref: SharedPreferences = try { @Suppress("DEPRECATION") context.getSharedPreferences(functionalConfigName, Context.MODE_WORLD_READABLE) @@ -20,10 +20,10 @@ class PreferenceUtils( // init context on constructor // context.getSharedPreferences(functionalConfigName, Context.MODE_PRIVATE) } + @Suppress("DEPRECATION") var managerPref: SharedPreferences = - context.getSharedPreferences(managerConfigName, Context.MODE_PRIVATE) + context.getSharedPreferences(managerConfigName, Context.MODE_WORLD_READABLE) - @SuppressLint("WorldReadableFiles") @Suppress("DEPRECATION") private val legacyFuncPref = context.getSharedPreferences(legacyConfigName, Context.MODE_WORLD_READABLE) @@ -80,7 +80,7 @@ class PreferenceUtils( // init context on constructor } fun addPackage(pkg: String): Int { - if(pkg.isEmpty()) return 0 + if(pkg.isEmpty() || pkg == "*") return 0 val ret = if (packages.add(pkg)) 1 else 0 // Log.w("PreferenceUtil", "addPackage: $pkg -> $ret") commitPackageList() @@ -113,6 +113,9 @@ class PreferenceUtils( // init context on constructor packages.forEach { pkg -> result += "+$pkg\n" } + if (packages.isEmpty()){ + result += "# +com.example.package # ${MyApplication.resourcesPublic.getString(R.string.export_demo_hint)}\n" + } return result } else -> throw NotImplementedError("Version $version is not implemented") diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index 17b0120..8ebd553 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -13,6 +13,8 @@ 清除搜索框 此模块仅能在主用户(用户0)中使用。当前用户为$1%d。 Shizuku / Sui 未运行,仅显示当前用户的应用。 + 基于 Activity 的应用列表过滤器未激活,因此显示了不出现在最近应用列表中的应用。这可能是你的系统的 bug。 + 获取应用列表失败。你仍然可以通过在设置页面导入或导出包名来遍及设置。 显示来自所有用户的应用 需要 Shizuku 或 Sui。重启应用生效。 从启动器(桌面)隐藏此模块 @@ -25,8 +27,9 @@ 导入配置失败。请检查你是否给予了对应权限。 导入配置失败。配置内容格式有误。 导入配置成功。 - 导入配置前,删掉此行的首个#以重置已有配置 + 导入配置前,删掉此行的首个`# `以重置已有配置 + 本行为配置格式的示例,删除`# `表示为`com.example.package`启用隐藏 成功更改 %1$d 条配置。 - \ No newline at end of file + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8895ae2..af13114 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -16,6 +16,8 @@ Clear search box Shizuku / Sui not working, only current user\'s packages are displayed. This module should be used in main user (user 0) only. Current user is %1$d. + Activity filter is not running properly, so non-applicable apps are shown. This may be a bug of your ROM. + Fetching application list failed. You can still edit settings by importing and exporting package names. Show applications of all users. Require Shizuku / Sui to work. Restart module after changing this. Hide module in launcher. @@ -29,6 +31,7 @@ Failed. Wrong config format. Config imported. uncomment this line to reset before importing + this line is a demo for config format, remove `# ` means enabling for `com.example.package` %1$d config entry changed. %1$d config entries changed.