blob: baf92dc7abe7c5863fececdfee302a086b71b452 [file] [log] [blame]
Mady Mellordea7ecf2018-12-10 15:47:40 -08001/*
2 * Copyright (C) 2018 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.systemui.bubbles;
18
Yuncheol Heo5d62e752020-04-08 14:54:24 -070019import static android.app.WindowConfiguration.WINDOWING_MODE_MULTI_WINDOW;
Mady Mellorc41ed322019-09-25 11:19:26 -070020import static android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
21import static android.content.Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
Yunfan Chenb83940c2020-04-06 16:43:09 +090022import static android.graphics.PixelFormat.TRANSPARENT;
Mady Mellor390bff42019-04-05 15:09:01 -070023import static android.view.Display.INVALID_DISPLAY;
Yunfan Chenb83940c2020-04-06 16:43:09 +090024import static android.view.InsetsState.ITYPE_IME;
Jorim Jaggi924ef752020-01-29 17:26:55 +010025import static android.view.ViewRootImpl.NEW_INSETS_MODE_FULL;
26import static android.view.ViewRootImpl.sNewInsetsMode;
Yunfan Chenb83940c2020-04-06 16:43:09 +090027import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
28import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
29import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
30import static android.view.WindowManager.LayoutParams.TYPE_NAVIGATION_BAR_PANEL;
Beverlya53fb0d2020-01-29 15:26:13 -050031
Mark Renoufdb861c92019-07-26 13:58:17 -040032import static com.android.systemui.bubbles.BubbleDebugConfig.DEBUG_BUBBLE_EXPANDED_VIEW;
Issei Suzukia8d07312019-06-07 12:56:19 +020033import static com.android.systemui.bubbles.BubbleDebugConfig.TAG_BUBBLES;
34import static com.android.systemui.bubbles.BubbleDebugConfig.TAG_WITH_CLASS_NAME;
Mady Mellorca0c24c2019-05-16 16:14:32 -070035
Lyn Hanb58c7562020-01-07 14:29:20 -080036import android.annotation.Nullable;
Mady Mellor60101c92019-04-11 19:04:00 -070037import android.app.ActivityOptions;
Mark Renouf5c732b32019-06-12 15:14:54 -040038import android.app.ActivityTaskManager;
Mady Mellor3dff9e62019-02-05 18:12:53 -080039import android.app.ActivityView;
Mady Mellor9801e852019-01-22 14:50:28 -080040import android.app.PendingIntent;
Mark Renouf5c732b32019-06-12 15:14:54 -040041import android.content.ComponentName;
Mady Mellordea7ecf2018-12-10 15:47:40 -080042import android.content.Context;
Mady Mellor9801e852019-01-22 14:50:28 -080043import android.content.Intent;
Mady Mellordea7ecf2018-12-10 15:47:40 -080044import android.content.res.Resources;
Mady Mellordd497052019-01-30 17:23:48 -080045import android.content.res.TypedArray;
Mady Mellordea7ecf2018-12-10 15:47:40 -080046import android.graphics.Color;
Mady Mellor3dff9e62019-02-05 18:12:53 -080047import android.graphics.Insets;
48import android.graphics.Point;
Mady Mellor47b11e32019-07-11 19:06:21 -070049import android.graphics.Rect;
Mady Mellordea7ecf2018-12-10 15:47:40 -080050import android.graphics.drawable.ShapeDrawable;
Yunfan Chenb83940c2020-04-06 16:43:09 +090051import android.hardware.display.VirtualDisplay;
52import android.os.Binder;
Mark Renouf5c732b32019-06-12 15:14:54 -040053import android.os.RemoteException;
Mady Mellordea7ecf2018-12-10 15:47:40 -080054import android.util.AttributeSet;
Mady Mellor9801e852019-01-22 14:50:28 -080055import android.util.Log;
Yunfan Chenb83940c2020-04-06 16:43:09 +090056import android.view.Gravity;
Mady Mellordea7ecf2018-12-10 15:47:40 -080057import android.view.View;
Mady Mellor3dff9e62019-02-05 18:12:53 -080058import android.view.WindowInsets;
Mady Mellora96c9ed2019-06-07 12:55:26 -070059import android.view.WindowManager;
Lyn Han670fa802020-05-05 12:56:33 -070060import android.view.accessibility.AccessibilityNodeInfo;
Mady Mellordea7ecf2018-12-10 15:47:40 -080061import android.widget.LinearLayout;
62
Mark Renouf34d04f32019-05-13 15:53:18 -040063import com.android.internal.policy.ScreenDecorationsUtils;
Mady Mellor3dff9e62019-02-05 18:12:53 -080064import com.android.systemui.Dependency;
Mady Mellordea7ecf2018-12-10 15:47:40 -080065import com.android.systemui.R;
66import com.android.systemui.recents.TriangleShape;
Lyn Han754e77b2019-04-30 14:34:49 -070067import com.android.systemui.statusbar.AlphaOptimizedButton;
Beverlya53fb0d2020-01-29 15:26:13 -050068import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Mady Mellordea7ecf2018-12-10 15:47:40 -080069
70/**
Lyn Han02cca812019-04-02 16:27:32 -070071 * Container for the expanded bubble view, handles rendering the caret and settings icon.
Mady Mellordea7ecf2018-12-10 15:47:40 -080072 */
Joshua Tsuji6855cab2020-04-16 01:05:39 -040073public class BubbleExpandedView extends LinearLayout {
Issei Suzukia8d07312019-06-07 12:56:19 +020074 private static final String TAG = TAG_WITH_CLASS_NAME ? "BubbleExpandedView" : TAG_BUBBLES;
Yunfan Chenb83940c2020-04-06 16:43:09 +090075 private static final String WINDOW_TITLE = "ImeInsetsWindowWithoutContent";
Mady Mellordea7ecf2018-12-10 15:47:40 -080076
Issei Suzuki734bc942019-06-05 13:59:52 +020077 private enum ActivityViewStatus {
78 // ActivityView is being initialized, cannot start an activity yet.
79 INITIALIZING,
80 // ActivityView is initialized, and ready to start an activity.
81 INITIALIZED,
82 // Activity runs in the ActivityView.
83 ACTIVITY_STARTED,
84 // ActivityView is released, so activity launching will no longer be permitted.
85 RELEASED,
86 }
Mady Mellordea7ecf2018-12-10 15:47:40 -080087
88 // The triangle pointing to the expanded view
89 private View mPointerView;
Mady Mellor44ee2fe2019-01-30 17:51:16 -080090 private int mPointerMargin;
Mady Mellore8e07712019-01-23 12:45:33 -080091
Lyn Han754e77b2019-04-30 14:34:49 -070092 private AlphaOptimizedButton mSettingsIcon;
Mady Mellore8e07712019-01-23 12:45:33 -080093
Mady Mellor3dff9e62019-02-05 18:12:53 -080094 // Views for expanded state
Mady Mellor3dff9e62019-02-05 18:12:53 -080095 private ActivityView mActivityView;
96
Issei Suzuki734bc942019-06-05 13:59:52 +020097 private ActivityViewStatus mActivityViewStatus = ActivityViewStatus.INITIALIZING;
Mark Renouf5c732b32019-06-12 15:14:54 -040098 private int mTaskId = -1;
99
Lyn Hanb58c7562020-01-07 14:29:20 -0800100 private PendingIntent mPendingIntent;
Mady Mellor3dff9e62019-02-05 18:12:53 -0800101
Mady Mellor5d8f1402019-02-21 18:23:52 -0800102 private boolean mKeyboardVisible;
103 private boolean mNeedsNewHeight;
104
Mady Mellora96c9ed2019-06-07 12:55:26 -0700105 private Point mDisplaySize;
Mady Mellorfe7ec032019-01-30 17:32:49 -0800106 private int mMinHeight;
Lyn Hancd4f87e2020-02-19 20:33:45 -0800107 private int mOverflowHeight;
Lyn Han02cca812019-04-02 16:27:32 -0700108 private int mSettingsIconHeight;
Lyn Han02cca812019-04-02 16:27:32 -0700109 private int mPointerWidth;
110 private int mPointerHeight;
Mark Renouf34d04f32019-05-13 15:53:18 -0400111 private ShapeDrawable mPointerDrawable;
Mady Mellordea7ecf2018-12-10 15:47:40 -0800112
Lyn Hanb58c7562020-01-07 14:29:20 -0800113 @Nullable private Bubble mBubble;
114
115 private boolean mIsOverflow;
Mady Mellore8e07712019-01-23 12:45:33 -0800116
Mady Mellor3dff9e62019-02-05 18:12:53 -0800117 private BubbleController mBubbleController = Dependency.get(BubbleController.class);
Mady Mellor9be3bed2019-08-21 17:26:26 -0700118 private WindowManager mWindowManager;
Mady Mellor9801e852019-01-22 14:50:28 -0800119
Mady Mellor9801e852019-01-22 14:50:28 -0800120 private BubbleStackView mStackView;
Yunfan Chenb83940c2020-04-06 16:43:09 +0900121 private View mVirtualImeView;
122 private WindowManager mVirtualDisplayWindowManager;
123 private boolean mImeShowing = false;
Mady Mellor9801e852019-01-22 14:50:28 -0800124
Mady Mellor3dff9e62019-02-05 18:12:53 -0800125 private ActivityView.StateCallback mStateCallback = new ActivityView.StateCallback() {
126 @Override
127 public void onActivityViewReady(ActivityView view) {
Mark Renoufdb861c92019-07-26 13:58:17 -0400128 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
129 Log.d(TAG, "onActivityViewReady: mActivityViewStatus=" + mActivityViewStatus
130 + " bubble=" + getBubbleKey());
131 }
Issei Suzuki734bc942019-06-05 13:59:52 +0200132 switch (mActivityViewStatus) {
133 case INITIALIZING:
134 case INITIALIZED:
135 // Custom options so there is no activity transition animation
136 ActivityOptions options = ActivityOptions.makeCustomAnimation(getContext(),
137 0 /* enterResId */, 0 /* exitResId */);
Winson Chung06200522020-03-31 22:47:02 -0700138 options.setTaskAlwaysOnTop(true);
Yuncheol Heo5d62e752020-04-08 14:54:24 -0700139 options.setLaunchWindowingMode(WINDOWING_MODE_MULTI_WINDOW);
Issei Suzuki734bc942019-06-05 13:59:52 +0200140 // Post to keep the lifecycle normal
Mark Renoufdb861c92019-07-26 13:58:17 -0400141 post(() -> {
142 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
143 Log.d(TAG, "onActivityViewReady: calling startActivity, "
144 + "bubble=" + getBubbleKey());
145 }
Mady Mellor8454ddf2019-08-15 11:16:23 -0700146 try {
Lyn Hanb58c7562020-01-07 14:29:20 -0800147 if (!mIsOverflow && mBubble.usingShortcutInfo()) {
Mady Mellor80d5ed12020-04-10 21:17:30 -0700148 options.setApplyActivityFlagsForBubbles(true);
Mady Mellorb547e5e2019-12-02 10:15:56 -0800149 mActivityView.startShortcutActivity(mBubble.getShortcutInfo(),
150 options, null /* sourceBounds */);
151 } else {
152 Intent fillInIntent = new Intent();
153 // Apply flags to make behaviour match documentLaunchMode=always.
154 fillInIntent.addFlags(FLAG_ACTIVITY_NEW_DOCUMENT);
155 fillInIntent.addFlags(FLAG_ACTIVITY_MULTIPLE_TASK);
Lyn Hanb58c7562020-01-07 14:29:20 -0800156 mActivityView.startActivity(mPendingIntent, fillInIntent, options);
Mady Mellorb547e5e2019-12-02 10:15:56 -0800157 }
Mady Mellor8454ddf2019-08-15 11:16:23 -0700158 } catch (RuntimeException e) {
159 // If there's a runtime exception here then there's something
160 // wrong with the intent, we can't really recover / try to populate
161 // the bubble again so we'll just remove it.
162 Log.w(TAG, "Exception while displaying bubble: " + getBubbleKey()
163 + ", " + e.getMessage() + "; removing bubble");
Beverlya53fb0d2020-01-29 15:26:13 -0500164 mBubbleController.removeBubble(getBubbleEntry(),
Mady Mellor8454ddf2019-08-15 11:16:23 -0700165 BubbleController.DISMISS_INVALID_INTENT);
166 }
Mark Renoufdb861c92019-07-26 13:58:17 -0400167 });
Issei Suzuki734bc942019-06-05 13:59:52 +0200168 mActivityViewStatus = ActivityViewStatus.ACTIVITY_STARTED;
Mady Mellor6d002032019-02-13 13:45:17 -0800169 }
Mady Mellor3dff9e62019-02-05 18:12:53 -0800170 }
171
172 @Override
173 public void onActivityViewDestroyed(ActivityView view) {
Mark Renoufdb861c92019-07-26 13:58:17 -0400174 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
175 Log.d(TAG, "onActivityViewDestroyed: mActivityViewStatus=" + mActivityViewStatus
176 + " bubble=" + getBubbleKey());
177 }
Issei Suzuki734bc942019-06-05 13:59:52 +0200178 mActivityViewStatus = ActivityViewStatus.RELEASED;
Mady Mellor3dff9e62019-02-05 18:12:53 -0800179 }
180
Mark Renouf5c732b32019-06-12 15:14:54 -0400181 @Override
182 public void onTaskCreated(int taskId, ComponentName componentName) {
Mark Renoufdb861c92019-07-26 13:58:17 -0400183 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
184 Log.d(TAG, "onTaskCreated: taskId=" + taskId
185 + " bubble=" + getBubbleKey());
186 }
Mark Renouf5c732b32019-06-12 15:14:54 -0400187 // Since Bubble ActivityView applies singleTaskDisplay this is
188 // guaranteed to only be called once per ActivityView. The taskId is
189 // saved to use for removeTask, preventing appearance in recent tasks.
190 mTaskId = taskId;
Mady Mellor3dff9e62019-02-05 18:12:53 -0800191 }
192
193 /**
194 * This is only called for tasks on this ActivityView, which is also set to
195 * single-task mode -- meaning never more than one task on this display. If a task
196 * is being removed, it's the top Activity finishing and this bubble should
197 * be removed or collapsed.
198 */
199 @Override
200 public void onTaskRemovalStarted(int taskId) {
Mark Renoufdb861c92019-07-26 13:58:17 -0400201 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
202 Log.d(TAG, "onTaskRemovalStarted: taskId=" + taskId
203 + " mActivityViewStatus=" + mActivityViewStatus
204 + " bubble=" + getBubbleKey());
205 }
Mady Mellordd6fe612020-04-15 11:47:55 -0700206 if (mBubble != null) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800207 // Must post because this is called from a binder thread.
Beverlya53fb0d2020-01-29 15:26:13 -0500208 post(() -> mBubbleController.removeBubble(mBubble.getEntry(),
Mady Mellorff076eb2019-11-13 10:12:06 -0800209 BubbleController.DISMISS_TASK_FINISHED));
Mady Mellor3dff9e62019-02-05 18:12:53 -0800210 }
211 }
212 };
Mady Mellore8e07712019-01-23 12:45:33 -0800213
Mady Mellor3d82e682019-02-05 13:34:48 -0800214 public BubbleExpandedView(Context context) {
Mady Mellordea7ecf2018-12-10 15:47:40 -0800215 this(context, null);
216 }
217
Mady Mellor3d82e682019-02-05 13:34:48 -0800218 public BubbleExpandedView(Context context, AttributeSet attrs) {
Mady Mellordea7ecf2018-12-10 15:47:40 -0800219 this(context, attrs, 0);
220 }
221
Mady Mellor3d82e682019-02-05 13:34:48 -0800222 public BubbleExpandedView(Context context, AttributeSet attrs, int defStyleAttr) {
Mady Mellordea7ecf2018-12-10 15:47:40 -0800223 this(context, attrs, defStyleAttr, 0);
224 }
225
Mady Mellor3d82e682019-02-05 13:34:48 -0800226 public BubbleExpandedView(Context context, AttributeSet attrs, int defStyleAttr,
Mady Mellordea7ecf2018-12-10 15:47:40 -0800227 int defStyleRes) {
228 super(context, attrs, defStyleAttr, defStyleRes);
Mady Mellora96c9ed2019-06-07 12:55:26 -0700229 mDisplaySize = new Point();
Mady Mellor9be3bed2019-08-21 17:26:26 -0700230 mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Mady Mellor8fe411d2019-08-16 11:27:53 -0700231 // Get the real size -- this includes screen decorations (notches, statusbar, navbar).
Mady Mellor9be3bed2019-08-21 17:26:26 -0700232 mWindowManager.getDefaultDisplay().getRealSize(mDisplaySize);
Mady Mellor47b11e32019-07-11 19:06:21 -0700233 Resources res = getResources();
234 mMinHeight = res.getDimensionPixelSize(R.dimen.bubble_expanded_default_height);
Lyn Hancd4f87e2020-02-19 20:33:45 -0800235 mOverflowHeight = res.getDimensionPixelSize(R.dimen.bubble_overflow_height);
Mady Mellor47b11e32019-07-11 19:06:21 -0700236 mPointerMargin = res.getDimensionPixelSize(R.dimen.bubble_pointer_margin);
Mady Mellordea7ecf2018-12-10 15:47:40 -0800237 }
238
239 @Override
240 protected void onFinishInflate() {
241 super.onFinishInflate();
Mark Renoufdb861c92019-07-26 13:58:17 -0400242 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
243 Log.d(TAG, "onFinishInflate: bubble=" + getBubbleKey());
244 }
Mady Mellordea7ecf2018-12-10 15:47:40 -0800245
246 Resources res = getResources();
247 mPointerView = findViewById(R.id.pointer_view);
Lyn Han02cca812019-04-02 16:27:32 -0700248 mPointerWidth = res.getDimensionPixelSize(R.dimen.bubble_pointer_width);
249 mPointerHeight = res.getDimensionPixelSize(R.dimen.bubble_pointer_height);
Mady Mellordd497052019-01-30 17:23:48 -0800250
Mark Renouf34d04f32019-05-13 15:53:18 -0400251 mPointerDrawable = new ShapeDrawable(TriangleShape.create(
Lyn Han5aa27e22019-05-15 10:55:07 -0700252 mPointerWidth, mPointerHeight, true /* pointUp */));
Mark Renouf34d04f32019-05-13 15:53:18 -0400253 mPointerView.setBackground(mPointerDrawable);
Mady Mellora96c9ed2019-06-07 12:55:26 -0700254 mPointerView.setVisibility(INVISIBLE);
Mady Mellor9801e852019-01-22 14:50:28 -0800255
Lyn Han02cca812019-04-02 16:27:32 -0700256 mSettingsIconHeight = getContext().getResources().getDimensionPixelSize(
Mady Mellor5a3e94b2020-02-07 12:16:21 -0800257 R.dimen.bubble_manage_button_height);
Lyn Han02cca812019-04-02 16:27:32 -0700258 mSettingsIcon = findViewById(R.id.settings_button);
Lyn Han02cca812019-04-02 16:27:32 -0700259
Mady Mellor3dff9e62019-02-05 18:12:53 -0800260 mActivityView = new ActivityView(mContext, null /* attrs */, 0 /* defStyle */,
261 true /* singleTaskInstance */);
Lyn Hanb58c7562020-01-07 14:29:20 -0800262
lumarkdc9b3192019-07-23 21:18:17 +0800263 // Set ActivityView's alpha value as zero, since there is no view content to be shown.
Issei Suzukicac2a502019-04-16 16:52:50 +0200264 setContentVisibility(false);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800265 addView(mActivityView);
266
Lyn Han5aa27e22019-05-15 10:55:07 -0700267 // Expanded stack layout, top to bottom:
268 // Expanded view container
269 // ==> bubble row
270 // ==> expanded view
271 // ==> activity view
272 // ==> manage button
273 bringChildToFront(mActivityView);
274 bringChildToFront(mSettingsIcon);
Mady Mellor52b1ac62019-04-10 16:59:03 -0700275
Mark Renouf34d04f32019-05-13 15:53:18 -0400276 applyThemeAttrs();
277
Mady Mellor5d8f1402019-02-21 18:23:52 -0800278 setOnApplyWindowInsetsListener((View view, WindowInsets insets) -> {
279 // Keep track of IME displaying because we should not make any adjustments that might
280 // cause a config change while the IME is displayed otherwise it'll loose focus.
Mady Mellor3dff9e62019-02-05 18:12:53 -0800281 final int keyboardHeight = insets.getSystemWindowInsetBottom()
282 - insets.getStableInsetBottom();
Mady Mellor5d8f1402019-02-21 18:23:52 -0800283 mKeyboardVisible = keyboardHeight != 0;
284 if (!mKeyboardVisible && mNeedsNewHeight) {
285 updateHeight();
286 }
Mady Mellor3dff9e62019-02-05 18:12:53 -0800287 return view.onApplyWindowInsets(insets);
288 });
Mady Mellore8e07712019-01-23 12:45:33 -0800289 }
290
Mark Renoufdb861c92019-07-26 13:58:17 -0400291 private String getBubbleKey() {
292 return mBubble != null ? mBubble.getKey() : "null";
293 }
294
Beverlya53fb0d2020-01-29 15:26:13 -0500295 private NotificationEntry getBubbleEntry() {
296 return mBubble != null ? mBubble.getEntry() : null;
297 }
298
Joshua Tsuji6855cab2020-04-16 01:05:39 -0400299 void setManageClickListener(OnClickListener manageClickListener) {
300 findViewById(R.id.settings_button).setOnClickListener(manageClickListener);
301 }
302
303 /**
304 * Updates the ActivityView's obscured touchable region. This calls onLocationChanged, which
305 * results in a call to {@link BubbleStackView#subtractObscuredTouchableRegion}. This is useful
306 * if a view has been added or removed from on top of the ActivityView, such as the manage menu.
307 */
308 void updateObscuredTouchableRegion() {
309 mActivityView.onLocationChanged();
310 }
311
Mark Renouf34d04f32019-05-13 15:53:18 -0400312 void applyThemeAttrs() {
Lyn Han61bfdb32019-12-10 14:34:12 -0800313 final TypedArray ta = mContext.obtainStyledAttributes(
314 new int[] {
315 android.R.attr.colorBackgroundFloating,
316 android.R.attr.dialogCornerRadius});
317 int bgColor = ta.getColor(0, Color.WHITE);
318 float cornerRadius = ta.getDimensionPixelSize(1, 0);
Mark Renouf34d04f32019-05-13 15:53:18 -0400319 ta.recycle();
320
Mark Renouf34d04f32019-05-13 15:53:18 -0400321 mPointerDrawable.setTint(bgColor);
Lyn Hanedb55e22020-02-12 18:55:39 -0800322 if (mActivityView != null && ScreenDecorationsUtils.supportsRoundedCornersOnWindows(
323 mContext.getResources())) {
Mark Renouf34d04f32019-05-13 15:53:18 -0400324 mActivityView.setCornerRadius(cornerRadius);
325 }
326 }
327
Mady Mellor5d8f1402019-02-21 18:23:52 -0800328 @Override
329 protected void onDetachedFromWindow() {
330 super.onDetachedFromWindow();
331 mKeyboardVisible = false;
332 mNeedsNewHeight = false;
333 if (mActivityView != null) {
Mady Melloraf02fa42020-03-04 14:22:16 -0800334 if (sNewInsetsMode == NEW_INSETS_MODE_FULL) {
Yunfan Chenb83940c2020-04-06 16:43:09 +0900335 setImeWindowToDisplay(0, 0);
Mady Melloraf02fa42020-03-04 14:22:16 -0800336 } else {
337 mActivityView.setForwardedInsets(Insets.of(0, 0, 0, 0));
338 }
Mady Mellor5d8f1402019-02-21 18:23:52 -0800339 }
Mark Renoufdb861c92019-07-26 13:58:17 -0400340 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
341 Log.d(TAG, "onDetachedFromWindow: bubble=" + getBubbleKey());
342 }
Mady Mellor5d8f1402019-02-21 18:23:52 -0800343 }
344
345 /**
Issei Suzukicac2a502019-04-16 16:52:50 +0200346 * Set visibility of contents in the expanded state.
347 *
348 * @param visibility {@code true} if the contents should be visible on the screen.
349 *
350 * Note that this contents visibility doesn't affect visibility at {@link android.view.View},
351 * and setting {@code false} actually means rendering the contents in transparent.
352 */
353 void setContentVisibility(boolean visibility) {
Mark Renoufdb861c92019-07-26 13:58:17 -0400354 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
355 Log.d(TAG, "setContentVisibility: visibility=" + visibility
356 + " bubble=" + getBubbleKey());
357 }
Issei Suzukicac2a502019-04-16 16:52:50 +0200358 final float alpha = visibility ? 1f : 0f;
359 mPointerView.setAlpha(alpha);
360 if (mActivityView != null) {
361 mActivityView.setAlpha(alpha);
362 }
363 }
364
365 /**
Mady Mellor5d8f1402019-02-21 18:23:52 -0800366 * Called by {@link BubbleStackView} when the insets for the expanded state should be updated.
367 * This should be done post-move and post-animation.
368 */
369 void updateInsets(WindowInsets insets) {
370 if (usingActivityView()) {
Mady Mellor8fe411d2019-08-16 11:27:53 -0700371 int[] screenLoc = mActivityView.getLocationOnScreen();
372 final int activityViewBottom = screenLoc[1] + mActivityView.getHeight();
Mady Mellor9be3bed2019-08-21 17:26:26 -0700373 final int keyboardTop = mDisplaySize.y - Math.max(insets.getSystemWindowInsetBottom(),
374 insets.getDisplayCutout() != null
375 ? insets.getDisplayCutout().getSafeInsetBottom()
376 : 0);
Mady Mellor8fe411d2019-08-16 11:27:53 -0700377 final int insetsBottom = Math.max(activityViewBottom - keyboardTop, 0);
Jorim Jaggi924ef752020-01-29 17:26:55 +0100378
Jorim Jaggi924ef752020-01-29 17:26:55 +0100379 if (sNewInsetsMode == NEW_INSETS_MODE_FULL) {
Yunfan Chenb83940c2020-04-06 16:43:09 +0900380 setImeWindowToDisplay(getWidth(), insetsBottom);
Jorim Jaggi924ef752020-01-29 17:26:55 +0100381 } else {
382 mActivityView.setForwardedInsets(Insets.of(0, 0, 0, insetsBottom));
383 }
Mady Mellor5d8f1402019-02-21 18:23:52 -0800384 }
385 }
386
Yunfan Chenb83940c2020-04-06 16:43:09 +0900387 private void setImeWindowToDisplay(int w, int h) {
388 if (getVirtualDisplayId() == INVALID_DISPLAY) {
389 return;
390 }
391 if (h == 0 || w == 0) {
392 if (mImeShowing) {
393 mVirtualImeView.setVisibility(GONE);
394 mImeShowing = false;
395 }
396 return;
397 }
398 final Context virtualDisplayContext = mContext.createDisplayContext(
399 getVirtualDisplay().getDisplay());
400
401 if (mVirtualDisplayWindowManager == null) {
402 mVirtualDisplayWindowManager =
403 (WindowManager) virtualDisplayContext.getSystemService(Context.WINDOW_SERVICE);
404 }
405 if (mVirtualImeView == null) {
406 mVirtualImeView = new View(virtualDisplayContext);
407 mVirtualImeView.setVisibility(VISIBLE);
408 mVirtualDisplayWindowManager.addView(mVirtualImeView,
409 getVirtualImeViewAttrs(w, h));
410 } else {
411 mVirtualDisplayWindowManager.updateViewLayout(mVirtualImeView,
412 getVirtualImeViewAttrs(w, h));
413 mVirtualImeView.setVisibility(VISIBLE);
414 }
415
416 mImeShowing = true;
417 }
418
419 private WindowManager.LayoutParams getVirtualImeViewAttrs(int w, int h) {
420 // To use TYPE_NAVIGATION_BAR_PANEL instead of TYPE_IME_BAR to bypass the IME window type
421 // token check when adding the window.
422 final WindowManager.LayoutParams attrs =
423 new WindowManager.LayoutParams(w, h, TYPE_NAVIGATION_BAR_PANEL,
424 FLAG_LAYOUT_NO_LIMITS | FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCHABLE,
425 TRANSPARENT);
426 attrs.gravity = Gravity.BOTTOM;
427 attrs.setTitle(WINDOW_TITLE);
428 attrs.token = new Binder();
429 attrs.providesInsetsTypes = new int[]{ITYPE_IME};
430 attrs.alpha = 0.0f;
431 return attrs;
432 }
433
Mady Mellor247ca2c2019-12-02 16:18:59 -0800434 void setStackView(BubbleStackView stackView) {
Mady Mellor9801e852019-01-22 14:50:28 -0800435 mStackView = stackView;
Mady Mellor6d002032019-02-13 13:45:17 -0800436 }
437
Lyn Hanb58c7562020-01-07 14:29:20 -0800438 public void setOverflow(boolean overflow) {
439 mIsOverflow = overflow;
440
441 Intent target = new Intent(mContext, BubbleOverflowActivity.class);
442 mPendingIntent = PendingIntent.getActivity(mContext, /* requestCode */ 0,
443 target, PendingIntent.FLAG_UPDATE_CURRENT);
444 mSettingsIcon.setVisibility(GONE);
445 }
446
Mady Mellor6d002032019-02-13 13:45:17 -0800447 /**
Mady Mellor247ca2c2019-12-02 16:18:59 -0800448 * Sets the bubble used to populate this view.
Mady Mellor6d002032019-02-13 13:45:17 -0800449 */
Mady Mellor247ca2c2019-12-02 16:18:59 -0800450 void update(Bubble bubble) {
451 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
452 Log.d(TAG, "update: bubble=" + (bubble != null ? bubble.getKey() : "null"));
453 }
454 boolean isNew = mBubble == null;
Lyn Hanb58c7562020-01-07 14:29:20 -0800455 if (isNew || bubble != null && bubble.getKey().equals(mBubble.getKey())) {
Mady Mellor247ca2c2019-12-02 16:18:59 -0800456 mBubble = bubble;
457 mSettingsIcon.setContentDescription(getResources().getString(
458 R.string.bubbles_settings_button_description, bubble.getAppName()));
459
Lyn Han670fa802020-05-05 12:56:33 -0700460 mSettingsIcon.setAccessibilityDelegate(
461 new AccessibilityDelegate() {
462 @Override
463 public void onInitializeAccessibilityNodeInfo(View host,
464 AccessibilityNodeInfo info) {
465 super.onInitializeAccessibilityNodeInfo(host, info);
466 // On focus, have TalkBack say
467 // "Actions available. Use swipe up then right to view."
468 // in addition to the default "double tap to activate".
469 mStackView.setupLocalMenu(info);
470 }
471 });
472
Mady Mellor247ca2c2019-12-02 16:18:59 -0800473 if (isNew) {
Lyn Hanb58c7562020-01-07 14:29:20 -0800474 mPendingIntent = mBubble.getBubbleIntent();
475 if (mPendingIntent != null || mBubble.getShortcutInfo() != null) {
Mady Mellor247ca2c2019-12-02 16:18:59 -0800476 setContentVisibility(false);
477 mActivityView.setVisibility(VISIBLE);
478 }
479 }
480 applyThemeAttrs();
481 } else {
482 Log.w(TAG, "Trying to update entry with different key, new bubble: "
483 + bubble.getKey() + " old bubble: " + bubble.getKey());
484 }
485 }
486
487 /**
488 * Lets activity view know it should be shown / populated with activity content.
489 */
490 void populateExpandedView() {
Mark Renoufdb861c92019-07-26 13:58:17 -0400491 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
492 Log.d(TAG, "populateExpandedView: "
493 + "bubble=" + getBubbleKey());
494 }
495
Mady Mellor5029fa62019-03-05 12:16:21 -0800496 if (usingActivityView()) {
497 mActivityView.setCallback(mStateCallback);
498 } else {
Issei Suzukia91f3962019-06-07 11:48:23 +0200499 Log.e(TAG, "Cannot populate expanded view.");
Mady Mellor5029fa62019-03-05 12:16:21 -0800500 }
Mady Mellor9801e852019-01-22 14:50:28 -0800501 }
502
Mark Renouf041d7262019-02-06 12:09:41 -0500503 boolean performBackPressIfNeeded() {
Mady Mellor323fb0b2019-03-25 12:15:22 -0700504 if (!usingActivityView()) {
Mark Renouf041d7262019-02-06 12:09:41 -0500505 return false;
506 }
507 mActivityView.performBackPress();
508 return true;
509 }
510
Mady Mellorfe7ec032019-01-30 17:32:49 -0800511 void updateHeight() {
Mark Renoufdb861c92019-07-26 13:58:17 -0400512 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
513 Log.d(TAG, "updateHeight: bubble=" + getBubbleKey());
514 }
Mady Mellorfe7ec032019-01-30 17:32:49 -0800515 if (usingActivityView()) {
Lyn Hancd4f87e2020-02-19 20:33:45 -0800516 float desiredHeight = mOverflowHeight;
Lyn Hanb58c7562020-01-07 14:29:20 -0800517 if (!mIsOverflow) {
518 desiredHeight = Math.max(mBubble.getDesiredHeight(mContext), mMinHeight);
519 }
Mady Mellor8fe411d2019-08-16 11:27:53 -0700520 float height = Math.min(desiredHeight, getMaxExpandedHeight());
Lyn Hancd4f87e2020-02-19 20:33:45 -0800521 height = Math.max(height, mIsOverflow? mOverflowHeight : mMinHeight);
Mady Mellorfe7ec032019-01-30 17:32:49 -0800522 LayoutParams lp = (LayoutParams) mActivityView.getLayoutParams();
Lyn Hancd4f87e2020-02-19 20:33:45 -0800523 mNeedsNewHeight = lp.height != height;
Mady Mellor5d8f1402019-02-21 18:23:52 -0800524 if (!mKeyboardVisible) {
525 // If the keyboard is visible... don't adjust the height because that will cause
526 // a configuration change and the keyboard will be lost.
Mady Mellor7af771a2019-03-07 15:04:54 -0800527 lp.height = (int) height;
Mady Mellor5d8f1402019-02-21 18:23:52 -0800528 mActivityView.setLayoutParams(lp);
529 mNeedsNewHeight = false;
530 }
Mark Renoufdb861c92019-07-26 13:58:17 -0400531 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
532 Log.d(TAG, "updateHeight: bubble=" + getBubbleKey() + " height=" + height
533 + " mNeedsNewHeight=" + mNeedsNewHeight);
534 }
Mady Mellorfe7ec032019-01-30 17:32:49 -0800535 }
536 }
537
Mady Mellora96c9ed2019-06-07 12:55:26 -0700538 private int getMaxExpandedHeight() {
Mady Mellor9be3bed2019-08-21 17:26:26 -0700539 mWindowManager.getDefaultDisplay().getRealSize(mDisplaySize);
Mady Mellora96c9ed2019-06-07 12:55:26 -0700540 int[] windowLocation = mActivityView.getLocationOnScreen();
Mady Mellor8fe411d2019-08-16 11:27:53 -0700541 int bottomInset = getRootWindowInsets() != null
542 ? getRootWindowInsets().getStableInsetBottom()
543 : 0;
Lyn Han0b955ed2020-01-31 09:47:35 -0800544 return mDisplaySize.y - windowLocation[1] - mSettingsIconHeight - mPointerHeight
Mady Mellor8fe411d2019-08-16 11:27:53 -0700545 - mPointerMargin - bottomInset;
Mady Mellora96c9ed2019-06-07 12:55:26 -0700546 }
547
Mady Mellor47b11e32019-07-11 19:06:21 -0700548 /**
Mady Mellor3dff9e62019-02-05 18:12:53 -0800549 * Update appearance of the expanded view being displayed.
550 */
551 public void updateView() {
Mark Renoufdb861c92019-07-26 13:58:17 -0400552 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
553 Log.d(TAG, "updateView: bubble="
554 + getBubbleKey());
555 }
Mady Mellor3dff9e62019-02-05 18:12:53 -0800556 if (usingActivityView()
557 && mActivityView.getVisibility() == VISIBLE
558 && mActivityView.isAttachedToWindow()) {
559 mActivityView.onLocationChanged();
Mady Mellor3dff9e62019-02-05 18:12:53 -0800560 }
Mady Mellorfe7ec032019-01-30 17:32:49 -0800561 updateHeight();
Mady Mellor3dff9e62019-02-05 18:12:53 -0800562 }
563
564 /**
Mady Mellordea7ecf2018-12-10 15:47:40 -0800565 * Set the x position that the tip of the triangle should point to.
566 */
Mady Mellor3dff9e62019-02-05 18:12:53 -0800567 public void setPointerPosition(float x) {
Lyn Han9a2f5cf2019-05-23 11:01:41 -0700568 float halfPointerWidth = mPointerWidth / 2f;
569 float pointerLeft = x - halfPointerWidth;
570 mPointerView.setTranslationX(pointerLeft);
Lyn Hanf74ba672019-05-20 16:08:48 -0700571 mPointerView.setVisibility(VISIBLE);
Mady Mellordea7ecf2018-12-10 15:47:40 -0800572 }
573
574 /**
Mady Mellor5a3e94b2020-02-07 12:16:21 -0800575 * Position of the manage button displayed in the expanded view. Used for placing user
576 * education about the manage button.
577 */
Joshua Tsuji6855cab2020-04-16 01:05:39 -0400578 public void getManageButtonBoundsOnScreen(Rect rect) {
579 mSettingsIcon.getBoundsOnScreen(rect);
Mady Mellor5a3e94b2020-02-07 12:16:21 -0800580 }
581
582 /**
Mady Mellor3dff9e62019-02-05 18:12:53 -0800583 * Removes and releases an ActivityView if one was previously created for this bubble.
Mady Mellordea7ecf2018-12-10 15:47:40 -0800584 */
Mady Mellor94d94a72019-03-05 18:16:59 -0800585 public void cleanUpExpandedState() {
Mark Renoufdb861c92019-07-26 13:58:17 -0400586 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
587 Log.d(TAG, "cleanUpExpandedState: mActivityViewStatus=" + mActivityViewStatus
588 + ", bubble=" + getBubbleKey());
589 }
Mady Mellor3dff9e62019-02-05 18:12:53 -0800590 if (mActivityView == null) {
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500591 return;
592 }
Issei Suzuki734bc942019-06-05 13:59:52 +0200593 switch (mActivityViewStatus) {
594 case INITIALIZED:
595 case ACTIVITY_STARTED:
596 mActivityView.release();
Mady Mellordea7ecf2018-12-10 15:47:40 -0800597 }
Mark Renouf5c732b32019-06-12 15:14:54 -0400598 if (mTaskId != -1) {
599 try {
600 ActivityTaskManager.getService().removeTask(mTaskId);
601 } catch (RemoteException e) {
602 Log.w(TAG, "Failed to remove taskId " + mTaskId);
603 }
604 mTaskId = -1;
Mady Mellordea7ecf2018-12-10 15:47:40 -0800605 }
Mark Renouf28c250d2019-02-25 16:47:34 -0500606 removeView(mActivityView);
Mark Renouf5c732b32019-06-12 15:14:54 -0400607
Mady Mellor3dff9e62019-02-05 18:12:53 -0800608 mActivityView = null;
Issei Suzuki734bc942019-06-05 13:59:52 +0200609 }
610
611 /**
612 * Called when the last task is removed from a {@link android.hardware.display.VirtualDisplay}
613 * which {@link ActivityView} uses.
614 */
615 void notifyDisplayEmpty() {
Mark Renoufdb861c92019-07-26 13:58:17 -0400616 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
617 Log.d(TAG, "notifyDisplayEmpty: bubble="
618 + getBubbleKey()
619 + " mActivityViewStatus=" + mActivityViewStatus);
620 }
Issei Suzuki734bc942019-06-05 13:59:52 +0200621 if (mActivityViewStatus == ActivityViewStatus.ACTIVITY_STARTED) {
622 mActivityViewStatus = ActivityViewStatus.INITIALIZED;
623 }
Mady Mellordea7ecf2018-12-10 15:47:40 -0800624 }
625
Mady Mellor3dff9e62019-02-05 18:12:53 -0800626 private boolean usingActivityView() {
Lyn Hanb58c7562020-01-07 14:29:20 -0800627 return (mPendingIntent != null || mBubble.getShortcutInfo() != null)
Mady Mellor2ac2d3a2020-01-08 17:18:54 -0800628 && mActivityView != null;
Mady Mellor3dff9e62019-02-05 18:12:53 -0800629 }
630
Mady Mellor390bff42019-04-05 15:09:01 -0700631 /**
632 * @return the display id of the virtual display.
633 */
634 public int getVirtualDisplayId() {
635 if (usingActivityView()) {
636 return mActivityView.getVirtualDisplayId();
637 }
638 return INVALID_DISPLAY;
639 }
Yunfan Chenb83940c2020-04-06 16:43:09 +0900640
641 private VirtualDisplay getVirtualDisplay() {
642 if (usingActivityView()) {
643 return mActivityView.getVirtualDisplay();
644 }
645 return null;
646 }
Mady Mellordea7ecf2018-12-10 15:47:40 -0800647}