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; |
| 22 | import android.os.RemoteException; |
| 23 | import android.util.AttributeSet; |
Skuhne | 81c524a | 2015-08-12 13:34:14 -0700 | [diff] [blame] | 24 | import android.view.MotionEvent; |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 25 | import android.view.View; |
| 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; |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 30 | |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 31 | import com.android.internal.R; |
| 32 | import com.android.internal.policy.PhoneWindow; |
| 33 | |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 34 | /** |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 35 | * This class represents the special screen elements to control a window on freeform |
| 36 | * environment. |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 37 | * As such this class handles the following things: |
| 38 | * <ul> |
| 39 | * <li>The caption, containing the system buttons like maximize, close and such as well as |
| 40 | * allowing the user to drag the window around.</li> |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 41 | * After creating the view, the function |
Wale Ogunwale | f3a62fb | 2015-10-14 17:07:29 -0700 | [diff] [blame] | 42 | * {@link #setPhoneWindow} needs to be called to make |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 43 | * the connection to it's owning PhoneWindow. |
| 44 | * Note: At this time the application can change various attributes of the DecorView which |
| 45 | * will break things (in settle/unexpected ways): |
| 46 | * <ul> |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 47 | * <li>setOutlineProvider</li> |
| 48 | * <li>setSurfaceFormat</li> |
| 49 | * <li>..</li> |
| 50 | * </ul> |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 51 | */ |
Filip Gruszczynski | 6325065 | 2015-11-18 14:43:01 -0800 | [diff] [blame] | 52 | public class DecorCaptionView extends ViewGroup |
Wale Ogunwale | bf9eefc | 2015-11-17 14:47:52 -0800 | [diff] [blame] | 53 | implements View.OnClickListener, View.OnTouchListener { |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 54 | private final static String TAG = "DecorCaptionView"; |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 55 | private PhoneWindow mOwner = null; |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 56 | private boolean mShow = false; |
Skuhne | 81c524a | 2015-08-12 13:34:14 -0700 | [diff] [blame] | 57 | |
| 58 | // True if the window is being dragged. |
| 59 | private boolean mDragging = false; |
| 60 | |
Skuhne | 81c524a | 2015-08-12 13:34:14 -0700 | [diff] [blame] | 61 | // True when the left mouse button got released while dragging. |
| 62 | private boolean mLeftMouseButtonReleased; |
| 63 | |
Filip Gruszczynski | 6325065 | 2015-11-18 14:43:01 -0800 | [diff] [blame] | 64 | private boolean mOverlayWithAppContent = false; |
| 65 | |
| 66 | private View mCaption; |
| 67 | private View mContent; |
| 68 | |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 69 | public DecorCaptionView(Context context) { |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 70 | super(context); |
| 71 | } |
| 72 | |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 73 | public DecorCaptionView(Context context, AttributeSet attrs) { |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 74 | super(context, attrs); |
| 75 | } |
| 76 | |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 77 | public DecorCaptionView(Context context, AttributeSet attrs, int defStyle) { |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 78 | super(context, attrs, defStyle); |
| 79 | } |
| 80 | |
Filip Gruszczynski | 6325065 | 2015-11-18 14:43:01 -0800 | [diff] [blame] | 81 | @Override |
| 82 | protected void onFinishInflate() { |
| 83 | super.onFinishInflate(); |
| 84 | mCaption = getChildAt(0); |
| 85 | } |
| 86 | |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 87 | public void setPhoneWindow(PhoneWindow owner, boolean show) { |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 88 | mOwner = owner; |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 89 | mShow = show; |
Filip Gruszczynski | 6325065 | 2015-11-18 14:43:01 -0800 | [diff] [blame] | 90 | mOverlayWithAppContent = owner.getOverlayDecorCaption(); |
Skuhne | f7b882c | 2015-08-11 17:18:58 -0700 | [diff] [blame] | 91 | updateCaptionVisibility(); |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 92 | // By changing the outline provider to BOUNDS, the window can remove its |
| 93 | // background without removing the shadow. |
| 94 | mOwner.getDecorView().setOutlineProvider(ViewOutlineProvider.BOUNDS); |
Skuhne | b816087 | 2015-09-22 09:51:39 -0700 | [diff] [blame] | 95 | |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 96 | findViewById(R.id.maximize_window).setOnClickListener(this); |
| 97 | findViewById(R.id.close_window).setOnClickListener(this); |
| 98 | } |
| 99 | |
Skuhne | 81c524a | 2015-08-12 13:34:14 -0700 | [diff] [blame] | 100 | @Override |
Chong Zhang | 509ea6b | 2015-09-30 14:09:52 -0700 | [diff] [blame] | 101 | public boolean onTouch(View v, MotionEvent e) { |
Skuhne | 81c524a | 2015-08-12 13:34:14 -0700 | [diff] [blame] | 102 | // Note: There are no mixed events. When a new device gets used (e.g. 1. Mouse, 2. touch) |
| 103 | // the old input device events get cancelled first. So no need to remember the kind of |
| 104 | // input device we are listening to. |
| 105 | switch (e.getActionMasked()) { |
| 106 | case MotionEvent.ACTION_DOWN: |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 107 | if (!mShow) { |
| 108 | // When there is no caption we should not react to anything. |
Skuhne | a635a26 | 2015-08-26 15:45:58 -0700 | [diff] [blame] | 109 | return false; |
| 110 | } |
Skuhne | 81c524a | 2015-08-12 13:34:14 -0700 | [diff] [blame] | 111 | // A drag action is started if we aren't dragging already and the starting event is |
| 112 | // either a left mouse button or any other input device. |
| 113 | if (!mDragging && |
| 114 | (e.getToolType(e.getActionIndex()) != MotionEvent.TOOL_TYPE_MOUSE || |
| 115 | (e.getButtonState() & MotionEvent.BUTTON_PRIMARY) != 0)) { |
| 116 | mDragging = true; |
Skuhne | 81c524a | 2015-08-12 13:34:14 -0700 | [diff] [blame] | 117 | mLeftMouseButtonReleased = false; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 118 | startMovingTask(e.getRawX(), e.getRawY()); |
Skuhne | 81c524a | 2015-08-12 13:34:14 -0700 | [diff] [blame] | 119 | } |
| 120 | break; |
| 121 | |
| 122 | case MotionEvent.ACTION_MOVE: |
| 123 | if (mDragging && !mLeftMouseButtonReleased) { |
| 124 | if (e.getToolType(e.getActionIndex()) == MotionEvent.TOOL_TYPE_MOUSE && |
| 125 | (e.getButtonState() & MotionEvent.BUTTON_PRIMARY) == 0) { |
| 126 | // There is no separate mouse button up call and if the user mixes mouse |
| 127 | // button drag actions, we stop dragging once he releases the button. |
| 128 | mLeftMouseButtonReleased = true; |
| 129 | break; |
| 130 | } |
Skuhne | 81c524a | 2015-08-12 13:34:14 -0700 | [diff] [blame] | 131 | } |
| 132 | break; |
| 133 | |
| 134 | case MotionEvent.ACTION_UP: |
Skuhne | 81c524a | 2015-08-12 13:34:14 -0700 | [diff] [blame] | 135 | case MotionEvent.ACTION_CANCEL: |
Skuhne | a5a93ee | 2015-08-20 15:43:57 -0700 | [diff] [blame] | 136 | if (!mDragging) { |
| 137 | break; |
Skuhne | 81c524a | 2015-08-12 13:34:14 -0700 | [diff] [blame] | 138 | } |
Skuhne | a5a93ee | 2015-08-20 15:43:57 -0700 | [diff] [blame] | 139 | // Abort the ongoing dragging. |
| 140 | mDragging = false; |
Skuhne | a5a93ee | 2015-08-20 15:43:57 -0700 | [diff] [blame] | 141 | return true; |
Skuhne | 81c524a | 2015-08-12 13:34:14 -0700 | [diff] [blame] | 142 | } |
| 143 | return mDragging; |
| 144 | } |
| 145 | |
Stefan Kuhne | f4dd71a | 2015-08-07 09:28:52 -0700 | [diff] [blame] | 146 | /** |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 147 | * The phone window configuration has changed and the caption needs to be updated. |
| 148 | * @param show True if the caption should be shown. |
Wale Ogunwale | 2b547c3 | 2015-11-18 10:33:22 -0800 | [diff] [blame] | 149 | */ |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 150 | public void onConfigurationChanged(boolean show) { |
| 151 | mShow = show; |
Skuhne | f7b882c | 2015-08-11 17:18:58 -0700 | [diff] [blame] | 152 | updateCaptionVisibility(); |
Stefan Kuhne | f4dd71a | 2015-08-07 09:28:52 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 155 | @Override |
| 156 | public void onClick(View view) { |
| 157 | if (view.getId() == R.id.maximize_window) { |
Stefan Kuhne | 1b42057 | 2015-08-07 10:50:19 -0700 | [diff] [blame] | 158 | maximizeWindow(); |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 159 | } else if (view.getId() == R.id.close_window) { |
Stefan Kuhne | 1b42057 | 2015-08-07 10:50:19 -0700 | [diff] [blame] | 160 | mOwner.dispatchOnWindowDismissed(true /*finishTask*/); |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | |
| 164 | @Override |
Skuhne | f7b882c | 2015-08-11 17:18:58 -0700 | [diff] [blame] | 165 | public void addView(View child, int index, ViewGroup.LayoutParams params) { |
Filip Gruszczynski | 6325065 | 2015-11-18 14:43:01 -0800 | [diff] [blame] | 166 | if (!(params instanceof MarginLayoutParams)) { |
| 167 | throw new IllegalArgumentException( |
| 168 | "params " + params + " must subclass MarginLayoutParams"); |
| 169 | } |
Stefan Kuhne | f4dd71a | 2015-08-07 09:28:52 -0700 | [diff] [blame] | 170 | // 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] | 171 | if (index >= 2 || getChildCount() >= 2) { |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 172 | throw new IllegalStateException("DecorCaptionView can only handle 1 client view"); |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 173 | } |
Filip Gruszczynski | 6325065 | 2015-11-18 14:43:01 -0800 | [diff] [blame] | 174 | // To support the overlaying content in the caption, we need to put the content view as the |
| 175 | // first child to get the right Z-Ordering. |
| 176 | super.addView(child, 0, params); |
| 177 | mContent = child; |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Filip Gruszczynski | 6325065 | 2015-11-18 14:43:01 -0800 | [diff] [blame] | 180 | @Override |
| 181 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 182 | final int captionHeight; |
| 183 | if (mCaption.getVisibility() != View.GONE) { |
| 184 | measureChildWithMargins(mCaption, widthMeasureSpec, 0, heightMeasureSpec, 0); |
| 185 | captionHeight = mCaption.getMeasuredHeight(); |
| 186 | } else { |
| 187 | captionHeight = 0; |
| 188 | } |
| 189 | if (mContent != null) { |
| 190 | if (mOverlayWithAppContent) { |
| 191 | measureChildWithMargins(mContent, widthMeasureSpec, 0, heightMeasureSpec, 0); |
| 192 | } else { |
| 193 | measureChildWithMargins(mContent, widthMeasureSpec, 0, heightMeasureSpec, |
| 194 | captionHeight); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), |
| 199 | MeasureSpec.getSize(heightMeasureSpec)); |
| 200 | } |
| 201 | |
| 202 | @Override |
| 203 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 204 | final int captionHeight; |
| 205 | if (mCaption.getVisibility() != View.GONE) { |
| 206 | mCaption.layout(0, 0, mCaption.getMeasuredWidth(), mCaption.getMeasuredHeight()); |
| 207 | captionHeight = mCaption.getBottom() - mCaption.getTop(); |
| 208 | } else { |
| 209 | captionHeight = 0; |
| 210 | } |
| 211 | |
| 212 | if (mContent != null) { |
| 213 | if (mOverlayWithAppContent) { |
| 214 | mContent.layout(0, 0, mContent.getMeasuredWidth(), mContent.getMeasuredHeight()); |
| 215 | } else { |
| 216 | mContent.layout(0, captionHeight, mContent.getMeasuredWidth(), |
| 217 | captionHeight + mContent.getMeasuredHeight()); |
| 218 | } |
| 219 | } |
| 220 | } |
Stefan Kuhne | f4dd71a | 2015-08-07 09:28:52 -0700 | [diff] [blame] | 221 | /** |
| 222 | * Determine if the workspace is entirely covered by the window. |
| 223 | * @return Returns true when the window is filling the entire screen/workspace. |
| 224 | **/ |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 225 | private boolean isFillingScreen() { |
| 226 | return (0 != ((getWindowSystemUiVisibility() | getSystemUiVisibility()) & |
| 227 | (View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | |
| 228 | View.SYSTEM_UI_FLAG_IMMERSIVE | View.SYSTEM_UI_FLAG_LOW_PROFILE))); |
| 229 | } |
| 230 | |
Stefan Kuhne | f4dd71a | 2015-08-07 09:28:52 -0700 | [diff] [blame] | 231 | /** |
Skuhne | f7b882c | 2015-08-11 17:18:58 -0700 | [diff] [blame] | 232 | * Updates the visibility of the caption. |
| 233 | **/ |
| 234 | private void updateCaptionVisibility() { |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 235 | // Don't show the caption if the window has e.g. entered full screen. |
| 236 | boolean invisible = isFillingScreen() || !mShow; |
Filip Gruszczynski | 6325065 | 2015-11-18 14:43:01 -0800 | [diff] [blame] | 237 | mCaption.setVisibility(invisible ? GONE : VISIBLE); |
| 238 | mCaption.setOnTouchListener(this); |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 239 | } |
Stefan Kuhne | 1b42057 | 2015-08-07 10:50:19 -0700 | [diff] [blame] | 240 | |
Stefan Kuhne | f4dd71a | 2015-08-07 09:28:52 -0700 | [diff] [blame] | 241 | /** |
| 242 | * Maximize the window by moving it to the maximized workspace stack. |
| 243 | **/ |
Stefan Kuhne | 1b42057 | 2015-08-07 10:50:19 -0700 | [diff] [blame] | 244 | private void maximizeWindow() { |
Skuhne | ce2faa5 | 2015-08-11 10:36:38 -0700 | [diff] [blame] | 245 | Window.WindowControllerCallback callback = mOwner.getWindowControllerCallback(); |
Stefan Kuhne | 1b42057 | 2015-08-07 10:50:19 -0700 | [diff] [blame] | 246 | if (callback != null) { |
| 247 | try { |
Wale Ogunwale | 3797c22 | 2015-10-27 14:21:58 -0700 | [diff] [blame] | 248 | callback.changeWindowStack(FULLSCREEN_WORKSPACE_STACK_ID); |
Stefan Kuhne | 1b42057 | 2015-08-07 10:50:19 -0700 | [diff] [blame] | 249 | } catch (RemoteException ex) { |
| 250 | Log.e(TAG, "Cannot change task workspace."); |
| 251 | } |
| 252 | } |
| 253 | } |
Wale Ogunwale | 8cc5a74 | 2015-11-17 15:41:05 -0800 | [diff] [blame] | 254 | |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 255 | public boolean isCaptionShowing() { |
| 256 | return mShow; |
Wale Ogunwale | 8cc5a74 | 2015-11-17 15:41:05 -0800 | [diff] [blame] | 257 | } |
| 258 | |
Wale Ogunwale | 62a91d6 | 2015-11-18 11:44:10 -0800 | [diff] [blame] | 259 | public int getCaptionHeight() { |
Filip Gruszczynski | 6325065 | 2015-11-18 14:43:01 -0800 | [diff] [blame] | 260 | return (mCaption != null) ? mCaption.getHeight() : 0; |
| 261 | } |
| 262 | |
| 263 | public void removeContentView() { |
| 264 | if (mContent != null) { |
| 265 | removeView(mContent); |
| 266 | mContent = null; |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | public View getCaption() { |
| 271 | return mCaption; |
| 272 | } |
| 273 | |
| 274 | @Override |
| 275 | public LayoutParams generateLayoutParams(AttributeSet attrs) { |
| 276 | return new MarginLayoutParams(getContext(), attrs); |
| 277 | } |
| 278 | |
| 279 | @Override |
| 280 | protected LayoutParams generateDefaultLayoutParams() { |
| 281 | return new MarginLayoutParams(MarginLayoutParams.MATCH_PARENT, |
| 282 | MarginLayoutParams.MATCH_PARENT); |
| 283 | } |
| 284 | |
| 285 | @Override |
| 286 | protected LayoutParams generateLayoutParams(LayoutParams p) { |
| 287 | return new MarginLayoutParams(p); |
| 288 | } |
| 289 | |
| 290 | @Override |
| 291 | protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { |
| 292 | return p instanceof MarginLayoutParams; |
Wale Ogunwale | 8cc5a74 | 2015-11-17 15:41:05 -0800 | [diff] [blame] | 293 | } |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 294 | } |