Fix density change bugs in SystemUI

- Reinflate KeyguardUserSwitcher on density changes.
- Update size of UserAvatar on Keyguard.
- Fix brightness slider width in quick settings.
- Refetch and rescale user icons on density changes.

Change-Id: I961c59b8b224d6ac22cbbb8e115b8a12547a04b9
Fixes: 28535987
Fixes: 28454340
diff --git a/packages/SystemUI/res/layout/keyguard_user_switcher.xml b/packages/SystemUI/res/layout/keyguard_user_switcher.xml
index 22834e1..983ba6d 100644
--- a/packages/SystemUI/res/layout/keyguard_user_switcher.xml
+++ b/packages/SystemUI/res/layout/keyguard_user_switcher.xml
@@ -19,14 +19,5 @@
         android:visibility="gone"
         android:layout_height="match_parent"
         android:layout_width="match_parent">
-    <com.android.keyguard.AlphaOptimizedLinearLayout
-            android:id="@+id/keyguard_user_switcher_inner"
-            android:orientation="vertical"
-            android:layout_height="wrap_content"
-            android:layout_width="wrap_content"
-            android:layout_marginTop="@dimen/status_bar_header_height_keyguard"
-            android:layout_gravity="end"
-            android:gravity="end"
-            android:paddingTop="4dp">
-    </com.android.keyguard.AlphaOptimizedLinearLayout>
+    <!-- KeyguardUserSwitcher loads keyguard_user_switcher_inner.xml here -->
 </view>
\ No newline at end of file
diff --git a/packages/SystemUI/res/layout/keyguard_user_switcher_inner.xml b/packages/SystemUI/res/layout/keyguard_user_switcher_inner.xml
new file mode 100644
index 0000000..4c1042e
--- /dev/null
+++ b/packages/SystemUI/res/layout/keyguard_user_switcher_inner.xml
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  ~ Copyright (C) 2016 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
+  -->
+<com.android.keyguard.AlphaOptimizedLinearLayout
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    android:id="@+id/keyguard_user_switcher_inner"
+    android:orientation="vertical"
+    android:layout_height="wrap_content"
+    android:layout_width="wrap_content"
+    android:layout_marginTop="@dimen/status_bar_header_height_keyguard"
+    android:layout_gravity="end"
+    android:gravity="end"
+    android:paddingTop="4dp">
+</com.android.keyguard.AlphaOptimizedLinearLayout>
diff --git a/packages/SystemUI/res/layout/quick_settings_brightness_dialog.xml b/packages/SystemUI/res/layout/quick_settings_brightness_dialog.xml
index 5fde4f6..5cbe635 100644
--- a/packages/SystemUI/res/layout/quick_settings_brightness_dialog.xml
+++ b/packages/SystemUI/res/layout/quick_settings_brightness_dialog.xml
@@ -16,6 +16,7 @@
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:systemui="http://schemas.android.com/apk/res-auto"
     android:layout_height="48dp"
+    android:layout_width="match_parent"
     android:paddingLeft="16dp"
     android:paddingRight="16dp"
     style="@style/BrightnessDialogContainer">
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/KeyguardStatusBarView.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/KeyguardStatusBarView.java
index 74bd096..e08b945 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/KeyguardStatusBarView.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/KeyguardStatusBarView.java
@@ -85,6 +85,11 @@
                         com.android.internal.R.dimen.text_size_small_material));
         mBatteryLevel.setTextSize(TypedValue.COMPLEX_UNIT_PX,
                 getResources().getDimensionPixelSize(R.dimen.battery_level_text_size));
+
+        MarginLayoutParams lp = (MarginLayoutParams) mMultiUserAvatar.getLayoutParams();
+        lp.width = lp.height = getResources().getDimensionPixelSize(
+                R.dimen.multi_user_avatar_keyguard_size);
+        mMultiUserAvatar.setLayoutParams(lp);
     }
 
     private void loadDimens() {
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 20809ea..dd0d31a 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
@@ -966,7 +966,9 @@
         super.onDensityOrFontScaleChanged();
         mScrimController.onDensityOrFontScaleChanged();
         mStatusBarView.onDensityOrFontScaleChanged();
-        mBrightnessMirrorController.onDensityOrFontScaleChanged();
+        if (mBrightnessMirrorController != null) {
+            mBrightnessMirrorController.onDensityOrFontScaleChanged();
+        }
         inflateSignalClusters();
         mIconController.onDensityOrFontScaleChanged();
         inflateDismissView();
@@ -975,6 +977,13 @@
         updateEmptyShadeView();
         inflateOverflowContainer();
         mStatusBarKeyguardViewManager.onDensityOrFontScaleChanged();
+        mUserInfoController.onDensityOrFontScaleChanged();
+        if (mUserSwitcherController != null) {
+            mUserSwitcherController.onDensityOrFontScaleChanged();
+        }
+        if (mKeyguardUserSwitcher != null) {
+            mKeyguardUserSwitcher.onDensityOrFontScaleChanged();
+        }
     }
 
     private void inflateSignalClusters() {
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/KeyguardUserSwitcher.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/KeyguardUserSwitcher.java
index c39d718..21f3f5e 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/KeyguardUserSwitcher.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/KeyguardUserSwitcher.java
@@ -21,8 +21,6 @@
 import android.animation.ObjectAnimator;
 import android.content.Context;
 import android.database.DataSetObserver;
-import android.graphics.drawable.BitmapDrawable;
-import android.graphics.drawable.Drawable;
 import android.util.AttributeSet;
 import android.view.LayoutInflater;
 import android.view.MotionEvent;
@@ -47,11 +45,12 @@
     private static final boolean ALWAYS_ON = false;
 
     private final Container mUserSwitcherContainer;
-    private final ViewGroup mUserSwitcher;
     private final KeyguardStatusBarView mStatusBarView;
     private final Adapter mAdapter;
     private final AppearAnimationUtils mAppearAnimationUtils;
     private final KeyguardUserSwitcherScrim mBackground;
+
+    private ViewGroup mUserSwitcher;
     private ObjectAnimator mBgAnimator;
     private UserSwitcherController mUserSwitcherController;
     private boolean mAnimating;
@@ -63,10 +62,8 @@
                 context.getResources().getBoolean(R.bool.config_keyguardUserSwitcher) || ALWAYS_ON;
         if (userSwitcherController != null && keyguardUserSwitcherEnabled) {
             mUserSwitcherContainer = (Container) userSwitcher.inflate();
-            mUserSwitcher = (ViewGroup)
-                    mUserSwitcherContainer.findViewById(R.id.keyguard_user_switcher_inner);
-            mBackground = new KeyguardUserSwitcherScrim(mUserSwitcher);
-            mUserSwitcher.setBackground(mBackground);
+            mBackground = new KeyguardUserSwitcherScrim(context);
+            reinflateViews();
             mStatusBarView = statusBarView;
             mStatusBarView.setKeyguardUserSwitcher(this);
             panelView.setKeyguardUserSwitcher(this);
@@ -78,7 +75,6 @@
             mUserSwitcherContainer.setKeyguardUserSwitcher(this);
         } else {
             mUserSwitcherContainer = null;
-            mUserSwitcher = null;
             mStatusBarView = null;
             mAdapter = null;
             mAppearAnimationUtils = null;
@@ -86,6 +82,22 @@
         }
     }
 
+    private void reinflateViews() {
+        if (mUserSwitcher != null) {
+            mUserSwitcher.setBackground(null);
+            mUserSwitcher.removeOnLayoutChangeListener(mBackground);
+        }
+        mUserSwitcherContainer.removeAllViews();
+
+        LayoutInflater.from(mUserSwitcherContainer.getContext())
+                .inflate(R.layout.keyguard_user_switcher_inner, mUserSwitcherContainer);
+
+        mUserSwitcher = (ViewGroup) mUserSwitcherContainer.findViewById(
+                R.id.keyguard_user_switcher_inner);
+        mUserSwitcher.addOnLayoutChangeListener(mBackground);
+        mUserSwitcher.setBackground(mBackground);
+    }
+
     public void setKeyguard(boolean keyguard, boolean animate) {
         if (mUserSwitcher != null) {
             if (keyguard && shouldExpandByDefault()) {
@@ -228,6 +240,13 @@
         }
     };
 
+    public void onDensityOrFontScaleChanged() {
+        if (mUserSwitcherContainer != null) {
+            reinflateViews();
+            refresh();
+        }
+    }
+
     public static class Adapter extends UserSwitcherController.BaseUserAdapter implements
             View.OnClickListener {
 
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/KeyguardUserSwitcherScrim.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/KeyguardUserSwitcherScrim.java
index 353e07d..49f5bcd 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/KeyguardUserSwitcherScrim.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/KeyguardUserSwitcherScrim.java
@@ -16,6 +16,7 @@
 
 package com.android.systemui.statusbar.policy;
 
+import android.content.Context;
 import android.graphics.Canvas;
 import android.graphics.Color;
 import android.graphics.ColorFilter;
@@ -45,9 +46,8 @@
     private Paint mRadialGradientPaint = new Paint();
     private int mLayoutWidth;
 
-    public KeyguardUserSwitcherScrim(View host) {
-        host.addOnLayoutChangeListener(this);
-        mDarkColor = host.getContext().getColor(
+    public KeyguardUserSwitcherScrim(Context context) {
+        mDarkColor = context.getColor(
                 R.color.keyguard_user_switcher_background_gradient_color);
     }
 
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/UserInfoController.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/UserInfoController.java
index bae5bda..c5a4f5e 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/UserInfoController.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/UserInfoController.java
@@ -50,7 +50,6 @@
             new ArrayList<OnUserInfoChangedListener>();
     private AsyncTask<Void, Void, Pair<String, Drawable>> mUserInfoTask;
 
-    private boolean mUseDefaultAvatar;
     private String mUserName;
     private Drawable mUserDrawable;
 
@@ -58,7 +57,6 @@
         mContext = context;
         IntentFilter filter = new IntentFilter();
         filter.addAction(Intent.ACTION_USER_SWITCHED);
-        filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
         mContext.registerReceiver(mReceiver, filter);
 
         IntentFilter profileFilter = new IntentFilter();
@@ -83,10 +81,6 @@
             final String action = intent.getAction();
             if (Intent.ACTION_USER_SWITCHED.equals(action)) {
                 reloadUserInfo();
-            } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
-                if (mUseDefaultAvatar) {
-                    reloadUserInfo();
-                }
             }
         }
     };
@@ -159,7 +153,6 @@
                 } else {
                     avatar = UserIcons.getDefaultUserIcon(isGuest? UserHandle.USER_NULL : userId,
                             /* light= */ true);
-                    mUseDefaultAvatar = true;
                 }
 
                 // If it's a single-user device, get the profile name, since the nickname is not
@@ -202,6 +195,10 @@
         }
     }
 
+    public void onDensityOrFontScaleChanged() {
+        reloadUserInfo();
+    }
+
     public interface OnUserInfoChangedListener {
         public void onUserInfoChanged(String name, Drawable picture);
     }
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 6800772..415b7a1 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/UserSwitcherController.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/UserSwitcherController.java
@@ -23,7 +23,6 @@
 import android.app.NotificationManager;
 import android.app.PendingIntent;
 import android.content.BroadcastReceiver;
-import android.content.ComponentName;
 import android.content.Context;
 import android.content.DialogInterface;
 import android.content.Intent;
@@ -169,12 +168,13 @@
             return;
         }
 
+        boolean forceAllUsers = mForcePictureLoadForUserId.get(UserHandle.USER_ALL);
         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.picture == null ||
-                    r.info == null || mForcePictureLoadForUserId.get(r.info.id)) {
+            if (r == null || r.picture == null || r.info == null || forceAllUsers
+                    || mForcePictureLoadForUserId.get(r.info.id)) {
                 continue;
             }
             bitmaps.put(r.info.id, r.picture);
@@ -600,6 +600,10 @@
         return item.info.name;
     }
 
+    public void onDensityOrFontScaleChanged() {
+        refreshUsers(UserHandle.USER_ALL);
+    }
+
     public static abstract class BaseUserAdapter extends BaseAdapter {
 
         final UserSwitcherController mController;