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