Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | |
| 17 | package com.android.internal.policy; |
| 18 | |
| 19 | import com.android.internal.R; |
Alan Viverette | 77fb85e | 2015-12-14 11:42:44 -0500 | [diff] [blame] | 20 | import com.android.internal.policy.PhoneWindow.PhoneWindowMenuCallback; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 21 | import com.android.internal.view.FloatingActionMode; |
| 22 | import com.android.internal.view.RootViewSurfaceTaker; |
| 23 | import com.android.internal.view.StandaloneActionMode; |
| 24 | import com.android.internal.view.menu.ContextMenuBuilder; |
Alan Viverette | 021627e | 2015-11-25 14:22:00 -0500 | [diff] [blame] | 25 | import com.android.internal.view.menu.MenuHelper; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 26 | import com.android.internal.widget.ActionBarContextView; |
| 27 | import com.android.internal.widget.BackgroundFallback; |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 28 | import com.android.internal.widget.DecorCaptionView; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 29 | import com.android.internal.widget.FloatingToolbar; |
| 30 | |
| 31 | import android.animation.Animator; |
| 32 | import android.animation.ObjectAnimator; |
| 33 | import android.app.ActivityManager; |
| 34 | import android.content.Context; |
| 35 | import android.content.res.Resources; |
| 36 | import android.graphics.Canvas; |
| 37 | import android.graphics.Color; |
| 38 | import android.graphics.PixelFormat; |
| 39 | import android.graphics.Rect; |
| 40 | import android.graphics.drawable.Drawable; |
| 41 | import android.os.Build; |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 42 | import android.os.RemoteException; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 43 | import android.util.DisplayMetrics; |
| 44 | import android.util.Log; |
| 45 | import android.util.TypedValue; |
| 46 | import android.view.ActionMode; |
| 47 | import android.view.ContextThemeWrapper; |
| 48 | import android.view.Gravity; |
| 49 | import android.view.InputQueue; |
| 50 | import android.view.KeyEvent; |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 51 | import android.view.LayoutInflater; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 52 | import android.view.Menu; |
| 53 | import android.view.MenuItem; |
| 54 | import android.view.MotionEvent; |
Wale Ogunwale | bf9eefc | 2015-11-17 14:47:52 -0800 | [diff] [blame] | 55 | import android.view.ThreadedRenderer; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 56 | import android.view.View; |
| 57 | import android.view.ViewGroup; |
| 58 | import android.view.ViewStub; |
| 59 | import android.view.ViewTreeObserver; |
| 60 | import android.view.Window; |
Wale Ogunwale | bf9eefc | 2015-11-17 14:47:52 -0800 | [diff] [blame] | 61 | import android.view.WindowCallbacks; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 62 | import android.view.WindowInsets; |
| 63 | import android.view.WindowManager; |
| 64 | import android.view.accessibility.AccessibilityEvent; |
| 65 | import android.view.accessibility.AccessibilityManager; |
| 66 | import android.view.animation.AnimationUtils; |
| 67 | import android.view.animation.Interpolator; |
| 68 | import android.widget.FrameLayout; |
| 69 | import android.widget.PopupWindow; |
| 70 | |
Wale Ogunwale | eb6722c | 2015-12-08 11:43:43 -0800 | [diff] [blame] | 71 | import static android.app.ActivityManager.StackId; |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 72 | import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID; |
| 73 | import static android.app.ActivityManager.StackId.INVALID_STACK_ID; |
Filip Gruszczynski | 34dab0b | 2015-12-22 08:29:07 -0800 | [diff] [blame] | 74 | import static android.content.res.Configuration.ORIENTATION_PORTRAIT; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 75 | import static android.view.View.MeasureSpec.AT_MOST; |
| 76 | import static android.view.View.MeasureSpec.EXACTLY; |
| 77 | import static android.view.View.MeasureSpec.getMode; |
| 78 | import static android.view.ViewGroup.LayoutParams.MATCH_PARENT; |
| 79 | import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT; |
Filip Gruszczynski | 3dec081 | 2015-12-09 08:42:41 -0800 | [diff] [blame] | 80 | import static android.view.Window.DECOR_CAPTION_SHADE_DARK; |
| 81 | import static android.view.Window.DECOR_CAPTION_SHADE_LIGHT; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 82 | import static android.view.WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS; |
| 83 | import static android.view.WindowManager.LayoutParams.FLAG_FULLSCREEN; |
| 84 | import static android.view.WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION; |
| 85 | import static android.view.WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS; |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 86 | import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION; |
| 87 | import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 88 | |
Wale Ogunwale | bf9eefc | 2015-11-17 14:47:52 -0800 | [diff] [blame] | 89 | /** @hide */ |
| 90 | public class DecorView extends FrameLayout implements RootViewSurfaceTaker, WindowCallbacks { |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 91 | private static final String TAG = "DecorView"; |
| 92 | |
| 93 | private static final boolean SWEEP_OPEN_MENU = false; |
| 94 | |
Wale Ogunwale | 2b547c3 | 2015-11-18 10:33:22 -0800 | [diff] [blame] | 95 | // The height of a window which has focus in DIP. |
| 96 | private final static int DECOR_SHADOW_FOCUSED_HEIGHT_IN_DIP = 20; |
| 97 | // The height of a window which has not in DIP. |
| 98 | private final static int DECOR_SHADOW_UNFOCUSED_HEIGHT_IN_DIP = 5; |
| 99 | |
| 100 | // Cludge to address b/22668382: Set the shadow size to the maximum so that the layer |
| 101 | // size calculation takes the shadow size into account. We set the elevation currently |
| 102 | // to max until the first layout command has been executed. |
| 103 | private boolean mAllowUpdateElevation = false; |
| 104 | |
| 105 | private boolean mElevationAdjustedForStack = false; |
| 106 | |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 107 | int mDefaultOpacity = PixelFormat.OPAQUE; |
| 108 | |
| 109 | /** The feature ID of the panel, or -1 if this is the application's DecorView */ |
| 110 | private final int mFeatureId; |
| 111 | |
| 112 | private final Rect mDrawingBounds = new Rect(); |
| 113 | |
| 114 | private final Rect mBackgroundPadding = new Rect(); |
| 115 | |
| 116 | private final Rect mFramePadding = new Rect(); |
| 117 | |
| 118 | private final Rect mFrameOffsets = new Rect(); |
| 119 | |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 120 | private boolean mHasCaption = false; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 121 | |
| 122 | private boolean mChanging; |
| 123 | |
| 124 | private Drawable mMenuBackground; |
| 125 | private boolean mWatchingForMenu; |
| 126 | private int mDownY; |
| 127 | |
| 128 | ActionMode mPrimaryActionMode; |
| 129 | private ActionMode mFloatingActionMode; |
| 130 | private ActionBarContextView mPrimaryActionModeView; |
| 131 | private PopupWindow mPrimaryActionModePopup; |
| 132 | private Runnable mShowPrimaryActionModePopup; |
| 133 | private ViewTreeObserver.OnPreDrawListener mFloatingToolbarPreDrawListener; |
| 134 | private View mFloatingActionModeOriginatingView; |
| 135 | private FloatingToolbar mFloatingToolbar; |
| 136 | private ObjectAnimator mFadeAnim; |
| 137 | |
| 138 | // View added at runtime to draw under the status bar area |
| 139 | private View mStatusGuard; |
| 140 | // View added at runtime to draw under the navigation bar area |
| 141 | private View mNavigationGuard; |
| 142 | |
| 143 | private final ColorViewState mStatusColorViewState = new ColorViewState( |
| 144 | SYSTEM_UI_FLAG_FULLSCREEN, FLAG_TRANSLUCENT_STATUS, |
| 145 | Gravity.TOP, Gravity.LEFT, |
| 146 | Window.STATUS_BAR_BACKGROUND_TRANSITION_NAME, |
| 147 | com.android.internal.R.id.statusBarBackground, |
| 148 | FLAG_FULLSCREEN); |
| 149 | private final ColorViewState mNavigationColorViewState = new ColorViewState( |
| 150 | SYSTEM_UI_FLAG_HIDE_NAVIGATION, FLAG_TRANSLUCENT_NAVIGATION, |
| 151 | Gravity.BOTTOM, Gravity.RIGHT, |
| 152 | Window.NAVIGATION_BAR_BACKGROUND_TRANSITION_NAME, |
| 153 | com.android.internal.R.id.navigationBarBackground, |
| 154 | 0 /* hideWindowFlag */); |
| 155 | |
| 156 | private final Interpolator mShowInterpolator; |
| 157 | private final Interpolator mHideInterpolator; |
| 158 | private final int mBarEnterExitDuration; |
Jorim Jaggi | 4fa7892 | 2015-11-30 17:13:56 -0800 | [diff] [blame] | 159 | private final boolean mForceWindowDrawsStatusBarBackground; |
| 160 | private final int mSemiTransparentStatusBarColor; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 161 | |
| 162 | private final BackgroundFallback mBackgroundFallback = new BackgroundFallback(); |
| 163 | |
| 164 | private int mLastTopInset = 0; |
| 165 | private int mLastBottomInset = 0; |
| 166 | private int mLastRightInset = 0; |
| 167 | private boolean mLastHasTopStableInset = false; |
| 168 | private boolean mLastHasBottomStableInset = false; |
| 169 | private boolean mLastHasRightStableInset = false; |
| 170 | private int mLastWindowFlags = 0; |
| 171 | |
| 172 | private int mRootScrollY = 0; |
| 173 | |
| 174 | private PhoneWindow mWindow; |
| 175 | |
| 176 | ViewGroup mContentRoot; |
| 177 | |
| 178 | private Rect mTempRect; |
| 179 | private Rect mOutsets = new Rect(); |
| 180 | |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 181 | // This is the caption view for the window, containing the caption and window control |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 182 | // buttons. The visibility of this decor depends on the workspace and the window type. |
| 183 | // If the window type does not require such a view, this member might be null. |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 184 | DecorCaptionView mDecorCaptionView; |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 185 | |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 186 | // Stack window is currently in. Since querying and changing the stack is expensive, |
| 187 | // this is the stack value the window is currently set up for. |
| 188 | int mStackId; |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 189 | |
Wale Ogunwale | bf9eefc | 2015-11-17 14:47:52 -0800 | [diff] [blame] | 190 | private boolean mWindowResizeCallbacksAdded = false; |
| 191 | |
Filip Gruszczynski | 3dec081 | 2015-12-09 08:42:41 -0800 | [diff] [blame] | 192 | private BackdropFrameRenderer mBackdropFrameRenderer = null; |
Wale Ogunwale | bf9eefc | 2015-11-17 14:47:52 -0800 | [diff] [blame] | 193 | private Drawable mResizingBackgroundDrawable; |
| 194 | private Drawable mCaptionBackgroundDrawable; |
Filip Gruszczynski | 3dec081 | 2015-12-09 08:42:41 -0800 | [diff] [blame] | 195 | private Drawable mUserCaptionBackgroundDrawable; |
Wale Ogunwale | bf9eefc | 2015-11-17 14:47:52 -0800 | [diff] [blame] | 196 | |
Filip Gruszczynski | 34dab0b | 2015-12-22 08:29:07 -0800 | [diff] [blame] | 197 | private float mAvailableWidth; |
| 198 | |
| 199 | String mLogTag = TAG; |
| 200 | |
| 201 | DecorView(Context context, int featureId, PhoneWindow window, |
| 202 | WindowManager.LayoutParams params) { |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 203 | super(context); |
| 204 | mFeatureId = featureId; |
| 205 | |
| 206 | mShowInterpolator = AnimationUtils.loadInterpolator(context, |
| 207 | android.R.interpolator.linear_out_slow_in); |
| 208 | mHideInterpolator = AnimationUtils.loadInterpolator(context, |
| 209 | android.R.interpolator.fast_out_linear_in); |
| 210 | |
| 211 | mBarEnterExitDuration = context.getResources().getInteger( |
| 212 | R.integer.dock_enter_exit_duration); |
Jorim Jaggi | 4fa7892 | 2015-11-30 17:13:56 -0800 | [diff] [blame] | 213 | mForceWindowDrawsStatusBarBackground = context.getResources().getBoolean( |
| 214 | R.bool.config_forceWindowDrawsStatusBarBackground); |
| 215 | mSemiTransparentStatusBarColor = context.getResources().getColor( |
| 216 | R.color.system_bar_background_semi_transparent, null /* theme */); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 217 | |
Filip Gruszczynski | 34dab0b | 2015-12-22 08:29:07 -0800 | [diff] [blame] | 218 | updateAvailableWidth(); |
| 219 | |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 220 | setWindow(window); |
Filip Gruszczynski | 34dab0b | 2015-12-22 08:29:07 -0800 | [diff] [blame] | 221 | |
| 222 | updateLogTag(params); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 223 | } |
| 224 | |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 225 | void setBackgroundFallback(int resId) { |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 226 | mBackgroundFallback.setDrawable(resId != 0 ? getContext().getDrawable(resId) : null); |
| 227 | setWillNotDraw(getBackground() == null && !mBackgroundFallback.hasFallback()); |
| 228 | } |
| 229 | |
| 230 | @Override |
| 231 | public void onDraw(Canvas c) { |
| 232 | super.onDraw(c); |
| 233 | mBackgroundFallback.draw(mContentRoot, c, mWindow.mContentParent); |
| 234 | } |
| 235 | |
| 236 | @Override |
| 237 | public boolean dispatchKeyEvent(KeyEvent event) { |
| 238 | final int keyCode = event.getKeyCode(); |
| 239 | final int action = event.getAction(); |
| 240 | final boolean isDown = action == KeyEvent.ACTION_DOWN; |
| 241 | |
| 242 | if (isDown && (event.getRepeatCount() == 0)) { |
| 243 | // First handle chording of panel key: if a panel key is held |
| 244 | // but not released, try to execute a shortcut in it. |
| 245 | if ((mWindow.mPanelChordingKey > 0) && (mWindow.mPanelChordingKey != keyCode)) { |
| 246 | boolean handled = dispatchKeyShortcutEvent(event); |
| 247 | if (handled) { |
| 248 | return true; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | // If a panel is open, perform a shortcut on it without the |
| 253 | // chorded panel key |
| 254 | if ((mWindow.mPreparedPanel != null) && mWindow.mPreparedPanel.isOpen) { |
| 255 | if (mWindow.performPanelShortcut(mWindow.mPreparedPanel, keyCode, event, 0)) { |
| 256 | return true; |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | if (!mWindow.isDestroyed()) { |
| 262 | final Window.Callback cb = mWindow.getCallback(); |
| 263 | final boolean handled = cb != null && mFeatureId < 0 ? cb.dispatchKeyEvent(event) |
| 264 | : super.dispatchKeyEvent(event); |
| 265 | if (handled) { |
| 266 | return true; |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | return isDown ? mWindow.onKeyDown(mFeatureId, event.getKeyCode(), event) |
| 271 | : mWindow.onKeyUp(mFeatureId, event.getKeyCode(), event); |
| 272 | } |
| 273 | |
| 274 | @Override |
| 275 | public boolean dispatchKeyShortcutEvent(KeyEvent ev) { |
| 276 | // If the panel is already prepared, then perform the shortcut using it. |
| 277 | boolean handled; |
| 278 | if (mWindow.mPreparedPanel != null) { |
| 279 | handled = mWindow.performPanelShortcut(mWindow.mPreparedPanel, ev.getKeyCode(), ev, |
| 280 | Menu.FLAG_PERFORM_NO_CLOSE); |
| 281 | if (handled) { |
| 282 | if (mWindow.mPreparedPanel != null) { |
| 283 | mWindow.mPreparedPanel.isHandled = true; |
| 284 | } |
| 285 | return true; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | // Shortcut not handled by the panel. Dispatch to the view hierarchy. |
| 290 | final Window.Callback cb = mWindow.getCallback(); |
| 291 | handled = cb != null && !mWindow.isDestroyed() && mFeatureId < 0 |
| 292 | ? cb.dispatchKeyShortcutEvent(ev) : super.dispatchKeyShortcutEvent(ev); |
| 293 | if (handled) { |
| 294 | return true; |
| 295 | } |
| 296 | |
| 297 | // If the panel is not prepared, then we may be trying to handle a shortcut key |
| 298 | // combination such as Control+C. Temporarily prepare the panel then mark it |
| 299 | // unprepared again when finished to ensure that the panel will again be prepared |
| 300 | // the next time it is shown for real. |
| 301 | PhoneWindow.PanelFeatureState st = |
| 302 | mWindow.getPanelState(Window.FEATURE_OPTIONS_PANEL, false); |
| 303 | if (st != null && mWindow.mPreparedPanel == null) { |
| 304 | mWindow.preparePanel(st, ev); |
| 305 | handled = mWindow.performPanelShortcut(st, ev.getKeyCode(), ev, |
| 306 | Menu.FLAG_PERFORM_NO_CLOSE); |
| 307 | st.isPrepared = false; |
| 308 | if (handled) { |
| 309 | return true; |
| 310 | } |
| 311 | } |
| 312 | return false; |
| 313 | } |
| 314 | |
| 315 | @Override |
| 316 | public boolean dispatchTouchEvent(MotionEvent ev) { |
| 317 | final Window.Callback cb = mWindow.getCallback(); |
| 318 | return cb != null && !mWindow.isDestroyed() && mFeatureId < 0 |
| 319 | ? cb.dispatchTouchEvent(ev) : super.dispatchTouchEvent(ev); |
| 320 | } |
| 321 | |
| 322 | @Override |
| 323 | public boolean dispatchTrackballEvent(MotionEvent ev) { |
| 324 | final Window.Callback cb = mWindow.getCallback(); |
| 325 | return cb != null && !mWindow.isDestroyed() && mFeatureId < 0 |
| 326 | ? cb.dispatchTrackballEvent(ev) : super.dispatchTrackballEvent(ev); |
| 327 | } |
| 328 | |
| 329 | @Override |
| 330 | public boolean dispatchGenericMotionEvent(MotionEvent ev) { |
| 331 | final Window.Callback cb = mWindow.getCallback(); |
| 332 | return cb != null && !mWindow.isDestroyed() && mFeatureId < 0 |
| 333 | ? cb.dispatchGenericMotionEvent(ev) : super.dispatchGenericMotionEvent(ev); |
| 334 | } |
| 335 | |
| 336 | public boolean superDispatchKeyEvent(KeyEvent event) { |
| 337 | // Give priority to closing action modes if applicable. |
| 338 | if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) { |
| 339 | final int action = event.getAction(); |
| 340 | // Back cancels action modes first. |
| 341 | if (mPrimaryActionMode != null) { |
| 342 | if (action == KeyEvent.ACTION_UP) { |
| 343 | mPrimaryActionMode.finish(); |
| 344 | } |
| 345 | return true; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | return super.dispatchKeyEvent(event); |
| 350 | } |
| 351 | |
| 352 | public boolean superDispatchKeyShortcutEvent(KeyEvent event) { |
| 353 | return super.dispatchKeyShortcutEvent(event); |
| 354 | } |
| 355 | |
| 356 | public boolean superDispatchTouchEvent(MotionEvent event) { |
| 357 | return super.dispatchTouchEvent(event); |
| 358 | } |
| 359 | |
| 360 | public boolean superDispatchTrackballEvent(MotionEvent event) { |
| 361 | return super.dispatchTrackballEvent(event); |
| 362 | } |
| 363 | |
| 364 | public boolean superDispatchGenericMotionEvent(MotionEvent event) { |
| 365 | return super.dispatchGenericMotionEvent(event); |
| 366 | } |
| 367 | |
| 368 | @Override |
| 369 | public boolean onTouchEvent(MotionEvent event) { |
| 370 | return onInterceptTouchEvent(event); |
| 371 | } |
| 372 | |
| 373 | private boolean isOutOfInnerBounds(int x, int y) { |
| 374 | return x < 0 || y < 0 || x > getWidth() || y > getHeight(); |
| 375 | } |
| 376 | |
| 377 | private boolean isOutOfBounds(int x, int y) { |
| 378 | return x < -5 || y < -5 || x > (getWidth() + 5) |
| 379 | || y > (getHeight() + 5); |
| 380 | } |
| 381 | |
| 382 | @Override |
| 383 | public boolean onInterceptTouchEvent(MotionEvent event) { |
| 384 | int action = event.getAction(); |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 385 | if (mHasCaption && isShowingCaption()) { |
| 386 | // Don't dispatch ACTION_DOWN to the captionr if the window is resizable and the event |
| 387 | // was (starting) outside the window. Window resizing events should be handled by |
| 388 | // WindowManager. |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 389 | // TODO: Investigate how to handle the outside touch in window manager |
| 390 | // without generating these events. |
| 391 | // Currently we receive these because we need to enlarge the window's |
| 392 | // touch region so that the monitor channel receives the events |
| 393 | // in the outside touch area. |
| 394 | if (action == MotionEvent.ACTION_DOWN) { |
| 395 | final int x = (int) event.getX(); |
| 396 | final int y = (int) event.getY(); |
| 397 | if (isOutOfInnerBounds(x, y)) { |
| 398 | return true; |
| 399 | } |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | if (mFeatureId >= 0) { |
| 404 | if (action == MotionEvent.ACTION_DOWN) { |
| 405 | int x = (int)event.getX(); |
| 406 | int y = (int)event.getY(); |
| 407 | if (isOutOfBounds(x, y)) { |
| 408 | mWindow.closePanel(mFeatureId); |
| 409 | return true; |
| 410 | } |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | if (!SWEEP_OPEN_MENU) { |
| 415 | return false; |
| 416 | } |
| 417 | |
| 418 | if (mFeatureId >= 0) { |
| 419 | if (action == MotionEvent.ACTION_DOWN) { |
Filip Gruszczynski | 34dab0b | 2015-12-22 08:29:07 -0800 | [diff] [blame] | 420 | Log.i(mLogTag, "Watchiing!"); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 421 | mWatchingForMenu = true; |
| 422 | mDownY = (int) event.getY(); |
| 423 | return false; |
| 424 | } |
| 425 | |
| 426 | if (!mWatchingForMenu) { |
| 427 | return false; |
| 428 | } |
| 429 | |
| 430 | int y = (int)event.getY(); |
| 431 | if (action == MotionEvent.ACTION_MOVE) { |
| 432 | if (y > (mDownY+30)) { |
Filip Gruszczynski | 34dab0b | 2015-12-22 08:29:07 -0800 | [diff] [blame] | 433 | Log.i(mLogTag, "Closing!"); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 434 | mWindow.closePanel(mFeatureId); |
| 435 | mWatchingForMenu = false; |
| 436 | return true; |
| 437 | } |
| 438 | } else if (action == MotionEvent.ACTION_UP) { |
| 439 | mWatchingForMenu = false; |
| 440 | } |
| 441 | |
| 442 | return false; |
| 443 | } |
| 444 | |
Filip Gruszczynski | 34dab0b | 2015-12-22 08:29:07 -0800 | [diff] [blame] | 445 | //Log.i(mLogTag, "Intercept: action=" + action + " y=" + event.getY() |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 446 | // + " (in " + getHeight() + ")"); |
| 447 | |
| 448 | if (action == MotionEvent.ACTION_DOWN) { |
| 449 | int y = (int)event.getY(); |
| 450 | if (y >= (getHeight()-5) && !mWindow.hasChildren()) { |
Filip Gruszczynski | 34dab0b | 2015-12-22 08:29:07 -0800 | [diff] [blame] | 451 | Log.i(mLogTag, "Watching!"); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 452 | mWatchingForMenu = true; |
| 453 | } |
| 454 | return false; |
| 455 | } |
| 456 | |
| 457 | if (!mWatchingForMenu) { |
| 458 | return false; |
| 459 | } |
| 460 | |
| 461 | int y = (int)event.getY(); |
| 462 | if (action == MotionEvent.ACTION_MOVE) { |
| 463 | if (y < (getHeight()-30)) { |
Filip Gruszczynski | 34dab0b | 2015-12-22 08:29:07 -0800 | [diff] [blame] | 464 | Log.i(mLogTag, "Opening!"); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 465 | mWindow.openPanel(Window.FEATURE_OPTIONS_PANEL, new KeyEvent( |
| 466 | KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MENU)); |
| 467 | mWatchingForMenu = false; |
| 468 | return true; |
| 469 | } |
| 470 | } else if (action == MotionEvent.ACTION_UP) { |
| 471 | mWatchingForMenu = false; |
| 472 | } |
| 473 | |
| 474 | return false; |
| 475 | } |
| 476 | |
| 477 | @Override |
| 478 | public void sendAccessibilityEvent(int eventType) { |
| 479 | if (!AccessibilityManager.getInstance(mContext).isEnabled()) { |
| 480 | return; |
| 481 | } |
| 482 | |
| 483 | // if we are showing a feature that should be announced and one child |
| 484 | // make this child the event source since this is the feature itself |
| 485 | // otherwise the callback will take over and announce its client |
| 486 | if ((mFeatureId == Window.FEATURE_OPTIONS_PANEL || |
| 487 | mFeatureId == Window.FEATURE_CONTEXT_MENU || |
| 488 | mFeatureId == Window.FEATURE_PROGRESS || |
| 489 | mFeatureId == Window.FEATURE_INDETERMINATE_PROGRESS) |
| 490 | && getChildCount() == 1) { |
| 491 | getChildAt(0).sendAccessibilityEvent(eventType); |
| 492 | } else { |
| 493 | super.sendAccessibilityEvent(eventType); |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | @Override |
| 498 | public boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) { |
| 499 | final Window.Callback cb = mWindow.getCallback(); |
| 500 | if (cb != null && !mWindow.isDestroyed()) { |
| 501 | if (cb.dispatchPopulateAccessibilityEvent(event)) { |
| 502 | return true; |
| 503 | } |
| 504 | } |
| 505 | return super.dispatchPopulateAccessibilityEventInternal(event); |
| 506 | } |
| 507 | |
| 508 | @Override |
| 509 | protected boolean setFrame(int l, int t, int r, int b) { |
| 510 | boolean changed = super.setFrame(l, t, r, b); |
| 511 | if (changed) { |
| 512 | final Rect drawingBounds = mDrawingBounds; |
| 513 | getDrawingRect(drawingBounds); |
| 514 | |
| 515 | Drawable fg = getForeground(); |
| 516 | if (fg != null) { |
| 517 | final Rect frameOffsets = mFrameOffsets; |
| 518 | drawingBounds.left += frameOffsets.left; |
| 519 | drawingBounds.top += frameOffsets.top; |
| 520 | drawingBounds.right -= frameOffsets.right; |
| 521 | drawingBounds.bottom -= frameOffsets.bottom; |
| 522 | fg.setBounds(drawingBounds); |
| 523 | final Rect framePadding = mFramePadding; |
| 524 | drawingBounds.left += framePadding.left - frameOffsets.left; |
| 525 | drawingBounds.top += framePadding.top - frameOffsets.top; |
| 526 | drawingBounds.right -= framePadding.right - frameOffsets.right; |
| 527 | drawingBounds.bottom -= framePadding.bottom - frameOffsets.bottom; |
| 528 | } |
| 529 | |
| 530 | Drawable bg = getBackground(); |
| 531 | if (bg != null) { |
| 532 | bg.setBounds(drawingBounds); |
| 533 | } |
| 534 | |
| 535 | if (SWEEP_OPEN_MENU) { |
| 536 | if (mMenuBackground == null && mFeatureId < 0 |
| 537 | && mWindow.getAttributes().height |
| 538 | == WindowManager.LayoutParams.MATCH_PARENT) { |
| 539 | mMenuBackground = getContext().getDrawable( |
| 540 | R.drawable.menu_background); |
| 541 | } |
| 542 | if (mMenuBackground != null) { |
| 543 | mMenuBackground.setBounds(drawingBounds.left, |
| 544 | drawingBounds.bottom-6, drawingBounds.right, |
| 545 | drawingBounds.bottom+20); |
| 546 | } |
| 547 | } |
| 548 | } |
| 549 | return changed; |
| 550 | } |
| 551 | |
| 552 | @Override |
| 553 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 554 | final DisplayMetrics metrics = getContext().getResources().getDisplayMetrics(); |
Filip Gruszczynski | 34dab0b | 2015-12-22 08:29:07 -0800 | [diff] [blame] | 555 | final boolean isPortrait = |
| 556 | getResources().getConfiguration().orientation == ORIENTATION_PORTRAIT; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 557 | |
| 558 | final int widthMode = getMode(widthMeasureSpec); |
| 559 | final int heightMode = getMode(heightMeasureSpec); |
| 560 | |
| 561 | boolean fixedWidth = false; |
| 562 | if (widthMode == AT_MOST) { |
Filip Gruszczynski | 34dab0b | 2015-12-22 08:29:07 -0800 | [diff] [blame] | 563 | final TypedValue tvw = isPortrait ? mWindow.mFixedWidthMinor : mWindow.mFixedWidthMajor; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 564 | if (tvw != null && tvw.type != TypedValue.TYPE_NULL) { |
| 565 | final int w; |
| 566 | if (tvw.type == TypedValue.TYPE_DIMENSION) { |
| 567 | w = (int) tvw.getDimension(metrics); |
| 568 | } else if (tvw.type == TypedValue.TYPE_FRACTION) { |
| 569 | w = (int) tvw.getFraction(metrics.widthPixels, metrics.widthPixels); |
| 570 | } else { |
| 571 | w = 0; |
| 572 | } |
| 573 | |
| 574 | if (w > 0) { |
| 575 | final int widthSize = MeasureSpec.getSize(widthMeasureSpec); |
| 576 | widthMeasureSpec = MeasureSpec.makeMeasureSpec( |
| 577 | Math.min(w, widthSize), EXACTLY); |
| 578 | fixedWidth = true; |
| 579 | } |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | if (heightMode == AT_MOST) { |
| 584 | final TypedValue tvh = isPortrait ? mWindow.mFixedHeightMajor |
| 585 | : mWindow.mFixedHeightMinor; |
| 586 | if (tvh != null && tvh.type != TypedValue.TYPE_NULL) { |
| 587 | final int h; |
| 588 | if (tvh.type == TypedValue.TYPE_DIMENSION) { |
| 589 | h = (int) tvh.getDimension(metrics); |
| 590 | } else if (tvh.type == TypedValue.TYPE_FRACTION) { |
| 591 | h = (int) tvh.getFraction(metrics.heightPixels, metrics.heightPixels); |
| 592 | } else { |
| 593 | h = 0; |
| 594 | } |
| 595 | if (h > 0) { |
| 596 | final int heightSize = MeasureSpec.getSize(heightMeasureSpec); |
| 597 | heightMeasureSpec = MeasureSpec.makeMeasureSpec( |
| 598 | Math.min(h, heightSize), EXACTLY); |
| 599 | } |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | getOutsets(mOutsets); |
| 604 | if (mOutsets.top > 0 || mOutsets.bottom > 0) { |
| 605 | int mode = MeasureSpec.getMode(heightMeasureSpec); |
| 606 | if (mode != MeasureSpec.UNSPECIFIED) { |
| 607 | int height = MeasureSpec.getSize(heightMeasureSpec); |
| 608 | heightMeasureSpec = MeasureSpec.makeMeasureSpec( |
| 609 | height + mOutsets.top + mOutsets.bottom, mode); |
| 610 | } |
| 611 | } |
| 612 | if (mOutsets.left > 0 || mOutsets.right > 0) { |
| 613 | int mode = MeasureSpec.getMode(widthMeasureSpec); |
| 614 | if (mode != MeasureSpec.UNSPECIFIED) { |
| 615 | int width = MeasureSpec.getSize(widthMeasureSpec); |
| 616 | widthMeasureSpec = MeasureSpec.makeMeasureSpec( |
| 617 | width + mOutsets.left + mOutsets.right, mode); |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 622 | |
| 623 | int width = getMeasuredWidth(); |
| 624 | boolean measure = false; |
| 625 | |
| 626 | widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, EXACTLY); |
| 627 | |
| 628 | if (!fixedWidth && widthMode == AT_MOST) { |
| 629 | final TypedValue tv = isPortrait ? mWindow.mMinWidthMinor : mWindow.mMinWidthMajor; |
| 630 | if (tv.type != TypedValue.TYPE_NULL) { |
| 631 | final int min; |
| 632 | if (tv.type == TypedValue.TYPE_DIMENSION) { |
| 633 | min = (int)tv.getDimension(metrics); |
| 634 | } else if (tv.type == TypedValue.TYPE_FRACTION) { |
Filip Gruszczynski | 34dab0b | 2015-12-22 08:29:07 -0800 | [diff] [blame] | 635 | min = (int)tv.getFraction(mAvailableWidth, mAvailableWidth); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 636 | } else { |
| 637 | min = 0; |
| 638 | } |
| 639 | |
| 640 | if (width < min) { |
| 641 | widthMeasureSpec = MeasureSpec.makeMeasureSpec(min, EXACTLY); |
| 642 | measure = true; |
| 643 | } |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | // TODO: Support height? |
| 648 | |
| 649 | if (measure) { |
| 650 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | @Override |
| 655 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 656 | super.onLayout(changed, left, top, right, bottom); |
| 657 | getOutsets(mOutsets); |
| 658 | if (mOutsets.left > 0) { |
| 659 | offsetLeftAndRight(-mOutsets.left); |
| 660 | } |
| 661 | if (mOutsets.top > 0) { |
| 662 | offsetTopAndBottom(-mOutsets.top); |
| 663 | } |
Wale Ogunwale | 2b547c3 | 2015-11-18 10:33:22 -0800 | [diff] [blame] | 664 | |
| 665 | // If the application changed its SystemUI metrics, we might also have to adapt |
| 666 | // our shadow elevation. |
| 667 | updateElevation(); |
| 668 | mAllowUpdateElevation = true; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | @Override |
| 672 | public void draw(Canvas canvas) { |
| 673 | super.draw(canvas); |
| 674 | |
| 675 | if (mMenuBackground != null) { |
| 676 | mMenuBackground.draw(canvas); |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | @Override |
| 681 | public boolean showContextMenuForChild(View originalView) { |
Alan Viverette | 021627e | 2015-11-25 14:22:00 -0500 | [diff] [blame] | 682 | return showContextMenuForChildInternal(originalView, 0, 0, false); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | @Override |
| 686 | public boolean showContextMenuForChild(View originalView, float x, float y) { |
Alan Viverette | 021627e | 2015-11-25 14:22:00 -0500 | [diff] [blame] | 687 | return showContextMenuForChildInternal(originalView, x, y, true); |
| 688 | } |
| 689 | |
| 690 | private boolean showContextMenuForChildInternal(View originalView, |
| 691 | float x, float y, boolean isPopup) { |
| 692 | // Only allow one context menu at a time. |
| 693 | if (mWindow.mContextMenuHelper != null) { |
| 694 | mWindow.mContextMenuHelper.dismiss(); |
| 695 | mWindow.mContextMenuHelper = null; |
| 696 | } |
| 697 | |
| 698 | // Reuse the context menu builder. |
Alan Viverette | 77fb85e | 2015-12-14 11:42:44 -0500 | [diff] [blame] | 699 | final PhoneWindowMenuCallback callback = mWindow.mContextMenuCallback; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 700 | if (mWindow.mContextMenu == null) { |
| 701 | mWindow.mContextMenu = new ContextMenuBuilder(getContext()); |
Alan Viverette | 77fb85e | 2015-12-14 11:42:44 -0500 | [diff] [blame] | 702 | mWindow.mContextMenu.setCallback(callback); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 703 | } else { |
| 704 | mWindow.mContextMenu.clearAll(); |
| 705 | } |
| 706 | |
Alan Viverette | 021627e | 2015-11-25 14:22:00 -0500 | [diff] [blame] | 707 | final MenuHelper helper; |
| 708 | if (isPopup) { |
| 709 | helper = mWindow.mContextMenu.showPopup(getContext(), originalView, x, y); |
| 710 | } else { |
| 711 | helper = mWindow.mContextMenu.showDialog(originalView, originalView.getWindowToken()); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 712 | } |
Alan Viverette | 021627e | 2015-11-25 14:22:00 -0500 | [diff] [blame] | 713 | |
Alan Viverette | 9084d22 | 2015-12-16 09:56:37 -0500 | [diff] [blame] | 714 | if (helper != null) { |
| 715 | // If it's a dialog, the callback needs to handle showing |
| 716 | // sub-menus. Either way, the callback is required for propagating |
| 717 | // selection to Context.onContextMenuItemSelected(). |
| 718 | callback.setShowDialogForSubmenu(!isPopup); |
| 719 | helper.setPresenterCallback(callback); |
| 720 | } |
Alan Viverette | 021627e | 2015-11-25 14:22:00 -0500 | [diff] [blame] | 721 | |
| 722 | mWindow.mContextMenuHelper = helper; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 723 | return helper != null; |
| 724 | } |
| 725 | |
| 726 | @Override |
| 727 | public ActionMode startActionModeForChild(View originalView, |
| 728 | ActionMode.Callback callback) { |
| 729 | return startActionModeForChild(originalView, callback, ActionMode.TYPE_PRIMARY); |
| 730 | } |
| 731 | |
| 732 | @Override |
| 733 | public ActionMode startActionModeForChild( |
| 734 | View child, ActionMode.Callback callback, int type) { |
| 735 | return startActionMode(child, callback, type); |
| 736 | } |
| 737 | |
| 738 | @Override |
| 739 | public ActionMode startActionMode(ActionMode.Callback callback) { |
| 740 | return startActionMode(callback, ActionMode.TYPE_PRIMARY); |
| 741 | } |
| 742 | |
| 743 | @Override |
| 744 | public ActionMode startActionMode(ActionMode.Callback callback, int type) { |
| 745 | return startActionMode(this, callback, type); |
| 746 | } |
| 747 | |
| 748 | private ActionMode startActionMode( |
| 749 | View originatingView, ActionMode.Callback callback, int type) { |
| 750 | ActionMode.Callback2 wrappedCallback = new ActionModeCallback2Wrapper(callback); |
| 751 | ActionMode mode = null; |
| 752 | if (mWindow.getCallback() != null && !mWindow.isDestroyed()) { |
| 753 | try { |
| 754 | mode = mWindow.getCallback().onWindowStartingActionMode(wrappedCallback, type); |
| 755 | } catch (AbstractMethodError ame) { |
| 756 | // Older apps might not implement the typed version of this method. |
| 757 | if (type == ActionMode.TYPE_PRIMARY) { |
| 758 | try { |
| 759 | mode = mWindow.getCallback().onWindowStartingActionMode( |
| 760 | wrappedCallback); |
| 761 | } catch (AbstractMethodError ame2) { |
| 762 | // Older apps might not implement this callback method at all. |
| 763 | } |
| 764 | } |
| 765 | } |
| 766 | } |
| 767 | if (mode != null) { |
| 768 | if (mode.getType() == ActionMode.TYPE_PRIMARY) { |
| 769 | cleanupPrimaryActionMode(); |
| 770 | mPrimaryActionMode = mode; |
| 771 | } else if (mode.getType() == ActionMode.TYPE_FLOATING) { |
| 772 | if (mFloatingActionMode != null) { |
| 773 | mFloatingActionMode.finish(); |
| 774 | } |
| 775 | mFloatingActionMode = mode; |
| 776 | } |
| 777 | } else { |
| 778 | mode = createActionMode(type, wrappedCallback, originatingView); |
| 779 | if (mode != null && wrappedCallback.onCreateActionMode(mode, mode.getMenu())) { |
| 780 | setHandledActionMode(mode); |
| 781 | } else { |
| 782 | mode = null; |
| 783 | } |
| 784 | } |
| 785 | if (mode != null && mWindow.getCallback() != null && !mWindow.isDestroyed()) { |
| 786 | try { |
| 787 | mWindow.getCallback().onActionModeStarted(mode); |
| 788 | } catch (AbstractMethodError ame) { |
| 789 | // Older apps might not implement this callback method. |
| 790 | } |
| 791 | } |
| 792 | return mode; |
| 793 | } |
| 794 | |
| 795 | private void cleanupPrimaryActionMode() { |
| 796 | if (mPrimaryActionMode != null) { |
| 797 | mPrimaryActionMode.finish(); |
| 798 | mPrimaryActionMode = null; |
| 799 | } |
| 800 | if (mPrimaryActionModeView != null) { |
| 801 | mPrimaryActionModeView.killMode(); |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | private void cleanupFloatingActionModeViews() { |
| 806 | if (mFloatingToolbar != null) { |
| 807 | mFloatingToolbar.dismiss(); |
| 808 | mFloatingToolbar = null; |
| 809 | } |
| 810 | if (mFloatingActionModeOriginatingView != null) { |
| 811 | if (mFloatingToolbarPreDrawListener != null) { |
| 812 | mFloatingActionModeOriginatingView.getViewTreeObserver() |
| 813 | .removeOnPreDrawListener(mFloatingToolbarPreDrawListener); |
| 814 | mFloatingToolbarPreDrawListener = null; |
| 815 | } |
| 816 | mFloatingActionModeOriginatingView = null; |
| 817 | } |
| 818 | } |
| 819 | |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 820 | void startChanging() { |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 821 | mChanging = true; |
| 822 | } |
| 823 | |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 824 | void finishChanging() { |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 825 | mChanging = false; |
| 826 | drawableChanged(); |
| 827 | } |
| 828 | |
| 829 | public void setWindowBackground(Drawable drawable) { |
| 830 | if (getBackground() != drawable) { |
| 831 | setBackgroundDrawable(drawable); |
| 832 | if (drawable != null) { |
| 833 | drawable.getPadding(mBackgroundPadding); |
| 834 | } else { |
| 835 | mBackgroundPadding.setEmpty(); |
| 836 | } |
| 837 | drawableChanged(); |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | public void setWindowFrame(Drawable drawable) { |
| 842 | if (getForeground() != drawable) { |
| 843 | setForeground(drawable); |
| 844 | if (drawable != null) { |
| 845 | drawable.getPadding(mFramePadding); |
| 846 | } else { |
| 847 | mFramePadding.setEmpty(); |
| 848 | } |
| 849 | drawableChanged(); |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | @Override |
| 854 | public void onWindowSystemUiVisibilityChanged(int visible) { |
| 855 | updateColorViews(null /* insets */, true /* animate */); |
| 856 | } |
| 857 | |
| 858 | @Override |
| 859 | public WindowInsets onApplyWindowInsets(WindowInsets insets) { |
| 860 | mFrameOffsets.set(insets.getSystemWindowInsets()); |
| 861 | insets = updateColorViews(insets, true /* animate */); |
| 862 | insets = updateStatusGuard(insets); |
| 863 | updateNavigationGuard(insets); |
| 864 | if (getForeground() != null) { |
| 865 | drawableChanged(); |
| 866 | } |
| 867 | return insets; |
| 868 | } |
| 869 | |
| 870 | @Override |
| 871 | public boolean isTransitionGroup() { |
| 872 | return false; |
| 873 | } |
| 874 | |
| 875 | WindowInsets updateColorViews(WindowInsets insets, boolean animate) { |
| 876 | WindowManager.LayoutParams attrs = mWindow.getAttributes(); |
| 877 | int sysUiVisibility = attrs.systemUiVisibility | getWindowSystemUiVisibility(); |
| 878 | |
| 879 | if (!mWindow.mIsFloating && ActivityManager.isHighEndGfx()) { |
| 880 | boolean disallowAnimate = !isLaidOut(); |
| 881 | disallowAnimate |= ((mLastWindowFlags ^ attrs.flags) |
| 882 | & FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS) != 0; |
| 883 | mLastWindowFlags = attrs.flags; |
| 884 | |
| 885 | if (insets != null) { |
| 886 | mLastTopInset = Math.min(insets.getStableInsetTop(), |
| 887 | insets.getSystemWindowInsetTop()); |
| 888 | mLastBottomInset = Math.min(insets.getStableInsetBottom(), |
| 889 | insets.getSystemWindowInsetBottom()); |
| 890 | mLastRightInset = Math.min(insets.getStableInsetRight(), |
| 891 | insets.getSystemWindowInsetRight()); |
| 892 | |
| 893 | // Don't animate if the presence of stable insets has changed, because that |
| 894 | // indicates that the window was either just added and received them for the |
| 895 | // first time, or the window size or position has changed. |
| 896 | boolean hasTopStableInset = insets.getStableInsetTop() != 0; |
| 897 | disallowAnimate |= (hasTopStableInset != mLastHasTopStableInset); |
| 898 | mLastHasTopStableInset = hasTopStableInset; |
| 899 | |
| 900 | boolean hasBottomStableInset = insets.getStableInsetBottom() != 0; |
| 901 | disallowAnimate |= (hasBottomStableInset != mLastHasBottomStableInset); |
| 902 | mLastHasBottomStableInset = hasBottomStableInset; |
| 903 | |
| 904 | boolean hasRightStableInset = insets.getStableInsetRight() != 0; |
| 905 | disallowAnimate |= (hasRightStableInset != mLastHasRightStableInset); |
| 906 | mLastHasRightStableInset = hasRightStableInset; |
| 907 | } |
| 908 | |
| 909 | boolean navBarToRightEdge = mLastBottomInset == 0 && mLastRightInset > 0; |
| 910 | int navBarSize = navBarToRightEdge ? mLastRightInset : mLastBottomInset; |
| 911 | updateColorViewInt(mNavigationColorViewState, sysUiVisibility, |
| 912 | mWindow.mNavigationBarColor, navBarSize, navBarToRightEdge, |
Jorim Jaggi | 4fa7892 | 2015-11-30 17:13:56 -0800 | [diff] [blame] | 913 | 0 /* rightInset */, animate && !disallowAnimate, false /* force */); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 914 | |
| 915 | boolean statusBarNeedsRightInset = navBarToRightEdge |
| 916 | && mNavigationColorViewState.present; |
| 917 | int statusBarRightInset = statusBarNeedsRightInset ? mLastRightInset : 0; |
Jorim Jaggi | 4fa7892 | 2015-11-30 17:13:56 -0800 | [diff] [blame] | 918 | updateColorViewInt(mStatusColorViewState, sysUiVisibility, |
| 919 | calculateStatusBarColor(), mLastTopInset, |
| 920 | false /* matchVertical */, statusBarRightInset, animate && !disallowAnimate, |
| 921 | mForceWindowDrawsStatusBarBackground); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | // When we expand the window with FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS, we still need |
| 925 | // to ensure that the rest of the view hierarchy doesn't notice it, unless they've |
| 926 | // explicitly asked for it. |
| 927 | |
| 928 | boolean consumingNavBar = |
| 929 | (attrs.flags & FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS) != 0 |
| 930 | && (sysUiVisibility & SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION) == 0 |
| 931 | && (sysUiVisibility & SYSTEM_UI_FLAG_HIDE_NAVIGATION) == 0; |
| 932 | |
| 933 | int consumedRight = consumingNavBar ? mLastRightInset : 0; |
| 934 | int consumedBottom = consumingNavBar ? mLastBottomInset : 0; |
| 935 | |
| 936 | if (mContentRoot != null |
| 937 | && mContentRoot.getLayoutParams() instanceof MarginLayoutParams) { |
| 938 | MarginLayoutParams lp = (MarginLayoutParams) mContentRoot.getLayoutParams(); |
| 939 | if (lp.rightMargin != consumedRight || lp.bottomMargin != consumedBottom) { |
| 940 | lp.rightMargin = consumedRight; |
| 941 | lp.bottomMargin = consumedBottom; |
| 942 | mContentRoot.setLayoutParams(lp); |
| 943 | |
| 944 | if (insets == null) { |
| 945 | // The insets have changed, but we're not currently in the process |
| 946 | // of dispatching them. |
| 947 | requestApplyInsets(); |
| 948 | } |
| 949 | } |
| 950 | if (insets != null) { |
| 951 | insets = insets.replaceSystemWindowInsets( |
| 952 | insets.getSystemWindowInsetLeft(), |
| 953 | insets.getSystemWindowInsetTop(), |
| 954 | insets.getSystemWindowInsetRight() - consumedRight, |
| 955 | insets.getSystemWindowInsetBottom() - consumedBottom); |
| 956 | } |
| 957 | } |
| 958 | |
| 959 | if (insets != null) { |
| 960 | insets = insets.consumeStableInsets(); |
| 961 | } |
| 962 | return insets; |
| 963 | } |
| 964 | |
Jorim Jaggi | 4fa7892 | 2015-11-30 17:13:56 -0800 | [diff] [blame] | 965 | private int calculateStatusBarColor() { |
| 966 | int flags = mWindow.getAttributes().flags; |
| 967 | return (flags & FLAG_TRANSLUCENT_STATUS) != 0 ? mSemiTransparentStatusBarColor |
| 968 | : (flags & FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS) != 0 ? mWindow.mStatusBarColor |
| 969 | : Color.BLACK; |
| 970 | } |
| 971 | |
Jorim Jaggi | 04c2fbd | 2015-12-02 15:11:45 -0800 | [diff] [blame] | 972 | private int getCurrentColor(ColorViewState state) { |
| 973 | if (state.visible) { |
| 974 | return state.color; |
| 975 | } else { |
| 976 | return 0; |
| 977 | } |
| 978 | } |
| 979 | |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 980 | /** |
| 981 | * Update a color view |
| 982 | * |
| 983 | * @param state the color view to update. |
| 984 | * @param sysUiVis the current systemUiVisibility to apply. |
| 985 | * @param color the current color to apply. |
| 986 | * @param size the current size in the non-parent-matching dimension. |
| 987 | * @param verticalBar if true the view is attached to a vertical edge, otherwise to a |
| 988 | * horizontal edge, |
| 989 | * @param rightMargin rightMargin for the color view. |
| 990 | * @param animate if true, the change will be animated. |
| 991 | */ |
| 992 | private void updateColorViewInt(final ColorViewState state, int sysUiVis, int color, |
Jorim Jaggi | 4fa7892 | 2015-11-30 17:13:56 -0800 | [diff] [blame] | 993 | int size, boolean verticalBar, int rightMargin, boolean animate, boolean force) { |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 994 | state.present = size > 0 && (sysUiVis & state.systemUiHideFlag) == 0 |
| 995 | && (mWindow.getAttributes().flags & state.hideWindowFlag) == 0 |
Jorim Jaggi | 4fa7892 | 2015-11-30 17:13:56 -0800 | [diff] [blame] | 996 | && ((mWindow.getAttributes().flags & FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS) != 0 |
| 997 | || force); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 998 | boolean show = state.present |
| 999 | && (color & Color.BLACK) != 0 |
Jorim Jaggi | 4fa7892 | 2015-11-30 17:13:56 -0800 | [diff] [blame] | 1000 | && ((mWindow.getAttributes().flags & state.translucentFlag) == 0 || force); |
Jorim Jaggi | 04c2fbd | 2015-12-02 15:11:45 -0800 | [diff] [blame] | 1001 | boolean showView = show && !isResizing(); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1002 | |
| 1003 | boolean visibilityChanged = false; |
| 1004 | View view = state.view; |
| 1005 | |
| 1006 | int resolvedHeight = verticalBar ? LayoutParams.MATCH_PARENT : size; |
| 1007 | int resolvedWidth = verticalBar ? size : LayoutParams.MATCH_PARENT; |
| 1008 | int resolvedGravity = verticalBar ? state.horizontalGravity : state.verticalGravity; |
| 1009 | |
| 1010 | if (view == null) { |
Jorim Jaggi | 04c2fbd | 2015-12-02 15:11:45 -0800 | [diff] [blame] | 1011 | if (showView) { |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1012 | state.view = view = new View(mContext); |
| 1013 | view.setBackgroundColor(color); |
| 1014 | view.setTransitionName(state.transitionName); |
| 1015 | view.setId(state.id); |
| 1016 | visibilityChanged = true; |
| 1017 | view.setVisibility(INVISIBLE); |
| 1018 | state.targetVisibility = VISIBLE; |
| 1019 | |
| 1020 | LayoutParams lp = new LayoutParams(resolvedWidth, resolvedHeight, |
| 1021 | resolvedGravity); |
| 1022 | lp.rightMargin = rightMargin; |
| 1023 | addView(view, lp); |
| 1024 | updateColorViewTranslations(); |
| 1025 | } |
| 1026 | } else { |
Jorim Jaggi | 04c2fbd | 2015-12-02 15:11:45 -0800 | [diff] [blame] | 1027 | int vis = showView ? VISIBLE : INVISIBLE; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1028 | visibilityChanged = state.targetVisibility != vis; |
| 1029 | state.targetVisibility = vis; |
| 1030 | LayoutParams lp = (LayoutParams) view.getLayoutParams(); |
| 1031 | if (lp.height != resolvedHeight || lp.width != resolvedWidth |
| 1032 | || lp.gravity != resolvedGravity || lp.rightMargin != rightMargin) { |
| 1033 | lp.height = resolvedHeight; |
| 1034 | lp.width = resolvedWidth; |
| 1035 | lp.gravity = resolvedGravity; |
| 1036 | lp.rightMargin = rightMargin; |
| 1037 | view.setLayoutParams(lp); |
| 1038 | } |
Jorim Jaggi | 04c2fbd | 2015-12-02 15:11:45 -0800 | [diff] [blame] | 1039 | if (showView) { |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1040 | view.setBackgroundColor(color); |
| 1041 | } |
| 1042 | } |
| 1043 | if (visibilityChanged) { |
| 1044 | view.animate().cancel(); |
Jorim Jaggi | 04c2fbd | 2015-12-02 15:11:45 -0800 | [diff] [blame] | 1045 | if (animate && !isResizing()) { |
| 1046 | if (showView) { |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1047 | if (view.getVisibility() != VISIBLE) { |
| 1048 | view.setVisibility(VISIBLE); |
| 1049 | view.setAlpha(0.0f); |
| 1050 | } |
| 1051 | view.animate().alpha(1.0f).setInterpolator(mShowInterpolator). |
| 1052 | setDuration(mBarEnterExitDuration); |
| 1053 | } else { |
| 1054 | view.animate().alpha(0.0f).setInterpolator(mHideInterpolator) |
| 1055 | .setDuration(mBarEnterExitDuration) |
| 1056 | .withEndAction(new Runnable() { |
| 1057 | @Override |
| 1058 | public void run() { |
| 1059 | state.view.setAlpha(1.0f); |
| 1060 | state.view.setVisibility(INVISIBLE); |
| 1061 | } |
| 1062 | }); |
| 1063 | } |
| 1064 | } else { |
| 1065 | view.setAlpha(1.0f); |
Jorim Jaggi | 04c2fbd | 2015-12-02 15:11:45 -0800 | [diff] [blame] | 1066 | view.setVisibility(showView ? VISIBLE : INVISIBLE); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1067 | } |
| 1068 | } |
Jorim Jaggi | 04c2fbd | 2015-12-02 15:11:45 -0800 | [diff] [blame] | 1069 | state.visible = show; |
| 1070 | state.color = color; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1071 | } |
| 1072 | |
| 1073 | private void updateColorViewTranslations() { |
| 1074 | // Put the color views back in place when they get moved off the screen |
| 1075 | // due to the the ViewRootImpl panning. |
| 1076 | int rootScrollY = mRootScrollY; |
| 1077 | if (mStatusColorViewState.view != null) { |
| 1078 | mStatusColorViewState.view.setTranslationY(rootScrollY > 0 ? rootScrollY : 0); |
| 1079 | } |
| 1080 | if (mNavigationColorViewState.view != null) { |
| 1081 | mNavigationColorViewState.view.setTranslationY(rootScrollY < 0 ? rootScrollY : 0); |
| 1082 | } |
| 1083 | } |
| 1084 | |
| 1085 | private WindowInsets updateStatusGuard(WindowInsets insets) { |
| 1086 | boolean showStatusGuard = false; |
| 1087 | // Show the status guard when the non-overlay contextual action bar is showing |
| 1088 | if (mPrimaryActionModeView != null) { |
| 1089 | if (mPrimaryActionModeView.getLayoutParams() instanceof MarginLayoutParams) { |
| 1090 | // Insets are magic! |
| 1091 | final MarginLayoutParams mlp = (MarginLayoutParams) |
| 1092 | mPrimaryActionModeView.getLayoutParams(); |
| 1093 | boolean mlpChanged = false; |
| 1094 | if (mPrimaryActionModeView.isShown()) { |
| 1095 | if (mTempRect == null) { |
| 1096 | mTempRect = new Rect(); |
| 1097 | } |
| 1098 | final Rect rect = mTempRect; |
| 1099 | |
| 1100 | // If the parent doesn't consume the insets, manually |
| 1101 | // apply the default system window insets. |
| 1102 | mWindow.mContentParent.computeSystemWindowInsets(insets, rect); |
| 1103 | final int newMargin = rect.top == 0 ? insets.getSystemWindowInsetTop() : 0; |
| 1104 | if (mlp.topMargin != newMargin) { |
| 1105 | mlpChanged = true; |
| 1106 | mlp.topMargin = insets.getSystemWindowInsetTop(); |
| 1107 | |
| 1108 | if (mStatusGuard == null) { |
| 1109 | mStatusGuard = new View(mContext); |
| 1110 | mStatusGuard.setBackgroundColor(mContext.getColor( |
| 1111 | R.color.input_method_navigation_guard)); |
| 1112 | addView(mStatusGuard, indexOfChild(mStatusColorViewState.view), |
| 1113 | new LayoutParams(LayoutParams.MATCH_PARENT, |
| 1114 | mlp.topMargin, Gravity.START | Gravity.TOP)); |
| 1115 | } else { |
| 1116 | final LayoutParams lp = (LayoutParams) |
| 1117 | mStatusGuard.getLayoutParams(); |
| 1118 | if (lp.height != mlp.topMargin) { |
| 1119 | lp.height = mlp.topMargin; |
| 1120 | mStatusGuard.setLayoutParams(lp); |
| 1121 | } |
| 1122 | } |
| 1123 | } |
| 1124 | |
| 1125 | // The action mode's theme may differ from the app, so |
| 1126 | // always show the status guard above it if we have one. |
| 1127 | showStatusGuard = mStatusGuard != null; |
| 1128 | |
| 1129 | // We only need to consume the insets if the action |
| 1130 | // mode is overlaid on the app content (e.g. it's |
| 1131 | // sitting in a FrameLayout, see |
| 1132 | // screen_simple_overlay_action_mode.xml). |
| 1133 | final boolean nonOverlay = (mWindow.getLocalFeaturesPrivate() |
| 1134 | & (1 << Window.FEATURE_ACTION_MODE_OVERLAY)) == 0; |
| 1135 | insets = insets.consumeSystemWindowInsets( |
| 1136 | false, nonOverlay && showStatusGuard /* top */, false, false); |
| 1137 | } else { |
| 1138 | // reset top margin |
| 1139 | if (mlp.topMargin != 0) { |
| 1140 | mlpChanged = true; |
| 1141 | mlp.topMargin = 0; |
| 1142 | } |
| 1143 | } |
| 1144 | if (mlpChanged) { |
| 1145 | mPrimaryActionModeView.setLayoutParams(mlp); |
| 1146 | } |
| 1147 | } |
| 1148 | } |
| 1149 | if (mStatusGuard != null) { |
| 1150 | mStatusGuard.setVisibility(showStatusGuard ? View.VISIBLE : View.GONE); |
| 1151 | } |
| 1152 | return insets; |
| 1153 | } |
| 1154 | |
| 1155 | private void updateNavigationGuard(WindowInsets insets) { |
| 1156 | // IMEs lay out below the nav bar, but the content view must not (for back compat) |
| 1157 | if (mWindow.getAttributes().type == WindowManager.LayoutParams.TYPE_INPUT_METHOD) { |
| 1158 | // prevent the content view from including the nav bar height |
| 1159 | if (mWindow.mContentParent != null) { |
| 1160 | if (mWindow.mContentParent.getLayoutParams() instanceof MarginLayoutParams) { |
| 1161 | MarginLayoutParams mlp = |
| 1162 | (MarginLayoutParams) mWindow.mContentParent.getLayoutParams(); |
| 1163 | mlp.bottomMargin = insets.getSystemWindowInsetBottom(); |
| 1164 | mWindow.mContentParent.setLayoutParams(mlp); |
| 1165 | } |
| 1166 | } |
| 1167 | // position the navigation guard view, creating it if necessary |
| 1168 | if (mNavigationGuard == null) { |
| 1169 | mNavigationGuard = new View(mContext); |
| 1170 | mNavigationGuard.setBackgroundColor(mContext.getColor( |
| 1171 | R.color.input_method_navigation_guard)); |
| 1172 | addView(mNavigationGuard, indexOfChild(mNavigationColorViewState.view), |
| 1173 | new LayoutParams(LayoutParams.MATCH_PARENT, |
| 1174 | insets.getSystemWindowInsetBottom(), |
| 1175 | Gravity.START | Gravity.BOTTOM)); |
| 1176 | } else { |
| 1177 | LayoutParams lp = (LayoutParams) mNavigationGuard.getLayoutParams(); |
| 1178 | lp.height = insets.getSystemWindowInsetBottom(); |
| 1179 | mNavigationGuard.setLayoutParams(lp); |
| 1180 | } |
Seigo Nonaka | 0a9d1ea | 2015-11-18 22:29:06 +0900 | [diff] [blame] | 1181 | updateNavigationGuardColor(); |
| 1182 | } |
| 1183 | } |
| 1184 | |
| 1185 | void updateNavigationGuardColor() { |
| 1186 | if (mNavigationGuard != null) { |
| 1187 | // Make navigation bar guard invisible if the transparent color is specified. |
| 1188 | // Only TRANSPARENT is sufficient for hiding the navigation bar if the no software |
| 1189 | // keyboard is shown by IMS. |
| 1190 | mNavigationGuard.setVisibility(mWindow.getNavigationBarColor() == Color.TRANSPARENT ? |
| 1191 | View.INVISIBLE : View.VISIBLE); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1192 | } |
| 1193 | } |
| 1194 | |
| 1195 | private void drawableChanged() { |
| 1196 | if (mChanging) { |
| 1197 | return; |
| 1198 | } |
| 1199 | |
| 1200 | setPadding(mFramePadding.left + mBackgroundPadding.left, |
| 1201 | mFramePadding.top + mBackgroundPadding.top, |
| 1202 | mFramePadding.right + mBackgroundPadding.right, |
| 1203 | mFramePadding.bottom + mBackgroundPadding.bottom); |
| 1204 | requestLayout(); |
| 1205 | invalidate(); |
| 1206 | |
| 1207 | int opacity = PixelFormat.OPAQUE; |
Wale Ogunwale | eb6722c | 2015-12-08 11:43:43 -0800 | [diff] [blame] | 1208 | if (StackId.hasWindowShadow(mStackId)) { |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1209 | // If the window has a shadow, it must be translucent. |
| 1210 | opacity = PixelFormat.TRANSLUCENT; |
| 1211 | } else{ |
| 1212 | // Note: If there is no background, we will assume opaque. The |
| 1213 | // common case seems to be that an application sets there to be |
| 1214 | // no background so it can draw everything itself. For that, |
| 1215 | // we would like to assume OPAQUE and let the app force it to |
| 1216 | // the slower TRANSLUCENT mode if that is really what it wants. |
| 1217 | Drawable bg = getBackground(); |
| 1218 | Drawable fg = getForeground(); |
| 1219 | if (bg != null) { |
| 1220 | if (fg == null) { |
| 1221 | opacity = bg.getOpacity(); |
| 1222 | } else if (mFramePadding.left <= 0 && mFramePadding.top <= 0 |
| 1223 | && mFramePadding.right <= 0 && mFramePadding.bottom <= 0) { |
| 1224 | // If the frame padding is zero, then we can be opaque |
| 1225 | // if either the frame -or- the background is opaque. |
| 1226 | int fop = fg.getOpacity(); |
| 1227 | int bop = bg.getOpacity(); |
| 1228 | if (false) |
Filip Gruszczynski | 34dab0b | 2015-12-22 08:29:07 -0800 | [diff] [blame] | 1229 | Log.v(mLogTag, "Background opacity: " + bop + ", Frame opacity: " + fop); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1230 | if (fop == PixelFormat.OPAQUE || bop == PixelFormat.OPAQUE) { |
| 1231 | opacity = PixelFormat.OPAQUE; |
| 1232 | } else if (fop == PixelFormat.UNKNOWN) { |
| 1233 | opacity = bop; |
| 1234 | } else if (bop == PixelFormat.UNKNOWN) { |
| 1235 | opacity = fop; |
| 1236 | } else { |
| 1237 | opacity = Drawable.resolveOpacity(fop, bop); |
| 1238 | } |
| 1239 | } else { |
| 1240 | // For now we have to assume translucent if there is a |
| 1241 | // frame with padding... there is no way to tell if the |
| 1242 | // frame and background together will draw all pixels. |
| 1243 | if (false) |
Filip Gruszczynski | 34dab0b | 2015-12-22 08:29:07 -0800 | [diff] [blame] | 1244 | Log.v(mLogTag, "Padding: " + mFramePadding); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1245 | opacity = PixelFormat.TRANSLUCENT; |
| 1246 | } |
| 1247 | } |
| 1248 | if (false) |
Filip Gruszczynski | 34dab0b | 2015-12-22 08:29:07 -0800 | [diff] [blame] | 1249 | Log.v(mLogTag, "Background: " + bg + ", Frame: " + fg); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1250 | } |
| 1251 | |
| 1252 | if (false) |
Filip Gruszczynski | 34dab0b | 2015-12-22 08:29:07 -0800 | [diff] [blame] | 1253 | Log.v(mLogTag, "Selected default opacity: " + opacity); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1254 | |
| 1255 | mDefaultOpacity = opacity; |
| 1256 | if (mFeatureId < 0) { |
| 1257 | mWindow.setDefaultWindowFormat(opacity); |
| 1258 | } |
| 1259 | } |
| 1260 | |
| 1261 | @Override |
| 1262 | public void onWindowFocusChanged(boolean hasWindowFocus) { |
| 1263 | super.onWindowFocusChanged(hasWindowFocus); |
| 1264 | |
| 1265 | // If the user is chording a menu shortcut, release the chord since |
| 1266 | // this window lost focus |
| 1267 | if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) && !hasWindowFocus |
| 1268 | && mWindow.mPanelChordingKey != 0) { |
| 1269 | mWindow.closePanel(Window.FEATURE_OPTIONS_PANEL); |
| 1270 | } |
| 1271 | |
| 1272 | final Window.Callback cb = mWindow.getCallback(); |
| 1273 | if (cb != null && !mWindow.isDestroyed() && mFeatureId < 0) { |
| 1274 | cb.onWindowFocusChanged(hasWindowFocus); |
| 1275 | } |
| 1276 | |
| 1277 | if (mPrimaryActionMode != null) { |
| 1278 | mPrimaryActionMode.onWindowFocusChanged(hasWindowFocus); |
| 1279 | } |
| 1280 | if (mFloatingActionMode != null) { |
| 1281 | mFloatingActionMode.onWindowFocusChanged(hasWindowFocus); |
| 1282 | } |
Wale Ogunwale | 2b547c3 | 2015-11-18 10:33:22 -0800 | [diff] [blame] | 1283 | |
| 1284 | updateElevation(); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1285 | } |
| 1286 | |
| 1287 | @Override |
| 1288 | protected void onAttachedToWindow() { |
| 1289 | super.onAttachedToWindow(); |
| 1290 | |
| 1291 | final Window.Callback cb = mWindow.getCallback(); |
| 1292 | if (cb != null && !mWindow.isDestroyed() && mFeatureId < 0) { |
| 1293 | cb.onAttachedToWindow(); |
| 1294 | } |
| 1295 | |
| 1296 | if (mFeatureId == -1) { |
| 1297 | /* |
| 1298 | * The main window has been attached, try to restore any panels |
| 1299 | * that may have been open before. This is called in cases where |
| 1300 | * an activity is being killed for configuration change and the |
| 1301 | * menu was open. When the activity is recreated, the menu |
| 1302 | * should be shown again. |
| 1303 | */ |
| 1304 | mWindow.openPanelsAfterRestore(); |
| 1305 | } |
Wale Ogunwale | bf9eefc | 2015-11-17 14:47:52 -0800 | [diff] [blame] | 1306 | |
| 1307 | if (!mWindowResizeCallbacksAdded) { |
| 1308 | // If there is no window callback installed there was no window set before. Set it now. |
| 1309 | // Note that our ViewRootImpl object will not change. |
| 1310 | getViewRootImpl().addWindowCallbacks(this); |
| 1311 | mWindowResizeCallbacksAdded = true; |
| 1312 | } else if (mBackdropFrameRenderer != null) { |
| 1313 | // We are resizing and this call happened due to a configuration change. Tell the |
| 1314 | // renderer about it. |
| 1315 | mBackdropFrameRenderer.onConfigurationChange(); |
| 1316 | } |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1317 | } |
| 1318 | |
| 1319 | @Override |
| 1320 | protected void onDetachedFromWindow() { |
| 1321 | super.onDetachedFromWindow(); |
| 1322 | |
| 1323 | final Window.Callback cb = mWindow.getCallback(); |
| 1324 | if (cb != null && mFeatureId < 0) { |
| 1325 | cb.onDetachedFromWindow(); |
| 1326 | } |
| 1327 | |
| 1328 | if (mWindow.mDecorContentParent != null) { |
| 1329 | mWindow.mDecorContentParent.dismissPopups(); |
| 1330 | } |
| 1331 | |
| 1332 | if (mPrimaryActionModePopup != null) { |
| 1333 | removeCallbacks(mShowPrimaryActionModePopup); |
| 1334 | if (mPrimaryActionModePopup.isShowing()) { |
| 1335 | mPrimaryActionModePopup.dismiss(); |
| 1336 | } |
| 1337 | mPrimaryActionModePopup = null; |
| 1338 | } |
| 1339 | if (mFloatingToolbar != null) { |
| 1340 | mFloatingToolbar.dismiss(); |
| 1341 | mFloatingToolbar = null; |
| 1342 | } |
| 1343 | |
| 1344 | PhoneWindow.PanelFeatureState st = mWindow.getPanelState(Window.FEATURE_OPTIONS_PANEL, false); |
| 1345 | if (st != null && st.menu != null && mFeatureId < 0) { |
| 1346 | st.menu.close(); |
| 1347 | } |
Wale Ogunwale | bf9eefc | 2015-11-17 14:47:52 -0800 | [diff] [blame] | 1348 | |
| 1349 | if (mWindowResizeCallbacksAdded) { |
| 1350 | getViewRootImpl().removeWindowCallbacks(this); |
| 1351 | mWindowResizeCallbacksAdded = false; |
| 1352 | } |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1353 | } |
| 1354 | |
| 1355 | @Override |
| 1356 | public void onCloseSystemDialogs(String reason) { |
| 1357 | if (mFeatureId >= 0) { |
| 1358 | mWindow.closeAllPanels(); |
| 1359 | } |
| 1360 | } |
| 1361 | |
| 1362 | public android.view.SurfaceHolder.Callback2 willYouTakeTheSurface() { |
| 1363 | return mFeatureId < 0 ? mWindow.mTakeSurfaceCallback : null; |
| 1364 | } |
| 1365 | |
| 1366 | public InputQueue.Callback willYouTakeTheInputQueue() { |
| 1367 | return mFeatureId < 0 ? mWindow.mTakeInputQueueCallback : null; |
| 1368 | } |
| 1369 | |
| 1370 | public void setSurfaceType(int type) { |
| 1371 | mWindow.setType(type); |
| 1372 | } |
| 1373 | |
| 1374 | public void setSurfaceFormat(int format) { |
| 1375 | mWindow.setFormat(format); |
| 1376 | } |
| 1377 | |
| 1378 | public void setSurfaceKeepScreenOn(boolean keepOn) { |
| 1379 | if (keepOn) mWindow.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); |
| 1380 | else mWindow.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); |
| 1381 | } |
| 1382 | |
| 1383 | @Override |
| 1384 | public void onRootViewScrollYChanged(int rootScrollY) { |
| 1385 | mRootScrollY = rootScrollY; |
| 1386 | updateColorViewTranslations(); |
| 1387 | } |
| 1388 | |
| 1389 | private ActionMode createActionMode( |
| 1390 | int type, ActionMode.Callback2 callback, View originatingView) { |
| 1391 | switch (type) { |
| 1392 | case ActionMode.TYPE_PRIMARY: |
| 1393 | default: |
| 1394 | return createStandaloneActionMode(callback); |
| 1395 | case ActionMode.TYPE_FLOATING: |
| 1396 | return createFloatingActionMode(originatingView, callback); |
| 1397 | } |
| 1398 | } |
| 1399 | |
| 1400 | private void setHandledActionMode(ActionMode mode) { |
| 1401 | if (mode.getType() == ActionMode.TYPE_PRIMARY) { |
| 1402 | setHandledPrimaryActionMode(mode); |
| 1403 | } else if (mode.getType() == ActionMode.TYPE_FLOATING) { |
| 1404 | setHandledFloatingActionMode(mode); |
| 1405 | } |
| 1406 | } |
| 1407 | |
| 1408 | private ActionMode createStandaloneActionMode(ActionMode.Callback callback) { |
| 1409 | endOnGoingFadeAnimation(); |
| 1410 | cleanupPrimaryActionMode(); |
| 1411 | if (mPrimaryActionModeView == null) { |
| 1412 | if (mWindow.isFloating()) { |
| 1413 | // Use the action bar theme. |
| 1414 | final TypedValue outValue = new TypedValue(); |
| 1415 | final Resources.Theme baseTheme = mContext.getTheme(); |
| 1416 | baseTheme.resolveAttribute(R.attr.actionBarTheme, outValue, true); |
| 1417 | |
| 1418 | final Context actionBarContext; |
| 1419 | if (outValue.resourceId != 0) { |
| 1420 | final Resources.Theme actionBarTheme = mContext.getResources().newTheme(); |
| 1421 | actionBarTheme.setTo(baseTheme); |
| 1422 | actionBarTheme.applyStyle(outValue.resourceId, true); |
| 1423 | |
| 1424 | actionBarContext = new ContextThemeWrapper(mContext, 0); |
| 1425 | actionBarContext.getTheme().setTo(actionBarTheme); |
| 1426 | } else { |
| 1427 | actionBarContext = mContext; |
| 1428 | } |
| 1429 | |
| 1430 | mPrimaryActionModeView = new ActionBarContextView(actionBarContext); |
| 1431 | mPrimaryActionModePopup = new PopupWindow(actionBarContext, null, |
| 1432 | R.attr.actionModePopupWindowStyle); |
| 1433 | mPrimaryActionModePopup.setWindowLayoutType( |
| 1434 | WindowManager.LayoutParams.TYPE_APPLICATION); |
| 1435 | mPrimaryActionModePopup.setContentView(mPrimaryActionModeView); |
| 1436 | mPrimaryActionModePopup.setWidth(MATCH_PARENT); |
| 1437 | |
| 1438 | actionBarContext.getTheme().resolveAttribute( |
| 1439 | R.attr.actionBarSize, outValue, true); |
| 1440 | final int height = TypedValue.complexToDimensionPixelSize(outValue.data, |
| 1441 | actionBarContext.getResources().getDisplayMetrics()); |
| 1442 | mPrimaryActionModeView.setContentHeight(height); |
| 1443 | mPrimaryActionModePopup.setHeight(WRAP_CONTENT); |
| 1444 | mShowPrimaryActionModePopup = new Runnable() { |
| 1445 | public void run() { |
| 1446 | mPrimaryActionModePopup.showAtLocation( |
| 1447 | mPrimaryActionModeView.getApplicationWindowToken(), |
| 1448 | Gravity.TOP | Gravity.FILL_HORIZONTAL, 0, 0); |
| 1449 | endOnGoingFadeAnimation(); |
| 1450 | mFadeAnim = ObjectAnimator.ofFloat(mPrimaryActionModeView, View.ALPHA, |
| 1451 | 0f, 1f); |
| 1452 | mFadeAnim.addListener(new Animator.AnimatorListener() { |
| 1453 | @Override |
| 1454 | public void onAnimationStart(Animator animation) { |
| 1455 | mPrimaryActionModeView.setVisibility(VISIBLE); |
| 1456 | } |
| 1457 | |
| 1458 | @Override |
| 1459 | public void onAnimationEnd(Animator animation) { |
| 1460 | mPrimaryActionModeView.setAlpha(1f); |
| 1461 | mFadeAnim = null; |
| 1462 | } |
| 1463 | |
| 1464 | @Override |
| 1465 | public void onAnimationCancel(Animator animation) { |
| 1466 | |
| 1467 | } |
| 1468 | |
| 1469 | @Override |
| 1470 | public void onAnimationRepeat(Animator animation) { |
| 1471 | |
| 1472 | } |
| 1473 | }); |
| 1474 | mFadeAnim.start(); |
| 1475 | } |
| 1476 | }; |
| 1477 | } else { |
| 1478 | ViewStub stub = (ViewStub) findViewById(R.id.action_mode_bar_stub); |
| 1479 | if (stub != null) { |
| 1480 | mPrimaryActionModeView = (ActionBarContextView) stub.inflate(); |
| 1481 | } |
| 1482 | } |
| 1483 | } |
| 1484 | if (mPrimaryActionModeView != null) { |
| 1485 | mPrimaryActionModeView.killMode(); |
| 1486 | ActionMode mode = new StandaloneActionMode( |
| 1487 | mPrimaryActionModeView.getContext(), mPrimaryActionModeView, |
| 1488 | callback, mPrimaryActionModePopup == null); |
| 1489 | return mode; |
| 1490 | } |
| 1491 | return null; |
| 1492 | } |
| 1493 | |
| 1494 | private void endOnGoingFadeAnimation() { |
| 1495 | if (mFadeAnim != null) { |
| 1496 | mFadeAnim.end(); |
| 1497 | } |
| 1498 | } |
| 1499 | |
| 1500 | private void setHandledPrimaryActionMode(ActionMode mode) { |
| 1501 | endOnGoingFadeAnimation(); |
| 1502 | mPrimaryActionMode = mode; |
| 1503 | mPrimaryActionMode.invalidate(); |
| 1504 | mPrimaryActionModeView.initForMode(mPrimaryActionMode); |
| 1505 | if (mPrimaryActionModePopup != null) { |
| 1506 | post(mShowPrimaryActionModePopup); |
| 1507 | } else { |
| 1508 | mFadeAnim = ObjectAnimator.ofFloat(mPrimaryActionModeView, View.ALPHA, 0f, 1f); |
| 1509 | mFadeAnim.addListener(new Animator.AnimatorListener() { |
| 1510 | @Override |
| 1511 | public void onAnimationStart(Animator animation) { |
| 1512 | mPrimaryActionModeView.setVisibility(View.VISIBLE); |
| 1513 | } |
| 1514 | |
| 1515 | @Override |
| 1516 | public void onAnimationEnd(Animator animation) { |
| 1517 | mPrimaryActionModeView.setAlpha(1f); |
| 1518 | mFadeAnim = null; |
| 1519 | } |
| 1520 | |
| 1521 | @Override |
| 1522 | public void onAnimationCancel(Animator animation) { |
| 1523 | |
| 1524 | } |
| 1525 | |
| 1526 | @Override |
| 1527 | public void onAnimationRepeat(Animator animation) { |
| 1528 | |
| 1529 | } |
| 1530 | }); |
| 1531 | mFadeAnim.start(); |
| 1532 | } |
| 1533 | mPrimaryActionModeView.sendAccessibilityEvent( |
| 1534 | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED); |
| 1535 | } |
| 1536 | |
| 1537 | private ActionMode createFloatingActionMode( |
| 1538 | View originatingView, ActionMode.Callback2 callback) { |
| 1539 | if (mFloatingActionMode != null) { |
| 1540 | mFloatingActionMode.finish(); |
| 1541 | } |
| 1542 | cleanupFloatingActionModeViews(); |
| 1543 | final FloatingActionMode mode = |
| 1544 | new FloatingActionMode(mContext, callback, originatingView); |
| 1545 | mFloatingActionModeOriginatingView = originatingView; |
| 1546 | mFloatingToolbarPreDrawListener = |
| 1547 | new ViewTreeObserver.OnPreDrawListener() { |
| 1548 | @Override |
| 1549 | public boolean onPreDraw() { |
| 1550 | mode.updateViewLocationInWindow(); |
| 1551 | return true; |
| 1552 | } |
| 1553 | }; |
| 1554 | return mode; |
| 1555 | } |
| 1556 | |
| 1557 | private void setHandledFloatingActionMode(ActionMode mode) { |
| 1558 | mFloatingActionMode = mode; |
| 1559 | mFloatingToolbar = new FloatingToolbar(mContext, mWindow); |
| 1560 | ((FloatingActionMode) mFloatingActionMode).setFloatingToolbar(mFloatingToolbar); |
| 1561 | mFloatingActionMode.invalidate(); // Will show the floating toolbar if necessary. |
| 1562 | mFloatingActionModeOriginatingView.getViewTreeObserver() |
| 1563 | .addOnPreDrawListener(mFloatingToolbarPreDrawListener); |
| 1564 | } |
| 1565 | |
| 1566 | /** |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 1567 | * Informs the decor if the caption is attached and visible. |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1568 | * @param attachedAndVisible true when the decor is visible. |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 1569 | * Note that this will even be called if there is no caption. |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1570 | **/ |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 1571 | void enableCaption(boolean attachedAndVisible) { |
| 1572 | if (mHasCaption != attachedAndVisible) { |
| 1573 | mHasCaption = attachedAndVisible; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1574 | if (getForeground() != null) { |
| 1575 | drawableChanged(); |
| 1576 | } |
| 1577 | } |
| 1578 | } |
| 1579 | |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1580 | void setWindow(PhoneWindow phoneWindow) { |
| 1581 | mWindow = phoneWindow; |
| 1582 | Context context = getContext(); |
| 1583 | if (context instanceof DecorContext) { |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 1584 | DecorContext decorContext = (DecorContext) context; |
| 1585 | decorContext.setPhoneWindow(mWindow); |
| 1586 | } |
| 1587 | } |
| 1588 | |
| 1589 | void onConfigurationChanged() { |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 1590 | int workspaceId = getStackId(); |
Wale Ogunwale | eb6722c | 2015-12-08 11:43:43 -0800 | [diff] [blame] | 1591 | if (mStackId != workspaceId) { |
| 1592 | mStackId = workspaceId; |
| 1593 | if (mDecorCaptionView == null && StackId.hasWindowDecor(mStackId)) { |
| 1594 | // Configuration now requires a caption. |
| 1595 | final LayoutInflater inflater = mWindow.getLayoutInflater(); |
| 1596 | mDecorCaptionView = createDecorCaptionView(inflater); |
| 1597 | if (mDecorCaptionView != null) { |
| 1598 | if (mDecorCaptionView.getParent() == null) { |
| 1599 | addView(mDecorCaptionView, 0, |
| 1600 | new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT)); |
| 1601 | } |
| 1602 | removeView(mContentRoot); |
| 1603 | mDecorCaptionView.addView(mContentRoot, |
| 1604 | new ViewGroup.MarginLayoutParams(MATCH_PARENT, MATCH_PARENT)); |
| 1605 | } |
| 1606 | } else if (mDecorCaptionView != null) { |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 1607 | // We might have to change the kind of surface before we do anything else. |
Wale Ogunwale | eb6722c | 2015-12-08 11:43:43 -0800 | [diff] [blame] | 1608 | mDecorCaptionView.onConfigurationChanged(StackId.hasWindowDecor(mStackId)); |
| 1609 | enableCaption(StackId.hasWindowDecor(workspaceId)); |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 1610 | } |
| 1611 | } |
Filip Gruszczynski | 34dab0b | 2015-12-22 08:29:07 -0800 | [diff] [blame] | 1612 | updateAvailableWidth(); |
Wale Ogunwale | 2b547c3 | 2015-11-18 10:33:22 -0800 | [diff] [blame] | 1613 | initializeElevation(); |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 1614 | } |
| 1615 | |
Filip Gruszczynski | 3dec081 | 2015-12-09 08:42:41 -0800 | [diff] [blame] | 1616 | void onResourcesLoaded(LayoutInflater inflater, int layoutResource) { |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 1617 | mStackId = getStackId(); |
Wale Ogunwale | 8cc5a74 | 2015-11-17 15:41:05 -0800 | [diff] [blame] | 1618 | |
Wale Ogunwale | bf9eefc | 2015-11-17 14:47:52 -0800 | [diff] [blame] | 1619 | if (mBackdropFrameRenderer != null) { |
Filip Gruszczynski | a40fd09 | 2016-01-07 16:38:11 -0800 | [diff] [blame^] | 1620 | loadBackgroundDrawablesIfNeeded(); |
Wale Ogunwale | bf9eefc | 2015-11-17 14:47:52 -0800 | [diff] [blame] | 1621 | mBackdropFrameRenderer.onResourcesLoaded( |
Jorim Jaggi | 04c2fbd | 2015-12-02 15:11:45 -0800 | [diff] [blame] | 1622 | this, mResizingBackgroundDrawable, mCaptionBackgroundDrawable, |
Filip Gruszczynski | 3dec081 | 2015-12-09 08:42:41 -0800 | [diff] [blame] | 1623 | mUserCaptionBackgroundDrawable, getCurrentColor(mStatusColorViewState)); |
Wale Ogunwale | bf9eefc | 2015-11-17 14:47:52 -0800 | [diff] [blame] | 1624 | } |
| 1625 | |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 1626 | mDecorCaptionView = createDecorCaptionView(inflater); |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 1627 | final View root = inflater.inflate(layoutResource, null); |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 1628 | if (mDecorCaptionView != null) { |
| 1629 | if (mDecorCaptionView.getParent() == null) { |
| 1630 | addView(mDecorCaptionView, |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 1631 | new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT)); |
| 1632 | } |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 1633 | mDecorCaptionView.addView(root, |
Filip Gruszczynski | 6325065 | 2015-11-18 14:43:01 -0800 | [diff] [blame] | 1634 | new ViewGroup.MarginLayoutParams(MATCH_PARENT, MATCH_PARENT)); |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 1635 | } else { |
| 1636 | addView(root, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT)); |
| 1637 | } |
| 1638 | mContentRoot = (ViewGroup) root; |
Wale Ogunwale | 2b547c3 | 2015-11-18 10:33:22 -0800 | [diff] [blame] | 1639 | initializeElevation(); |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 1640 | } |
| 1641 | |
Filip Gruszczynski | a40fd09 | 2016-01-07 16:38:11 -0800 | [diff] [blame^] | 1642 | private void loadBackgroundDrawablesIfNeeded() { |
| 1643 | if (mResizingBackgroundDrawable == null) { |
| 1644 | mResizingBackgroundDrawable = getResizingBackgroundDrawable( |
| 1645 | mWindow.mBackgroundResource, mWindow.mBackgroundFallbackResource); |
| 1646 | } |
| 1647 | if (mCaptionBackgroundDrawable == null) { |
| 1648 | mCaptionBackgroundDrawable = getContext().getDrawable( |
| 1649 | R.drawable.decor_caption_title_focused); |
| 1650 | } |
| 1651 | } |
| 1652 | |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 1653 | // Free floating overlapping windows require a caption. |
| 1654 | private DecorCaptionView createDecorCaptionView(LayoutInflater inflater) { |
Filip Gruszczynski | 3dec081 | 2015-12-09 08:42:41 -0800 | [diff] [blame] | 1655 | DecorCaptionView decorCaptionView = null; |
| 1656 | for (int i = getChildCount() - 1; i >= 0 && decorCaptionView == null; i--) { |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 1657 | View view = getChildAt(i); |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 1658 | if (view instanceof DecorCaptionView) { |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 1659 | // The decor was most likely saved from a relaunch - so reuse it. |
Filip Gruszczynski | 3dec081 | 2015-12-09 08:42:41 -0800 | [diff] [blame] | 1660 | decorCaptionView = (DecorCaptionView) view; |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 1661 | removeViewAt(i); |
| 1662 | } |
| 1663 | } |
| 1664 | final WindowManager.LayoutParams attrs = mWindow.getAttributes(); |
Wale Ogunwale | 2b547c3 | 2015-11-18 10:33:22 -0800 | [diff] [blame] | 1665 | final boolean isApplication = attrs.type == TYPE_BASE_APPLICATION || |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 1666 | attrs.type == TYPE_APPLICATION; |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 1667 | // Only a non floating application window on one of the allowed workspaces can get a caption |
Wale Ogunwale | eb6722c | 2015-12-08 11:43:43 -0800 | [diff] [blame] | 1668 | if (!mWindow.isFloating() && isApplication && StackId.hasWindowDecor(mStackId)) { |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 1669 | // Dependent on the brightness of the used title we either use the |
| 1670 | // dark or the light button frame. |
Filip Gruszczynski | 3dec081 | 2015-12-09 08:42:41 -0800 | [diff] [blame] | 1671 | if (decorCaptionView == null) { |
| 1672 | decorCaptionView = inflateDecorCaptionView(inflater); |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 1673 | } |
Filip Gruszczynski | 3dec081 | 2015-12-09 08:42:41 -0800 | [diff] [blame] | 1674 | decorCaptionView.setPhoneWindow(mWindow, true /*showDecor*/); |
Wale Ogunwale | 8cc5a74 | 2015-11-17 15:41:05 -0800 | [diff] [blame] | 1675 | } else { |
Filip Gruszczynski | 3dec081 | 2015-12-09 08:42:41 -0800 | [diff] [blame] | 1676 | decorCaptionView = null; |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 1677 | } |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 1678 | |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 1679 | // Tell the decor if it has a visible caption. |
Filip Gruszczynski | 3dec081 | 2015-12-09 08:42:41 -0800 | [diff] [blame] | 1680 | enableCaption(decorCaptionView != null); |
| 1681 | return decorCaptionView; |
| 1682 | } |
| 1683 | |
| 1684 | private DecorCaptionView inflateDecorCaptionView(LayoutInflater inflater) { |
| 1685 | final Context context = getContext(); |
| 1686 | // We make a copy of the inflater, so it has the right context associated with it. |
| 1687 | inflater = inflater.from(context); |
| 1688 | final DecorCaptionView view = (DecorCaptionView) inflater.inflate(R.layout.decor_caption, |
| 1689 | null); |
| 1690 | setDecorCaptionShade(context, view); |
| 1691 | return view; |
| 1692 | } |
| 1693 | |
| 1694 | private void setDecorCaptionShade(Context context, DecorCaptionView view) { |
| 1695 | final int shade = mWindow.getDecorCaptionShade(); |
| 1696 | switch (shade) { |
| 1697 | case DECOR_CAPTION_SHADE_LIGHT: |
| 1698 | setLightDecorCaptionShade(view); |
| 1699 | break; |
| 1700 | case DECOR_CAPTION_SHADE_DARK: |
| 1701 | setDarkDecorCaptionShade(view); |
| 1702 | break; |
| 1703 | default: { |
| 1704 | TypedValue value = new TypedValue(); |
| 1705 | context.getTheme().resolveAttribute(R.attr.colorPrimary, value, true); |
| 1706 | // We invert the shade depending on brightness of the theme. Dark shade for light |
| 1707 | // theme and vice versa. Thanks to this the buttons should be visible on the |
| 1708 | // background. |
| 1709 | if (Color.luminance(value.data) < 0.5) { |
| 1710 | setLightDecorCaptionShade(view); |
| 1711 | } else { |
| 1712 | setDarkDecorCaptionShade(view); |
| 1713 | } |
| 1714 | break; |
| 1715 | } |
| 1716 | } |
| 1717 | } |
| 1718 | |
| 1719 | void updateDecorCaptionShade() { |
| 1720 | if (mDecorCaptionView != null) { |
| 1721 | setDecorCaptionShade(getContext(), mDecorCaptionView); |
| 1722 | } |
| 1723 | } |
| 1724 | |
| 1725 | private void setLightDecorCaptionShade(DecorCaptionView view) { |
| 1726 | view.findViewById(R.id.maximize_window).setBackgroundResource( |
| 1727 | R.drawable.decor_maximize_button_light); |
| 1728 | view.findViewById(R.id.close_window).setBackgroundResource( |
| 1729 | R.drawable.decor_close_button_light); |
| 1730 | } |
| 1731 | |
| 1732 | private void setDarkDecorCaptionShade(DecorCaptionView view) { |
| 1733 | view.findViewById(R.id.maximize_window).setBackgroundResource( |
| 1734 | R.drawable.decor_maximize_button_dark); |
| 1735 | view.findViewById(R.id.close_window).setBackgroundResource( |
| 1736 | R.drawable.decor_close_button_dark); |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 1737 | } |
| 1738 | |
| 1739 | /** |
Wale Ogunwale | bf9eefc | 2015-11-17 14:47:52 -0800 | [diff] [blame] | 1740 | * Returns the color used to fill areas the app has not rendered content to yet when the |
| 1741 | * user is resizing the window of an activity in multi-window mode. |
| 1742 | */ |
| 1743 | private Drawable getResizingBackgroundDrawable(int backgroundRes, int backgroundFallbackRes) { |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 1744 | final Context context = getContext(); |
| 1745 | |
Wale Ogunwale | bf9eefc | 2015-11-17 14:47:52 -0800 | [diff] [blame] | 1746 | if (backgroundRes != 0) { |
| 1747 | final Drawable drawable = context.getDrawable(backgroundRes); |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 1748 | if (drawable != null) { |
| 1749 | return drawable; |
| 1750 | } |
| 1751 | } |
| 1752 | |
Wale Ogunwale | bf9eefc | 2015-11-17 14:47:52 -0800 | [diff] [blame] | 1753 | if (backgroundFallbackRes != 0) { |
| 1754 | final Drawable fallbackDrawable = context.getDrawable(backgroundFallbackRes); |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 1755 | if (fallbackDrawable != null) { |
| 1756 | return fallbackDrawable; |
| 1757 | } |
| 1758 | } |
| 1759 | |
| 1760 | // We shouldn't really get here as the background fallback should be always available since |
| 1761 | // it is defaulted by the system. |
Filip Gruszczynski | 34dab0b | 2015-12-22 08:29:07 -0800 | [diff] [blame] | 1762 | Log.w(mLogTag, "Failed to find background drawable for PhoneWindow=" + mWindow); |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 1763 | return null; |
| 1764 | } |
| 1765 | |
| 1766 | /** |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 1767 | * Returns the Id of the stack which contains this window. |
| 1768 | * Note that if no stack can be determined - which usually means that it was not |
| 1769 | * created for an activity - the fullscreen stack ID will be returned. |
| 1770 | * @return Returns the stack id which contains this window. |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 1771 | **/ |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 1772 | private int getStackId() { |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 1773 | int workspaceId = INVALID_STACK_ID; |
| 1774 | final Window.WindowControllerCallback callback = mWindow.getWindowControllerCallback(); |
| 1775 | if (callback != null) { |
| 1776 | try { |
| 1777 | workspaceId = callback.getWindowStackId(); |
| 1778 | } catch (RemoteException ex) { |
Filip Gruszczynski | 34dab0b | 2015-12-22 08:29:07 -0800 | [diff] [blame] | 1779 | Log.e(mLogTag, "Failed to get the workspace ID of a PhoneWindow."); |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 1780 | } |
| 1781 | } |
| 1782 | if (workspaceId == INVALID_STACK_ID) { |
| 1783 | return FULLSCREEN_WORKSPACE_STACK_ID; |
| 1784 | } |
| 1785 | return workspaceId; |
| 1786 | } |
| 1787 | |
| 1788 | void clearContentView() { |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 1789 | if (mDecorCaptionView != null) { |
Filip Gruszczynski | 6325065 | 2015-11-18 14:43:01 -0800 | [diff] [blame] | 1790 | mDecorCaptionView.removeContentView(); |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 1791 | } else { |
Jorim Jaggi | 6e0ce28 | 2015-12-01 15:19:49 -0800 | [diff] [blame] | 1792 | // This window doesn't have caption, so we need to remove everything except our views |
| 1793 | // we might have added. |
| 1794 | for (int i = getChildCount() - 1; i >= 0; i--) { |
| 1795 | View v = getChildAt(i); |
| 1796 | if (v != mStatusColorViewState.view && v != mNavigationColorViewState.view |
| 1797 | && v != mStatusGuard && v != mNavigationGuard) { |
| 1798 | removeViewAt(i); |
| 1799 | } |
| 1800 | } |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1801 | } |
| 1802 | } |
| 1803 | |
Wale Ogunwale | bf9eefc | 2015-11-17 14:47:52 -0800 | [diff] [blame] | 1804 | @Override |
| 1805 | public void onWindowSizeIsChanging(Rect newBounds) { |
| 1806 | if (mBackdropFrameRenderer != null) { |
| 1807 | mBackdropFrameRenderer.setTargetRect(newBounds); |
| 1808 | } |
| 1809 | } |
| 1810 | |
| 1811 | @Override |
| 1812 | public void onWindowDragResizeStart(Rect initialBounds) { |
| 1813 | if (mWindow.isDestroyed()) { |
| 1814 | // If the owner's window is gone, we should not be able to come here anymore. |
| 1815 | releaseThreadedRenderer(); |
| 1816 | return; |
| 1817 | } |
| 1818 | if (mBackdropFrameRenderer != null) { |
| 1819 | return; |
| 1820 | } |
Filip Gruszczynski | 3dec081 | 2015-12-09 08:42:41 -0800 | [diff] [blame] | 1821 | final ThreadedRenderer renderer = getHardwareRenderer(); |
Wale Ogunwale | bf9eefc | 2015-11-17 14:47:52 -0800 | [diff] [blame] | 1822 | if (renderer != null) { |
Filip Gruszczynski | a40fd09 | 2016-01-07 16:38:11 -0800 | [diff] [blame^] | 1823 | loadBackgroundDrawablesIfNeeded(); |
Wale Ogunwale | bf9eefc | 2015-11-17 14:47:52 -0800 | [diff] [blame] | 1824 | mBackdropFrameRenderer = new BackdropFrameRenderer(this, renderer, |
Jorim Jaggi | 04c2fbd | 2015-12-02 15:11:45 -0800 | [diff] [blame] | 1825 | initialBounds, mResizingBackgroundDrawable, mCaptionBackgroundDrawable, |
Filip Gruszczynski | 3dec081 | 2015-12-09 08:42:41 -0800 | [diff] [blame] | 1826 | mUserCaptionBackgroundDrawable, getCurrentColor(mStatusColorViewState)); |
Wale Ogunwale | bf9eefc | 2015-11-17 14:47:52 -0800 | [diff] [blame] | 1827 | |
| 1828 | // Get rid of the shadow while we are resizing. Shadow drawing takes considerable time. |
| 1829 | // If we want to get the shadow shown while resizing, we would need to elevate a new |
| 1830 | // element which owns the caption and has the elevation. |
| 1831 | updateElevation(); |
Jorim Jaggi | 04c2fbd | 2015-12-02 15:11:45 -0800 | [diff] [blame] | 1832 | |
| 1833 | updateColorViews(null /* insets */, false); |
Wale Ogunwale | bf9eefc | 2015-11-17 14:47:52 -0800 | [diff] [blame] | 1834 | } |
| 1835 | } |
| 1836 | |
| 1837 | @Override |
| 1838 | public void onWindowDragResizeEnd() { |
| 1839 | releaseThreadedRenderer(); |
Jorim Jaggi | 04c2fbd | 2015-12-02 15:11:45 -0800 | [diff] [blame] | 1840 | updateColorViews(null /* insets */, false); |
Wale Ogunwale | bf9eefc | 2015-11-17 14:47:52 -0800 | [diff] [blame] | 1841 | } |
| 1842 | |
| 1843 | @Override |
| 1844 | public boolean onContentDrawn(int offsetX, int offsetY, int sizeX, int sizeY) { |
| 1845 | if (mBackdropFrameRenderer == null) { |
| 1846 | return false; |
| 1847 | } |
| 1848 | return mBackdropFrameRenderer.onContentDrawn(offsetX, offsetY, sizeX, sizeY); |
| 1849 | } |
| 1850 | |
| 1851 | @Override |
| 1852 | public void onRequestDraw(boolean reportNextDraw) { |
| 1853 | if (mBackdropFrameRenderer != null) { |
| 1854 | mBackdropFrameRenderer.onRequestDraw(reportNextDraw); |
| 1855 | } else if (reportNextDraw) { |
| 1856 | // If render thread is gone, just report immediately. |
| 1857 | if (isAttachedToWindow()) { |
| 1858 | getViewRootImpl().reportDrawFinish(); |
| 1859 | } |
| 1860 | } |
| 1861 | } |
| 1862 | |
| 1863 | /** Release the renderer thread which is usually done when the user stops resizing. */ |
| 1864 | private void releaseThreadedRenderer() { |
| 1865 | if (mBackdropFrameRenderer != null) { |
| 1866 | mBackdropFrameRenderer.releaseRenderer(); |
| 1867 | mBackdropFrameRenderer = null; |
| 1868 | // Bring the shadow back. |
| 1869 | updateElevation(); |
| 1870 | } |
| 1871 | } |
| 1872 | |
Jorim Jaggi | 04c2fbd | 2015-12-02 15:11:45 -0800 | [diff] [blame] | 1873 | private boolean isResizing() { |
| 1874 | return mBackdropFrameRenderer != null; |
| 1875 | } |
| 1876 | |
Wale Ogunwale | 2b547c3 | 2015-11-18 10:33:22 -0800 | [diff] [blame] | 1877 | /** |
| 1878 | * The elevation gets set for the first time and the framework needs to be informed that |
| 1879 | * the surface layer gets created with the shadow size in mind. |
| 1880 | */ |
| 1881 | private void initializeElevation() { |
| 1882 | // TODO(skuhne): Call setMaxElevation here accordingly after b/22668382 got fixed. |
| 1883 | mAllowUpdateElevation = false; |
| 1884 | updateElevation(); |
| 1885 | } |
| 1886 | |
Wale Ogunwale | bf9eefc | 2015-11-17 14:47:52 -0800 | [diff] [blame] | 1887 | private void updateElevation() { |
Wale Ogunwale | 2b547c3 | 2015-11-18 10:33:22 -0800 | [diff] [blame] | 1888 | float elevation = 0; |
| 1889 | final boolean wasAdjustedForStack = mElevationAdjustedForStack; |
| 1890 | // Do not use a shadow when we are in resizing mode (mBackdropFrameRenderer not null) |
| 1891 | // since the shadow is bound to the content size and not the target size. |
Wale Ogunwale | eb6722c | 2015-12-08 11:43:43 -0800 | [diff] [blame] | 1892 | if (StackId.hasWindowShadow(mStackId) && !isResizing()) { |
Wale Ogunwale | 2b547c3 | 2015-11-18 10:33:22 -0800 | [diff] [blame] | 1893 | elevation = hasWindowFocus() ? |
| 1894 | DECOR_SHADOW_FOCUSED_HEIGHT_IN_DIP : DECOR_SHADOW_UNFOCUSED_HEIGHT_IN_DIP; |
| 1895 | // TODO(skuhne): Remove this if clause once b/22668382 got fixed. |
| 1896 | if (!mAllowUpdateElevation) { |
| 1897 | elevation = DECOR_SHADOW_FOCUSED_HEIGHT_IN_DIP; |
| 1898 | } |
| 1899 | // Convert the DP elevation into physical pixels. |
| 1900 | elevation = dipToPx(elevation); |
| 1901 | mElevationAdjustedForStack = true; |
| 1902 | } else { |
| 1903 | mElevationAdjustedForStack = false; |
| 1904 | } |
| 1905 | |
| 1906 | // Don't change the elevation if we didn't previously adjust it for the stack it was in |
| 1907 | // or it didn't change. |
| 1908 | if ((wasAdjustedForStack || mElevationAdjustedForStack) |
| 1909 | && getElevation() != elevation) { |
| 1910 | mWindow.setElevation(elevation); |
Wale Ogunwale | bf9eefc | 2015-11-17 14:47:52 -0800 | [diff] [blame] | 1911 | } |
| 1912 | } |
| 1913 | |
Wale Ogunwale | 8cc5a74 | 2015-11-17 15:41:05 -0800 | [diff] [blame] | 1914 | boolean isShowingCaption() { |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 1915 | return mDecorCaptionView != null && mDecorCaptionView.isCaptionShowing(); |
Wale Ogunwale | 8cc5a74 | 2015-11-17 15:41:05 -0800 | [diff] [blame] | 1916 | } |
| 1917 | |
| 1918 | int getCaptionHeight() { |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 1919 | return isShowingCaption() ? mDecorCaptionView.getCaptionHeight() : 0; |
Wale Ogunwale | 8cc5a74 | 2015-11-17 15:41:05 -0800 | [diff] [blame] | 1920 | } |
| 1921 | |
Jorim Jaggi | 04c2fbd | 2015-12-02 15:11:45 -0800 | [diff] [blame] | 1922 | int getStatusBarHeight() { |
| 1923 | return mStatusColorViewState.view != null ? mStatusColorViewState.view.getHeight() : 0; |
| 1924 | } |
| 1925 | |
Wale Ogunwale | 2b547c3 | 2015-11-18 10:33:22 -0800 | [diff] [blame] | 1926 | /** |
| 1927 | * Converts a DIP measure into physical pixels. |
| 1928 | * @param dip The dip value. |
| 1929 | * @return Returns the number of pixels. |
| 1930 | */ |
| 1931 | private float dipToPx(float dip) { |
| 1932 | return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, |
| 1933 | getResources().getDisplayMetrics()); |
| 1934 | } |
| 1935 | |
Filip Gruszczynski | 3dec081 | 2015-12-09 08:42:41 -0800 | [diff] [blame] | 1936 | /** |
| 1937 | * Provide an override of the caption background drawable. |
| 1938 | */ |
| 1939 | void setUserCaptionBackgroundDrawable(Drawable drawable) { |
| 1940 | mUserCaptionBackgroundDrawable = drawable; |
| 1941 | if (mBackdropFrameRenderer != null) { |
| 1942 | mBackdropFrameRenderer.setUserCaptionBackgroundDrawable(drawable); |
| 1943 | } |
| 1944 | } |
| 1945 | |
Filip Gruszczynski | 34dab0b | 2015-12-22 08:29:07 -0800 | [diff] [blame] | 1946 | void updateLogTag(WindowManager.LayoutParams params) { |
| 1947 | final String[] split = params.getTitle().toString().split("\\."); |
| 1948 | if (split.length > 0) { |
| 1949 | mLogTag = TAG + "[" + split[split.length - 1] + "]"; |
| 1950 | } |
| 1951 | } |
| 1952 | |
| 1953 | private void updateAvailableWidth() { |
| 1954 | Resources res = getResources(); |
| 1955 | mAvailableWidth = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, |
| 1956 | res.getConfiguration().screenWidthDp, res.getDisplayMetrics()); |
| 1957 | } |
| 1958 | |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1959 | private static class ColorViewState { |
| 1960 | View view = null; |
| 1961 | int targetVisibility = View.INVISIBLE; |
| 1962 | boolean present = false; |
Jorim Jaggi | 04c2fbd | 2015-12-02 15:11:45 -0800 | [diff] [blame] | 1963 | boolean visible; |
| 1964 | int color; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1965 | |
| 1966 | final int id; |
| 1967 | final int systemUiHideFlag; |
| 1968 | final int translucentFlag; |
| 1969 | final int verticalGravity; |
| 1970 | final int horizontalGravity; |
| 1971 | final String transitionName; |
| 1972 | final int hideWindowFlag; |
| 1973 | |
| 1974 | ColorViewState(int systemUiHideFlag, |
| 1975 | int translucentFlag, int verticalGravity, int horizontalGravity, |
| 1976 | String transitionName, int id, int hideWindowFlag) { |
| 1977 | this.id = id; |
| 1978 | this.systemUiHideFlag = systemUiHideFlag; |
| 1979 | this.translucentFlag = translucentFlag; |
| 1980 | this.verticalGravity = verticalGravity; |
| 1981 | this.horizontalGravity = horizontalGravity; |
| 1982 | this.transitionName = transitionName; |
| 1983 | this.hideWindowFlag = hideWindowFlag; |
| 1984 | } |
| 1985 | } |
| 1986 | |
| 1987 | /** |
| 1988 | * Clears out internal references when the action mode is destroyed. |
| 1989 | */ |
| 1990 | private class ActionModeCallback2Wrapper extends ActionMode.Callback2 { |
| 1991 | private final ActionMode.Callback mWrapped; |
| 1992 | |
| 1993 | public ActionModeCallback2Wrapper(ActionMode.Callback wrapped) { |
| 1994 | mWrapped = wrapped; |
| 1995 | } |
| 1996 | |
| 1997 | public boolean onCreateActionMode(ActionMode mode, Menu menu) { |
| 1998 | return mWrapped.onCreateActionMode(mode, menu); |
| 1999 | } |
| 2000 | |
| 2001 | public boolean onPrepareActionMode(ActionMode mode, Menu menu) { |
| 2002 | requestFitSystemWindows(); |
| 2003 | return mWrapped.onPrepareActionMode(mode, menu); |
| 2004 | } |
| 2005 | |
| 2006 | public boolean onActionItemClicked(ActionMode mode, MenuItem item) { |
| 2007 | return mWrapped.onActionItemClicked(mode, item); |
| 2008 | } |
| 2009 | |
| 2010 | public void onDestroyActionMode(ActionMode mode) { |
| 2011 | mWrapped.onDestroyActionMode(mode); |
| 2012 | final boolean isMncApp = mContext.getApplicationInfo().targetSdkVersion |
| 2013 | >= Build.VERSION_CODES.M; |
| 2014 | final boolean isPrimary; |
| 2015 | final boolean isFloating; |
| 2016 | if (isMncApp) { |
| 2017 | isPrimary = mode == mPrimaryActionMode; |
| 2018 | isFloating = mode == mFloatingActionMode; |
| 2019 | if (!isPrimary && mode.getType() == ActionMode.TYPE_PRIMARY) { |
Filip Gruszczynski | 34dab0b | 2015-12-22 08:29:07 -0800 | [diff] [blame] | 2020 | Log.e(mLogTag, "Destroying unexpected ActionMode instance of TYPE_PRIMARY; " |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 2021 | + mode + " was not the current primary action mode! Expected " |
| 2022 | + mPrimaryActionMode); |
| 2023 | } |
| 2024 | if (!isFloating && mode.getType() == ActionMode.TYPE_FLOATING) { |
Filip Gruszczynski | 34dab0b | 2015-12-22 08:29:07 -0800 | [diff] [blame] | 2025 | Log.e(mLogTag, "Destroying unexpected ActionMode instance of TYPE_FLOATING; " |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 2026 | + mode + " was not the current floating action mode! Expected " |
| 2027 | + mFloatingActionMode); |
| 2028 | } |
| 2029 | } else { |
| 2030 | isPrimary = mode.getType() == ActionMode.TYPE_PRIMARY; |
| 2031 | isFloating = mode.getType() == ActionMode.TYPE_FLOATING; |
| 2032 | } |
| 2033 | if (isPrimary) { |
| 2034 | if (mPrimaryActionModePopup != null) { |
| 2035 | removeCallbacks(mShowPrimaryActionModePopup); |
| 2036 | } |
| 2037 | if (mPrimaryActionModeView != null) { |
| 2038 | endOnGoingFadeAnimation(); |
| 2039 | mFadeAnim = ObjectAnimator.ofFloat(mPrimaryActionModeView, View.ALPHA, |
| 2040 | 1f, 0f); |
| 2041 | mFadeAnim.addListener(new Animator.AnimatorListener() { |
| 2042 | @Override |
| 2043 | public void onAnimationStart(Animator animation) { |
| 2044 | |
| 2045 | } |
| 2046 | |
| 2047 | @Override |
| 2048 | public void onAnimationEnd(Animator animation) { |
| 2049 | mPrimaryActionModeView.setVisibility(GONE); |
| 2050 | if (mPrimaryActionModePopup != null) { |
| 2051 | mPrimaryActionModePopup.dismiss(); |
| 2052 | } |
| 2053 | mPrimaryActionModeView.removeAllViews(); |
| 2054 | mFadeAnim = null; |
| 2055 | } |
| 2056 | |
| 2057 | @Override |
| 2058 | public void onAnimationCancel(Animator animation) { |
| 2059 | |
| 2060 | } |
| 2061 | |
| 2062 | @Override |
| 2063 | public void onAnimationRepeat(Animator animation) { |
| 2064 | |
| 2065 | } |
| 2066 | }); |
| 2067 | mFadeAnim.start(); |
| 2068 | } |
| 2069 | |
| 2070 | mPrimaryActionMode = null; |
| 2071 | } else if (isFloating) { |
| 2072 | cleanupFloatingActionModeViews(); |
| 2073 | mFloatingActionMode = null; |
| 2074 | } |
| 2075 | if (mWindow.getCallback() != null && !mWindow.isDestroyed()) { |
| 2076 | try { |
| 2077 | mWindow.getCallback().onActionModeFinished(mode); |
| 2078 | } catch (AbstractMethodError ame) { |
| 2079 | // Older apps might not implement this callback method. |
| 2080 | } |
| 2081 | } |
| 2082 | requestFitSystemWindows(); |
| 2083 | } |
| 2084 | |
| 2085 | @Override |
| 2086 | public void onGetContentRect(ActionMode mode, View view, Rect outRect) { |
| 2087 | if (mWrapped instanceof ActionMode.Callback2) { |
| 2088 | ((ActionMode.Callback2) mWrapped).onGetContentRect(mode, view, outRect); |
| 2089 | } else { |
| 2090 | super.onGetContentRect(mode, view, outRect); |
| 2091 | } |
| 2092 | } |
| 2093 | } |
| 2094 | } |