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