Fix when instant app notif is showing

Test: manual
Change-Id: If17eddad60f8207fecf9518430a280261bde5b30
Fixes: 37107497
diff --git a/packages/SystemUI/src/com/android/systemui/DockedStackExistsListener.java b/packages/SystemUI/src/com/android/systemui/DockedStackExistsListener.java
new file mode 100644
index 0000000..c382882
--- /dev/null
+++ b/packages/SystemUI/src/com/android/systemui/DockedStackExistsListener.java
@@ -0,0 +1,69 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the
+ * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the specific language governing
+ * permissions and limitations under the License.
+ */
+
+package com.android.systemui;
+
+import android.os.RemoteException;
+import android.util.Log;
+import android.view.IDockedStackListener;
+import android.view.WindowManagerGlobal;
+
+import java.util.function.Consumer;
+
+/**
+ * Utility wrapper to listen for whether or not a docked stack exists, to be
+ * used for things like the different overview icon in that mode.
+ */
+public class DockedStackExistsListener extends IDockedStackListener.Stub {
+
+    private static final String TAG = "DockedStackExistsListener";
+
+    private final Consumer<Boolean> mCallback;
+
+    private DockedStackExistsListener(Consumer<Boolean> callback) {
+        mCallback = callback;
+    }
+
+    @Override
+    public void onDividerVisibilityChanged(boolean visible) throws RemoteException {
+    }
+
+    @Override
+    public void onDockedStackExistsChanged(final boolean exists) throws RemoteException {
+        mCallback.accept(exists);
+    }
+
+    @Override
+    public void onDockedStackMinimizedChanged(boolean minimized, long animDuration,
+                                              boolean isHomeStackResizable) throws RemoteException {
+    }
+
+    @Override
+    public void onAdjustedForImeChanged(boolean adjustedForIme, long animDuration)
+            throws RemoteException {
+    }
+
+    @Override
+    public void onDockSideChanged(int newDockSide) throws RemoteException {
+    }
+
+    public static void register(Consumer<Boolean> callback) {
+        try {
+            WindowManagerGlobal.getWindowManagerService().registerDockedStackListener(
+                    new DockedStackExistsListener(callback));
+        } catch (RemoteException e) {
+            Log.e(TAG, "Failed registering docked stack exists listener", e);
+        }
+    }
+}
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarView.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarView.java
index a5d7c57..1fd329c 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarView.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarView.java
@@ -47,6 +47,7 @@
 import android.widget.FrameLayout;
 
 import com.android.systemui.Dependency;
+import com.android.systemui.DockedStackExistsListener;
 import com.android.systemui.R;
 import com.android.systemui.RecentsComponent;
 import com.android.systemui.plugins.PluginListener;
@@ -566,40 +567,10 @@
 
         getImeSwitchButton().setOnClickListener(mImeSwitcherClickListener);
 
-        try {
-            WindowManagerGlobal.getWindowManagerService().registerDockedStackListener(new Stub() {
-                @Override
-                public void onDividerVisibilityChanged(boolean visible) throws RemoteException {
-                }
-
-                @Override
-                public void onDockedStackExistsChanged(final boolean exists) throws RemoteException {
-                    mHandler.post(new Runnable() {
-                        @Override
-                        public void run() {
-                            mDockedStackExists = exists;
-                            updateRecentsIcon();
-                        }
-                    });
-                }
-
-                @Override
-                public void onDockedStackMinimizedChanged(boolean minimized, long animDuration,
-                        boolean isHomeStackResizable) throws RemoteException {
-                }
-
-                @Override
-                public void onAdjustedForImeChanged(boolean adjustedForIme, long animDuration)
-                        throws RemoteException {
-                }
-
-                @Override
-                public void onDockSideChanged(int newDockSide) throws RemoteException {
-                }
-            });
-        } catch (RemoteException e) {
-            Log.e(TAG, "Failed registering docked stack exists listener", e);
-        }
+        DockedStackExistsListener.register(exists -> mHandler.post(() -> {
+            mDockedStackExists = exists;
+            updateRecentsIcon();
+        }));
     }
 
     void updateRotatedViews() {
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBarPolicy.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBarPolicy.java
index 5910557..47c6362 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBarPolicy.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBarPolicy.java
@@ -16,17 +16,11 @@
 
 package com.android.systemui.statusbar.phone;
 
-import android.app.ActivityManager;
+import android.app.*;
 import android.app.ActivityManager.StackId;
 import android.app.ActivityManager.StackInfo;
-import android.app.AlarmManager;
 import android.app.AlarmManager.AlarmClockInfo;
-import android.app.AppGlobals;
-import android.app.Notification;
 import android.app.Notification.Action;
-import android.app.NotificationManager;
-import android.app.PendingIntent;
-import android.app.SynchronousUserSwitchObserver;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
@@ -55,6 +49,7 @@
 import com.android.internal.telephony.IccCardConstants;
 import com.android.internal.telephony.TelephonyIntents;
 import com.android.systemui.Dependency;
+import com.android.systemui.DockedStackExistsListener;
 import com.android.systemui.R;
 import com.android.systemui.SysUiServiceProvider;
 import com.android.systemui.qs.tiles.DndTile;
@@ -132,6 +127,7 @@
     private boolean mZenVisible;
     private boolean mVolumeVisible;
     private boolean mCurrentUserSetup;
+    private boolean mDockedStackExists;
 
     private boolean mManagedProfileIconVisible = false;
     private boolean mManagedProfileInQuietMode = false;
@@ -248,6 +244,10 @@
                 noMan.cancel(notification.getTag(), notification.getId());
             }
         }
+        DockedStackExistsListener.register(exists -> {
+            mDockedStackExists = exists;
+            updateForegroundInstantApps();
+        });
     }
 
     public void destroy() {
@@ -495,23 +495,18 @@
         IPackageManager pm = AppGlobals.getPackageManager();
         mCurrentNotifs.clear();
         try {
+            ArraySet<Integer> stacksToCheck = new ArraySet<>();
             int[] STACKS_TO_CHECK = new int[]{
                     StackId.FULLSCREEN_WORKSPACE_STACK_ID,
                     StackId.DOCKED_STACK_ID,
             };
-            for (int i = 0; i < STACKS_TO_CHECK.length; i++) {
-                StackInfo info = ActivityManager.getService().getStackInfo(STACKS_TO_CHECK[i]);
-                if (info == null || info.topActivity == null) continue;
-                String pkg = info.topActivity.getPackageName();
-                if (!hasNotif(notifs, pkg, info.userId)) {
-                    // TODO: Optimize by not always needing to get application info.
-                    // Maybe cache non-ephemeral packages?
-                    ApplicationInfo appInfo = pm.getApplicationInfo(pkg,
-                            PackageManager.MATCH_UNINSTALLED_PACKAGES, info.userId);
-                    if (appInfo.isInstantApp()) {
-                        postEphemeralNotif(pkg, info.userId, appInfo, noMan);
-                    }
-                }
+            int focusedId = ActivityManager.getService().getFocusedStackId();
+            if (focusedId == StackId.FULLSCREEN_WORKSPACE_STACK_ID
+                    || focusedId == StackId.FULLSCREEN_WORKSPACE_STACK_ID) {
+                checkStack(StackId.FULLSCREEN_WORKSPACE_STACK_ID, notifs, noMan, pm);
+            }
+            if (mDockedStackExists) {
+                checkStack(StackId.DOCKED_STACK_ID, notifs, noMan, pm);
             }
         } catch (RemoteException e) {
             e.rethrowFromSystemServer();
@@ -521,6 +516,26 @@
                 new UserHandle(v.second)));
     }
 
+    private void checkStack(int stackId, ArraySet<Pair<String, Integer>> notifs,
+            NotificationManager noMan, IPackageManager pm) {
+        try {
+            StackInfo info = ActivityManager.getService().getStackInfo(stackId);
+            if (info == null || info.topActivity == null) return;
+            String pkg = info.topActivity.getPackageName();
+            if (!hasNotif(notifs, pkg, info.userId)) {
+                // TODO: Optimize by not always needing to get application info.
+                // Maybe cache non-ephemeral packages?
+                ApplicationInfo appInfo = pm.getApplicationInfo(pkg,
+                        PackageManager.MATCH_UNINSTALLED_PACKAGES, info.userId);
+                if (appInfo.isInstantApp()) {
+                    postEphemeralNotif(pkg, info.userId, appInfo, noMan);
+                }
+            }
+        } catch (RemoteException e) {
+            e.rethrowFromSystemServer();
+        }
+    }
+
     private void postEphemeralNotif(String pkg, int userId, ApplicationInfo appInfo,
             NotificationManager noMan) {
         final Bundle extras = new Bundle();