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.content.Context;
20import android.content.pm.UserInfo;
21import android.content.res.Resources;
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.SpinnerAdapter;
32import android.widget.TextView;
33
34import com.android.internal.util.UserIcons;
35
36import java.util.ArrayList;
37
38/**
39 * Adapter for a spinner that shows a list of users.
40 */
41public 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
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
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
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
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
95    private View createUser(ViewGroup parent) {
96        return mInflater.inflate(R.layout.user_preference, parent, false);
97    }
98
99    @Override
100    public void registerDataSetObserver(DataSetObserver observer) {
101        // We don't support observers
102    }
103
104    @Override
105    public void unregisterDataSetObserver(DataSetObserver observer) {
106        // We don't support observers
107    }
108
109    @Override
110    public int getCount() {
111        return data.size();
112    }
113
114    @Override
115    public UserDetails getItem(int position) {
116        return data.get(position);
117    }
118
119    @Override
120    public long getItemId(int position) {
121        return data.get(position).mUserHandle.getIdentifier();
122    }
123
124    @Override
125    public boolean hasStableIds() {
126        return false;
127    }
128
129    @Override
130    public View getView(int position, View convertView, ViewGroup parent) {
131        return getDropDownView(position, convertView, parent);
132    }
133
134    @Override
135    public int getItemViewType(int position) {
136        return 0;
137    }
138
139    @Override
140    public int getViewTypeCount() {
141        return 1;
142    }
143
144    @Override
145    public boolean isEmpty() {
146        return data.isEmpty();
147    }
148}