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; |
Skuhne | f7b882c | 2015-08-11 17:18:58 -0700 | [diff] [blame] | 26 | import android.widget.LinearLayout; |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 27 | import android.view.ViewGroup; |
Stefan Kuhne | f4dd71a | 2015-08-07 09:28:52 -0700 | [diff] [blame] | 28 | import android.view.ViewOutlineProvider; |
| 29 | import android.view.Window; |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 30 | import android.util.Log; |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 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 |
Wale Ogunwale | 2b547c3 | 2015-11-18 10:33:22 -0800 | [diff] [blame^] | 37 | * environment. All these screen elements are added in the "non client area" which is the area of |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 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> |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 43 | * After creating the view, the function |
Wale Ogunwale | f3a62fb | 2015-10-14 17:07:29 -0700 | [diff] [blame] | 44 | * {@link #setPhoneWindow} needs to be called to make |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 45 | * the connection to it's owning PhoneWindow. |
| 46 | * Note: At this time the application can change various attributes of the DecorView which |
| 47 | * will break things (in settle/unexpected ways): |
| 48 | * <ul> |
| 49 | * <li>setElevation</li> |
| 50 | * <li>setOutlineProvider</li> |
| 51 | * <li>setSurfaceFormat</li> |
| 52 | * <li>..</li> |
| 53 | * </ul> |
| 54 | * This will be mitigated once b/22527834 will be addressed. |
| 55 | */ |
Chong Zhang | 509ea6b | 2015-09-30 14:09:52 -0700 | [diff] [blame] | 56 | public class NonClientDecorView extends LinearLayout |
Wale Ogunwale | bf9eefc | 2015-11-17 14:47:52 -0800 | [diff] [blame] | 57 | implements View.OnClickListener, View.OnTouchListener { |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 58 | private final static String TAG = "NonClientDecorView"; |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 59 | private PhoneWindow mOwner = null; |
Wale Ogunwale | 8cc5a74 | 2015-11-17 15:41:05 -0800 | [diff] [blame] | 60 | private boolean mShowDecor = false; |
Skuhne | 81c524a | 2015-08-12 13:34:14 -0700 | [diff] [blame] | 61 | |
| 62 | // True if the window is being dragged. |
| 63 | private boolean mDragging = false; |
| 64 | |
Skuhne | 81c524a | 2015-08-12 13:34:14 -0700 | [diff] [blame] | 65 | // True when the left mouse button got released while dragging. |
| 66 | private boolean mLeftMouseButtonReleased; |
| 67 | |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 68 | public NonClientDecorView(Context context) { |
| 69 | super(context); |
| 70 | } |
| 71 | |
| 72 | public NonClientDecorView(Context context, AttributeSet attrs) { |
| 73 | super(context, attrs); |
| 74 | } |
| 75 | |
| 76 | public NonClientDecorView(Context context, AttributeSet attrs, int defStyle) { |
| 77 | super(context, attrs, defStyle); |
| 78 | } |
| 79 | |
Wale Ogunwale | 2b547c3 | 2015-11-18 10:33:22 -0800 | [diff] [blame^] | 80 | public void setPhoneWindow(PhoneWindow owner, boolean showDecor) { |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 81 | mOwner = owner; |
Stefan Kuhne | f4dd71a | 2015-08-07 09:28:52 -0700 | [diff] [blame] | 82 | mShowDecor = showDecor; |
Skuhne | f7b882c | 2015-08-11 17:18:58 -0700 | [diff] [blame] | 83 | updateCaptionVisibility(); |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 84 | // By changing the outline provider to BOUNDS, the window can remove its |
| 85 | // background without removing the shadow. |
| 86 | mOwner.getDecorView().setOutlineProvider(ViewOutlineProvider.BOUNDS); |
Skuhne | b816087 | 2015-09-22 09:51:39 -0700 | [diff] [blame] | 87 | |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 88 | findViewById(R.id.maximize_window).setOnClickListener(this); |
| 89 | findViewById(R.id.close_window).setOnClickListener(this); |
| 90 | } |
| 91 | |
Skuhne | 81c524a | 2015-08-12 13:34:14 -0700 | [diff] [blame] | 92 | @Override |
Chong Zhang | 509ea6b | 2015-09-30 14:09:52 -0700 | [diff] [blame] | 93 | public boolean onTouch(View v, MotionEvent e) { |
Skuhne | 81c524a | 2015-08-12 13:34:14 -0700 | [diff] [blame] | 94 | // Note: There are no mixed events. When a new device gets used (e.g. 1. Mouse, 2. touch) |
| 95 | // the old input device events get cancelled first. So no need to remember the kind of |
| 96 | // input device we are listening to. |
| 97 | switch (e.getActionMasked()) { |
| 98 | case MotionEvent.ACTION_DOWN: |
Skuhne | a635a26 | 2015-08-26 15:45:58 -0700 | [diff] [blame] | 99 | if (!mShowDecor) { |
| 100 | // When there is no decor we should not react to anything. |
| 101 | return false; |
| 102 | } |
Skuhne | 81c524a | 2015-08-12 13:34:14 -0700 | [diff] [blame] | 103 | // A drag action is started if we aren't dragging already and the starting event is |
| 104 | // either a left mouse button or any other input device. |
| 105 | if (!mDragging && |
| 106 | (e.getToolType(e.getActionIndex()) != MotionEvent.TOOL_TYPE_MOUSE || |
| 107 | (e.getButtonState() & MotionEvent.BUTTON_PRIMARY) != 0)) { |
| 108 | mDragging = true; |
Skuhne | 81c524a | 2015-08-12 13:34:14 -0700 | [diff] [blame] | 109 | mLeftMouseButtonReleased = false; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 110 | startMovingTask(e.getRawX(), e.getRawY()); |
Skuhne | 81c524a | 2015-08-12 13:34:14 -0700 | [diff] [blame] | 111 | } |
| 112 | break; |
| 113 | |
| 114 | case MotionEvent.ACTION_MOVE: |
| 115 | if (mDragging && !mLeftMouseButtonReleased) { |
| 116 | if (e.getToolType(e.getActionIndex()) == MotionEvent.TOOL_TYPE_MOUSE && |
| 117 | (e.getButtonState() & MotionEvent.BUTTON_PRIMARY) == 0) { |
| 118 | // There is no separate mouse button up call and if the user mixes mouse |
| 119 | // button drag actions, we stop dragging once he releases the button. |
| 120 | mLeftMouseButtonReleased = true; |
| 121 | break; |
| 122 | } |
Skuhne | 81c524a | 2015-08-12 13:34:14 -0700 | [diff] [blame] | 123 | } |
| 124 | break; |
| 125 | |
| 126 | case MotionEvent.ACTION_UP: |
Skuhne | 81c524a | 2015-08-12 13:34:14 -0700 | [diff] [blame] | 127 | case MotionEvent.ACTION_CANCEL: |
Skuhne | a5a93ee | 2015-08-20 15:43:57 -0700 | [diff] [blame] | 128 | if (!mDragging) { |
| 129 | break; |
Skuhne | 81c524a | 2015-08-12 13:34:14 -0700 | [diff] [blame] | 130 | } |
Skuhne | a5a93ee | 2015-08-20 15:43:57 -0700 | [diff] [blame] | 131 | // Abort the ongoing dragging. |
| 132 | mDragging = false; |
Skuhne | a5a93ee | 2015-08-20 15:43:57 -0700 | [diff] [blame] | 133 | return true; |
Skuhne | 81c524a | 2015-08-12 13:34:14 -0700 | [diff] [blame] | 134 | } |
| 135 | return mDragging; |
| 136 | } |
| 137 | |
Stefan Kuhne | f4dd71a | 2015-08-07 09:28:52 -0700 | [diff] [blame] | 138 | /** |
| 139 | * The phone window configuration has changed and the decor needs to be updated. |
| 140 | * @param showDecor True if the decor should be shown. |
Wale Ogunwale | 2b547c3 | 2015-11-18 10:33:22 -0800 | [diff] [blame^] | 141 | */ |
| 142 | public void onConfigurationChanged(boolean showDecor) { |
Stefan Kuhne | f4dd71a | 2015-08-07 09:28:52 -0700 | [diff] [blame] | 143 | mShowDecor = showDecor; |
Skuhne | f7b882c | 2015-08-11 17:18:58 -0700 | [diff] [blame] | 144 | updateCaptionVisibility(); |
Stefan Kuhne | f4dd71a | 2015-08-07 09:28:52 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 147 | @Override |
| 148 | public void onClick(View view) { |
| 149 | if (view.getId() == R.id.maximize_window) { |
Stefan Kuhne | 1b42057 | 2015-08-07 10:50:19 -0700 | [diff] [blame] | 150 | maximizeWindow(); |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 151 | } else if (view.getId() == R.id.close_window) { |
Stefan Kuhne | 1b42057 | 2015-08-07 10:50:19 -0700 | [diff] [blame] | 152 | mOwner.dispatchOnWindowDismissed(true /*finishTask*/); |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
| 156 | @Override |
Skuhne | f7b882c | 2015-08-11 17:18:58 -0700 | [diff] [blame] | 157 | public void addView(View child, int index, ViewGroup.LayoutParams params) { |
Stefan Kuhne | f4dd71a | 2015-08-07 09:28:52 -0700 | [diff] [blame] | 158 | // 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] | 159 | if (index >= 2 || getChildCount() >= 2) { |
| 160 | throw new IllegalStateException("NonClientDecorView can only handle 1 client view"); |
| 161 | } |
| 162 | super.addView(child, index, params); |
| 163 | } |
| 164 | |
Stefan Kuhne | f4dd71a | 2015-08-07 09:28:52 -0700 | [diff] [blame] | 165 | /** |
| 166 | * Determine if the workspace is entirely covered by the window. |
| 167 | * @return Returns true when the window is filling the entire screen/workspace. |
| 168 | **/ |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 169 | private boolean isFillingScreen() { |
| 170 | return (0 != ((getWindowSystemUiVisibility() | getSystemUiVisibility()) & |
| 171 | (View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | |
| 172 | View.SYSTEM_UI_FLAG_IMMERSIVE | View.SYSTEM_UI_FLAG_LOW_PROFILE))); |
| 173 | } |
| 174 | |
Stefan Kuhne | f4dd71a | 2015-08-07 09:28:52 -0700 | [diff] [blame] | 175 | /** |
Skuhne | f7b882c | 2015-08-11 17:18:58 -0700 | [diff] [blame] | 176 | * Updates the visibility of the caption. |
| 177 | **/ |
| 178 | private void updateCaptionVisibility() { |
| 179 | // Don't show the decor if the window has e.g. entered full screen. |
| 180 | boolean invisible = isFillingScreen() || !mShowDecor; |
| 181 | View caption = getChildAt(0); |
| 182 | caption.setVisibility(invisible ? GONE : VISIBLE); |
Chong Zhang | 509ea6b | 2015-09-30 14:09:52 -0700 | [diff] [blame] | 183 | caption.setOnTouchListener(this); |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 184 | } |
Stefan Kuhne | 1b42057 | 2015-08-07 10:50:19 -0700 | [diff] [blame] | 185 | |
Stefan Kuhne | f4dd71a | 2015-08-07 09:28:52 -0700 | [diff] [blame] | 186 | /** |
| 187 | * Maximize the window by moving it to the maximized workspace stack. |
| 188 | **/ |
Stefan Kuhne | 1b42057 | 2015-08-07 10:50:19 -0700 | [diff] [blame] | 189 | private void maximizeWindow() { |
Skuhne | ce2faa5 | 2015-08-11 10:36:38 -0700 | [diff] [blame] | 190 | Window.WindowControllerCallback callback = mOwner.getWindowControllerCallback(); |
Stefan Kuhne | 1b42057 | 2015-08-07 10:50:19 -0700 | [diff] [blame] | 191 | if (callback != null) { |
| 192 | try { |
Wale Ogunwale | 3797c22 | 2015-10-27 14:21:58 -0700 | [diff] [blame] | 193 | callback.changeWindowStack(FULLSCREEN_WORKSPACE_STACK_ID); |
Stefan Kuhne | 1b42057 | 2015-08-07 10:50:19 -0700 | [diff] [blame] | 194 | } catch (RemoteException ex) { |
| 195 | Log.e(TAG, "Cannot change task workspace."); |
| 196 | } |
| 197 | } |
| 198 | } |
Wale Ogunwale | 8cc5a74 | 2015-11-17 15:41:05 -0800 | [diff] [blame] | 199 | |
| 200 | public boolean isShowingDecor() { |
| 201 | return mShowDecor; |
| 202 | } |
| 203 | |
| 204 | public int getDecorCaptionHeight() { |
| 205 | final View caption = getChildAt(0); |
| 206 | return (caption != null) ? caption.getHeight() : 0; |
| 207 | } |
Stefan Kuhne | 61b47bb | 2015-07-28 14:04:25 -0700 | [diff] [blame] | 208 | } |