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 | |
Robert Carr | 32bcb10 | 2018-01-29 15:03:23 -0800 | [diff] [blame] | 19 | import static android.app.WindowConfiguration.PINNED_WINDOWING_MODE_ELEVATION_IN_DIP; |
Wale Ogunwale | 3382ab1 | 2017-07-27 08:55:03 -0700 | [diff] [blame] | 20 | import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM; |
| 21 | import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED; |
Filip Gruszczynski | 34dab0b | 2015-12-22 08:29:07 -0800 | [diff] [blame] | 22 | import static android.content.res.Configuration.ORIENTATION_PORTRAIT; |
Jorim Jaggi | 8f5701b | 2016-04-04 18:36:02 -0700 | [diff] [blame] | 23 | import static android.os.Build.VERSION_CODES.M; |
| 24 | import static android.os.Build.VERSION_CODES.N; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 25 | import static android.view.View.MeasureSpec.AT_MOST; |
| 26 | import static android.view.View.MeasureSpec.EXACTLY; |
| 27 | import static android.view.View.MeasureSpec.getMode; |
| 28 | import static android.view.ViewGroup.LayoutParams.MATCH_PARENT; |
| 29 | import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT; |
Filip Gruszczynski | 3dec081 | 2015-12-09 08:42:41 -0800 | [diff] [blame] | 30 | import static android.view.Window.DECOR_CAPTION_SHADE_DARK; |
| 31 | import static android.view.Window.DECOR_CAPTION_SHADE_LIGHT; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 32 | import static android.view.WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS; |
| 33 | import static android.view.WindowManager.LayoutParams.FLAG_FULLSCREEN; |
Jorim Jaggi | 9f6798a | 2016-02-10 22:16:06 -0800 | [diff] [blame] | 34 | import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR; |
Jorim Jaggi | 65bff3e | 2016-02-08 19:17:07 -0800 | [diff] [blame] | 35 | import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 36 | import static android.view.WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION; |
| 37 | import static android.view.WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS; |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 38 | import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION; |
| 39 | import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION; |
Chong Zhang | fea963e | 2016-08-15 17:14:16 -0700 | [diff] [blame] | 40 | import static android.view.WindowManager.LayoutParams.TYPE_DRAWN_APPLICATION; |
John Reck | 32f140aa6 | 2018-10-04 15:08:24 -0700 | [diff] [blame] | 41 | |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 42 | import static com.android.internal.policy.PhoneWindow.FEATURE_OPTIONS_PANEL; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 43 | |
John Reck | 32f140aa6 | 2018-10-04 15:08:24 -0700 | [diff] [blame] | 44 | import android.animation.Animator; |
| 45 | import android.animation.AnimatorListenerAdapter; |
| 46 | import android.animation.ObjectAnimator; |
| 47 | import android.annotation.Nullable; |
| 48 | import android.annotation.TestApi; |
| 49 | import android.app.WindowConfiguration; |
| 50 | import android.content.Context; |
| 51 | import android.content.res.Configuration; |
| 52 | import android.content.res.Resources; |
| 53 | import android.graphics.Canvas; |
| 54 | import android.graphics.Color; |
| 55 | import android.graphics.LinearGradient; |
| 56 | import android.graphics.Outline; |
| 57 | import android.graphics.Paint; |
| 58 | import android.graphics.PixelFormat; |
| 59 | import android.graphics.RecordingCanvas; |
| 60 | import android.graphics.Rect; |
| 61 | import android.graphics.Region; |
| 62 | import android.graphics.Shader; |
| 63 | import android.graphics.drawable.ColorDrawable; |
| 64 | import android.graphics.drawable.Drawable; |
| 65 | import android.graphics.drawable.InsetDrawable; |
| 66 | import android.graphics.drawable.LayerDrawable; |
| 67 | import android.util.DisplayMetrics; |
| 68 | import android.util.Log; |
| 69 | import android.util.Pair; |
| 70 | import android.util.TypedValue; |
| 71 | import android.view.ActionMode; |
| 72 | import android.view.ContextThemeWrapper; |
| 73 | import android.view.Gravity; |
| 74 | import android.view.InputQueue; |
| 75 | import android.view.KeyEvent; |
| 76 | import android.view.KeyboardShortcutGroup; |
| 77 | import android.view.LayoutInflater; |
| 78 | import android.view.Menu; |
| 79 | import android.view.MenuItem; |
| 80 | import android.view.MotionEvent; |
| 81 | import android.view.ThreadedRenderer; |
| 82 | import android.view.View; |
| 83 | import android.view.ViewGroup; |
| 84 | import android.view.ViewOutlineProvider; |
| 85 | import android.view.ViewStub; |
| 86 | import android.view.ViewTreeObserver; |
| 87 | import android.view.Window; |
| 88 | import android.view.WindowCallbacks; |
| 89 | import android.view.WindowInsets; |
| 90 | import android.view.WindowManager; |
| 91 | import android.view.accessibility.AccessibilityEvent; |
| 92 | import android.view.accessibility.AccessibilityManager; |
| 93 | import android.view.accessibility.AccessibilityNodeInfo; |
| 94 | import android.view.animation.AnimationUtils; |
| 95 | import android.view.animation.Interpolator; |
| 96 | import android.widget.FrameLayout; |
| 97 | import android.widget.PopupWindow; |
| 98 | |
| 99 | import com.android.internal.R; |
| 100 | import com.android.internal.policy.PhoneWindow.PanelFeatureState; |
| 101 | import com.android.internal.policy.PhoneWindow.PhoneWindowMenuCallback; |
| 102 | import com.android.internal.view.FloatingActionMode; |
| 103 | import com.android.internal.view.RootViewSurfaceTaker; |
| 104 | import com.android.internal.view.StandaloneActionMode; |
| 105 | import com.android.internal.view.menu.ContextMenuBuilder; |
| 106 | import com.android.internal.view.menu.MenuHelper; |
| 107 | import com.android.internal.widget.ActionBarContextView; |
| 108 | import com.android.internal.widget.BackgroundFallback; |
| 109 | import com.android.internal.widget.DecorCaptionView; |
| 110 | import com.android.internal.widget.FloatingToolbar; |
| 111 | |
| 112 | import java.util.List; |
| 113 | |
Wale Ogunwale | bf9eefc | 2015-11-17 14:47:52 -0800 | [diff] [blame] | 114 | /** @hide */ |
| 115 | public class DecorView extends FrameLayout implements RootViewSurfaceTaker, WindowCallbacks { |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 116 | private static final String TAG = "DecorView"; |
| 117 | |
Filip Gruszczynski | 1937a4c | 2016-01-19 16:17:13 -0800 | [diff] [blame] | 118 | private static final boolean DEBUG_MEASURE = false; |
| 119 | |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 120 | private static final boolean SWEEP_OPEN_MENU = false; |
| 121 | |
Wale Ogunwale | 2b547c3 | 2015-11-18 10:33:22 -0800 | [diff] [blame] | 122 | // The height of a window which has focus in DIP. |
| 123 | private final static int DECOR_SHADOW_FOCUSED_HEIGHT_IN_DIP = 20; |
| 124 | // The height of a window which has not in DIP. |
| 125 | private final static int DECOR_SHADOW_UNFOCUSED_HEIGHT_IN_DIP = 5; |
| 126 | |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 127 | public static final ColorViewAttributes STATUS_BAR_COLOR_VIEW_ATTRIBUTES = |
| 128 | new ColorViewAttributes(SYSTEM_UI_FLAG_FULLSCREEN, FLAG_TRANSLUCENT_STATUS, |
| 129 | Gravity.TOP, Gravity.LEFT, Gravity.RIGHT, |
| 130 | Window.STATUS_BAR_BACKGROUND_TRANSITION_NAME, |
| 131 | com.android.internal.R.id.statusBarBackground, |
| 132 | FLAG_FULLSCREEN); |
| 133 | |
| 134 | public static final ColorViewAttributes NAVIGATION_BAR_COLOR_VIEW_ATTRIBUTES = |
| 135 | new ColorViewAttributes( |
| 136 | SYSTEM_UI_FLAG_HIDE_NAVIGATION, FLAG_TRANSLUCENT_NAVIGATION, |
| 137 | Gravity.BOTTOM, Gravity.RIGHT, Gravity.LEFT, |
| 138 | Window.NAVIGATION_BAR_BACKGROUND_TRANSITION_NAME, |
| 139 | com.android.internal.R.id.navigationBarBackground, |
| 140 | 0 /* hideWindowFlag */); |
| 141 | |
Winson Chung | 4d8681f | 2017-05-23 16:22:08 -0700 | [diff] [blame] | 142 | // This is used to workaround an issue where the PiP shadow can be transparent if the window |
| 143 | // background is transparent |
| 144 | private static final ViewOutlineProvider PIP_OUTLINE_PROVIDER = new ViewOutlineProvider() { |
| 145 | @Override |
| 146 | public void getOutline(View view, Outline outline) { |
| 147 | outline.setRect(0, 0, view.getWidth(), view.getHeight()); |
| 148 | outline.setAlpha(1f); |
| 149 | } |
| 150 | }; |
| 151 | |
Wale Ogunwale | 2b547c3 | 2015-11-18 10:33:22 -0800 | [diff] [blame] | 152 | // Cludge to address b/22668382: Set the shadow size to the maximum so that the layer |
| 153 | // size calculation takes the shadow size into account. We set the elevation currently |
| 154 | // to max until the first layout command has been executed. |
| 155 | private boolean mAllowUpdateElevation = false; |
| 156 | |
| 157 | private boolean mElevationAdjustedForStack = false; |
| 158 | |
Winson Chung | 4d8681f | 2017-05-23 16:22:08 -0700 | [diff] [blame] | 159 | // Keeps track of the picture-in-picture mode for the view shadow |
| 160 | private boolean mIsInPictureInPictureMode; |
| 161 | |
| 162 | // Stores the previous outline provider prior to applying PIP_OUTLINE_PROVIDER |
| 163 | private ViewOutlineProvider mLastOutlineProvider; |
| 164 | |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 165 | int mDefaultOpacity = PixelFormat.OPAQUE; |
| 166 | |
| 167 | /** The feature ID of the panel, or -1 if this is the application's DecorView */ |
| 168 | private final int mFeatureId; |
| 169 | |
| 170 | private final Rect mDrawingBounds = new Rect(); |
| 171 | |
| 172 | private final Rect mBackgroundPadding = new Rect(); |
| 173 | |
| 174 | private final Rect mFramePadding = new Rect(); |
| 175 | |
| 176 | private final Rect mFrameOffsets = new Rect(); |
| 177 | |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 178 | private boolean mHasCaption = false; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 179 | |
| 180 | private boolean mChanging; |
| 181 | |
| 182 | private Drawable mMenuBackground; |
| 183 | private boolean mWatchingForMenu; |
| 184 | private int mDownY; |
| 185 | |
| 186 | ActionMode mPrimaryActionMode; |
| 187 | private ActionMode mFloatingActionMode; |
| 188 | private ActionBarContextView mPrimaryActionModeView; |
| 189 | private PopupWindow mPrimaryActionModePopup; |
| 190 | private Runnable mShowPrimaryActionModePopup; |
| 191 | private ViewTreeObserver.OnPreDrawListener mFloatingToolbarPreDrawListener; |
| 192 | private View mFloatingActionModeOriginatingView; |
| 193 | private FloatingToolbar mFloatingToolbar; |
| 194 | private ObjectAnimator mFadeAnim; |
| 195 | |
| 196 | // View added at runtime to draw under the status bar area |
| 197 | private View mStatusGuard; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 198 | |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 199 | private final ColorViewState mStatusColorViewState = |
| 200 | new ColorViewState(STATUS_BAR_COLOR_VIEW_ATTRIBUTES); |
| 201 | private final ColorViewState mNavigationColorViewState = |
| 202 | new ColorViewState(NAVIGATION_BAR_COLOR_VIEW_ATTRIBUTES); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 203 | |
| 204 | private final Interpolator mShowInterpolator; |
| 205 | private final Interpolator mHideInterpolator; |
| 206 | private final int mBarEnterExitDuration; |
Jorim Jaggi | 8f5701b | 2016-04-04 18:36:02 -0700 | [diff] [blame] | 207 | final boolean mForceWindowDrawsStatusBarBackground; |
Jorim Jaggi | 4fa7892 | 2015-11-30 17:13:56 -0800 | [diff] [blame] | 208 | private final int mSemiTransparentStatusBarColor; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 209 | |
| 210 | private final BackgroundFallback mBackgroundFallback = new BackgroundFallback(); |
| 211 | |
| 212 | private int mLastTopInset = 0; |
| 213 | private int mLastBottomInset = 0; |
| 214 | private int mLastRightInset = 0; |
Adrian Roos | 85d202b | 2016-06-02 16:27:47 -0700 | [diff] [blame] | 215 | private int mLastLeftInset = 0; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 216 | private boolean mLastHasTopStableInset = false; |
| 217 | private boolean mLastHasBottomStableInset = false; |
| 218 | private boolean mLastHasRightStableInset = false; |
Adrian Roos | 85d202b | 2016-06-02 16:27:47 -0700 | [diff] [blame] | 219 | private boolean mLastHasLeftStableInset = false; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 220 | private int mLastWindowFlags = 0; |
Jorim Jaggi | e5638a6 | 2016-03-25 22:57:01 -0700 | [diff] [blame] | 221 | private boolean mLastShouldAlwaysConsumeNavBar = false; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 222 | |
| 223 | private int mRootScrollY = 0; |
| 224 | |
| 225 | private PhoneWindow mWindow; |
| 226 | |
| 227 | ViewGroup mContentRoot; |
| 228 | |
| 229 | private Rect mTempRect; |
| 230 | private Rect mOutsets = new Rect(); |
| 231 | |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 232 | // 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] | 233 | // buttons. The visibility of this decor depends on the workspace and the window type. |
| 234 | // If the window type does not require such a view, this member might be null. |
Garfield Tan | 3b9613c | 2018-12-26 17:08:51 -0800 | [diff] [blame] | 235 | private DecorCaptionView mDecorCaptionView; |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 236 | |
Wale Ogunwale | bf9eefc | 2015-11-17 14:47:52 -0800 | [diff] [blame] | 237 | private boolean mWindowResizeCallbacksAdded = false; |
Chong Zhang | d3fd96c | 2016-02-08 18:25:24 -0800 | [diff] [blame] | 238 | private Drawable.Callback mLastBackgroundDrawableCb = null; |
Filip Gruszczynski | 3dec081 | 2015-12-09 08:42:41 -0800 | [diff] [blame] | 239 | private BackdropFrameRenderer mBackdropFrameRenderer = null; |
Wale Ogunwale | bf9eefc | 2015-11-17 14:47:52 -0800 | [diff] [blame] | 240 | private Drawable mResizingBackgroundDrawable; |
| 241 | private Drawable mCaptionBackgroundDrawable; |
Filip Gruszczynski | 3dec081 | 2015-12-09 08:42:41 -0800 | [diff] [blame] | 242 | private Drawable mUserCaptionBackgroundDrawable; |
Wale Ogunwale | bf9eefc | 2015-11-17 14:47:52 -0800 | [diff] [blame] | 243 | |
Filip Gruszczynski | 34dab0b | 2015-12-22 08:29:07 -0800 | [diff] [blame] | 244 | private float mAvailableWidth; |
| 245 | |
| 246 | String mLogTag = TAG; |
Filip Gruszczynski | adf7b5e | 2016-01-27 16:23:05 -0800 | [diff] [blame] | 247 | private final Rect mFloatingInsets = new Rect(); |
| 248 | private boolean mApplyFloatingVerticalInsets = false; |
| 249 | private boolean mApplyFloatingHorizontalInsets = false; |
Filip Gruszczynski | 34dab0b | 2015-12-22 08:29:07 -0800 | [diff] [blame] | 250 | |
Jorim Jaggi | c39c7b0 | 2016-03-24 10:47:07 -0700 | [diff] [blame] | 251 | private int mResizeMode = RESIZE_MODE_INVALID; |
| 252 | private final int mResizeShadowSize; |
| 253 | private final Paint mVerticalResizeShadowPaint = new Paint(); |
| 254 | private final Paint mHorizontalResizeShadowPaint = new Paint(); |
| 255 | |
Filip Gruszczynski | 34dab0b | 2015-12-22 08:29:07 -0800 | [diff] [blame] | 256 | DecorView(Context context, int featureId, PhoneWindow window, |
| 257 | WindowManager.LayoutParams params) { |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 258 | super(context); |
| 259 | mFeatureId = featureId; |
| 260 | |
| 261 | mShowInterpolator = AnimationUtils.loadInterpolator(context, |
| 262 | android.R.interpolator.linear_out_slow_in); |
| 263 | mHideInterpolator = AnimationUtils.loadInterpolator(context, |
| 264 | android.R.interpolator.fast_out_linear_in); |
| 265 | |
| 266 | mBarEnterExitDuration = context.getResources().getInteger( |
| 267 | R.integer.dock_enter_exit_duration); |
Jorim Jaggi | 4fa7892 | 2015-11-30 17:13:56 -0800 | [diff] [blame] | 268 | mForceWindowDrawsStatusBarBackground = context.getResources().getBoolean( |
Jorim Jaggi | 8f5701b | 2016-04-04 18:36:02 -0700 | [diff] [blame] | 269 | R.bool.config_forceWindowDrawsStatusBarBackground) |
| 270 | && context.getApplicationInfo().targetSdkVersion >= N; |
Jorim Jaggi | 4fa7892 | 2015-11-30 17:13:56 -0800 | [diff] [blame] | 271 | mSemiTransparentStatusBarColor = context.getResources().getColor( |
| 272 | R.color.system_bar_background_semi_transparent, null /* theme */); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 273 | |
Filip Gruszczynski | 34dab0b | 2015-12-22 08:29:07 -0800 | [diff] [blame] | 274 | updateAvailableWidth(); |
| 275 | |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 276 | setWindow(window); |
Filip Gruszczynski | 34dab0b | 2015-12-22 08:29:07 -0800 | [diff] [blame] | 277 | |
| 278 | updateLogTag(params); |
Jorim Jaggi | c39c7b0 | 2016-03-24 10:47:07 -0700 | [diff] [blame] | 279 | |
| 280 | mResizeShadowSize = context.getResources().getDimensionPixelSize( |
| 281 | R.dimen.resize_shadow_size); |
| 282 | initResizingPaints(); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 283 | } |
| 284 | |
Nader Jawad | 56c68bc | 2018-04-24 17:08:51 -0700 | [diff] [blame] | 285 | void setBackgroundFallback(@Nullable Drawable fallbackDrawable) { |
| 286 | mBackgroundFallback.setDrawable(fallbackDrawable); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 287 | setWillNotDraw(getBackground() == null && !mBackgroundFallback.hasFallback()); |
| 288 | } |
| 289 | |
Nader Jawad | 56c68bc | 2018-04-24 17:08:51 -0700 | [diff] [blame] | 290 | @TestApi |
| 291 | public @Nullable Drawable getBackgroundFallback() { |
| 292 | return mBackgroundFallback.getDrawable(); |
| 293 | } |
| 294 | |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 295 | @Override |
Chris Craik | 867b812 | 2016-05-05 16:19:22 -0700 | [diff] [blame] | 296 | public boolean gatherTransparentRegion(Region region) { |
| 297 | boolean statusOpaque = gatherTransparentRegion(mStatusColorViewState, region); |
| 298 | boolean navOpaque = gatherTransparentRegion(mNavigationColorViewState, region); |
| 299 | boolean decorOpaque = super.gatherTransparentRegion(region); |
| 300 | |
| 301 | // combine bools after computation, so each method above always executes |
| 302 | return statusOpaque || navOpaque || decorOpaque; |
| 303 | } |
| 304 | |
| 305 | boolean gatherTransparentRegion(ColorViewState colorViewState, Region region) { |
| 306 | if (colorViewState.view != null && colorViewState.visible && isResizing()) { |
| 307 | // If a visible ColorViewState is in a resizing host DecorView, forcibly register its |
| 308 | // opaque area, since it's drawn by a different root RenderNode. It would otherwise be |
| 309 | // rejected by ViewGroup#gatherTransparentRegion() for the view not being VISIBLE. |
| 310 | return colorViewState.view.gatherTransparentRegion(region); |
| 311 | } |
| 312 | return false; // no opaque area added |
| 313 | } |
| 314 | |
| 315 | @Override |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 316 | public void onDraw(Canvas c) { |
| 317 | super.onDraw(c); |
Jorim Jaggi | 853d17d | 2017-05-19 14:53:55 +0200 | [diff] [blame] | 318 | |
Adrian Roos | 786ea78 | 2018-04-30 16:05:00 +0200 | [diff] [blame] | 319 | mBackgroundFallback.draw(this, mContentRoot, c, mWindow.mContentParent, |
| 320 | mStatusColorViewState.view, mNavigationColorViewState.view); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | @Override |
| 324 | public boolean dispatchKeyEvent(KeyEvent event) { |
| 325 | final int keyCode = event.getKeyCode(); |
| 326 | final int action = event.getAction(); |
| 327 | final boolean isDown = action == KeyEvent.ACTION_DOWN; |
| 328 | |
| 329 | if (isDown && (event.getRepeatCount() == 0)) { |
| 330 | // First handle chording of panel key: if a panel key is held |
| 331 | // but not released, try to execute a shortcut in it. |
| 332 | if ((mWindow.mPanelChordingKey > 0) && (mWindow.mPanelChordingKey != keyCode)) { |
| 333 | boolean handled = dispatchKeyShortcutEvent(event); |
| 334 | if (handled) { |
| 335 | return true; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | // If a panel is open, perform a shortcut on it without the |
| 340 | // chorded panel key |
| 341 | if ((mWindow.mPreparedPanel != null) && mWindow.mPreparedPanel.isOpen) { |
| 342 | if (mWindow.performPanelShortcut(mWindow.mPreparedPanel, keyCode, event, 0)) { |
| 343 | return true; |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | if (!mWindow.isDestroyed()) { |
| 349 | final Window.Callback cb = mWindow.getCallback(); |
| 350 | final boolean handled = cb != null && mFeatureId < 0 ? cb.dispatchKeyEvent(event) |
| 351 | : super.dispatchKeyEvent(event); |
| 352 | if (handled) { |
| 353 | return true; |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | return isDown ? mWindow.onKeyDown(mFeatureId, event.getKeyCode(), event) |
| 358 | : mWindow.onKeyUp(mFeatureId, event.getKeyCode(), event); |
| 359 | } |
| 360 | |
| 361 | @Override |
| 362 | public boolean dispatchKeyShortcutEvent(KeyEvent ev) { |
| 363 | // If the panel is already prepared, then perform the shortcut using it. |
| 364 | boolean handled; |
| 365 | if (mWindow.mPreparedPanel != null) { |
| 366 | handled = mWindow.performPanelShortcut(mWindow.mPreparedPanel, ev.getKeyCode(), ev, |
| 367 | Menu.FLAG_PERFORM_NO_CLOSE); |
| 368 | if (handled) { |
| 369 | if (mWindow.mPreparedPanel != null) { |
| 370 | mWindow.mPreparedPanel.isHandled = true; |
| 371 | } |
| 372 | return true; |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | // Shortcut not handled by the panel. Dispatch to the view hierarchy. |
| 377 | final Window.Callback cb = mWindow.getCallback(); |
| 378 | handled = cb != null && !mWindow.isDestroyed() && mFeatureId < 0 |
| 379 | ? cb.dispatchKeyShortcutEvent(ev) : super.dispatchKeyShortcutEvent(ev); |
| 380 | if (handled) { |
| 381 | return true; |
| 382 | } |
| 383 | |
| 384 | // If the panel is not prepared, then we may be trying to handle a shortcut key |
| 385 | // combination such as Control+C. Temporarily prepare the panel then mark it |
| 386 | // unprepared again when finished to ensure that the panel will again be prepared |
| 387 | // the next time it is shown for real. |
| 388 | PhoneWindow.PanelFeatureState st = |
| 389 | mWindow.getPanelState(Window.FEATURE_OPTIONS_PANEL, false); |
| 390 | if (st != null && mWindow.mPreparedPanel == null) { |
| 391 | mWindow.preparePanel(st, ev); |
| 392 | handled = mWindow.performPanelShortcut(st, ev.getKeyCode(), ev, |
| 393 | Menu.FLAG_PERFORM_NO_CLOSE); |
| 394 | st.isPrepared = false; |
| 395 | if (handled) { |
| 396 | return true; |
| 397 | } |
| 398 | } |
| 399 | return false; |
| 400 | } |
| 401 | |
| 402 | @Override |
| 403 | public boolean dispatchTouchEvent(MotionEvent ev) { |
| 404 | final Window.Callback cb = mWindow.getCallback(); |
| 405 | return cb != null && !mWindow.isDestroyed() && mFeatureId < 0 |
| 406 | ? cb.dispatchTouchEvent(ev) : super.dispatchTouchEvent(ev); |
| 407 | } |
| 408 | |
| 409 | @Override |
| 410 | public boolean dispatchTrackballEvent(MotionEvent ev) { |
| 411 | final Window.Callback cb = mWindow.getCallback(); |
| 412 | return cb != null && !mWindow.isDestroyed() && mFeatureId < 0 |
| 413 | ? cb.dispatchTrackballEvent(ev) : super.dispatchTrackballEvent(ev); |
| 414 | } |
| 415 | |
| 416 | @Override |
| 417 | public boolean dispatchGenericMotionEvent(MotionEvent ev) { |
| 418 | final Window.Callback cb = mWindow.getCallback(); |
| 419 | return cb != null && !mWindow.isDestroyed() && mFeatureId < 0 |
| 420 | ? cb.dispatchGenericMotionEvent(ev) : super.dispatchGenericMotionEvent(ev); |
| 421 | } |
| 422 | |
| 423 | public boolean superDispatchKeyEvent(KeyEvent event) { |
| 424 | // Give priority to closing action modes if applicable. |
| 425 | if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) { |
| 426 | final int action = event.getAction(); |
| 427 | // Back cancels action modes first. |
| 428 | if (mPrimaryActionMode != null) { |
| 429 | if (action == KeyEvent.ACTION_UP) { |
| 430 | mPrimaryActionMode.finish(); |
| 431 | } |
| 432 | return true; |
| 433 | } |
| 434 | } |
| 435 | |
Evan Rosky | 5e29c07 | 2017-06-02 17:31:22 -0700 | [diff] [blame] | 436 | if (super.dispatchKeyEvent(event)) { |
| 437 | return true; |
| 438 | } |
| 439 | |
Evan Rosky | cd80e61 | 2018-05-17 17:46:09 -0700 | [diff] [blame] | 440 | return (getViewRootImpl() != null) && getViewRootImpl().dispatchUnhandledKeyEvent(event); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | public boolean superDispatchKeyShortcutEvent(KeyEvent event) { |
| 444 | return super.dispatchKeyShortcutEvent(event); |
| 445 | } |
| 446 | |
| 447 | public boolean superDispatchTouchEvent(MotionEvent event) { |
| 448 | return super.dispatchTouchEvent(event); |
| 449 | } |
| 450 | |
| 451 | public boolean superDispatchTrackballEvent(MotionEvent event) { |
| 452 | return super.dispatchTrackballEvent(event); |
| 453 | } |
| 454 | |
| 455 | public boolean superDispatchGenericMotionEvent(MotionEvent event) { |
| 456 | return super.dispatchGenericMotionEvent(event); |
| 457 | } |
| 458 | |
| 459 | @Override |
| 460 | public boolean onTouchEvent(MotionEvent event) { |
| 461 | return onInterceptTouchEvent(event); |
| 462 | } |
| 463 | |
| 464 | private boolean isOutOfInnerBounds(int x, int y) { |
| 465 | return x < 0 || y < 0 || x > getWidth() || y > getHeight(); |
| 466 | } |
| 467 | |
| 468 | private boolean isOutOfBounds(int x, int y) { |
| 469 | return x < -5 || y < -5 || x > (getWidth() + 5) |
| 470 | || y > (getHeight() + 5); |
| 471 | } |
| 472 | |
| 473 | @Override |
| 474 | public boolean onInterceptTouchEvent(MotionEvent event) { |
| 475 | int action = event.getAction(); |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 476 | if (mHasCaption && isShowingCaption()) { |
| 477 | // Don't dispatch ACTION_DOWN to the captionr if the window is resizable and the event |
| 478 | // was (starting) outside the window. Window resizing events should be handled by |
| 479 | // WindowManager. |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 480 | // TODO: Investigate how to handle the outside touch in window manager |
| 481 | // without generating these events. |
| 482 | // Currently we receive these because we need to enlarge the window's |
| 483 | // touch region so that the monitor channel receives the events |
| 484 | // in the outside touch area. |
| 485 | if (action == MotionEvent.ACTION_DOWN) { |
| 486 | final int x = (int) event.getX(); |
| 487 | final int y = (int) event.getY(); |
| 488 | if (isOutOfInnerBounds(x, y)) { |
| 489 | return true; |
| 490 | } |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | if (mFeatureId >= 0) { |
| 495 | if (action == MotionEvent.ACTION_DOWN) { |
| 496 | int x = (int)event.getX(); |
| 497 | int y = (int)event.getY(); |
| 498 | if (isOutOfBounds(x, y)) { |
| 499 | mWindow.closePanel(mFeatureId); |
| 500 | return true; |
| 501 | } |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | if (!SWEEP_OPEN_MENU) { |
| 506 | return false; |
| 507 | } |
| 508 | |
| 509 | if (mFeatureId >= 0) { |
| 510 | if (action == MotionEvent.ACTION_DOWN) { |
Filip Gruszczynski | 34dab0b | 2015-12-22 08:29:07 -0800 | [diff] [blame] | 511 | Log.i(mLogTag, "Watchiing!"); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 512 | mWatchingForMenu = true; |
| 513 | mDownY = (int) event.getY(); |
| 514 | return false; |
| 515 | } |
| 516 | |
| 517 | if (!mWatchingForMenu) { |
| 518 | return false; |
| 519 | } |
| 520 | |
| 521 | int y = (int)event.getY(); |
| 522 | if (action == MotionEvent.ACTION_MOVE) { |
| 523 | if (y > (mDownY+30)) { |
Filip Gruszczynski | 34dab0b | 2015-12-22 08:29:07 -0800 | [diff] [blame] | 524 | Log.i(mLogTag, "Closing!"); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 525 | mWindow.closePanel(mFeatureId); |
| 526 | mWatchingForMenu = false; |
| 527 | return true; |
| 528 | } |
| 529 | } else if (action == MotionEvent.ACTION_UP) { |
| 530 | mWatchingForMenu = false; |
| 531 | } |
| 532 | |
| 533 | return false; |
| 534 | } |
| 535 | |
Filip Gruszczynski | 34dab0b | 2015-12-22 08:29:07 -0800 | [diff] [blame] | 536 | //Log.i(mLogTag, "Intercept: action=" + action + " y=" + event.getY() |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 537 | // + " (in " + getHeight() + ")"); |
| 538 | |
| 539 | if (action == MotionEvent.ACTION_DOWN) { |
| 540 | int y = (int)event.getY(); |
| 541 | if (y >= (getHeight()-5) && !mWindow.hasChildren()) { |
Filip Gruszczynski | 34dab0b | 2015-12-22 08:29:07 -0800 | [diff] [blame] | 542 | Log.i(mLogTag, "Watching!"); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 543 | mWatchingForMenu = true; |
| 544 | } |
| 545 | return false; |
| 546 | } |
| 547 | |
| 548 | if (!mWatchingForMenu) { |
| 549 | return false; |
| 550 | } |
| 551 | |
| 552 | int y = (int)event.getY(); |
| 553 | if (action == MotionEvent.ACTION_MOVE) { |
| 554 | if (y < (getHeight()-30)) { |
Filip Gruszczynski | 34dab0b | 2015-12-22 08:29:07 -0800 | [diff] [blame] | 555 | Log.i(mLogTag, "Opening!"); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 556 | mWindow.openPanel(Window.FEATURE_OPTIONS_PANEL, new KeyEvent( |
| 557 | KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MENU)); |
| 558 | mWatchingForMenu = false; |
| 559 | return true; |
| 560 | } |
| 561 | } else if (action == MotionEvent.ACTION_UP) { |
| 562 | mWatchingForMenu = false; |
| 563 | } |
| 564 | |
| 565 | return false; |
| 566 | } |
| 567 | |
| 568 | @Override |
| 569 | public void sendAccessibilityEvent(int eventType) { |
| 570 | if (!AccessibilityManager.getInstance(mContext).isEnabled()) { |
| 571 | return; |
| 572 | } |
| 573 | |
| 574 | // if we are showing a feature that should be announced and one child |
| 575 | // make this child the event source since this is the feature itself |
| 576 | // otherwise the callback will take over and announce its client |
| 577 | if ((mFeatureId == Window.FEATURE_OPTIONS_PANEL || |
| 578 | mFeatureId == Window.FEATURE_CONTEXT_MENU || |
| 579 | mFeatureId == Window.FEATURE_PROGRESS || |
| 580 | mFeatureId == Window.FEATURE_INDETERMINATE_PROGRESS) |
| 581 | && getChildCount() == 1) { |
| 582 | getChildAt(0).sendAccessibilityEvent(eventType); |
| 583 | } else { |
| 584 | super.sendAccessibilityEvent(eventType); |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | @Override |
| 589 | public boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) { |
| 590 | final Window.Callback cb = mWindow.getCallback(); |
| 591 | if (cb != null && !mWindow.isDestroyed()) { |
| 592 | if (cb.dispatchPopulateAccessibilityEvent(event)) { |
| 593 | return true; |
| 594 | } |
| 595 | } |
| 596 | return super.dispatchPopulateAccessibilityEventInternal(event); |
| 597 | } |
| 598 | |
| 599 | @Override |
| 600 | protected boolean setFrame(int l, int t, int r, int b) { |
| 601 | boolean changed = super.setFrame(l, t, r, b); |
| 602 | if (changed) { |
| 603 | final Rect drawingBounds = mDrawingBounds; |
| 604 | getDrawingRect(drawingBounds); |
| 605 | |
| 606 | Drawable fg = getForeground(); |
| 607 | if (fg != null) { |
| 608 | final Rect frameOffsets = mFrameOffsets; |
| 609 | drawingBounds.left += frameOffsets.left; |
| 610 | drawingBounds.top += frameOffsets.top; |
| 611 | drawingBounds.right -= frameOffsets.right; |
| 612 | drawingBounds.bottom -= frameOffsets.bottom; |
| 613 | fg.setBounds(drawingBounds); |
| 614 | final Rect framePadding = mFramePadding; |
| 615 | drawingBounds.left += framePadding.left - frameOffsets.left; |
| 616 | drawingBounds.top += framePadding.top - frameOffsets.top; |
| 617 | drawingBounds.right -= framePadding.right - frameOffsets.right; |
| 618 | drawingBounds.bottom -= framePadding.bottom - frameOffsets.bottom; |
| 619 | } |
| 620 | |
| 621 | Drawable bg = getBackground(); |
| 622 | if (bg != null) { |
| 623 | bg.setBounds(drawingBounds); |
| 624 | } |
| 625 | |
| 626 | if (SWEEP_OPEN_MENU) { |
| 627 | if (mMenuBackground == null && mFeatureId < 0 |
| 628 | && mWindow.getAttributes().height |
| 629 | == WindowManager.LayoutParams.MATCH_PARENT) { |
| 630 | mMenuBackground = getContext().getDrawable( |
| 631 | R.drawable.menu_background); |
| 632 | } |
| 633 | if (mMenuBackground != null) { |
| 634 | mMenuBackground.setBounds(drawingBounds.left, |
| 635 | drawingBounds.bottom-6, drawingBounds.right, |
| 636 | drawingBounds.bottom+20); |
| 637 | } |
| 638 | } |
| 639 | } |
| 640 | return changed; |
| 641 | } |
| 642 | |
| 643 | @Override |
| 644 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 645 | final DisplayMetrics metrics = getContext().getResources().getDisplayMetrics(); |
Filip Gruszczynski | 34dab0b | 2015-12-22 08:29:07 -0800 | [diff] [blame] | 646 | final boolean isPortrait = |
| 647 | getResources().getConfiguration().orientation == ORIENTATION_PORTRAIT; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 648 | |
| 649 | final int widthMode = getMode(widthMeasureSpec); |
| 650 | final int heightMode = getMode(heightMeasureSpec); |
| 651 | |
| 652 | boolean fixedWidth = false; |
Filip Gruszczynski | adf7b5e | 2016-01-27 16:23:05 -0800 | [diff] [blame] | 653 | mApplyFloatingHorizontalInsets = false; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 654 | if (widthMode == AT_MOST) { |
Filip Gruszczynski | 34dab0b | 2015-12-22 08:29:07 -0800 | [diff] [blame] | 655 | final TypedValue tvw = isPortrait ? mWindow.mFixedWidthMinor : mWindow.mFixedWidthMajor; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 656 | if (tvw != null && tvw.type != TypedValue.TYPE_NULL) { |
| 657 | final int w; |
| 658 | if (tvw.type == TypedValue.TYPE_DIMENSION) { |
| 659 | w = (int) tvw.getDimension(metrics); |
| 660 | } else if (tvw.type == TypedValue.TYPE_FRACTION) { |
| 661 | w = (int) tvw.getFraction(metrics.widthPixels, metrics.widthPixels); |
| 662 | } else { |
| 663 | w = 0; |
| 664 | } |
Filip Gruszczynski | 1937a4c | 2016-01-19 16:17:13 -0800 | [diff] [blame] | 665 | if (DEBUG_MEASURE) Log.d(mLogTag, "Fixed width: " + w); |
Jorim Jaggi | ffd0490 | 2016-02-23 19:20:59 -0500 | [diff] [blame] | 666 | final int widthSize = MeasureSpec.getSize(widthMeasureSpec); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 667 | if (w > 0) { |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 668 | widthMeasureSpec = MeasureSpec.makeMeasureSpec( |
| 669 | Math.min(w, widthSize), EXACTLY); |
| 670 | fixedWidth = true; |
Filip Gruszczynski | adf7b5e | 2016-01-27 16:23:05 -0800 | [diff] [blame] | 671 | } else { |
| 672 | widthMeasureSpec = MeasureSpec.makeMeasureSpec( |
Jorim Jaggi | ffd0490 | 2016-02-23 19:20:59 -0500 | [diff] [blame] | 673 | widthSize - mFloatingInsets.left - mFloatingInsets.right, |
Filip Gruszczynski | adf7b5e | 2016-01-27 16:23:05 -0800 | [diff] [blame] | 674 | AT_MOST); |
| 675 | mApplyFloatingHorizontalInsets = true; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 676 | } |
| 677 | } |
| 678 | } |
| 679 | |
Filip Gruszczynski | adf7b5e | 2016-01-27 16:23:05 -0800 | [diff] [blame] | 680 | mApplyFloatingVerticalInsets = false; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 681 | if (heightMode == AT_MOST) { |
| 682 | final TypedValue tvh = isPortrait ? mWindow.mFixedHeightMajor |
| 683 | : mWindow.mFixedHeightMinor; |
| 684 | if (tvh != null && tvh.type != TypedValue.TYPE_NULL) { |
| 685 | final int h; |
| 686 | if (tvh.type == TypedValue.TYPE_DIMENSION) { |
| 687 | h = (int) tvh.getDimension(metrics); |
| 688 | } else if (tvh.type == TypedValue.TYPE_FRACTION) { |
| 689 | h = (int) tvh.getFraction(metrics.heightPixels, metrics.heightPixels); |
| 690 | } else { |
| 691 | h = 0; |
| 692 | } |
Filip Gruszczynski | 1937a4c | 2016-01-19 16:17:13 -0800 | [diff] [blame] | 693 | if (DEBUG_MEASURE) Log.d(mLogTag, "Fixed height: " + h); |
Filip Gruszczynski | adf7b5e | 2016-01-27 16:23:05 -0800 | [diff] [blame] | 694 | final int heightSize = MeasureSpec.getSize(heightMeasureSpec); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 695 | if (h > 0) { |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 696 | heightMeasureSpec = MeasureSpec.makeMeasureSpec( |
| 697 | Math.min(h, heightSize), EXACTLY); |
Jorim Jaggi | 65bff3e | 2016-02-08 19:17:07 -0800 | [diff] [blame] | 698 | } else if ((mWindow.getAttributes().flags & FLAG_LAYOUT_IN_SCREEN) == 0) { |
Filip Gruszczynski | adf7b5e | 2016-01-27 16:23:05 -0800 | [diff] [blame] | 699 | heightMeasureSpec = MeasureSpec.makeMeasureSpec( |
| 700 | heightSize - mFloatingInsets.top - mFloatingInsets.bottom, AT_MOST); |
| 701 | mApplyFloatingVerticalInsets = true; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 702 | } |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | getOutsets(mOutsets); |
| 707 | if (mOutsets.top > 0 || mOutsets.bottom > 0) { |
| 708 | int mode = MeasureSpec.getMode(heightMeasureSpec); |
| 709 | if (mode != MeasureSpec.UNSPECIFIED) { |
| 710 | int height = MeasureSpec.getSize(heightMeasureSpec); |
| 711 | heightMeasureSpec = MeasureSpec.makeMeasureSpec( |
| 712 | height + mOutsets.top + mOutsets.bottom, mode); |
| 713 | } |
| 714 | } |
| 715 | if (mOutsets.left > 0 || mOutsets.right > 0) { |
| 716 | int mode = MeasureSpec.getMode(widthMeasureSpec); |
| 717 | if (mode != MeasureSpec.UNSPECIFIED) { |
| 718 | int width = MeasureSpec.getSize(widthMeasureSpec); |
| 719 | widthMeasureSpec = MeasureSpec.makeMeasureSpec( |
| 720 | width + mOutsets.left + mOutsets.right, mode); |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 725 | |
| 726 | int width = getMeasuredWidth(); |
| 727 | boolean measure = false; |
| 728 | |
| 729 | widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, EXACTLY); |
| 730 | |
| 731 | if (!fixedWidth && widthMode == AT_MOST) { |
| 732 | final TypedValue tv = isPortrait ? mWindow.mMinWidthMinor : mWindow.mMinWidthMajor; |
| 733 | if (tv.type != TypedValue.TYPE_NULL) { |
| 734 | final int min; |
| 735 | if (tv.type == TypedValue.TYPE_DIMENSION) { |
| 736 | min = (int)tv.getDimension(metrics); |
| 737 | } else if (tv.type == TypedValue.TYPE_FRACTION) { |
Filip Gruszczynski | 34dab0b | 2015-12-22 08:29:07 -0800 | [diff] [blame] | 738 | min = (int)tv.getFraction(mAvailableWidth, mAvailableWidth); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 739 | } else { |
| 740 | min = 0; |
| 741 | } |
Filip Gruszczynski | 1937a4c | 2016-01-19 16:17:13 -0800 | [diff] [blame] | 742 | if (DEBUG_MEASURE) Log.d(mLogTag, "Adjust for min width: " + min + ", value::" |
| 743 | + tv.coerceToString() + ", mAvailableWidth=" + mAvailableWidth); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 744 | |
| 745 | if (width < min) { |
| 746 | widthMeasureSpec = MeasureSpec.makeMeasureSpec(min, EXACTLY); |
| 747 | measure = true; |
| 748 | } |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | // TODO: Support height? |
| 753 | |
| 754 | if (measure) { |
| 755 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | @Override |
| 760 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 761 | super.onLayout(changed, left, top, right, bottom); |
| 762 | getOutsets(mOutsets); |
| 763 | if (mOutsets.left > 0) { |
| 764 | offsetLeftAndRight(-mOutsets.left); |
| 765 | } |
| 766 | if (mOutsets.top > 0) { |
| 767 | offsetTopAndBottom(-mOutsets.top); |
| 768 | } |
Filip Gruszczynski | adf7b5e | 2016-01-27 16:23:05 -0800 | [diff] [blame] | 769 | if (mApplyFloatingVerticalInsets) { |
| 770 | offsetTopAndBottom(mFloatingInsets.top); |
| 771 | } |
| 772 | if (mApplyFloatingHorizontalInsets) { |
| 773 | offsetLeftAndRight(mFloatingInsets.left); |
| 774 | } |
Wale Ogunwale | 2b547c3 | 2015-11-18 10:33:22 -0800 | [diff] [blame] | 775 | |
| 776 | // If the application changed its SystemUI metrics, we might also have to adapt |
| 777 | // our shadow elevation. |
| 778 | updateElevation(); |
| 779 | mAllowUpdateElevation = true; |
Jorim Jaggi | c39c7b0 | 2016-03-24 10:47:07 -0700 | [diff] [blame] | 780 | |
| 781 | if (changed && mResizeMode == RESIZE_MODE_DOCKED_DIVIDER) { |
| 782 | getViewRootImpl().requestInvalidateRootRenderNode(); |
| 783 | } |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | @Override |
| 787 | public void draw(Canvas canvas) { |
| 788 | super.draw(canvas); |
| 789 | |
| 790 | if (mMenuBackground != null) { |
| 791 | mMenuBackground.draw(canvas); |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | @Override |
| 796 | public boolean showContextMenuForChild(View originalView) { |
Oren Blasberg | 23e282d | 2016-04-20 13:43:45 -0700 | [diff] [blame] | 797 | return showContextMenuForChildInternal(originalView, Float.NaN, Float.NaN); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | @Override |
| 801 | public boolean showContextMenuForChild(View originalView, float x, float y) { |
Oren Blasberg | 23e282d | 2016-04-20 13:43:45 -0700 | [diff] [blame] | 802 | return showContextMenuForChildInternal(originalView, x, y); |
Alan Viverette | 021627e | 2015-11-25 14:22:00 -0500 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | private boolean showContextMenuForChildInternal(View originalView, |
Oren Blasberg | 23e282d | 2016-04-20 13:43:45 -0700 | [diff] [blame] | 806 | float x, float y) { |
Alan Viverette | 021627e | 2015-11-25 14:22:00 -0500 | [diff] [blame] | 807 | // Only allow one context menu at a time. |
| 808 | if (mWindow.mContextMenuHelper != null) { |
| 809 | mWindow.mContextMenuHelper.dismiss(); |
| 810 | mWindow.mContextMenuHelper = null; |
| 811 | } |
| 812 | |
| 813 | // Reuse the context menu builder. |
Alan Viverette | 77fb85e | 2015-12-14 11:42:44 -0500 | [diff] [blame] | 814 | final PhoneWindowMenuCallback callback = mWindow.mContextMenuCallback; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 815 | if (mWindow.mContextMenu == null) { |
| 816 | mWindow.mContextMenu = new ContextMenuBuilder(getContext()); |
Alan Viverette | 77fb85e | 2015-12-14 11:42:44 -0500 | [diff] [blame] | 817 | mWindow.mContextMenu.setCallback(callback); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 818 | } else { |
| 819 | mWindow.mContextMenu.clearAll(); |
| 820 | } |
| 821 | |
Alan Viverette | 021627e | 2015-11-25 14:22:00 -0500 | [diff] [blame] | 822 | final MenuHelper helper; |
Oren Blasberg | 23e282d | 2016-04-20 13:43:45 -0700 | [diff] [blame] | 823 | final boolean isPopup = !Float.isNaN(x) && !Float.isNaN(y); |
Alan Viverette | 021627e | 2015-11-25 14:22:00 -0500 | [diff] [blame] | 824 | if (isPopup) { |
| 825 | helper = mWindow.mContextMenu.showPopup(getContext(), originalView, x, y); |
| 826 | } else { |
| 827 | helper = mWindow.mContextMenu.showDialog(originalView, originalView.getWindowToken()); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 828 | } |
Alan Viverette | 021627e | 2015-11-25 14:22:00 -0500 | [diff] [blame] | 829 | |
Alan Viverette | 9084d22 | 2015-12-16 09:56:37 -0500 | [diff] [blame] | 830 | if (helper != null) { |
| 831 | // If it's a dialog, the callback needs to handle showing |
| 832 | // sub-menus. Either way, the callback is required for propagating |
| 833 | // selection to Context.onContextMenuItemSelected(). |
| 834 | callback.setShowDialogForSubmenu(!isPopup); |
| 835 | helper.setPresenterCallback(callback); |
| 836 | } |
Alan Viverette | 021627e | 2015-11-25 14:22:00 -0500 | [diff] [blame] | 837 | |
| 838 | mWindow.mContextMenuHelper = helper; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 839 | return helper != null; |
| 840 | } |
| 841 | |
| 842 | @Override |
| 843 | public ActionMode startActionModeForChild(View originalView, |
| 844 | ActionMode.Callback callback) { |
| 845 | return startActionModeForChild(originalView, callback, ActionMode.TYPE_PRIMARY); |
| 846 | } |
| 847 | |
| 848 | @Override |
| 849 | public ActionMode startActionModeForChild( |
| 850 | View child, ActionMode.Callback callback, int type) { |
| 851 | return startActionMode(child, callback, type); |
| 852 | } |
| 853 | |
| 854 | @Override |
| 855 | public ActionMode startActionMode(ActionMode.Callback callback) { |
| 856 | return startActionMode(callback, ActionMode.TYPE_PRIMARY); |
| 857 | } |
| 858 | |
| 859 | @Override |
| 860 | public ActionMode startActionMode(ActionMode.Callback callback, int type) { |
| 861 | return startActionMode(this, callback, type); |
| 862 | } |
| 863 | |
| 864 | private ActionMode startActionMode( |
| 865 | View originatingView, ActionMode.Callback callback, int type) { |
| 866 | ActionMode.Callback2 wrappedCallback = new ActionModeCallback2Wrapper(callback); |
| 867 | ActionMode mode = null; |
| 868 | if (mWindow.getCallback() != null && !mWindow.isDestroyed()) { |
| 869 | try { |
| 870 | mode = mWindow.getCallback().onWindowStartingActionMode(wrappedCallback, type); |
| 871 | } catch (AbstractMethodError ame) { |
| 872 | // Older apps might not implement the typed version of this method. |
| 873 | if (type == ActionMode.TYPE_PRIMARY) { |
| 874 | try { |
| 875 | mode = mWindow.getCallback().onWindowStartingActionMode( |
| 876 | wrappedCallback); |
| 877 | } catch (AbstractMethodError ame2) { |
| 878 | // Older apps might not implement this callback method at all. |
| 879 | } |
| 880 | } |
| 881 | } |
| 882 | } |
| 883 | if (mode != null) { |
| 884 | if (mode.getType() == ActionMode.TYPE_PRIMARY) { |
| 885 | cleanupPrimaryActionMode(); |
| 886 | mPrimaryActionMode = mode; |
| 887 | } else if (mode.getType() == ActionMode.TYPE_FLOATING) { |
| 888 | if (mFloatingActionMode != null) { |
| 889 | mFloatingActionMode.finish(); |
| 890 | } |
| 891 | mFloatingActionMode = mode; |
| 892 | } |
| 893 | } else { |
| 894 | mode = createActionMode(type, wrappedCallback, originatingView); |
| 895 | if (mode != null && wrappedCallback.onCreateActionMode(mode, mode.getMenu())) { |
| 896 | setHandledActionMode(mode); |
| 897 | } else { |
| 898 | mode = null; |
| 899 | } |
| 900 | } |
| 901 | if (mode != null && mWindow.getCallback() != null && !mWindow.isDestroyed()) { |
| 902 | try { |
| 903 | mWindow.getCallback().onActionModeStarted(mode); |
| 904 | } catch (AbstractMethodError ame) { |
| 905 | // Older apps might not implement this callback method. |
| 906 | } |
| 907 | } |
| 908 | return mode; |
| 909 | } |
| 910 | |
| 911 | private void cleanupPrimaryActionMode() { |
| 912 | if (mPrimaryActionMode != null) { |
| 913 | mPrimaryActionMode.finish(); |
| 914 | mPrimaryActionMode = null; |
| 915 | } |
| 916 | if (mPrimaryActionModeView != null) { |
| 917 | mPrimaryActionModeView.killMode(); |
| 918 | } |
| 919 | } |
| 920 | |
| 921 | private void cleanupFloatingActionModeViews() { |
| 922 | if (mFloatingToolbar != null) { |
| 923 | mFloatingToolbar.dismiss(); |
| 924 | mFloatingToolbar = null; |
| 925 | } |
| 926 | if (mFloatingActionModeOriginatingView != null) { |
| 927 | if (mFloatingToolbarPreDrawListener != null) { |
| 928 | mFloatingActionModeOriginatingView.getViewTreeObserver() |
| 929 | .removeOnPreDrawListener(mFloatingToolbarPreDrawListener); |
| 930 | mFloatingToolbarPreDrawListener = null; |
| 931 | } |
| 932 | mFloatingActionModeOriginatingView = null; |
| 933 | } |
| 934 | } |
| 935 | |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 936 | void startChanging() { |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 937 | mChanging = true; |
| 938 | } |
| 939 | |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 940 | void finishChanging() { |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 941 | mChanging = false; |
| 942 | drawableChanged(); |
| 943 | } |
| 944 | |
| 945 | public void setWindowBackground(Drawable drawable) { |
| 946 | if (getBackground() != drawable) { |
| 947 | setBackgroundDrawable(drawable); |
| 948 | if (drawable != null) { |
Jorim Jaggi | 8e879f1 | 2016-05-25 16:41:49 -0700 | [diff] [blame] | 949 | mResizingBackgroundDrawable = enforceNonTranslucentBackground(drawable, |
| 950 | mWindow.isTranslucent() || mWindow.isShowingWallpaper()); |
Robert Carr | 692a5fe | 2016-05-19 14:11:15 -0700 | [diff] [blame] | 951 | } else { |
| 952 | mResizingBackgroundDrawable = getResizingBackgroundDrawable( |
Nader Jawad | 56c68bc | 2018-04-24 17:08:51 -0700 | [diff] [blame] | 953 | mWindow.mBackgroundDrawable, mWindow.mBackgroundFallbackDrawable, |
Jorim Jaggi | 8e879f1 | 2016-05-25 16:41:49 -0700 | [diff] [blame] | 954 | mWindow.isTranslucent() || mWindow.isShowingWallpaper()); |
Robert Carr | 692a5fe | 2016-05-19 14:11:15 -0700 | [diff] [blame] | 955 | } |
| 956 | if (mResizingBackgroundDrawable != null) { |
| 957 | mResizingBackgroundDrawable.getPadding(mBackgroundPadding); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 958 | } else { |
| 959 | mBackgroundPadding.setEmpty(); |
| 960 | } |
| 961 | drawableChanged(); |
| 962 | } |
| 963 | } |
| 964 | |
| 965 | public void setWindowFrame(Drawable drawable) { |
| 966 | if (getForeground() != drawable) { |
| 967 | setForeground(drawable); |
| 968 | if (drawable != null) { |
| 969 | drawable.getPadding(mFramePadding); |
| 970 | } else { |
| 971 | mFramePadding.setEmpty(); |
| 972 | } |
| 973 | drawableChanged(); |
| 974 | } |
| 975 | } |
| 976 | |
| 977 | @Override |
| 978 | public void onWindowSystemUiVisibilityChanged(int visible) { |
| 979 | updateColorViews(null /* insets */, true /* animate */); |
Garfield Tan | 3b9613c | 2018-12-26 17:08:51 -0800 | [diff] [blame] | 980 | updateDecorCaptionStatus(getResources().getConfiguration()); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | @Override |
| 984 | public WindowInsets onApplyWindowInsets(WindowInsets insets) { |
Filip Gruszczynski | adf7b5e | 2016-01-27 16:23:05 -0800 | [diff] [blame] | 985 | final WindowManager.LayoutParams attrs = mWindow.getAttributes(); |
| 986 | mFloatingInsets.setEmpty(); |
Jorim Jaggi | 65bff3e | 2016-02-08 19:17:07 -0800 | [diff] [blame] | 987 | if ((attrs.flags & FLAG_LAYOUT_IN_SCREEN) == 0) { |
Filip Gruszczynski | adf7b5e | 2016-01-27 16:23:05 -0800 | [diff] [blame] | 988 | // For dialog windows we want to make sure they don't go over the status bar or nav bar. |
| 989 | // We consume the system insets and we will reuse them later during the measure phase. |
Jorim Jaggi | 65bff3e | 2016-02-08 19:17:07 -0800 | [diff] [blame] | 990 | // We allow the app to ignore this and handle insets itself by using |
| 991 | // FLAG_LAYOUT_IN_SCREEN. |
Filip Gruszczynski | adf7b5e | 2016-01-27 16:23:05 -0800 | [diff] [blame] | 992 | if (attrs.height == WindowManager.LayoutParams.WRAP_CONTENT) { |
| 993 | mFloatingInsets.top = insets.getSystemWindowInsetTop(); |
| 994 | mFloatingInsets.bottom = insets.getSystemWindowInsetBottom(); |
Adrian Roos | f7b7426 | 2017-11-22 14:21:01 +0100 | [diff] [blame] | 995 | insets = insets.inset(0, insets.getSystemWindowInsetTop(), |
| 996 | 0, insets.getSystemWindowInsetBottom()); |
Filip Gruszczynski | adf7b5e | 2016-01-27 16:23:05 -0800 | [diff] [blame] | 997 | } |
| 998 | if (mWindow.getAttributes().width == WindowManager.LayoutParams.WRAP_CONTENT) { |
| 999 | mFloatingInsets.left = insets.getSystemWindowInsetTop(); |
| 1000 | mFloatingInsets.right = insets.getSystemWindowInsetBottom(); |
Adrian Roos | f7b7426 | 2017-11-22 14:21:01 +0100 | [diff] [blame] | 1001 | insets = insets.inset(insets.getSystemWindowInsetLeft(), 0, |
| 1002 | insets.getSystemWindowInsetRight(), 0); |
Filip Gruszczynski | adf7b5e | 2016-01-27 16:23:05 -0800 | [diff] [blame] | 1003 | } |
| 1004 | } |
Adrian Roos | 60f5929 | 2018-08-24 16:29:06 +0200 | [diff] [blame] | 1005 | mFrameOffsets.set(insets.getSystemWindowInsetsAsRect()); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1006 | insets = updateColorViews(insets, true /* animate */); |
| 1007 | insets = updateStatusGuard(insets); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1008 | if (getForeground() != null) { |
| 1009 | drawableChanged(); |
| 1010 | } |
| 1011 | return insets; |
| 1012 | } |
| 1013 | |
| 1014 | @Override |
| 1015 | public boolean isTransitionGroup() { |
| 1016 | return false; |
| 1017 | } |
| 1018 | |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 1019 | public static int getColorViewTopInset(int stableTop, int systemTop) { |
Jorim Jaggi | 9511b0f | 2016-01-29 19:12:44 -0800 | [diff] [blame] | 1020 | return Math.min(stableTop, systemTop); |
| 1021 | } |
| 1022 | |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 1023 | public static int getColorViewBottomInset(int stableBottom, int systemBottom) { |
Jorim Jaggi | 9511b0f | 2016-01-29 19:12:44 -0800 | [diff] [blame] | 1024 | return Math.min(stableBottom, systemBottom); |
| 1025 | } |
| 1026 | |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 1027 | public static int getColorViewRightInset(int stableRight, int systemRight) { |
Jorim Jaggi | 9511b0f | 2016-01-29 19:12:44 -0800 | [diff] [blame] | 1028 | return Math.min(stableRight, systemRight); |
| 1029 | } |
| 1030 | |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 1031 | public static int getColorViewLeftInset(int stableLeft, int systemLeft) { |
Adrian Roos | 85d202b | 2016-06-02 16:27:47 -0700 | [diff] [blame] | 1032 | return Math.min(stableLeft, systemLeft); |
| 1033 | } |
| 1034 | |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 1035 | public static boolean isNavBarToRightEdge(int bottomInset, int rightInset) { |
Jorim Jaggi | 9511b0f | 2016-01-29 19:12:44 -0800 | [diff] [blame] | 1036 | return bottomInset == 0 && rightInset > 0; |
| 1037 | } |
| 1038 | |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 1039 | public static boolean isNavBarToLeftEdge(int bottomInset, int leftInset) { |
Adrian Roos | 85d202b | 2016-06-02 16:27:47 -0700 | [diff] [blame] | 1040 | return bottomInset == 0 && leftInset > 0; |
| 1041 | } |
| 1042 | |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 1043 | public static int getNavBarSize(int bottomInset, int rightInset, int leftInset) { |
Adrian Roos | 85d202b | 2016-06-02 16:27:47 -0700 | [diff] [blame] | 1044 | return isNavBarToRightEdge(bottomInset, rightInset) ? rightInset |
| 1045 | : isNavBarToLeftEdge(bottomInset, leftInset) ? leftInset : bottomInset; |
Jorim Jaggi | 9511b0f | 2016-01-29 19:12:44 -0800 | [diff] [blame] | 1046 | } |
| 1047 | |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 1048 | public static void getNavigationBarRect(int canvasWidth, int canvasHeight, Rect stableInsets, |
| 1049 | Rect contentInsets, Rect outRect) { |
| 1050 | final int bottomInset = getColorViewBottomInset(stableInsets.bottom, contentInsets.bottom); |
| 1051 | final int leftInset = getColorViewLeftInset(stableInsets.left, contentInsets.left); |
| 1052 | final int rightInset = getColorViewLeftInset(stableInsets.right, contentInsets.right); |
| 1053 | final int size = getNavBarSize(bottomInset, rightInset, leftInset); |
| 1054 | if (isNavBarToRightEdge(bottomInset, rightInset)) { |
| 1055 | outRect.set(canvasWidth - size, 0, canvasWidth, canvasHeight); |
| 1056 | } else if (isNavBarToLeftEdge(bottomInset, leftInset)) { |
| 1057 | outRect.set(0, 0, size, canvasHeight); |
| 1058 | } else { |
| 1059 | outRect.set(0, canvasHeight - size, canvasWidth, canvasHeight); |
| 1060 | } |
| 1061 | } |
| 1062 | |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1063 | WindowInsets updateColorViews(WindowInsets insets, boolean animate) { |
| 1064 | WindowManager.LayoutParams attrs = mWindow.getAttributes(); |
| 1065 | int sysUiVisibility = attrs.systemUiVisibility | getWindowSystemUiVisibility(); |
| 1066 | |
Yohei Yukawa | 8f162c6 | 2018-01-10 13:18:09 -0800 | [diff] [blame] | 1067 | // IME is an exceptional floating window that requires color view. |
| 1068 | final boolean isImeWindow = |
| 1069 | mWindow.getAttributes().type == WindowManager.LayoutParams.TYPE_INPUT_METHOD; |
| 1070 | if (!mWindow.mIsFloating || isImeWindow) { |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1071 | boolean disallowAnimate = !isLaidOut(); |
| 1072 | disallowAnimate |= ((mLastWindowFlags ^ attrs.flags) |
| 1073 | & FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS) != 0; |
| 1074 | mLastWindowFlags = attrs.flags; |
| 1075 | |
| 1076 | if (insets != null) { |
Jorim Jaggi | 9511b0f | 2016-01-29 19:12:44 -0800 | [diff] [blame] | 1077 | mLastTopInset = getColorViewTopInset(insets.getStableInsetTop(), |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1078 | insets.getSystemWindowInsetTop()); |
Jorim Jaggi | 9511b0f | 2016-01-29 19:12:44 -0800 | [diff] [blame] | 1079 | mLastBottomInset = getColorViewBottomInset(insets.getStableInsetBottom(), |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1080 | insets.getSystemWindowInsetBottom()); |
Jorim Jaggi | 9511b0f | 2016-01-29 19:12:44 -0800 | [diff] [blame] | 1081 | mLastRightInset = getColorViewRightInset(insets.getStableInsetRight(), |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1082 | insets.getSystemWindowInsetRight()); |
Adrian Roos | 85d202b | 2016-06-02 16:27:47 -0700 | [diff] [blame] | 1083 | mLastLeftInset = getColorViewRightInset(insets.getStableInsetLeft(), |
| 1084 | insets.getSystemWindowInsetLeft()); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1085 | |
| 1086 | // Don't animate if the presence of stable insets has changed, because that |
| 1087 | // indicates that the window was either just added and received them for the |
| 1088 | // first time, or the window size or position has changed. |
| 1089 | boolean hasTopStableInset = insets.getStableInsetTop() != 0; |
| 1090 | disallowAnimate |= (hasTopStableInset != mLastHasTopStableInset); |
| 1091 | mLastHasTopStableInset = hasTopStableInset; |
| 1092 | |
| 1093 | boolean hasBottomStableInset = insets.getStableInsetBottom() != 0; |
| 1094 | disallowAnimate |= (hasBottomStableInset != mLastHasBottomStableInset); |
| 1095 | mLastHasBottomStableInset = hasBottomStableInset; |
| 1096 | |
| 1097 | boolean hasRightStableInset = insets.getStableInsetRight() != 0; |
| 1098 | disallowAnimate |= (hasRightStableInset != mLastHasRightStableInset); |
| 1099 | mLastHasRightStableInset = hasRightStableInset; |
Adrian Roos | 85d202b | 2016-06-02 16:27:47 -0700 | [diff] [blame] | 1100 | |
| 1101 | boolean hasLeftStableInset = insets.getStableInsetLeft() != 0; |
| 1102 | disallowAnimate |= (hasLeftStableInset != mLastHasLeftStableInset); |
| 1103 | mLastHasLeftStableInset = hasLeftStableInset; |
| 1104 | |
Jorim Jaggi | e5638a6 | 2016-03-25 22:57:01 -0700 | [diff] [blame] | 1105 | mLastShouldAlwaysConsumeNavBar = insets.shouldAlwaysConsumeNavBar(); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1106 | } |
| 1107 | |
Jorim Jaggi | 9511b0f | 2016-01-29 19:12:44 -0800 | [diff] [blame] | 1108 | boolean navBarToRightEdge = isNavBarToRightEdge(mLastBottomInset, mLastRightInset); |
Adrian Roos | 85d202b | 2016-06-02 16:27:47 -0700 | [diff] [blame] | 1109 | boolean navBarToLeftEdge = isNavBarToLeftEdge(mLastBottomInset, mLastLeftInset); |
| 1110 | int navBarSize = getNavBarSize(mLastBottomInset, mLastRightInset, mLastLeftInset); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1111 | updateColorViewInt(mNavigationColorViewState, sysUiVisibility, |
Jason Monk | ea506c6 | 2017-09-01 12:40:06 -0400 | [diff] [blame] | 1112 | mWindow.mNavigationBarColor, mWindow.mNavigationBarDividerColor, navBarSize, |
| 1113 | navBarToRightEdge || navBarToLeftEdge, navBarToLeftEdge, |
Adrian Roos | 85d202b | 2016-06-02 16:27:47 -0700 | [diff] [blame] | 1114 | 0 /* sideInset */, animate && !disallowAnimate, false /* force */); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1115 | |
| 1116 | boolean statusBarNeedsRightInset = navBarToRightEdge |
| 1117 | && mNavigationColorViewState.present; |
Adrian Roos | 85d202b | 2016-06-02 16:27:47 -0700 | [diff] [blame] | 1118 | boolean statusBarNeedsLeftInset = navBarToLeftEdge |
| 1119 | && mNavigationColorViewState.present; |
| 1120 | int statusBarSideInset = statusBarNeedsRightInset ? mLastRightInset |
| 1121 | : statusBarNeedsLeftInset ? mLastLeftInset : 0; |
Jorim Jaggi | 4fa7892 | 2015-11-30 17:13:56 -0800 | [diff] [blame] | 1122 | updateColorViewInt(mStatusColorViewState, sysUiVisibility, |
Jason Monk | ea506c6 | 2017-09-01 12:40:06 -0400 | [diff] [blame] | 1123 | calculateStatusBarColor(), 0, mLastTopInset, |
Adrian Roos | 85d202b | 2016-06-02 16:27:47 -0700 | [diff] [blame] | 1124 | false /* matchVertical */, statusBarNeedsLeftInset, statusBarSideInset, |
| 1125 | animate && !disallowAnimate, |
Jorim Jaggi | 4fa7892 | 2015-11-30 17:13:56 -0800 | [diff] [blame] | 1126 | mForceWindowDrawsStatusBarBackground); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1127 | } |
| 1128 | |
| 1129 | // When we expand the window with FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS, we still need |
| 1130 | // to ensure that the rest of the view hierarchy doesn't notice it, unless they've |
| 1131 | // explicitly asked for it. |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1132 | boolean consumingNavBar = |
| 1133 | (attrs.flags & FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS) != 0 |
| 1134 | && (sysUiVisibility & SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION) == 0 |
Jorim Jaggi | 0ffd49c | 2016-02-12 15:04:21 -0800 | [diff] [blame] | 1135 | && (sysUiVisibility & SYSTEM_UI_FLAG_HIDE_NAVIGATION) == 0 |
Jorim Jaggi | e5638a6 | 2016-03-25 22:57:01 -0700 | [diff] [blame] | 1136 | || mLastShouldAlwaysConsumeNavBar; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1137 | |
Jorim Jaggi | 9f6798a | 2016-02-10 22:16:06 -0800 | [diff] [blame] | 1138 | // If we didn't request fullscreen layout, but we still got it because of the |
| 1139 | // mForceWindowDrawsStatusBarBackground flag, also consume top inset. |
| 1140 | boolean consumingStatusBar = (sysUiVisibility & SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN) == 0 |
| 1141 | && (sysUiVisibility & FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS) == 0 |
| 1142 | && (attrs.flags & FLAG_LAYOUT_IN_SCREEN) == 0 |
| 1143 | && (attrs.flags & FLAG_LAYOUT_INSET_DECOR) == 0 |
| 1144 | && mForceWindowDrawsStatusBarBackground |
| 1145 | && mLastTopInset != 0; |
| 1146 | |
| 1147 | int consumedTop = consumingStatusBar ? mLastTopInset : 0; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1148 | int consumedRight = consumingNavBar ? mLastRightInset : 0; |
| 1149 | int consumedBottom = consumingNavBar ? mLastBottomInset : 0; |
Adrian Roos | 85d202b | 2016-06-02 16:27:47 -0700 | [diff] [blame] | 1150 | int consumedLeft = consumingNavBar ? mLastLeftInset : 0; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1151 | |
| 1152 | if (mContentRoot != null |
| 1153 | && mContentRoot.getLayoutParams() instanceof MarginLayoutParams) { |
| 1154 | MarginLayoutParams lp = (MarginLayoutParams) mContentRoot.getLayoutParams(); |
Jorim Jaggi | 9f6798a | 2016-02-10 22:16:06 -0800 | [diff] [blame] | 1155 | if (lp.topMargin != consumedTop || lp.rightMargin != consumedRight |
Adrian Roos | 85d202b | 2016-06-02 16:27:47 -0700 | [diff] [blame] | 1156 | || lp.bottomMargin != consumedBottom || lp.leftMargin != consumedLeft) { |
Jorim Jaggi | 9f6798a | 2016-02-10 22:16:06 -0800 | [diff] [blame] | 1157 | lp.topMargin = consumedTop; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1158 | lp.rightMargin = consumedRight; |
| 1159 | lp.bottomMargin = consumedBottom; |
Adrian Roos | 85d202b | 2016-06-02 16:27:47 -0700 | [diff] [blame] | 1160 | lp.leftMargin = consumedLeft; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1161 | mContentRoot.setLayoutParams(lp); |
| 1162 | |
| 1163 | if (insets == null) { |
| 1164 | // The insets have changed, but we're not currently in the process |
| 1165 | // of dispatching them. |
| 1166 | requestApplyInsets(); |
| 1167 | } |
| 1168 | } |
| 1169 | if (insets != null) { |
Adrian Roos | f7b7426 | 2017-11-22 14:21:01 +0100 | [diff] [blame] | 1170 | insets = insets.inset(consumedLeft, consumedTop, consumedRight, consumedBottom); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1171 | } |
| 1172 | } |
| 1173 | |
| 1174 | if (insets != null) { |
| 1175 | insets = insets.consumeStableInsets(); |
| 1176 | } |
| 1177 | return insets; |
| 1178 | } |
| 1179 | |
Jorim Jaggi | 4fa7892 | 2015-11-30 17:13:56 -0800 | [diff] [blame] | 1180 | private int calculateStatusBarColor() { |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 1181 | return calculateStatusBarColor(mWindow.getAttributes().flags, |
| 1182 | mSemiTransparentStatusBarColor, mWindow.mStatusBarColor); |
| 1183 | } |
| 1184 | |
| 1185 | public static int calculateStatusBarColor(int flags, int semiTransparentStatusBarColor, |
| 1186 | int statusBarColor) { |
| 1187 | return (flags & FLAG_TRANSLUCENT_STATUS) != 0 ? semiTransparentStatusBarColor |
| 1188 | : (flags & FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS) != 0 ? statusBarColor |
Jorim Jaggi | 4fa7892 | 2015-11-30 17:13:56 -0800 | [diff] [blame] | 1189 | : Color.BLACK; |
| 1190 | } |
| 1191 | |
Jorim Jaggi | 04c2fbd | 2015-12-02 15:11:45 -0800 | [diff] [blame] | 1192 | private int getCurrentColor(ColorViewState state) { |
| 1193 | if (state.visible) { |
| 1194 | return state.color; |
| 1195 | } else { |
| 1196 | return 0; |
| 1197 | } |
| 1198 | } |
| 1199 | |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1200 | /** |
| 1201 | * Update a color view |
| 1202 | * |
| 1203 | * @param state the color view to update. |
| 1204 | * @param sysUiVis the current systemUiVisibility to apply. |
| 1205 | * @param color the current color to apply. |
Jason Monk | ea506c6 | 2017-09-01 12:40:06 -0400 | [diff] [blame] | 1206 | * @param dividerColor the current divider color to apply. |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1207 | * @param size the current size in the non-parent-matching dimension. |
| 1208 | * @param verticalBar if true the view is attached to a vertical edge, otherwise to a |
| 1209 | * horizontal edge, |
Adrian Roos | 85d202b | 2016-06-02 16:27:47 -0700 | [diff] [blame] | 1210 | * @param sideMargin sideMargin for the color view. |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1211 | * @param animate if true, the change will be animated. |
| 1212 | */ |
| 1213 | private void updateColorViewInt(final ColorViewState state, int sysUiVis, int color, |
Jason Monk | ea506c6 | 2017-09-01 12:40:06 -0400 | [diff] [blame] | 1214 | int dividerColor, int size, boolean verticalBar, boolean seascape, int sideMargin, |
Adrian Roos | 85d202b | 2016-06-02 16:27:47 -0700 | [diff] [blame] | 1215 | boolean animate, boolean force) { |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 1216 | state.present = state.attributes.isPresent(sysUiVis, mWindow.getAttributes().flags, force); |
| 1217 | boolean show = state.attributes.isVisible(state.present, color, |
| 1218 | mWindow.getAttributes().flags, force); |
Jorim Jaggi | 9511b0f | 2016-01-29 19:12:44 -0800 | [diff] [blame] | 1219 | boolean showView = show && !isResizing() && size > 0; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1220 | |
| 1221 | boolean visibilityChanged = false; |
| 1222 | View view = state.view; |
| 1223 | |
| 1224 | int resolvedHeight = verticalBar ? LayoutParams.MATCH_PARENT : size; |
| 1225 | int resolvedWidth = verticalBar ? size : LayoutParams.MATCH_PARENT; |
Adrian Roos | 85d202b | 2016-06-02 16:27:47 -0700 | [diff] [blame] | 1226 | int resolvedGravity = verticalBar |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 1227 | ? (seascape ? state.attributes.seascapeGravity : state.attributes.horizontalGravity) |
| 1228 | : state.attributes.verticalGravity; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1229 | |
| 1230 | if (view == null) { |
Jorim Jaggi | 04c2fbd | 2015-12-02 15:11:45 -0800 | [diff] [blame] | 1231 | if (showView) { |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1232 | state.view = view = new View(mContext); |
Jason Monk | ea506c6 | 2017-09-01 12:40:06 -0400 | [diff] [blame] | 1233 | setColor(view, color, dividerColor, verticalBar, seascape); |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 1234 | view.setTransitionName(state.attributes.transitionName); |
| 1235 | view.setId(state.attributes.id); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1236 | visibilityChanged = true; |
| 1237 | view.setVisibility(INVISIBLE); |
| 1238 | state.targetVisibility = VISIBLE; |
| 1239 | |
| 1240 | LayoutParams lp = new LayoutParams(resolvedWidth, resolvedHeight, |
| 1241 | resolvedGravity); |
Adrian Roos | 85d202b | 2016-06-02 16:27:47 -0700 | [diff] [blame] | 1242 | if (seascape) { |
| 1243 | lp.leftMargin = sideMargin; |
| 1244 | } else { |
| 1245 | lp.rightMargin = sideMargin; |
| 1246 | } |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1247 | addView(view, lp); |
| 1248 | updateColorViewTranslations(); |
| 1249 | } |
| 1250 | } else { |
Jorim Jaggi | 04c2fbd | 2015-12-02 15:11:45 -0800 | [diff] [blame] | 1251 | int vis = showView ? VISIBLE : INVISIBLE; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1252 | visibilityChanged = state.targetVisibility != vis; |
| 1253 | state.targetVisibility = vis; |
| 1254 | LayoutParams lp = (LayoutParams) view.getLayoutParams(); |
Adrian Roos | 85d202b | 2016-06-02 16:27:47 -0700 | [diff] [blame] | 1255 | int rightMargin = seascape ? 0 : sideMargin; |
| 1256 | int leftMargin = seascape ? sideMargin : 0; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1257 | if (lp.height != resolvedHeight || lp.width != resolvedWidth |
Adrian Roos | 85d202b | 2016-06-02 16:27:47 -0700 | [diff] [blame] | 1258 | || lp.gravity != resolvedGravity || lp.rightMargin != rightMargin |
| 1259 | || lp.leftMargin != leftMargin) { |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1260 | lp.height = resolvedHeight; |
| 1261 | lp.width = resolvedWidth; |
| 1262 | lp.gravity = resolvedGravity; |
| 1263 | lp.rightMargin = rightMargin; |
Adrian Roos | 85d202b | 2016-06-02 16:27:47 -0700 | [diff] [blame] | 1264 | lp.leftMargin = leftMargin; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1265 | view.setLayoutParams(lp); |
| 1266 | } |
Jorim Jaggi | 04c2fbd | 2015-12-02 15:11:45 -0800 | [diff] [blame] | 1267 | if (showView) { |
Jason Monk | ea506c6 | 2017-09-01 12:40:06 -0400 | [diff] [blame] | 1268 | setColor(view, color, dividerColor, verticalBar, seascape); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1269 | } |
| 1270 | } |
| 1271 | if (visibilityChanged) { |
| 1272 | view.animate().cancel(); |
Jorim Jaggi | 04c2fbd | 2015-12-02 15:11:45 -0800 | [diff] [blame] | 1273 | if (animate && !isResizing()) { |
| 1274 | if (showView) { |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1275 | if (view.getVisibility() != VISIBLE) { |
| 1276 | view.setVisibility(VISIBLE); |
| 1277 | view.setAlpha(0.0f); |
| 1278 | } |
| 1279 | view.animate().alpha(1.0f).setInterpolator(mShowInterpolator). |
| 1280 | setDuration(mBarEnterExitDuration); |
| 1281 | } else { |
| 1282 | view.animate().alpha(0.0f).setInterpolator(mHideInterpolator) |
| 1283 | .setDuration(mBarEnterExitDuration) |
| 1284 | .withEndAction(new Runnable() { |
| 1285 | @Override |
| 1286 | public void run() { |
| 1287 | state.view.setAlpha(1.0f); |
| 1288 | state.view.setVisibility(INVISIBLE); |
| 1289 | } |
| 1290 | }); |
| 1291 | } |
| 1292 | } else { |
| 1293 | view.setAlpha(1.0f); |
Jorim Jaggi | 04c2fbd | 2015-12-02 15:11:45 -0800 | [diff] [blame] | 1294 | view.setVisibility(showView ? VISIBLE : INVISIBLE); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1295 | } |
| 1296 | } |
Jorim Jaggi | 04c2fbd | 2015-12-02 15:11:45 -0800 | [diff] [blame] | 1297 | state.visible = show; |
| 1298 | state.color = color; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1299 | } |
| 1300 | |
Jason Monk | ea506c6 | 2017-09-01 12:40:06 -0400 | [diff] [blame] | 1301 | private static void setColor(View v, int color, int dividerColor, boolean verticalBar, |
| 1302 | boolean seascape) { |
| 1303 | if (dividerColor != 0) { |
| 1304 | final Pair<Boolean, Boolean> dir = (Pair<Boolean, Boolean>) v.getTag(); |
| 1305 | if (dir == null || dir.first != verticalBar || dir.second != seascape) { |
| 1306 | final int size = Math.round( |
| 1307 | TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, |
| 1308 | v.getContext().getResources().getDisplayMetrics())); |
| 1309 | // Use an inset to make the divider line on the side that faces the app. |
| 1310 | final InsetDrawable d = new InsetDrawable(new ColorDrawable(color), |
| 1311 | verticalBar && !seascape ? size : 0, |
| 1312 | !verticalBar ? size : 0, |
| 1313 | verticalBar && seascape ? size : 0, 0); |
| 1314 | v.setBackground(new LayerDrawable(new Drawable[] { |
| 1315 | new ColorDrawable(dividerColor), d })); |
| 1316 | v.setTag(new Pair<>(verticalBar, seascape)); |
| 1317 | } else { |
| 1318 | final LayerDrawable d = (LayerDrawable) v.getBackground(); |
| 1319 | final InsetDrawable inset = ((InsetDrawable) d.getDrawable(1)); |
| 1320 | ((ColorDrawable) inset.getDrawable()).setColor(color); |
| 1321 | ((ColorDrawable) d.getDrawable(0)).setColor(dividerColor); |
| 1322 | } |
| 1323 | } else { |
| 1324 | v.setTag(null); |
| 1325 | v.setBackgroundColor(color); |
| 1326 | } |
| 1327 | } |
| 1328 | |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1329 | private void updateColorViewTranslations() { |
| 1330 | // Put the color views back in place when they get moved off the screen |
| 1331 | // due to the the ViewRootImpl panning. |
| 1332 | int rootScrollY = mRootScrollY; |
| 1333 | if (mStatusColorViewState.view != null) { |
| 1334 | mStatusColorViewState.view.setTranslationY(rootScrollY > 0 ? rootScrollY : 0); |
| 1335 | } |
| 1336 | if (mNavigationColorViewState.view != null) { |
| 1337 | mNavigationColorViewState.view.setTranslationY(rootScrollY < 0 ? rootScrollY : 0); |
| 1338 | } |
| 1339 | } |
| 1340 | |
| 1341 | private WindowInsets updateStatusGuard(WindowInsets insets) { |
| 1342 | boolean showStatusGuard = false; |
| 1343 | // Show the status guard when the non-overlay contextual action bar is showing |
| 1344 | if (mPrimaryActionModeView != null) { |
| 1345 | if (mPrimaryActionModeView.getLayoutParams() instanceof MarginLayoutParams) { |
| 1346 | // Insets are magic! |
| 1347 | final MarginLayoutParams mlp = (MarginLayoutParams) |
| 1348 | mPrimaryActionModeView.getLayoutParams(); |
| 1349 | boolean mlpChanged = false; |
| 1350 | if (mPrimaryActionModeView.isShown()) { |
| 1351 | if (mTempRect == null) { |
| 1352 | mTempRect = new Rect(); |
| 1353 | } |
| 1354 | final Rect rect = mTempRect; |
| 1355 | |
| 1356 | // If the parent doesn't consume the insets, manually |
| 1357 | // apply the default system window insets. |
| 1358 | mWindow.mContentParent.computeSystemWindowInsets(insets, rect); |
| 1359 | final int newMargin = rect.top == 0 ? insets.getSystemWindowInsetTop() : 0; |
| 1360 | if (mlp.topMargin != newMargin) { |
| 1361 | mlpChanged = true; |
| 1362 | mlp.topMargin = insets.getSystemWindowInsetTop(); |
| 1363 | |
| 1364 | if (mStatusGuard == null) { |
| 1365 | mStatusGuard = new View(mContext); |
| 1366 | mStatusGuard.setBackgroundColor(mContext.getColor( |
Yohei Yukawa | 8f162c6 | 2018-01-10 13:18:09 -0800 | [diff] [blame] | 1367 | R.color.decor_view_status_guard)); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1368 | addView(mStatusGuard, indexOfChild(mStatusColorViewState.view), |
| 1369 | new LayoutParams(LayoutParams.MATCH_PARENT, |
| 1370 | mlp.topMargin, Gravity.START | Gravity.TOP)); |
| 1371 | } else { |
| 1372 | final LayoutParams lp = (LayoutParams) |
| 1373 | mStatusGuard.getLayoutParams(); |
| 1374 | if (lp.height != mlp.topMargin) { |
| 1375 | lp.height = mlp.topMargin; |
| 1376 | mStatusGuard.setLayoutParams(lp); |
| 1377 | } |
| 1378 | } |
| 1379 | } |
| 1380 | |
| 1381 | // The action mode's theme may differ from the app, so |
| 1382 | // always show the status guard above it if we have one. |
| 1383 | showStatusGuard = mStatusGuard != null; |
| 1384 | |
| 1385 | // We only need to consume the insets if the action |
| 1386 | // mode is overlaid on the app content (e.g. it's |
| 1387 | // sitting in a FrameLayout, see |
| 1388 | // screen_simple_overlay_action_mode.xml). |
| 1389 | final boolean nonOverlay = (mWindow.getLocalFeaturesPrivate() |
| 1390 | & (1 << Window.FEATURE_ACTION_MODE_OVERLAY)) == 0; |
Adrian Roos | f7b7426 | 2017-11-22 14:21:01 +0100 | [diff] [blame] | 1391 | if (nonOverlay && showStatusGuard) { |
| 1392 | insets = insets.inset(0, insets.getSystemWindowInsetTop(), 0, 0); |
| 1393 | } |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1394 | } else { |
| 1395 | // reset top margin |
| 1396 | if (mlp.topMargin != 0) { |
| 1397 | mlpChanged = true; |
| 1398 | mlp.topMargin = 0; |
| 1399 | } |
| 1400 | } |
| 1401 | if (mlpChanged) { |
| 1402 | mPrimaryActionModeView.setLayoutParams(mlp); |
| 1403 | } |
| 1404 | } |
| 1405 | } |
| 1406 | if (mStatusGuard != null) { |
| 1407 | mStatusGuard.setVisibility(showStatusGuard ? View.VISIBLE : View.GONE); |
| 1408 | } |
| 1409 | return insets; |
| 1410 | } |
| 1411 | |
Winson Chung | 4d8681f | 2017-05-23 16:22:08 -0700 | [diff] [blame] | 1412 | /** |
| 1413 | * Overrides the view outline when the activity enters picture-in-picture to ensure that it has |
| 1414 | * an opaque shadow even if the window background is completely transparent. This only applies |
| 1415 | * to activities that are currently the task root. |
| 1416 | */ |
| 1417 | public void updatePictureInPictureOutlineProvider(boolean isInPictureInPictureMode) { |
| 1418 | if (mIsInPictureInPictureMode == isInPictureInPictureMode) { |
| 1419 | return; |
| 1420 | } |
| 1421 | |
| 1422 | if (isInPictureInPictureMode) { |
| 1423 | final Window.WindowControllerCallback callback = |
| 1424 | mWindow.getWindowControllerCallback(); |
| 1425 | if (callback != null && callback.isTaskRoot()) { |
| 1426 | // Call super implementation directly as we don't want to save the PIP outline |
| 1427 | // provider to be restored |
| 1428 | super.setOutlineProvider(PIP_OUTLINE_PROVIDER); |
| 1429 | } |
| 1430 | } else { |
| 1431 | // Restore the previous outline provider |
| 1432 | if (getOutlineProvider() != mLastOutlineProvider) { |
| 1433 | setOutlineProvider(mLastOutlineProvider); |
| 1434 | } |
| 1435 | } |
| 1436 | mIsInPictureInPictureMode = isInPictureInPictureMode; |
| 1437 | } |
| 1438 | |
| 1439 | @Override |
| 1440 | public void setOutlineProvider(ViewOutlineProvider provider) { |
| 1441 | super.setOutlineProvider(provider); |
| 1442 | |
| 1443 | // Save the outline provider set to ensure that we can restore when the activity leaves PiP |
| 1444 | mLastOutlineProvider = provider; |
| 1445 | } |
| 1446 | |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1447 | private void drawableChanged() { |
| 1448 | if (mChanging) { |
| 1449 | return; |
| 1450 | } |
| 1451 | |
| 1452 | setPadding(mFramePadding.left + mBackgroundPadding.left, |
| 1453 | mFramePadding.top + mBackgroundPadding.top, |
| 1454 | mFramePadding.right + mBackgroundPadding.right, |
| 1455 | mFramePadding.bottom + mBackgroundPadding.bottom); |
| 1456 | requestLayout(); |
| 1457 | invalidate(); |
| 1458 | |
| 1459 | int opacity = PixelFormat.OPAQUE; |
Wale Ogunwale | 3382ab1 | 2017-07-27 08:55:03 -0700 | [diff] [blame] | 1460 | final WindowConfiguration winConfig = getResources().getConfiguration().windowConfiguration; |
| 1461 | if (winConfig.hasWindowShadow()) { |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1462 | // If the window has a shadow, it must be translucent. |
| 1463 | opacity = PixelFormat.TRANSLUCENT; |
| 1464 | } else{ |
| 1465 | // Note: If there is no background, we will assume opaque. The |
| 1466 | // common case seems to be that an application sets there to be |
| 1467 | // no background so it can draw everything itself. For that, |
| 1468 | // we would like to assume OPAQUE and let the app force it to |
| 1469 | // the slower TRANSLUCENT mode if that is really what it wants. |
| 1470 | Drawable bg = getBackground(); |
| 1471 | Drawable fg = getForeground(); |
| 1472 | if (bg != null) { |
| 1473 | if (fg == null) { |
| 1474 | opacity = bg.getOpacity(); |
| 1475 | } else if (mFramePadding.left <= 0 && mFramePadding.top <= 0 |
| 1476 | && mFramePadding.right <= 0 && mFramePadding.bottom <= 0) { |
| 1477 | // If the frame padding is zero, then we can be opaque |
| 1478 | // if either the frame -or- the background is opaque. |
| 1479 | int fop = fg.getOpacity(); |
| 1480 | int bop = bg.getOpacity(); |
| 1481 | if (false) |
Filip Gruszczynski | 34dab0b | 2015-12-22 08:29:07 -0800 | [diff] [blame] | 1482 | Log.v(mLogTag, "Background opacity: " + bop + ", Frame opacity: " + fop); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1483 | if (fop == PixelFormat.OPAQUE || bop == PixelFormat.OPAQUE) { |
| 1484 | opacity = PixelFormat.OPAQUE; |
| 1485 | } else if (fop == PixelFormat.UNKNOWN) { |
| 1486 | opacity = bop; |
| 1487 | } else if (bop == PixelFormat.UNKNOWN) { |
| 1488 | opacity = fop; |
| 1489 | } else { |
| 1490 | opacity = Drawable.resolveOpacity(fop, bop); |
| 1491 | } |
| 1492 | } else { |
| 1493 | // For now we have to assume translucent if there is a |
| 1494 | // frame with padding... there is no way to tell if the |
| 1495 | // frame and background together will draw all pixels. |
| 1496 | if (false) |
Filip Gruszczynski | 34dab0b | 2015-12-22 08:29:07 -0800 | [diff] [blame] | 1497 | Log.v(mLogTag, "Padding: " + mFramePadding); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1498 | opacity = PixelFormat.TRANSLUCENT; |
| 1499 | } |
| 1500 | } |
| 1501 | if (false) |
Filip Gruszczynski | 34dab0b | 2015-12-22 08:29:07 -0800 | [diff] [blame] | 1502 | Log.v(mLogTag, "Background: " + bg + ", Frame: " + fg); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1503 | } |
| 1504 | |
| 1505 | if (false) |
Filip Gruszczynski | 34dab0b | 2015-12-22 08:29:07 -0800 | [diff] [blame] | 1506 | Log.v(mLogTag, "Selected default opacity: " + opacity); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1507 | |
| 1508 | mDefaultOpacity = opacity; |
| 1509 | if (mFeatureId < 0) { |
| 1510 | mWindow.setDefaultWindowFormat(opacity); |
| 1511 | } |
| 1512 | } |
| 1513 | |
| 1514 | @Override |
| 1515 | public void onWindowFocusChanged(boolean hasWindowFocus) { |
| 1516 | super.onWindowFocusChanged(hasWindowFocus); |
| 1517 | |
| 1518 | // If the user is chording a menu shortcut, release the chord since |
| 1519 | // this window lost focus |
| 1520 | if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) && !hasWindowFocus |
| 1521 | && mWindow.mPanelChordingKey != 0) { |
| 1522 | mWindow.closePanel(Window.FEATURE_OPTIONS_PANEL); |
| 1523 | } |
| 1524 | |
| 1525 | final Window.Callback cb = mWindow.getCallback(); |
| 1526 | if (cb != null && !mWindow.isDestroyed() && mFeatureId < 0) { |
| 1527 | cb.onWindowFocusChanged(hasWindowFocus); |
| 1528 | } |
| 1529 | |
| 1530 | if (mPrimaryActionMode != null) { |
| 1531 | mPrimaryActionMode.onWindowFocusChanged(hasWindowFocus); |
| 1532 | } |
| 1533 | if (mFloatingActionMode != null) { |
| 1534 | mFloatingActionMode.onWindowFocusChanged(hasWindowFocus); |
| 1535 | } |
Wale Ogunwale | 2b547c3 | 2015-11-18 10:33:22 -0800 | [diff] [blame] | 1536 | |
| 1537 | updateElevation(); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1538 | } |
| 1539 | |
| 1540 | @Override |
| 1541 | protected void onAttachedToWindow() { |
| 1542 | super.onAttachedToWindow(); |
| 1543 | |
| 1544 | final Window.Callback cb = mWindow.getCallback(); |
| 1545 | if (cb != null && !mWindow.isDestroyed() && mFeatureId < 0) { |
| 1546 | cb.onAttachedToWindow(); |
| 1547 | } |
| 1548 | |
| 1549 | if (mFeatureId == -1) { |
| 1550 | /* |
| 1551 | * The main window has been attached, try to restore any panels |
| 1552 | * that may have been open before. This is called in cases where |
| 1553 | * an activity is being killed for configuration change and the |
| 1554 | * menu was open. When the activity is recreated, the menu |
| 1555 | * should be shown again. |
| 1556 | */ |
| 1557 | mWindow.openPanelsAfterRestore(); |
| 1558 | } |
Wale Ogunwale | bf9eefc | 2015-11-17 14:47:52 -0800 | [diff] [blame] | 1559 | |
| 1560 | if (!mWindowResizeCallbacksAdded) { |
| 1561 | // If there is no window callback installed there was no window set before. Set it now. |
| 1562 | // Note that our ViewRootImpl object will not change. |
| 1563 | getViewRootImpl().addWindowCallbacks(this); |
| 1564 | mWindowResizeCallbacksAdded = true; |
| 1565 | } else if (mBackdropFrameRenderer != null) { |
| 1566 | // We are resizing and this call happened due to a configuration change. Tell the |
| 1567 | // renderer about it. |
| 1568 | mBackdropFrameRenderer.onConfigurationChange(); |
| 1569 | } |
Andrii Kulian | 51c1b67 | 2017-04-07 18:39:32 -0700 | [diff] [blame] | 1570 | mWindow.onViewRootImplSet(getViewRootImpl()); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1571 | } |
| 1572 | |
| 1573 | @Override |
| 1574 | protected void onDetachedFromWindow() { |
| 1575 | super.onDetachedFromWindow(); |
| 1576 | |
| 1577 | final Window.Callback cb = mWindow.getCallback(); |
| 1578 | if (cb != null && mFeatureId < 0) { |
| 1579 | cb.onDetachedFromWindow(); |
| 1580 | } |
| 1581 | |
| 1582 | if (mWindow.mDecorContentParent != null) { |
| 1583 | mWindow.mDecorContentParent.dismissPopups(); |
| 1584 | } |
| 1585 | |
| 1586 | if (mPrimaryActionModePopup != null) { |
| 1587 | removeCallbacks(mShowPrimaryActionModePopup); |
| 1588 | if (mPrimaryActionModePopup.isShowing()) { |
| 1589 | mPrimaryActionModePopup.dismiss(); |
| 1590 | } |
| 1591 | mPrimaryActionModePopup = null; |
| 1592 | } |
| 1593 | if (mFloatingToolbar != null) { |
| 1594 | mFloatingToolbar.dismiss(); |
| 1595 | mFloatingToolbar = null; |
| 1596 | } |
| 1597 | |
| 1598 | PhoneWindow.PanelFeatureState st = mWindow.getPanelState(Window.FEATURE_OPTIONS_PANEL, false); |
| 1599 | if (st != null && st.menu != null && mFeatureId < 0) { |
| 1600 | st.menu.close(); |
| 1601 | } |
Wale Ogunwale | bf9eefc | 2015-11-17 14:47:52 -0800 | [diff] [blame] | 1602 | |
Jorim Jaggi | b2005a0 | 2016-04-08 14:13:30 -0700 | [diff] [blame] | 1603 | releaseThreadedRenderer(); |
| 1604 | |
Wale Ogunwale | bf9eefc | 2015-11-17 14:47:52 -0800 | [diff] [blame] | 1605 | if (mWindowResizeCallbacksAdded) { |
| 1606 | getViewRootImpl().removeWindowCallbacks(this); |
| 1607 | mWindowResizeCallbacksAdded = false; |
| 1608 | } |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1609 | } |
| 1610 | |
| 1611 | @Override |
| 1612 | public void onCloseSystemDialogs(String reason) { |
| 1613 | if (mFeatureId >= 0) { |
| 1614 | mWindow.closeAllPanels(); |
| 1615 | } |
| 1616 | } |
| 1617 | |
| 1618 | public android.view.SurfaceHolder.Callback2 willYouTakeTheSurface() { |
| 1619 | return mFeatureId < 0 ? mWindow.mTakeSurfaceCallback : null; |
| 1620 | } |
| 1621 | |
| 1622 | public InputQueue.Callback willYouTakeTheInputQueue() { |
| 1623 | return mFeatureId < 0 ? mWindow.mTakeInputQueueCallback : null; |
| 1624 | } |
| 1625 | |
| 1626 | public void setSurfaceType(int type) { |
| 1627 | mWindow.setType(type); |
| 1628 | } |
| 1629 | |
| 1630 | public void setSurfaceFormat(int format) { |
| 1631 | mWindow.setFormat(format); |
| 1632 | } |
| 1633 | |
| 1634 | public void setSurfaceKeepScreenOn(boolean keepOn) { |
| 1635 | if (keepOn) mWindow.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); |
| 1636 | else mWindow.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); |
| 1637 | } |
| 1638 | |
| 1639 | @Override |
| 1640 | public void onRootViewScrollYChanged(int rootScrollY) { |
| 1641 | mRootScrollY = rootScrollY; |
Evan Rosky | 0d654cb | 2019-02-26 10:59:10 -0800 | [diff] [blame] | 1642 | if (mDecorCaptionView != null) { |
| 1643 | mDecorCaptionView.onRootViewScrollYChanged(rootScrollY); |
| 1644 | } |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1645 | updateColorViewTranslations(); |
| 1646 | } |
| 1647 | |
| 1648 | private ActionMode createActionMode( |
| 1649 | int type, ActionMode.Callback2 callback, View originatingView) { |
| 1650 | switch (type) { |
| 1651 | case ActionMode.TYPE_PRIMARY: |
| 1652 | default: |
| 1653 | return createStandaloneActionMode(callback); |
| 1654 | case ActionMode.TYPE_FLOATING: |
| 1655 | return createFloatingActionMode(originatingView, callback); |
| 1656 | } |
| 1657 | } |
| 1658 | |
| 1659 | private void setHandledActionMode(ActionMode mode) { |
| 1660 | if (mode.getType() == ActionMode.TYPE_PRIMARY) { |
| 1661 | setHandledPrimaryActionMode(mode); |
| 1662 | } else if (mode.getType() == ActionMode.TYPE_FLOATING) { |
| 1663 | setHandledFloatingActionMode(mode); |
| 1664 | } |
| 1665 | } |
| 1666 | |
| 1667 | private ActionMode createStandaloneActionMode(ActionMode.Callback callback) { |
| 1668 | endOnGoingFadeAnimation(); |
| 1669 | cleanupPrimaryActionMode(); |
Andrii Kulian | 8d6ac26 | 2016-06-08 13:14:19 -0700 | [diff] [blame] | 1670 | // We want to create new mPrimaryActionModeView in two cases: if there is no existing |
| 1671 | // instance at all, or if there is one, but it is detached from window. The latter case |
| 1672 | // might happen when app is resized in multi-window mode and decor view is preserved |
| 1673 | // along with the main app window. Keeping mPrimaryActionModeView reference doesn't cause |
| 1674 | // app memory leaks because killMode() is called when the dismiss animation ends and from |
| 1675 | // cleanupPrimaryActionMode() invocation above. |
| 1676 | if (mPrimaryActionModeView == null || !mPrimaryActionModeView.isAttachedToWindow()) { |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1677 | if (mWindow.isFloating()) { |
| 1678 | // Use the action bar theme. |
| 1679 | final TypedValue outValue = new TypedValue(); |
| 1680 | final Resources.Theme baseTheme = mContext.getTheme(); |
| 1681 | baseTheme.resolveAttribute(R.attr.actionBarTheme, outValue, true); |
| 1682 | |
| 1683 | final Context actionBarContext; |
| 1684 | if (outValue.resourceId != 0) { |
| 1685 | final Resources.Theme actionBarTheme = mContext.getResources().newTheme(); |
| 1686 | actionBarTheme.setTo(baseTheme); |
| 1687 | actionBarTheme.applyStyle(outValue.resourceId, true); |
| 1688 | |
| 1689 | actionBarContext = new ContextThemeWrapper(mContext, 0); |
| 1690 | actionBarContext.getTheme().setTo(actionBarTheme); |
| 1691 | } else { |
| 1692 | actionBarContext = mContext; |
| 1693 | } |
| 1694 | |
| 1695 | mPrimaryActionModeView = new ActionBarContextView(actionBarContext); |
| 1696 | mPrimaryActionModePopup = new PopupWindow(actionBarContext, null, |
| 1697 | R.attr.actionModePopupWindowStyle); |
| 1698 | mPrimaryActionModePopup.setWindowLayoutType( |
| 1699 | WindowManager.LayoutParams.TYPE_APPLICATION); |
| 1700 | mPrimaryActionModePopup.setContentView(mPrimaryActionModeView); |
| 1701 | mPrimaryActionModePopup.setWidth(MATCH_PARENT); |
| 1702 | |
| 1703 | actionBarContext.getTheme().resolveAttribute( |
| 1704 | R.attr.actionBarSize, outValue, true); |
| 1705 | final int height = TypedValue.complexToDimensionPixelSize(outValue.data, |
| 1706 | actionBarContext.getResources().getDisplayMetrics()); |
| 1707 | mPrimaryActionModeView.setContentHeight(height); |
| 1708 | mPrimaryActionModePopup.setHeight(WRAP_CONTENT); |
| 1709 | mShowPrimaryActionModePopup = new Runnable() { |
| 1710 | public void run() { |
| 1711 | mPrimaryActionModePopup.showAtLocation( |
| 1712 | mPrimaryActionModeView.getApplicationWindowToken(), |
| 1713 | Gravity.TOP | Gravity.FILL_HORIZONTAL, 0, 0); |
| 1714 | endOnGoingFadeAnimation(); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1715 | |
Chris Banes | e65b3fb | 2016-06-01 11:39:54 +0100 | [diff] [blame] | 1716 | if (shouldAnimatePrimaryActionModeView()) { |
| 1717 | mFadeAnim = ObjectAnimator.ofFloat(mPrimaryActionModeView, View.ALPHA, |
| 1718 | 0f, 1f); |
| 1719 | mFadeAnim.addListener(new AnimatorListenerAdapter() { |
| 1720 | @Override |
| 1721 | public void onAnimationStart(Animator animation) { |
| 1722 | mPrimaryActionModeView.setVisibility(VISIBLE); |
| 1723 | } |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1724 | |
Chris Banes | e65b3fb | 2016-06-01 11:39:54 +0100 | [diff] [blame] | 1725 | @Override |
| 1726 | public void onAnimationEnd(Animator animation) { |
| 1727 | mPrimaryActionModeView.setAlpha(1f); |
| 1728 | mFadeAnim = null; |
| 1729 | } |
| 1730 | }); |
| 1731 | mFadeAnim.start(); |
| 1732 | } else { |
| 1733 | mPrimaryActionModeView.setAlpha(1f); |
| 1734 | mPrimaryActionModeView.setVisibility(VISIBLE); |
| 1735 | } |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1736 | } |
| 1737 | }; |
| 1738 | } else { |
Alan Viverette | 51efddb | 2017-04-05 10:00:01 -0400 | [diff] [blame] | 1739 | ViewStub stub = findViewById(R.id.action_mode_bar_stub); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1740 | if (stub != null) { |
| 1741 | mPrimaryActionModeView = (ActionBarContextView) stub.inflate(); |
Andrii Kulian | 8d6ac26 | 2016-06-08 13:14:19 -0700 | [diff] [blame] | 1742 | mPrimaryActionModePopup = null; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1743 | } |
| 1744 | } |
| 1745 | } |
| 1746 | if (mPrimaryActionModeView != null) { |
| 1747 | mPrimaryActionModeView.killMode(); |
| 1748 | ActionMode mode = new StandaloneActionMode( |
| 1749 | mPrimaryActionModeView.getContext(), mPrimaryActionModeView, |
| 1750 | callback, mPrimaryActionModePopup == null); |
| 1751 | return mode; |
| 1752 | } |
| 1753 | return null; |
| 1754 | } |
| 1755 | |
| 1756 | private void endOnGoingFadeAnimation() { |
| 1757 | if (mFadeAnim != null) { |
| 1758 | mFadeAnim.end(); |
| 1759 | } |
| 1760 | } |
| 1761 | |
| 1762 | private void setHandledPrimaryActionMode(ActionMode mode) { |
| 1763 | endOnGoingFadeAnimation(); |
| 1764 | mPrimaryActionMode = mode; |
| 1765 | mPrimaryActionMode.invalidate(); |
| 1766 | mPrimaryActionModeView.initForMode(mPrimaryActionMode); |
| 1767 | if (mPrimaryActionModePopup != null) { |
| 1768 | post(mShowPrimaryActionModePopup); |
| 1769 | } else { |
Chris Banes | e65b3fb | 2016-06-01 11:39:54 +0100 | [diff] [blame] | 1770 | if (shouldAnimatePrimaryActionModeView()) { |
| 1771 | mFadeAnim = ObjectAnimator.ofFloat(mPrimaryActionModeView, View.ALPHA, 0f, 1f); |
| 1772 | mFadeAnim.addListener(new AnimatorListenerAdapter() { |
| 1773 | @Override |
| 1774 | public void onAnimationStart(Animator animation) { |
| 1775 | mPrimaryActionModeView.setVisibility(View.VISIBLE); |
| 1776 | } |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1777 | |
Chris Banes | e65b3fb | 2016-06-01 11:39:54 +0100 | [diff] [blame] | 1778 | @Override |
| 1779 | public void onAnimationEnd(Animator animation) { |
| 1780 | mPrimaryActionModeView.setAlpha(1f); |
| 1781 | mFadeAnim = null; |
| 1782 | } |
| 1783 | }); |
| 1784 | mFadeAnim.start(); |
| 1785 | } else { |
| 1786 | mPrimaryActionModeView.setAlpha(1f); |
| 1787 | mPrimaryActionModeView.setVisibility(View.VISIBLE); |
| 1788 | } |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1789 | } |
| 1790 | mPrimaryActionModeView.sendAccessibilityEvent( |
| 1791 | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED); |
| 1792 | } |
| 1793 | |
Chris Banes | e65b3fb | 2016-06-01 11:39:54 +0100 | [diff] [blame] | 1794 | boolean shouldAnimatePrimaryActionModeView() { |
| 1795 | // We only to animate the action mode in if the decor has already been laid out. |
| 1796 | // If it hasn't been laid out, it hasn't been drawn to screen yet. |
| 1797 | return isLaidOut(); |
| 1798 | } |
| 1799 | |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1800 | private ActionMode createFloatingActionMode( |
| 1801 | View originatingView, ActionMode.Callback2 callback) { |
| 1802 | if (mFloatingActionMode != null) { |
| 1803 | mFloatingActionMode.finish(); |
| 1804 | } |
| 1805 | cleanupFloatingActionModeViews(); |
Tarandeep Singh | c9c83a9 | 2017-08-29 14:39:22 -0700 | [diff] [blame] | 1806 | mFloatingToolbar = new FloatingToolbar(mWindow); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1807 | final FloatingActionMode mode = |
Abodunrinwa Toki | 17293cc | 2017-05-22 14:16:04 +0100 | [diff] [blame] | 1808 | new FloatingActionMode(mContext, callback, originatingView, mFloatingToolbar); |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1809 | mFloatingActionModeOriginatingView = originatingView; |
| 1810 | mFloatingToolbarPreDrawListener = |
| 1811 | new ViewTreeObserver.OnPreDrawListener() { |
| 1812 | @Override |
| 1813 | public boolean onPreDraw() { |
| 1814 | mode.updateViewLocationInWindow(); |
| 1815 | return true; |
| 1816 | } |
| 1817 | }; |
| 1818 | return mode; |
| 1819 | } |
| 1820 | |
| 1821 | private void setHandledFloatingActionMode(ActionMode mode) { |
| 1822 | mFloatingActionMode = mode; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1823 | mFloatingActionMode.invalidate(); // Will show the floating toolbar if necessary. |
| 1824 | mFloatingActionModeOriginatingView.getViewTreeObserver() |
| 1825 | .addOnPreDrawListener(mFloatingToolbarPreDrawListener); |
| 1826 | } |
| 1827 | |
| 1828 | /** |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 1829 | * Informs the decor if the caption is attached and visible. |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1830 | * @param attachedAndVisible true when the decor is visible. |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 1831 | * Note that this will even be called if there is no caption. |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1832 | **/ |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 1833 | void enableCaption(boolean attachedAndVisible) { |
| 1834 | if (mHasCaption != attachedAndVisible) { |
| 1835 | mHasCaption = attachedAndVisible; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1836 | if (getForeground() != null) { |
| 1837 | drawableChanged(); |
| 1838 | } |
| 1839 | } |
| 1840 | } |
| 1841 | |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 1842 | void setWindow(PhoneWindow phoneWindow) { |
| 1843 | mWindow = phoneWindow; |
| 1844 | Context context = getContext(); |
| 1845 | if (context instanceof DecorContext) { |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 1846 | DecorContext decorContext = (DecorContext) context; |
| 1847 | decorContext.setPhoneWindow(mWindow); |
| 1848 | } |
| 1849 | } |
| 1850 | |
Filip Gruszczynski | 1937a4c | 2016-01-19 16:17:13 -0800 | [diff] [blame] | 1851 | @Override |
chaviw | a213d30 | 2018-04-23 13:34:36 -0700 | [diff] [blame] | 1852 | public Resources getResources() { |
| 1853 | // Make sure the Resources object is propogated from the Context since it can be updated in |
| 1854 | // the Context object. |
| 1855 | return getContext().getResources(); |
| 1856 | } |
| 1857 | |
| 1858 | @Override |
Filip Gruszczynski | 1937a4c | 2016-01-19 16:17:13 -0800 | [diff] [blame] | 1859 | protected void onConfigurationChanged(Configuration newConfig) { |
| 1860 | super.onConfigurationChanged(newConfig); |
Wale Ogunwale | 3382ab1 | 2017-07-27 08:55:03 -0700 | [diff] [blame] | 1861 | |
Garfield Tan | 3b9613c | 2018-12-26 17:08:51 -0800 | [diff] [blame] | 1862 | updateDecorCaptionStatus(newConfig); |
| 1863 | |
| 1864 | updateAvailableWidth(); |
| 1865 | initializeElevation(); |
| 1866 | } |
| 1867 | |
| 1868 | /** |
| 1869 | * Determines if the workspace is entirely covered by the window. |
| 1870 | * @return {@code true} when the window is filling the entire screen/workspace. |
| 1871 | **/ |
| 1872 | private boolean isFillingScreen(Configuration config) { |
| 1873 | final boolean isFullscreen = config.windowConfiguration.getWindowingMode() |
| 1874 | == WindowConfiguration.WINDOWING_MODE_FULLSCREEN; |
| 1875 | return isFullscreen && (0 != ((getWindowSystemUiVisibility() | getSystemUiVisibility()) |
| 1876 | & (View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
| 1877 | | View.SYSTEM_UI_FLAG_IMMERSIVE | View.SYSTEM_UI_FLAG_LOW_PROFILE))); |
| 1878 | } |
| 1879 | |
| 1880 | private void updateDecorCaptionStatus(Configuration config) { |
| 1881 | final boolean displayWindowDecor = config.windowConfiguration.hasWindowDecorCaption() |
| 1882 | && !isFillingScreen(config); |
Wale Ogunwale | 3382ab1 | 2017-07-27 08:55:03 -0700 | [diff] [blame] | 1883 | if (mDecorCaptionView == null && displayWindowDecor) { |
| 1884 | // Configuration now requires a caption. |
| 1885 | final LayoutInflater inflater = mWindow.getLayoutInflater(); |
| 1886 | mDecorCaptionView = createDecorCaptionView(inflater); |
| 1887 | if (mDecorCaptionView != null) { |
| 1888 | if (mDecorCaptionView.getParent() == null) { |
| 1889 | addView(mDecorCaptionView, 0, |
| 1890 | new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT)); |
Wale Ogunwale | eb6722c | 2015-12-08 11:43:43 -0800 | [diff] [blame] | 1891 | } |
Wale Ogunwale | 3382ab1 | 2017-07-27 08:55:03 -0700 | [diff] [blame] | 1892 | removeView(mContentRoot); |
| 1893 | mDecorCaptionView.addView(mContentRoot, |
| 1894 | new ViewGroup.MarginLayoutParams(MATCH_PARENT, MATCH_PARENT)); |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 1895 | } |
Wale Ogunwale | 3382ab1 | 2017-07-27 08:55:03 -0700 | [diff] [blame] | 1896 | } else if (mDecorCaptionView != null) { |
| 1897 | // We might have to change the kind of surface before we do anything else. |
| 1898 | mDecorCaptionView.onConfigurationChanged(displayWindowDecor); |
| 1899 | enableCaption(displayWindowDecor); |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 1900 | } |
| 1901 | } |
| 1902 | |
Filip Gruszczynski | 3dec081 | 2015-12-09 08:42:41 -0800 | [diff] [blame] | 1903 | void onResourcesLoaded(LayoutInflater inflater, int layoutResource) { |
Wale Ogunwale | bf9eefc | 2015-11-17 14:47:52 -0800 | [diff] [blame] | 1904 | if (mBackdropFrameRenderer != null) { |
Filip Gruszczynski | a40fd09 | 2016-01-07 16:38:11 -0800 | [diff] [blame] | 1905 | loadBackgroundDrawablesIfNeeded(); |
Wale Ogunwale | bf9eefc | 2015-11-17 14:47:52 -0800 | [diff] [blame] | 1906 | mBackdropFrameRenderer.onResourcesLoaded( |
Jorim Jaggi | 04c2fbd | 2015-12-02 15:11:45 -0800 | [diff] [blame] | 1907 | this, mResizingBackgroundDrawable, mCaptionBackgroundDrawable, |
Jorim Jaggi | 9511b0f | 2016-01-29 19:12:44 -0800 | [diff] [blame] | 1908 | mUserCaptionBackgroundDrawable, getCurrentColor(mStatusColorViewState), |
| 1909 | getCurrentColor(mNavigationColorViewState)); |
Wale Ogunwale | bf9eefc | 2015-11-17 14:47:52 -0800 | [diff] [blame] | 1910 | } |
| 1911 | |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 1912 | mDecorCaptionView = createDecorCaptionView(inflater); |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 1913 | final View root = inflater.inflate(layoutResource, null); |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 1914 | if (mDecorCaptionView != null) { |
| 1915 | if (mDecorCaptionView.getParent() == null) { |
| 1916 | addView(mDecorCaptionView, |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 1917 | new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT)); |
| 1918 | } |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 1919 | mDecorCaptionView.addView(root, |
Filip Gruszczynski | 6325065 | 2015-11-18 14:43:01 -0800 | [diff] [blame] | 1920 | new ViewGroup.MarginLayoutParams(MATCH_PARENT, MATCH_PARENT)); |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 1921 | } else { |
Jorim Jaggi | 0a13bfd | 2016-02-04 18:34:50 -0800 | [diff] [blame] | 1922 | |
| 1923 | // Put it below the color views. |
| 1924 | addView(root, 0, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT)); |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 1925 | } |
| 1926 | mContentRoot = (ViewGroup) root; |
Wale Ogunwale | 2b547c3 | 2015-11-18 10:33:22 -0800 | [diff] [blame] | 1927 | initializeElevation(); |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 1928 | } |
| 1929 | |
Filip Gruszczynski | a40fd09 | 2016-01-07 16:38:11 -0800 | [diff] [blame] | 1930 | private void loadBackgroundDrawablesIfNeeded() { |
| 1931 | if (mResizingBackgroundDrawable == null) { |
Nader Jawad | 56c68bc | 2018-04-24 17:08:51 -0700 | [diff] [blame] | 1932 | mResizingBackgroundDrawable = getResizingBackgroundDrawable(mWindow.mBackgroundDrawable, |
| 1933 | mWindow.mBackgroundFallbackDrawable, mWindow.isTranslucent() |
| 1934 | || mWindow.isShowingWallpaper()); |
Winson Chung | 1af8eda | 2016-02-05 17:55:56 +0000 | [diff] [blame] | 1935 | if (mResizingBackgroundDrawable == null) { |
| 1936 | // We shouldn't really get here as the background fallback should be always |
| 1937 | // available since it is defaulted by the system. |
| 1938 | Log.w(mLogTag, "Failed to find background drawable for PhoneWindow=" + mWindow); |
| 1939 | } |
Filip Gruszczynski | a40fd09 | 2016-01-07 16:38:11 -0800 | [diff] [blame] | 1940 | } |
| 1941 | if (mCaptionBackgroundDrawable == null) { |
| 1942 | mCaptionBackgroundDrawable = getContext().getDrawable( |
| 1943 | R.drawable.decor_caption_title_focused); |
| 1944 | } |
Chong Zhang | 0df63d5 | 2016-02-24 15:39:53 -0800 | [diff] [blame] | 1945 | if (mResizingBackgroundDrawable != null) { |
| 1946 | mLastBackgroundDrawableCb = mResizingBackgroundDrawable.getCallback(); |
| 1947 | mResizingBackgroundDrawable.setCallback(null); |
| 1948 | } |
Filip Gruszczynski | a40fd09 | 2016-01-07 16:38:11 -0800 | [diff] [blame] | 1949 | } |
| 1950 | |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 1951 | // Free floating overlapping windows require a caption. |
| 1952 | private DecorCaptionView createDecorCaptionView(LayoutInflater inflater) { |
Filip Gruszczynski | 3dec081 | 2015-12-09 08:42:41 -0800 | [diff] [blame] | 1953 | DecorCaptionView decorCaptionView = null; |
| 1954 | for (int i = getChildCount() - 1; i >= 0 && decorCaptionView == null; i--) { |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 1955 | View view = getChildAt(i); |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 1956 | if (view instanceof DecorCaptionView) { |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 1957 | // The decor was most likely saved from a relaunch - so reuse it. |
Filip Gruszczynski | 3dec081 | 2015-12-09 08:42:41 -0800 | [diff] [blame] | 1958 | decorCaptionView = (DecorCaptionView) view; |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 1959 | removeViewAt(i); |
| 1960 | } |
| 1961 | } |
| 1962 | final WindowManager.LayoutParams attrs = mWindow.getAttributes(); |
Wale Ogunwale | 2b547c3 | 2015-11-18 10:33:22 -0800 | [diff] [blame] | 1963 | final boolean isApplication = attrs.type == TYPE_BASE_APPLICATION || |
Chong Zhang | fea963e | 2016-08-15 17:14:16 -0700 | [diff] [blame] | 1964 | attrs.type == TYPE_APPLICATION || attrs.type == TYPE_DRAWN_APPLICATION; |
Wale Ogunwale | 3382ab1 | 2017-07-27 08:55:03 -0700 | [diff] [blame] | 1965 | final WindowConfiguration winConfig = getResources().getConfiguration().windowConfiguration; |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 1966 | // Only a non floating application window on one of the allowed workspaces can get a caption |
Wale Ogunwale | 3382ab1 | 2017-07-27 08:55:03 -0700 | [diff] [blame] | 1967 | if (!mWindow.isFloating() && isApplication && winConfig.hasWindowDecorCaption()) { |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 1968 | // Dependent on the brightness of the used title we either use the |
| 1969 | // dark or the light button frame. |
Filip Gruszczynski | 3dec081 | 2015-12-09 08:42:41 -0800 | [diff] [blame] | 1970 | if (decorCaptionView == null) { |
| 1971 | decorCaptionView = inflateDecorCaptionView(inflater); |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 1972 | } |
Filip Gruszczynski | 3dec081 | 2015-12-09 08:42:41 -0800 | [diff] [blame] | 1973 | decorCaptionView.setPhoneWindow(mWindow, true /*showDecor*/); |
Wale Ogunwale | 8cc5a74 | 2015-11-17 15:41:05 -0800 | [diff] [blame] | 1974 | } else { |
Filip Gruszczynski | 3dec081 | 2015-12-09 08:42:41 -0800 | [diff] [blame] | 1975 | decorCaptionView = null; |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 1976 | } |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 1977 | |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 1978 | // Tell the decor if it has a visible caption. |
Filip Gruszczynski | 3dec081 | 2015-12-09 08:42:41 -0800 | [diff] [blame] | 1979 | enableCaption(decorCaptionView != null); |
| 1980 | return decorCaptionView; |
| 1981 | } |
| 1982 | |
| 1983 | private DecorCaptionView inflateDecorCaptionView(LayoutInflater inflater) { |
| 1984 | final Context context = getContext(); |
| 1985 | // We make a copy of the inflater, so it has the right context associated with it. |
| 1986 | inflater = inflater.from(context); |
| 1987 | final DecorCaptionView view = (DecorCaptionView) inflater.inflate(R.layout.decor_caption, |
| 1988 | null); |
| 1989 | setDecorCaptionShade(context, view); |
| 1990 | return view; |
| 1991 | } |
| 1992 | |
| 1993 | private void setDecorCaptionShade(Context context, DecorCaptionView view) { |
| 1994 | final int shade = mWindow.getDecorCaptionShade(); |
| 1995 | switch (shade) { |
| 1996 | case DECOR_CAPTION_SHADE_LIGHT: |
| 1997 | setLightDecorCaptionShade(view); |
| 1998 | break; |
| 1999 | case DECOR_CAPTION_SHADE_DARK: |
| 2000 | setDarkDecorCaptionShade(view); |
| 2001 | break; |
| 2002 | default: { |
| 2003 | TypedValue value = new TypedValue(); |
| 2004 | context.getTheme().resolveAttribute(R.attr.colorPrimary, value, true); |
| 2005 | // We invert the shade depending on brightness of the theme. Dark shade for light |
| 2006 | // theme and vice versa. Thanks to this the buttons should be visible on the |
| 2007 | // background. |
| 2008 | if (Color.luminance(value.data) < 0.5) { |
| 2009 | setLightDecorCaptionShade(view); |
| 2010 | } else { |
| 2011 | setDarkDecorCaptionShade(view); |
| 2012 | } |
| 2013 | break; |
| 2014 | } |
| 2015 | } |
| 2016 | } |
| 2017 | |
| 2018 | void updateDecorCaptionShade() { |
| 2019 | if (mDecorCaptionView != null) { |
| 2020 | setDecorCaptionShade(getContext(), mDecorCaptionView); |
| 2021 | } |
| 2022 | } |
| 2023 | |
| 2024 | private void setLightDecorCaptionShade(DecorCaptionView view) { |
| 2025 | view.findViewById(R.id.maximize_window).setBackgroundResource( |
| 2026 | R.drawable.decor_maximize_button_light); |
| 2027 | view.findViewById(R.id.close_window).setBackgroundResource( |
| 2028 | R.drawable.decor_close_button_light); |
| 2029 | } |
| 2030 | |
| 2031 | private void setDarkDecorCaptionShade(DecorCaptionView view) { |
| 2032 | view.findViewById(R.id.maximize_window).setBackgroundResource( |
| 2033 | R.drawable.decor_maximize_button_dark); |
| 2034 | view.findViewById(R.id.close_window).setBackgroundResource( |
| 2035 | R.drawable.decor_close_button_dark); |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 2036 | } |
| 2037 | |
| 2038 | /** |
Wale Ogunwale | bf9eefc | 2015-11-17 14:47:52 -0800 | [diff] [blame] | 2039 | * Returns the color used to fill areas the app has not rendered content to yet when the |
| 2040 | * user is resizing the window of an activity in multi-window mode. |
| 2041 | */ |
Nader Jawad | 56c68bc | 2018-04-24 17:08:51 -0700 | [diff] [blame] | 2042 | public static Drawable getResizingBackgroundDrawable(@Nullable Drawable backgroundDrawable, |
| 2043 | @Nullable Drawable fallbackDrawable, boolean windowTranslucent) { |
| 2044 | if (backgroundDrawable != null) { |
| 2045 | return enforceNonTranslucentBackground(backgroundDrawable, windowTranslucent); |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 2046 | } |
| 2047 | |
Nader Jawad | 56c68bc | 2018-04-24 17:08:51 -0700 | [diff] [blame] | 2048 | if (fallbackDrawable != null) { |
| 2049 | return enforceNonTranslucentBackground(fallbackDrawable, windowTranslucent); |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 2050 | } |
Jorim Jaggi | 8e879f1 | 2016-05-25 16:41:49 -0700 | [diff] [blame] | 2051 | return new ColorDrawable(Color.BLACK); |
| 2052 | } |
| 2053 | |
| 2054 | /** |
| 2055 | * Enforces a drawable to be non-translucent to act as a background if needed, i.e. if the |
| 2056 | * window is not translucent. |
| 2057 | */ |
| 2058 | private static Drawable enforceNonTranslucentBackground(Drawable drawable, |
| 2059 | boolean windowTranslucent) { |
| 2060 | if (!windowTranslucent && drawable instanceof ColorDrawable) { |
| 2061 | ColorDrawable colorDrawable = (ColorDrawable) drawable; |
| 2062 | int color = colorDrawable.getColor(); |
| 2063 | if (Color.alpha(color) != 255) { |
| 2064 | ColorDrawable copy = (ColorDrawable) colorDrawable.getConstantState().newDrawable() |
| 2065 | .mutate(); |
| 2066 | copy.setColor( |
| 2067 | Color.argb(255, Color.red(color), Color.green(color), Color.blue(color))); |
| 2068 | return copy; |
| 2069 | } |
| 2070 | } |
| 2071 | return drawable; |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 2072 | } |
| 2073 | |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 2074 | void clearContentView() { |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 2075 | if (mDecorCaptionView != null) { |
Filip Gruszczynski | 6325065 | 2015-11-18 14:43:01 -0800 | [diff] [blame] | 2076 | mDecorCaptionView.removeContentView(); |
Wale Ogunwale | 0d7e912 | 2015-11-17 10:45:06 -0800 | [diff] [blame] | 2077 | } else { |
Jorim Jaggi | 6e0ce28 | 2015-12-01 15:19:49 -0800 | [diff] [blame] | 2078 | // This window doesn't have caption, so we need to remove everything except our views |
| 2079 | // we might have added. |
| 2080 | for (int i = getChildCount() - 1; i >= 0; i--) { |
| 2081 | View v = getChildAt(i); |
| 2082 | if (v != mStatusColorViewState.view && v != mNavigationColorViewState.view |
Yohei Yukawa | 8f162c6 | 2018-01-10 13:18:09 -0800 | [diff] [blame] | 2083 | && v != mStatusGuard) { |
Jorim Jaggi | 6e0ce28 | 2015-12-01 15:19:49 -0800 | [diff] [blame] | 2084 | removeViewAt(i); |
| 2085 | } |
| 2086 | } |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 2087 | } |
| 2088 | } |
| 2089 | |
Wale Ogunwale | bf9eefc | 2015-11-17 14:47:52 -0800 | [diff] [blame] | 2090 | @Override |
Jorim Jaggi | 9511b0f | 2016-01-29 19:12:44 -0800 | [diff] [blame] | 2091 | public void onWindowSizeIsChanging(Rect newBounds, boolean fullscreen, Rect systemInsets, |
| 2092 | Rect stableInsets) { |
Wale Ogunwale | bf9eefc | 2015-11-17 14:47:52 -0800 | [diff] [blame] | 2093 | if (mBackdropFrameRenderer != null) { |
Jorim Jaggi | 9511b0f | 2016-01-29 19:12:44 -0800 | [diff] [blame] | 2094 | mBackdropFrameRenderer.setTargetRect(newBounds, fullscreen, systemInsets, stableInsets); |
Wale Ogunwale | bf9eefc | 2015-11-17 14:47:52 -0800 | [diff] [blame] | 2095 | } |
| 2096 | } |
| 2097 | |
| 2098 | @Override |
Jorim Jaggi | 9511b0f | 2016-01-29 19:12:44 -0800 | [diff] [blame] | 2099 | public void onWindowDragResizeStart(Rect initialBounds, boolean fullscreen, Rect systemInsets, |
Jorim Jaggi | c39c7b0 | 2016-03-24 10:47:07 -0700 | [diff] [blame] | 2100 | Rect stableInsets, int resizeMode) { |
Wale Ogunwale | bf9eefc | 2015-11-17 14:47:52 -0800 | [diff] [blame] | 2101 | if (mWindow.isDestroyed()) { |
| 2102 | // If the owner's window is gone, we should not be able to come here anymore. |
| 2103 | releaseThreadedRenderer(); |
| 2104 | return; |
| 2105 | } |
| 2106 | if (mBackdropFrameRenderer != null) { |
| 2107 | return; |
| 2108 | } |
Stan Iliev | 45faba5 | 2016-06-28 13:33:15 -0400 | [diff] [blame] | 2109 | final ThreadedRenderer renderer = getThreadedRenderer(); |
Wale Ogunwale | bf9eefc | 2015-11-17 14:47:52 -0800 | [diff] [blame] | 2110 | if (renderer != null) { |
Filip Gruszczynski | a40fd09 | 2016-01-07 16:38:11 -0800 | [diff] [blame] | 2111 | loadBackgroundDrawablesIfNeeded(); |
Wale Ogunwale | bf9eefc | 2015-11-17 14:47:52 -0800 | [diff] [blame] | 2112 | mBackdropFrameRenderer = new BackdropFrameRenderer(this, renderer, |
Jorim Jaggi | 04c2fbd | 2015-12-02 15:11:45 -0800 | [diff] [blame] | 2113 | initialBounds, mResizingBackgroundDrawable, mCaptionBackgroundDrawable, |
Jorim Jaggi | 9511b0f | 2016-01-29 19:12:44 -0800 | [diff] [blame] | 2114 | mUserCaptionBackgroundDrawable, getCurrentColor(mStatusColorViewState), |
| 2115 | getCurrentColor(mNavigationColorViewState), fullscreen, systemInsets, |
Garfield Tan | fbd8ea6 | 2018-10-16 17:09:49 -0700 | [diff] [blame] | 2116 | stableInsets); |
Wale Ogunwale | bf9eefc | 2015-11-17 14:47:52 -0800 | [diff] [blame] | 2117 | |
| 2118 | // Get rid of the shadow while we are resizing. Shadow drawing takes considerable time. |
| 2119 | // If we want to get the shadow shown while resizing, we would need to elevate a new |
| 2120 | // element which owns the caption and has the elevation. |
| 2121 | updateElevation(); |
Jorim Jaggi | 04c2fbd | 2015-12-02 15:11:45 -0800 | [diff] [blame] | 2122 | |
| 2123 | updateColorViews(null /* insets */, false); |
Wale Ogunwale | bf9eefc | 2015-11-17 14:47:52 -0800 | [diff] [blame] | 2124 | } |
Jorim Jaggi | c39c7b0 | 2016-03-24 10:47:07 -0700 | [diff] [blame] | 2125 | mResizeMode = resizeMode; |
| 2126 | getViewRootImpl().requestInvalidateRootRenderNode(); |
Wale Ogunwale | bf9eefc | 2015-11-17 14:47:52 -0800 | [diff] [blame] | 2127 | } |
| 2128 | |
| 2129 | @Override |
| 2130 | public void onWindowDragResizeEnd() { |
| 2131 | releaseThreadedRenderer(); |
Jorim Jaggi | 04c2fbd | 2015-12-02 15:11:45 -0800 | [diff] [blame] | 2132 | updateColorViews(null /* insets */, false); |
Jorim Jaggi | c39c7b0 | 2016-03-24 10:47:07 -0700 | [diff] [blame] | 2133 | mResizeMode = RESIZE_MODE_INVALID; |
| 2134 | getViewRootImpl().requestInvalidateRootRenderNode(); |
Wale Ogunwale | bf9eefc | 2015-11-17 14:47:52 -0800 | [diff] [blame] | 2135 | } |
| 2136 | |
| 2137 | @Override |
| 2138 | public boolean onContentDrawn(int offsetX, int offsetY, int sizeX, int sizeY) { |
| 2139 | if (mBackdropFrameRenderer == null) { |
| 2140 | return false; |
| 2141 | } |
| 2142 | return mBackdropFrameRenderer.onContentDrawn(offsetX, offsetY, sizeX, sizeY); |
| 2143 | } |
| 2144 | |
| 2145 | @Override |
| 2146 | public void onRequestDraw(boolean reportNextDraw) { |
| 2147 | if (mBackdropFrameRenderer != null) { |
| 2148 | mBackdropFrameRenderer.onRequestDraw(reportNextDraw); |
| 2149 | } else if (reportNextDraw) { |
| 2150 | // If render thread is gone, just report immediately. |
| 2151 | if (isAttachedToWindow()) { |
| 2152 | getViewRootImpl().reportDrawFinish(); |
| 2153 | } |
| 2154 | } |
| 2155 | } |
| 2156 | |
Jorim Jaggi | c39c7b0 | 2016-03-24 10:47:07 -0700 | [diff] [blame] | 2157 | @Override |
John Reck | 32f140aa6 | 2018-10-04 15:08:24 -0700 | [diff] [blame] | 2158 | public void onPostDraw(RecordingCanvas canvas) { |
Jorim Jaggi | c39c7b0 | 2016-03-24 10:47:07 -0700 | [diff] [blame] | 2159 | drawResizingShadowIfNeeded(canvas); |
| 2160 | } |
| 2161 | |
| 2162 | private void initResizingPaints() { |
| 2163 | final int startColor = mContext.getResources().getColor( |
| 2164 | R.color.resize_shadow_start_color, null); |
| 2165 | final int endColor = mContext.getResources().getColor( |
| 2166 | R.color.resize_shadow_end_color, null); |
| 2167 | final int middleColor = (startColor + endColor) / 2; |
| 2168 | mHorizontalResizeShadowPaint.setShader(new LinearGradient( |
| 2169 | 0, 0, 0, mResizeShadowSize, new int[] { startColor, middleColor, endColor }, |
| 2170 | new float[] { 0f, 0.3f, 1f }, Shader.TileMode.CLAMP)); |
| 2171 | mVerticalResizeShadowPaint.setShader(new LinearGradient( |
| 2172 | 0, 0, mResizeShadowSize, 0, new int[] { startColor, middleColor, endColor }, |
| 2173 | new float[] { 0f, 0.3f, 1f }, Shader.TileMode.CLAMP)); |
| 2174 | } |
| 2175 | |
John Reck | 32f140aa6 | 2018-10-04 15:08:24 -0700 | [diff] [blame] | 2176 | private void drawResizingShadowIfNeeded(RecordingCanvas canvas) { |
Jorim Jaggi | c39c7b0 | 2016-03-24 10:47:07 -0700 | [diff] [blame] | 2177 | if (mResizeMode != RESIZE_MODE_DOCKED_DIVIDER || mWindow.mIsFloating |
| 2178 | || mWindow.isTranslucent() |
Jorim Jaggi | 8e879f1 | 2016-05-25 16:41:49 -0700 | [diff] [blame] | 2179 | || mWindow.isShowingWallpaper()) { |
Jorim Jaggi | c39c7b0 | 2016-03-24 10:47:07 -0700 | [diff] [blame] | 2180 | return; |
| 2181 | } |
| 2182 | canvas.save(); |
| 2183 | canvas.translate(0, getHeight() - mFrameOffsets.bottom); |
| 2184 | canvas.drawRect(0, 0, getWidth(), mResizeShadowSize, mHorizontalResizeShadowPaint); |
| 2185 | canvas.restore(); |
| 2186 | canvas.save(); |
| 2187 | canvas.translate(getWidth() - mFrameOffsets.right, 0); |
| 2188 | canvas.drawRect(0, 0, mResizeShadowSize, getHeight(), mVerticalResizeShadowPaint); |
| 2189 | canvas.restore(); |
| 2190 | } |
| 2191 | |
Wale Ogunwale | bf9eefc | 2015-11-17 14:47:52 -0800 | [diff] [blame] | 2192 | /** Release the renderer thread which is usually done when the user stops resizing. */ |
| 2193 | private void releaseThreadedRenderer() { |
Chong Zhang | d3fd96c | 2016-02-08 18:25:24 -0800 | [diff] [blame] | 2194 | if (mResizingBackgroundDrawable != null && mLastBackgroundDrawableCb != null) { |
| 2195 | mResizingBackgroundDrawable.setCallback(mLastBackgroundDrawableCb); |
| 2196 | mLastBackgroundDrawableCb = null; |
| 2197 | } |
| 2198 | |
Wale Ogunwale | bf9eefc | 2015-11-17 14:47:52 -0800 | [diff] [blame] | 2199 | if (mBackdropFrameRenderer != null) { |
| 2200 | mBackdropFrameRenderer.releaseRenderer(); |
| 2201 | mBackdropFrameRenderer = null; |
| 2202 | // Bring the shadow back. |
| 2203 | updateElevation(); |
| 2204 | } |
| 2205 | } |
| 2206 | |
Jorim Jaggi | 04c2fbd | 2015-12-02 15:11:45 -0800 | [diff] [blame] | 2207 | private boolean isResizing() { |
| 2208 | return mBackdropFrameRenderer != null; |
| 2209 | } |
| 2210 | |
Wale Ogunwale | 2b547c3 | 2015-11-18 10:33:22 -0800 | [diff] [blame] | 2211 | /** |
| 2212 | * The elevation gets set for the first time and the framework needs to be informed that |
| 2213 | * the surface layer gets created with the shadow size in mind. |
| 2214 | */ |
| 2215 | private void initializeElevation() { |
| 2216 | // TODO(skuhne): Call setMaxElevation here accordingly after b/22668382 got fixed. |
| 2217 | mAllowUpdateElevation = false; |
| 2218 | updateElevation(); |
| 2219 | } |
| 2220 | |
Wale Ogunwale | bf9eefc | 2015-11-17 14:47:52 -0800 | [diff] [blame] | 2221 | private void updateElevation() { |
Wale Ogunwale | 2b547c3 | 2015-11-18 10:33:22 -0800 | [diff] [blame] | 2222 | float elevation = 0; |
| 2223 | final boolean wasAdjustedForStack = mElevationAdjustedForStack; |
| 2224 | // Do not use a shadow when we are in resizing mode (mBackdropFrameRenderer not null) |
| 2225 | // since the shadow is bound to the content size and not the target size. |
Wale Ogunwale | 3382ab1 | 2017-07-27 08:55:03 -0700 | [diff] [blame] | 2226 | final int windowingMode = |
| 2227 | getResources().getConfiguration().windowConfiguration.getWindowingMode(); |
| 2228 | if ((windowingMode == WINDOWING_MODE_FREEFORM) && !isResizing()) { |
Wale Ogunwale | 2b547c3 | 2015-11-18 10:33:22 -0800 | [diff] [blame] | 2229 | elevation = hasWindowFocus() ? |
| 2230 | DECOR_SHADOW_FOCUSED_HEIGHT_IN_DIP : DECOR_SHADOW_UNFOCUSED_HEIGHT_IN_DIP; |
Jaewan Kim | 880eff6 | 2016-04-27 10:36:35 +0900 | [diff] [blame] | 2231 | // Add a maximum shadow height value to the top level view. |
| 2232 | // Note that pinned stack doesn't have focus |
| 2233 | // so maximum shadow height adjustment isn't needed. |
Wale Ogunwale | 2b547c3 | 2015-11-18 10:33:22 -0800 | [diff] [blame] | 2234 | // TODO(skuhne): Remove this if clause once b/22668382 got fixed. |
Robert Carr | 232b5f8 | 2017-04-17 15:11:35 -0700 | [diff] [blame] | 2235 | if (!mAllowUpdateElevation) { |
Wale Ogunwale | 2b547c3 | 2015-11-18 10:33:22 -0800 | [diff] [blame] | 2236 | elevation = DECOR_SHADOW_FOCUSED_HEIGHT_IN_DIP; |
| 2237 | } |
| 2238 | // Convert the DP elevation into physical pixels. |
| 2239 | elevation = dipToPx(elevation); |
| 2240 | mElevationAdjustedForStack = true; |
Wale Ogunwale | 3382ab1 | 2017-07-27 08:55:03 -0700 | [diff] [blame] | 2241 | } else if (windowingMode == WINDOWING_MODE_PINNED) { |
Robert Carr | 32bcb10 | 2018-01-29 15:03:23 -0800 | [diff] [blame] | 2242 | elevation = dipToPx(PINNED_WINDOWING_MODE_ELEVATION_IN_DIP); |
Robert Carr | 232b5f8 | 2017-04-17 15:11:35 -0700 | [diff] [blame] | 2243 | mElevationAdjustedForStack = true; |
Wale Ogunwale | 2b547c3 | 2015-11-18 10:33:22 -0800 | [diff] [blame] | 2244 | } else { |
| 2245 | mElevationAdjustedForStack = false; |
| 2246 | } |
| 2247 | |
| 2248 | // Don't change the elevation if we didn't previously adjust it for the stack it was in |
| 2249 | // or it didn't change. |
| 2250 | if ((wasAdjustedForStack || mElevationAdjustedForStack) |
| 2251 | && getElevation() != elevation) { |
Garfield Tan | fbd8ea6 | 2018-10-16 17:09:49 -0700 | [diff] [blame] | 2252 | if (!isResizing()) { |
| 2253 | mWindow.setElevation(elevation); |
| 2254 | } else { |
| 2255 | // Just suppress the shadow when resizing, don't adjust surface insets because it'll |
| 2256 | // cause a flicker when drag resize for freeform window starts. #onContentDrawn() |
| 2257 | // will compensate the offset when passing to BackdropFrameRenderer. |
| 2258 | setElevation(elevation); |
| 2259 | } |
Wale Ogunwale | bf9eefc | 2015-11-17 14:47:52 -0800 | [diff] [blame] | 2260 | } |
| 2261 | } |
| 2262 | |
Wale Ogunwale | 8cc5a74 | 2015-11-17 15:41:05 -0800 | [diff] [blame] | 2263 | boolean isShowingCaption() { |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 2264 | return mDecorCaptionView != null && mDecorCaptionView.isCaptionShowing(); |
Wale Ogunwale | 8cc5a74 | 2015-11-17 15:41:05 -0800 | [diff] [blame] | 2265 | } |
| 2266 | |
| 2267 | int getCaptionHeight() { |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 2268 | return isShowingCaption() ? mDecorCaptionView.getCaptionHeight() : 0; |
Wale Ogunwale | 8cc5a74 | 2015-11-17 15:41:05 -0800 | [diff] [blame] | 2269 | } |
| 2270 | |
Wale Ogunwale | 2b547c3 | 2015-11-18 10:33:22 -0800 | [diff] [blame] | 2271 | /** |
| 2272 | * Converts a DIP measure into physical pixels. |
| 2273 | * @param dip The dip value. |
| 2274 | * @return Returns the number of pixels. |
| 2275 | */ |
| 2276 | private float dipToPx(float dip) { |
| 2277 | return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, |
| 2278 | getResources().getDisplayMetrics()); |
| 2279 | } |
| 2280 | |
Filip Gruszczynski | 3dec081 | 2015-12-09 08:42:41 -0800 | [diff] [blame] | 2281 | /** |
| 2282 | * Provide an override of the caption background drawable. |
| 2283 | */ |
| 2284 | void setUserCaptionBackgroundDrawable(Drawable drawable) { |
| 2285 | mUserCaptionBackgroundDrawable = drawable; |
| 2286 | if (mBackdropFrameRenderer != null) { |
| 2287 | mBackdropFrameRenderer.setUserCaptionBackgroundDrawable(drawable); |
| 2288 | } |
| 2289 | } |
| 2290 | |
Filip Gruszczynski | 1937a4c | 2016-01-19 16:17:13 -0800 | [diff] [blame] | 2291 | private static String getTitleSuffix(WindowManager.LayoutParams params) { |
| 2292 | if (params == null) { |
| 2293 | return ""; |
| 2294 | } |
Filip Gruszczynski | 34dab0b | 2015-12-22 08:29:07 -0800 | [diff] [blame] | 2295 | final String[] split = params.getTitle().toString().split("\\."); |
| 2296 | if (split.length > 0) { |
Filip Gruszczynski | 1937a4c | 2016-01-19 16:17:13 -0800 | [diff] [blame] | 2297 | return split[split.length - 1]; |
| 2298 | } else { |
| 2299 | return ""; |
Filip Gruszczynski | 34dab0b | 2015-12-22 08:29:07 -0800 | [diff] [blame] | 2300 | } |
| 2301 | } |
| 2302 | |
Filip Gruszczynski | 1937a4c | 2016-01-19 16:17:13 -0800 | [diff] [blame] | 2303 | void updateLogTag(WindowManager.LayoutParams params) { |
| 2304 | mLogTag = TAG + "[" + getTitleSuffix(params) + "]"; |
| 2305 | } |
| 2306 | |
Filip Gruszczynski | 34dab0b | 2015-12-22 08:29:07 -0800 | [diff] [blame] | 2307 | private void updateAvailableWidth() { |
| 2308 | Resources res = getResources(); |
| 2309 | mAvailableWidth = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, |
| 2310 | res.getConfiguration().screenWidthDp, res.getDisplayMetrics()); |
| 2311 | } |
| 2312 | |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 2313 | /** |
| 2314 | * @hide |
| 2315 | */ |
| 2316 | @Override |
Clara Bayarri | fcd7e80 | 2016-03-10 12:58:18 +0000 | [diff] [blame] | 2317 | public void requestKeyboardShortcuts(List<KeyboardShortcutGroup> list, int deviceId) { |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 2318 | final PanelFeatureState st = mWindow.getPanelState(FEATURE_OPTIONS_PANEL, false); |
Michael Wright | 936f27c | 2017-04-11 23:23:42 +0100 | [diff] [blame] | 2319 | final Menu menu = st != null ? st.menu : null; |
| 2320 | if (!mWindow.isDestroyed() && mWindow.getCallback() != null) { |
| 2321 | mWindow.getCallback().onProvideKeyboardShortcuts(list, menu, deviceId); |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 2322 | } |
| 2323 | } |
| 2324 | |
Filip Gruszczynski | 1937a4c | 2016-01-19 16:17:13 -0800 | [diff] [blame] | 2325 | @Override |
Vladislav Kaznacheev | 3787de1 | 2016-12-21 10:36:35 -0800 | [diff] [blame] | 2326 | public void dispatchPointerCaptureChanged(boolean hasCapture) { |
| 2327 | super.dispatchPointerCaptureChanged(hasCapture); |
| 2328 | if (!mWindow.isDestroyed() && mWindow.getCallback() != null) { |
| 2329 | mWindow.getCallback().onPointerCaptureChanged(hasCapture); |
| 2330 | } |
| 2331 | } |
| 2332 | |
| 2333 | @Override |
Phil Weaver | f00cd14 | 2017-03-03 13:44:00 -0800 | [diff] [blame] | 2334 | public int getAccessibilityViewId() { |
| 2335 | return AccessibilityNodeInfo.ROOT_ITEM_ID; |
| 2336 | } |
| 2337 | |
| 2338 | @Override |
Filip Gruszczynski | 1937a4c | 2016-01-19 16:17:13 -0800 | [diff] [blame] | 2339 | public String toString() { |
| 2340 | return "DecorView@" + Integer.toHexString(this.hashCode()) + "[" |
| 2341 | + getTitleSuffix(mWindow.getAttributes()) + "]"; |
| 2342 | } |
| 2343 | |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 2344 | private static class ColorViewState { |
| 2345 | View view = null; |
| 2346 | int targetVisibility = View.INVISIBLE; |
| 2347 | boolean present = false; |
Jorim Jaggi | 04c2fbd | 2015-12-02 15:11:45 -0800 | [diff] [blame] | 2348 | boolean visible; |
| 2349 | int color; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 2350 | |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 2351 | final ColorViewAttributes attributes; |
| 2352 | |
| 2353 | ColorViewState(ColorViewAttributes attributes) { |
| 2354 | this.attributes = attributes; |
| 2355 | } |
| 2356 | } |
| 2357 | |
| 2358 | public static class ColorViewAttributes { |
| 2359 | |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 2360 | final int id; |
| 2361 | final int systemUiHideFlag; |
| 2362 | final int translucentFlag; |
| 2363 | final int verticalGravity; |
| 2364 | final int horizontalGravity; |
Adrian Roos | 85d202b | 2016-06-02 16:27:47 -0700 | [diff] [blame] | 2365 | final int seascapeGravity; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 2366 | final String transitionName; |
| 2367 | final int hideWindowFlag; |
| 2368 | |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 2369 | private ColorViewAttributes(int systemUiHideFlag, int translucentFlag, int verticalGravity, |
| 2370 | int horizontalGravity, int seascapeGravity, String transitionName, int id, |
| 2371 | int hideWindowFlag) { |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 2372 | this.id = id; |
| 2373 | this.systemUiHideFlag = systemUiHideFlag; |
| 2374 | this.translucentFlag = translucentFlag; |
| 2375 | this.verticalGravity = verticalGravity; |
| 2376 | this.horizontalGravity = horizontalGravity; |
Adrian Roos | 85d202b | 2016-06-02 16:27:47 -0700 | [diff] [blame] | 2377 | this.seascapeGravity = seascapeGravity; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 2378 | this.transitionName = transitionName; |
| 2379 | this.hideWindowFlag = hideWindowFlag; |
| 2380 | } |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 2381 | |
| 2382 | public boolean isPresent(int sysUiVis, int windowFlags, boolean force) { |
| 2383 | return (sysUiVis & systemUiHideFlag) == 0 |
| 2384 | && (windowFlags & hideWindowFlag) == 0 |
| 2385 | && ((windowFlags & FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS) != 0 |
| 2386 | || force); |
| 2387 | } |
| 2388 | |
| 2389 | public boolean isVisible(boolean present, int color, int windowFlags, boolean force) { |
| 2390 | return present |
| 2391 | && (color & Color.BLACK) != 0 |
| 2392 | && ((windowFlags & translucentFlag) == 0 || force); |
| 2393 | } |
| 2394 | |
| 2395 | public boolean isVisible(int sysUiVis, int color, int windowFlags, boolean force) { |
| 2396 | final boolean present = isPresent(sysUiVis, windowFlags, force); |
| 2397 | return isVisible(present, color, windowFlags, force); |
| 2398 | } |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 2399 | } |
| 2400 | |
| 2401 | /** |
| 2402 | * Clears out internal references when the action mode is destroyed. |
| 2403 | */ |
| 2404 | private class ActionModeCallback2Wrapper extends ActionMode.Callback2 { |
| 2405 | private final ActionMode.Callback mWrapped; |
| 2406 | |
| 2407 | public ActionModeCallback2Wrapper(ActionMode.Callback wrapped) { |
| 2408 | mWrapped = wrapped; |
| 2409 | } |
| 2410 | |
| 2411 | public boolean onCreateActionMode(ActionMode mode, Menu menu) { |
| 2412 | return mWrapped.onCreateActionMode(mode, menu); |
| 2413 | } |
| 2414 | |
| 2415 | public boolean onPrepareActionMode(ActionMode mode, Menu menu) { |
| 2416 | requestFitSystemWindows(); |
| 2417 | return mWrapped.onPrepareActionMode(mode, menu); |
| 2418 | } |
| 2419 | |
| 2420 | public boolean onActionItemClicked(ActionMode mode, MenuItem item) { |
| 2421 | return mWrapped.onActionItemClicked(mode, item); |
| 2422 | } |
| 2423 | |
| 2424 | public void onDestroyActionMode(ActionMode mode) { |
| 2425 | mWrapped.onDestroyActionMode(mode); |
| 2426 | final boolean isMncApp = mContext.getApplicationInfo().targetSdkVersion |
Jorim Jaggi | 8f5701b | 2016-04-04 18:36:02 -0700 | [diff] [blame] | 2427 | >= M; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 2428 | final boolean isPrimary; |
| 2429 | final boolean isFloating; |
| 2430 | if (isMncApp) { |
| 2431 | isPrimary = mode == mPrimaryActionMode; |
| 2432 | isFloating = mode == mFloatingActionMode; |
| 2433 | if (!isPrimary && mode.getType() == ActionMode.TYPE_PRIMARY) { |
Filip Gruszczynski | 34dab0b | 2015-12-22 08:29:07 -0800 | [diff] [blame] | 2434 | Log.e(mLogTag, "Destroying unexpected ActionMode instance of TYPE_PRIMARY; " |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 2435 | + mode + " was not the current primary action mode! Expected " |
| 2436 | + mPrimaryActionMode); |
| 2437 | } |
| 2438 | if (!isFloating && mode.getType() == ActionMode.TYPE_FLOATING) { |
Filip Gruszczynski | 34dab0b | 2015-12-22 08:29:07 -0800 | [diff] [blame] | 2439 | Log.e(mLogTag, "Destroying unexpected ActionMode instance of TYPE_FLOATING; " |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 2440 | + mode + " was not the current floating action mode! Expected " |
| 2441 | + mFloatingActionMode); |
| 2442 | } |
| 2443 | } else { |
| 2444 | isPrimary = mode.getType() == ActionMode.TYPE_PRIMARY; |
| 2445 | isFloating = mode.getType() == ActionMode.TYPE_FLOATING; |
| 2446 | } |
| 2447 | if (isPrimary) { |
| 2448 | if (mPrimaryActionModePopup != null) { |
| 2449 | removeCallbacks(mShowPrimaryActionModePopup); |
| 2450 | } |
| 2451 | if (mPrimaryActionModeView != null) { |
| 2452 | endOnGoingFadeAnimation(); |
Andrii Kulian | 8d6ac26 | 2016-06-08 13:14:19 -0700 | [diff] [blame] | 2453 | // Store action mode view reference, so we can access it safely when animation |
| 2454 | // ends. mPrimaryActionModePopup is set together with mPrimaryActionModeView, |
| 2455 | // so no need to store reference to it in separate variable. |
| 2456 | final ActionBarContextView lastActionModeView = mPrimaryActionModeView; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 2457 | mFadeAnim = ObjectAnimator.ofFloat(mPrimaryActionModeView, View.ALPHA, |
| 2458 | 1f, 0f); |
| 2459 | mFadeAnim.addListener(new Animator.AnimatorListener() { |
Andrii Kulian | 8d6ac26 | 2016-06-08 13:14:19 -0700 | [diff] [blame] | 2460 | |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 2461 | @Override |
| 2462 | public void onAnimationStart(Animator animation) { |
| 2463 | |
| 2464 | } |
| 2465 | |
| 2466 | @Override |
| 2467 | public void onAnimationEnd(Animator animation) { |
Andrii Kulian | 8d6ac26 | 2016-06-08 13:14:19 -0700 | [diff] [blame] | 2468 | // If mPrimaryActionModeView has changed - it means that we've |
| 2469 | // cleared the content while preserving decor view. We don't |
| 2470 | // want to change the state of new instances accidentally here. |
| 2471 | if (lastActionModeView == mPrimaryActionModeView) { |
| 2472 | lastActionModeView.setVisibility(GONE); |
| 2473 | if (mPrimaryActionModePopup != null) { |
| 2474 | mPrimaryActionModePopup.dismiss(); |
| 2475 | } |
| 2476 | lastActionModeView.killMode(); |
| 2477 | mFadeAnim = null; |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 2478 | } |
Wale Ogunwale | 8804af2 | 2015-11-17 09:18:15 -0800 | [diff] [blame] | 2479 | } |
| 2480 | |
| 2481 | @Override |
| 2482 | public void onAnimationCancel(Animator animation) { |
| 2483 | |
| 2484 | } |
| 2485 | |
| 2486 | @Override |
| 2487 | public void onAnimationRepeat(Animator animation) { |
| 2488 | |
| 2489 | } |
| 2490 | }); |
| 2491 | mFadeAnim.start(); |
| 2492 | } |
| 2493 | |
| 2494 | mPrimaryActionMode = null; |
| 2495 | } else if (isFloating) { |
| 2496 | cleanupFloatingActionModeViews(); |
| 2497 | mFloatingActionMode = null; |
| 2498 | } |
| 2499 | if (mWindow.getCallback() != null && !mWindow.isDestroyed()) { |
| 2500 | try { |
| 2501 | mWindow.getCallback().onActionModeFinished(mode); |
| 2502 | } catch (AbstractMethodError ame) { |
| 2503 | // Older apps might not implement this callback method. |
| 2504 | } |
| 2505 | } |
| 2506 | requestFitSystemWindows(); |
| 2507 | } |
| 2508 | |
| 2509 | @Override |
| 2510 | public void onGetContentRect(ActionMode mode, View view, Rect outRect) { |
| 2511 | if (mWrapped instanceof ActionMode.Callback2) { |
| 2512 | ((ActionMode.Callback2) mWrapped).onGetContentRect(mode, view, outRect); |
| 2513 | } else { |
| 2514 | super.onGetContentRect(mode, view, outRect); |
| 2515 | } |
| 2516 | } |
| 2517 | } |
| 2518 | } |