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