KeyguardUserSwitcher.java revision 4538027dedbe7ebfc884ca35d20522a2a21a42d4
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 17package com.android.systemui.statusbar.policy; 18 19import android.content.Context; 20import android.database.DataSetObserver; 21import android.provider.Settings; 22import android.view.LayoutInflater; 23import android.view.View; 24import android.view.ViewGroup; 25import android.view.ViewStub; 26import android.widget.TextView; 27 28import com.android.systemui.R; 29import com.android.systemui.statusbar.phone.KeyguardStatusBarView; 30import com.android.systemui.statusbar.phone.UserAvatarView; 31 32/** 33 * Manages the user switcher on the Keyguard. 34 */ 35public class KeyguardUserSwitcher { 36 37 private static final String TAG = "KeyguardUserSwitcher"; 38 private static final boolean ALWAYS_ON = false; 39 private static final String SIMPLE_USER_SWITCHER_GLOBAL_SETTING = 40 "lockscreenSimpleUserSwitcher"; 41 42 private final ViewGroup mUserSwitcher; 43 private final KeyguardStatusBarView mStatusBarView; 44 private final Adapter mAdapter; 45 private final boolean mSimpleUserSwitcher; 46 47 public KeyguardUserSwitcher(Context context, ViewStub userSwitcher, 48 KeyguardStatusBarView statusBarView, UserSwitcherController userSwitcherController) { 49 if (context.getResources().getBoolean(R.bool.config_keyguardUserSwitcher) || ALWAYS_ON) { 50 mUserSwitcher = (ViewGroup) userSwitcher.inflate(); 51 mStatusBarView = statusBarView; 52 mStatusBarView.setKeyguardUserSwitcher(this); 53 mAdapter = new Adapter(context, userSwitcherController); 54 mAdapter.registerDataSetObserver(mDataSetObserver); 55 mSimpleUserSwitcher = Settings.Global.getInt(context.getContentResolver(), 56 SIMPLE_USER_SWITCHER_GLOBAL_SETTING, 0) != 0; 57 } else { 58 mUserSwitcher = null; 59 mStatusBarView = null; 60 mAdapter = null; 61 mSimpleUserSwitcher = false; 62 } 63 } 64 65 public void setKeyguard(boolean keyguard) { 66 if (mUserSwitcher != null) { 67 if (keyguard && shouldExpandByDefault()) { 68 show(); 69 } else { 70 hide(); 71 } 72 } 73 } 74 75 /** 76 * @return true if the user switcher should be expanded by default on the lock screen. 77 * @see android.os.UserManager#isUserSwitcherEnabled() 78 */ 79 private boolean shouldExpandByDefault() { 80 return mSimpleUserSwitcher || mAdapter.getSwitchableUsers() > 1; 81 } 82 83 public void show() { 84 if (mUserSwitcher != null) { 85 // TODO: animate 86 mUserSwitcher.setVisibility(View.VISIBLE); 87 mStatusBarView.setKeyguardUserSwitcherShowing(true); 88 } 89 } 90 91 public void hide() { 92 if (mUserSwitcher != null) { 93 // TODO: animate 94 mUserSwitcher.setVisibility(View.GONE); 95 mStatusBarView.setKeyguardUserSwitcherShowing(false); 96 } 97 } 98 99 private void refresh() { 100 final int childCount = mUserSwitcher.getChildCount(); 101 final int adapterCount = mAdapter.getCount(); 102 final int N = Math.max(childCount, adapterCount); 103 for (int i = 0; i < N; i++) { 104 if (i < adapterCount) { 105 View oldView = null; 106 if (i < childCount) { 107 oldView = mUserSwitcher.getChildAt(i); 108 } 109 View newView = mAdapter.getView(i, oldView, mUserSwitcher); 110 if (oldView == null) { 111 // We ran out of existing views. Add it at the end. 112 mUserSwitcher.addView(newView); 113 } else if (oldView != newView) { 114 // We couldn't rebind the view. Replace it. 115 mUserSwitcher.removeViewAt(i); 116 mUserSwitcher.addView(newView, i); 117 } 118 } else { 119 int lastIndex = mUserSwitcher.getChildCount() - 1; 120 mUserSwitcher.removeViewAt(lastIndex); 121 } 122 } 123 } 124 125 public final DataSetObserver mDataSetObserver = new DataSetObserver() { 126 @Override 127 public void onChanged() { 128 refresh(); 129 } 130 }; 131 132 public static class Adapter extends UserSwitcherController.BaseUserAdapter implements 133 View.OnClickListener { 134 135 private Context mContext; 136 137 public Adapter(Context context, UserSwitcherController controller) { 138 super(controller); 139 mContext = context; 140 } 141 142 @Override 143 public View getView(int position, View convertView, ViewGroup parent) { 144 UserSwitcherController.UserRecord item = getItem(position); 145 146 if (convertView == null 147 || !(convertView.getTag() instanceof UserSwitcherController.UserRecord)) { 148 convertView = LayoutInflater.from(mContext).inflate( 149 R.layout.keyguard_user_switcher_item, parent, false); 150 convertView.setOnClickListener(this); 151 } 152 153 TextView nameView = (TextView) convertView.findViewById(R.id.name); 154 UserAvatarView pictureView = (UserAvatarView) convertView.findViewById(R.id.picture); 155 156 nameView.setText(getName(mContext, item)); 157 if (item.picture == null) { 158 pictureView.setDrawable(mContext.getDrawable(R.drawable.ic_account_circle_qs)); 159 } else { 160 pictureView.setBitmap(item.picture); 161 } 162 convertView.setActivated(item.isCurrent); 163 convertView.setTag(item); 164 return convertView; 165 } 166 167 @Override 168 public void onClick(View v) { 169 switchTo(((UserSwitcherController.UserRecord)v.getTag())); 170 } 171 } 172} 173