1 /*
2  * Copyright (C) 2015 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.systemui.statusbar.car;
18 
19 import android.content.Context;
20 import android.os.UserHandle;
21 import android.util.AttributeSet;
22 import android.view.LayoutInflater;
23 import android.view.View;
24 import android.view.ViewGroup;
25 import android.widget.AdapterView;
26 import android.widget.GridView;
27 import android.widget.ImageView;
28 import android.widget.TextView;
29 
30 import com.android.systemui.R;
31 import com.android.systemui.statusbar.UserUtil;
32 import com.android.systemui.statusbar.phone.PhoneStatusBar;
33 import com.android.systemui.statusbar.policy.UserSwitcherController;
34 
35 public class UserGridView extends GridView {
36 
37     private PhoneStatusBar mStatusBar;
38     private UserSwitcherController mUserSwitcherController;
39     private Adapter mAdapter;
40     private int mPendingUserId = UserHandle.USER_NULL;
41 
UserGridView(Context context, AttributeSet attrs)42     public UserGridView(Context context, AttributeSet attrs) {
43         super(context, attrs);
44     }
45 
init(PhoneStatusBar statusBar, UserSwitcherController userSwitcherController)46     public void init(PhoneStatusBar statusBar, UserSwitcherController userSwitcherController) {
47         mStatusBar = statusBar;
48         mUserSwitcherController = userSwitcherController;
49         mAdapter = new Adapter(mUserSwitcherController);
50         setAdapter(mAdapter);
51 
52         setOnItemClickListener(new OnItemClickListener() {
53             @Override
54             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
55                 mPendingUserId = UserHandle.USER_NULL;
56                 UserSwitcherController.UserRecord record = mAdapter.getItem(position);
57                 if (record == null) {
58                     return;
59                 }
60 
61                 if (record.isGuest || record.isAddUser) {
62                     mUserSwitcherController.switchTo(record);
63                     return;
64                 }
65 
66                 if (record.isCurrent) {
67                     showOfflineAuthUi();
68                 } else {
69                     mPendingUserId = record.info.id;
70                     mUserSwitcherController.switchTo(record);
71                 }
72             }
73         });
74 
75         setOnItemLongClickListener(new OnItemLongClickListener() {
76             @Override
77             public boolean onItemLongClick(AdapterView<?> parent,
78                     View view, int position, long id) {
79                 UserSwitcherController.UserRecord record = mAdapter.getItem(position);
80                 if (record == null || record.isAddUser) {
81                     return false;
82                 }
83                 if (record.isGuest) {
84                     if (record.isCurrent) {
85                         mUserSwitcherController.switchTo(record);
86                     }
87                     return true;
88                 }
89 
90                 UserUtil.deleteUserWithPrompt(getContext(), record.info.id,
91                         mUserSwitcherController);
92                 return true;
93             }
94         });
95     }
96 
onUserSwitched(int newUserId)97     public void onUserSwitched(int newUserId) {
98         if (mPendingUserId == newUserId) {
99             // Bring up security view after user switch is completed.
100             post(new Runnable() {
101                 @Override
102                 public void run() {
103                     showOfflineAuthUi();
104                 }
105             });
106         }
107         mPendingUserId = UserHandle.USER_NULL;
108     }
109 
showOfflineAuthUi()110     private void showOfflineAuthUi() {
111         // TODO: Show keyguard UI in-place.
112         mStatusBar.executeRunnableDismissingKeyguard(null, null, true, true, true);
113     }
114 
115     @Override
onMeasure(int widthMeasureSpec, int heightMeasureSpec)116     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
117         int widthMode = MeasureSpec.getMode(widthMeasureSpec);
118         int widthSize = MeasureSpec.getSize(widthMeasureSpec);
119         if (widthMode == MeasureSpec.UNSPECIFIED) {
120             setNumColumns(AUTO_FIT);
121         } else {
122             int columnWidth = Math.max(1, getRequestedColumnWidth());
123             int itemCount = getAdapter() == null ? 0 : getAdapter().getCount();
124             int numColumns = Math.max(1, Math.min(itemCount, widthSize / columnWidth));
125             setNumColumns(numColumns);
126         }
127         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
128     }
129 
130     private final class Adapter extends UserSwitcherController.BaseUserAdapter {
Adapter(UserSwitcherController controller)131         public Adapter(UserSwitcherController controller) {
132             super(controller);
133         }
134 
135         @Override
getView(int position, View convertView, ViewGroup parent)136         public View getView(int position, View convertView, ViewGroup parent) {
137             if (convertView == null) {
138                 LayoutInflater inflater = (LayoutInflater)getContext().getSystemService
139                         (Context.LAYOUT_INFLATER_SERVICE);
140                 convertView = inflater.inflate(R.layout.car_fullscreen_user_pod, null);
141             }
142             UserSwitcherController.UserRecord record = getItem(position);
143 
144             TextView nameView = (TextView) convertView.findViewById(R.id.user_name);
145             if (record != null) {
146                 nameView.setText(getName(getContext(), record));
147                 convertView.setActivated(record.isCurrent);
148             } else {
149                 nameView.setText("Unknown");
150             }
151 
152             ImageView iconView = (ImageView) convertView.findViewById(R.id.user_avatar);
153             if (record == null || record.picture == null) {
154                 iconView.setImageDrawable(getDrawable(getContext(), record));
155             } else {
156                 iconView.setImageBitmap(record.picture);
157             }
158 
159             return convertView;
160         }
161     }
162 }
163