UserTile.java revision 383db5ebcc3a4a615faf249bf4f126f42e80b82e
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 */
16package com.android.systemui.qs.tiles;
17
18import android.content.Context;
19import android.content.Intent;
20import android.graphics.drawable.Drawable;
21import android.provider.Settings;
22import android.util.Pair;
23
24import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
25import com.android.systemui.plugins.qs.QSContainer.DetailAdapter;
26import com.android.systemui.qs.QSTile;
27import com.android.systemui.statusbar.policy.UserInfoController;
28import com.android.systemui.statusbar.policy.UserSwitcherController;
29
30public class UserTile extends QSTile<QSTile.State> implements UserInfoController.OnUserInfoChangedListener {
31
32    private final UserSwitcherController mUserSwitcherController;
33    private final UserInfoController mUserInfoController;
34    private Pair<String, Drawable> mLastUpdate;
35
36    public UserTile(Host host) {
37        super(host);
38        mUserSwitcherController = host.getUserSwitcherController();
39        mUserInfoController = host.getUserInfoController();
40    }
41
42    @Override
43    public State newTileState() {
44        return new QSTile.State();
45    }
46
47    @Override
48    public Intent getLongClickIntent() {
49        return new Intent(Settings.ACTION_USER_SETTINGS);
50    }
51
52    @Override
53    protected void handleClick() {
54        showDetail(true);
55    }
56
57    @Override
58    public DetailAdapter getDetailAdapter() {
59        return mUserSwitcherController.userDetailAdapter;
60    }
61
62    @Override
63    public int getMetricsCategory() {
64        return MetricsEvent.QS_USER_TILE;
65    }
66
67    @Override
68    public void setListening(boolean listening) {
69        if (listening) {
70            mUserInfoController.addListener(this);
71        } else {
72            mUserInfoController.remListener(this);
73        }
74    }
75
76    @Override
77    public CharSequence getTileLabel() {
78        return getState().label;
79    }
80
81    @Override
82    protected void handleUpdateState(State state, Object arg) {
83        final Pair<String, Drawable> p = arg != null ? (Pair<String, Drawable>) arg : mLastUpdate;
84        if (p != null) {
85            state.label = p.first;
86            // TODO: Better content description.
87            state.contentDescription = p.first;
88            state.icon = new Icon() {
89                @Override
90                public Drawable getDrawable(Context context) {
91                    return p.second;
92                }
93            };
94        } else {
95            // TODO: Default state.
96        }
97    }
98
99    @Override
100    public void onUserInfoChanged(String name, Drawable picture, String userAccount) {
101        mLastUpdate = new Pair<>(name, picture);
102        refreshState(mLastUpdate);
103    }
104}
105