1616
1717package com .android .systemui .statusbar .phone ;
1818
19+ import com .android .internal .view .RotationPolicy ;
20+ import com .android .internal .widget .LockPatternUtils ;
21+ import com .android .systemui .R ;
22+ import com .android .systemui .statusbar .phone .QuickSettingsModel .RSSIState ;
23+ import com .android .systemui .statusbar .phone .QuickSettingsModel .State ;
24+ import com .android .systemui .statusbar .phone .QuickSettingsModel .UserState ;
25+ import com .android .systemui .statusbar .policy .BatteryController ;
26+ import com .android .systemui .statusbar .policy .BluetoothController ;
27+ import com .android .systemui .statusbar .policy .BrightnessController ;
28+ import com .android .systemui .statusbar .policy .LocationController ;
29+ import com .android .systemui .statusbar .policy .NetworkController ;
30+ import com .android .systemui .statusbar .policy .ToggleSlider ;
31+
1932import android .app .ActivityManagerNative ;
2033import android .app .AlertDialog ;
2134import android .app .Dialog ;
2942import android .content .DialogInterface .OnClickListener ;
3043import android .content .Intent ;
3144import android .content .IntentFilter ;
32- import android .content .Loader ;
45+ import android .content .pm .PackageManager .NameNotFoundException ;
46+ import android .content .pm .UserInfo ;
3347import android .content .res .Resources ;
3448import android .database .Cursor ;
49+ import android .graphics .Bitmap ;
3550import android .graphics .drawable .BitmapDrawable ;
3651import android .graphics .drawable .Drawable ;
3752import android .graphics .drawable .LevelListDrawable ;
3853import android .hardware .display .DisplayManager ;
3954import android .hardware .display .WifiDisplayStatus ;
40- import android .net . Uri ;
55+ import android .os . AsyncTask ;
4156import android .os .Handler ;
4257import android .os .RemoteException ;
43- import android .os .SystemProperties ;
4458import android .os .UserHandle ;
4559import android .os .UserManager ;
4660import android .provider .ContactsContract ;
61+ import android .provider .ContactsContract .CommonDataKinds .Phone ;
62+ import android .provider .ContactsContract .Profile ;
4763import android .provider .Settings ;
64+ import android .util .Log ;
65+ import android .util .Pair ;
4866import android .view .LayoutInflater ;
4967import android .view .View ;
5068import android .view .ViewGroup ;
5169import android .view .Window ;
5270import android .view .WindowManager ;
71+ import android .view .WindowManagerGlobal ;
5372import android .widget .ImageView ;
5473import android .widget .TextView ;
5574
56- import com .android .internal .view .RotationPolicy ;
57- import com .android .systemui .R ;
58- import com .android .systemui .statusbar .phone .QuickSettingsModel .RSSIState ;
59- import com .android .systemui .statusbar .phone .QuickSettingsModel .State ;
60- import com .android .systemui .statusbar .phone .QuickSettingsModel .UserState ;
61- import com .android .systemui .statusbar .policy .BatteryController ;
62- import com .android .systemui .statusbar .policy .BluetoothController ;
63- import com .android .systemui .statusbar .policy .BrightnessController ;
64- import com .android .systemui .statusbar .policy .LocationController ;
65- import com .android .systemui .statusbar .policy .NetworkController ;
66- import com .android .systemui .statusbar .policy .ToggleSlider ;
67-
6875import java .util .ArrayList ;
6976
7077
7178/**
7279 *
7380 */
7481class QuickSettings {
82+ private static final String TAG = "QuickSettings" ;
7583 public static final boolean SHOW_IME_TILE = false ;
7684
7785 private Context mContext ;
@@ -91,11 +99,13 @@ class QuickSettings {
9199 private int mBrightnessDialogShortTimeout ;
92100 private int mBrightnessDialogLongTimeout ;
93101
94- private CursorLoader mUserInfoLoader ;
102+ private AsyncTask < Void , Void , Pair < String , BitmapDrawable >> mUserInfoTask ;
95103
96104 private LevelListDrawable mBatteryLevels ;
97105 private LevelListDrawable mChargingBatteryLevels ;
98106
107+ boolean mTilesSetUp = false ;
108+
99109 private Handler mHandler ;
100110
101111 // The set of QuickSettingsTiles that have dynamic spans (and need to be updated on
@@ -132,7 +142,14 @@ public QuickSettings(Context context, QuickSettingsContainerView container) {
132142 IntentFilter filter = new IntentFilter ();
133143 filter .addAction (DisplayManager .ACTION_WIFI_DISPLAY_STATUS_CHANGED );
134144 filter .addAction (BluetoothAdapter .ACTION_CONNECTION_STATE_CHANGED );
145+ filter .addAction (Intent .ACTION_USER_SWITCHED );
135146 mContext .registerReceiver (mReceiver , filter );
147+
148+ IntentFilter profileFilter = new IntentFilter ();
149+ profileFilter .addAction (ContactsContract .Intents .ACTION_PROFILE_CHANGED );
150+ profileFilter .addAction (Intent .ACTION_USER_INFO_CHANGED );
151+ mContext .registerReceiverAsUser (mProfileReceiver , UserHandle .ALL , profileFilter ,
152+ null , null );
136153 }
137154
138155 void setBar (PanelBar bar ) {
@@ -168,47 +185,51 @@ void setup(NetworkController networkController, BluetoothController bluetoothCon
168185 }
169186
170187 private void queryForUserInformation () {
171- System .out .println ("queryForUserInformation" );
188+ Context currentUserContext = null ;
189+ UserInfo userInfo = null ;
190+ try {
191+ userInfo = ActivityManagerNative .getDefault ().getCurrentUser ();
192+ currentUserContext = mContext .createPackageContextAsUser ("android" , 0 ,
193+ new UserHandle (userInfo .id ));
194+ } catch (NameNotFoundException e ) {
195+ Log .e (TAG , "Couldn't create user context" , e );
196+ throw new RuntimeException (e );
197+ } catch (RemoteException e ) {
198+ Log .e (TAG , "Couldn't get user info" , e );
199+ }
200+ final int userId = userInfo .id ;
172201
173- Uri userContactUri = Uri .withAppendedPath (
174- ContactsContract .Profile .CONTENT_URI ,
175- ContactsContract .Contacts .Data .CONTENT_DIRECTORY );
202+ final Context context = currentUserContext ;
203+ mUserInfoTask = new AsyncTask <Void , Void , Pair <String , BitmapDrawable >>() {
204+ @ Override
205+ protected Pair <String , BitmapDrawable > doInBackground (Void ... params ) {
206+ Cursor cursor = context .getContentResolver ().query (
207+ Profile .CONTENT_URI , new String [] {Phone ._ID , Phone .DISPLAY_NAME },
208+ null , null , null );
209+
210+ String name = null ;
211+ try {
212+ if (cursor .moveToFirst ()) {
213+ name = cursor .getString (cursor .getColumnIndex (Phone .DISPLAY_NAME ));
214+ }
215+ } finally {
216+ cursor .close ();
217+ }
218+ final UserManager userManager =
219+ (UserManager ) mContext .getSystemService (Context .USER_SERVICE );
220+ final BitmapDrawable icon = new BitmapDrawable (mContext .getResources (),
221+ userManager .getUserIcon (userId ));
222+ return new Pair <String , BitmapDrawable >(name , icon );
223+ }
176224
177- String [] selectArgs = {
178- ContactsContract .CommonDataKinds .StructuredName .DISPLAY_NAME ,
179- ContactsContract .CommonDataKinds .Photo .PHOTO
180- };
181- String where = String .format ("(%s = ? OR %s = ?) AND %s IS NULL" ,
182- ContactsContract .Contacts .Data .MIMETYPE ,
183- ContactsContract .Contacts .Data .MIMETYPE ,
184- ContactsContract .RawContacts .ACCOUNT_TYPE );
185- String [] whereArgs = {
186- ContactsContract .CommonDataKinds .StructuredName .CONTENT_ITEM_TYPE ,
187- ContactsContract .CommonDataKinds .Photo .CONTENT_ITEM_TYPE
225+ @ Override
226+ protected void onPostExecute (Pair <String , BitmapDrawable > result ) {
227+ super .onPostExecute (result );
228+ mModel .setUserTileInfo (result .first , result .second );
229+ mUserInfoTask = null ;
230+ }
188231 };
189-
190- mUserInfoLoader = new CursorLoader (mContext , userContactUri , selectArgs , where , whereArgs ,
191- null );
192- mUserInfoLoader .registerListener (0 ,
193- new Loader .OnLoadCompleteListener <Cursor >() {
194- @ Override
195- public void onLoadComplete (Loader <Cursor > loader ,
196- Cursor cursor ) {
197- UserManager userManager = (UserManager ) mContext .getSystemService (Context .USER_SERVICE );
198- if (cursor != null && cursor .moveToFirst ()) {
199- String name = cursor .getString (0 ); // DISPLAY_NAME
200- BitmapDrawable d = new BitmapDrawable (userManager .getUserIcon (userManager .getUserHandle ()));
201- mModel .setUserTileInfo (name , d );
202- /*
203- byte[] photoData = cursor.getBlob(0);
204- Bitmap b =
205- BitmapFactory.decodeByteArray(photoData, 0, photoData.length);
206- */
207- }
208- mUserInfoLoader .stopLoading ();
209- }
210- });
211- mUserInfoLoader .startLoading ();
232+ mUserInfoTask .execute ();
212233 }
213234
214235 private void setupQuickSettings () {
@@ -220,6 +241,7 @@ private void setupQuickSettings() {
220241 addTemporaryTiles (mContainerView , inflater );
221242
222243 queryForUserInformation ();
244+ mTilesSetUp = true ;
223245 }
224246
225247 private void startSettingsActivity (String action ) {
@@ -251,10 +273,21 @@ private void addUserTiles(ViewGroup parent, LayoutInflater inflater) {
251273 @ Override
252274 public void onClick (View v ) {
253275 mBar .collapseAllPanels (true );
254- Intent intent = ContactsContract .QuickContact .composeQuickContactsIntent (mContext ,
255- v , ContactsContract .Profile .CONTENT_URI ,
256- ContactsContract .QuickContact .MODE_LARGE , null );
257- mContext .startActivityAsUser (intent , new UserHandle (UserHandle .USER_CURRENT ));
276+ final UserManager um =
277+ (UserManager ) mContext .getSystemService (Context .USER_SERVICE );
278+ if (um .getUsers ().size () > 1 ) {
279+ try {
280+ WindowManagerGlobal .getWindowManagerService ().lockNow (
281+ LockPatternUtils .USER_SWITCH_LOCK_OPTIONS );
282+ } catch (RemoteException e ) {
283+ Log .e (TAG , "Couldn't show user switcher" , e );
284+ }
285+ } else {
286+ Intent intent = ContactsContract .QuickContact .composeQuickContactsIntent (
287+ mContext , v , ContactsContract .Profile .CONTENT_URI ,
288+ ContactsContract .QuickContact .MODE_LARGE , null );
289+ mContext .startActivityAsUser (intent , new UserHandle (UserHandle .USER_CURRENT ));
290+ }
258291 }
259292 });
260293 mModel .addUserTile (userTile , new QuickSettingsModel .RefreshCallback () {
@@ -748,20 +781,50 @@ private void applyBluetoothStatus() {
748781 mModel .onBluetoothStateChange (mBluetoothState );
749782 }
750783
784+ void reloadUserInfo () {
785+ if (mUserInfoTask != null ) {
786+ mUserInfoTask .cancel (false );
787+ mUserInfoTask = null ;
788+ }
789+ if (mTilesSetUp ) {
790+ queryForUserInformation ();
791+ }
792+ }
793+
751794 private final BroadcastReceiver mReceiver = new BroadcastReceiver () {
752795 @ Override
753796 public void onReceive (Context context , Intent intent ) {
754- if (intent .getAction ().equals (DisplayManager .ACTION_WIFI_DISPLAY_STATUS_CHANGED )) {
797+ final String action = intent .getAction ();
798+ if (DisplayManager .ACTION_WIFI_DISPLAY_STATUS_CHANGED .equals (action )) {
755799 WifiDisplayStatus status = (WifiDisplayStatus )intent .getParcelableExtra (
756800 DisplayManager .EXTRA_WIFI_DISPLAY_STATUS );
757801 mWifiDisplayStatus = status ;
758802 applyWifiDisplayStatus ();
759- }
760- if (intent .getAction ().equals (BluetoothAdapter .ACTION_CONNECTION_STATE_CHANGED )) {
803+ } else if (BluetoothAdapter .ACTION_CONNECTION_STATE_CHANGED .equals (action )) {
761804 int status = intent .getIntExtra (BluetoothAdapter .EXTRA_CONNECTION_STATE ,
762805 BluetoothAdapter .STATE_DISCONNECTED );
763806 mBluetoothState .connected = (status == BluetoothAdapter .STATE_CONNECTED );
764807 applyBluetoothStatus ();
808+ } else if (Intent .ACTION_USER_SWITCHED .equals (action )) {
809+ reloadUserInfo ();
810+ }
811+ }
812+ };
813+
814+ private final BroadcastReceiver mProfileReceiver = new BroadcastReceiver () {
815+ @ Override
816+ public void onReceive (Context context , Intent intent ) {
817+ final String action = intent .getAction ();
818+ if (ContactsContract .Intents .ACTION_PROFILE_CHANGED .equals (action ) ||
819+ Intent .ACTION_USER_INFO_CHANGED .equals (action )) {
820+ try {
821+ final int userId = ActivityManagerNative .getDefault ().getCurrentUser ().id ;
822+ if (getSendingUserId () == userId ) {
823+ reloadUserInfo ();
824+ }
825+ } catch (RemoteException e ) {
826+ Log .e (TAG , "Couldn't get current user id for profile change" , e );
827+ }
765828 }
766829 }
767830 };
0 commit comments