Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.internal.widget; |
| 18 | |
Wale Ogunwale | 3797c22 | 2015-10-27 14:21:58 -0700 | [diff] [blame^] | 19 | import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID; |
| 20 | |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 21 | import android.content.Context; |
Skuhne | ce2faa5 | 2015-08-11 10:36:38 -0700 | [diff] [blame] | 22 | import android.graphics.Rect; |
Wale Ogunwale | f3a62fb | 2015-10-14 17:07:29 -0700 | [diff] [blame] | 23 | import android.graphics.drawable.Drawable; |
Skuhne | b816087 | 2015-09-22 09:51:39 -0700 | [diff] [blame] | 24 | import android.os.Looper; |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 25 | import android.os.RemoteException; |
| 26 | import android.util.AttributeSet; |
Skuhne | b816087 | 2015-09-22 09:51:39 -0700 | [diff] [blame] | 27 | import android.view.Choreographer; |
| 28 | import android.view.DisplayListCanvas; |
Skuhne | 81c524a | 2015-08-12 13:34:14 -0700 | [diff] [blame] | 29 | import android.view.MotionEvent; |
Skuhne | b816087 | 2015-09-22 09:51:39 -0700 | [diff] [blame] | 30 | import android.view.RenderNode; |
| 31 | import android.view.ThreadedRenderer; |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 32 | import android.view.View; |
Skuhne | f7b882c | 2015-08-11 17:18:58 -0700 | [diff] [blame] | 33 | import android.widget.LinearLayout; |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 34 | import android.view.ViewGroup; |
Stefan Kuhne | f4dd71a | 2015-08-07 09:28:52 -0700 | [diff] [blame] | 35 | import android.view.ViewOutlineProvider; |
| 36 | import android.view.Window; |
Skuhne | b816087 | 2015-09-22 09:51:39 -0700 | [diff] [blame] | 37 | import android.view.WindowCallbacks; |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 38 | import android.util.Log; |
| 39 | import android.util.TypedValue; |
| 40 | |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 41 | import com.android.internal.R; |
| 42 | import com.android.internal.policy.PhoneWindow; |
| 43 | |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 44 | /** |
| 45 | * This class represents the special screen elements to control a window on free form |
| 46 | * environment. All thse screen elements are added in the "non client area" which is the area of |
| 47 | * the window which is handled by the OS and not the application. |
| 48 | * As such this class handles the following things: |
| 49 | * <ul> |
| 50 | * <li>The caption, containing the system buttons like maximize, close and such as well as |
| 51 | * allowing the user to drag the window around.</li> |
| 52 | * <li>The shadow - which is changing dependent on the window focus.</li> |
| 53 | * <li>The border around the client area (if there is one).</li> |
| 54 | * <li>The resize handles which allow to resize the window.</li> |
| 55 | * </ul> |
| 56 | * After creating the view, the function |
Wale Ogunwale | f3a62fb | 2015-10-14 17:07:29 -0700 | [diff] [blame] | 57 | * {@link #setPhoneWindow} needs to be called to make |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 58 | * the connection to it's owning PhoneWindow. |
| 59 | * Note: At this time the application can change various attributes of the DecorView which |
| 60 | * will break things (in settle/unexpected ways): |
| 61 | * <ul> |
| 62 | * <li>setElevation</li> |
| 63 | * <li>setOutlineProvider</li> |
| 64 | * <li>setSurfaceFormat</li> |
| 65 | * <li>..</li> |
| 66 | * </ul> |
| 67 | * This will be mitigated once b/22527834 will be addressed. |
| 68 | */ |
Chong Zhang | 509ea6b | 2015-09-30 14:09:52 -0700 | [diff] [blame] | 69 | public class NonClientDecorView extends LinearLayout |
Skuhne | b816087 | 2015-09-22 09:51:39 -0700 | [diff] [blame] | 70 | implements View.OnClickListener, View.OnTouchListener, WindowCallbacks { |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 71 | private final static String TAG = "NonClientDecorView"; |
| 72 | // The height of a window which has focus in DIP. |
| 73 | private final int DECOR_SHADOW_FOCUSED_HEIGHT_IN_DIP = 20; |
| 74 | // The height of a window which has not in DIP. |
| 75 | private final int DECOR_SHADOW_UNFOCUSED_HEIGHT_IN_DIP = 5; |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 76 | private PhoneWindow mOwner = null; |
Skuhne | 81c524a | 2015-08-12 13:34:14 -0700 | [diff] [blame] | 77 | private boolean mWindowHasShadow = false; |
| 78 | private boolean mShowDecor = false; |
Skuhne | b816087 | 2015-09-22 09:51:39 -0700 | [diff] [blame] | 79 | // True when this object is listening for window size changes. |
| 80 | private boolean mAttachedCallbacksToRootViewImpl = false; |
Skuhne | 81c524a | 2015-08-12 13:34:14 -0700 | [diff] [blame] | 81 | |
| 82 | // True if the window is being dragged. |
| 83 | private boolean mDragging = false; |
| 84 | |
Skuhne | 81c524a | 2015-08-12 13:34:14 -0700 | [diff] [blame] | 85 | // True when the left mouse button got released while dragging. |
| 86 | private boolean mLeftMouseButtonReleased; |
| 87 | |
Skuhne | a5a93ee | 2015-08-20 15:43:57 -0700 | [diff] [blame] | 88 | // True if this window is resizable (which is currently only true when the decor is shown). |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 89 | public boolean mVisible = false; |
Skuhne | a5a93ee | 2015-08-20 15:43:57 -0700 | [diff] [blame] | 90 | |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 91 | // The current focus state of the window for updating the window elevation. |
Skuhne | 81c524a | 2015-08-12 13:34:14 -0700 | [diff] [blame] | 92 | private boolean mWindowHasFocus = true; |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 93 | |
| 94 | // Cludge to address b/22668382: Set the shadow size to the maximum so that the layer |
| 95 | // size calculation takes the shadow size into account. We set the elevation currently |
| 96 | // to max until the first layout command has been executed. |
Skuhne | 81c524a | 2015-08-12 13:34:14 -0700 | [diff] [blame] | 97 | private boolean mAllowUpdateElevation = false; |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 98 | |
Skuhne | b816087 | 2015-09-22 09:51:39 -0700 | [diff] [blame] | 99 | // The resize frame renderer. |
| 100 | private ResizeFrameThread mFrameRendererThread = null; |
| 101 | |
Wale Ogunwale | f3a62fb | 2015-10-14 17:07:29 -0700 | [diff] [blame] | 102 | private Drawable mResizingBackgroundDrawable; |
| 103 | private Drawable mCaptionBackgroundDrawable; |
| 104 | |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 105 | public NonClientDecorView(Context context) { |
| 106 | super(context); |
| 107 | } |
| 108 | |
| 109 | public NonClientDecorView(Context context, AttributeSet attrs) { |
| 110 | super(context, attrs); |
| 111 | } |
| 112 | |
| 113 | public NonClientDecorView(Context context, AttributeSet attrs, int defStyle) { |
| 114 | super(context, attrs, defStyle); |
| 115 | } |
| 116 | |
Chong Zhang | dcee1de | 2015-10-06 10:26:00 -0700 | [diff] [blame] | 117 | @Override |
| 118 | protected void onAttachedToWindow() { |
| 119 | super.onAttachedToWindow(); |
| 120 | if (!mAttachedCallbacksToRootViewImpl) { |
| 121 | // If there is no window callback installed there was no window set before. Set it now. |
| 122 | // Note that our ViewRootImpl object will not change. |
| 123 | getViewRootImpl().addWindowCallbacks(this); |
| 124 | mAttachedCallbacksToRootViewImpl = true; |
| 125 | } else if (mFrameRendererThread != null) { |
| 126 | // We are resizing and this call happened due to a configuration change. Tell the |
| 127 | // renderer about it. |
| 128 | mFrameRendererThread.onConfigurationChange(); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | protected void onDetachedFromWindow() { |
| 134 | super.onDetachedFromWindow(); |
| 135 | if (mAttachedCallbacksToRootViewImpl) { |
| 136 | getViewRootImpl().removeWindowCallbacks(this); |
| 137 | mAttachedCallbacksToRootViewImpl = false; |
| 138 | } |
| 139 | } |
| 140 | |
Wale Ogunwale | f3a62fb | 2015-10-14 17:07:29 -0700 | [diff] [blame] | 141 | public void setPhoneWindow(PhoneWindow owner, boolean showDecor, boolean windowHasShadow, |
| 142 | Drawable resizingBackgroundDrawable, Drawable captionBackgroundDrawableDrawable) { |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 143 | mOwner = owner; |
| 144 | mWindowHasShadow = windowHasShadow; |
Stefan Kuhne | f4dd71a | 2015-08-07 09:28:52 -0700 | [diff] [blame] | 145 | mShowDecor = showDecor; |
Wale Ogunwale | f3a62fb | 2015-10-14 17:07:29 -0700 | [diff] [blame] | 146 | mResizingBackgroundDrawable = resizingBackgroundDrawable; |
| 147 | mCaptionBackgroundDrawable = captionBackgroundDrawableDrawable; |
Skuhne | f7b882c | 2015-08-11 17:18:58 -0700 | [diff] [blame] | 148 | updateCaptionVisibility(); |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 149 | if (mWindowHasShadow) { |
Stefan Kuhne | f4dd71a | 2015-08-07 09:28:52 -0700 | [diff] [blame] | 150 | initializeElevation(); |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 151 | } |
| 152 | // By changing the outline provider to BOUNDS, the window can remove its |
| 153 | // background without removing the shadow. |
| 154 | mOwner.getDecorView().setOutlineProvider(ViewOutlineProvider.BOUNDS); |
Skuhne | b816087 | 2015-09-22 09:51:39 -0700 | [diff] [blame] | 155 | |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 156 | findViewById(R.id.maximize_window).setOnClickListener(this); |
| 157 | findViewById(R.id.close_window).setOnClickListener(this); |
| 158 | } |
| 159 | |
Skuhne | 81c524a | 2015-08-12 13:34:14 -0700 | [diff] [blame] | 160 | @Override |
Chong Zhang | 509ea6b | 2015-09-30 14:09:52 -0700 | [diff] [blame] | 161 | public boolean onTouch(View v, MotionEvent e) { |
Skuhne | 81c524a | 2015-08-12 13:34:14 -0700 | [diff] [blame] | 162 | // Note: There are no mixed events. When a new device gets used (e.g. 1. Mouse, 2. touch) |
| 163 | // the old input device events get cancelled first. So no need to remember the kind of |
| 164 | // input device we are listening to. |
| 165 | switch (e.getActionMasked()) { |
| 166 | case MotionEvent.ACTION_DOWN: |
Skuhne | a635a26 | 2015-08-26 15:45:58 -0700 | [diff] [blame] | 167 | if (!mShowDecor) { |
| 168 | // When there is no decor we should not react to anything. |
| 169 | return false; |
| 170 | } |
Skuhne | 81c524a | 2015-08-12 13:34:14 -0700 | [diff] [blame] | 171 | // A drag action is started if we aren't dragging already and the starting event is |
| 172 | // either a left mouse button or any other input device. |
| 173 | if (!mDragging && |
| 174 | (e.getToolType(e.getActionIndex()) != MotionEvent.TOOL_TYPE_MOUSE || |
| 175 | (e.getButtonState() & MotionEvent.BUTTON_PRIMARY) != 0)) { |
| 176 | mDragging = true; |
Skuhne | 81c524a | 2015-08-12 13:34:14 -0700 | [diff] [blame] | 177 | mLeftMouseButtonReleased = false; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 178 | startMovingTask(e.getRawX(), e.getRawY()); |
Skuhne | 81c524a | 2015-08-12 13:34:14 -0700 | [diff] [blame] | 179 | } |
| 180 | break; |
| 181 | |
| 182 | case MotionEvent.ACTION_MOVE: |
| 183 | if (mDragging && !mLeftMouseButtonReleased) { |
| 184 | if (e.getToolType(e.getActionIndex()) == MotionEvent.TOOL_TYPE_MOUSE && |
| 185 | (e.getButtonState() & MotionEvent.BUTTON_PRIMARY) == 0) { |
| 186 | // There is no separate mouse button up call and if the user mixes mouse |
| 187 | // button drag actions, we stop dragging once he releases the button. |
| 188 | mLeftMouseButtonReleased = true; |
| 189 | break; |
| 190 | } |
Skuhne | 81c524a | 2015-08-12 13:34:14 -0700 | [diff] [blame] | 191 | } |
| 192 | break; |
| 193 | |
| 194 | case MotionEvent.ACTION_UP: |
Skuhne | 81c524a | 2015-08-12 13:34:14 -0700 | [diff] [blame] | 195 | case MotionEvent.ACTION_CANCEL: |
Skuhne | a5a93ee | 2015-08-20 15:43:57 -0700 | [diff] [blame] | 196 | if (!mDragging) { |
| 197 | break; |
Skuhne | 81c524a | 2015-08-12 13:34:14 -0700 | [diff] [blame] | 198 | } |
Skuhne | a5a93ee | 2015-08-20 15:43:57 -0700 | [diff] [blame] | 199 | // Abort the ongoing dragging. |
| 200 | mDragging = false; |
Skuhne | a5a93ee | 2015-08-20 15:43:57 -0700 | [diff] [blame] | 201 | return true; |
Skuhne | 81c524a | 2015-08-12 13:34:14 -0700 | [diff] [blame] | 202 | } |
| 203 | return mDragging; |
| 204 | } |
| 205 | |
Stefan Kuhne | f4dd71a | 2015-08-07 09:28:52 -0700 | [diff] [blame] | 206 | /** |
| 207 | * The phone window configuration has changed and the decor needs to be updated. |
| 208 | * @param showDecor True if the decor should be shown. |
| 209 | * @param windowHasShadow True when the window should show a shadow. |
| 210 | **/ |
| 211 | public void phoneWindowUpdated(boolean showDecor, boolean windowHasShadow) { |
| 212 | mShowDecor = showDecor; |
Skuhne | f7b882c | 2015-08-11 17:18:58 -0700 | [diff] [blame] | 213 | updateCaptionVisibility(); |
Stefan Kuhne | f4dd71a | 2015-08-07 09:28:52 -0700 | [diff] [blame] | 214 | if (windowHasShadow != mWindowHasShadow) { |
| 215 | mWindowHasShadow = windowHasShadow; |
| 216 | initializeElevation(); |
| 217 | } |
| 218 | } |
| 219 | |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 220 | @Override |
| 221 | public void onClick(View view) { |
| 222 | if (view.getId() == R.id.maximize_window) { |
Stefan Kuhne | 1b42057 | 2015-08-07 10:50:19 -0700 | [diff] [blame] | 223 | maximizeWindow(); |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 224 | } else if (view.getId() == R.id.close_window) { |
Stefan Kuhne | 1b42057 | 2015-08-07 10:50:19 -0700 | [diff] [blame] | 225 | mOwner.dispatchOnWindowDismissed(true /*finishTask*/); |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
| 229 | @Override |
| 230 | public void onWindowFocusChanged(boolean hasWindowFocus) { |
| 231 | mWindowHasFocus = hasWindowFocus; |
| 232 | updateElevation(); |
| 233 | super.onWindowFocusChanged(hasWindowFocus); |
| 234 | } |
| 235 | |
| 236 | @Override |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 237 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 238 | // If the application changed its SystemUI metrics, we might also have to adapt |
| 239 | // our shadow elevation. |
| 240 | updateElevation(); |
| 241 | mAllowUpdateElevation = true; |
| 242 | |
Skuhne | f7b882c | 2015-08-11 17:18:58 -0700 | [diff] [blame] | 243 | super.onLayout(changed, left, top, right, bottom); |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 246 | @Override |
Skuhne | f7b882c | 2015-08-11 17:18:58 -0700 | [diff] [blame] | 247 | public void addView(View child, int index, ViewGroup.LayoutParams params) { |
Stefan Kuhne | f4dd71a | 2015-08-07 09:28:52 -0700 | [diff] [blame] | 248 | // Make sure that we never get more then one client area in our view. |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 249 | if (index >= 2 || getChildCount() >= 2) { |
| 250 | throw new IllegalStateException("NonClientDecorView can only handle 1 client view"); |
| 251 | } |
| 252 | super.addView(child, index, params); |
| 253 | } |
| 254 | |
Stefan Kuhne | f4dd71a | 2015-08-07 09:28:52 -0700 | [diff] [blame] | 255 | /** |
| 256 | * Determine if the workspace is entirely covered by the window. |
| 257 | * @return Returns true when the window is filling the entire screen/workspace. |
| 258 | **/ |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 259 | private boolean isFillingScreen() { |
| 260 | return (0 != ((getWindowSystemUiVisibility() | getSystemUiVisibility()) & |
| 261 | (View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | |
| 262 | View.SYSTEM_UI_FLAG_IMMERSIVE | View.SYSTEM_UI_FLAG_LOW_PROFILE))); |
| 263 | } |
| 264 | |
Stefan Kuhne | f4dd71a | 2015-08-07 09:28:52 -0700 | [diff] [blame] | 265 | /** |
Skuhne | f7b882c | 2015-08-11 17:18:58 -0700 | [diff] [blame] | 266 | * Updates the visibility of the caption. |
| 267 | **/ |
| 268 | private void updateCaptionVisibility() { |
| 269 | // Don't show the decor if the window has e.g. entered full screen. |
| 270 | boolean invisible = isFillingScreen() || !mShowDecor; |
| 271 | View caption = getChildAt(0); |
| 272 | caption.setVisibility(invisible ? GONE : VISIBLE); |
Chong Zhang | 509ea6b | 2015-09-30 14:09:52 -0700 | [diff] [blame] | 273 | caption.setOnTouchListener(this); |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 274 | mVisible = !invisible; |
Skuhne | f7b882c | 2015-08-11 17:18:58 -0700 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | /** |
Stefan Kuhne | f4dd71a | 2015-08-07 09:28:52 -0700 | [diff] [blame] | 278 | * The elevation gets set for the first time and the framework needs to be informed that |
| 279 | * the surface layer gets created with the shadow size in mind. |
| 280 | **/ |
| 281 | private void initializeElevation() { |
| 282 | // TODO(skuhne): Call setMaxElevation here accordingly after b/22668382 got fixed. |
| 283 | mAllowUpdateElevation = false; |
| 284 | if (mWindowHasShadow) { |
| 285 | updateElevation(); |
| 286 | } else { |
| 287 | mOwner.setElevation(0); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * The shadow height gets controlled by the focus to visualize highlighted windows. |
| 293 | * Note: This will overwrite application elevation properties. |
| 294 | * Note: Windows which have (temporarily) changed their attributes to cover the SystemUI |
| 295 | * will get no shadow as they are expected to be "full screen". |
| 296 | **/ |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 297 | private void updateElevation() { |
| 298 | float elevation = 0; |
Skuhne | b816087 | 2015-09-22 09:51:39 -0700 | [diff] [blame] | 299 | // Do not use a shadow when we are in resizing mode (mRenderer not null) since the shadow |
| 300 | // is bound to the content size and not the target size. |
| 301 | if (mWindowHasShadow && mFrameRendererThread == null) { |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 302 | boolean fill = isFillingScreen(); |
| 303 | elevation = fill ? 0 : |
| 304 | (mWindowHasFocus ? DECOR_SHADOW_FOCUSED_HEIGHT_IN_DIP : |
| 305 | DECOR_SHADOW_UNFOCUSED_HEIGHT_IN_DIP); |
Stefan Kuhne | f4dd71a | 2015-08-07 09:28:52 -0700 | [diff] [blame] | 306 | // TODO(skuhne): Remove this if clause once b/22668382 got fixed. |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 307 | if (!mAllowUpdateElevation && !fill) { |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 308 | elevation = DECOR_SHADOW_FOCUSED_HEIGHT_IN_DIP; |
| 309 | } |
| 310 | // Convert the DP elevation into physical pixels. |
| 311 | elevation = dipToPx(elevation); |
| 312 | } |
| 313 | // Don't change the elevation if it didn't change since it can require some time. |
| 314 | if (mOwner.getDecorView().getElevation() != elevation) { |
| 315 | mOwner.setElevation(elevation); |
| 316 | } |
| 317 | } |
| 318 | |
Stefan Kuhne | f4dd71a | 2015-08-07 09:28:52 -0700 | [diff] [blame] | 319 | /** |
| 320 | * Converts a DIP measure into physical pixels. |
| 321 | * @param dip The dip value. |
| 322 | * @return Returns the number of pixels. |
| 323 | */ |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 324 | private float dipToPx(float dip) { |
| 325 | return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, |
| 326 | getResources().getDisplayMetrics()); |
| 327 | } |
Stefan Kuhne | 1b42057 | 2015-08-07 10:50:19 -0700 | [diff] [blame] | 328 | |
Stefan Kuhne | f4dd71a | 2015-08-07 09:28:52 -0700 | [diff] [blame] | 329 | /** |
| 330 | * Maximize the window by moving it to the maximized workspace stack. |
| 331 | **/ |
Stefan Kuhne | 1b42057 | 2015-08-07 10:50:19 -0700 | [diff] [blame] | 332 | private void maximizeWindow() { |
Skuhne | ce2faa5 | 2015-08-11 10:36:38 -0700 | [diff] [blame] | 333 | Window.WindowControllerCallback callback = mOwner.getWindowControllerCallback(); |
Stefan Kuhne | 1b42057 | 2015-08-07 10:50:19 -0700 | [diff] [blame] | 334 | if (callback != null) { |
| 335 | try { |
Wale Ogunwale | 3797c22 | 2015-10-27 14:21:58 -0700 | [diff] [blame^] | 336 | callback.changeWindowStack(FULLSCREEN_WORKSPACE_STACK_ID); |
Stefan Kuhne | 1b42057 | 2015-08-07 10:50:19 -0700 | [diff] [blame] | 337 | } catch (RemoteException ex) { |
| 338 | Log.e(TAG, "Cannot change task workspace."); |
| 339 | } |
| 340 | } |
| 341 | } |
Skuhne | b816087 | 2015-09-22 09:51:39 -0700 | [diff] [blame] | 342 | |
| 343 | @Override |
| 344 | public void onWindowDragResizeStart(Rect initialBounds) { |
| 345 | if (mOwner.isDestroyed()) { |
| 346 | // If the owner's window is gone, we should not be able to come here anymore. |
| 347 | releaseResources(); |
| 348 | return; |
| 349 | } |
| 350 | if (mFrameRendererThread != null) { |
| 351 | return; |
| 352 | } |
| 353 | final ThreadedRenderer renderer = |
| 354 | (ThreadedRenderer) mOwner.getDecorView().getHardwareRenderer(); |
| 355 | if (renderer != null) { |
| 356 | mFrameRendererThread = new ResizeFrameThread(renderer, initialBounds); |
| 357 | // Get rid of the shadow while we are resizing. Shadow drawing takes considerable time. |
| 358 | // If we want to get the shadow shown while resizing, we would need to elevate a new |
| 359 | // element which owns the caption and has the elevation. |
| 360 | updateElevation(); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | @Override |
Chong Zhang | 8dbd9ad | 2015-10-09 10:06:11 -0700 | [diff] [blame] | 365 | public boolean onContentDrawn(int xOffset, int yOffset, int xSize, int ySize) { |
| 366 | if (mFrameRendererThread == null) { |
| 367 | return false; |
| 368 | } |
| 369 | return mFrameRendererThread.onContentDrawn(xOffset, yOffset, xSize, ySize); |
| 370 | } |
| 371 | |
| 372 | @Override |
| 373 | public void onRequestDraw(boolean reportNextDraw) { |
Skuhne | 980ee47 | 2015-10-06 11:31:31 -0700 | [diff] [blame] | 374 | if (mFrameRendererThread != null) { |
Chong Zhang | 8dbd9ad | 2015-10-09 10:06:11 -0700 | [diff] [blame] | 375 | mFrameRendererThread.onRequsetDraw(reportNextDraw); |
| 376 | } else if (reportNextDraw) { |
| 377 | // If render thread is gone, just report immediately. |
| 378 | if (isAttachedToWindow()) { |
| 379 | getViewRootImpl().reportDrawFinish(); |
| 380 | } |
Skuhne | 980ee47 | 2015-10-06 11:31:31 -0700 | [diff] [blame] | 381 | } |
| 382 | } |
| 383 | |
| 384 | @Override |
Skuhne | b816087 | 2015-09-22 09:51:39 -0700 | [diff] [blame] | 385 | public void onWindowDragResizeEnd() { |
| 386 | releaseThreadedRenderer(); |
| 387 | } |
| 388 | |
| 389 | @Override |
| 390 | public void onWindowSizeIsChanging(Rect newBounds) { |
| 391 | if (mFrameRendererThread != null) { |
| 392 | mFrameRendererThread.setTargetRect(newBounds); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | /** |
| 397 | * Release the renderer thread which is usually done when the user stops resizing. |
| 398 | */ |
| 399 | private void releaseThreadedRenderer() { |
| 400 | if (mFrameRendererThread != null) { |
| 401 | mFrameRendererThread.releaseRenderer(); |
| 402 | mFrameRendererThread = null; |
| 403 | // Bring the shadow back. |
| 404 | updateElevation(); |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | /** |
| 409 | * Called when the parent window is destroyed to release all resources. Note that this will also |
| 410 | * destroy the renderer thread. |
| 411 | */ |
| 412 | private void releaseResources() { |
| 413 | releaseThreadedRenderer(); |
Skuhne | b816087 | 2015-09-22 09:51:39 -0700 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | /** |
| 417 | * The thread which draws the chrome while we are resizing. |
| 418 | * It starts with the creation and it ends once someone calls destroy(). |
| 419 | * Any size changes can be passed by a call to setTargetRect will passed to the thread and |
| 420 | * executed via the Choreographer. |
Wale Ogunwale | d16ebb0 | 2015-10-09 13:18:44 -0700 | [diff] [blame] | 421 | * TODO(b/24810450): Separate functionality from non-client-decor so that it can be used |
| 422 | * independently. |
Skuhne | b816087 | 2015-09-22 09:51:39 -0700 | [diff] [blame] | 423 | */ |
| 424 | private class ResizeFrameThread extends Thread implements Choreographer.FrameCallback { |
| 425 | // This is containing the last requested size by a resize command. Note that this size might |
| 426 | // or might not have been applied to the output already. |
| 427 | private final Rect mTargetRect = new Rect(); |
| 428 | |
| 429 | // The render nodes for the multi threaded renderer. |
| 430 | private ThreadedRenderer mRenderer; |
| 431 | private RenderNode mFrameNode; |
| 432 | private RenderNode mBackdropNode; |
| 433 | |
| 434 | private final Rect mOldTargetRect = new Rect(); |
| 435 | private final Rect mNewTargetRect = new Rect(); |
| 436 | private Choreographer mChoreographer; |
| 437 | |
| 438 | // Cached size values from the last render for the case that the view hierarchy is gone |
| 439 | // during a configuration change. |
| 440 | private int mLastContentWidth; |
| 441 | private int mLastContentHeight; |
| 442 | private int mLastCaptionHeight; |
| 443 | private int mLastXOffset; |
| 444 | private int mLastYOffset; |
| 445 | |
Chong Zhang | 8dbd9ad | 2015-10-09 10:06:11 -0700 | [diff] [blame] | 446 | // Whether to report when next frame is drawn or not. |
| 447 | private boolean mReportNextDraw; |
| 448 | |
Skuhne | b816087 | 2015-09-22 09:51:39 -0700 | [diff] [blame] | 449 | ResizeFrameThread(ThreadedRenderer renderer, Rect initialBounds) { |
Wale Ogunwale | 25cd08c | 2015-10-08 10:09:28 -0700 | [diff] [blame] | 450 | setName("ResizeFrame"); |
Skuhne | b816087 | 2015-09-22 09:51:39 -0700 | [diff] [blame] | 451 | mRenderer = renderer; |
| 452 | |
| 453 | // Create the render nodes for our frame and backdrop which can be resized independently |
| 454 | // from the content. |
| 455 | mFrameNode = RenderNode.create("FrameNode", null); |
| 456 | mBackdropNode = RenderNode.create("BackdropNode", null); |
| 457 | |
| 458 | mRenderer.addRenderNode(mFrameNode, false); |
| 459 | mRenderer.addRenderNode(mBackdropNode, true); |
| 460 | |
| 461 | // Set the initial bounds and draw once so that we do not get a broken frame. |
| 462 | mTargetRect.set(initialBounds); |
Wale Ogunwale | 25cd08c | 2015-10-08 10:09:28 -0700 | [diff] [blame] | 463 | synchronized (this) { |
| 464 | changeWindowSizeLocked(initialBounds); |
| 465 | } |
Skuhne | b816087 | 2015-09-22 09:51:39 -0700 | [diff] [blame] | 466 | |
| 467 | // Kick off our draw thread. |
| 468 | start(); |
| 469 | } |
| 470 | |
| 471 | /** |
| 472 | * Call this function asynchronously when the window size has been changed. The change will |
| 473 | * be picked up once per frame and the frame will be re-rendered accordingly. |
| 474 | * @param newTargetBounds The new target bounds. |
| 475 | */ |
| 476 | public void setTargetRect(Rect newTargetBounds) { |
| 477 | synchronized (this) { |
| 478 | mTargetRect.set(newTargetBounds); |
| 479 | // Notify of a bounds change. |
| 480 | pingRenderLocked(); |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | /** |
| 485 | * The window got replaced due to a configuration change. |
| 486 | */ |
| 487 | public void onConfigurationChange() { |
Wale Ogunwale | 25cd08c | 2015-10-08 10:09:28 -0700 | [diff] [blame] | 488 | synchronized (this) { |
| 489 | if (mRenderer != null) { |
| 490 | // Enforce a window redraw. |
| 491 | mOldTargetRect.set(0, 0, 0, 0); |
| 492 | pingRenderLocked(); |
| 493 | } |
Skuhne | b816087 | 2015-09-22 09:51:39 -0700 | [diff] [blame] | 494 | } |
| 495 | } |
| 496 | |
| 497 | /** |
| 498 | * All resources of the renderer will be released. This function can be called from the |
| 499 | * the UI thread as well as the renderer thread. |
| 500 | */ |
| 501 | public void releaseRenderer() { |
| 502 | synchronized (this) { |
| 503 | if (mRenderer != null) { |
| 504 | // Invalidate the current content bounds. |
| 505 | mRenderer.setContentDrawBounds(0, 0, 0, 0); |
| 506 | |
Wale Ogunwale | 25cd08c | 2015-10-08 10:09:28 -0700 | [diff] [blame] | 507 | // Remove the render nodes again |
| 508 | // (see comment above - better to do that only once). |
Skuhne | b816087 | 2015-09-22 09:51:39 -0700 | [diff] [blame] | 509 | mRenderer.removeRenderNode(mFrameNode); |
| 510 | mRenderer.removeRenderNode(mBackdropNode); |
| 511 | |
| 512 | mRenderer = null; |
| 513 | |
| 514 | // Exit the renderer loop. |
| 515 | pingRenderLocked(); |
| 516 | } |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | @Override |
| 521 | public void run() { |
| 522 | try { |
| 523 | Looper.prepare(); |
| 524 | mChoreographer = Choreographer.getInstance(); |
| 525 | Looper.loop(); |
| 526 | } finally { |
| 527 | releaseRenderer(); |
| 528 | } |
| 529 | synchronized (this) { |
| 530 | // Make sure no more messages are being sent. |
| 531 | mChoreographer = null; |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | /** |
| 536 | * The implementation of the FrameCallback. |
| 537 | * @param frameTimeNanos The time in nanoseconds when the frame started being rendered, |
| 538 | * in the {@link System#nanoTime()} timebase. Divide this value by {@code 1000000} |
| 539 | */ |
| 540 | @Override |
| 541 | public void doFrame(long frameTimeNanos) { |
Skuhne | b816087 | 2015-09-22 09:51:39 -0700 | [diff] [blame] | 542 | synchronized (this) { |
Wale Ogunwale | 25cd08c | 2015-10-08 10:09:28 -0700 | [diff] [blame] | 543 | if (mRenderer == null) { |
Chong Zhang | 8dbd9ad | 2015-10-09 10:06:11 -0700 | [diff] [blame] | 544 | reportDrawIfNeeded(); |
Wale Ogunwale | 25cd08c | 2015-10-08 10:09:28 -0700 | [diff] [blame] | 545 | // Tell the looper to stop. We are done. |
| 546 | Looper.myLooper().quit(); |
| 547 | return; |
| 548 | } |
Skuhne | b816087 | 2015-09-22 09:51:39 -0700 | [diff] [blame] | 549 | mNewTargetRect.set(mTargetRect); |
Chong Zhang | 8dbd9ad | 2015-10-09 10:06:11 -0700 | [diff] [blame] | 550 | if (!mNewTargetRect.equals(mOldTargetRect) || mReportNextDraw) { |
Wale Ogunwale | 25cd08c | 2015-10-08 10:09:28 -0700 | [diff] [blame] | 551 | mOldTargetRect.set(mNewTargetRect); |
| 552 | changeWindowSizeLocked(mNewTargetRect); |
| 553 | } |
Skuhne | b816087 | 2015-09-22 09:51:39 -0700 | [diff] [blame] | 554 | } |
| 555 | } |
| 556 | |
| 557 | /** |
Skuhne | 980ee47 | 2015-10-06 11:31:31 -0700 | [diff] [blame] | 558 | * The content is about to be drawn and we got the location of where it will be shown. |
Wale Ogunwale | 25cd08c | 2015-10-08 10:09:28 -0700 | [diff] [blame] | 559 | * If a "changeWindowSizeLocked" call has already been processed, we will re-issue the call |
Skuhne | 980ee47 | 2015-10-06 11:31:31 -0700 | [diff] [blame] | 560 | * if the previous call was ignored since the size was unknown. |
| 561 | * @param xOffset The x offset where the content is drawn to. |
| 562 | * @param yOffset The y offset where the content is drawn to. |
| 563 | * @param xSize The width size of the content. This should not be 0. |
| 564 | * @param ySize The height of the content. |
Chong Zhang | 8dbd9ad | 2015-10-09 10:06:11 -0700 | [diff] [blame] | 565 | * @return true if a frame should be requested after the content is drawn; false otherwise. |
Skuhne | 980ee47 | 2015-10-06 11:31:31 -0700 | [diff] [blame] | 566 | */ |
Chong Zhang | 8dbd9ad | 2015-10-09 10:06:11 -0700 | [diff] [blame] | 567 | public boolean onContentDrawn(int xOffset, int yOffset, int xSize, int ySize) { |
Skuhne | 980ee47 | 2015-10-06 11:31:31 -0700 | [diff] [blame] | 568 | synchronized (this) { |
| 569 | final boolean firstCall = mLastContentWidth == 0; |
| 570 | // The current content buffer is drawn here. |
| 571 | mLastContentWidth = xSize; |
| 572 | mLastContentHeight = ySize - mLastCaptionHeight; |
| 573 | mLastXOffset = xOffset; |
| 574 | mLastYOffset = yOffset; |
| 575 | |
| 576 | mRenderer.setContentDrawBounds( |
| 577 | mLastXOffset, |
| 578 | mLastYOffset + mLastCaptionHeight, |
| 579 | mLastXOffset + mLastContentWidth, |
| 580 | mLastYOffset + mLastCaptionHeight + mLastContentHeight); |
Wale Ogunwale | 25cd08c | 2015-10-08 10:09:28 -0700 | [diff] [blame] | 581 | // If this was the first call and changeWindowSizeLocked got already called prior |
| 582 | // to us, we should re-issue a changeWindowSizeLocked now. |
Chong Zhang | 8dbd9ad | 2015-10-09 10:06:11 -0700 | [diff] [blame] | 583 | return firstCall && (mLastCaptionHeight != 0 || !mShowDecor); |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | public void onRequsetDraw(boolean reportNextDraw) { |
| 588 | synchronized (this) { |
| 589 | mReportNextDraw = reportNextDraw; |
| 590 | mOldTargetRect.set(0, 0, 0, 0); |
| 591 | pingRenderLocked(); |
Skuhne | 980ee47 | 2015-10-06 11:31:31 -0700 | [diff] [blame] | 592 | } |
| 593 | } |
| 594 | |
| 595 | /** |
Skuhne | b816087 | 2015-09-22 09:51:39 -0700 | [diff] [blame] | 596 | * Resizing the frame to fit the new window size. |
| 597 | * @param newBounds The window bounds which needs to be drawn. |
| 598 | */ |
Wale Ogunwale | 25cd08c | 2015-10-08 10:09:28 -0700 | [diff] [blame] | 599 | private void changeWindowSizeLocked(Rect newBounds) { |
Skuhne | b816087 | 2015-09-22 09:51:39 -0700 | [diff] [blame] | 600 | // While a configuration change is taking place the view hierarchy might become |
| 601 | // inaccessible. For that case we remember the previous metrics to avoid flashes. |
Skuhne | 980ee47 | 2015-10-06 11:31:31 -0700 | [diff] [blame] | 602 | // Note that even when there is no visible caption, the caption child will exist. |
Skuhne | b816087 | 2015-09-22 09:51:39 -0700 | [diff] [blame] | 603 | View caption = getChildAt(0); |
Skuhne | 980ee47 | 2015-10-06 11:31:31 -0700 | [diff] [blame] | 604 | if (caption != null) { |
| 605 | final int captionHeight = caption.getHeight(); |
| 606 | // The caption height will probably never dynamically change while we are resizing. |
| 607 | // Once set to something other then 0 it should be kept that way. |
| 608 | if (captionHeight != 0) { |
| 609 | // Remember the height of the caption. |
Skuhne | cc075c7 | 2015-10-02 16:08:26 -0700 | [diff] [blame] | 610 | mLastCaptionHeight = captionHeight; |
Skuhne | cc075c7 | 2015-10-02 16:08:26 -0700 | [diff] [blame] | 611 | } |
Skuhne | b816087 | 2015-09-22 09:51:39 -0700 | [diff] [blame] | 612 | } |
Skuhne | 980ee47 | 2015-10-06 11:31:31 -0700 | [diff] [blame] | 613 | // Make sure that the other thread has already prepared the render draw calls for the |
| 614 | // content. If any size is 0, we have to wait for it to be drawn first. |
| 615 | if ((mLastCaptionHeight == 0 && mShowDecor) || |
| 616 | mLastContentWidth == 0 || mLastContentHeight == 0) { |
| 617 | return; |
| 618 | } |
Skuhne | b816087 | 2015-09-22 09:51:39 -0700 | [diff] [blame] | 619 | // Since the surface is spanning the entire screen, we have to add the start offset of |
| 620 | // the bounds to get to the surface location. |
| 621 | final int left = mLastXOffset + newBounds.left; |
| 622 | final int top = mLastYOffset + newBounds.top; |
| 623 | final int width = newBounds.width(); |
| 624 | final int height = newBounds.height(); |
| 625 | |
| 626 | // Produce the draw calls. |
| 627 | // TODO(skuhne): Create a separate caption view which draws this. If the shadow should |
| 628 | // be resized while the window resizes, this hierarchy needs to have the elevation. |
| 629 | // That said - it is probably no good idea to draw the shadow every time since it costs |
| 630 | // a considerable time which we should rather spend for resizing the content and it does |
| 631 | // barely show while the entire screen is moving. |
| 632 | mFrameNode.setLeftTopRightBottom(left, top, left + width, top + mLastCaptionHeight); |
| 633 | DisplayListCanvas canvas = mFrameNode.start(width, height); |
Wale Ogunwale | f3a62fb | 2015-10-14 17:07:29 -0700 | [diff] [blame] | 634 | mCaptionBackgroundDrawable.setBounds(0, 0, left + width, top + mLastCaptionHeight); |
| 635 | mCaptionBackgroundDrawable.draw(canvas); |
Skuhne | b816087 | 2015-09-22 09:51:39 -0700 | [diff] [blame] | 636 | mFrameNode.end(canvas); |
| 637 | |
| 638 | mBackdropNode.setLeftTopRightBottom(left, top + mLastCaptionHeight, left + width, |
| 639 | top + height); |
| 640 | |
| 641 | // The backdrop: clear everything with the background. Clipping is done elsewhere. |
| 642 | canvas = mBackdropNode.start(width, height - mLastCaptionHeight); |
Wale Ogunwale | f3a62fb | 2015-10-14 17:07:29 -0700 | [diff] [blame] | 643 | mResizingBackgroundDrawable.setBounds(0, 0, left + width, top + height); |
| 644 | mResizingBackgroundDrawable.draw(canvas); |
Skuhne | b816087 | 2015-09-22 09:51:39 -0700 | [diff] [blame] | 645 | mBackdropNode.end(canvas); |
| 646 | |
Skuhne | b816087 | 2015-09-22 09:51:39 -0700 | [diff] [blame] | 647 | // We need to render both rendered nodes explicitly. |
| 648 | mRenderer.drawRenderNode(mFrameNode); |
| 649 | mRenderer.drawRenderNode(mBackdropNode); |
Chong Zhang | 8dbd9ad | 2015-10-09 10:06:11 -0700 | [diff] [blame] | 650 | |
| 651 | reportDrawIfNeeded(); |
| 652 | } |
| 653 | |
| 654 | /** |
| 655 | * Notify view root that a frame has been drawn by us, if it has requested so. |
| 656 | */ |
| 657 | private void reportDrawIfNeeded() { |
| 658 | if (mReportNextDraw) { |
| 659 | if (isAttachedToWindow()) { |
| 660 | getViewRootImpl().reportDrawFinish(); |
| 661 | } |
| 662 | mReportNextDraw = false; |
| 663 | } |
Skuhne | b816087 | 2015-09-22 09:51:39 -0700 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | /** |
| 667 | * Sends a message to the renderer to wake up and perform the next action which can be |
| 668 | * either the next rendering or the self destruction if mRenderer is null. |
| 669 | * Note: This call must be synchronized. |
| 670 | */ |
| 671 | private void pingRenderLocked() { |
| 672 | if (mChoreographer != null) { |
| 673 | mChoreographer.postFrameCallback(this); |
| 674 | } |
| 675 | } |
| 676 | } |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 677 | } |