blob: e22bd10b63603afbd589da041a2d689172cf5450 [file] [log] [blame]
Stefan Kuhne61b47bb2015-07-28 14:04:25 -07001/*
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
17package com.android.internal.widget;
18
Wale Ogunwale3797c222015-10-27 14:21:58 -070019import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
20
Stefan Kuhne61b47bb2015-07-28 14:04:25 -070021import android.content.Context;
22import android.os.RemoteException;
23import android.util.AttributeSet;
Skuhne81c524a2015-08-12 13:34:14 -070024import android.view.MotionEvent;
Stefan Kuhne61b47bb2015-07-28 14:04:25 -070025import android.view.View;
Skuhnef7b882c2015-08-11 17:18:58 -070026import android.widget.LinearLayout;
Stefan Kuhne61b47bb2015-07-28 14:04:25 -070027import android.view.ViewGroup;
Stefan Kuhnef4dd71a2015-08-07 09:28:52 -070028import android.view.ViewOutlineProvider;
29import android.view.Window;
Stefan Kuhne61b47bb2015-07-28 14:04:25 -070030import android.util.Log;
Stefan Kuhne61b47bb2015-07-28 14:04:25 -070031
Stefan Kuhne61b47bb2015-07-28 14:04:25 -070032import com.android.internal.R;
33import com.android.internal.policy.PhoneWindow;
34
Stefan Kuhne61b47bb2015-07-28 14:04:25 -070035/**
Wale Ogunwale62a91d62015-11-18 11:44:10 -080036 * This class represents the special screen elements to control a window on freeform
37 * environment.
Stefan Kuhne61b47bb2015-07-28 14:04:25 -070038 * 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 Kuhne61b47bb2015-07-28 14:04:25 -070042 * After creating the view, the function
Wale Ogunwalef3a62fb2015-10-14 17:07:29 -070043 * {@link #setPhoneWindow} needs to be called to make
Stefan Kuhne61b47bb2015-07-28 14:04:25 -070044 * 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 Kuhne61b47bb2015-07-28 14:04:25 -070048 * <li>setOutlineProvider</li>
49 * <li>setSurfaceFormat</li>
50 * <li>..</li>
51 * </ul>
Stefan Kuhne61b47bb2015-07-28 14:04:25 -070052 */
Wale Ogunwale62a91d62015-11-18 11:44:10 -080053public class DecorCaptionView extends LinearLayout
Wale Ogunwalebf9eefc2015-11-17 14:47:52 -080054 implements View.OnClickListener, View.OnTouchListener {
Wale Ogunwale62a91d62015-11-18 11:44:10 -080055 private final static String TAG = "DecorCaptionView";
Stefan Kuhne61b47bb2015-07-28 14:04:25 -070056 private PhoneWindow mOwner = null;
Wale Ogunwale62a91d62015-11-18 11:44:10 -080057 private boolean mShow = false;
Skuhne81c524a2015-08-12 13:34:14 -070058
59 // True if the window is being dragged.
60 private boolean mDragging = false;
61
Skuhne81c524a2015-08-12 13:34:14 -070062 // True when the left mouse button got released while dragging.
63 private boolean mLeftMouseButtonReleased;
64
Wale Ogunwale62a91d62015-11-18 11:44:10 -080065 public DecorCaptionView(Context context) {
Stefan Kuhne61b47bb2015-07-28 14:04:25 -070066 super(context);
67 }
68
Wale Ogunwale62a91d62015-11-18 11:44:10 -080069 public DecorCaptionView(Context context, AttributeSet attrs) {
Stefan Kuhne61b47bb2015-07-28 14:04:25 -070070 super(context, attrs);
71 }
72
Wale Ogunwale62a91d62015-11-18 11:44:10 -080073 public DecorCaptionView(Context context, AttributeSet attrs, int defStyle) {
Stefan Kuhne61b47bb2015-07-28 14:04:25 -070074 super(context, attrs, defStyle);
75 }
76
Wale Ogunwale62a91d62015-11-18 11:44:10 -080077 public void setPhoneWindow(PhoneWindow owner, boolean show) {
Stefan Kuhne61b47bb2015-07-28 14:04:25 -070078 mOwner = owner;
Wale Ogunwale62a91d62015-11-18 11:44:10 -080079 mShow = show;
Skuhnef7b882c2015-08-11 17:18:58 -070080 updateCaptionVisibility();
Stefan Kuhne61b47bb2015-07-28 14:04:25 -070081 // By changing the outline provider to BOUNDS, the window can remove its
82 // background without removing the shadow.
83 mOwner.getDecorView().setOutlineProvider(ViewOutlineProvider.BOUNDS);
Skuhneb8160872015-09-22 09:51:39 -070084
Stefan Kuhne61b47bb2015-07-28 14:04:25 -070085 findViewById(R.id.maximize_window).setOnClickListener(this);
86 findViewById(R.id.close_window).setOnClickListener(this);
87 }
88
Skuhne81c524a2015-08-12 13:34:14 -070089 @Override
Chong Zhang509ea6b2015-09-30 14:09:52 -070090 public boolean onTouch(View v, MotionEvent e) {
Skuhne81c524a2015-08-12 13:34:14 -070091 // 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 Ogunwale62a91d62015-11-18 11:44:10 -080096 if (!mShow) {
97 // When there is no caption we should not react to anything.
Skuhnea635a262015-08-26 15:45:58 -070098 return false;
99 }
Skuhne81c524a2015-08-12 13:34:14 -0700100 // 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;
Skuhne81c524a2015-08-12 13:34:14 -0700106 mLeftMouseButtonReleased = false;
Chong Zhang8e89b312015-09-09 15:09:30 -0700107 startMovingTask(e.getRawX(), e.getRawY());
Skuhne81c524a2015-08-12 13:34:14 -0700108 }
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 }
Skuhne81c524a2015-08-12 13:34:14 -0700120 }
121 break;
122
123 case MotionEvent.ACTION_UP:
Skuhne81c524a2015-08-12 13:34:14 -0700124 case MotionEvent.ACTION_CANCEL:
Skuhnea5a93ee2015-08-20 15:43:57 -0700125 if (!mDragging) {
126 break;
Skuhne81c524a2015-08-12 13:34:14 -0700127 }
Skuhnea5a93ee2015-08-20 15:43:57 -0700128 // Abort the ongoing dragging.
129 mDragging = false;
Skuhnea5a93ee2015-08-20 15:43:57 -0700130 return true;
Skuhne81c524a2015-08-12 13:34:14 -0700131 }
132 return mDragging;
133 }
134
Stefan Kuhnef4dd71a2015-08-07 09:28:52 -0700135 /**
Wale Ogunwale62a91d62015-11-18 11:44:10 -0800136 * The phone window configuration has changed and the caption needs to be updated.
137 * @param show True if the caption should be shown.
Wale Ogunwale2b547c32015-11-18 10:33:22 -0800138 */
Wale Ogunwale62a91d62015-11-18 11:44:10 -0800139 public void onConfigurationChanged(boolean show) {
140 mShow = show;
Skuhnef7b882c2015-08-11 17:18:58 -0700141 updateCaptionVisibility();
Stefan Kuhnef4dd71a2015-08-07 09:28:52 -0700142 }
143
Stefan Kuhne61b47bb2015-07-28 14:04:25 -0700144 @Override
145 public void onClick(View view) {
146 if (view.getId() == R.id.maximize_window) {
Stefan Kuhne1b420572015-08-07 10:50:19 -0700147 maximizeWindow();
Stefan Kuhne61b47bb2015-07-28 14:04:25 -0700148 } else if (view.getId() == R.id.close_window) {
Stefan Kuhne1b420572015-08-07 10:50:19 -0700149 mOwner.dispatchOnWindowDismissed(true /*finishTask*/);
Stefan Kuhne61b47bb2015-07-28 14:04:25 -0700150 }
151 }
152
153 @Override
Skuhnef7b882c2015-08-11 17:18:58 -0700154 public void addView(View child, int index, ViewGroup.LayoutParams params) {
Stefan Kuhnef4dd71a2015-08-07 09:28:52 -0700155 // Make sure that we never get more then one client area in our view.
Stefan Kuhne61b47bb2015-07-28 14:04:25 -0700156 if (index >= 2 || getChildCount() >= 2) {
Wale Ogunwale62a91d62015-11-18 11:44:10 -0800157 throw new IllegalStateException("DecorCaptionView can only handle 1 client view");
Stefan Kuhne61b47bb2015-07-28 14:04:25 -0700158 }
159 super.addView(child, index, params);
160 }
161
Stefan Kuhnef4dd71a2015-08-07 09:28:52 -0700162 /**
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 Kuhne61b47bb2015-07-28 14:04:25 -0700166 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 Kuhnef4dd71a2015-08-07 09:28:52 -0700172 /**
Skuhnef7b882c2015-08-11 17:18:58 -0700173 * Updates the visibility of the caption.
174 **/
175 private void updateCaptionVisibility() {
Wale Ogunwale62a91d62015-11-18 11:44:10 -0800176 // Don't show the caption if the window has e.g. entered full screen.
177 boolean invisible = isFillingScreen() || !mShow;
Skuhnef7b882c2015-08-11 17:18:58 -0700178 View caption = getChildAt(0);
179 caption.setVisibility(invisible ? GONE : VISIBLE);
Chong Zhang509ea6b2015-09-30 14:09:52 -0700180 caption.setOnTouchListener(this);
Stefan Kuhne61b47bb2015-07-28 14:04:25 -0700181 }
Stefan Kuhne1b420572015-08-07 10:50:19 -0700182
Stefan Kuhnef4dd71a2015-08-07 09:28:52 -0700183 /**
184 * Maximize the window by moving it to the maximized workspace stack.
185 **/
Stefan Kuhne1b420572015-08-07 10:50:19 -0700186 private void maximizeWindow() {
Skuhnece2faa52015-08-11 10:36:38 -0700187 Window.WindowControllerCallback callback = mOwner.getWindowControllerCallback();
Stefan Kuhne1b420572015-08-07 10:50:19 -0700188 if (callback != null) {
189 try {
Wale Ogunwale3797c222015-10-27 14:21:58 -0700190 callback.changeWindowStack(FULLSCREEN_WORKSPACE_STACK_ID);
Stefan Kuhne1b420572015-08-07 10:50:19 -0700191 } catch (RemoteException ex) {
192 Log.e(TAG, "Cannot change task workspace.");
193 }
194 }
195 }
Wale Ogunwale8cc5a742015-11-17 15:41:05 -0800196
Wale Ogunwale62a91d62015-11-18 11:44:10 -0800197 public boolean isCaptionShowing() {
198 return mShow;
Wale Ogunwale8cc5a742015-11-17 15:41:05 -0800199 }
200
Wale Ogunwale62a91d62015-11-18 11:44:10 -0800201 public int getCaptionHeight() {
Wale Ogunwale8cc5a742015-11-17 15:41:05 -0800202 final View caption = getChildAt(0);
203 return (caption != null) ? caption.getHeight() : 0;
204 }
Stefan Kuhne61b47bb2015-07-28 14:04:25 -0700205}