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