John Spurlock | 34e13d9 | 2013-08-10 06:52:28 -0400 | [diff] [blame] | 1 | /* |
| 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 Jaggi | b10e33f | 2015-02-04 21:57:40 +0100 | [diff] [blame] | 17 | package com.android.server.policy; |
John Spurlock | 34e13d9 | 2013-08-10 06:52:28 -0400 | [diff] [blame] | 18 | |
Daniel Sandler | a953b6d | 2013-10-11 14:27:53 -0400 | [diff] [blame] | 19 | import android.animation.ArgbEvaluator; |
| 20 | import android.animation.ValueAnimator; |
| 21 | import android.app.ActivityManager; |
Daniel Sandler | b83f5c6 | 2013-10-13 22:04:36 -0400 | [diff] [blame] | 22 | import android.content.BroadcastReceiver; |
John Spurlock | 34e13d9 | 2013-08-10 06:52:28 -0400 | [diff] [blame] | 23 | import android.content.Context; |
Daniel Sandler | b83f5c6 | 2013-10-13 22:04:36 -0400 | [diff] [blame] | 24 | import android.content.Intent; |
| 25 | import android.content.IntentFilter; |
Daniel Sandler | a953b6d | 2013-10-11 14:27:53 -0400 | [diff] [blame] | 26 | import android.graphics.PixelFormat; |
| 27 | import android.graphics.drawable.ColorDrawable; |
John Spurlock | 34e13d9 | 2013-08-10 06:52:28 -0400 | [diff] [blame] | 28 | import android.os.Handler; |
John Spurlock | 4cf6a94 | 2013-08-13 20:02:46 -0400 | [diff] [blame] | 29 | import android.os.Message; |
John Spurlock | d67ec25 | 2013-09-05 11:31:54 -0400 | [diff] [blame] | 30 | import android.os.UserHandle; |
| 31 | import android.provider.Settings; |
Daniel Sandler | a953b6d | 2013-10-11 14:27:53 -0400 | [diff] [blame] | 32 | import android.util.DisplayMetrics; |
John Spurlock | 34e13d9 | 2013-08-10 06:52:28 -0400 | [diff] [blame] | 33 | import android.util.Slog; |
John Spurlock | d9b70bd | 2014-02-06 17:02:44 -0500 | [diff] [blame] | 34 | import android.util.SparseBooleanArray; |
Daniel Sandler | a953b6d | 2013-10-11 14:27:53 -0400 | [diff] [blame] | 35 | import android.view.Gravity; |
| 36 | import android.view.MotionEvent; |
John Spurlock | 34e13d9 | 2013-08-10 06:52:28 -0400 | [diff] [blame] | 37 | import android.view.View; |
Daniel Sandler | a953b6d | 2013-10-11 14:27:53 -0400 | [diff] [blame] | 38 | import android.view.ViewGroup; |
Adrian Roos | 3e8dd51 | 2015-05-21 13:27:58 -0700 | [diff] [blame] | 39 | import android.view.ViewTreeObserver; |
Daniel Sandler | a953b6d | 2013-10-11 14:27:53 -0400 | [diff] [blame] | 40 | import android.view.WindowManager; |
John Spurlock | 4cf6a94 | 2013-08-13 20:02:46 -0400 | [diff] [blame] | 41 | import android.view.animation.Animation; |
John Spurlock | 4cf6a94 | 2013-08-13 20:02:46 -0400 | [diff] [blame] | 42 | import android.view.animation.AnimationUtils; |
Adrian Roos | 62b65e4 | 2015-02-25 18:05:34 +0100 | [diff] [blame] | 43 | import android.view.animation.Interpolator; |
Daniel Sandler | a953b6d | 2013-10-11 14:27:53 -0400 | [diff] [blame] | 44 | import android.widget.Button; |
| 45 | import android.widget.FrameLayout; |
John Spurlock | 34e13d9 | 2013-08-10 06:52:28 -0400 | [diff] [blame] | 46 | |
| 47 | import com.android.internal.R; |
| 48 | |
| 49 | /** |
John Spurlock | f1a3664 | 2013-10-12 17:50:42 -0400 | [diff] [blame] | 50 | * Helper to manage showing/hiding a confirmation prompt when the navigation bar is hidden |
| 51 | * entering immersive mode. |
John Spurlock | 34e13d9 | 2013-08-10 06:52:28 -0400 | [diff] [blame] | 52 | */ |
John Spurlock | f1a3664 | 2013-10-12 17:50:42 -0400 | [diff] [blame] | 53 | public class ImmersiveModeConfirmation { |
| 54 | private static final String TAG = "ImmersiveModeConfirmation"; |
John Spurlock | 4cf6a94 | 2013-08-13 20:02:46 -0400 | [diff] [blame] | 55 | private static final boolean DEBUG = false; |
Daniel Sandler | a953b6d | 2013-10-11 14:27:53 -0400 | [diff] [blame] | 56 | private static final boolean DEBUG_SHOW_EVERY_TIME = false; // super annoying, use with caution |
John Spurlock | d9b70bd | 2014-02-06 17:02:44 -0500 | [diff] [blame] | 57 | private static final String CONFIRMED = "confirmed"; |
John Spurlock | 34e13d9 | 2013-08-10 06:52:28 -0400 | [diff] [blame] | 58 | |
| 59 | private final Context mContext; |
John Spurlock | 4cf6a94 | 2013-08-13 20:02:46 -0400 | [diff] [blame] | 60 | private final H mHandler; |
John Spurlock | 4cf6a94 | 2013-08-13 20:02:46 -0400 | [diff] [blame] | 61 | private final long mShowDelayMs; |
John Spurlock | d67ec25 | 2013-09-05 11:31:54 -0400 | [diff] [blame] | 62 | private final long mPanicThresholdMs; |
John Spurlock | 34e13d9 | 2013-08-10 06:52:28 -0400 | [diff] [blame] | 63 | |
John Spurlock | d9b70bd | 2014-02-06 17:02:44 -0500 | [diff] [blame] | 64 | private boolean mConfirmed; |
Daniel Sandler | a953b6d | 2013-10-11 14:27:53 -0400 | [diff] [blame] | 65 | private ClingWindowView mClingWindow; |
John Spurlock | d67ec25 | 2013-09-05 11:31:54 -0400 | [diff] [blame] | 66 | private long mPanicTime; |
Daniel Sandler | a953b6d | 2013-10-11 14:27:53 -0400 | [diff] [blame] | 67 | private WindowManager mWindowManager; |
John Spurlock | d9b70bd | 2014-02-06 17:02:44 -0500 | [diff] [blame] | 68 | private int mCurrentUserId; |
John Spurlock | 34e13d9 | 2013-08-10 06:52:28 -0400 | [diff] [blame] | 69 | |
John Spurlock | f1a3664 | 2013-10-12 17:50:42 -0400 | [diff] [blame] | 70 | public ImmersiveModeConfirmation(Context context) { |
John Spurlock | 34e13d9 | 2013-08-10 06:52:28 -0400 | [diff] [blame] | 71 | mContext = context; |
John Spurlock | 4cf6a94 | 2013-08-13 20:02:46 -0400 | [diff] [blame] | 72 | mHandler = new H(); |
| 73 | mShowDelayMs = getNavBarExitDuration() * 3; |
John Spurlock | d67ec25 | 2013-09-05 11:31:54 -0400 | [diff] [blame] | 74 | mPanicThresholdMs = context.getResources() |
John Spurlock | f1a3664 | 2013-10-12 17:50:42 -0400 | [diff] [blame] | 75 | .getInteger(R.integer.config_immersive_mode_confirmation_panic); |
Daniel Sandler | a953b6d | 2013-10-11 14:27:53 -0400 | [diff] [blame] | 76 | mWindowManager = (WindowManager) |
| 77 | mContext.getSystemService(Context.WINDOW_SERVICE); |
John Spurlock | 4cf6a94 | 2013-08-13 20:02:46 -0400 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | private long getNavBarExitDuration() { |
| 81 | Animation exit = AnimationUtils.loadAnimation(mContext, R.anim.dock_bottom_exit); |
| 82 | return exit != null ? exit.getDuration() : 0; |
John Spurlock | 34e13d9 | 2013-08-10 06:52:28 -0400 | [diff] [blame] | 83 | } |
| 84 | |
John Spurlock | 4355a53 | 2014-02-19 09:49:25 -0500 | [diff] [blame] | 85 | public void loadSetting(int currentUserId) { |
John Spurlock | d9b70bd | 2014-02-06 17:02:44 -0500 | [diff] [blame] | 86 | mConfirmed = false; |
John Spurlock | 4355a53 | 2014-02-19 09:49:25 -0500 | [diff] [blame] | 87 | mCurrentUserId = currentUserId; |
Adrian Roos | c5d8fbc | 2015-05-21 17:20:46 -0700 | [diff] [blame] | 88 | if (DEBUG) Slog.d(TAG, String.format("loadSetting() mCurrentUserId=%d", mCurrentUserId)); |
John Spurlock | d9b70bd | 2014-02-06 17:02:44 -0500 | [diff] [blame] | 89 | String value = null; |
John Spurlock | d67ec25 | 2013-09-05 11:31:54 -0400 | [diff] [blame] | 90 | try { |
John Spurlock | d9b70bd | 2014-02-06 17:02:44 -0500 | [diff] [blame] | 91 | value = Settings.Secure.getStringForUser(mContext.getContentResolver(), |
John Spurlock | f1a3664 | 2013-10-12 17:50:42 -0400 | [diff] [blame] | 92 | Settings.Secure.IMMERSIVE_MODE_CONFIRMATIONS, |
John Spurlock | d67ec25 | 2013-09-05 11:31:54 -0400 | [diff] [blame] | 93 | UserHandle.USER_CURRENT); |
John Spurlock | d9b70bd | 2014-02-06 17:02:44 -0500 | [diff] [blame] | 94 | mConfirmed = CONFIRMED.equals(value); |
| 95 | if (DEBUG) Slog.d(TAG, "Loaded mConfirmed=" + mConfirmed); |
John Spurlock | d67ec25 | 2013-09-05 11:31:54 -0400 | [diff] [blame] | 96 | } catch (Throwable t) { |
John Spurlock | d9b70bd | 2014-02-06 17:02:44 -0500 | [diff] [blame] | 97 | Slog.w(TAG, "Error loading confirmations, value=" + value, t); |
John Spurlock | d67ec25 | 2013-09-05 11:31:54 -0400 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
| 101 | private void saveSetting() { |
| 102 | if (DEBUG) Slog.d(TAG, "saveSetting()"); |
| 103 | try { |
John Spurlock | d9b70bd | 2014-02-06 17:02:44 -0500 | [diff] [blame] | 104 | final String value = mConfirmed ? CONFIRMED : null; |
John Spurlock | d67ec25 | 2013-09-05 11:31:54 -0400 | [diff] [blame] | 105 | Settings.Secure.putStringForUser(mContext.getContentResolver(), |
John Spurlock | f1a3664 | 2013-10-12 17:50:42 -0400 | [diff] [blame] | 106 | Settings.Secure.IMMERSIVE_MODE_CONFIRMATIONS, |
John Spurlock | d9b70bd | 2014-02-06 17:02:44 -0500 | [diff] [blame] | 107 | value, |
John Spurlock | d67ec25 | 2013-09-05 11:31:54 -0400 | [diff] [blame] | 108 | UserHandle.USER_CURRENT); |
John Spurlock | d9b70bd | 2014-02-06 17:02:44 -0500 | [diff] [blame] | 109 | if (DEBUG) Slog.d(TAG, "Saved value=" + value); |
John Spurlock | d67ec25 | 2013-09-05 11:31:54 -0400 | [diff] [blame] | 110 | } catch (Throwable t) { |
John Spurlock | d9b70bd | 2014-02-06 17:02:44 -0500 | [diff] [blame] | 111 | Slog.w(TAG, "Error saving confirmations, mConfirmed=" + mConfirmed, t); |
John Spurlock | d67ec25 | 2013-09-05 11:31:54 -0400 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
Maurice Lam | 99c6e07 | 2014-04-28 18:24:28 -0700 | [diff] [blame] | 115 | public void immersiveModeChanged(String pkg, boolean isImmersiveMode, |
| 116 | boolean userSetupComplete) { |
John Spurlock | 4cf6a94 | 2013-08-13 20:02:46 -0400 | [diff] [blame] | 117 | mHandler.removeMessages(H.SHOW); |
John Spurlock | f1a3664 | 2013-10-12 17:50:42 -0400 | [diff] [blame] | 118 | if (isImmersiveMode) { |
John Spurlock | d9b70bd | 2014-02-06 17:02:44 -0500 | [diff] [blame] | 119 | final boolean disabled = PolicyControl.disableImmersiveConfirmation(pkg); |
| 120 | if (DEBUG) Slog.d(TAG, String.format("immersiveModeChanged() disabled=%s mConfirmed=%s", |
| 121 | disabled, mConfirmed)); |
Maurice Lam | 99c6e07 | 2014-04-28 18:24:28 -0700 | [diff] [blame] | 122 | if (!disabled && (DEBUG_SHOW_EVERY_TIME || !mConfirmed) && userSetupComplete) { |
John Spurlock | d9b70bd | 2014-02-06 17:02:44 -0500 | [diff] [blame] | 123 | mHandler.sendEmptyMessageDelayed(H.SHOW, mShowDelayMs); |
John Spurlock | 34e13d9 | 2013-08-10 06:52:28 -0400 | [diff] [blame] | 124 | } |
| 125 | } else { |
John Spurlock | 4cf6a94 | 2013-08-13 20:02:46 -0400 | [diff] [blame] | 126 | mHandler.sendEmptyMessage(H.HIDE); |
John Spurlock | 34e13d9 | 2013-08-10 06:52:28 -0400 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
John Spurlock | d9b70bd | 2014-02-06 17:02:44 -0500 | [diff] [blame] | 130 | public boolean onPowerKeyDown(boolean isScreenOn, long time, boolean inImmersiveMode) { |
| 131 | if (!isScreenOn && (time - mPanicTime < mPanicThresholdMs)) { |
John Spurlock | d67ec25 | 2013-09-05 11:31:54 -0400 | [diff] [blame] | 132 | // turning the screen back on within the panic threshold |
John Spurlock | d9b70bd | 2014-02-06 17:02:44 -0500 | [diff] [blame] | 133 | return mClingWindow == null; |
John Spurlock | d67ec25 | 2013-09-05 11:31:54 -0400 | [diff] [blame] | 134 | } |
John Spurlock | f1a3664 | 2013-10-12 17:50:42 -0400 | [diff] [blame] | 135 | if (isScreenOn && inImmersiveMode) { |
| 136 | // turning the screen off, remember if we were in immersive mode |
John Spurlock | d67ec25 | 2013-09-05 11:31:54 -0400 | [diff] [blame] | 137 | mPanicTime = time; |
John Spurlock | d67ec25 | 2013-09-05 11:31:54 -0400 | [diff] [blame] | 138 | } else { |
| 139 | mPanicTime = 0; |
John Spurlock | d67ec25 | 2013-09-05 11:31:54 -0400 | [diff] [blame] | 140 | } |
John Spurlock | d9b70bd | 2014-02-06 17:02:44 -0500 | [diff] [blame] | 141 | return false; |
John Spurlock | d67ec25 | 2013-09-05 11:31:54 -0400 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | public void confirmCurrentPrompt() { |
John Spurlock | d9b70bd | 2014-02-06 17:02:44 -0500 | [diff] [blame] | 145 | if (mClingWindow != null) { |
| 146 | if (DEBUG) Slog.d(TAG, "confirmCurrentPrompt()"); |
| 147 | mHandler.post(mConfirm); |
| 148 | } |
John Spurlock | d67ec25 | 2013-09-05 11:31:54 -0400 | [diff] [blame] | 149 | } |
| 150 | |
John Spurlock | 4cf6a94 | 2013-08-13 20:02:46 -0400 | [diff] [blame] | 151 | private void handleHide() { |
Daniel Sandler | a953b6d | 2013-10-11 14:27:53 -0400 | [diff] [blame] | 152 | if (mClingWindow != null) { |
John Spurlock | d9b70bd | 2014-02-06 17:02:44 -0500 | [diff] [blame] | 153 | if (DEBUG) Slog.d(TAG, "Hiding immersive mode confirmation"); |
Daniel Sandler | a953b6d | 2013-10-11 14:27:53 -0400 | [diff] [blame] | 154 | mWindowManager.removeView(mClingWindow); |
| 155 | mClingWindow = null; |
| 156 | } |
| 157 | } |
| 158 | |
Daniel Sandler | b83f5c6 | 2013-10-13 22:04:36 -0400 | [diff] [blame] | 159 | public WindowManager.LayoutParams getClingWindowLayoutParams() { |
| 160 | final WindowManager.LayoutParams lp = new WindowManager.LayoutParams( |
| 161 | ViewGroup.LayoutParams.MATCH_PARENT, |
| 162 | ViewGroup.LayoutParams.MATCH_PARENT, |
Adrian Roos | 62b65e4 | 2015-02-25 18:05:34 +0100 | [diff] [blame] | 163 | WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL, |
Daniel Sandler | b83f5c6 | 2013-10-13 22:04:36 -0400 | [diff] [blame] | 164 | 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 Spurlock | 0513d5a | 2013-12-09 11:59:45 -0500 | [diff] [blame] | 170 | lp.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS; |
John Spurlock | f1a3664 | 2013-10-12 17:50:42 -0400 | [diff] [blame] | 171 | lp.setTitle("ImmersiveModeConfirmation"); |
Adrian Roos | 62b65e4 | 2015-02-25 18:05:34 +0100 | [diff] [blame] | 172 | lp.windowAnimations = com.android.internal.R.style.Animation_ImmersiveModeConfirmation; |
Daniel Sandler | b83f5c6 | 2013-10-13 22:04:36 -0400 | [diff] [blame] | 173 | 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 Sandler | a953b6d | 2013-10-11 14:27:53 -0400 | [diff] [blame] | 184 | private class ClingWindowView extends FrameLayout { |
| 185 | private static final int BGCOLOR = 0x80000000; |
Adrian Roos | 62b65e4 | 2015-02-25 18:05:34 +0100 | [diff] [blame] | 186 | private static final int OFFSET_DP = 96; |
| 187 | private static final int ANIMATION_DURATION = 250; |
Daniel Sandler | a953b6d | 2013-10-11 14:27:53 -0400 | [diff] [blame] | 188 | |
John Spurlock | 47e3de2 | 2013-10-13 19:20:04 -0400 | [diff] [blame] | 189 | private final Runnable mConfirm; |
Daniel Sandler | a953b6d | 2013-10-11 14:27:53 -0400 | [diff] [blame] | 190 | private final ColorDrawable mColor = new ColorDrawable(0); |
Adrian Roos | 62b65e4 | 2015-02-25 18:05:34 +0100 | [diff] [blame] | 191 | private final Interpolator mInterpolator; |
Daniel Sandler | a953b6d | 2013-10-11 14:27:53 -0400 | [diff] [blame] | 192 | private ValueAnimator mColorAnim; |
Daniel Sandler | b83f5c6 | 2013-10-13 22:04:36 -0400 | [diff] [blame] | 193 | private ViewGroup mClingLayout; |
| 194 | |
Daniel Sandler | da35dde | 2013-10-18 12:05:04 -0400 | [diff] [blame] | 195 | 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 Roos | 3e8dd51 | 2015-05-21 13:27:58 -0700 | [diff] [blame] | 204 | 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 Sandler | b83f5c6 | 2013-10-13 22:04:36 -0400 | [diff] [blame] | 223 | 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 Sandler | da35dde | 2013-10-18 12:05:04 -0400 | [diff] [blame] | 227 | post(mUpdateLayoutRunnable); |
Daniel Sandler | b83f5c6 | 2013-10-13 22:04:36 -0400 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | }; |
Daniel Sandler | a953b6d | 2013-10-11 14:27:53 -0400 | [diff] [blame] | 231 | |
John Spurlock | 47e3de2 | 2013-10-13 19:20:04 -0400 | [diff] [blame] | 232 | public ClingWindowView(Context context, Runnable confirm) { |
Daniel Sandler | a953b6d | 2013-10-11 14:27:53 -0400 | [diff] [blame] | 233 | super(context); |
John Spurlock | 47e3de2 | 2013-10-13 19:20:04 -0400 | [diff] [blame] | 234 | mConfirm = confirm; |
Daniel Sandler | a953b6d | 2013-10-11 14:27:53 -0400 | [diff] [blame] | 235 | setBackground(mColor); |
Adrian Roos | 62b65e4 | 2015-02-25 18:05:34 +0100 | [diff] [blame] | 236 | setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO); |
| 237 | mInterpolator = AnimationUtils |
| 238 | .loadInterpolator(mContext, android.R.interpolator.linear_out_slow_in); |
Daniel Sandler | a953b6d | 2013-10-11 14:27:53 -0400 | [diff] [blame] | 239 | } |
| 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 Roos | 3e8dd51 | 2015-05-21 13:27:58 -0700 | [diff] [blame] | 249 | getViewTreeObserver().addOnComputeInternalInsetsListener(mInsetsListener); |
| 250 | |
Daniel Sandler | a953b6d | 2013-10-11 14:27:53 -0400 | [diff] [blame] | 251 | // create the confirmation cling |
Daniel Sandler | b83f5c6 | 2013-10-13 22:04:36 -0400 | [diff] [blame] | 252 | mClingLayout = (ViewGroup) |
John Spurlock | f1a3664 | 2013-10-12 17:50:42 -0400 | [diff] [blame] | 253 | View.inflate(getContext(), R.layout.immersive_mode_cling, null); |
Daniel Sandler | a953b6d | 2013-10-11 14:27:53 -0400 | [diff] [blame] | 254 | |
Daniel Sandler | b83f5c6 | 2013-10-13 22:04:36 -0400 | [diff] [blame] | 255 | final Button ok = (Button) mClingLayout.findViewById(R.id.ok); |
Daniel Sandler | a953b6d | 2013-10-11 14:27:53 -0400 | [diff] [blame] | 256 | ok.setOnClickListener(new OnClickListener() { |
| 257 | @Override |
| 258 | public void onClick(View v) { |
John Spurlock | 47e3de2 | 2013-10-13 19:20:04 -0400 | [diff] [blame] | 259 | mConfirm.run(); |
Daniel Sandler | a953b6d | 2013-10-11 14:27:53 -0400 | [diff] [blame] | 260 | } |
| 261 | }); |
Daniel Sandler | b83f5c6 | 2013-10-13 22:04:36 -0400 | [diff] [blame] | 262 | addView(mClingLayout, getBubbleLayoutParams()); |
Daniel Sandler | a953b6d | 2013-10-11 14:27:53 -0400 | [diff] [blame] | 263 | |
| 264 | if (ActivityManager.isHighEndGfx()) { |
Adrian Roos | 62b65e4 | 2015-02-25 18:05:34 +0100 | [diff] [blame] | 265 | final View cling = mClingLayout; |
| 266 | cling.setAlpha(0f); |
| 267 | cling.setTranslationY(-OFFSET_DP * density); |
Daniel Sandler | a953b6d | 2013-10-11 14:27:53 -0400 | [diff] [blame] | 268 | |
Adrian Roos | 62b65e4 | 2015-02-25 18:05:34 +0100 | [diff] [blame] | 269 | postOnAnimation(new Runnable() { |
Daniel Sandler | a953b6d | 2013-10-11 14:27:53 -0400 | [diff] [blame] | 270 | @Override |
Adrian Roos | 62b65e4 | 2015-02-25 18:05:34 +0100 | [diff] [blame] | 271 | 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 Sandler | a953b6d | 2013-10-11 14:27:53 -0400 | [diff] [blame] | 291 | } |
| 292 | }); |
Daniel Sandler | a953b6d | 2013-10-11 14:27:53 -0400 | [diff] [blame] | 293 | } else { |
| 294 | mColor.setColor(BGCOLOR); |
| 295 | } |
Daniel Sandler | b83f5c6 | 2013-10-13 22:04:36 -0400 | [diff] [blame] | 296 | |
Adrian Roos | 62b65e4 | 2015-02-25 18:05:34 +0100 | [diff] [blame] | 297 | mContext.registerReceiver(mReceiver, |
| 298 | new IntentFilter(Intent.ACTION_CONFIGURATION_CHANGED)); |
Daniel Sandler | b83f5c6 | 2013-10-13 22:04:36 -0400 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | @Override |
| 302 | public void onDetachedFromWindow() { |
| 303 | mContext.unregisterReceiver(mReceiver); |
Daniel Sandler | a953b6d | 2013-10-11 14:27:53 -0400 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | @Override |
| 307 | public boolean onTouchEvent(MotionEvent motion) { |
Daniel Sandler | a953b6d | 2013-10-11 14:27:53 -0400 | [diff] [blame] | 308 | return true; |
John Spurlock | 4cf6a94 | 2013-08-13 20:02:46 -0400 | [diff] [blame] | 309 | } |
| 310 | } |
John Spurlock | 34e13d9 | 2013-08-10 06:52:28 -0400 | [diff] [blame] | 311 | |
John Spurlock | d9b70bd | 2014-02-06 17:02:44 -0500 | [diff] [blame] | 312 | private void handleShow() { |
| 313 | if (DEBUG) Slog.d(TAG, "Showing immersive mode confirmation"); |
John Spurlock | d67ec25 | 2013-09-05 11:31:54 -0400 | [diff] [blame] | 314 | |
John Spurlock | d9b70bd | 2014-02-06 17:02:44 -0500 | [diff] [blame] | 315 | mClingWindow = new ClingWindowView(mContext, mConfirm); |
John Spurlock | 34e13d9 | 2013-08-10 06:52:28 -0400 | [diff] [blame] | 316 | |
John Spurlock | 4cf6a94 | 2013-08-13 20:02:46 -0400 | [diff] [blame] | 317 | // we will be hiding the nav bar, so layout as if it's already hidden |
Daniel Sandler | a953b6d | 2013-10-11 14:27:53 -0400 | [diff] [blame] | 318 | mClingWindow.setSystemUiVisibility( |
| 319 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
| 320 | | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); |
John Spurlock | 4cf6a94 | 2013-08-13 20:02:46 -0400 | [diff] [blame] | 321 | |
| 322 | // show the confirmation |
Daniel Sandler | b83f5c6 | 2013-10-13 22:04:36 -0400 | [diff] [blame] | 323 | WindowManager.LayoutParams lp = getClingWindowLayoutParams(); |
Daniel Sandler | a953b6d | 2013-10-11 14:27:53 -0400 | [diff] [blame] | 324 | mWindowManager.addView(mClingWindow, lp); |
John Spurlock | 34e13d9 | 2013-08-10 06:52:28 -0400 | [diff] [blame] | 325 | } |
| 326 | |
John Spurlock | d9b70bd | 2014-02-06 17:02:44 -0500 | [diff] [blame] | 327 | 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 Spurlock | 34e13d9 | 2013-08-10 06:52:28 -0400 | [diff] [blame] | 334 | } |
John Spurlock | d9b70bd | 2014-02-06 17:02:44 -0500 | [diff] [blame] | 335 | handleHide(); |
| 336 | } |
| 337 | }; |
John Spurlock | 4cf6a94 | 2013-08-13 20:02:46 -0400 | [diff] [blame] | 338 | |
| 339 | private final class H extends Handler { |
John Spurlock | d9b70bd | 2014-02-06 17:02:44 -0500 | [diff] [blame] | 340 | private static final int SHOW = 1; |
| 341 | private static final int HIDE = 2; |
John Spurlock | 4cf6a94 | 2013-08-13 20:02:46 -0400 | [diff] [blame] | 342 | |
| 343 | @Override |
| 344 | public void handleMessage(Message msg) { |
| 345 | switch(msg.what) { |
| 346 | case SHOW: |
John Spurlock | d9b70bd | 2014-02-06 17:02:44 -0500 | [diff] [blame] | 347 | handleShow(); |
John Spurlock | 4cf6a94 | 2013-08-13 20:02:46 -0400 | [diff] [blame] | 348 | break; |
| 349 | case HIDE: |
| 350 | handleHide(); |
| 351 | break; |
| 352 | } |
| 353 | } |
| 354 | } |
John Spurlock | 34e13d9 | 2013-08-10 06:52:28 -0400 | [diff] [blame] | 355 | } |