Skip to content

Commit

Permalink
bug catch
Browse files Browse the repository at this point in the history
  • Loading branch information
Young-Lord committed Dec 26, 2023
1 parent f79f4d7 commit 509dc64
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 11 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,4 @@ dependencies {

debugImplementation 'androidx.compose.ui:ui-tooling'
debugImplementation 'androidx.compose.ui:ui-test-manifest'
}
}
22 changes: 22 additions & 0 deletions app/src/main/java/moe/lyniko/hiderecent/ui/HomeView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
}
}
}
}

Expand Down
37 changes: 33 additions & 4 deletions app/src/main/java/moe/lyniko/hiderecent/utils/AppUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<PackageInfo> 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<PackageInfo> by lazy {
// get all the apps
if (
PreferenceUtils.getInstance(context).managerPref.getBoolean(
Expand Down Expand Up @@ -63,7 +89,10 @@ class AppUtils(
}

@SuppressLint("PrivateApi")
private fun getInstalledPackagesAsUser(@Suppress("SameParameterValue") flags: Int, userId: Int): List<PackageInfo> {
private fun getInstalledPackagesAsUser(
@Suppress("SameParameterValue") flags: Int,
userId: Int
): List<PackageInfo> {
// fuck android.
// https://www.xda-developers.com/implementing-shizuku/
// Previous version: https://github.com/Young-Lord/hideRecent/commit/8f956002e1edbb95e2e3e945c28ec1a716596347
Expand Down Expand Up @@ -105,7 +134,7 @@ class AppUtils(
// get all the apps
val result = ArrayList<PackageInfo>()
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)
}
Expand Down
11 changes: 7 additions & 4 deletions app/src/main/java/moe/lyniko/hiderecent/utils/PreferenceUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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")
Expand Down
7 changes: 5 additions & 2 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
<string name="clear_search">清除搜索框</string>
<string name="main_user_only">此模块仅能在主用户(用户0)中使用。当前用户为$1%d。</string>
<string name="shizuku_not_available_toast">Shizuku / Sui 未运行,仅显示当前用户的应用。</string>
<string name="activity_not_fetched">基于 Activity 的应用列表过滤器未激活,因此显示了不出现在最近应用列表中的应用。这可能是你的系统的 bug。</string>
<string name="apps_not_fetched">获取应用列表失败。你仍然可以通过在设置页面导入或导出包名来遍及设置。</string>
<string name="show_package_for_all_user">显示来自所有用户的应用</string>
<string name="show_package_for_all_user_summary">需要 Shizuku 或 Sui。重启应用生效。</string>
<string name="hide_icon_in_launcher">从启动器(桌面)隐藏此模块</string>
Expand All @@ -25,8 +27,9 @@
<string name="import_config_failed_perm">导入配置失败。请检查你是否给予了对应权限。</string>
<string name="import_config_failed_wrong">导入配置失败。配置内容格式有误。</string>
<string name="import_config_success">导入配置成功。</string>
<string name="export_uncomment_hint">导入配置前,删掉此行的首个#以重置已有配置</string>
<string name="export_uncomment_hint">导入配置前,删掉此行的首个`# `以重置已有配置</string>
<string name="export_demo_hint">本行为配置格式的示例,删除`# `表示为`com.example.package`启用隐藏</string>
<plurals name="import_config_success_count">
<item quantity="other">成功更改 %1$d 条配置。</item>
</plurals>
</resources>
</resources>
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
<string name="clear_search">Clear search box</string>
<string name="shizuku_not_available_toast">Shizuku / Sui not working, only current user\'s packages are displayed.</string>
<string name="main_user_only">This module should be used in main user (user 0) only. Current user is %1$d.</string>
<string name="activity_not_fetched">Activity filter is not running properly, so non-applicable apps are shown. This may be a bug of your ROM.</string>
<string name="apps_not_fetched">Fetching application list failed. You can still edit settings by importing and exporting package names.</string>
<string name="show_package_for_all_user">Show applications of all users.</string>
<string name="show_package_for_all_user_summary">Require Shizuku / Sui to work. Restart module after changing this.</string>
<string name="hide_icon_in_launcher">Hide module in launcher.</string>
Expand All @@ -29,6 +31,7 @@
<string name="import_config_failed_wrong">Failed. Wrong config format.</string>
<string name="import_config_success">Config imported.</string>
<string name="export_uncomment_hint">uncomment this line to reset before importing</string>
<string name="export_demo_hint">this line is a demo for config format, remove `# ` means enabling for `com.example.package`</string>
<plurals name="import_config_success_count">
<item quantity="one">%1$d config entry changed.</item>
<item quantity="other">%1$d config entries changed.</item>
Expand Down

0 comments on commit 509dc64

Please sign in to comment.