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