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 | |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 19 | import android.content.Context; |
Skuhne | ce2faa5 | 2015-08-11 10:36:38 -0700 | [diff] [blame] | 20 | import android.graphics.Rect; |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 21 | import android.os.RemoteException; |
| 22 | import android.util.AttributeSet; |
Skuhne | 81c524a | 2015-08-12 13:34:14 -0700 | [diff] [blame] | 23 | import android.view.MotionEvent; |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 24 | import android.view.View; |
Skuhne | f7b882c | 2015-08-11 17:18:58 -0700 | [diff] [blame] | 25 | import android.widget.LinearLayout; |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 26 | import android.view.ViewGroup; |
Stefan Kuhne | f4dd71a | 2015-08-07 09:28:52 -0700 | [diff] [blame] | 27 | import android.view.ViewOutlineProvider; |
| 28 | import android.view.Window; |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 29 | import android.util.Log; |
| 30 | import android.util.TypedValue; |
| 31 | |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 32 | import com.android.internal.R; |
| 33 | import com.android.internal.policy.PhoneWindow; |
| 34 | |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 35 | /** |
| 36 | * This class represents the special screen elements to control a window on free form |
| 37 | * environment. All thse screen elements are added in the "non client area" which is the area of |
| 38 | * the window which is handled by the OS and not the application. |
| 39 | * As such this class handles the following things: |
| 40 | * <ul> |
| 41 | * <li>The caption, containing the system buttons like maximize, close and such as well as |
| 42 | * allowing the user to drag the window around.</li> |
| 43 | * <li>The shadow - which is changing dependent on the window focus.</li> |
| 44 | * <li>The border around the client area (if there is one).</li> |
| 45 | * <li>The resize handles which allow to resize the window.</li> |
| 46 | * </ul> |
| 47 | * After creating the view, the function |
| 48 | * {@link #setPhoneWindow(PhoneWindow owner, boolean windowHasShadow)} needs to be called to make |
| 49 | * the connection to it's owning PhoneWindow. |
| 50 | * Note: At this time the application can change various attributes of the DecorView which |
| 51 | * will break things (in settle/unexpected ways): |
| 52 | * <ul> |
| 53 | * <li>setElevation</li> |
| 54 | * <li>setOutlineProvider</li> |
| 55 | * <li>setSurfaceFormat</li> |
| 56 | * <li>..</li> |
| 57 | * </ul> |
| 58 | * This will be mitigated once b/22527834 will be addressed. |
| 59 | */ |
Skuhne | f7b882c | 2015-08-11 17:18:58 -0700 | [diff] [blame] | 60 | public class NonClientDecorView extends LinearLayout implements View.OnClickListener { |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 61 | private final static String TAG = "NonClientDecorView"; |
| 62 | // The height of a window which has focus in DIP. |
| 63 | private final int DECOR_SHADOW_FOCUSED_HEIGHT_IN_DIP = 20; |
| 64 | // The height of a window which has not in DIP. |
| 65 | private final int DECOR_SHADOW_UNFOCUSED_HEIGHT_IN_DIP = 5; |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 66 | private PhoneWindow mOwner = null; |
Skuhne | 81c524a | 2015-08-12 13:34:14 -0700 | [diff] [blame] | 67 | private boolean mWindowHasShadow = false; |
| 68 | private boolean mShowDecor = false; |
| 69 | |
| 70 | // True if the window is being dragged. |
| 71 | private boolean mDragging = false; |
| 72 | |
Skuhne | 81c524a | 2015-08-12 13:34:14 -0700 | [diff] [blame] | 73 | // True when the left mouse button got released while dragging. |
| 74 | private boolean mLeftMouseButtonReleased; |
| 75 | |
Skuhne | a5a93ee | 2015-08-20 15:43:57 -0700 | [diff] [blame] | 76 | // 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^] | 77 | public boolean mVisible = false; |
Skuhne | a5a93ee | 2015-08-20 15:43:57 -0700 | [diff] [blame] | 78 | |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 79 | // The current focus state of the window for updating the window elevation. |
Skuhne | 81c524a | 2015-08-12 13:34:14 -0700 | [diff] [blame] | 80 | private boolean mWindowHasFocus = true; |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 81 | |
| 82 | // Cludge to address b/22668382: Set the shadow size to the maximum so that the layer |
| 83 | // size calculation takes the shadow size into account. We set the elevation currently |
| 84 | // to max until the first layout command has been executed. |
Skuhne | 81c524a | 2015-08-12 13:34:14 -0700 | [diff] [blame] | 85 | private boolean mAllowUpdateElevation = false; |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 86 | |
| 87 | public NonClientDecorView(Context context) { |
| 88 | super(context); |
| 89 | } |
| 90 | |
| 91 | public NonClientDecorView(Context context, AttributeSet attrs) { |
| 92 | super(context, attrs); |
| 93 | } |
| 94 | |
| 95 | public NonClientDecorView(Context context, AttributeSet attrs, int defStyle) { |
| 96 | super(context, attrs, defStyle); |
| 97 | } |
| 98 | |
Stefan Kuhne | f4dd71a | 2015-08-07 09:28:52 -0700 | [diff] [blame] | 99 | public void setPhoneWindow(PhoneWindow owner, boolean showDecor, boolean windowHasShadow) { |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 100 | mOwner = owner; |
| 101 | mWindowHasShadow = windowHasShadow; |
Stefan Kuhne | f4dd71a | 2015-08-07 09:28:52 -0700 | [diff] [blame] | 102 | mShowDecor = showDecor; |
Skuhne | f7b882c | 2015-08-11 17:18:58 -0700 | [diff] [blame] | 103 | updateCaptionVisibility(); |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 104 | if (mWindowHasShadow) { |
Stefan Kuhne | f4dd71a | 2015-08-07 09:28:52 -0700 | [diff] [blame] | 105 | initializeElevation(); |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 106 | } |
| 107 | // By changing the outline provider to BOUNDS, the window can remove its |
| 108 | // background without removing the shadow. |
| 109 | mOwner.getDecorView().setOutlineProvider(ViewOutlineProvider.BOUNDS); |
| 110 | findViewById(R.id.maximize_window).setOnClickListener(this); |
| 111 | findViewById(R.id.close_window).setOnClickListener(this); |
| 112 | } |
| 113 | |
Skuhne | 81c524a | 2015-08-12 13:34:14 -0700 | [diff] [blame] | 114 | @Override |
| 115 | public boolean onTouchEvent(MotionEvent e) { |
| 116 | // Note: There are no mixed events. When a new device gets used (e.g. 1. Mouse, 2. touch) |
| 117 | // the old input device events get cancelled first. So no need to remember the kind of |
| 118 | // input device we are listening to. |
| 119 | switch (e.getActionMasked()) { |
| 120 | case MotionEvent.ACTION_DOWN: |
Skuhne | a635a26 | 2015-08-26 15:45:58 -0700 | [diff] [blame] | 121 | if (!mShowDecor) { |
| 122 | // When there is no decor we should not react to anything. |
| 123 | return false; |
| 124 | } |
Skuhne | 81c524a | 2015-08-12 13:34:14 -0700 | [diff] [blame] | 125 | // A drag action is started if we aren't dragging already and the starting event is |
| 126 | // either a left mouse button or any other input device. |
| 127 | if (!mDragging && |
| 128 | (e.getToolType(e.getActionIndex()) != MotionEvent.TOOL_TYPE_MOUSE || |
| 129 | (e.getButtonState() & MotionEvent.BUTTON_PRIMARY) != 0)) { |
| 130 | mDragging = true; |
Skuhne | 81c524a | 2015-08-12 13:34:14 -0700 | [diff] [blame] | 131 | mLeftMouseButtonReleased = false; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame^] | 132 | startMovingTask(e.getRawX(), e.getRawY()); |
Skuhne | 81c524a | 2015-08-12 13:34:14 -0700 | [diff] [blame] | 133 | } |
| 134 | break; |
| 135 | |
| 136 | case MotionEvent.ACTION_MOVE: |
| 137 | if (mDragging && !mLeftMouseButtonReleased) { |
| 138 | if (e.getToolType(e.getActionIndex()) == MotionEvent.TOOL_TYPE_MOUSE && |
| 139 | (e.getButtonState() & MotionEvent.BUTTON_PRIMARY) == 0) { |
| 140 | // There is no separate mouse button up call and if the user mixes mouse |
| 141 | // button drag actions, we stop dragging once he releases the button. |
| 142 | mLeftMouseButtonReleased = true; |
| 143 | break; |
| 144 | } |
Skuhne | 81c524a | 2015-08-12 13:34:14 -0700 | [diff] [blame] | 145 | } |
| 146 | break; |
| 147 | |
| 148 | case MotionEvent.ACTION_UP: |
Skuhne | 81c524a | 2015-08-12 13:34:14 -0700 | [diff] [blame] | 149 | case MotionEvent.ACTION_CANCEL: |
Skuhne | a5a93ee | 2015-08-20 15:43:57 -0700 | [diff] [blame] | 150 | if (!mDragging) { |
| 151 | break; |
Skuhne | 81c524a | 2015-08-12 13:34:14 -0700 | [diff] [blame] | 152 | } |
Skuhne | a5a93ee | 2015-08-20 15:43:57 -0700 | [diff] [blame] | 153 | // Abort the ongoing dragging. |
| 154 | mDragging = false; |
Skuhne | a5a93ee | 2015-08-20 15:43:57 -0700 | [diff] [blame] | 155 | return true; |
Skuhne | 81c524a | 2015-08-12 13:34:14 -0700 | [diff] [blame] | 156 | } |
| 157 | return mDragging; |
| 158 | } |
| 159 | |
Stefan Kuhne | f4dd71a | 2015-08-07 09:28:52 -0700 | [diff] [blame] | 160 | /** |
| 161 | * The phone window configuration has changed and the decor needs to be updated. |
| 162 | * @param showDecor True if the decor should be shown. |
| 163 | * @param windowHasShadow True when the window should show a shadow. |
| 164 | **/ |
| 165 | public void phoneWindowUpdated(boolean showDecor, boolean windowHasShadow) { |
| 166 | mShowDecor = showDecor; |
Skuhne | f7b882c | 2015-08-11 17:18:58 -0700 | [diff] [blame] | 167 | updateCaptionVisibility(); |
Stefan Kuhne | f4dd71a | 2015-08-07 09:28:52 -0700 | [diff] [blame] | 168 | if (windowHasShadow != mWindowHasShadow) { |
| 169 | mWindowHasShadow = windowHasShadow; |
| 170 | initializeElevation(); |
| 171 | } |
| 172 | } |
| 173 | |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 174 | @Override |
| 175 | public void onClick(View view) { |
| 176 | if (view.getId() == R.id.maximize_window) { |
Stefan Kuhne | 1b42057 | 2015-08-07 10:50:19 -0700 | [diff] [blame] | 177 | maximizeWindow(); |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 178 | } else if (view.getId() == R.id.close_window) { |
Stefan Kuhne | 1b42057 | 2015-08-07 10:50:19 -0700 | [diff] [blame] | 179 | mOwner.dispatchOnWindowDismissed(true /*finishTask*/); |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
| 183 | @Override |
| 184 | public void onWindowFocusChanged(boolean hasWindowFocus) { |
| 185 | mWindowHasFocus = hasWindowFocus; |
| 186 | updateElevation(); |
| 187 | super.onWindowFocusChanged(hasWindowFocus); |
| 188 | } |
| 189 | |
| 190 | @Override |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 191 | 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] | 192 | // If the application changed its SystemUI metrics, we might also have to adapt |
| 193 | // our shadow elevation. |
| 194 | updateElevation(); |
| 195 | mAllowUpdateElevation = true; |
| 196 | |
Skuhne | f7b882c | 2015-08-11 17:18:58 -0700 | [diff] [blame] | 197 | super.onLayout(changed, left, top, right, bottom); |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 200 | @Override |
Skuhne | f7b882c | 2015-08-11 17:18:58 -0700 | [diff] [blame] | 201 | public void addView(View child, int index, ViewGroup.LayoutParams params) { |
Stefan Kuhne | f4dd71a | 2015-08-07 09:28:52 -0700 | [diff] [blame] | 202 | // 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] | 203 | if (index >= 2 || getChildCount() >= 2) { |
| 204 | throw new IllegalStateException("NonClientDecorView can only handle 1 client view"); |
| 205 | } |
| 206 | super.addView(child, index, params); |
| 207 | } |
| 208 | |
Stefan Kuhne | f4dd71a | 2015-08-07 09:28:52 -0700 | [diff] [blame] | 209 | /** |
| 210 | * Determine if the workspace is entirely covered by the window. |
| 211 | * @return Returns true when the window is filling the entire screen/workspace. |
| 212 | **/ |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 213 | private boolean isFillingScreen() { |
| 214 | return (0 != ((getWindowSystemUiVisibility() | getSystemUiVisibility()) & |
| 215 | (View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | |
| 216 | View.SYSTEM_UI_FLAG_IMMERSIVE | View.SYSTEM_UI_FLAG_LOW_PROFILE))); |
| 217 | } |
| 218 | |
Stefan Kuhne | f4dd71a | 2015-08-07 09:28:52 -0700 | [diff] [blame] | 219 | /** |
Skuhne | f7b882c | 2015-08-11 17:18:58 -0700 | [diff] [blame] | 220 | * Updates the visibility of the caption. |
| 221 | **/ |
| 222 | private void updateCaptionVisibility() { |
| 223 | // Don't show the decor if the window has e.g. entered full screen. |
| 224 | boolean invisible = isFillingScreen() || !mShowDecor; |
| 225 | View caption = getChildAt(0); |
| 226 | caption.setVisibility(invisible ? GONE : VISIBLE); |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame^] | 227 | mVisible = !invisible; |
Skuhne | f7b882c | 2015-08-11 17:18:58 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | /** |
Stefan Kuhne | f4dd71a | 2015-08-07 09:28:52 -0700 | [diff] [blame] | 231 | * The elevation gets set for the first time and the framework needs to be informed that |
| 232 | * the surface layer gets created with the shadow size in mind. |
| 233 | **/ |
| 234 | private void initializeElevation() { |
| 235 | // TODO(skuhne): Call setMaxElevation here accordingly after b/22668382 got fixed. |
| 236 | mAllowUpdateElevation = false; |
| 237 | if (mWindowHasShadow) { |
| 238 | updateElevation(); |
| 239 | } else { |
| 240 | mOwner.setElevation(0); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * The shadow height gets controlled by the focus to visualize highlighted windows. |
| 246 | * Note: This will overwrite application elevation properties. |
| 247 | * Note: Windows which have (temporarily) changed their attributes to cover the SystemUI |
| 248 | * will get no shadow as they are expected to be "full screen". |
| 249 | **/ |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 250 | private void updateElevation() { |
| 251 | float elevation = 0; |
| 252 | if (mWindowHasShadow) { |
| 253 | boolean fill = isFillingScreen(); |
| 254 | elevation = fill ? 0 : |
| 255 | (mWindowHasFocus ? DECOR_SHADOW_FOCUSED_HEIGHT_IN_DIP : |
| 256 | DECOR_SHADOW_UNFOCUSED_HEIGHT_IN_DIP); |
Stefan Kuhne | f4dd71a | 2015-08-07 09:28:52 -0700 | [diff] [blame] | 257 | // TODO(skuhne): Remove this if clause once b/22668382 got fixed. |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 258 | if (!mAllowUpdateElevation && !fill) { |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 259 | elevation = DECOR_SHADOW_FOCUSED_HEIGHT_IN_DIP; |
| 260 | } |
| 261 | // Convert the DP elevation into physical pixels. |
| 262 | elevation = dipToPx(elevation); |
| 263 | } |
| 264 | // Don't change the elevation if it didn't change since it can require some time. |
| 265 | if (mOwner.getDecorView().getElevation() != elevation) { |
| 266 | mOwner.setElevation(elevation); |
| 267 | } |
| 268 | } |
| 269 | |
Stefan Kuhne | f4dd71a | 2015-08-07 09:28:52 -0700 | [diff] [blame] | 270 | /** |
| 271 | * Converts a DIP measure into physical pixels. |
| 272 | * @param dip The dip value. |
| 273 | * @return Returns the number of pixels. |
| 274 | */ |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 275 | private float dipToPx(float dip) { |
| 276 | return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, |
| 277 | getResources().getDisplayMetrics()); |
| 278 | } |
Stefan Kuhne | 1b42057 | 2015-08-07 10:50:19 -0700 | [diff] [blame] | 279 | |
Stefan Kuhne | f4dd71a | 2015-08-07 09:28:52 -0700 | [diff] [blame] | 280 | /** |
| 281 | * Maximize the window by moving it to the maximized workspace stack. |
| 282 | **/ |
Stefan Kuhne | 1b42057 | 2015-08-07 10:50:19 -0700 | [diff] [blame] | 283 | private void maximizeWindow() { |
Skuhne | ce2faa5 | 2015-08-11 10:36:38 -0700 | [diff] [blame] | 284 | Window.WindowControllerCallback callback = mOwner.getWindowControllerCallback(); |
Stefan Kuhne | 1b42057 | 2015-08-07 10:50:19 -0700 | [diff] [blame] | 285 | if (callback != null) { |
| 286 | try { |
| 287 | callback.changeWindowStack( |
| 288 | android.app.ActivityManager.FULLSCREEN_WORKSPACE_STACK_ID); |
| 289 | } catch (RemoteException ex) { |
| 290 | Log.e(TAG, "Cannot change task workspace."); |
| 291 | } |
| 292 | } |
| 293 | } |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 294 | } |