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