blob: c09747b73f9598381730a7b401b7c6e0a2246f19 [file] [log] [blame]
Jorim Jaggi3d878be2014-05-10 03:22:32 +02001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
15 */
16
17package com.android.systemui.statusbar.policy;
18
Sudheer Shankadc589ac2016-11-10 15:30:17 -080019import android.app.ActivityManager;
Jorim Jaggi3d878be2014-05-10 03:22:32 +020020import android.content.BroadcastReceiver;
21import android.content.Context;
22import android.content.Intent;
23import android.content.IntentFilter;
24import android.content.pm.PackageManager;
25import android.content.pm.UserInfo;
Adrian Roos2b154a92014-11-17 15:18:39 +010026import android.content.res.Resources;
Jorim Jaggi3d878be2014-05-10 03:22:32 +020027import android.database.Cursor;
28import android.graphics.Bitmap;
Jorim Jaggi3d878be2014-05-10 03:22:32 +020029import android.graphics.drawable.Drawable;
Jorim Jaggi3d878be2014-05-10 03:22:32 +020030import android.os.AsyncTask;
31import android.os.RemoteException;
32import android.os.UserHandle;
33import android.os.UserManager;
34import android.provider.ContactsContract;
Jorim Jaggi3d878be2014-05-10 03:22:32 +020035import android.util.Log;
Jorim Jaggi3d878be2014-05-10 03:22:32 +020036
Winsonc0d70582016-01-29 10:24:39 -080037import com.android.internal.util.UserIcons;
Evan Roskyaa7f51f2016-03-16 13:15:53 -070038import com.android.settingslib.drawable.UserIconDrawable;
Jorim Jaggi3d878be2014-05-10 03:22:32 +020039import com.android.systemui.R;
Jason Monk88529052016-11-04 13:29:58 -040040import com.android.systemui.statusbar.policy.UserInfoController.OnUserInfoChangedListener;
Jorim Jaggi3d878be2014-05-10 03:22:32 +020041
42import java.util.ArrayList;
Jorim Jaggi3d878be2014-05-10 03:22:32 +020043
Jason Monk88529052016-11-04 13:29:58 -040044public class UserInfoController implements CallbackController<OnUserInfoChangedListener> {
Jorim Jaggi3d878be2014-05-10 03:22:32 +020045
46 private static final String TAG = "UserInfoController";
47
48 private final Context mContext;
49 private final ArrayList<OnUserInfoChangedListener> mCallbacks =
50 new ArrayList<OnUserInfoChangedListener>();
Jiaquan Hee1206372016-05-03 14:11:23 -070051 private AsyncTask<Void, Void, UserInfoQueryResult> mUserInfoTask;
Jorim Jaggi3d878be2014-05-10 03:22:32 +020052
Jorim Jaggi3d878be2014-05-10 03:22:32 +020053 private String mUserName;
54 private Drawable mUserDrawable;
Jiaquan Hee1206372016-05-03 14:11:23 -070055 private String mUserAccount;
Jorim Jaggi3d878be2014-05-10 03:22:32 +020056
57 public UserInfoController(Context context) {
58 mContext = context;
59 IntentFilter filter = new IntentFilter();
60 filter.addAction(Intent.ACTION_USER_SWITCHED);
Jorim Jaggi3d878be2014-05-10 03:22:32 +020061 mContext.registerReceiver(mReceiver, filter);
62
63 IntentFilter profileFilter = new IntentFilter();
64 profileFilter.addAction(ContactsContract.Intents.ACTION_PROFILE_CHANGED);
65 profileFilter.addAction(Intent.ACTION_USER_INFO_CHANGED);
66 mContext.registerReceiverAsUser(mProfileReceiver, UserHandle.ALL, profileFilter,
67 null, null);
68 }
69
Jason Monk88529052016-11-04 13:29:58 -040070 public void addCallback(OnUserInfoChangedListener callback) {
Jorim Jaggi3d878be2014-05-10 03:22:32 +020071 mCallbacks.add(callback);
Jiaquan Hee1206372016-05-03 14:11:23 -070072 callback.onUserInfoChanged(mUserName, mUserDrawable, mUserAccount);
Jason Monkabe19742015-09-29 09:47:06 -040073 }
74
Jason Monk88529052016-11-04 13:29:58 -040075 public void removeCallback(OnUserInfoChangedListener callback) {
Jason Monkabe19742015-09-29 09:47:06 -040076 mCallbacks.remove(callback);
Jorim Jaggi3d878be2014-05-10 03:22:32 +020077 }
78
79 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
80 @Override
81 public void onReceive(Context context, Intent intent) {
82 final String action = intent.getAction();
83 if (Intent.ACTION_USER_SWITCHED.equals(action)) {
84 reloadUserInfo();
Jorim Jaggi3d878be2014-05-10 03:22:32 +020085 }
86 }
87 };
88
89 private final BroadcastReceiver mProfileReceiver = new BroadcastReceiver() {
90 @Override
91 public void onReceive(Context context, Intent intent) {
92 final String action = intent.getAction();
93 if (ContactsContract.Intents.ACTION_PROFILE_CHANGED.equals(action) ||
94 Intent.ACTION_USER_INFO_CHANGED.equals(action)) {
95 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -080096 final int currentUser = ActivityManager.getService().getCurrentUser().id;
Jorim Jaggi3d878be2014-05-10 03:22:32 +020097 final int changedUser =
98 intent.getIntExtra(Intent.EXTRA_USER_HANDLE, getSendingUserId());
99 if (changedUser == currentUser) {
100 reloadUserInfo();
101 }
102 } catch (RemoteException e) {
103 Log.e(TAG, "Couldn't get current user id for profile change", e);
104 }
105 }
106 }
107 };
108
109 public void reloadUserInfo() {
110 if (mUserInfoTask != null) {
111 mUserInfoTask.cancel(false);
112 mUserInfoTask = null;
113 }
114 queryForUserInformation();
115 }
116
Jorim Jaggi3d878be2014-05-10 03:22:32 +0200117 private void queryForUserInformation() {
118 Context currentUserContext;
119 UserInfo userInfo;
120 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800121 userInfo = ActivityManager.getService().getCurrentUser();
Jorim Jaggi3d878be2014-05-10 03:22:32 +0200122 currentUserContext = mContext.createPackageContextAsUser("android", 0,
123 new UserHandle(userInfo.id));
124 } catch (PackageManager.NameNotFoundException e) {
125 Log.e(TAG, "Couldn't create user context", e);
126 throw new RuntimeException(e);
127 } catch (RemoteException e) {
128 Log.e(TAG, "Couldn't get user info", e);
129 throw new RuntimeException(e);
130 }
131 final int userId = userInfo.id;
Alexandra Gherghina64d4dca2014-08-28 18:26:56 +0100132 final boolean isGuest = userInfo.isGuest();
Jorim Jaggi3d878be2014-05-10 03:22:32 +0200133 final String userName = userInfo.name;
Adrian Roos2b154a92014-11-17 15:18:39 +0100134
135 final Resources res = mContext.getResources();
136 final int avatarSize = Math.max(
137 res.getDimensionPixelSize(R.dimen.multi_user_avatar_expanded_size),
138 res.getDimensionPixelSize(R.dimen.multi_user_avatar_keyguard_size));
Jorim Jaggi3d878be2014-05-10 03:22:32 +0200139
140 final Context context = currentUserContext;
Jiaquan Hee1206372016-05-03 14:11:23 -0700141 mUserInfoTask = new AsyncTask<Void, Void, UserInfoQueryResult>() {
142
Jorim Jaggi3d878be2014-05-10 03:22:32 +0200143 @Override
Jiaquan Hee1206372016-05-03 14:11:23 -0700144 protected UserInfoQueryResult doInBackground(Void... params) {
Jorim Jaggi3d878be2014-05-10 03:22:32 +0200145 final UserManager um = UserManager.get(mContext);
146
147 // Fall back to the UserManager nickname if we can't read the name from the local
148 // profile below.
149 String name = userName;
150 Drawable avatar = null;
151 Bitmap rawAvatar = um.getUserIcon(userId);
152 if (rawAvatar != null) {
Evan Roskyaa7f51f2016-03-16 13:15:53 -0700153 avatar = new UserIconDrawable(avatarSize)
154 .setIcon(rawAvatar).setBadgeIfManagedUser(mContext, userId).bake();
Jorim Jaggi3d878be2014-05-10 03:22:32 +0200155 } else {
Alexandra Gherghina64d4dca2014-08-28 18:26:56 +0100156 avatar = UserIcons.getDefaultUserIcon(isGuest? UserHandle.USER_NULL : userId,
157 /* light= */ true);
Jorim Jaggi3d878be2014-05-10 03:22:32 +0200158 }
159
160 // If it's a single-user device, get the profile name, since the nickname is not
161 // usually valid
162 if (um.getUsers().size() <= 1) {
163 // Try and read the display name from the local profile
164 final Cursor cursor = context.getContentResolver().query(
165 ContactsContract.Profile.CONTENT_URI, new String[] {
Adrian Roos2b154a92014-11-17 15:18:39 +0100166 ContactsContract.CommonDataKinds.Phone._ID,
167 ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
168 }, null, null, null);
Jorim Jaggi3d878be2014-05-10 03:22:32 +0200169 if (cursor != null) {
170 try {
171 if (cursor.moveToFirst()) {
172 name = cursor.getString(cursor.getColumnIndex(
173 ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
174 }
175 } finally {
176 cursor.close();
177 }
178 }
179 }
Jiaquan Hee1206372016-05-03 14:11:23 -0700180 String userAccount = um.getUserAccount(userId);
181 return new UserInfoQueryResult(name, avatar, userAccount);
Jorim Jaggi3d878be2014-05-10 03:22:32 +0200182 }
183
184 @Override
Jiaquan Hee1206372016-05-03 14:11:23 -0700185 protected void onPostExecute(UserInfoQueryResult result) {
186 mUserName = result.getName();
187 mUserDrawable = result.getAvatar();
188 mUserAccount = result.getUserAccount();
Jorim Jaggi3d878be2014-05-10 03:22:32 +0200189 mUserInfoTask = null;
190 notifyChanged();
191 }
192 };
193 mUserInfoTask.execute();
194 }
195
196 private void notifyChanged() {
197 for (OnUserInfoChangedListener listener : mCallbacks) {
Jiaquan Hee1206372016-05-03 14:11:23 -0700198 listener.onUserInfoChanged(mUserName, mUserDrawable, mUserAccount);
Jorim Jaggi3d878be2014-05-10 03:22:32 +0200199 }
200 }
201
Adrian Roosd390b892016-05-05 10:50:49 -0400202 public void onDensityOrFontScaleChanged() {
203 reloadUserInfo();
204 }
205
Jorim Jaggi3d878be2014-05-10 03:22:32 +0200206 public interface OnUserInfoChangedListener {
Jiaquan Hee1206372016-05-03 14:11:23 -0700207 public void onUserInfoChanged(String name, Drawable picture, String userAccount);
208 }
209
210 private static class UserInfoQueryResult {
211 private String mName;
212 private Drawable mAvatar;
213 private String mUserAccount;
214
215 public UserInfoQueryResult(String name, Drawable avatar, String userAccount) {
216 mName = name;
217 mAvatar = avatar;
218 mUserAccount = userAccount;
219 }
220
221 public String getName() {
222 return mName;
223 }
224
225 public Drawable getAvatar() {
226 return mAvatar;
227 }
228
229 public String getUserAccount() {
230 return mUserAccount;
231 }
Jorim Jaggi3d878be2014-05-10 03:22:32 +0200232 }
233}