blob: 160d44c58bc9e8036b5d5d882409a92ff5085b5f [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;
Adrian Roos3e8dd512015-05-21 13:27:58 -070039import android.view.ViewTreeObserver;
Daniel Sandlera953b6d2013-10-11 14:27:53 -040040import android.view.WindowManager;
John Spurlock4cf6a942013-08-13 20:02:46 -040041import android.view.animation.Animation;
John Spurlock4cf6a942013-08-13 20:02:46 -040042import android.view.animation.AnimationUtils;
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 Spurlock34e13d92013-08-10 06:52:28 -040063
John Spurlockd9b70bd2014-02-06 17:02:44 -050064 private boolean mConfirmed;
Daniel Sandlera953b6d2013-10-11 14:27:53 -040065 private ClingWindowView mClingWindow;
John Spurlockd67ec252013-09-05 11:31:54 -040066 private long mPanicTime;
Daniel Sandlera953b6d2013-10-11 14:27:53 -040067 private WindowManager mWindowManager;
John Spurlockd9b70bd2014-02-06 17:02:44 -050068 private int mCurrentUserId;
John Spurlock34e13d92013-08-10 06:52:28 -040069
John Spurlockf1a36642013-10-12 17:50:42 -040070 public ImmersiveModeConfirmation(Context context) {
John Spurlock34e13d92013-08-10 06:52:28 -040071 mContext = context;
John Spurlock4cf6a942013-08-13 20:02:46 -040072 mHandler = new H();
73 mShowDelayMs = getNavBarExitDuration() * 3;
John Spurlockd67ec252013-09-05 11:31:54 -040074 mPanicThresholdMs = context.getResources()
John Spurlockf1a36642013-10-12 17:50:42 -040075 .getInteger(R.integer.config_immersive_mode_confirmation_panic);
Daniel Sandlera953b6d2013-10-11 14:27:53 -040076 mWindowManager = (WindowManager)
77 mContext.getSystemService(Context.WINDOW_SERVICE);
John Spurlock4cf6a942013-08-13 20:02:46 -040078 }
79
80 private long getNavBarExitDuration() {
81 Animation exit = AnimationUtils.loadAnimation(mContext, R.anim.dock_bottom_exit);
82 return exit != null ? exit.getDuration() : 0;
John Spurlock34e13d92013-08-10 06:52:28 -040083 }
84
John Spurlock4355a532014-02-19 09:49:25 -050085 public void loadSetting(int currentUserId) {
John Spurlockd9b70bd2014-02-06 17:02:44 -050086 mConfirmed = false;
John Spurlock4355a532014-02-19 09:49:25 -050087 mCurrentUserId = currentUserId;
Adrian Roosc5d8fbc2015-05-21 17:20:46 -070088 if (DEBUG) Slog.d(TAG, String.format("loadSetting() mCurrentUserId=%d", mCurrentUserId));
John Spurlockd9b70bd2014-02-06 17:02:44 -050089 String value = null;
John Spurlockd67ec252013-09-05 11:31:54 -040090 try {
John Spurlockd9b70bd2014-02-06 17:02:44 -050091 value = Settings.Secure.getStringForUser(mContext.getContentResolver(),
John Spurlockf1a36642013-10-12 17:50:42 -040092 Settings.Secure.IMMERSIVE_MODE_CONFIRMATIONS,
John Spurlockd67ec252013-09-05 11:31:54 -040093 UserHandle.USER_CURRENT);
John Spurlockd9b70bd2014-02-06 17:02:44 -050094 mConfirmed = CONFIRMED.equals(value);
95 if (DEBUG) Slog.d(TAG, "Loaded mConfirmed=" + mConfirmed);
John Spurlockd67ec252013-09-05 11:31:54 -040096 } catch (Throwable t) {
John Spurlockd9b70bd2014-02-06 17:02:44 -050097 Slog.w(TAG, "Error loading confirmations, value=" + value, t);
John Spurlockd67ec252013-09-05 11:31:54 -040098 }
99 }
100
101 private void saveSetting() {
102 if (DEBUG) Slog.d(TAG, "saveSetting()");
103 try {
John Spurlockd9b70bd2014-02-06 17:02:44 -0500104 final String value = mConfirmed ? CONFIRMED : null;
John Spurlockd67ec252013-09-05 11:31:54 -0400105 Settings.Secure.putStringForUser(mContext.getContentResolver(),
John Spurlockf1a36642013-10-12 17:50:42 -0400106 Settings.Secure.IMMERSIVE_MODE_CONFIRMATIONS,
John Spurlockd9b70bd2014-02-06 17:02:44 -0500107 value,
John Spurlockd67ec252013-09-05 11:31:54 -0400108 UserHandle.USER_CURRENT);
John Spurlockd9b70bd2014-02-06 17:02:44 -0500109 if (DEBUG) Slog.d(TAG, "Saved value=" + value);
John Spurlockd67ec252013-09-05 11:31:54 -0400110 } catch (Throwable t) {
John Spurlockd9b70bd2014-02-06 17:02:44 -0500111 Slog.w(TAG, "Error saving confirmations, mConfirmed=" + mConfirmed, t);
John Spurlockd67ec252013-09-05 11:31:54 -0400112 }
113 }
114
Maurice Lam99c6e072014-04-28 18:24:28 -0700115 public void immersiveModeChanged(String pkg, boolean isImmersiveMode,
116 boolean userSetupComplete) {
John Spurlock4cf6a942013-08-13 20:02:46 -0400117 mHandler.removeMessages(H.SHOW);
John Spurlockf1a36642013-10-12 17:50:42 -0400118 if (isImmersiveMode) {
John Spurlockd9b70bd2014-02-06 17:02:44 -0500119 final boolean disabled = PolicyControl.disableImmersiveConfirmation(pkg);
120 if (DEBUG) Slog.d(TAG, String.format("immersiveModeChanged() disabled=%s mConfirmed=%s",
121 disabled, mConfirmed));
Maurice Lam99c6e072014-04-28 18:24:28 -0700122 if (!disabled && (DEBUG_SHOW_EVERY_TIME || !mConfirmed) && userSetupComplete) {
John Spurlockd9b70bd2014-02-06 17:02:44 -0500123 mHandler.sendEmptyMessageDelayed(H.SHOW, mShowDelayMs);
John Spurlock34e13d92013-08-10 06:52:28 -0400124 }
125 } else {
John Spurlock4cf6a942013-08-13 20:02:46 -0400126 mHandler.sendEmptyMessage(H.HIDE);
John Spurlock34e13d92013-08-10 06:52:28 -0400127 }
128 }
129
John Spurlockd9b70bd2014-02-06 17:02:44 -0500130 public boolean onPowerKeyDown(boolean isScreenOn, long time, boolean inImmersiveMode) {
131 if (!isScreenOn && (time - mPanicTime < mPanicThresholdMs)) {
John Spurlockd67ec252013-09-05 11:31:54 -0400132 // turning the screen back on within the panic threshold
John Spurlockd9b70bd2014-02-06 17:02:44 -0500133 return mClingWindow == null;
John Spurlockd67ec252013-09-05 11:31:54 -0400134 }
John Spurlockf1a36642013-10-12 17:50:42 -0400135 if (isScreenOn && inImmersiveMode) {
136 // turning the screen off, remember if we were in immersive mode
John Spurlockd67ec252013-09-05 11:31:54 -0400137 mPanicTime = time;
John Spurlockd67ec252013-09-05 11:31:54 -0400138 } else {
139 mPanicTime = 0;
John Spurlockd67ec252013-09-05 11:31:54 -0400140 }
John Spurlockd9b70bd2014-02-06 17:02:44 -0500141 return false;
John Spurlockd67ec252013-09-05 11:31:54 -0400142 }
143
144 public void confirmCurrentPrompt() {
John Spurlockd9b70bd2014-02-06 17:02:44 -0500145 if (mClingWindow != null) {
146 if (DEBUG) Slog.d(TAG, "confirmCurrentPrompt()");
147 mHandler.post(mConfirm);
148 }
John Spurlockd67ec252013-09-05 11:31:54 -0400149 }
150
John Spurlock4cf6a942013-08-13 20:02:46 -0400151 private void handleHide() {
Daniel Sandlera953b6d2013-10-11 14:27:53 -0400152 if (mClingWindow != null) {
John Spurlockd9b70bd2014-02-06 17:02:44 -0500153 if (DEBUG) Slog.d(TAG, "Hiding immersive mode confirmation");
Daniel Sandlera953b6d2013-10-11 14:27:53 -0400154 mWindowManager.removeView(mClingWindow);
155 mClingWindow = null;
156 }
157 }
158
Daniel Sandlerb83f5c62013-10-13 22:04:36 -0400159 public WindowManager.LayoutParams getClingWindowLayoutParams() {
160 final WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
161 ViewGroup.LayoutParams.MATCH_PARENT,
162 ViewGroup.LayoutParams.MATCH_PARENT,
Adrian Roos62b65e42015-02-25 18:05:34 +0100163 WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL,
Daniel Sandlerb83f5c62013-10-13 22:04:36 -0400164 0
165 | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
166 | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
167 | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED
168 ,
169 PixelFormat.TRANSLUCENT);
John Spurlock0513d5a2013-12-09 11:59:45 -0500170 lp.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
John Spurlockf1a36642013-10-12 17:50:42 -0400171 lp.setTitle("ImmersiveModeConfirmation");
Adrian Roos62b65e42015-02-25 18:05:34 +0100172 lp.windowAnimations = com.android.internal.R.style.Animation_ImmersiveModeConfirmation;
Daniel Sandlerb83f5c62013-10-13 22:04:36 -0400173 return lp;
174 }
175
176 public FrameLayout.LayoutParams getBubbleLayoutParams() {
177 return new FrameLayout.LayoutParams(
178 mContext.getResources().getDimensionPixelSize(
179 R.dimen.immersive_mode_cling_width),
180 ViewGroup.LayoutParams.WRAP_CONTENT,
181 Gravity.CENTER_HORIZONTAL | Gravity.TOP);
182 }
183
Daniel Sandlera953b6d2013-10-11 14:27:53 -0400184 private class ClingWindowView extends FrameLayout {
185 private static final int BGCOLOR = 0x80000000;
Adrian Roos62b65e42015-02-25 18:05:34 +0100186 private static final int OFFSET_DP = 96;
187 private static final int ANIMATION_DURATION = 250;
Daniel Sandlera953b6d2013-10-11 14:27:53 -0400188
John Spurlock47e3de22013-10-13 19:20:04 -0400189 private final Runnable mConfirm;
Daniel Sandlera953b6d2013-10-11 14:27:53 -0400190 private final ColorDrawable mColor = new ColorDrawable(0);
Adrian Roos62b65e42015-02-25 18:05:34 +0100191 private final Interpolator mInterpolator;
Daniel Sandlera953b6d2013-10-11 14:27:53 -0400192 private ValueAnimator mColorAnim;
Daniel Sandlerb83f5c62013-10-13 22:04:36 -0400193 private ViewGroup mClingLayout;
194
Daniel Sandlerda35dde2013-10-18 12:05:04 -0400195 private Runnable mUpdateLayoutRunnable = new Runnable() {
196 @Override
197 public void run() {
198 if (mClingLayout != null && mClingLayout.getParent() != null) {
199 mClingLayout.setLayoutParams(getBubbleLayoutParams());
200 }
201 }
202 };
203
Adrian Roos3e8dd512015-05-21 13:27:58 -0700204 private ViewTreeObserver.OnComputeInternalInsetsListener mInsetsListener =
205 new ViewTreeObserver.OnComputeInternalInsetsListener() {
206 private final int[] mTmpInt2 = new int[2];
207
208 @Override
209 public void onComputeInternalInsets(
210 ViewTreeObserver.InternalInsetsInfo inoutInfo) {
211 // Set touchable region to cover the cling layout.
212 mClingLayout.getLocationInWindow(mTmpInt2);
213 inoutInfo.setTouchableInsets(
214 ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_REGION);
215 inoutInfo.touchableRegion.set(
216 mTmpInt2[0],
217 mTmpInt2[1],
218 mTmpInt2[0] + mClingLayout.getWidth(),
219 mTmpInt2[1] + mClingLayout.getHeight());
220 }
221 };
222
Daniel Sandlerb83f5c62013-10-13 22:04:36 -0400223 private BroadcastReceiver mReceiver = new BroadcastReceiver() {
224 @Override
225 public void onReceive(Context context, Intent intent) {
226 if (intent.getAction().equals(Intent.ACTION_CONFIGURATION_CHANGED)) {
Daniel Sandlerda35dde2013-10-18 12:05:04 -0400227 post(mUpdateLayoutRunnable);
Daniel Sandlerb83f5c62013-10-13 22:04:36 -0400228 }
229 }
230 };
Daniel Sandlera953b6d2013-10-11 14:27:53 -0400231
John Spurlock47e3de22013-10-13 19:20:04 -0400232 public ClingWindowView(Context context, Runnable confirm) {
Daniel Sandlera953b6d2013-10-11 14:27:53 -0400233 super(context);
John Spurlock47e3de22013-10-13 19:20:04 -0400234 mConfirm = confirm;
Daniel Sandlera953b6d2013-10-11 14:27:53 -0400235 setBackground(mColor);
Adrian Roos62b65e42015-02-25 18:05:34 +0100236 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
237 mInterpolator = AnimationUtils
238 .loadInterpolator(mContext, android.R.interpolator.linear_out_slow_in);
Daniel Sandlera953b6d2013-10-11 14:27:53 -0400239 }
240
241 @Override
242 public void onAttachedToWindow() {
243 super.onAttachedToWindow();
244
245 DisplayMetrics metrics = new DisplayMetrics();
246 mWindowManager.getDefaultDisplay().getMetrics(metrics);
247 float density = metrics.density;
248
Adrian Roos3e8dd512015-05-21 13:27:58 -0700249 getViewTreeObserver().addOnComputeInternalInsetsListener(mInsetsListener);
250
Daniel Sandlera953b6d2013-10-11 14:27:53 -0400251 // create the confirmation cling
Daniel Sandlerb83f5c62013-10-13 22:04:36 -0400252 mClingLayout = (ViewGroup)
John Spurlockf1a36642013-10-12 17:50:42 -0400253 View.inflate(getContext(), R.layout.immersive_mode_cling, null);
Daniel Sandlera953b6d2013-10-11 14:27:53 -0400254
Daniel Sandlerb83f5c62013-10-13 22:04:36 -0400255 final Button ok = (Button) mClingLayout.findViewById(R.id.ok);
Daniel Sandlera953b6d2013-10-11 14:27:53 -0400256 ok.setOnClickListener(new OnClickListener() {
257 @Override
258 public void onClick(View v) {
John Spurlock47e3de22013-10-13 19:20:04 -0400259 mConfirm.run();
Daniel Sandlera953b6d2013-10-11 14:27:53 -0400260 }
261 });
Daniel Sandlerb83f5c62013-10-13 22:04:36 -0400262 addView(mClingLayout, getBubbleLayoutParams());
Daniel Sandlera953b6d2013-10-11 14:27:53 -0400263
264 if (ActivityManager.isHighEndGfx()) {
Adrian Roos62b65e42015-02-25 18:05:34 +0100265 final View cling = mClingLayout;
266 cling.setAlpha(0f);
267 cling.setTranslationY(-OFFSET_DP * density);
Daniel Sandlera953b6d2013-10-11 14:27:53 -0400268
Adrian Roos62b65e42015-02-25 18:05:34 +0100269 postOnAnimation(new Runnable() {
Daniel Sandlera953b6d2013-10-11 14:27:53 -0400270 @Override
Adrian Roos62b65e42015-02-25 18:05:34 +0100271 public void run() {
272 cling.animate()
273 .alpha(1f)
274 .translationY(0)
275 .setDuration(ANIMATION_DURATION)
276 .setInterpolator(mInterpolator)
277 .withLayer()
278 .start();
279
280 mColorAnim = ValueAnimator.ofObject(new ArgbEvaluator(), 0, BGCOLOR);
281 mColorAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
282 @Override
283 public void onAnimationUpdate(ValueAnimator animation) {
284 final int c = (Integer) animation.getAnimatedValue();
285 mColor.setColor(c);
286 }
287 });
288 mColorAnim.setDuration(ANIMATION_DURATION);
289 mColorAnim.setInterpolator(mInterpolator);
290 mColorAnim.start();
Daniel Sandlera953b6d2013-10-11 14:27:53 -0400291 }
292 });
Daniel Sandlera953b6d2013-10-11 14:27:53 -0400293 } else {
294 mColor.setColor(BGCOLOR);
295 }
Daniel Sandlerb83f5c62013-10-13 22:04:36 -0400296
Adrian Roos62b65e42015-02-25 18:05:34 +0100297 mContext.registerReceiver(mReceiver,
298 new IntentFilter(Intent.ACTION_CONFIGURATION_CHANGED));
Daniel Sandlerb83f5c62013-10-13 22:04:36 -0400299 }
300
301 @Override
302 public void onDetachedFromWindow() {
303 mContext.unregisterReceiver(mReceiver);
Daniel Sandlera953b6d2013-10-11 14:27:53 -0400304 }
305
306 @Override
307 public boolean onTouchEvent(MotionEvent motion) {
Daniel Sandlera953b6d2013-10-11 14:27:53 -0400308 return true;
John Spurlock4cf6a942013-08-13 20:02:46 -0400309 }
310 }
John Spurlock34e13d92013-08-10 06:52:28 -0400311
John Spurlockd9b70bd2014-02-06 17:02:44 -0500312 private void handleShow() {
313 if (DEBUG) Slog.d(TAG, "Showing immersive mode confirmation");
John Spurlockd67ec252013-09-05 11:31:54 -0400314
John Spurlockd9b70bd2014-02-06 17:02:44 -0500315 mClingWindow = new ClingWindowView(mContext, mConfirm);
John Spurlock34e13d92013-08-10 06:52:28 -0400316
John Spurlock4cf6a942013-08-13 20:02:46 -0400317 // we will be hiding the nav bar, so layout as if it's already hidden
Daniel Sandlera953b6d2013-10-11 14:27:53 -0400318 mClingWindow.setSystemUiVisibility(
319 View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
320 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
John Spurlock4cf6a942013-08-13 20:02:46 -0400321
322 // show the confirmation
Daniel Sandlerb83f5c62013-10-13 22:04:36 -0400323 WindowManager.LayoutParams lp = getClingWindowLayoutParams();
Daniel Sandlera953b6d2013-10-11 14:27:53 -0400324 mWindowManager.addView(mClingWindow, lp);
John Spurlock34e13d92013-08-10 06:52:28 -0400325 }
326
John Spurlockd9b70bd2014-02-06 17:02:44 -0500327 private final Runnable mConfirm = new Runnable() {
328 @Override
329 public void run() {
330 if (DEBUG) Slog.d(TAG, "mConfirm.run()");
331 if (!mConfirmed) {
332 mConfirmed = true;
333 saveSetting();
John Spurlock34e13d92013-08-10 06:52:28 -0400334 }
John Spurlockd9b70bd2014-02-06 17:02:44 -0500335 handleHide();
336 }
337 };
John Spurlock4cf6a942013-08-13 20:02:46 -0400338
339 private final class H extends Handler {
John Spurlockd9b70bd2014-02-06 17:02:44 -0500340 private static final int SHOW = 1;
341 private static final int HIDE = 2;
John Spurlock4cf6a942013-08-13 20:02:46 -0400342
343 @Override
344 public void handleMessage(Message msg) {
345 switch(msg.what) {
346 case SHOW:
John Spurlockd9b70bd2014-02-06 17:02:44 -0500347 handleShow();
John Spurlock4cf6a942013-08-13 20:02:46 -0400348 break;
349 case HIDE:
350 handleHide();
351 break;
352 }
353 }
354 }
John Spurlock34e13d92013-08-10 06:52:28 -0400355}