Jim Miller | 0b72824 | 2012-10-28 19:42:30 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | |
Jim Miller | 5ecd811 | 2013-01-09 18:50:26 -0800 | [diff] [blame] | 17 | package com.android.keyguard; |
Jim Miller | 0b72824 | 2012-10-28 19:42:30 -0700 | [diff] [blame] | 18 | |
Chris Wren | c345146 | 2012-10-30 11:22:58 -0400 | [diff] [blame] | 19 | import android.animation.Animator; |
| 20 | import android.animation.AnimatorListenerAdapter; |
| 21 | import android.animation.ObjectAnimator; |
Jim Miller | 0b72824 | 2012-10-28 19:42:30 -0700 | [diff] [blame] | 22 | import android.content.ContentResolver; |
| 23 | import android.content.Context; |
Daniel Sandler | fe0e1e4 | 2012-11-29 15:11:50 -0500 | [diff] [blame] | 24 | import android.os.BatteryManager; |
Jim Miller | 0b72824 | 2012-10-28 19:42:30 -0700 | [diff] [blame] | 25 | import android.os.Handler; |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 26 | import android.os.IBinder; |
Jim Miller | 0b72824 | 2012-10-28 19:42:30 -0700 | [diff] [blame] | 27 | import android.os.Looper; |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 28 | import android.os.RemoteException; |
| 29 | import android.os.ServiceManager; |
Alan Viverette | b428b0f | 2013-02-07 12:19:15 -0800 | [diff] [blame] | 30 | import android.os.SystemClock; |
Jim Miller | 0b72824 | 2012-10-28 19:42:30 -0700 | [diff] [blame] | 31 | import android.os.UserHandle; |
| 32 | import android.provider.Settings; |
| 33 | import android.text.TextUtils; |
| 34 | import android.util.AttributeSet; |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 35 | import android.util.Slog; |
Jim Miller | 0b72824 | 2012-10-28 19:42:30 -0700 | [diff] [blame] | 36 | import android.view.View; |
| 37 | import android.widget.TextView; |
| 38 | |
| 39 | import libcore.util.MutableInt; |
| 40 | |
Alan Viverette | b428b0f | 2013-02-07 12:19:15 -0800 | [diff] [blame] | 41 | import java.lang.ref.WeakReference; |
| 42 | |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 43 | import com.android.internal.widget.LockPatternUtils; |
Jim Miller | 0b72824 | 2012-10-28 19:42:30 -0700 | [diff] [blame] | 44 | |
| 45 | /*** |
| 46 | * Manages a number of views inside of the given layout. See below for a list of widgets. |
| 47 | */ |
| 48 | class KeyguardMessageArea extends TextView { |
Alan Viverette | b428b0f | 2013-02-07 12:19:15 -0800 | [diff] [blame] | 49 | /** Handler token posted with accessibility announcement runnables. */ |
| 50 | private static final Object ANNOUNCE_TOKEN = new Object(); |
| 51 | |
| 52 | /** |
| 53 | * Delay before speaking an accessibility announcement. Used to prevent |
| 54 | * lift-to-type from interrupting itself. |
| 55 | */ |
| 56 | private static final long ANNOUNCEMENT_DELAY = 250; |
| 57 | |
Jim Miller | 0b72824 | 2012-10-28 19:42:30 -0700 | [diff] [blame] | 58 | static final int CHARGING_ICON = 0; //R.drawable.ic_lock_idle_charging; |
| 59 | static final int BATTERY_LOW_ICON = 0; //R.drawable.ic_lock_idle_low_battery; |
| 60 | |
| 61 | static final int SECURITY_MESSAGE_DURATION = 5000; |
Chris Wren | c345146 | 2012-10-30 11:22:58 -0400 | [diff] [blame] | 62 | protected static final int FADE_DURATION = 750; |
Jim Miller | 0b72824 | 2012-10-28 19:42:30 -0700 | [diff] [blame] | 63 | |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 64 | private static final String TAG = "KeyguardMessageArea"; |
| 65 | |
Jim Miller | 0b72824 | 2012-10-28 19:42:30 -0700 | [diff] [blame] | 66 | // are we showing battery information? |
| 67 | boolean mShowingBatteryInfo = false; |
| 68 | |
Chris Wren | c345146 | 2012-10-30 11:22:58 -0400 | [diff] [blame] | 69 | // is the bouncer up? |
| 70 | boolean mShowingBouncer = false; |
| 71 | |
Jim Miller | 0b72824 | 2012-10-28 19:42:30 -0700 | [diff] [blame] | 72 | // last known plugged in state |
Daniel Sandler | fe0e1e4 | 2012-11-29 15:11:50 -0500 | [diff] [blame] | 73 | boolean mCharging = false; |
Jim Miller | 0b72824 | 2012-10-28 19:42:30 -0700 | [diff] [blame] | 74 | |
| 75 | // last known battery level |
| 76 | int mBatteryLevel = 100; |
| 77 | |
| 78 | KeyguardUpdateMonitor mUpdateMonitor; |
| 79 | |
| 80 | // Timeout before we reset the message to show charging/owner info |
| 81 | long mTimeout = SECURITY_MESSAGE_DURATION; |
| 82 | |
| 83 | // Shadowed text values |
| 84 | protected boolean mBatteryCharged; |
| 85 | protected boolean mBatteryIsLow; |
| 86 | |
| 87 | private Handler mHandler; |
| 88 | |
| 89 | CharSequence mMessage; |
| 90 | boolean mShowingMessage; |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 91 | private CharSequence mSeparator; |
| 92 | private LockPatternUtils mLockPatternUtils; |
| 93 | |
Jim Miller | 0b72824 | 2012-10-28 19:42:30 -0700 | [diff] [blame] | 94 | Runnable mClearMessageRunnable = new Runnable() { |
| 95 | @Override |
| 96 | public void run() { |
| 97 | mMessage = null; |
| 98 | mShowingMessage = false; |
Chris Wren | c345146 | 2012-10-30 11:22:58 -0400 | [diff] [blame] | 99 | if (mShowingBouncer) { |
| 100 | hideMessage(FADE_DURATION, true); |
| 101 | } else { |
| 102 | update(); |
| 103 | } |
Jim Miller | 0b72824 | 2012-10-28 19:42:30 -0700 | [diff] [blame] | 104 | } |
| 105 | }; |
| 106 | |
| 107 | public static class Helper implements SecurityMessageDisplay { |
| 108 | KeyguardMessageArea mMessageArea; |
| 109 | Helper(View v) { |
| 110 | mMessageArea = (KeyguardMessageArea) v.findViewById(R.id.keyguard_message_area); |
| 111 | if (mMessageArea == null) { |
| 112 | throw new RuntimeException("Can't find keyguard_message_area in " + v.getClass()); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | public void setMessage(CharSequence msg, boolean important) { |
| 117 | if (!TextUtils.isEmpty(msg) && important) { |
| 118 | mMessageArea.mMessage = msg; |
| 119 | mMessageArea.securityMessageChanged(); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | public void setMessage(int resId, boolean important) { |
| 124 | if (resId != 0 && important) { |
| 125 | mMessageArea.mMessage = mMessageArea.getContext().getResources().getText(resId); |
| 126 | mMessageArea.securityMessageChanged(); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | public void setMessage(int resId, boolean important, Object... formatArgs) { |
| 131 | if (resId != 0 && important) { |
| 132 | mMessageArea.mMessage = mMessageArea.getContext().getString(resId, formatArgs); |
| 133 | mMessageArea.securityMessageChanged(); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | @Override |
Chris Wren | c345146 | 2012-10-30 11:22:58 -0400 | [diff] [blame] | 138 | public void showBouncer(int duration) { |
| 139 | mMessageArea.hideMessage(duration, false); |
| 140 | mMessageArea.mShowingBouncer = true; |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public void hideBouncer(int duration) { |
| 145 | mMessageArea.showMessage(duration); |
| 146 | mMessageArea.mShowingBouncer = false; |
| 147 | } |
| 148 | |
| 149 | @Override |
Jim Miller | 0b72824 | 2012-10-28 19:42:30 -0700 | [diff] [blame] | 150 | public void setTimeout(int timeoutMs) { |
| 151 | mMessageArea.mTimeout = timeoutMs; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | private KeyguardUpdateMonitorCallback mInfoCallback = new KeyguardUpdateMonitorCallback() { |
| 156 | @Override |
| 157 | public void onRefreshBatteryInfo(KeyguardUpdateMonitor.BatteryStatus status) { |
| 158 | mShowingBatteryInfo = status.isPluggedIn() || status.isBatteryLow(); |
Daniel Sandler | fe0e1e4 | 2012-11-29 15:11:50 -0500 | [diff] [blame] | 159 | mCharging = status.status == BatteryManager.BATTERY_STATUS_CHARGING |
| 160 | || status.status == BatteryManager.BATTERY_STATUS_FULL; |
Jim Miller | 0b72824 | 2012-10-28 19:42:30 -0700 | [diff] [blame] | 161 | mBatteryLevel = status.level; |
| 162 | mBatteryCharged = status.isCharged(); |
| 163 | mBatteryIsLow = status.isBatteryLow(); |
| 164 | update(); |
| 165 | } |
| 166 | }; |
| 167 | |
| 168 | public KeyguardMessageArea(Context context) { |
| 169 | this(context, null); |
| 170 | } |
| 171 | |
| 172 | public KeyguardMessageArea(Context context, AttributeSet attrs) { |
| 173 | super(context, attrs); |
| 174 | |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 175 | mLockPatternUtils = new LockPatternUtils(context); |
| 176 | |
Jim Miller | 0b72824 | 2012-10-28 19:42:30 -0700 | [diff] [blame] | 177 | // This is required to ensure marquee works |
| 178 | setSelected(true); |
| 179 | |
| 180 | // Registering this callback immediately updates the battery state, among other things. |
| 181 | mUpdateMonitor = KeyguardUpdateMonitor.getInstance(getContext()); |
| 182 | mUpdateMonitor.registerCallback(mInfoCallback); |
| 183 | mHandler = new Handler(Looper.myLooper()); |
| 184 | |
Jim Miller | e9be140 | 2012-11-01 16:14:20 -0700 | [diff] [blame] | 185 | mSeparator = getResources().getString(R.string.kg_text_message_separator); |
| 186 | |
Jim Miller | 0b72824 | 2012-10-28 19:42:30 -0700 | [diff] [blame] | 187 | update(); |
| 188 | } |
| 189 | |
| 190 | public void securityMessageChanged() { |
Chris Wren | c345146 | 2012-10-30 11:22:58 -0400 | [diff] [blame] | 191 | setAlpha(1f); |
Jim Miller | 0b72824 | 2012-10-28 19:42:30 -0700 | [diff] [blame] | 192 | mShowingMessage = true; |
| 193 | update(); |
| 194 | mHandler.removeCallbacks(mClearMessageRunnable); |
| 195 | if (mTimeout > 0) { |
| 196 | mHandler.postDelayed(mClearMessageRunnable, mTimeout); |
| 197 | } |
Alan Viverette | b428b0f | 2013-02-07 12:19:15 -0800 | [diff] [blame] | 198 | mHandler.removeCallbacksAndMessages(ANNOUNCE_TOKEN); |
| 199 | mHandler.postAtTime(new AnnounceRunnable(this, getText()), ANNOUNCE_TOKEN, |
| 200 | (SystemClock.uptimeMillis() + ANNOUNCEMENT_DELAY)); |
Jim Miller | 0b72824 | 2012-10-28 19:42:30 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Update the status lines based on these rules: |
| 205 | * AlarmStatus: Alarm state always gets it's own line. |
| 206 | * Status1 is shared between help, battery status and generic unlock instructions, |
| 207 | * prioritized in that order. |
| 208 | * @param showStatusLines status lines are shown if true |
| 209 | */ |
| 210 | void update() { |
| 211 | MutableInt icon = new MutableInt(0); |
| 212 | CharSequence status = concat(getChargeInfo(icon), getOwnerInfo(), getCurrentMessage()); |
| 213 | setCompoundDrawablesWithIntrinsicBounds(icon.value, 0, 0, 0); |
| 214 | setText(status); |
| 215 | } |
| 216 | |
Jim Miller | e9be140 | 2012-11-01 16:14:20 -0700 | [diff] [blame] | 217 | private CharSequence concat(CharSequence... args) { |
Jim Miller | 0b72824 | 2012-10-28 19:42:30 -0700 | [diff] [blame] | 218 | StringBuilder b = new StringBuilder(); |
Jim Miller | e9be140 | 2012-11-01 16:14:20 -0700 | [diff] [blame] | 219 | if (!TextUtils.isEmpty(args[0])) { |
| 220 | b.append(args[0]); |
| 221 | } |
| 222 | for (int i = 1; i < args.length; i++) { |
| 223 | CharSequence text = args[i]; |
| 224 | if (!TextUtils.isEmpty(text)) { |
| 225 | if (b.length() > 0) { |
| 226 | b.append(mSeparator); |
| 227 | } |
| 228 | b.append(text); |
Jim Miller | 0b72824 | 2012-10-28 19:42:30 -0700 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | return b.toString(); |
| 232 | } |
| 233 | |
Jim Miller | 0b72824 | 2012-10-28 19:42:30 -0700 | [diff] [blame] | 234 | CharSequence getCurrentMessage() { |
| 235 | return mShowingMessage ? mMessage : null; |
| 236 | } |
| 237 | |
| 238 | String getOwnerInfo() { |
| 239 | ContentResolver res = getContext().getContentResolver(); |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 240 | String info = null; |
| 241 | final boolean ownerInfoEnabled = mLockPatternUtils.isOwnerInfoEnabled(); |
| 242 | if (ownerInfoEnabled && !mShowingMessage) { |
| 243 | info = mLockPatternUtils.getOwnerInfo(mLockPatternUtils.getCurrentUser()); |
| 244 | } |
| 245 | return info; |
Jim Miller | 0b72824 | 2012-10-28 19:42:30 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | private CharSequence getChargeInfo(MutableInt icon) { |
| 249 | CharSequence string = null; |
| 250 | if (mShowingBatteryInfo && !mShowingMessage) { |
| 251 | // Battery status |
Daniel Sandler | fe0e1e4 | 2012-11-29 15:11:50 -0500 | [diff] [blame] | 252 | if (mCharging) { |
Jim Miller | 0b72824 | 2012-10-28 19:42:30 -0700 | [diff] [blame] | 253 | // Charging, charged or waiting to charge. |
Daniel Sandler | fe0e1e4 | 2012-11-29 15:11:50 -0500 | [diff] [blame] | 254 | string = getContext().getString(mBatteryCharged |
Jim Miller | 5ecd811 | 2013-01-09 18:50:26 -0800 | [diff] [blame] | 255 | ? R.string.keyguard_charged |
| 256 | : R.string.keyguard_plugged_in, mBatteryLevel); |
Jim Miller | 0b72824 | 2012-10-28 19:42:30 -0700 | [diff] [blame] | 257 | icon.value = CHARGING_ICON; |
| 258 | } else if (mBatteryIsLow) { |
| 259 | // Battery is low |
Jim Miller | 5ecd811 | 2013-01-09 18:50:26 -0800 | [diff] [blame] | 260 | string = getContext().getString(R.string.keyguard_low_battery); |
Jim Miller | 0b72824 | 2012-10-28 19:42:30 -0700 | [diff] [blame] | 261 | icon.value = BATTERY_LOW_ICON; |
| 262 | } |
| 263 | } |
| 264 | return string; |
| 265 | } |
| 266 | |
Chris Wren | c345146 | 2012-10-30 11:22:58 -0400 | [diff] [blame] | 267 | private void hideMessage(int duration, boolean thenUpdate) { |
Chris Wren | c0ae9e6 | 2012-11-05 13:16:31 -0500 | [diff] [blame] | 268 | if (duration > 0) { |
| 269 | Animator anim = ObjectAnimator.ofFloat(this, "alpha", 0f); |
| 270 | anim.setDuration(duration); |
| 271 | if (thenUpdate) { |
| 272 | anim.addListener(new AnimatorListenerAdapter() { |
| 273 | @Override |
| 274 | public void onAnimationEnd(Animator animation) { |
| 275 | update(); |
| 276 | } |
Chris Wren | c345146 | 2012-10-30 11:22:58 -0400 | [diff] [blame] | 277 | }); |
Chris Wren | c0ae9e6 | 2012-11-05 13:16:31 -0500 | [diff] [blame] | 278 | } |
| 279 | anim.start(); |
| 280 | } else { |
| 281 | setAlpha(0f); |
| 282 | if (thenUpdate) { |
| 283 | update(); |
| 284 | } |
Chris Wren | c345146 | 2012-10-30 11:22:58 -0400 | [diff] [blame] | 285 | } |
Chris Wren | c345146 | 2012-10-30 11:22:58 -0400 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | private void showMessage(int duration) { |
Chris Wren | c0ae9e6 | 2012-11-05 13:16:31 -0500 | [diff] [blame] | 289 | if (duration > 0) { |
| 290 | Animator anim = ObjectAnimator.ofFloat(this, "alpha", 1f); |
| 291 | anim.setDuration(duration); |
| 292 | anim.start(); |
| 293 | } else { |
| 294 | setAlpha(1f); |
| 295 | } |
Chris Wren | c345146 | 2012-10-30 11:22:58 -0400 | [diff] [blame] | 296 | } |
Alan Viverette | b428b0f | 2013-02-07 12:19:15 -0800 | [diff] [blame] | 297 | |
| 298 | /** |
| 299 | * Runnable used to delay accessibility announcements. |
| 300 | */ |
| 301 | private static class AnnounceRunnable implements Runnable { |
| 302 | private final WeakReference<View> mHost; |
| 303 | private final CharSequence mTextToAnnounce; |
| 304 | |
| 305 | public AnnounceRunnable(View host, CharSequence textToAnnounce) { |
| 306 | mHost = new WeakReference<View>(host); |
| 307 | mTextToAnnounce = textToAnnounce; |
| 308 | } |
| 309 | |
| 310 | @Override |
| 311 | public void run() { |
| 312 | final View host = mHost.get(); |
| 313 | if (host != null) { |
| 314 | host.announceForAccessibility(mTextToAnnounce); |
| 315 | } |
| 316 | } |
| 317 | } |
Jim Miller | 0b72824 | 2012-10-28 19:42:30 -0700 | [diff] [blame] | 318 | } |