diff --git a/android/app/src/main/java/com/razinj/context_launcher/AppProvider.kt b/android/app/src/main/java/com/razinj/context_launcher/AppProvider.kt index 3d9a9a0..960b7b4 100644 --- a/android/app/src/main/java/com/razinj/context_launcher/AppProvider.kt +++ b/android/app/src/main/java/com/razinj/context_launcher/AppProvider.kt @@ -9,7 +9,6 @@ import android.content.IntentFilter import android.content.pm.LauncherApps import android.os.Build import android.os.IBinder -import android.os.Process import android.os.UserHandle import androidx.core.app.NotificationCompat @@ -17,19 +16,19 @@ class AppProvider : Service() { private var packageChangeReceiver: PackageChangeReceiver? = null override fun onCreate() { - val launcherApps = (getSystemService(LAUNCHER_APPS_SERVICE) as LauncherApps) + val launcherApps = getSystemService(LAUNCHER_APPS_SERVICE) as LauncherApps + launcherApps.registerCallback( object : LauncherAppsCallback() { override fun onPackageAdded( packageName: String, user: UserHandle, ) { - if (user == Process.myUserHandle()) return - PackageChangeReceiver.handleEvent( this@AppProvider, Intent.ACTION_PACKAGE_ADDED, packageName, + false, ) } @@ -37,12 +36,11 @@ class AppProvider : Service() { packageName: String, user: UserHandle, ) { - if (user == Process.myUserHandle()) return - PackageChangeReceiver.handleEvent( this@AppProvider, Intent.ACTION_PACKAGE_CHANGED, packageName, + true, ) } @@ -50,12 +48,11 @@ class AppProvider : Service() { packageName: String, user: UserHandle, ) { - if (user == Process.myUserHandle()) return - PackageChangeReceiver.handleEvent( this@AppProvider, Intent.ACTION_PACKAGE_REMOVED, packageName, + false, ) } }, diff --git a/android/app/src/main/java/com/razinj/context_launcher/PackageChangeReceiver.kt b/android/app/src/main/java/com/razinj/context_launcher/PackageChangeReceiver.kt index 05ae714..2febb18 100644 --- a/android/app/src/main/java/com/razinj/context_launcher/PackageChangeReceiver.kt +++ b/android/app/src/main/java/com/razinj/context_launcher/PackageChangeReceiver.kt @@ -23,6 +23,7 @@ class PackageChangeReceiver : BroadcastReceiver() { context, action, packageName, + intent.getBooleanExtra(Intent.EXTRA_REPLACING, false), ) } @@ -31,25 +32,22 @@ class PackageChangeReceiver : BroadcastReceiver() { context: Context, action: String, packageName: String, + replacing: Boolean, ) { - if (action != Intent.ACTION_PACKAGE_ADDED && - action != Intent.ACTION_PACKAGE_CHANGED && - action != Intent.ACTION_PACKAGE_REMOVED - ) { - return - } - val intent = Intent() intent.setAction(Constants.PACKAGE_UPDATE_ACTION) intent.putExtra(Constants.PACKAGE_CHANGE_NAME, packageName) - if (action == Intent.ACTION_PACKAGE_ADDED || action == Intent.ACTION_PACKAGE_CHANGED) { - // Ignore plugin apps - context.packageManager.getLaunchIntentForPackage(packageName) ?: return - - intent.putExtra(Constants.PACKAGE_CHANGE_IS_REMOVED, false) + if (Intent.ACTION_PACKAGE_ADDED == action) { + if (!replacing) { + intent.putExtra(Constants.PACKAGE_CHANGE_IS_REMOVED, false) + } + } else if (Intent.ACTION_PACKAGE_REMOVED == action) { + if (!replacing) { + intent.putExtra(Constants.PACKAGE_CHANGE_IS_REMOVED, true) + } } else { - intent.putExtra(Constants.PACKAGE_CHANGE_IS_REMOVED, true) + intent.putExtra(Constants.PACKAGE_CHANGE_IS_REMOVED, false) } context.sendBroadcast(intent)