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