blob: 27fe03cec97bcdc72e3291710453bc20049ec18f [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;
24import com.android.internal.view.menu.MenuDialogHelper;
25import com.android.internal.view.menu.MenuPopupHelper;
26import com.android.internal.widget.ActionBarContextView;
27import com.android.internal.widget.BackgroundFallback;
Wale Ogunwale62a91d62015-11-18 11:44:10 -080028import com.android.internal.widget.DecorCaptionView;
Wale Ogunwale8804af22015-11-17 09:18:15 -080029import com.android.internal.widget.FloatingToolbar;
30
31import android.animation.Animator;
32import android.animation.ObjectAnimator;
33import android.app.ActivityManager;
34import android.content.Context;
35import android.content.res.Resources;
36import android.graphics.Canvas;
37import android.graphics.Color;
38import android.graphics.PixelFormat;
39import android.graphics.Rect;
40import android.graphics.drawable.Drawable;
41import android.os.Build;
Wale Ogunwale0d7e9122015-11-17 10:45:06 -080042import android.os.RemoteException;
Wale Ogunwale8804af22015-11-17 09:18:15 -080043import android.util.DisplayMetrics;
44import android.util.Log;
45import android.util.TypedValue;
46import android.view.ActionMode;
47import android.view.ContextThemeWrapper;
48import android.view.Gravity;
49import android.view.InputQueue;
50import android.view.KeyEvent;
Wale Ogunwale0d7e9122015-11-17 10:45:06 -080051import android.view.LayoutInflater;
Wale Ogunwale8804af22015-11-17 09:18:15 -080052import android.view.Menu;
53import android.view.MenuItem;
54import android.view.MotionEvent;
Wale Ogunwalebf9eefc2015-11-17 14:47:52 -080055import android.view.ThreadedRenderer;
Wale Ogunwale8804af22015-11-17 09:18:15 -080056import android.view.View;
57import android.view.ViewGroup;
58import android.view.ViewStub;
59import android.view.ViewTreeObserver;
60import android.view.Window;
Wale Ogunwalebf9eefc2015-11-17 14:47:52 -080061import android.view.WindowCallbacks;
Wale Ogunwale8804af22015-11-17 09:18:15 -080062import android.view.WindowInsets;
63import android.view.WindowManager;
64import android.view.accessibility.AccessibilityEvent;
65import android.view.accessibility.AccessibilityManager;
66import android.view.animation.AnimationUtils;
67import android.view.animation.Interpolator;
68import android.widget.FrameLayout;
69import android.widget.PopupWindow;
70
Wale Ogunwale0d7e9122015-11-17 10:45:06 -080071import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
72import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
Wale Ogunwale8804af22015-11-17 09:18:15 -080073import static android.view.View.MeasureSpec.AT_MOST;
74import static android.view.View.MeasureSpec.EXACTLY;
75import static android.view.View.MeasureSpec.getMode;
76import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
77import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
78import static android.view.WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS;
79import static android.view.WindowManager.LayoutParams.FLAG_FULLSCREEN;
80import static android.view.WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;
81import static android.view.WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
Wale Ogunwale0d7e9122015-11-17 10:45:06 -080082import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
83import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
Wale Ogunwale8804af22015-11-17 09:18:15 -080084
Wale Ogunwalebf9eefc2015-11-17 14:47:52 -080085/** @hide */
86public class DecorView extends FrameLayout implements RootViewSurfaceTaker, WindowCallbacks {
Wale Ogunwale8804af22015-11-17 09:18:15 -080087 private static final String TAG = "DecorView";
88
89 private static final boolean SWEEP_OPEN_MENU = false;
90
Wale Ogunwale2b547c32015-11-18 10:33:22 -080091 // The height of a window which has focus in DIP.
92 private final static int DECOR_SHADOW_FOCUSED_HEIGHT_IN_DIP = 20;
93 // The height of a window which has not in DIP.
94 private final static int DECOR_SHADOW_UNFOCUSED_HEIGHT_IN_DIP = 5;
95
96 // Cludge to address b/22668382: Set the shadow size to the maximum so that the layer
97 // size calculation takes the shadow size into account. We set the elevation currently
98 // to max until the first layout command has been executed.
99 private boolean mAllowUpdateElevation = false;
100
101 private boolean mElevationAdjustedForStack = false;
102
Wale Ogunwale8804af22015-11-17 09:18:15 -0800103 int mDefaultOpacity = PixelFormat.OPAQUE;
104
105 /** The feature ID of the panel, or -1 if this is the application's DecorView */
106 private final int mFeatureId;
107
108 private final Rect mDrawingBounds = new Rect();
109
110 private final Rect mBackgroundPadding = new Rect();
111
112 private final Rect mFramePadding = new Rect();
113
114 private final Rect mFrameOffsets = new Rect();
115
Wale Ogunwale62a91d62015-11-18 11:44:10 -0800116 private boolean mHasCaption = false;
Wale Ogunwale8804af22015-11-17 09:18:15 -0800117
118 private boolean mChanging;
119
120 private Drawable mMenuBackground;
121 private boolean mWatchingForMenu;
122 private int mDownY;
123
124 ActionMode mPrimaryActionMode;
125 private ActionMode mFloatingActionMode;
126 private ActionBarContextView mPrimaryActionModeView;
127 private PopupWindow mPrimaryActionModePopup;
128 private Runnable mShowPrimaryActionModePopup;
129 private ViewTreeObserver.OnPreDrawListener mFloatingToolbarPreDrawListener;
130 private View mFloatingActionModeOriginatingView;
131 private FloatingToolbar mFloatingToolbar;
132 private ObjectAnimator mFadeAnim;
133
134 // View added at runtime to draw under the status bar area
135 private View mStatusGuard;
136 // View added at runtime to draw under the navigation bar area
137 private View mNavigationGuard;
138
139 private final ColorViewState mStatusColorViewState = new ColorViewState(
140 SYSTEM_UI_FLAG_FULLSCREEN, FLAG_TRANSLUCENT_STATUS,
141 Gravity.TOP, Gravity.LEFT,
142 Window.STATUS_BAR_BACKGROUND_TRANSITION_NAME,
143 com.android.internal.R.id.statusBarBackground,
144 FLAG_FULLSCREEN);
145 private final ColorViewState mNavigationColorViewState = new ColorViewState(
146 SYSTEM_UI_FLAG_HIDE_NAVIGATION, FLAG_TRANSLUCENT_NAVIGATION,
147 Gravity.BOTTOM, Gravity.RIGHT,
148 Window.NAVIGATION_BAR_BACKGROUND_TRANSITION_NAME,
149 com.android.internal.R.id.navigationBarBackground,
150 0 /* hideWindowFlag */);
151
152 private final Interpolator mShowInterpolator;
153 private final Interpolator mHideInterpolator;
154 private final int mBarEnterExitDuration;
155
156 private final BackgroundFallback mBackgroundFallback = new BackgroundFallback();
157
158 private int mLastTopInset = 0;
159 private int mLastBottomInset = 0;
160 private int mLastRightInset = 0;
161 private boolean mLastHasTopStableInset = false;
162 private boolean mLastHasBottomStableInset = false;
163 private boolean mLastHasRightStableInset = false;
164 private int mLastWindowFlags = 0;
165
166 private int mRootScrollY = 0;
167
168 private PhoneWindow mWindow;
169
170 ViewGroup mContentRoot;
171
172 private Rect mTempRect;
173 private Rect mOutsets = new Rect();
174
Wale Ogunwale62a91d62015-11-18 11:44:10 -0800175 // This is the caption view for the window, containing the caption and window control
Wale Ogunwale0d7e9122015-11-17 10:45:06 -0800176 // buttons. The visibility of this decor depends on the workspace and the window type.
177 // If the window type does not require such a view, this member might be null.
Wale Ogunwale62a91d62015-11-18 11:44:10 -0800178 DecorCaptionView mDecorCaptionView;
Wale Ogunwale0d7e9122015-11-17 10:45:06 -0800179
Wale Ogunwale62a91d62015-11-18 11:44:10 -0800180 // Stack window is currently in. Since querying and changing the stack is expensive,
181 // this is the stack value the window is currently set up for.
182 int mStackId;
Wale Ogunwale0d7e9122015-11-17 10:45:06 -0800183
Wale Ogunwalebf9eefc2015-11-17 14:47:52 -0800184 private boolean mWindowResizeCallbacksAdded = false;
185
Wale Ogunwale62a91d62015-11-18 11:44:10 -0800186 BackdropFrameRenderer mBackdropFrameRenderer = null;
Wale Ogunwalebf9eefc2015-11-17 14:47:52 -0800187 private Drawable mResizingBackgroundDrawable;
188 private Drawable mCaptionBackgroundDrawable;
189
Wale Ogunwale8804af22015-11-17 09:18:15 -0800190 DecorView(Context context, int featureId, PhoneWindow window) {
191 super(context);
192 mFeatureId = featureId;
193
194 mShowInterpolator = AnimationUtils.loadInterpolator(context,
195 android.R.interpolator.linear_out_slow_in);
196 mHideInterpolator = AnimationUtils.loadInterpolator(context,
197 android.R.interpolator.fast_out_linear_in);
198
199 mBarEnterExitDuration = context.getResources().getInteger(
200 R.integer.dock_enter_exit_duration);
201
202 setWindow(window);
203 }
204
Wale Ogunwale62a91d62015-11-18 11:44:10 -0800205 void setBackgroundFallback(int resId) {
Wale Ogunwale8804af22015-11-17 09:18:15 -0800206 mBackgroundFallback.setDrawable(resId != 0 ? getContext().getDrawable(resId) : null);
207 setWillNotDraw(getBackground() == null && !mBackgroundFallback.hasFallback());
208 }
209
210 @Override
211 public void onDraw(Canvas c) {
212 super.onDraw(c);
213 mBackgroundFallback.draw(mContentRoot, c, mWindow.mContentParent);
214 }
215
216 @Override
217 public boolean dispatchKeyEvent(KeyEvent event) {
218 final int keyCode = event.getKeyCode();
219 final int action = event.getAction();
220 final boolean isDown = action == KeyEvent.ACTION_DOWN;
221
222 if (isDown && (event.getRepeatCount() == 0)) {
223 // First handle chording of panel key: if a panel key is held
224 // but not released, try to execute a shortcut in it.
225 if ((mWindow.mPanelChordingKey > 0) && (mWindow.mPanelChordingKey != keyCode)) {
226 boolean handled = dispatchKeyShortcutEvent(event);
227 if (handled) {
228 return true;
229 }
230 }
231
232 // If a panel is open, perform a shortcut on it without the
233 // chorded panel key
234 if ((mWindow.mPreparedPanel != null) && mWindow.mPreparedPanel.isOpen) {
235 if (mWindow.performPanelShortcut(mWindow.mPreparedPanel, keyCode, event, 0)) {
236 return true;
237 }
238 }
239 }
240
241 if (!mWindow.isDestroyed()) {
242 final Window.Callback cb = mWindow.getCallback();
243 final boolean handled = cb != null && mFeatureId < 0 ? cb.dispatchKeyEvent(event)
244 : super.dispatchKeyEvent(event);
245 if (handled) {
246 return true;
247 }
248 }
249
250 return isDown ? mWindow.onKeyDown(mFeatureId, event.getKeyCode(), event)
251 : mWindow.onKeyUp(mFeatureId, event.getKeyCode(), event);
252 }
253
254 @Override
255 public boolean dispatchKeyShortcutEvent(KeyEvent ev) {
256 // If the panel is already prepared, then perform the shortcut using it.
257 boolean handled;
258 if (mWindow.mPreparedPanel != null) {
259 handled = mWindow.performPanelShortcut(mWindow.mPreparedPanel, ev.getKeyCode(), ev,
260 Menu.FLAG_PERFORM_NO_CLOSE);
261 if (handled) {
262 if (mWindow.mPreparedPanel != null) {
263 mWindow.mPreparedPanel.isHandled = true;
264 }
265 return true;
266 }
267 }
268
269 // Shortcut not handled by the panel. Dispatch to the view hierarchy.
270 final Window.Callback cb = mWindow.getCallback();
271 handled = cb != null && !mWindow.isDestroyed() && mFeatureId < 0
272 ? cb.dispatchKeyShortcutEvent(ev) : super.dispatchKeyShortcutEvent(ev);
273 if (handled) {
274 return true;
275 }
276
277 // If the panel is not prepared, then we may be trying to handle a shortcut key
278 // combination such as Control+C. Temporarily prepare the panel then mark it
279 // unprepared again when finished to ensure that the panel will again be prepared
280 // the next time it is shown for real.
281 PhoneWindow.PanelFeatureState st =
282 mWindow.getPanelState(Window.FEATURE_OPTIONS_PANEL, false);
283 if (st != null && mWindow.mPreparedPanel == null) {
284 mWindow.preparePanel(st, ev);
285 handled = mWindow.performPanelShortcut(st, ev.getKeyCode(), ev,
286 Menu.FLAG_PERFORM_NO_CLOSE);
287 st.isPrepared = false;
288 if (handled) {
289 return true;
290 }
291 }
292 return false;
293 }
294
295 @Override
296 public boolean dispatchTouchEvent(MotionEvent ev) {
297 final Window.Callback cb = mWindow.getCallback();
298 return cb != null && !mWindow.isDestroyed() && mFeatureId < 0
299 ? cb.dispatchTouchEvent(ev) : super.dispatchTouchEvent(ev);
300 }
301
302 @Override
303 public boolean dispatchTrackballEvent(MotionEvent ev) {
304 final Window.Callback cb = mWindow.getCallback();
305 return cb != null && !mWindow.isDestroyed() && mFeatureId < 0
306 ? cb.dispatchTrackballEvent(ev) : super.dispatchTrackballEvent(ev);
307 }
308
309 @Override
310 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
311 final Window.Callback cb = mWindow.getCallback();
312 return cb != null && !mWindow.isDestroyed() && mFeatureId < 0
313 ? cb.dispatchGenericMotionEvent(ev) : super.dispatchGenericMotionEvent(ev);
314 }
315
316 public boolean superDispatchKeyEvent(KeyEvent event) {
317 // Give priority to closing action modes if applicable.
318 if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
319 final int action = event.getAction();
320 // Back cancels action modes first.
321 if (mPrimaryActionMode != null) {
322 if (action == KeyEvent.ACTION_UP) {
323 mPrimaryActionMode.finish();
324 }
325 return true;
326 }
327 }
328
329 return super.dispatchKeyEvent(event);
330 }
331
332 public boolean superDispatchKeyShortcutEvent(KeyEvent event) {
333 return super.dispatchKeyShortcutEvent(event);
334 }
335
336 public boolean superDispatchTouchEvent(MotionEvent event) {
337 return super.dispatchTouchEvent(event);
338 }
339
340 public boolean superDispatchTrackballEvent(MotionEvent event) {
341 return super.dispatchTrackballEvent(event);
342 }
343
344 public boolean superDispatchGenericMotionEvent(MotionEvent event) {
345 return super.dispatchGenericMotionEvent(event);
346 }
347
348 @Override
349 public boolean onTouchEvent(MotionEvent event) {
350 return onInterceptTouchEvent(event);
351 }
352
353 private boolean isOutOfInnerBounds(int x, int y) {
354 return x < 0 || y < 0 || x > getWidth() || y > getHeight();
355 }
356
357 private boolean isOutOfBounds(int x, int y) {
358 return x < -5 || y < -5 || x > (getWidth() + 5)
359 || y > (getHeight() + 5);
360 }
361
362 @Override
363 public boolean onInterceptTouchEvent(MotionEvent event) {
364 int action = event.getAction();
Wale Ogunwale62a91d62015-11-18 11:44:10 -0800365 if (mHasCaption && isShowingCaption()) {
366 // Don't dispatch ACTION_DOWN to the captionr if the window is resizable and the event
367 // was (starting) outside the window. Window resizing events should be handled by
368 // WindowManager.
Wale Ogunwale8804af22015-11-17 09:18:15 -0800369 // TODO: Investigate how to handle the outside touch in window manager
370 // without generating these events.
371 // Currently we receive these because we need to enlarge the window's
372 // touch region so that the monitor channel receives the events
373 // in the outside touch area.
374 if (action == MotionEvent.ACTION_DOWN) {
375 final int x = (int) event.getX();
376 final int y = (int) event.getY();
377 if (isOutOfInnerBounds(x, y)) {
378 return true;
379 }
380 }
381 }
382
383 if (mFeatureId >= 0) {
384 if (action == MotionEvent.ACTION_DOWN) {
385 int x = (int)event.getX();
386 int y = (int)event.getY();
387 if (isOutOfBounds(x, y)) {
388 mWindow.closePanel(mFeatureId);
389 return true;
390 }
391 }
392 }
393
394 if (!SWEEP_OPEN_MENU) {
395 return false;
396 }
397
398 if (mFeatureId >= 0) {
399 if (action == MotionEvent.ACTION_DOWN) {
400 Log.i(TAG, "Watchiing!");
401 mWatchingForMenu = true;
402 mDownY = (int) event.getY();
403 return false;
404 }
405
406 if (!mWatchingForMenu) {
407 return false;
408 }
409
410 int y = (int)event.getY();
411 if (action == MotionEvent.ACTION_MOVE) {
412 if (y > (mDownY+30)) {
413 Log.i(TAG, "Closing!");
414 mWindow.closePanel(mFeatureId);
415 mWatchingForMenu = false;
416 return true;
417 }
418 } else if (action == MotionEvent.ACTION_UP) {
419 mWatchingForMenu = false;
420 }
421
422 return false;
423 }
424
425 //Log.i(TAG, "Intercept: action=" + action + " y=" + event.getY()
426 // + " (in " + getHeight() + ")");
427
428 if (action == MotionEvent.ACTION_DOWN) {
429 int y = (int)event.getY();
430 if (y >= (getHeight()-5) && !mWindow.hasChildren()) {
431 Log.i(TAG, "Watching!");
432 mWatchingForMenu = true;
433 }
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 < (getHeight()-30)) {
444 Log.i(TAG, "Opening!");
445 mWindow.openPanel(Window.FEATURE_OPTIONS_PANEL, new KeyEvent(
446 KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MENU));
447 mWatchingForMenu = false;
448 return true;
449 }
450 } else if (action == MotionEvent.ACTION_UP) {
451 mWatchingForMenu = false;
452 }
453
454 return false;
455 }
456
457 @Override
458 public void sendAccessibilityEvent(int eventType) {
459 if (!AccessibilityManager.getInstance(mContext).isEnabled()) {
460 return;
461 }
462
463 // if we are showing a feature that should be announced and one child
464 // make this child the event source since this is the feature itself
465 // otherwise the callback will take over and announce its client
466 if ((mFeatureId == Window.FEATURE_OPTIONS_PANEL ||
467 mFeatureId == Window.FEATURE_CONTEXT_MENU ||
468 mFeatureId == Window.FEATURE_PROGRESS ||
469 mFeatureId == Window.FEATURE_INDETERMINATE_PROGRESS)
470 && getChildCount() == 1) {
471 getChildAt(0).sendAccessibilityEvent(eventType);
472 } else {
473 super.sendAccessibilityEvent(eventType);
474 }
475 }
476
477 @Override
478 public boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
479 final Window.Callback cb = mWindow.getCallback();
480 if (cb != null && !mWindow.isDestroyed()) {
481 if (cb.dispatchPopulateAccessibilityEvent(event)) {
482 return true;
483 }
484 }
485 return super.dispatchPopulateAccessibilityEventInternal(event);
486 }
487
488 @Override
489 protected boolean setFrame(int l, int t, int r, int b) {
490 boolean changed = super.setFrame(l, t, r, b);
491 if (changed) {
492 final Rect drawingBounds = mDrawingBounds;
493 getDrawingRect(drawingBounds);
494
495 Drawable fg = getForeground();
496 if (fg != null) {
497 final Rect frameOffsets = mFrameOffsets;
498 drawingBounds.left += frameOffsets.left;
499 drawingBounds.top += frameOffsets.top;
500 drawingBounds.right -= frameOffsets.right;
501 drawingBounds.bottom -= frameOffsets.bottom;
502 fg.setBounds(drawingBounds);
503 final Rect framePadding = mFramePadding;
504 drawingBounds.left += framePadding.left - frameOffsets.left;
505 drawingBounds.top += framePadding.top - frameOffsets.top;
506 drawingBounds.right -= framePadding.right - frameOffsets.right;
507 drawingBounds.bottom -= framePadding.bottom - frameOffsets.bottom;
508 }
509
510 Drawable bg = getBackground();
511 if (bg != null) {
512 bg.setBounds(drawingBounds);
513 }
514
515 if (SWEEP_OPEN_MENU) {
516 if (mMenuBackground == null && mFeatureId < 0
517 && mWindow.getAttributes().height
518 == WindowManager.LayoutParams.MATCH_PARENT) {
519 mMenuBackground = getContext().getDrawable(
520 R.drawable.menu_background);
521 }
522 if (mMenuBackground != null) {
523 mMenuBackground.setBounds(drawingBounds.left,
524 drawingBounds.bottom-6, drawingBounds.right,
525 drawingBounds.bottom+20);
526 }
527 }
528 }
529 return changed;
530 }
531
532 @Override
533 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
534 final DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
535 final boolean isPortrait = metrics.widthPixels < metrics.heightPixels;
536
537 final int widthMode = getMode(widthMeasureSpec);
538 final int heightMode = getMode(heightMeasureSpec);
539
540 boolean fixedWidth = false;
541 if (widthMode == AT_MOST) {
542 final TypedValue tvw = isPortrait ? mWindow.mFixedWidthMinor
543 : mWindow.mFixedWidthMajor;
544 if (tvw != null && tvw.type != TypedValue.TYPE_NULL) {
545 final int w;
546 if (tvw.type == TypedValue.TYPE_DIMENSION) {
547 w = (int) tvw.getDimension(metrics);
548 } else if (tvw.type == TypedValue.TYPE_FRACTION) {
549 w = (int) tvw.getFraction(metrics.widthPixels, metrics.widthPixels);
550 } else {
551 w = 0;
552 }
553
554 if (w > 0) {
555 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
556 widthMeasureSpec = MeasureSpec.makeMeasureSpec(
557 Math.min(w, widthSize), EXACTLY);
558 fixedWidth = true;
559 }
560 }
561 }
562
563 if (heightMode == AT_MOST) {
564 final TypedValue tvh = isPortrait ? mWindow.mFixedHeightMajor
565 : mWindow.mFixedHeightMinor;
566 if (tvh != null && tvh.type != TypedValue.TYPE_NULL) {
567 final int h;
568 if (tvh.type == TypedValue.TYPE_DIMENSION) {
569 h = (int) tvh.getDimension(metrics);
570 } else if (tvh.type == TypedValue.TYPE_FRACTION) {
571 h = (int) tvh.getFraction(metrics.heightPixels, metrics.heightPixels);
572 } else {
573 h = 0;
574 }
575 if (h > 0) {
576 final int heightSize = MeasureSpec.getSize(heightMeasureSpec);
577 heightMeasureSpec = MeasureSpec.makeMeasureSpec(
578 Math.min(h, heightSize), EXACTLY);
579 }
580 }
581 }
582
583 getOutsets(mOutsets);
584 if (mOutsets.top > 0 || mOutsets.bottom > 0) {
585 int mode = MeasureSpec.getMode(heightMeasureSpec);
586 if (mode != MeasureSpec.UNSPECIFIED) {
587 int height = MeasureSpec.getSize(heightMeasureSpec);
588 heightMeasureSpec = MeasureSpec.makeMeasureSpec(
589 height + mOutsets.top + mOutsets.bottom, mode);
590 }
591 }
592 if (mOutsets.left > 0 || mOutsets.right > 0) {
593 int mode = MeasureSpec.getMode(widthMeasureSpec);
594 if (mode != MeasureSpec.UNSPECIFIED) {
595 int width = MeasureSpec.getSize(widthMeasureSpec);
596 widthMeasureSpec = MeasureSpec.makeMeasureSpec(
597 width + mOutsets.left + mOutsets.right, mode);
598 }
599 }
600
601 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
602
603 int width = getMeasuredWidth();
604 boolean measure = false;
605
606 widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, EXACTLY);
607
608 if (!fixedWidth && widthMode == AT_MOST) {
609 final TypedValue tv = isPortrait ? mWindow.mMinWidthMinor : mWindow.mMinWidthMajor;
610 if (tv.type != TypedValue.TYPE_NULL) {
611 final int min;
612 if (tv.type == TypedValue.TYPE_DIMENSION) {
613 min = (int)tv.getDimension(metrics);
614 } else if (tv.type == TypedValue.TYPE_FRACTION) {
615 min = (int)tv.getFraction(metrics.widthPixels, metrics.widthPixels);
616 } else {
617 min = 0;
618 }
619
620 if (width < min) {
621 widthMeasureSpec = MeasureSpec.makeMeasureSpec(min, EXACTLY);
622 measure = true;
623 }
624 }
625 }
626
627 // TODO: Support height?
628
629 if (measure) {
630 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
631 }
632 }
633
634 @Override
635 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
636 super.onLayout(changed, left, top, right, bottom);
637 getOutsets(mOutsets);
638 if (mOutsets.left > 0) {
639 offsetLeftAndRight(-mOutsets.left);
640 }
641 if (mOutsets.top > 0) {
642 offsetTopAndBottom(-mOutsets.top);
643 }
Wale Ogunwale2b547c32015-11-18 10:33:22 -0800644
645 // If the application changed its SystemUI metrics, we might also have to adapt
646 // our shadow elevation.
647 updateElevation();
648 mAllowUpdateElevation = true;
Wale Ogunwale8804af22015-11-17 09:18:15 -0800649 }
650
651 @Override
652 public void draw(Canvas canvas) {
653 super.draw(canvas);
654
655 if (mMenuBackground != null) {
656 mMenuBackground.draw(canvas);
657 }
658 }
659
660 @Override
661 public boolean showContextMenuForChild(View originalView) {
662 // Reuse the context menu builder
663 if (mWindow.mContextMenu == null) {
664 mWindow.mContextMenu = new ContextMenuBuilder(getContext());
665 mWindow.mContextMenu.setCallback(mWindow.mContextMenuCallback);
666 } else {
667 mWindow.mContextMenu.clearAll();
668 }
669
670 final MenuDialogHelper helper = mWindow.mContextMenu.show(originalView,
671 originalView.getWindowToken());
672 if (helper != null) {
673 helper.setPresenterCallback(mWindow.mContextMenuCallback);
674 } else if (mWindow.mContextMenuHelper != null) {
675 // No menu to show, but if we have a menu currently showing it just became blank.
676 // Close it.
677 mWindow.mContextMenuHelper.dismiss();
678 }
679 mWindow.mContextMenuHelper = helper;
680 return helper != null;
681 }
682
683 @Override
684 public boolean showContextMenuForChild(View originalView, float x, float y) {
685 // Reuse the context menu builder
686 if (mWindow.mContextMenu == null) {
687 mWindow.mContextMenu = new ContextMenuBuilder(getContext());
688 mWindow.mContextMenu.setCallback(mWindow.mContextMenuCallback);
689 } else {
690 mWindow.mContextMenu.clearAll();
691 }
692
693 final MenuPopupHelper helper = mWindow.mContextMenu.showPopup(
694 getContext(), originalView, x, y);
695 if (helper != null) {
696 helper.setCallback(mWindow.mContextMenuCallback);
697 } else if (mWindow.mContextMenuPopupHelper != null) {
698 // No menu to show, but if we have a menu currently showing it just became blank.
699 // Close it.
700 mWindow.mContextMenuPopupHelper.dismiss();
701 }
702 mWindow.mContextMenuPopupHelper = helper;
703 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,
893 0 /* rightInset */, animate && !disallowAnimate);
894
895 boolean statusBarNeedsRightInset = navBarToRightEdge
896 && mNavigationColorViewState.present;
897 int statusBarRightInset = statusBarNeedsRightInset ? mLastRightInset : 0;
898 updateColorViewInt(mStatusColorViewState, sysUiVisibility, mWindow.mStatusBarColor,
899 mLastTopInset, false /* matchVertical */, statusBarRightInset,
900 animate && !disallowAnimate);
901 }
902
903 // When we expand the window with FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS, we still need
904 // to ensure that the rest of the view hierarchy doesn't notice it, unless they've
905 // explicitly asked for it.
906
907 boolean consumingNavBar =
908 (attrs.flags & FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS) != 0
909 && (sysUiVisibility & SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION) == 0
910 && (sysUiVisibility & SYSTEM_UI_FLAG_HIDE_NAVIGATION) == 0;
911
912 int consumedRight = consumingNavBar ? mLastRightInset : 0;
913 int consumedBottom = consumingNavBar ? mLastBottomInset : 0;
914
915 if (mContentRoot != null
916 && mContentRoot.getLayoutParams() instanceof MarginLayoutParams) {
917 MarginLayoutParams lp = (MarginLayoutParams) mContentRoot.getLayoutParams();
918 if (lp.rightMargin != consumedRight || lp.bottomMargin != consumedBottom) {
919 lp.rightMargin = consumedRight;
920 lp.bottomMargin = consumedBottom;
921 mContentRoot.setLayoutParams(lp);
922
923 if (insets == null) {
924 // The insets have changed, but we're not currently in the process
925 // of dispatching them.
926 requestApplyInsets();
927 }
928 }
929 if (insets != null) {
930 insets = insets.replaceSystemWindowInsets(
931 insets.getSystemWindowInsetLeft(),
932 insets.getSystemWindowInsetTop(),
933 insets.getSystemWindowInsetRight() - consumedRight,
934 insets.getSystemWindowInsetBottom() - consumedBottom);
935 }
936 }
937
938 if (insets != null) {
939 insets = insets.consumeStableInsets();
940 }
941 return insets;
942 }
943
944 /**
945 * Update a color view
946 *
947 * @param state the color view to update.
948 * @param sysUiVis the current systemUiVisibility to apply.
949 * @param color the current color to apply.
950 * @param size the current size in the non-parent-matching dimension.
951 * @param verticalBar if true the view is attached to a vertical edge, otherwise to a
952 * horizontal edge,
953 * @param rightMargin rightMargin for the color view.
954 * @param animate if true, the change will be animated.
955 */
956 private void updateColorViewInt(final ColorViewState state, int sysUiVis, int color,
957 int size, boolean verticalBar, int rightMargin, boolean animate) {
958 state.present = size > 0 && (sysUiVis & state.systemUiHideFlag) == 0
959 && (mWindow.getAttributes().flags & state.hideWindowFlag) == 0
960 && (mWindow.getAttributes().flags & FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS) != 0;
961 boolean show = state.present
962 && (color & Color.BLACK) != 0
963 && (mWindow.getAttributes().flags & state.translucentFlag) == 0;
964
965 boolean visibilityChanged = false;
966 View view = state.view;
967
968 int resolvedHeight = verticalBar ? LayoutParams.MATCH_PARENT : size;
969 int resolvedWidth = verticalBar ? size : LayoutParams.MATCH_PARENT;
970 int resolvedGravity = verticalBar ? state.horizontalGravity : state.verticalGravity;
971
972 if (view == null) {
973 if (show) {
974 state.view = view = new View(mContext);
975 view.setBackgroundColor(color);
976 view.setTransitionName(state.transitionName);
977 view.setId(state.id);
978 visibilityChanged = true;
979 view.setVisibility(INVISIBLE);
980 state.targetVisibility = VISIBLE;
981
982 LayoutParams lp = new LayoutParams(resolvedWidth, resolvedHeight,
983 resolvedGravity);
984 lp.rightMargin = rightMargin;
985 addView(view, lp);
986 updateColorViewTranslations();
987 }
988 } else {
989 int vis = show ? VISIBLE : INVISIBLE;
990 visibilityChanged = state.targetVisibility != vis;
991 state.targetVisibility = vis;
992 LayoutParams lp = (LayoutParams) view.getLayoutParams();
993 if (lp.height != resolvedHeight || lp.width != resolvedWidth
994 || lp.gravity != resolvedGravity || lp.rightMargin != rightMargin) {
995 lp.height = resolvedHeight;
996 lp.width = resolvedWidth;
997 lp.gravity = resolvedGravity;
998 lp.rightMargin = rightMargin;
999 view.setLayoutParams(lp);
1000 }
1001 if (show) {
1002 view.setBackgroundColor(color);
1003 }
1004 }
1005 if (visibilityChanged) {
1006 view.animate().cancel();
1007 if (animate) {
1008 if (show) {
1009 if (view.getVisibility() != VISIBLE) {
1010 view.setVisibility(VISIBLE);
1011 view.setAlpha(0.0f);
1012 }
1013 view.animate().alpha(1.0f).setInterpolator(mShowInterpolator).
1014 setDuration(mBarEnterExitDuration);
1015 } else {
1016 view.animate().alpha(0.0f).setInterpolator(mHideInterpolator)
1017 .setDuration(mBarEnterExitDuration)
1018 .withEndAction(new Runnable() {
1019 @Override
1020 public void run() {
1021 state.view.setAlpha(1.0f);
1022 state.view.setVisibility(INVISIBLE);
1023 }
1024 });
1025 }
1026 } else {
1027 view.setAlpha(1.0f);
1028 view.setVisibility(show ? VISIBLE : INVISIBLE);
1029 }
1030 }
1031 }
1032
1033 private void updateColorViewTranslations() {
1034 // Put the color views back in place when they get moved off the screen
1035 // due to the the ViewRootImpl panning.
1036 int rootScrollY = mRootScrollY;
1037 if (mStatusColorViewState.view != null) {
1038 mStatusColorViewState.view.setTranslationY(rootScrollY > 0 ? rootScrollY : 0);
1039 }
1040 if (mNavigationColorViewState.view != null) {
1041 mNavigationColorViewState.view.setTranslationY(rootScrollY < 0 ? rootScrollY : 0);
1042 }
1043 }
1044
1045 private WindowInsets updateStatusGuard(WindowInsets insets) {
1046 boolean showStatusGuard = false;
1047 // Show the status guard when the non-overlay contextual action bar is showing
1048 if (mPrimaryActionModeView != null) {
1049 if (mPrimaryActionModeView.getLayoutParams() instanceof MarginLayoutParams) {
1050 // Insets are magic!
1051 final MarginLayoutParams mlp = (MarginLayoutParams)
1052 mPrimaryActionModeView.getLayoutParams();
1053 boolean mlpChanged = false;
1054 if (mPrimaryActionModeView.isShown()) {
1055 if (mTempRect == null) {
1056 mTempRect = new Rect();
1057 }
1058 final Rect rect = mTempRect;
1059
1060 // If the parent doesn't consume the insets, manually
1061 // apply the default system window insets.
1062 mWindow.mContentParent.computeSystemWindowInsets(insets, rect);
1063 final int newMargin = rect.top == 0 ? insets.getSystemWindowInsetTop() : 0;
1064 if (mlp.topMargin != newMargin) {
1065 mlpChanged = true;
1066 mlp.topMargin = insets.getSystemWindowInsetTop();
1067
1068 if (mStatusGuard == null) {
1069 mStatusGuard = new View(mContext);
1070 mStatusGuard.setBackgroundColor(mContext.getColor(
1071 R.color.input_method_navigation_guard));
1072 addView(mStatusGuard, indexOfChild(mStatusColorViewState.view),
1073 new LayoutParams(LayoutParams.MATCH_PARENT,
1074 mlp.topMargin, Gravity.START | Gravity.TOP));
1075 } else {
1076 final LayoutParams lp = (LayoutParams)
1077 mStatusGuard.getLayoutParams();
1078 if (lp.height != mlp.topMargin) {
1079 lp.height = mlp.topMargin;
1080 mStatusGuard.setLayoutParams(lp);
1081 }
1082 }
1083 }
1084
1085 // The action mode's theme may differ from the app, so
1086 // always show the status guard above it if we have one.
1087 showStatusGuard = mStatusGuard != null;
1088
1089 // We only need to consume the insets if the action
1090 // mode is overlaid on the app content (e.g. it's
1091 // sitting in a FrameLayout, see
1092 // screen_simple_overlay_action_mode.xml).
1093 final boolean nonOverlay = (mWindow.getLocalFeaturesPrivate()
1094 & (1 << Window.FEATURE_ACTION_MODE_OVERLAY)) == 0;
1095 insets = insets.consumeSystemWindowInsets(
1096 false, nonOverlay && showStatusGuard /* top */, false, false);
1097 } else {
1098 // reset top margin
1099 if (mlp.topMargin != 0) {
1100 mlpChanged = true;
1101 mlp.topMargin = 0;
1102 }
1103 }
1104 if (mlpChanged) {
1105 mPrimaryActionModeView.setLayoutParams(mlp);
1106 }
1107 }
1108 }
1109 if (mStatusGuard != null) {
1110 mStatusGuard.setVisibility(showStatusGuard ? View.VISIBLE : View.GONE);
1111 }
1112 return insets;
1113 }
1114
1115 private void updateNavigationGuard(WindowInsets insets) {
1116 // IMEs lay out below the nav bar, but the content view must not (for back compat)
1117 if (mWindow.getAttributes().type == WindowManager.LayoutParams.TYPE_INPUT_METHOD) {
1118 // prevent the content view from including the nav bar height
1119 if (mWindow.mContentParent != null) {
1120 if (mWindow.mContentParent.getLayoutParams() instanceof MarginLayoutParams) {
1121 MarginLayoutParams mlp =
1122 (MarginLayoutParams) mWindow.mContentParent.getLayoutParams();
1123 mlp.bottomMargin = insets.getSystemWindowInsetBottom();
1124 mWindow.mContentParent.setLayoutParams(mlp);
1125 }
1126 }
1127 // position the navigation guard view, creating it if necessary
1128 if (mNavigationGuard == null) {
1129 mNavigationGuard = new View(mContext);
1130 mNavigationGuard.setBackgroundColor(mContext.getColor(
1131 R.color.input_method_navigation_guard));
1132 addView(mNavigationGuard, indexOfChild(mNavigationColorViewState.view),
1133 new LayoutParams(LayoutParams.MATCH_PARENT,
1134 insets.getSystemWindowInsetBottom(),
1135 Gravity.START | Gravity.BOTTOM));
1136 } else {
1137 LayoutParams lp = (LayoutParams) mNavigationGuard.getLayoutParams();
1138 lp.height = insets.getSystemWindowInsetBottom();
1139 mNavigationGuard.setLayoutParams(lp);
1140 }
1141 }
1142 }
1143
1144 private void drawableChanged() {
1145 if (mChanging) {
1146 return;
1147 }
1148
1149 setPadding(mFramePadding.left + mBackgroundPadding.left,
1150 mFramePadding.top + mBackgroundPadding.top,
1151 mFramePadding.right + mBackgroundPadding.right,
1152 mFramePadding.bottom + mBackgroundPadding.bottom);
1153 requestLayout();
1154 invalidate();
1155
1156 int opacity = PixelFormat.OPAQUE;
Wale Ogunwale62a91d62015-11-18 11:44:10 -08001157 if (ActivityManager.StackId.hasWindowShadow(mStackId)) {
Wale Ogunwale8804af22015-11-17 09:18:15 -08001158 // If the window has a shadow, it must be translucent.
1159 opacity = PixelFormat.TRANSLUCENT;
1160 } else{
1161 // Note: If there is no background, we will assume opaque. The
1162 // common case seems to be that an application sets there to be
1163 // no background so it can draw everything itself. For that,
1164 // we would like to assume OPAQUE and let the app force it to
1165 // the slower TRANSLUCENT mode if that is really what it wants.
1166 Drawable bg = getBackground();
1167 Drawable fg = getForeground();
1168 if (bg != null) {
1169 if (fg == null) {
1170 opacity = bg.getOpacity();
1171 } else if (mFramePadding.left <= 0 && mFramePadding.top <= 0
1172 && mFramePadding.right <= 0 && mFramePadding.bottom <= 0) {
1173 // If the frame padding is zero, then we can be opaque
1174 // if either the frame -or- the background is opaque.
1175 int fop = fg.getOpacity();
1176 int bop = bg.getOpacity();
1177 if (false)
1178 Log.v(TAG, "Background opacity: " + bop + ", Frame opacity: " + fop);
1179 if (fop == PixelFormat.OPAQUE || bop == PixelFormat.OPAQUE) {
1180 opacity = PixelFormat.OPAQUE;
1181 } else if (fop == PixelFormat.UNKNOWN) {
1182 opacity = bop;
1183 } else if (bop == PixelFormat.UNKNOWN) {
1184 opacity = fop;
1185 } else {
1186 opacity = Drawable.resolveOpacity(fop, bop);
1187 }
1188 } else {
1189 // For now we have to assume translucent if there is a
1190 // frame with padding... there is no way to tell if the
1191 // frame and background together will draw all pixels.
1192 if (false)
1193 Log.v(TAG, "Padding: " + mFramePadding);
1194 opacity = PixelFormat.TRANSLUCENT;
1195 }
1196 }
1197 if (false)
1198 Log.v(TAG, "Background: " + bg + ", Frame: " + fg);
1199 }
1200
1201 if (false)
1202 Log.v(TAG, "Selected default opacity: " + opacity);
1203
1204 mDefaultOpacity = opacity;
1205 if (mFeatureId < 0) {
1206 mWindow.setDefaultWindowFormat(opacity);
1207 }
1208 }
1209
1210 @Override
1211 public void onWindowFocusChanged(boolean hasWindowFocus) {
1212 super.onWindowFocusChanged(hasWindowFocus);
1213
1214 // If the user is chording a menu shortcut, release the chord since
1215 // this window lost focus
1216 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL) && !hasWindowFocus
1217 && mWindow.mPanelChordingKey != 0) {
1218 mWindow.closePanel(Window.FEATURE_OPTIONS_PANEL);
1219 }
1220
1221 final Window.Callback cb = mWindow.getCallback();
1222 if (cb != null && !mWindow.isDestroyed() && mFeatureId < 0) {
1223 cb.onWindowFocusChanged(hasWindowFocus);
1224 }
1225
1226 if (mPrimaryActionMode != null) {
1227 mPrimaryActionMode.onWindowFocusChanged(hasWindowFocus);
1228 }
1229 if (mFloatingActionMode != null) {
1230 mFloatingActionMode.onWindowFocusChanged(hasWindowFocus);
1231 }
Wale Ogunwale2b547c32015-11-18 10:33:22 -08001232
1233 updateElevation();
Wale Ogunwale8804af22015-11-17 09:18:15 -08001234 }
1235
1236 @Override
1237 protected void onAttachedToWindow() {
1238 super.onAttachedToWindow();
1239
1240 final Window.Callback cb = mWindow.getCallback();
1241 if (cb != null && !mWindow.isDestroyed() && mFeatureId < 0) {
1242 cb.onAttachedToWindow();
1243 }
1244
1245 if (mFeatureId == -1) {
1246 /*
1247 * The main window has been attached, try to restore any panels
1248 * that may have been open before. This is called in cases where
1249 * an activity is being killed for configuration change and the
1250 * menu was open. When the activity is recreated, the menu
1251 * should be shown again.
1252 */
1253 mWindow.openPanelsAfterRestore();
1254 }
Wale Ogunwalebf9eefc2015-11-17 14:47:52 -08001255
1256 if (!mWindowResizeCallbacksAdded) {
1257 // If there is no window callback installed there was no window set before. Set it now.
1258 // Note that our ViewRootImpl object will not change.
1259 getViewRootImpl().addWindowCallbacks(this);
1260 mWindowResizeCallbacksAdded = true;
1261 } else if (mBackdropFrameRenderer != null) {
1262 // We are resizing and this call happened due to a configuration change. Tell the
1263 // renderer about it.
1264 mBackdropFrameRenderer.onConfigurationChange();
1265 }
Wale Ogunwale8804af22015-11-17 09:18:15 -08001266 }
1267
1268 @Override
1269 protected void onDetachedFromWindow() {
1270 super.onDetachedFromWindow();
1271
1272 final Window.Callback cb = mWindow.getCallback();
1273 if (cb != null && mFeatureId < 0) {
1274 cb.onDetachedFromWindow();
1275 }
1276
1277 if (mWindow.mDecorContentParent != null) {
1278 mWindow.mDecorContentParent.dismissPopups();
1279 }
1280
1281 if (mPrimaryActionModePopup != null) {
1282 removeCallbacks(mShowPrimaryActionModePopup);
1283 if (mPrimaryActionModePopup.isShowing()) {
1284 mPrimaryActionModePopup.dismiss();
1285 }
1286 mPrimaryActionModePopup = null;
1287 }
1288 if (mFloatingToolbar != null) {
1289 mFloatingToolbar.dismiss();
1290 mFloatingToolbar = null;
1291 }
1292
1293 PhoneWindow.PanelFeatureState st = mWindow.getPanelState(Window.FEATURE_OPTIONS_PANEL, false);
1294 if (st != null && st.menu != null && mFeatureId < 0) {
1295 st.menu.close();
1296 }
Wale Ogunwalebf9eefc2015-11-17 14:47:52 -08001297
1298 if (mWindowResizeCallbacksAdded) {
1299 getViewRootImpl().removeWindowCallbacks(this);
1300 mWindowResizeCallbacksAdded = false;
1301 }
Wale Ogunwale8804af22015-11-17 09:18:15 -08001302 }
1303
1304 @Override
1305 public void onCloseSystemDialogs(String reason) {
1306 if (mFeatureId >= 0) {
1307 mWindow.closeAllPanels();
1308 }
1309 }
1310
1311 public android.view.SurfaceHolder.Callback2 willYouTakeTheSurface() {
1312 return mFeatureId < 0 ? mWindow.mTakeSurfaceCallback : null;
1313 }
1314
1315 public InputQueue.Callback willYouTakeTheInputQueue() {
1316 return mFeatureId < 0 ? mWindow.mTakeInputQueueCallback : null;
1317 }
1318
1319 public void setSurfaceType(int type) {
1320 mWindow.setType(type);
1321 }
1322
1323 public void setSurfaceFormat(int format) {
1324 mWindow.setFormat(format);
1325 }
1326
1327 public void setSurfaceKeepScreenOn(boolean keepOn) {
1328 if (keepOn) mWindow.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
1329 else mWindow.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
1330 }
1331
1332 @Override
1333 public void onRootViewScrollYChanged(int rootScrollY) {
1334 mRootScrollY = rootScrollY;
1335 updateColorViewTranslations();
1336 }
1337
1338 private ActionMode createActionMode(
1339 int type, ActionMode.Callback2 callback, View originatingView) {
1340 switch (type) {
1341 case ActionMode.TYPE_PRIMARY:
1342 default:
1343 return createStandaloneActionMode(callback);
1344 case ActionMode.TYPE_FLOATING:
1345 return createFloatingActionMode(originatingView, callback);
1346 }
1347 }
1348
1349 private void setHandledActionMode(ActionMode mode) {
1350 if (mode.getType() == ActionMode.TYPE_PRIMARY) {
1351 setHandledPrimaryActionMode(mode);
1352 } else if (mode.getType() == ActionMode.TYPE_FLOATING) {
1353 setHandledFloatingActionMode(mode);
1354 }
1355 }
1356
1357 private ActionMode createStandaloneActionMode(ActionMode.Callback callback) {
1358 endOnGoingFadeAnimation();
1359 cleanupPrimaryActionMode();
1360 if (mPrimaryActionModeView == null) {
1361 if (mWindow.isFloating()) {
1362 // Use the action bar theme.
1363 final TypedValue outValue = new TypedValue();
1364 final Resources.Theme baseTheme = mContext.getTheme();
1365 baseTheme.resolveAttribute(R.attr.actionBarTheme, outValue, true);
1366
1367 final Context actionBarContext;
1368 if (outValue.resourceId != 0) {
1369 final Resources.Theme actionBarTheme = mContext.getResources().newTheme();
1370 actionBarTheme.setTo(baseTheme);
1371 actionBarTheme.applyStyle(outValue.resourceId, true);
1372
1373 actionBarContext = new ContextThemeWrapper(mContext, 0);
1374 actionBarContext.getTheme().setTo(actionBarTheme);
1375 } else {
1376 actionBarContext = mContext;
1377 }
1378
1379 mPrimaryActionModeView = new ActionBarContextView(actionBarContext);
1380 mPrimaryActionModePopup = new PopupWindow(actionBarContext, null,
1381 R.attr.actionModePopupWindowStyle);
1382 mPrimaryActionModePopup.setWindowLayoutType(
1383 WindowManager.LayoutParams.TYPE_APPLICATION);
1384 mPrimaryActionModePopup.setContentView(mPrimaryActionModeView);
1385 mPrimaryActionModePopup.setWidth(MATCH_PARENT);
1386
1387 actionBarContext.getTheme().resolveAttribute(
1388 R.attr.actionBarSize, outValue, true);
1389 final int height = TypedValue.complexToDimensionPixelSize(outValue.data,
1390 actionBarContext.getResources().getDisplayMetrics());
1391 mPrimaryActionModeView.setContentHeight(height);
1392 mPrimaryActionModePopup.setHeight(WRAP_CONTENT);
1393 mShowPrimaryActionModePopup = new Runnable() {
1394 public void run() {
1395 mPrimaryActionModePopup.showAtLocation(
1396 mPrimaryActionModeView.getApplicationWindowToken(),
1397 Gravity.TOP | Gravity.FILL_HORIZONTAL, 0, 0);
1398 endOnGoingFadeAnimation();
1399 mFadeAnim = ObjectAnimator.ofFloat(mPrimaryActionModeView, View.ALPHA,
1400 0f, 1f);
1401 mFadeAnim.addListener(new Animator.AnimatorListener() {
1402 @Override
1403 public void onAnimationStart(Animator animation) {
1404 mPrimaryActionModeView.setVisibility(VISIBLE);
1405 }
1406
1407 @Override
1408 public void onAnimationEnd(Animator animation) {
1409 mPrimaryActionModeView.setAlpha(1f);
1410 mFadeAnim = null;
1411 }
1412
1413 @Override
1414 public void onAnimationCancel(Animator animation) {
1415
1416 }
1417
1418 @Override
1419 public void onAnimationRepeat(Animator animation) {
1420
1421 }
1422 });
1423 mFadeAnim.start();
1424 }
1425 };
1426 } else {
1427 ViewStub stub = (ViewStub) findViewById(R.id.action_mode_bar_stub);
1428 if (stub != null) {
1429 mPrimaryActionModeView = (ActionBarContextView) stub.inflate();
1430 }
1431 }
1432 }
1433 if (mPrimaryActionModeView != null) {
1434 mPrimaryActionModeView.killMode();
1435 ActionMode mode = new StandaloneActionMode(
1436 mPrimaryActionModeView.getContext(), mPrimaryActionModeView,
1437 callback, mPrimaryActionModePopup == null);
1438 return mode;
1439 }
1440 return null;
1441 }
1442
1443 private void endOnGoingFadeAnimation() {
1444 if (mFadeAnim != null) {
1445 mFadeAnim.end();
1446 }
1447 }
1448
1449 private void setHandledPrimaryActionMode(ActionMode mode) {
1450 endOnGoingFadeAnimation();
1451 mPrimaryActionMode = mode;
1452 mPrimaryActionMode.invalidate();
1453 mPrimaryActionModeView.initForMode(mPrimaryActionMode);
1454 if (mPrimaryActionModePopup != null) {
1455 post(mShowPrimaryActionModePopup);
1456 } else {
1457 mFadeAnim = ObjectAnimator.ofFloat(mPrimaryActionModeView, View.ALPHA, 0f, 1f);
1458 mFadeAnim.addListener(new Animator.AnimatorListener() {
1459 @Override
1460 public void onAnimationStart(Animator animation) {
1461 mPrimaryActionModeView.setVisibility(View.VISIBLE);
1462 }
1463
1464 @Override
1465 public void onAnimationEnd(Animator animation) {
1466 mPrimaryActionModeView.setAlpha(1f);
1467 mFadeAnim = null;
1468 }
1469
1470 @Override
1471 public void onAnimationCancel(Animator animation) {
1472
1473 }
1474
1475 @Override
1476 public void onAnimationRepeat(Animator animation) {
1477
1478 }
1479 });
1480 mFadeAnim.start();
1481 }
1482 mPrimaryActionModeView.sendAccessibilityEvent(
1483 AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
1484 }
1485
1486 private ActionMode createFloatingActionMode(
1487 View originatingView, ActionMode.Callback2 callback) {
1488 if (mFloatingActionMode != null) {
1489 mFloatingActionMode.finish();
1490 }
1491 cleanupFloatingActionModeViews();
1492 final FloatingActionMode mode =
1493 new FloatingActionMode(mContext, callback, originatingView);
1494 mFloatingActionModeOriginatingView = originatingView;
1495 mFloatingToolbarPreDrawListener =
1496 new ViewTreeObserver.OnPreDrawListener() {
1497 @Override
1498 public boolean onPreDraw() {
1499 mode.updateViewLocationInWindow();
1500 return true;
1501 }
1502 };
1503 return mode;
1504 }
1505
1506 private void setHandledFloatingActionMode(ActionMode mode) {
1507 mFloatingActionMode = mode;
1508 mFloatingToolbar = new FloatingToolbar(mContext, mWindow);
1509 ((FloatingActionMode) mFloatingActionMode).setFloatingToolbar(mFloatingToolbar);
1510 mFloatingActionMode.invalidate(); // Will show the floating toolbar if necessary.
1511 mFloatingActionModeOriginatingView.getViewTreeObserver()
1512 .addOnPreDrawListener(mFloatingToolbarPreDrawListener);
1513 }
1514
1515 /**
Wale Ogunwale62a91d62015-11-18 11:44:10 -08001516 * Informs the decor if the caption is attached and visible.
Wale Ogunwale8804af22015-11-17 09:18:15 -08001517 * @param attachedAndVisible true when the decor is visible.
Wale Ogunwale62a91d62015-11-18 11:44:10 -08001518 * Note that this will even be called if there is no caption.
Wale Ogunwale8804af22015-11-17 09:18:15 -08001519 **/
Wale Ogunwale62a91d62015-11-18 11:44:10 -08001520 void enableCaption(boolean attachedAndVisible) {
1521 if (mHasCaption != attachedAndVisible) {
1522 mHasCaption = attachedAndVisible;
Wale Ogunwale8804af22015-11-17 09:18:15 -08001523 if (getForeground() != null) {
1524 drawableChanged();
1525 }
1526 }
1527 }
1528
Wale Ogunwale8804af22015-11-17 09:18:15 -08001529 void setWindow(PhoneWindow phoneWindow) {
1530 mWindow = phoneWindow;
1531 Context context = getContext();
1532 if (context instanceof DecorContext) {
Wale Ogunwale0d7e9122015-11-17 10:45:06 -08001533 DecorContext decorContext = (DecorContext) context;
1534 decorContext.setPhoneWindow(mWindow);
1535 }
1536 }
1537
1538 void onConfigurationChanged() {
Wale Ogunwale62a91d62015-11-18 11:44:10 -08001539 int workspaceId = getStackId();
1540 if (mDecorCaptionView != null) {
1541 if (mStackId != workspaceId) {
1542 mStackId = workspaceId;
Wale Ogunwale0d7e9122015-11-17 10:45:06 -08001543 // We might have to change the kind of surface before we do anything else.
Wale Ogunwale62a91d62015-11-18 11:44:10 -08001544 mDecorCaptionView.onConfigurationChanged(
1545 ActivityManager.StackId.hasWindowDecor(mStackId));
1546 enableCaption(ActivityManager.StackId.hasWindowDecor(workspaceId));
Wale Ogunwale0d7e9122015-11-17 10:45:06 -08001547 }
1548 }
Wale Ogunwale2b547c32015-11-18 10:33:22 -08001549 initializeElevation();
Wale Ogunwale0d7e9122015-11-17 10:45:06 -08001550 }
1551
1552 View onResourcesLoaded(LayoutInflater inflater, int layoutResource) {
Wale Ogunwale62a91d62015-11-18 11:44:10 -08001553 mStackId = getStackId();
Wale Ogunwale8cc5a742015-11-17 15:41:05 -08001554
Wale Ogunwalebf9eefc2015-11-17 14:47:52 -08001555 mResizingBackgroundDrawable = getResizingBackgroundDrawable(
1556 mWindow.mBackgroundResource, mWindow.mBackgroundFallbackResource);
1557 mCaptionBackgroundDrawable =
Wale Ogunwale62a91d62015-11-18 11:44:10 -08001558 getContext().getDrawable(R.drawable.decor_caption_title_focused);
Wale Ogunwalebf9eefc2015-11-17 14:47:52 -08001559
1560 if (mBackdropFrameRenderer != null) {
1561 mBackdropFrameRenderer.onResourcesLoaded(
1562 this, mResizingBackgroundDrawable, mCaptionBackgroundDrawable);
1563 }
1564
Wale Ogunwale62a91d62015-11-18 11:44:10 -08001565 mDecorCaptionView = createDecorCaptionView(inflater);
Wale Ogunwale0d7e9122015-11-17 10:45:06 -08001566 final View root = inflater.inflate(layoutResource, null);
Wale Ogunwale62a91d62015-11-18 11:44:10 -08001567 if (mDecorCaptionView != null) {
1568 if (mDecorCaptionView.getParent() == null) {
1569 addView(mDecorCaptionView,
Wale Ogunwale0d7e9122015-11-17 10:45:06 -08001570 new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));
1571 }
Wale Ogunwale62a91d62015-11-18 11:44:10 -08001572 mDecorCaptionView.addView(root,
Filip Gruszczynski63250652015-11-18 14:43:01 -08001573 new ViewGroup.MarginLayoutParams(MATCH_PARENT, MATCH_PARENT));
Wale Ogunwale0d7e9122015-11-17 10:45:06 -08001574 } else {
1575 addView(root, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));
1576 }
1577 mContentRoot = (ViewGroup) root;
Wale Ogunwale2b547c32015-11-18 10:33:22 -08001578 initializeElevation();
Wale Ogunwale0d7e9122015-11-17 10:45:06 -08001579 return root;
1580 }
1581
Wale Ogunwale62a91d62015-11-18 11:44:10 -08001582 // Free floating overlapping windows require a caption.
1583 private DecorCaptionView createDecorCaptionView(LayoutInflater inflater) {
1584 DecorCaptionView DecorCaptionView = null;
1585 for (int i = getChildCount() - 1; i >= 0 && DecorCaptionView == null; i--) {
Wale Ogunwale0d7e9122015-11-17 10:45:06 -08001586 View view = getChildAt(i);
Wale Ogunwale62a91d62015-11-18 11:44:10 -08001587 if (view instanceof DecorCaptionView) {
Wale Ogunwale0d7e9122015-11-17 10:45:06 -08001588 // The decor was most likely saved from a relaunch - so reuse it.
Wale Ogunwale62a91d62015-11-18 11:44:10 -08001589 DecorCaptionView = (DecorCaptionView) view;
Wale Ogunwale0d7e9122015-11-17 10:45:06 -08001590 removeViewAt(i);
1591 }
1592 }
1593 final WindowManager.LayoutParams attrs = mWindow.getAttributes();
Wale Ogunwale2b547c32015-11-18 10:33:22 -08001594 final boolean isApplication = attrs.type == TYPE_BASE_APPLICATION ||
Wale Ogunwale0d7e9122015-11-17 10:45:06 -08001595 attrs.type == TYPE_APPLICATION;
Wale Ogunwale62a91d62015-11-18 11:44:10 -08001596 // Only a non floating application window on one of the allowed workspaces can get a caption
Wale Ogunwale2b547c32015-11-18 10:33:22 -08001597 if (!mWindow.isFloating() && isApplication
Wale Ogunwale62a91d62015-11-18 11:44:10 -08001598 && ActivityManager.StackId.hasWindowDecor(mStackId)) {
Wale Ogunwale0d7e9122015-11-17 10:45:06 -08001599 // Dependent on the brightness of the used title we either use the
1600 // dark or the light button frame.
Wale Ogunwale62a91d62015-11-18 11:44:10 -08001601 if (DecorCaptionView == null) {
Wale Ogunwale0d7e9122015-11-17 10:45:06 -08001602 Context context = getContext();
1603 TypedValue value = new TypedValue();
1604 context.getTheme().resolveAttribute(R.attr.colorPrimary, value, true);
1605 inflater = inflater.from(context);
1606 if (Color.luminance(value.data) < 0.5) {
Wale Ogunwale62a91d62015-11-18 11:44:10 -08001607 DecorCaptionView = (DecorCaptionView) inflater.inflate(
1608 R.layout.decor_caption_dark, null);
Wale Ogunwale0d7e9122015-11-17 10:45:06 -08001609 } else {
Wale Ogunwale62a91d62015-11-18 11:44:10 -08001610 DecorCaptionView = (DecorCaptionView) inflater.inflate(
1611 R.layout.decor_caption_light, null);
Wale Ogunwale0d7e9122015-11-17 10:45:06 -08001612 }
1613 }
Wale Ogunwale62a91d62015-11-18 11:44:10 -08001614 DecorCaptionView.setPhoneWindow(mWindow, true /*showDecor*/);
Wale Ogunwale8cc5a742015-11-17 15:41:05 -08001615 } else {
Wale Ogunwale62a91d62015-11-18 11:44:10 -08001616 DecorCaptionView = null;
Wale Ogunwale0d7e9122015-11-17 10:45:06 -08001617 }
Wale Ogunwale0d7e9122015-11-17 10:45:06 -08001618
Wale Ogunwale62a91d62015-11-18 11:44:10 -08001619 // Tell the decor if it has a visible caption.
1620 enableCaption(DecorCaptionView != null);
1621 return DecorCaptionView;
Wale Ogunwale0d7e9122015-11-17 10:45:06 -08001622 }
1623
1624 /**
Wale Ogunwalebf9eefc2015-11-17 14:47:52 -08001625 * Returns the color used to fill areas the app has not rendered content to yet when the
1626 * user is resizing the window of an activity in multi-window mode.
1627 */
1628 private Drawable getResizingBackgroundDrawable(int backgroundRes, int backgroundFallbackRes) {
Wale Ogunwale0d7e9122015-11-17 10:45:06 -08001629 final Context context = getContext();
1630
Wale Ogunwalebf9eefc2015-11-17 14:47:52 -08001631 if (backgroundRes != 0) {
1632 final Drawable drawable = context.getDrawable(backgroundRes);
Wale Ogunwale0d7e9122015-11-17 10:45:06 -08001633 if (drawable != null) {
1634 return drawable;
1635 }
1636 }
1637
Wale Ogunwalebf9eefc2015-11-17 14:47:52 -08001638 if (backgroundFallbackRes != 0) {
1639 final Drawable fallbackDrawable = context.getDrawable(backgroundFallbackRes);
Wale Ogunwale0d7e9122015-11-17 10:45:06 -08001640 if (fallbackDrawable != null) {
1641 return fallbackDrawable;
1642 }
1643 }
1644
1645 // We shouldn't really get here as the background fallback should be always available since
1646 // it is defaulted by the system.
1647 Log.w(TAG, "Failed to find background drawable for PhoneWindow=" + mWindow);
1648 return null;
1649 }
1650
1651 /**
Wale Ogunwale62a91d62015-11-18 11:44:10 -08001652 * Returns the Id of the stack which contains this window.
1653 * Note that if no stack can be determined - which usually means that it was not
1654 * created for an activity - the fullscreen stack ID will be returned.
1655 * @return Returns the stack id which contains this window.
Wale Ogunwale0d7e9122015-11-17 10:45:06 -08001656 **/
Wale Ogunwale62a91d62015-11-18 11:44:10 -08001657 private int getStackId() {
Wale Ogunwale0d7e9122015-11-17 10:45:06 -08001658 int workspaceId = INVALID_STACK_ID;
1659 final Window.WindowControllerCallback callback = mWindow.getWindowControllerCallback();
1660 if (callback != null) {
1661 try {
1662 workspaceId = callback.getWindowStackId();
1663 } catch (RemoteException ex) {
1664 Log.e(TAG, "Failed to get the workspace ID of a PhoneWindow.");
1665 }
1666 }
1667 if (workspaceId == INVALID_STACK_ID) {
1668 return FULLSCREEN_WORKSPACE_STACK_ID;
1669 }
1670 return workspaceId;
1671 }
1672
1673 void clearContentView() {
Wale Ogunwale62a91d62015-11-18 11:44:10 -08001674 if (mDecorCaptionView != null) {
Filip Gruszczynski63250652015-11-18 14:43:01 -08001675 mDecorCaptionView.removeContentView();
Wale Ogunwale0d7e9122015-11-17 10:45:06 -08001676 } else {
Wale Ogunwale62a91d62015-11-18 11:44:10 -08001677 // This window doesn't have caption, so we need to just remove the
Wale Ogunwale0d7e9122015-11-17 10:45:06 -08001678 // children of the decor view.
1679 removeAllViews();
Wale Ogunwale8804af22015-11-17 09:18:15 -08001680 }
1681 }
1682
Wale Ogunwalebf9eefc2015-11-17 14:47:52 -08001683 @Override
1684 public void onWindowSizeIsChanging(Rect newBounds) {
1685 if (mBackdropFrameRenderer != null) {
1686 mBackdropFrameRenderer.setTargetRect(newBounds);
1687 }
1688 }
1689
1690 @Override
1691 public void onWindowDragResizeStart(Rect initialBounds) {
1692 if (mWindow.isDestroyed()) {
1693 // If the owner's window is gone, we should not be able to come here anymore.
1694 releaseThreadedRenderer();
1695 return;
1696 }
1697 if (mBackdropFrameRenderer != null) {
1698 return;
1699 }
1700 final ThreadedRenderer renderer = (ThreadedRenderer) getHardwareRenderer();
1701 if (renderer != null) {
1702 mBackdropFrameRenderer = new BackdropFrameRenderer(this, renderer,
1703 initialBounds, mResizingBackgroundDrawable, mCaptionBackgroundDrawable);
1704
1705 // Get rid of the shadow while we are resizing. Shadow drawing takes considerable time.
1706 // If we want to get the shadow shown while resizing, we would need to elevate a new
1707 // element which owns the caption and has the elevation.
1708 updateElevation();
1709 }
1710 }
1711
1712 @Override
1713 public void onWindowDragResizeEnd() {
1714 releaseThreadedRenderer();
1715 }
1716
1717 @Override
1718 public boolean onContentDrawn(int offsetX, int offsetY, int sizeX, int sizeY) {
1719 if (mBackdropFrameRenderer == null) {
1720 return false;
1721 }
1722 return mBackdropFrameRenderer.onContentDrawn(offsetX, offsetY, sizeX, sizeY);
1723 }
1724
1725 @Override
1726 public void onRequestDraw(boolean reportNextDraw) {
1727 if (mBackdropFrameRenderer != null) {
1728 mBackdropFrameRenderer.onRequestDraw(reportNextDraw);
1729 } else if (reportNextDraw) {
1730 // If render thread is gone, just report immediately.
1731 if (isAttachedToWindow()) {
1732 getViewRootImpl().reportDrawFinish();
1733 }
1734 }
1735 }
1736
1737 /** Release the renderer thread which is usually done when the user stops resizing. */
1738 private void releaseThreadedRenderer() {
1739 if (mBackdropFrameRenderer != null) {
1740 mBackdropFrameRenderer.releaseRenderer();
1741 mBackdropFrameRenderer = null;
1742 // Bring the shadow back.
1743 updateElevation();
1744 }
1745 }
1746
Wale Ogunwale2b547c32015-11-18 10:33:22 -08001747 /**
1748 * The elevation gets set for the first time and the framework needs to be informed that
1749 * the surface layer gets created with the shadow size in mind.
1750 */
1751 private void initializeElevation() {
1752 // TODO(skuhne): Call setMaxElevation here accordingly after b/22668382 got fixed.
1753 mAllowUpdateElevation = false;
1754 updateElevation();
1755 }
1756
Wale Ogunwalebf9eefc2015-11-17 14:47:52 -08001757 private void updateElevation() {
Wale Ogunwale2b547c32015-11-18 10:33:22 -08001758 float elevation = 0;
1759 final boolean wasAdjustedForStack = mElevationAdjustedForStack;
1760 // Do not use a shadow when we are in resizing mode (mBackdropFrameRenderer not null)
1761 // since the shadow is bound to the content size and not the target size.
Wale Ogunwale62a91d62015-11-18 11:44:10 -08001762 if (ActivityManager.StackId.hasWindowShadow(mStackId)
Wale Ogunwale2b547c32015-11-18 10:33:22 -08001763 && mBackdropFrameRenderer == null) {
1764 elevation = hasWindowFocus() ?
1765 DECOR_SHADOW_FOCUSED_HEIGHT_IN_DIP : DECOR_SHADOW_UNFOCUSED_HEIGHT_IN_DIP;
1766 // TODO(skuhne): Remove this if clause once b/22668382 got fixed.
1767 if (!mAllowUpdateElevation) {
1768 elevation = DECOR_SHADOW_FOCUSED_HEIGHT_IN_DIP;
1769 }
1770 // Convert the DP elevation into physical pixels.
1771 elevation = dipToPx(elevation);
1772 mElevationAdjustedForStack = true;
1773 } else {
1774 mElevationAdjustedForStack = false;
1775 }
1776
1777 // Don't change the elevation if we didn't previously adjust it for the stack it was in
1778 // or it didn't change.
1779 if ((wasAdjustedForStack || mElevationAdjustedForStack)
1780 && getElevation() != elevation) {
1781 mWindow.setElevation(elevation);
Wale Ogunwalebf9eefc2015-11-17 14:47:52 -08001782 }
1783 }
1784
Wale Ogunwale8cc5a742015-11-17 15:41:05 -08001785 boolean isShowingCaption() {
Wale Ogunwale62a91d62015-11-18 11:44:10 -08001786 return mDecorCaptionView != null && mDecorCaptionView.isCaptionShowing();
Wale Ogunwale8cc5a742015-11-17 15:41:05 -08001787 }
1788
1789 int getCaptionHeight() {
Wale Ogunwale62a91d62015-11-18 11:44:10 -08001790 return isShowingCaption() ? mDecorCaptionView.getCaptionHeight() : 0;
Wale Ogunwale8cc5a742015-11-17 15:41:05 -08001791 }
1792
Wale Ogunwale2b547c32015-11-18 10:33:22 -08001793 /**
1794 * Converts a DIP measure into physical pixels.
1795 * @param dip The dip value.
1796 * @return Returns the number of pixels.
1797 */
1798 private float dipToPx(float dip) {
1799 return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip,
1800 getResources().getDisplayMetrics());
1801 }
1802
Wale Ogunwale8804af22015-11-17 09:18:15 -08001803 private static class ColorViewState {
1804 View view = null;
1805 int targetVisibility = View.INVISIBLE;
1806 boolean present = false;
1807
1808 final int id;
1809 final int systemUiHideFlag;
1810 final int translucentFlag;
1811 final int verticalGravity;
1812 final int horizontalGravity;
1813 final String transitionName;
1814 final int hideWindowFlag;
1815
1816 ColorViewState(int systemUiHideFlag,
1817 int translucentFlag, int verticalGravity, int horizontalGravity,
1818 String transitionName, int id, int hideWindowFlag) {
1819 this.id = id;
1820 this.systemUiHideFlag = systemUiHideFlag;
1821 this.translucentFlag = translucentFlag;
1822 this.verticalGravity = verticalGravity;
1823 this.horizontalGravity = horizontalGravity;
1824 this.transitionName = transitionName;
1825 this.hideWindowFlag = hideWindowFlag;
1826 }
1827 }
1828
1829 /**
1830 * Clears out internal references when the action mode is destroyed.
1831 */
1832 private class ActionModeCallback2Wrapper extends ActionMode.Callback2 {
1833 private final ActionMode.Callback mWrapped;
1834
1835 public ActionModeCallback2Wrapper(ActionMode.Callback wrapped) {
1836 mWrapped = wrapped;
1837 }
1838
1839 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
1840 return mWrapped.onCreateActionMode(mode, menu);
1841 }
1842
1843 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
1844 requestFitSystemWindows();
1845 return mWrapped.onPrepareActionMode(mode, menu);
1846 }
1847
1848 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
1849 return mWrapped.onActionItemClicked(mode, item);
1850 }
1851
1852 public void onDestroyActionMode(ActionMode mode) {
1853 mWrapped.onDestroyActionMode(mode);
1854 final boolean isMncApp = mContext.getApplicationInfo().targetSdkVersion
1855 >= Build.VERSION_CODES.M;
1856 final boolean isPrimary;
1857 final boolean isFloating;
1858 if (isMncApp) {
1859 isPrimary = mode == mPrimaryActionMode;
1860 isFloating = mode == mFloatingActionMode;
1861 if (!isPrimary && mode.getType() == ActionMode.TYPE_PRIMARY) {
1862 Log.e(TAG, "Destroying unexpected ActionMode instance of TYPE_PRIMARY; "
1863 + mode + " was not the current primary action mode! Expected "
1864 + mPrimaryActionMode);
1865 }
1866 if (!isFloating && mode.getType() == ActionMode.TYPE_FLOATING) {
1867 Log.e(TAG, "Destroying unexpected ActionMode instance of TYPE_FLOATING; "
1868 + mode + " was not the current floating action mode! Expected "
1869 + mFloatingActionMode);
1870 }
1871 } else {
1872 isPrimary = mode.getType() == ActionMode.TYPE_PRIMARY;
1873 isFloating = mode.getType() == ActionMode.TYPE_FLOATING;
1874 }
1875 if (isPrimary) {
1876 if (mPrimaryActionModePopup != null) {
1877 removeCallbacks(mShowPrimaryActionModePopup);
1878 }
1879 if (mPrimaryActionModeView != null) {
1880 endOnGoingFadeAnimation();
1881 mFadeAnim = ObjectAnimator.ofFloat(mPrimaryActionModeView, View.ALPHA,
1882 1f, 0f);
1883 mFadeAnim.addListener(new Animator.AnimatorListener() {
1884 @Override
1885 public void onAnimationStart(Animator animation) {
1886
1887 }
1888
1889 @Override
1890 public void onAnimationEnd(Animator animation) {
1891 mPrimaryActionModeView.setVisibility(GONE);
1892 if (mPrimaryActionModePopup != null) {
1893 mPrimaryActionModePopup.dismiss();
1894 }
1895 mPrimaryActionModeView.removeAllViews();
1896 mFadeAnim = null;
1897 }
1898
1899 @Override
1900 public void onAnimationCancel(Animator animation) {
1901
1902 }
1903
1904 @Override
1905 public void onAnimationRepeat(Animator animation) {
1906
1907 }
1908 });
1909 mFadeAnim.start();
1910 }
1911
1912 mPrimaryActionMode = null;
1913 } else if (isFloating) {
1914 cleanupFloatingActionModeViews();
1915 mFloatingActionMode = null;
1916 }
1917 if (mWindow.getCallback() != null && !mWindow.isDestroyed()) {
1918 try {
1919 mWindow.getCallback().onActionModeFinished(mode);
1920 } catch (AbstractMethodError ame) {
1921 // Older apps might not implement this callback method.
1922 }
1923 }
1924 requestFitSystemWindows();
1925 }
1926
1927 @Override
1928 public void onGetContentRect(ActionMode mode, View view, Rect outRect) {
1929 if (mWrapped instanceof ActionMode.Callback2) {
1930 ((ActionMode.Callback2) mWrapped).onGetContentRect(mode, view, outRect);
1931 } else {
1932 super.onGetContentRect(mode, view, outRect);
1933 }
1934 }
1935 }
1936}