blob: e720f3ebd312b0248864d581c76d6ef397a5006f [file] [log] [blame]
John Spurlock34e13d92013-08-10 06:52:28 -04001/*
2 * Copyright (C) 2013 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
Jorim Jaggib10e33f2015-02-04 21:57:40 +010017package com.android.server.policy;
John Spurlock34e13d92013-08-10 06:52:28 -040018
Daniel Sandlera953b6d2013-10-11 14:27:53 -040019import android.animation.ArgbEvaluator;
20import android.animation.ValueAnimator;
21import android.app.ActivityManager;
Daniel Sandlerb83f5c62013-10-13 22:04:36 -040022import android.content.BroadcastReceiver;
John Spurlock34e13d92013-08-10 06:52:28 -040023import android.content.Context;
Daniel Sandlerb83f5c62013-10-13 22:04:36 -040024import android.content.Intent;
25import android.content.IntentFilter;
Daniel Sandlera953b6d2013-10-11 14:27:53 -040026import android.graphics.PixelFormat;
27import android.graphics.drawable.ColorDrawable;
John Spurlock34e13d92013-08-10 06:52:28 -040028import android.os.Handler;
John Spurlock4cf6a942013-08-13 20:02:46 -040029import android.os.Message;
John Spurlockd67ec252013-09-05 11:31:54 -040030import android.os.UserHandle;
31import android.provider.Settings;
Daniel Sandlera953b6d2013-10-11 14:27:53 -040032import android.util.DisplayMetrics;
John Spurlock34e13d92013-08-10 06:52:28 -040033import android.util.Slog;
John Spurlockd9b70bd2014-02-06 17:02:44 -050034import android.util.SparseBooleanArray;
Daniel Sandlera953b6d2013-10-11 14:27:53 -040035import android.view.Gravity;
36import android.view.MotionEvent;
John Spurlock34e13d92013-08-10 06:52:28 -040037import android.view.View;
Daniel Sandlera953b6d2013-10-11 14:27:53 -040038import android.view.ViewGroup;
39import android.view.WindowManager;
John Spurlock4cf6a942013-08-13 20:02:46 -040040import android.view.animation.Animation;
John Spurlock4cf6a942013-08-13 20:02:46 -040041import android.view.animation.AnimationUtils;
Daniel Sandlera953b6d2013-10-11 14:27:53 -040042import android.view.animation.DecelerateInterpolator;
Adrian Roos62b65e42015-02-25 18:05:34 +010043import android.view.animation.Interpolator;
Daniel Sandlera953b6d2013-10-11 14:27:53 -040044import android.widget.Button;
45import android.widget.FrameLayout;
John Spurlock34e13d92013-08-10 06:52:28 -040046
47import com.android.internal.R;
48
49/**
John Spurlockf1a36642013-10-12 17:50:42 -040050 * Helper to manage showing/hiding a confirmation prompt when the navigation bar is hidden
51 * entering immersive mode.
John Spurlock34e13d92013-08-10 06:52:28 -040052 */
John Spurlockf1a36642013-10-12 17:50:42 -040053public class ImmersiveModeConfirmation {
54 private static final String TAG = "ImmersiveModeConfirmation";
John Spurlock4cf6a942013-08-13 20:02:46 -040055 private static final boolean DEBUG = false;
Daniel Sandlera953b6d2013-10-11 14:27:53 -040056 private static final boolean DEBUG_SHOW_EVERY_TIME = false; // super annoying, use with caution
John Spurlockd9b70bd2014-02-06 17:02:44 -050057 private static final String CONFIRMED = "confirmed";
John Spurlock34e13d92013-08-10 06:52:28 -040058
59 private final Context mContext;
John Spurlock4cf6a942013-08-13 20:02:46 -040060 private final H mHandler;
John Spurlock4cf6a942013-08-13 20:02:46 -040061 private final long mShowDelayMs;
John Spurlockd67ec252013-09-05 11:31:54 -040062 private final long mPanicThresholdMs;
John Spurlockd9b70bd2014-02-06 17:02:44 -050063 private final SparseBooleanArray mUserPanicResets = new SparseBooleanArray();
John Spurlock34e13d92013-08-10 06:52:28 -040064
John Spurlockd9b70bd2014-02-06 17:02:44 -050065 private boolean mConfirmed;
Daniel Sandlera953b6d2013-10-11 14:27:53 -040066 private ClingWindowView mClingWindow;
John Spurlockd67ec252013-09-05 11:31:54 -040067 private long mPanicTime;
Daniel Sandlera953b6d2013-10-11 14:27:53 -040068 private WindowManager mWindowManager;
John Spurlockd9b70bd2014-02-06 17:02:44 -050069 private int mCurrentUserId;
John Spurlock34e13d92013-08-10 06:52:28 -040070
John Spurlockf1a36642013-10-12 17:50:42 -040071 public ImmersiveModeConfirmation(Context context) {
John Spurlock34e13d92013-08-10 06:52:28 -040072 mContext = context;
John Spurlock4cf6a942013-08-13 20:02:46 -040073 mHandler = new H();
74 mShowDelayMs = getNavBarExitDuration() * 3;
John Spurlockd67ec252013-09-05 11:31:54 -040075 mPanicThresholdMs = context.getResources()
John Spurlockf1a36642013-10-12 17:50:42 -040076 .getInteger(R.integer.config_immersive_mode_confirmation_panic);
Daniel Sandlera953b6d2013-10-11 14:27:53 -040077 mWindowManager = (WindowManager)
78 mContext.getSystemService(Context.WINDOW_SERVICE);
John Spurlock4cf6a942013-08-13 20:02:46 -040079 }
80
81 private long getNavBarExitDuration() {
82 Animation exit = AnimationUtils.loadAnimation(mContext, R.anim.dock_bottom_exit);
83 return exit != null ? exit.getDuration() : 0;
John Spurlock34e13d92013-08-10 06:52:28 -040084 }
85
John Spurlock4355a532014-02-19 09:49:25 -050086 public void loadSetting(int currentUserId) {
John Spurlockd9b70bd2014-02-06 17:02:44 -050087 mConfirmed = false;
John Spurlock4355a532014-02-19 09:49:25 -050088 mCurrentUserId = currentUserId;
John Spurlockd9b70bd2014-02-06 17:02:44 -050089 if (DEBUG) Slog.d(TAG, String.format("loadSetting() mCurrentUserId=%d resetForPanic=%s",
90 mCurrentUserId, mUserPanicResets.get(mCurrentUserId, false)));
91 String value = null;
John Spurlockd67ec252013-09-05 11:31:54 -040092 try {
John Spurlockd9b70bd2014-02-06 17:02:44 -050093 value = Settings.Secure.getStringForUser(mContext.getContentResolver(),
John Spurlockf1a36642013-10-12 17:50:42 -040094 Settings.Secure.IMMERSIVE_MODE_CONFIRMATIONS,
John Spurlockd67ec252013-09-05 11:31:54 -040095 UserHandle.USER_CURRENT);
John Spurlockd9b70bd2014-02-06 17:02:44 -050096 mConfirmed = CONFIRMED.equals(value);
97 if (DEBUG) Slog.d(TAG, "Loaded mConfirmed=" + mConfirmed);
John Spurlockd67ec252013-09-05 11:31:54 -040098 } catch (Throwable t) {
John Spurlockd9b70bd2014-02-06 17:02:44 -050099 Slog.w(TAG, "Error loading confirmations, value=" + value, t);
John Spurlockd67ec252013-09-05 11:31:54 -0400100 }
101 }
102
103 private void saveSetting() {
104 if (DEBUG) Slog.d(TAG, "saveSetting()");
105 try {
John Spurlockd9b70bd2014-02-06 17:02:44 -0500106 final String value = mConfirmed ? CONFIRMED : null;
John Spurlockd67ec252013-09-05 11:31:54 -0400107 Settings.Secure.putStringForUser(mContext.getContentResolver(),
John Spurlockf1a36642013-10-12 17:50:42 -0400108 Settings.Secure.IMMERSIVE_MODE_CONFIRMATIONS,
John Spurlockd9b70bd2014-02-06 17:02:44 -0500109 value,
John Spurlockd67ec252013-09-05 11:31:54 -0400110 UserHandle.USER_CURRENT);
John Spurlockd9b70bd2014-02-06 17:02:44 -0500111 if (DEBUG) Slog.d(TAG, "Saved value=" + value);
John Spurlockd67ec252013-09-05 11:31:54 -0400112 } catch (Throwable t) {
John Spurlockd9b70bd2014-02-06 17:02:44 -0500113 Slog.w(TAG, "Error saving confirmations, mConfirmed=" + mConfirmed, t);
John Spurlockd67ec252013-09-05 11:31:54 -0400114 }
115 }
116
Maurice Lam99c6e072014-04-28 18:24:28 -0700117 public void immersiveModeChanged(String pkg, boolean isImmersiveMode,
118 boolean userSetupComplete) {
John Spurlock4cf6a942013-08-13 20:02:46 -0400119 mHandler.removeMessages(H.SHOW);
John Spurlockf1a36642013-10-12 17:50:42 -0400120 if (isImmersiveMode) {
John Spurlockd9b70bd2014-02-06 17:02:44 -0500121 final boolean disabled = PolicyControl.disableImmersiveConfirmation(pkg);
122 if (DEBUG) Slog.d(TAG, String.format("immersiveModeChanged() disabled=%s mConfirmed=%s",
123 disabled, mConfirmed));
Maurice Lam99c6e072014-04-28 18:24:28 -0700124 if (!disabled && (DEBUG_SHOW_EVERY_TIME || !mConfirmed) && userSetupComplete) {
John Spurlockd9b70bd2014-02-06 17:02:44 -0500125 mHandler.sendEmptyMessageDelayed(H.SHOW, mShowDelayMs);
John Spurlock34e13d92013-08-10 06:52:28 -0400126 }
127 } else {
John Spurlock4cf6a942013-08-13 20:02:46 -0400128 mHandler.sendEmptyMessage(H.HIDE);
John Spurlock34e13d92013-08-10 06:52:28 -0400129 }
130 }
131
John Spurlockd9b70bd2014-02-06 17:02:44 -0500132 public boolean onPowerKeyDown(boolean isScreenOn, long time, boolean inImmersiveMode) {
133 if (!isScreenOn && (time - mPanicTime < mPanicThresholdMs)) {
John Spurlockd67ec252013-09-05 11:31:54 -0400134 // turning the screen back on within the panic threshold
John Spurlockd9b70bd2014-02-06 17:02:44 -0500135 mHandler.sendEmptyMessage(H.PANIC);
136 return mClingWindow == null;
John Spurlockd67ec252013-09-05 11:31:54 -0400137 }
John Spurlockf1a36642013-10-12 17:50:42 -0400138 if (isScreenOn && inImmersiveMode) {
139 // turning the screen off, remember if we were in immersive mode
John Spurlockd67ec252013-09-05 11:31:54 -0400140 mPanicTime = time;
John Spurlockd67ec252013-09-05 11:31:54 -0400141 } else {
142 mPanicTime = 0;
John Spurlockd67ec252013-09-05 11:31:54 -0400143 }
John Spurlockd9b70bd2014-02-06 17:02:44 -0500144 return false;
John Spurlockd67ec252013-09-05 11:31:54 -0400145 }
146
147 public void confirmCurrentPrompt() {
John Spurlockd9b70bd2014-02-06 17:02:44 -0500148 if (mClingWindow != null) {
149 if (DEBUG) Slog.d(TAG, "confirmCurrentPrompt()");
150 mHandler.post(mConfirm);
151 }
John Spurlockd67ec252013-09-05 11:31:54 -0400152 }
153
John Spurlockd9b70bd2014-02-06 17:02:44 -0500154 private void handlePanic() {
155 if (DEBUG) Slog.d(TAG, "handlePanic()");
156 if (mUserPanicResets.get(mCurrentUserId, false)) return; // already reset for panic
157 mUserPanicResets.put(mCurrentUserId, true);
158 mConfirmed = false;
159 saveSetting();
160 }
161
John Spurlock4cf6a942013-08-13 20:02:46 -0400162 private void handleHide() {
Daniel Sandlera953b6d2013-10-11 14:27:53 -0400163 if (mClingWindow != null) {
John Spurlockd9b70bd2014-02-06 17:02:44 -0500164 if (DEBUG) Slog.d(TAG, "Hiding immersive mode confirmation");
Daniel Sandlera953b6d2013-10-11 14:27:53 -0400165 mWindowManager.removeView(mClingWindow);
166 mClingWindow = null;
167 }
168 }
169
Daniel Sandlerb83f5c62013-10-13 22:04:36 -0400170 public WindowManager.LayoutParams getClingWindowLayoutParams() {
171 final WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
172 ViewGroup.LayoutParams.MATCH_PARENT,
173 ViewGroup.LayoutParams.MATCH_PARENT,
Adrian Roos62b65e42015-02-25 18:05:34 +0100174 WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL,
Daniel Sandlerb83f5c62013-10-13 22:04:36 -0400175 0
176 | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
177 | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
178 | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED
179 ,
180 PixelFormat.TRANSLUCENT);
John Spurlock0513d5a2013-12-09 11:59:45 -0500181 lp.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
John Spurlockf1a36642013-10-12 17:50:42 -0400182 lp.setTitle("ImmersiveModeConfirmation");
Adrian Roos62b65e42015-02-25 18:05:34 +0100183 lp.windowAnimations = com.android.internal.R.style.Animation_ImmersiveModeConfirmation;
Daniel Sandlerb83f5c62013-10-13 22:04:36 -0400184 return lp;
185 }
186
187 public FrameLayout.LayoutParams getBubbleLayoutParams() {
188 return new FrameLayout.LayoutParams(
189 mContext.getResources().getDimensionPixelSize(
190 R.dimen.immersive_mode_cling_width),
191 ViewGroup.LayoutParams.WRAP_CONTENT,
192 Gravity.CENTER_HORIZONTAL | Gravity.TOP);
193 }
194
Daniel Sandlera953b6d2013-10-11 14:27:53 -0400195 private class ClingWindowView extends FrameLayout {
196 private static final int BGCOLOR = 0x80000000;
Adrian Roos62b65e42015-02-25 18:05:34 +0100197 private static final int OFFSET_DP = 96;
198 private static final int ANIMATION_DURATION = 250;
Daniel Sandlera953b6d2013-10-11 14:27:53 -0400199
John Spurlock47e3de22013-10-13 19:20:04 -0400200 private final Runnable mConfirm;
Daniel Sandlera953b6d2013-10-11 14:27:53 -0400201 private final ColorDrawable mColor = new ColorDrawable(0);
Adrian Roos62b65e42015-02-25 18:05:34 +0100202 private final Interpolator mInterpolator;
Daniel Sandlera953b6d2013-10-11 14:27:53 -0400203 private ValueAnimator mColorAnim;
Daniel Sandlerb83f5c62013-10-13 22:04:36 -0400204 private ViewGroup mClingLayout;
205
Daniel Sandlerda35dde2013-10-18 12:05:04 -0400206 private Runnable mUpdateLayoutRunnable = new Runnable() {
207 @Override
208 public void run() {
209 if (mClingLayout != null && mClingLayout.getParent() != null) {
210 mClingLayout.setLayoutParams(getBubbleLayoutParams());
211 }
212 }
213 };
214
Daniel Sandlerb83f5c62013-10-13 22:04:36 -0400215 private BroadcastReceiver mReceiver = new BroadcastReceiver() {
216 @Override
217 public void onReceive(Context context, Intent intent) {
218 if (intent.getAction().equals(Intent.ACTION_CONFIGURATION_CHANGED)) {
Daniel Sandlerda35dde2013-10-18 12:05:04 -0400219 post(mUpdateLayoutRunnable);
Daniel Sandlerb83f5c62013-10-13 22:04:36 -0400220 }
221 }
222 };
Daniel Sandlera953b6d2013-10-11 14:27:53 -0400223
John Spurlock47e3de22013-10-13 19:20:04 -0400224 public ClingWindowView(Context context, Runnable confirm) {
Daniel Sandlera953b6d2013-10-11 14:27:53 -0400225 super(context);
John Spurlock47e3de22013-10-13 19:20:04 -0400226 mConfirm = confirm;
Daniel Sandlera953b6d2013-10-11 14:27:53 -0400227 setBackground(mColor);
Adrian Roos62b65e42015-02-25 18:05:34 +0100228 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
229 mInterpolator = AnimationUtils
230 .loadInterpolator(mContext, android.R.interpolator.linear_out_slow_in);
Daniel Sandlera953b6d2013-10-11 14:27:53 -0400231 }
232
233 @Override
234 public void onAttachedToWindow() {
235 super.onAttachedToWindow();
236
237 DisplayMetrics metrics = new DisplayMetrics();
238 mWindowManager.getDefaultDisplay().getMetrics(metrics);
239 float density = metrics.density;
240
241 // create the confirmation cling
Daniel Sandlerb83f5c62013-10-13 22:04:36 -0400242 mClingLayout = (ViewGroup)
John Spurlockf1a36642013-10-12 17:50:42 -0400243 View.inflate(getContext(), R.layout.immersive_mode_cling, null);
Daniel Sandlera953b6d2013-10-11 14:27:53 -0400244
Daniel Sandlerb83f5c62013-10-13 22:04:36 -0400245 final Button ok = (Button) mClingLayout.findViewById(R.id.ok);
Daniel Sandlera953b6d2013-10-11 14:27:53 -0400246 ok.setOnClickListener(new OnClickListener() {
247 @Override
248 public void onClick(View v) {
John Spurlock47e3de22013-10-13 19:20:04 -0400249 mConfirm.run();
Daniel Sandlera953b6d2013-10-11 14:27:53 -0400250 }
251 });
Daniel Sandlerb83f5c62013-10-13 22:04:36 -0400252 addView(mClingLayout, getBubbleLayoutParams());
Daniel Sandlera953b6d2013-10-11 14:27:53 -0400253
254 if (ActivityManager.isHighEndGfx()) {
Adrian Roos62b65e42015-02-25 18:05:34 +0100255 final View cling = mClingLayout;
256 cling.setAlpha(0f);
257 cling.setTranslationY(-OFFSET_DP * density);
Daniel Sandlera953b6d2013-10-11 14:27:53 -0400258
Adrian Roos62b65e42015-02-25 18:05:34 +0100259 postOnAnimation(new Runnable() {
Daniel Sandlera953b6d2013-10-11 14:27:53 -0400260 @Override
Adrian Roos62b65e42015-02-25 18:05:34 +0100261 public void run() {
262 cling.animate()
263 .alpha(1f)
264 .translationY(0)
265 .setDuration(ANIMATION_DURATION)
266 .setInterpolator(mInterpolator)
267 .withLayer()
268 .start();
269
270 mColorAnim = ValueAnimator.ofObject(new ArgbEvaluator(), 0, BGCOLOR);
271 mColorAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
272 @Override
273 public void onAnimationUpdate(ValueAnimator animation) {
274 final int c = (Integer) animation.getAnimatedValue();
275 mColor.setColor(c);
276 }
277 });
278 mColorAnim.setDuration(ANIMATION_DURATION);
279 mColorAnim.setInterpolator(mInterpolator);
280 mColorAnim.start();
Daniel Sandlera953b6d2013-10-11 14:27:53 -0400281 }
282 });
Daniel Sandlera953b6d2013-10-11 14:27:53 -0400283 } else {
284 mColor.setColor(BGCOLOR);
285 }
Daniel Sandlerb83f5c62013-10-13 22:04:36 -0400286
Adrian Roos62b65e42015-02-25 18:05:34 +0100287 mContext.registerReceiver(mReceiver,
288 new IntentFilter(Intent.ACTION_CONFIGURATION_CHANGED));
Daniel Sandlerb83f5c62013-10-13 22:04:36 -0400289 }
290
291 @Override
292 public void onDetachedFromWindow() {
293 mContext.unregisterReceiver(mReceiver);
Daniel Sandlera953b6d2013-10-11 14:27:53 -0400294 }
295
296 @Override
297 public boolean onTouchEvent(MotionEvent motion) {
Daniel Sandlera953b6d2013-10-11 14:27:53 -0400298 return true;
John Spurlock4cf6a942013-08-13 20:02:46 -0400299 }
300 }
John Spurlock34e13d92013-08-10 06:52:28 -0400301
John Spurlockd9b70bd2014-02-06 17:02:44 -0500302 private void handleShow() {
303 if (DEBUG) Slog.d(TAG, "Showing immersive mode confirmation");
John Spurlockd67ec252013-09-05 11:31:54 -0400304
John Spurlockd9b70bd2014-02-06 17:02:44 -0500305 mClingWindow = new ClingWindowView(mContext, mConfirm);
John Spurlock34e13d92013-08-10 06:52:28 -0400306
John Spurlock4cf6a942013-08-13 20:02:46 -0400307 // we will be hiding the nav bar, so layout as if it's already hidden
Daniel Sandlera953b6d2013-10-11 14:27:53 -0400308 mClingWindow.setSystemUiVisibility(
309 View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
310 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
John Spurlock4cf6a942013-08-13 20:02:46 -0400311
312 // show the confirmation
Daniel Sandlerb83f5c62013-10-13 22:04:36 -0400313 WindowManager.LayoutParams lp = getClingWindowLayoutParams();
Daniel Sandlera953b6d2013-10-11 14:27:53 -0400314 mWindowManager.addView(mClingWindow, lp);
John Spurlock34e13d92013-08-10 06:52:28 -0400315 }
316
John Spurlockd9b70bd2014-02-06 17:02:44 -0500317 private final Runnable mConfirm = new Runnable() {
318 @Override
319 public void run() {
320 if (DEBUG) Slog.d(TAG, "mConfirm.run()");
321 if (!mConfirmed) {
322 mConfirmed = true;
323 saveSetting();
John Spurlock34e13d92013-08-10 06:52:28 -0400324 }
John Spurlockd9b70bd2014-02-06 17:02:44 -0500325 handleHide();
326 }
327 };
John Spurlock4cf6a942013-08-13 20:02:46 -0400328
329 private final class H extends Handler {
John Spurlockd9b70bd2014-02-06 17:02:44 -0500330 private static final int SHOW = 1;
331 private static final int HIDE = 2;
332 private static final int PANIC = 3;
John Spurlock4cf6a942013-08-13 20:02:46 -0400333
334 @Override
335 public void handleMessage(Message msg) {
336 switch(msg.what) {
337 case SHOW:
John Spurlockd9b70bd2014-02-06 17:02:44 -0500338 handleShow();
John Spurlock4cf6a942013-08-13 20:02:46 -0400339 break;
340 case HIDE:
341 handleHide();
342 break;
John Spurlockd9b70bd2014-02-06 17:02:44 -0500343 case PANIC:
344 handlePanic();
345 break;
John Spurlock4cf6a942013-08-13 20:02:46 -0400346 }
347 }
348 }
John Spurlock34e13d92013-08-10 06:52:28 -0400349}