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