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