From 33c36fc1de214523e8b51ff947831c5d899480d4 Mon Sep 17 00:00:00 2001 From: Thibault Deckers Date: Sun, 17 Apr 2022 12:59:22 +0900 Subject: [PATCH] handle PM dying when fetching all packages --- .../aves/channel/calls/AppAdapterHandler.kt | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/android/app/src/main/kotlin/deckers/thibault/aves/channel/calls/AppAdapterHandler.kt b/android/app/src/main/kotlin/deckers/thibault/aves/channel/calls/AppAdapterHandler.kt index 048a5a38f..195e9d4ab 100644 --- a/android/app/src/main/kotlin/deckers/thibault/aves/channel/calls/AppAdapterHandler.kt +++ b/android/app/src/main/kotlin/deckers/thibault/aves/channel/calls/AppAdapterHandler.kt @@ -110,8 +110,29 @@ class AppAdapterHandler(private val context: Context) : MethodCallHandler { } } - addPackageDetails(Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER)) - addPackageDetails(Intent(Intent.ACTION_MAIN)) + // identify launcher category packages, which typically include user apps + // they should be fetched before the other packages, to be marked as launcher packages + try { + addPackageDetails(Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER)) + } catch (e: Exception) { + Log.w(LOG_TAG, "failed to list launcher packages", e) + } + + try { + // complete with all the other packages + addPackageDetails(Intent(Intent.ACTION_MAIN)) + } catch (e: Exception) { + // `PackageManager.queryIntentActivities()` may kill the package manager if the response is too large + Log.w(LOG_TAG, "failed to list all packages", e) + + // fallback to the default category packages, which typically include system and OEM tools + try { + addPackageDetails(Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_DEFAULT)) + } catch (e: Exception) { + Log.w(LOG_TAG, "failed to list default packages", e) + } + } + result.success(ArrayList(packages.values)) }