Fix janky add guest transition

Delay updating the user switcher until the
switch is complete, so we aren't showing
an intermediate state while the screen is
frozen.

Also pokes the UserInfoController to update
the user avatar while the screen is still frozen
so it is ready by thawing time.

Bug: 22557684
Change-Id: I020b8c1ad42c03ab9162ed384ecfe54ed12f998d
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
index 1e78f66..48d865e 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
@@ -591,7 +591,8 @@
         addNavigationBar();
 
         // Lastly, call to the icon policy to install/update all the icons.
-        mIconPolicy = new PhoneStatusBarPolicy(mContext, mCastController, mHotspotController);
+        mIconPolicy = new PhoneStatusBarPolicy(mContext, mCastController, mHotspotController,
+                mUserInfoController);
         mIconPolicy.setCurrentUserSetup(mUserSetup);
         mSettingsObserver.onChange(false); // set up
 
@@ -839,7 +840,8 @@
         mNextAlarmController = new NextAlarmController(mContext);
         mKeyguardMonitor = new KeyguardMonitor(mContext);
         if (UserSwitcherController.isUserSwitcherAvailable(UserManager.get(mContext))) {
-            mUserSwitcherController = new UserSwitcherController(mContext, mKeyguardMonitor);
+            mUserSwitcherController = new UserSwitcherController(mContext, mKeyguardMonitor,
+                    mHandler);
         }
         mKeyguardUserSwitcher = new KeyguardUserSwitcher(mContext,
                 (ViewStub) mStatusBarWindow.findViewById(R.id.keyguard_user_switcher),
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 b2a67bd..53dae5c 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBarPolicy.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBarPolicy.java
@@ -44,6 +44,7 @@
 import com.android.systemui.statusbar.policy.CastController;
 import com.android.systemui.statusbar.policy.CastController.CastDevice;
 import com.android.systemui.statusbar.policy.HotspotController;
+import com.android.systemui.statusbar.policy.UserInfoController;
 
 /**
  * This class contains all of the policy about which icons are installed in the status
@@ -69,6 +70,7 @@
     private final CastController mCast;
     private final HotspotController mHotspot;
     private final AlarmManager mAlarmManager;
+    private final UserInfoController mUserInfoController;
 
     // Assume it's all good unless we hear otherwise.  We don't always seem
     // to get broadcasts that it *is* there.
@@ -111,12 +113,14 @@
         }
     };
 
-    public PhoneStatusBarPolicy(Context context, CastController cast, HotspotController hotspot) {
+    public PhoneStatusBarPolicy(Context context, CastController cast, HotspotController hotspot,
+            UserInfoController userInfoController) {
         mContext = context;
         mCast = cast;
         mHotspot = hotspot;
         mService = (StatusBarManager) context.getSystemService(Context.STATUS_BAR_SERVICE);
         mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
+        mUserInfoController = userInfoController;
 
         // listen for broadcasts
         IntentFilter filter = new IntentFilter();
@@ -360,6 +364,7 @@
             new IUserSwitchObserver.Stub() {
                 @Override
                 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
+                    mUserInfoController.reloadUserInfo();
                 }
 
                 @Override
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/UserSwitcherController.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/UserSwitcherController.java
index 41fc967..6d1ea1c 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/UserSwitcherController.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/UserSwitcherController.java
@@ -39,6 +39,8 @@
 import android.provider.Settings;
 import android.util.Log;
 import android.util.SparseArray;
+import android.util.SparseBooleanArray;
+import android.util.SparseIntArray;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.BaseAdapter;
@@ -68,6 +70,7 @@
     private static final String SIMPLE_USER_SWITCHER_GLOBAL_SETTING =
             "lockscreenSimpleUserSwitcher";
     private static final String ACTION_REMOVE_GUEST = "com.android.systemui.REMOVE_GUEST";
+    private static final int PAUSE_REFRESH_USERS_TIMEOUT_MS = 3000;
 
     private final Context mContext;
     private final UserManager mUserManager;
@@ -75,6 +78,7 @@
     private final GuestResumeSessionReceiver mGuestResumeSessionReceiver
             = new GuestResumeSessionReceiver();
     private final KeyguardMonitor mKeyguardMonitor;
+    private final Handler mHandler;
 
     private ArrayList<UserRecord> mUsers = new ArrayList<>();
     private Dialog mExitGuestDialog;
@@ -82,11 +86,15 @@
     private int mLastNonGuestUser = UserHandle.USER_OWNER;
     private boolean mSimpleUserSwitcher;
     private boolean mAddUsersWhenLocked;
+    private boolean mPauseRefreshUsers;
+    private SparseBooleanArray mForcePictureLoadForUserId = new SparseBooleanArray(2);
 
-    public UserSwitcherController(Context context, KeyguardMonitor keyguardMonitor) {
+    public UserSwitcherController(Context context, KeyguardMonitor keyguardMonitor,
+            Handler handler) {
         mContext = context;
         mGuestResumeSessionReceiver.register(context);
         mKeyguardMonitor = keyguardMonitor;
+        mHandler = handler;
         mUserManager = UserManager.get(context);
         IntentFilter filter = new IntentFilter();
         filter.addAction(Intent.ACTION_USER_ADDED);
@@ -122,17 +130,26 @@
      */
     @SuppressWarnings("unchecked")
     private void refreshUsers(int forcePictureLoadForId) {
+        if (DEBUG) Log.d(TAG, "refreshUsers(forcePictureLoadForId=" + forcePictureLoadForId+")");
+        if (forcePictureLoadForId != UserHandle.USER_NULL) {
+            mForcePictureLoadForUserId.put(forcePictureLoadForId, true);
+        }
+
+        if (mPauseRefreshUsers) {
+            return;
+        }
 
         SparseArray<Bitmap> bitmaps = new SparseArray<>(mUsers.size());
         final int N = mUsers.size();
         for (int i = 0; i < N; i++) {
             UserRecord r = mUsers.get(i);
-            if (r == null || r.info == null
-                    || r.info.id == forcePictureLoadForId || r.picture == null) {
+            if (r == null || r.picture == null ||
+                    r.info == null || mForcePictureLoadForUserId.get(r.info.id)) {
                 continue;
             }
             bitmaps.put(r.info.id, r.picture);
         }
+        mForcePictureLoadForUserId.clear();
 
         final boolean addUsersWhenLocked = mAddUsersWhenLocked;
         new AsyncTask<SparseArray<Bitmap>, Void, ArrayList<UserRecord>>() {
@@ -215,6 +232,13 @@
         }.execute((SparseArray) bitmaps);
     }
 
+    private void pauseRefreshUsers() {
+        if (!mPauseRefreshUsers) {
+            mHandler.postDelayed(mUnpauseRefreshUsers, PAUSE_REFRESH_USERS_TIMEOUT_MS);
+            mPauseRefreshUsers = true;
+        }
+    }
+
     private void notifyAdapters() {
         for (int i = mAdapters.size() - 1; i >= 0; i--) {
             BaseUserAdapter adapter = mAdapters.get(i).get();
@@ -261,6 +285,7 @@
 
     private void switchToUserId(int id) {
         try {
+            pauseRefreshUsers();
             ActivityManagerNative.getDefault().switchUser(id);
         } catch (RemoteException e) {
             Log.e(TAG, "Couldn't switch user.", e);
@@ -302,6 +327,10 @@
                 Log.v(TAG, "Broadcast: a=" + intent.getAction()
                        + " user=" + intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1));
             }
+
+            boolean unpauseRefreshUsers = false;
+            int forcePictureLoadForId = UserHandle.USER_NULL;
+
             if (ACTION_REMOVE_GUEST.equals(intent.getAction())) {
                 int currentUser = ActivityManager.getCurrentUser();
                 UserInfo userInfo = mUserManager.getUserInfo(currentUser);
@@ -309,16 +338,13 @@
                     showExitGuestDialog(currentUser);
                 }
                 return;
-            }
-            if (Intent.ACTION_USER_ADDED.equals(intent.getAction())) {
+            } else if (Intent.ACTION_USER_ADDED.equals(intent.getAction())) {
                 final int currentId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1);
                 UserInfo userInfo = mUserManager.getUserInfo(currentId);
                 if (userInfo != null && userInfo.isGuest()) {
                     showGuestNotification(currentId);
                 }
-            }
-
-            if (Intent.ACTION_USER_SWITCHED.equals(intent.getAction())) {
+            } else if (Intent.ACTION_USER_SWITCHED.equals(intent.getAction())) {
                 if (mExitGuestDialog != null && mExitGuestDialog.isShowing()) {
                     mExitGuestDialog.cancel();
                     mExitGuestDialog = null;
@@ -343,13 +369,15 @@
                     }
                 }
                 notifyAdapters();
-            }
-            int forcePictureLoadForId = UserHandle.USER_NULL;
-            if (Intent.ACTION_USER_INFO_CHANGED.equals(intent.getAction())) {
+                unpauseRefreshUsers = true;
+            } else if (Intent.ACTION_USER_INFO_CHANGED.equals(intent.getAction())) {
                 forcePictureLoadForId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE,
                         UserHandle.USER_NULL);
             }
             refreshUsers(forcePictureLoadForId);
+            if (unpauseRefreshUsers) {
+                mUnpauseRefreshUsers.run();
+            }
         }
 
         private void showGuestNotification(int guestUserId) {
@@ -371,6 +399,15 @@
         }
     };
 
+    private final Runnable mUnpauseRefreshUsers = new Runnable() {
+        @Override
+        public void run() {
+            mHandler.removeCallbacks(this);
+            mPauseRefreshUsers = false;
+            refreshUsers(UserHandle.USER_NULL);
+        }
+    };
+
     private final ContentObserver mSettingsObserver = new ContentObserver(new Handler()) {
         public void onChange(boolean selfChange) {
             mSimpleUserSwitcher = Settings.Global.getInt(mContext.getContentResolver(),