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; 18 19 import android.content.Context; 20 import android.content.pm.UserInfo; 21 import android.content.res.Resources; 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.SpinnerAdapter; 32 import android.widget.TextView; 33 34 import com.android.internal.util.UserIcons; 35 36 import java.util.ArrayList; 37 38 /** 39 * Adapter for a spinner that shows a list of users. 40 */ 41 public class UserSpinnerAdapter implements SpinnerAdapter { 42 // TODO: Update UI. See: http://b/16518801 43 /** Holder for user details */ 44 public static class UserDetails { 45 private final UserHandle mUserHandle; 46 private final String name; 47 private final Drawable icon; 48 UserDetails(UserHandle userHandle, UserManager um, Context context)49 public UserDetails(UserHandle userHandle, UserManager um, Context context) { 50 mUserHandle = userHandle; 51 UserInfo userInfo = um.getUserInfo(mUserHandle.getIdentifier()); 52 if (userInfo.isManagedProfile()) { 53 name = context.getString(R.string.managed_user_title); 54 icon = Resources.getSystem().getDrawable( 55 com.android.internal.R.drawable.ic_corp_icon); 56 } else { 57 name = userInfo.name; 58 final int userId = userInfo.id; 59 if (um.getUserIcon(userId) != null) { 60 icon = new BitmapDrawable(context.getResources(), um.getUserIcon(userId)); 61 } else { 62 icon = UserIcons.getDefaultUserIcon(userId, /* light= */ false); 63 } 64 } 65 } 66 } 67 private ArrayList<UserDetails> data; 68 private final LayoutInflater mInflater; 69 UserSpinnerAdapter(Context context, ArrayList<UserDetails> users)70 public UserSpinnerAdapter(Context context, ArrayList<UserDetails> users) { 71 if (users == null) { 72 throw new IllegalArgumentException("A list of user details must be provided"); 73 } 74 this.data = users; 75 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 76 } 77 getUserHandle(int position)78 public UserHandle getUserHandle(int position) { 79 if (position < 0 || position >= data.size()) { 80 return null; 81 } 82 return data.get(position).mUserHandle; 83 } 84 85 @Override getDropDownView(int position, View convertView, ViewGroup parent)86 public View getDropDownView(int position, View convertView, ViewGroup parent) { 87 final View row = convertView != null ? convertView : createUser(parent); 88 89 UserDetails user = data.get(position); 90 ((ImageView) row.findViewById(android.R.id.icon)).setImageDrawable(user.icon); 91 ((TextView) row.findViewById(android.R.id.title)).setText(user.name); 92 return row; 93 } 94 createUser(ViewGroup parent)95 private View createUser(ViewGroup parent) { 96 return mInflater.inflate(R.layout.user_preference, parent, false); 97 } 98 99 @Override registerDataSetObserver(DataSetObserver observer)100 public void registerDataSetObserver(DataSetObserver observer) { 101 // We don't support observers 102 } 103 104 @Override unregisterDataSetObserver(DataSetObserver observer)105 public void unregisterDataSetObserver(DataSetObserver observer) { 106 // We don't support observers 107 } 108 109 @Override getCount()110 public int getCount() { 111 return data.size(); 112 } 113 114 @Override getItem(int position)115 public UserDetails getItem(int position) { 116 return data.get(position); 117 } 118 119 @Override getItemId(int position)120 public long getItemId(int position) { 121 return data.get(position).mUserHandle.getIdentifier(); 122 } 123 124 @Override hasStableIds()125 public boolean hasStableIds() { 126 return false; 127 } 128 129 @Override getView(int position, View convertView, ViewGroup parent)130 public View getView(int position, View convertView, ViewGroup parent) { 131 return getDropDownView(position, convertView, parent); 132 } 133 134 @Override getItemViewType(int position)135 public int getItemViewType(int position) { 136 return 0; 137 } 138 139 @Override getViewTypeCount()140 public int getViewTypeCount() { 141 return 1; 142 } 143 144 @Override isEmpty()145 public boolean isEmpty() { 146 return data.isEmpty(); 147 } 148 }