Merge "Introduce isForegroundDefaultLauncher in ShortcutServiceInternal"
diff --git a/core/java/android/content/pm/ShortcutServiceInternal.java b/core/java/android/content/pm/ShortcutServiceInternal.java
index dadfaa9..e6f682d 100644
--- a/core/java/android/content/pm/ShortcutServiceInternal.java
+++ b/core/java/android/content/pm/ShortcutServiceInternal.java
@@ -81,4 +81,7 @@
             @Nullable IntentSender resultIntent, int userId);
 
     public abstract boolean isRequestPinItemSupported(int callingUserId, int requestType);
+
+    public abstract boolean isForegroundDefaultLauncher(@NonNull String callingPackage,
+            int callingUid);
 }
diff --git a/services/core/java/com/android/server/pm/ShortcutService.java b/services/core/java/com/android/server/pm/ShortcutService.java
index 0907dd7..ee2f374 100644
--- a/services/core/java/com/android/server/pm/ShortcutService.java
+++ b/services/core/java/com/android/server/pm/ShortcutService.java
@@ -2739,6 +2739,26 @@
         public boolean isRequestPinItemSupported(int callingUserId, int requestType) {
             return ShortcutService.this.isRequestPinItemSupported(callingUserId, requestType);
         }
+
+        @Override
+        public boolean isForegroundDefaultLauncher(@NonNull String callingPackage, int callingUid) {
+            Preconditions.checkNotNull(callingPackage);
+
+            final int userId = UserHandle.getUserId(callingUid);
+            final ComponentName defaultLauncher = getDefaultLauncher(userId);
+            if (defaultLauncher == null) {
+                return false;
+            }
+            if (!callingPackage.equals(defaultLauncher.getPackageName())) {
+                return false;
+            }
+            synchronized (mLock) {
+                if (!isUidForegroundLocked(callingUid)) {
+                    return false;
+                }
+            }
+            return true;
+        }
     }
 
     final BroadcastReceiver mReceiver = new BroadcastReceiver() {
diff --git a/services/tests/servicestests/src/com/android/server/pm/BaseShortcutManagerTest.java b/services/tests/servicestests/src/com/android/server/pm/BaseShortcutManagerTest.java
index 926a911..8cf575e 100644
--- a/services/tests/servicestests/src/com/android/server/pm/BaseShortcutManagerTest.java
+++ b/services/tests/servicestests/src/com/android/server/pm/BaseShortcutManagerTest.java
@@ -2121,22 +2121,30 @@
                 PACKAGE_FALLBACK_LAUNCHER_PRIORITY);
     }
 
-    protected void makeCallerForeground() {
+    protected void makeUidForeground(int uid) {
         try {
             mService.mUidObserver.onUidStateChanged(
-                    mInjectedCallingUid, ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE, 0);
+                    uid, ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE, 0);
+        } catch (RemoteException e) {
+            e.rethrowAsRuntimeException();
+        }
+    }
+
+    protected void makeCallerForeground() {
+        makeUidForeground(mInjectedCallingUid);
+    }
+
+    protected void makeUidBackground(int uid) {
+        try {
+            mService.mUidObserver.onUidStateChanged(
+                    uid, ActivityManager.PROCESS_STATE_TOP_SLEEPING, 0);
         } catch (RemoteException e) {
             e.rethrowAsRuntimeException();
         }
     }
 
     protected void makeCallerBackground() {
-        try {
-            mService.mUidObserver.onUidStateChanged(
-                    mInjectedCallingUid, ActivityManager.PROCESS_STATE_TOP_SLEEPING, 0);
-        } catch (RemoteException e) {
-            e.rethrowAsRuntimeException();
-        }
+        makeUidBackground(mInjectedCallingUid);
     }
 
     protected void publishManifestShortcutsAsCaller(int resId) {
diff --git a/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest1.java b/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest1.java
index f92b575..ee33e79 100644
--- a/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest1.java
+++ b/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest1.java
@@ -7912,4 +7912,35 @@
                     .forAllShortcuts(si -> assertTrue(si.isReturnedByServer()));
         });
     }
+
+    public void testIsForegroundDefaultLauncher_true() {
+        final ComponentName defaultLauncher = new ComponentName("default", "launcher");
+        final int uid = 1024;
+
+        setDefaultLauncher(UserHandle.USER_SYSTEM, defaultLauncher);
+        makeUidForeground(uid);
+
+        assertTrue(mInternal.isForegroundDefaultLauncher("default", uid));
+    }
+
+
+    public void testIsForegroundDefaultLauncher_defaultButNotForeground() {
+        final ComponentName defaultLauncher = new ComponentName("default", "launcher");
+        final int uid = 1024;
+
+        setDefaultLauncher(UserHandle.USER_SYSTEM, defaultLauncher);
+        makeUidBackground(uid);
+
+        assertFalse(mInternal.isForegroundDefaultLauncher("default", uid));
+    }
+
+    public void testIsForegroundDefaultLauncher_foregroundButNotDefault() {
+        final ComponentName defaultLauncher = new ComponentName("default", "launcher");
+        final int uid = 1024;
+
+        setDefaultLauncher(UserHandle.USER_SYSTEM, defaultLauncher);
+        makeUidForeground(uid);
+
+        assertFalse(mInternal.isForegroundDefaultLauncher("another", uid));
+    }
 }