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.systemui.qs.tiles;
18 
19 import android.content.Context;
20 import android.content.Intent;
21 import android.util.AttributeSet;
22 import android.view.LayoutInflater;
23 import android.view.View;
24 import android.view.ViewGroup;
25 
26 import com.android.internal.logging.MetricsLogger;
27 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
28 import com.android.settingslib.RestrictedLockUtils;
29 import com.android.systemui.R;
30 import com.android.systemui.qs.PseudoGridView;
31 import com.android.systemui.statusbar.policy.UserSwitcherController;
32 /**
33  * Quick settings detail view for user switching.
34  */
35 public class UserDetailView extends PseudoGridView {
36 
37     protected Adapter mAdapter;
38 
UserDetailView(Context context, AttributeSet attrs)39     public UserDetailView(Context context, AttributeSet attrs) {
40         super(context, attrs);
41     }
42 
inflate(Context context, ViewGroup parent, boolean attach)43     public static UserDetailView inflate(Context context, ViewGroup parent, boolean attach) {
44         return (UserDetailView) LayoutInflater.from(context).inflate(
45                 R.layout.qs_user_detail, parent, attach);
46     }
47 
createAndSetAdapter(UserSwitcherController controller)48     public void createAndSetAdapter(UserSwitcherController controller) {
49         mAdapter = new Adapter(mContext, controller);
50         ViewGroupAdapterBridge.link(this, mAdapter);
51     }
52 
refreshAdapter()53     public void refreshAdapter() {
54         mAdapter.refresh();
55     }
56 
57     public static class Adapter extends UserSwitcherController.BaseUserAdapter
58             implements OnClickListener {
59 
60         private final Context mContext;
61         protected UserSwitcherController mController;
62 
Adapter(Context context, UserSwitcherController controller)63         public Adapter(Context context, UserSwitcherController controller) {
64             super(controller);
65             mContext = context;
66             mController = controller;
67         }
68 
69         @Override
getView(int position, View convertView, ViewGroup parent)70         public View getView(int position, View convertView, ViewGroup parent) {
71             UserSwitcherController.UserRecord item = getItem(position);
72             return createUserDetailItemView(convertView, parent, item);
73         }
74 
createUserDetailItemView(View convertView, ViewGroup parent, UserSwitcherController.UserRecord item)75         public UserDetailItemView createUserDetailItemView(View convertView, ViewGroup parent,
76                 UserSwitcherController.UserRecord item) {
77             UserDetailItemView v = UserDetailItemView.convertOrInflate(
78                     mContext, convertView, parent);
79             if (v != convertView) {
80                 v.setOnClickListener(this);
81             }
82             String name = getName(mContext, item);
83             if (item.picture == null) {
84                 v.bind(name, getDrawable(mContext, item), item.resolveId());
85             } else {
86                 v.bind(name, item.picture, item.info.id);
87             }
88             v.setActivated(item.isCurrent);
89             v.setDisabledByAdmin(item.isDisabledByAdmin);
90             if (!item.isSwitchToEnabled) {
91                 v.setEnabled(false);
92             }
93             v.setTag(item);
94             return v;
95         }
96 
97         @Override
onClick(View view)98         public void onClick(View view) {
99             UserSwitcherController.UserRecord tag =
100                     (UserSwitcherController.UserRecord) view.getTag();
101             if (tag.isDisabledByAdmin) {
102                 final Intent intent = RestrictedLockUtils.getShowAdminSupportDetailsIntent(
103                         mContext, tag.enforcedAdmin);
104                 mController.startActivity(intent);
105             } else if (tag.isSwitchToEnabled) {
106                 MetricsLogger.action(mContext, MetricsEvent.QS_SWITCH_USER);
107                 switchTo(tag);
108             }
109         }
110     }
111 }
112