Merge "Fix default home detection logic" into qt-dev
diff --git a/packages/SystemUI/src/com/android/systemui/assist/AssistHandleReminderExpBehavior.java b/packages/SystemUI/src/com/android/systemui/assist/AssistHandleReminderExpBehavior.java
index 87fb28b..909b68b 100644
--- a/packages/SystemUI/src/com/android/systemui/assist/AssistHandleReminderExpBehavior.java
+++ b/packages/SystemUI/src/com/android/systemui/assist/AssistHandleReminderExpBehavior.java
@@ -16,14 +16,13 @@
 
 package com.android.systemui.assist;
 
-import static com.android.systemui.shared.system.PackageManagerWrapper.ACTION_PREFERRED_ACTIVITY_CHANGED;
-
 import android.app.ActivityManager;
 import android.content.BroadcastReceiver;
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
+import android.content.pm.ResolveInfo;
 import android.os.Handler;
 import android.os.SystemClock;
 import android.provider.Settings;
@@ -44,6 +43,7 @@
 import java.io.PrintWriter;
 import java.time.LocalDate;
 import java.util.ArrayList;
+import java.util.List;
 import java.util.concurrent.TimeUnit;
 
 /**
@@ -68,6 +68,14 @@
     private static final boolean DEFAULT_SUPPRESS_ON_LAUNCHER = false;
     private static final boolean DEFAULT_SUPPRESS_ON_APPS = false;
 
+    private static final String[] DEFAULT_HOME_CHANGE_ACTIONS = new String[] {
+            PackageManagerWrapper.ACTION_PREFERRED_ACTIVITY_CHANGED,
+            Intent.ACTION_BOOT_COMPLETED,
+            Intent.ACTION_PACKAGE_ADDED,
+            Intent.ACTION_PACKAGE_CHANGED,
+            Intent.ACTION_PACKAGE_REMOVED
+    };
+
     private final StatusBarStateController.StateListener mStatusBarStateListener =
             new StatusBarStateController.StateListener() {
                 @Override
@@ -110,8 +118,7 @@
             mDefaultHome = getCurrentDefaultHome();
         }
     };
-    private final IntentFilter mDefaultHomeIntentFilter =
-            new IntentFilter(ACTION_PREFERRED_ACTIVITY_CHANGED);
+    private final IntentFilter mDefaultHomeIntentFilter;
     private final Runnable mResetConsecutiveTaskSwitches = this::resetConsecutiveTaskSwitches;
 
     private final Handler mHandler;
@@ -146,6 +153,10 @@
         mStatusBarStateController = Dependency.get(StatusBarStateController.class);
         mActivityManagerWrapper = ActivityManagerWrapper.getInstance();
         mOverviewProxyService = Dependency.get(OverviewProxyService.class);
+        mDefaultHomeIntentFilter = new IntentFilter();
+        for (String action : DEFAULT_HOME_CHANGE_ACTIONS) {
+            mDefaultHomeIntentFilter.addAction(action);
+        }
     }
 
     @Override
@@ -205,7 +216,24 @@
 
     @Nullable
     private static ComponentName getCurrentDefaultHome() {
-        return PackageManagerWrapper.getInstance().getHomeActivities(new ArrayList<>());
+        List<ResolveInfo> homeActivities = new ArrayList<>();
+        ComponentName defaultHome =
+                PackageManagerWrapper.getInstance().getHomeActivities(homeActivities);
+        if (defaultHome != null) {
+            return defaultHome;
+        }
+
+        int topPriority = Integer.MIN_VALUE;
+        ComponentName topComponent = null;
+        for (ResolveInfo resolveInfo : homeActivities) {
+            if (resolveInfo.priority > topPriority) {
+                topComponent = resolveInfo.activityInfo.getComponentName();
+                topPriority = resolveInfo.priority;
+            } else if (resolveInfo.priority == topPriority) {
+                topComponent = null;
+            }
+        }
+        return topComponent;
     }
 
     private void handleStatusBarStateChanged(int newState) {
diff --git a/packages/SystemUI/src/com/android/systemui/assist/PhoneStateMonitor.java b/packages/SystemUI/src/com/android/systemui/assist/PhoneStateMonitor.java
index 1fd3089..e73dc4a 100644
--- a/packages/SystemUI/src/com/android/systemui/assist/PhoneStateMonitor.java
+++ b/packages/SystemUI/src/com/android/systemui/assist/PhoneStateMonitor.java
@@ -16,8 +16,6 @@
 
 package com.android.systemui.assist;
 
-import static com.android.systemui.shared.system.PackageManagerWrapper.ACTION_PREFERRED_ACTIVITY_CHANGED;
-
 import android.app.ActivityManager;
 import android.app.KeyguardManager;
 import android.content.BroadcastReceiver;
@@ -25,6 +23,7 @@
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
+import android.content.pm.ResolveInfo;
 
 import androidx.annotation.Nullable;
 
@@ -38,6 +37,7 @@
 import com.android.systemui.statusbar.phone.StatusBar;
 
 import java.util.ArrayList;
+import java.util.List;
 
 /** Class to monitor and report the state of the phone. */
 final class PhoneStateMonitor {
@@ -53,6 +53,14 @@
     private static final int PHONE_STATE_APP_IMMERSIVE = 9;
     private static final int PHONE_STATE_APP_FULLSCREEN = 10;
 
+    private static final String[] DEFAULT_HOME_CHANGE_ACTIONS = new String[] {
+            PackageManagerWrapper.ACTION_PREFERRED_ACTIVITY_CHANGED,
+            Intent.ACTION_BOOT_COMPLETED,
+            Intent.ACTION_PACKAGE_ADDED,
+            Intent.ACTION_PACKAGE_CHANGED,
+            Intent.ACTION_PACKAGE_REMOVED
+    };
+
     private final Context mContext;
     private final StatusBarStateController mStatusBarStateController;
 
@@ -64,14 +72,17 @@
         mStatusBarStateController = Dependency.get(StatusBarStateController.class);
 
         ActivityManagerWrapper activityManagerWrapper = ActivityManagerWrapper.getInstance();
-        mDefaultHome = PackageManagerWrapper.getInstance().getHomeActivities(new ArrayList<>());
+        mDefaultHome = getCurrentDefaultHome();
+        IntentFilter intentFilter = new IntentFilter();
+        for (String action : DEFAULT_HOME_CHANGE_ACTIONS) {
+            intentFilter.addAction(action);
+        }
         mContext.registerReceiver(new BroadcastReceiver() {
             @Override
             public void onReceive(Context context, Intent intent) {
-                mDefaultHome =
-                        PackageManagerWrapper.getInstance().getHomeActivities(new ArrayList<>());
+                mDefaultHome = getCurrentDefaultHome();
             }
-        }, new IntentFilter(ACTION_PREFERRED_ACTIVITY_CHANGED));
+        }, intentFilter);
         mLauncherShowing = isLauncherShowing(activityManagerWrapper.getRunningTask());
         activityManagerWrapper.registerTaskStackListener(new TaskStackChangeListener() {
             @Override
@@ -93,6 +104,28 @@
         return phoneState;
     }
 
+    @Nullable
+    private static ComponentName getCurrentDefaultHome() {
+        List<ResolveInfo> homeActivities = new ArrayList<>();
+        ComponentName defaultHome =
+                PackageManagerWrapper.getInstance().getHomeActivities(homeActivities);
+        if (defaultHome != null) {
+            return defaultHome;
+        }
+
+        int topPriority = Integer.MIN_VALUE;
+        ComponentName topComponent = null;
+        for (ResolveInfo resolveInfo : homeActivities) {
+            if (resolveInfo.priority > topPriority) {
+                topComponent = resolveInfo.activityInfo.getComponentName();
+                topPriority = resolveInfo.priority;
+            } else if (resolveInfo.priority == topPriority) {
+                topComponent = null;
+            }
+        }
+        return topComponent;
+    }
+
     private int getPhoneLockscreenState() {
         if (isDozing()) {
             return PHONE_STATE_AOD1;