• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 package com.android.settings.dashboard.profileselector;
18 
19 import android.app.ActivityManager;
20 import android.content.Context;
21 import android.content.pm.UserInfo;
22 import android.database.DataSetObserver;
23 import android.graphics.drawable.BitmapDrawable;
24 import android.graphics.drawable.Drawable;
25 import android.os.UserHandle;
26 import android.os.UserManager;
27 import android.view.LayoutInflater;
28 import android.view.View;
29 import android.view.ViewGroup;
30 import android.widget.ImageView;
31 import android.widget.ListAdapter;
32 import android.widget.SpinnerAdapter;
33 import android.widget.TextView;
34 
35 import com.android.internal.util.UserIcons;
36 import com.android.settingslib.R;
37 import com.android.settingslib.drawable.UserIconDrawable;
38 
39 import java.util.ArrayList;
40 import java.util.List;
41 
42 /**
43  * Adapter for a spinner that shows a list of users.
44  */
45 public class UserAdapter implements SpinnerAdapter, ListAdapter {
46     /** Holder for user details */
47     public static class UserDetails {
48         private final UserHandle mUserHandle;
49         private final String mName;
50         private final Drawable mIcon;
51 
UserDetails(UserHandle userHandle, UserManager um, Context context)52         public UserDetails(UserHandle userHandle, UserManager um, Context context) {
53             mUserHandle = userHandle;
54             UserInfo userInfo = um.getUserInfo(mUserHandle.getIdentifier());
55             Drawable icon;
56             if (userInfo.isManagedProfile()) {
57                 mName = context.getString(R.string.managed_user_title);
58                 icon = context.getDrawable(
59                         com.android.internal.R.drawable.ic_corp_badge);
60             } else {
61                 mName = userInfo.name;
62                 final int userId = userInfo.id;
63                 if (um.getUserIcon(userId) != null) {
64                     icon = new BitmapDrawable(context.getResources(), um.getUserIcon(userId));
65                 } else {
66                     icon = UserIcons.getDefaultUserIcon(
67                             context.getResources(), userId, /* light= */ false);
68                 }
69             }
70             this.mIcon = encircle(context, icon);
71         }
72 
encircle(Context context, Drawable icon)73         private static Drawable encircle(Context context, Drawable icon) {
74             return new UserIconDrawable(UserIconDrawable.getSizeForList(context))
75                     .setIconDrawable(icon).bake();
76         }
77     }
78 
79     private ArrayList<UserDetails> data;
80     private final LayoutInflater mInflater;
81 
UserAdapter(Context context, ArrayList<UserDetails> users)82     public UserAdapter(Context context, ArrayList<UserDetails> users) {
83         if (users == null) {
84             throw new IllegalArgumentException("A list of user details must be provided");
85         }
86         this.data = users;
87         mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
88     }
89 
getUserHandle(int position)90     public UserHandle getUserHandle(int position) {
91         if (position < 0 || position >= data.size()) {
92             return null;
93         }
94         return data.get(position).mUserHandle;
95     }
96 
97     @Override
getDropDownView(int position, View convertView, ViewGroup parent)98     public View getDropDownView(int position, View convertView, ViewGroup parent) {
99         final View row = convertView != null ? convertView : createUser(parent);
100 
101         UserDetails user = data.get(position);
102         ((ImageView) row.findViewById(android.R.id.icon)).setImageDrawable(user.mIcon);
103         ((TextView) row.findViewById(android.R.id.title)).setText(getTitle(user));
104         return row;
105     }
106 
getTitle(UserDetails user)107     private int getTitle(UserDetails user) {
108         int userHandle = user.mUserHandle.getIdentifier();
109         if (userHandle == UserHandle.USER_CURRENT
110                 || userHandle == ActivityManager.getCurrentUser()) {
111             return R.string.category_personal;
112         } else {
113             return R.string.category_work;
114         }
115     }
116 
createUser(ViewGroup parent)117     private View createUser(ViewGroup parent) {
118         return mInflater.inflate(R.layout.user_preference, parent, false);
119     }
120 
121     @Override
registerDataSetObserver(DataSetObserver observer)122     public void registerDataSetObserver(DataSetObserver observer) {
123         // We don't support observers
124     }
125 
126     @Override
unregisterDataSetObserver(DataSetObserver observer)127     public void unregisterDataSetObserver(DataSetObserver observer) {
128         // We don't support observers
129     }
130 
131     @Override
getCount()132     public int getCount() {
133         return data.size();
134     }
135 
136     @Override
getItem(int position)137     public UserAdapter.UserDetails getItem(int position) {
138         return data.get(position);
139     }
140 
141     @Override
getItemId(int position)142     public long getItemId(int position) {
143         return data.get(position).mUserHandle.getIdentifier();
144     }
145 
146     @Override
hasStableIds()147     public boolean hasStableIds() {
148         return false;
149     }
150 
151     @Override
getView(int position, View convertView, ViewGroup parent)152     public View getView(int position, View convertView, ViewGroup parent) {
153         return getDropDownView(position, convertView, parent);
154     }
155 
156     @Override
getItemViewType(int position)157     public int getItemViewType(int position) {
158         return 0;
159     }
160 
161     @Override
getViewTypeCount()162     public int getViewTypeCount() {
163         return 1;
164     }
165 
166     @Override
isEmpty()167     public boolean isEmpty() {
168         return data.isEmpty();
169     }
170 
171     @Override
areAllItemsEnabled()172     public boolean areAllItemsEnabled() {
173         return true;
174     }
175 
176     @Override
isEnabled(int position)177     public boolean isEnabled(int position) {
178         return true;
179     }
180 
181     /**
182      * Creates a {@link UserAdapter} if there is more than one
183      * profile on the device.
184      *
185      * <p> The adapter can be used to populate a spinner that switches between the Settings
186      * app on the different profiles.
187      *
188      * @return a {@link UserAdapter} or null if there is only one
189      * profile.
190      */
createUserSpinnerAdapter(UserManager userManager, Context context)191     public static UserAdapter createUserSpinnerAdapter(UserManager userManager, Context context) {
192         List<UserHandle> userProfiles = userManager.getUserProfiles();
193         if (userProfiles.size() < 2) {
194             return null;
195         }
196 
197         UserHandle myUserHandle = new UserHandle(UserHandle.myUserId());
198         // The first option should be the current profile
199         userProfiles.remove(myUserHandle);
200         userProfiles.add(0, myUserHandle);
201 
202         return createUserAdapter(userManager, context, userProfiles);
203     }
204 
createUserAdapter( UserManager userManager, Context context, List<UserHandle> userProfiles)205     public static UserAdapter createUserAdapter(
206             UserManager userManager, Context context, List<UserHandle> userProfiles) {
207         ArrayList<UserDetails> userDetails = new ArrayList<>(userProfiles.size());
208         final int count = userProfiles.size();
209         for (int i = 0; i < count; i++) {
210             userDetails.add(new UserDetails(userProfiles.get(i), userManager, context));
211         }
212         return new UserAdapter(context, userDetails);
213     }
214 }
215