blob: 8722ac9a36b589f9f0ee0127af81bdb26c82a3ab [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;
Joshua Tsuji06785ab2020-06-08 11:18:40 -040025import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
Jorim Jaggi924ef752020-01-29 17:26:55 +010026import static android.view.ViewRootImpl.NEW_INSETS_MODE_FULL;
27import static android.view.ViewRootImpl.sNewInsetsMode;
Yunfan Chenb83940c2020-04-06 16:43:09 +090028import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
29import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
30import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
31import static android.view.WindowManager.LayoutParams.TYPE_NAVIGATION_BAR_PANEL;
Beverlya53fb0d2020-01-29 15:26:13 -050032
Mark Renoufdb861c92019-07-26 13:58:17 -040033import static com.android.systemui.bubbles.BubbleDebugConfig.DEBUG_BUBBLE_EXPANDED_VIEW;
Issei Suzukia8d07312019-06-07 12:56:19 +020034import static com.android.systemui.bubbles.BubbleDebugConfig.TAG_BUBBLES;
35import static com.android.systemui.bubbles.BubbleDebugConfig.TAG_WITH_CLASS_NAME;
Mady Mellorca0c24c2019-05-16 16:14:32 -070036
Joshua Tsuji847b1d422020-06-01 13:42:19 -040037import android.annotation.SuppressLint;
Tony Huang86724102020-06-11 17:51:55 +080038import android.app.ActivityManager;
Mady Mellor60101c92019-04-11 19:04:00 -070039import android.app.ActivityOptions;
Mark Renouf5c732b32019-06-12 15:14:54 -040040import android.app.ActivityTaskManager;
Mady Mellor3dff9e62019-02-05 18:12:53 -080041import android.app.ActivityView;
Mady Mellor9801e852019-01-22 14:50:28 -080042import android.app.PendingIntent;
Mark Renouf5c732b32019-06-12 15:14:54 -040043import android.content.ComponentName;
Mady Mellordea7ecf2018-12-10 15:47:40 -080044import android.content.Context;
Mady Mellor9801e852019-01-22 14:50:28 -080045import android.content.Intent;
Lyn Han26bb1982020-06-08 15:38:01 -070046import android.content.res.Configuration;
Mady Mellordea7ecf2018-12-10 15:47:40 -080047import android.content.res.Resources;
Mady Mellordd497052019-01-30 17:23:48 -080048import android.content.res.TypedArray;
Mady Mellordea7ecf2018-12-10 15:47:40 -080049import android.graphics.Color;
Mady Mellor3dff9e62019-02-05 18:12:53 -080050import android.graphics.Insets;
Joshua Tsuji06785ab2020-06-08 11:18:40 -040051import android.graphics.Outline;
Mady Mellor3dff9e62019-02-05 18:12:53 -080052import android.graphics.Point;
Mady Mellor47b11e32019-07-11 19:06:21 -070053import android.graphics.Rect;
Mady Mellordea7ecf2018-12-10 15:47:40 -080054import android.graphics.drawable.ShapeDrawable;
Yunfan Chenb83940c2020-04-06 16:43:09 +090055import android.hardware.display.VirtualDisplay;
56import android.os.Binder;
Mark Renouf5c732b32019-06-12 15:14:54 -040057import android.os.RemoteException;
Mady Mellordea7ecf2018-12-10 15:47:40 -080058import android.util.AttributeSet;
Mady Mellor9801e852019-01-22 14:50:28 -080059import android.util.Log;
Yunfan Chenb83940c2020-04-06 16:43:09 +090060import android.view.Gravity;
Joshua Tsuji06785ab2020-06-08 11:18:40 -040061import android.view.SurfaceControl;
62import android.view.SurfaceView;
Mady Mellordea7ecf2018-12-10 15:47:40 -080063import android.view.View;
Joshua Tsuji06785ab2020-06-08 11:18:40 -040064import android.view.ViewGroup;
65import android.view.ViewOutlineProvider;
Mady Mellor3dff9e62019-02-05 18:12:53 -080066import android.view.WindowInsets;
Mady Mellora96c9ed2019-06-07 12:55:26 -070067import android.view.WindowManager;
Lyn Han670fa802020-05-05 12:56:33 -070068import android.view.accessibility.AccessibilityNodeInfo;
Joshua Tsuji06785ab2020-06-08 11:18:40 -040069import android.widget.FrameLayout;
Mady Mellordea7ecf2018-12-10 15:47:40 -080070import android.widget.LinearLayout;
71
Joshua Tsuji06785ab2020-06-08 11:18:40 -040072import androidx.annotation.Nullable;
73
Mark Renouf34d04f32019-05-13 15:53:18 -040074import com.android.internal.policy.ScreenDecorationsUtils;
Mady Mellor3dff9e62019-02-05 18:12:53 -080075import com.android.systemui.Dependency;
Mady Mellordea7ecf2018-12-10 15:47:40 -080076import com.android.systemui.R;
77import com.android.systemui.recents.TriangleShape;
Lyn Han754e77b2019-04-30 14:34:49 -070078import com.android.systemui.statusbar.AlphaOptimizedButton;
Mady Mellordea7ecf2018-12-10 15:47:40 -080079
80/**
Lyn Han02cca812019-04-02 16:27:32 -070081 * Container for the expanded bubble view, handles rendering the caret and settings icon.
Mady Mellordea7ecf2018-12-10 15:47:40 -080082 */
Joshua Tsuji6855cab2020-04-16 01:05:39 -040083public class BubbleExpandedView extends LinearLayout {
Issei Suzukia8d07312019-06-07 12:56:19 +020084 private static final String TAG = TAG_WITH_CLASS_NAME ? "BubbleExpandedView" : TAG_BUBBLES;
Yunfan Chenb83940c2020-04-06 16:43:09 +090085 private static final String WINDOW_TITLE = "ImeInsetsWindowWithoutContent";
Mady Mellordea7ecf2018-12-10 15:47:40 -080086
Issei Suzuki734bc942019-06-05 13:59:52 +020087 private enum ActivityViewStatus {
88 // ActivityView is being initialized, cannot start an activity yet.
89 INITIALIZING,
90 // ActivityView is initialized, and ready to start an activity.
91 INITIALIZED,
92 // Activity runs in the ActivityView.
93 ACTIVITY_STARTED,
94 // ActivityView is released, so activity launching will no longer be permitted.
95 RELEASED,
96 }
Mady Mellordea7ecf2018-12-10 15:47:40 -080097
98 // The triangle pointing to the expanded view
99 private View mPointerView;
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800100 private int mPointerMargin;
Joshua Tsuji06785ab2020-06-08 11:18:40 -0400101 @Nullable private int[] mExpandedViewContainerLocation;
Mady Mellore8e07712019-01-23 12:45:33 -0800102
Lyn Han754e77b2019-04-30 14:34:49 -0700103 private AlphaOptimizedButton mSettingsIcon;
Mady Mellore8e07712019-01-23 12:45:33 -0800104
Mady Mellor3dff9e62019-02-05 18:12:53 -0800105 // Views for expanded state
Mady Mellor3dff9e62019-02-05 18:12:53 -0800106 private ActivityView mActivityView;
107
Issei Suzuki734bc942019-06-05 13:59:52 +0200108 private ActivityViewStatus mActivityViewStatus = ActivityViewStatus.INITIALIZING;
Mark Renouf5c732b32019-06-12 15:14:54 -0400109 private int mTaskId = -1;
110
Lyn Hanb58c7562020-01-07 14:29:20 -0800111 private PendingIntent mPendingIntent;
Mady Mellor3dff9e62019-02-05 18:12:53 -0800112
Mady Mellor5d8f1402019-02-21 18:23:52 -0800113 private boolean mKeyboardVisible;
114 private boolean mNeedsNewHeight;
115
Mady Mellora96c9ed2019-06-07 12:55:26 -0700116 private Point mDisplaySize;
Mady Mellorfe7ec032019-01-30 17:32:49 -0800117 private int mMinHeight;
Lyn Hancd4f87e2020-02-19 20:33:45 -0800118 private int mOverflowHeight;
Lyn Han02cca812019-04-02 16:27:32 -0700119 private int mSettingsIconHeight;
Lyn Han02cca812019-04-02 16:27:32 -0700120 private int mPointerWidth;
121 private int mPointerHeight;
Mark Renouf34d04f32019-05-13 15:53:18 -0400122 private ShapeDrawable mPointerDrawable;
Lyn Han39892602020-06-18 21:15:28 -0700123 private int mExpandedViewPadding;
124
Mady Mellordea7ecf2018-12-10 15:47:40 -0800125
Lyn Hanb58c7562020-01-07 14:29:20 -0800126 @Nullable private Bubble mBubble;
127
128 private boolean mIsOverflow;
Mady Mellore8e07712019-01-23 12:45:33 -0800129
Mady Mellor3dff9e62019-02-05 18:12:53 -0800130 private BubbleController mBubbleController = Dependency.get(BubbleController.class);
Mady Mellor9be3bed2019-08-21 17:26:26 -0700131 private WindowManager mWindowManager;
Tony Huang86724102020-06-11 17:51:55 +0800132 private ActivityManager mActivityManager;
Mady Mellor9801e852019-01-22 14:50:28 -0800133
Mady Mellor9801e852019-01-22 14:50:28 -0800134 private BubbleStackView mStackView;
Yunfan Chenb83940c2020-04-06 16:43:09 +0900135 private View mVirtualImeView;
136 private WindowManager mVirtualDisplayWindowManager;
137 private boolean mImeShowing = false;
Joshua Tsuji06785ab2020-06-08 11:18:40 -0400138 private float mCornerRadius = 0f;
139
140 /**
141 * Container for the ActivityView that has a solid, round-rect background that shows if the
142 * ActivityView hasn't loaded.
143 */
144 private FrameLayout mActivityViewContainer = new FrameLayout(getContext());
145
146 /** The SurfaceView that the ActivityView draws to. */
147 @Nullable private SurfaceView mActivitySurface;
Mady Mellor9801e852019-01-22 14:50:28 -0800148
Mady Mellor3dff9e62019-02-05 18:12:53 -0800149 private ActivityView.StateCallback mStateCallback = new ActivityView.StateCallback() {
150 @Override
151 public void onActivityViewReady(ActivityView view) {
Mark Renoufdb861c92019-07-26 13:58:17 -0400152 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
153 Log.d(TAG, "onActivityViewReady: mActivityViewStatus=" + mActivityViewStatus
154 + " bubble=" + getBubbleKey());
155 }
Issei Suzuki734bc942019-06-05 13:59:52 +0200156 switch (mActivityViewStatus) {
157 case INITIALIZING:
158 case INITIALIZED:
159 // Custom options so there is no activity transition animation
160 ActivityOptions options = ActivityOptions.makeCustomAnimation(getContext(),
161 0 /* enterResId */, 0 /* exitResId */);
Winson Chung06200522020-03-31 22:47:02 -0700162 options.setTaskAlwaysOnTop(true);
Yuncheol Heo5d62e752020-04-08 14:54:24 -0700163 options.setLaunchWindowingMode(WINDOWING_MODE_MULTI_WINDOW);
Issei Suzuki734bc942019-06-05 13:59:52 +0200164 // Post to keep the lifecycle normal
Mark Renoufdb861c92019-07-26 13:58:17 -0400165 post(() -> {
166 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
167 Log.d(TAG, "onActivityViewReady: calling startActivity, "
168 + "bubble=" + getBubbleKey());
169 }
Mady Mellorc1a75a92020-06-11 22:24:24 -0700170 if (mActivityView == null) {
171 mBubbleController.removeBubble(getBubbleKey(),
172 BubbleController.DISMISS_INVALID_INTENT);
173 return;
174 }
Mady Mellor8454ddf2019-08-15 11:16:23 -0700175 try {
Pinyao Ting175a5b82020-06-15 23:41:14 +0000176 if (!mIsOverflow && mBubble.getShortcutInfo() != null) {
Mady Mellor80d5ed12020-04-10 21:17:30 -0700177 options.setApplyActivityFlagsForBubbles(true);
Mady Mellorb547e5e2019-12-02 10:15:56 -0800178 mActivityView.startShortcutActivity(mBubble.getShortcutInfo(),
179 options, null /* sourceBounds */);
180 } else {
181 Intent fillInIntent = new Intent();
182 // Apply flags to make behaviour match documentLaunchMode=always.
183 fillInIntent.addFlags(FLAG_ACTIVITY_NEW_DOCUMENT);
184 fillInIntent.addFlags(FLAG_ACTIVITY_MULTIPLE_TASK);
Lyn Hanb58c7562020-01-07 14:29:20 -0800185 mActivityView.startActivity(mPendingIntent, fillInIntent, options);
Mady Mellorb547e5e2019-12-02 10:15:56 -0800186 }
Mady Mellor8454ddf2019-08-15 11:16:23 -0700187 } catch (RuntimeException e) {
188 // If there's a runtime exception here then there's something
189 // wrong with the intent, we can't really recover / try to populate
190 // the bubble again so we'll just remove it.
191 Log.w(TAG, "Exception while displaying bubble: " + getBubbleKey()
192 + ", " + e.getMessage() + "; removing bubble");
Pinyao Ting3c930612020-05-19 00:26:03 +0000193 mBubbleController.removeBubble(getBubbleKey(),
Mady Mellor8454ddf2019-08-15 11:16:23 -0700194 BubbleController.DISMISS_INVALID_INTENT);
195 }
Mark Renoufdb861c92019-07-26 13:58:17 -0400196 });
Issei Suzuki734bc942019-06-05 13:59:52 +0200197 mActivityViewStatus = ActivityViewStatus.ACTIVITY_STARTED;
Tony Huang86724102020-06-11 17:51:55 +0800198 break;
199 case ACTIVITY_STARTED:
200 post(() -> mActivityManager.moveTaskToFront(mTaskId, 0));
201 break;
Mady Mellor6d002032019-02-13 13:45:17 -0800202 }
Mady Mellor3dff9e62019-02-05 18:12:53 -0800203 }
204
205 @Override
206 public void onActivityViewDestroyed(ActivityView view) {
Mark Renoufdb861c92019-07-26 13:58:17 -0400207 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
208 Log.d(TAG, "onActivityViewDestroyed: mActivityViewStatus=" + mActivityViewStatus
209 + " bubble=" + getBubbleKey());
210 }
Issei Suzuki734bc942019-06-05 13:59:52 +0200211 mActivityViewStatus = ActivityViewStatus.RELEASED;
Mady Mellor3dff9e62019-02-05 18:12:53 -0800212 }
213
Mark Renouf5c732b32019-06-12 15:14:54 -0400214 @Override
215 public void onTaskCreated(int taskId, ComponentName componentName) {
Mark Renoufdb861c92019-07-26 13:58:17 -0400216 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
217 Log.d(TAG, "onTaskCreated: taskId=" + taskId
218 + " bubble=" + getBubbleKey());
219 }
Mark Renouf5c732b32019-06-12 15:14:54 -0400220 // Since Bubble ActivityView applies singleTaskDisplay this is
221 // guaranteed to only be called once per ActivityView. The taskId is
222 // saved to use for removeTask, preventing appearance in recent tasks.
223 mTaskId = taskId;
Mady Mellor3dff9e62019-02-05 18:12:53 -0800224 }
225
226 /**
227 * This is only called for tasks on this ActivityView, which is also set to
228 * single-task mode -- meaning never more than one task on this display. If a task
229 * is being removed, it's the top Activity finishing and this bubble should
230 * be removed or collapsed.
231 */
232 @Override
233 public void onTaskRemovalStarted(int taskId) {
Mark Renoufdb861c92019-07-26 13:58:17 -0400234 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
235 Log.d(TAG, "onTaskRemovalStarted: taskId=" + taskId
236 + " mActivityViewStatus=" + mActivityViewStatus
237 + " bubble=" + getBubbleKey());
238 }
Mady Mellordd6fe612020-04-15 11:47:55 -0700239 if (mBubble != null) {
Mady Mellorff076eb2019-11-13 10:12:06 -0800240 // Must post because this is called from a binder thread.
Pinyao Ting3c930612020-05-19 00:26:03 +0000241 post(() -> mBubbleController.removeBubble(mBubble.getKey(),
Mady Mellorff076eb2019-11-13 10:12:06 -0800242 BubbleController.DISMISS_TASK_FINISHED));
Mady Mellor3dff9e62019-02-05 18:12:53 -0800243 }
244 }
245 };
Mady Mellore8e07712019-01-23 12:45:33 -0800246
Mady Mellor3d82e682019-02-05 13:34:48 -0800247 public BubbleExpandedView(Context context) {
Mady Mellordea7ecf2018-12-10 15:47:40 -0800248 this(context, null);
249 }
250
Mady Mellor3d82e682019-02-05 13:34:48 -0800251 public BubbleExpandedView(Context context, AttributeSet attrs) {
Mady Mellordea7ecf2018-12-10 15:47:40 -0800252 this(context, attrs, 0);
253 }
254
Mady Mellor3d82e682019-02-05 13:34:48 -0800255 public BubbleExpandedView(Context context, AttributeSet attrs, int defStyleAttr) {
Mady Mellordea7ecf2018-12-10 15:47:40 -0800256 this(context, attrs, defStyleAttr, 0);
257 }
258
Mady Mellor3d82e682019-02-05 13:34:48 -0800259 public BubbleExpandedView(Context context, AttributeSet attrs, int defStyleAttr,
Mady Mellordea7ecf2018-12-10 15:47:40 -0800260 int defStyleRes) {
261 super(context, attrs, defStyleAttr, defStyleRes);
Lyn Hanbc985c52020-05-18 17:57:58 -0700262 updateDimensions();
Tony Huang86724102020-06-11 17:51:55 +0800263 mActivityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
Lyn Hanbc985c52020-05-18 17:57:58 -0700264 }
265
266 void updateDimensions() {
Mady Mellora96c9ed2019-06-07 12:55:26 -0700267 mDisplaySize = new Point();
Lyn Hanbc985c52020-05-18 17:57:58 -0700268 mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
Mady Mellor8fe411d2019-08-16 11:27:53 -0700269 // Get the real size -- this includes screen decorations (notches, statusbar, navbar).
Mady Mellor9be3bed2019-08-21 17:26:26 -0700270 mWindowManager.getDefaultDisplay().getRealSize(mDisplaySize);
Mady Mellor47b11e32019-07-11 19:06:21 -0700271 Resources res = getResources();
272 mMinHeight = res.getDimensionPixelSize(R.dimen.bubble_expanded_default_height);
Lyn Hancd4f87e2020-02-19 20:33:45 -0800273 mOverflowHeight = res.getDimensionPixelSize(R.dimen.bubble_overflow_height);
Mady Mellor47b11e32019-07-11 19:06:21 -0700274 mPointerMargin = res.getDimensionPixelSize(R.dimen.bubble_pointer_margin);
Mady Mellordea7ecf2018-12-10 15:47:40 -0800275 }
276
Joshua Tsuji847b1d422020-06-01 13:42:19 -0400277 @SuppressLint("ClickableViewAccessibility")
Mady Mellordea7ecf2018-12-10 15:47:40 -0800278 @Override
279 protected void onFinishInflate() {
280 super.onFinishInflate();
Mark Renoufdb861c92019-07-26 13:58:17 -0400281 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
282 Log.d(TAG, "onFinishInflate: bubble=" + getBubbleKey());
283 }
Mady Mellordea7ecf2018-12-10 15:47:40 -0800284
285 Resources res = getResources();
286 mPointerView = findViewById(R.id.pointer_view);
Lyn Han02cca812019-04-02 16:27:32 -0700287 mPointerWidth = res.getDimensionPixelSize(R.dimen.bubble_pointer_width);
288 mPointerHeight = res.getDimensionPixelSize(R.dimen.bubble_pointer_height);
Mady Mellordd497052019-01-30 17:23:48 -0800289
Mark Renouf34d04f32019-05-13 15:53:18 -0400290 mPointerDrawable = new ShapeDrawable(TriangleShape.create(
Lyn Han5aa27e22019-05-15 10:55:07 -0700291 mPointerWidth, mPointerHeight, true /* pointUp */));
Mady Mellora96c9ed2019-06-07 12:55:26 -0700292 mPointerView.setVisibility(INVISIBLE);
Mady Mellor9801e852019-01-22 14:50:28 -0800293
Lyn Han02cca812019-04-02 16:27:32 -0700294 mSettingsIconHeight = getContext().getResources().getDimensionPixelSize(
Mady Mellor5a3e94b2020-02-07 12:16:21 -0800295 R.dimen.bubble_manage_button_height);
Lyn Han02cca812019-04-02 16:27:32 -0700296 mSettingsIcon = findViewById(R.id.settings_button);
Lyn Han02cca812019-04-02 16:27:32 -0700297
Mady Mellor3dff9e62019-02-05 18:12:53 -0800298 mActivityView = new ActivityView(mContext, null /* attrs */, 0 /* defStyle */,
shawnlinba532af2020-06-02 17:48:37 +0800299 true /* singleTaskInstance */, false /* usePublicVirtualDisplay*/,
300 true /* disableSurfaceViewBackgroundLayer */);
Lyn Hanb58c7562020-01-07 14:29:20 -0800301
lumarkdc9b3192019-07-23 21:18:17 +0800302 // Set ActivityView's alpha value as zero, since there is no view content to be shown.
Issei Suzukicac2a502019-04-16 16:52:50 +0200303 setContentVisibility(false);
Joshua Tsuji06785ab2020-06-08 11:18:40 -0400304
305 mActivityViewContainer.setBackgroundColor(Color.WHITE);
306 mActivityViewContainer.setOutlineProvider(new ViewOutlineProvider() {
307 @Override
308 public void getOutline(View view, Outline outline) {
309 outline.setRoundRect(0, 0, view.getWidth(), view.getHeight(), mCornerRadius);
310 }
311 });
312 mActivityViewContainer.setClipToOutline(true);
313 mActivityViewContainer.addView(mActivityView);
314 mActivityViewContainer.setLayoutParams(
315 new ViewGroup.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
316 addView(mActivityViewContainer);
317
318 if (mActivityView != null
319 && mActivityView.getChildCount() > 0
320 && mActivityView.getChildAt(0) instanceof SurfaceView) {
321 // Retrieve the surface from the ActivityView so we can screenshot it and change its
322 // z-ordering. This should always be possible, since ActivityView's constructor adds the
323 // SurfaceView as its first child.
324 mActivitySurface = (SurfaceView) mActivityView.getChildAt(0);
325 }
Mady Mellor3dff9e62019-02-05 18:12:53 -0800326
Lyn Han5aa27e22019-05-15 10:55:07 -0700327 // Expanded stack layout, top to bottom:
328 // Expanded view container
329 // ==> bubble row
330 // ==> expanded view
331 // ==> activity view
332 // ==> manage button
333 bringChildToFront(mActivityView);
334 bringChildToFront(mSettingsIcon);
Mady Mellor52b1ac62019-04-10 16:59:03 -0700335
Mark Renouf34d04f32019-05-13 15:53:18 -0400336 applyThemeAttrs();
337
Mady Mellor5d8f1402019-02-21 18:23:52 -0800338 setOnApplyWindowInsetsListener((View view, WindowInsets insets) -> {
339 // Keep track of IME displaying because we should not make any adjustments that might
340 // cause a config change while the IME is displayed otherwise it'll loose focus.
Mady Mellor3dff9e62019-02-05 18:12:53 -0800341 final int keyboardHeight = insets.getSystemWindowInsetBottom()
342 - insets.getStableInsetBottom();
Mady Mellor5d8f1402019-02-21 18:23:52 -0800343 mKeyboardVisible = keyboardHeight != 0;
344 if (!mKeyboardVisible && mNeedsNewHeight) {
345 updateHeight();
346 }
Mady Mellor3dff9e62019-02-05 18:12:53 -0800347 return view.onApplyWindowInsets(insets);
348 });
Joshua Tsuji847b1d422020-06-01 13:42:19 -0400349
Lyn Han39892602020-06-18 21:15:28 -0700350 mExpandedViewPadding = res.getDimensionPixelSize(R.dimen.bubble_expanded_view_padding);
351 setPadding(mExpandedViewPadding, mExpandedViewPadding, mExpandedViewPadding,
352 mExpandedViewPadding);
Joshua Tsuji847b1d422020-06-01 13:42:19 -0400353 setOnTouchListener((view, motionEvent) -> {
354 if (!usingActivityView()) {
355 return false;
356 }
357
358 final Rect avBounds = new Rect();
359 mActivityView.getBoundsOnScreen(avBounds);
360
361 // Consume and ignore events on the expanded view padding that are within the
362 // ActivityView's vertical bounds. These events are part of a back gesture, and so they
363 // should not collapse the stack (which all other touches on areas around the AV would
364 // do).
Josh Tsuji5421eb62020-06-10 18:24:56 -0400365 if (motionEvent.getRawY() >= avBounds.top
366 && motionEvent.getRawY() <= avBounds.bottom
367 && (motionEvent.getRawX() < avBounds.left
368 || motionEvent.getRawX() > avBounds.right)) {
Joshua Tsuji847b1d422020-06-01 13:42:19 -0400369 return true;
370 }
371
372 return false;
373 });
Joshua Tsuji519d4c12020-05-29 15:22:31 -0400374
375 // BubbleStackView is forced LTR, but we want to respect the locale for expanded view layout
376 // so the Manage button appears on the right.
377 setLayoutDirection(LAYOUT_DIRECTION_LOCALE);
Mady Mellore8e07712019-01-23 12:45:33 -0800378 }
379
Mark Renoufdb861c92019-07-26 13:58:17 -0400380 private String getBubbleKey() {
381 return mBubble != null ? mBubble.getKey() : "null";
382 }
383
Joshua Tsuji06785ab2020-06-08 11:18:40 -0400384 /**
385 * Asks the ActivityView's surface to draw on top of all other views in the window. This is
386 * useful for ordering surfaces during animations, but should otherwise be set to false so that
387 * bubbles and menus can draw over the ActivityView.
388 */
389 void setSurfaceZOrderedOnTop(boolean onTop) {
390 if (mActivitySurface == null) {
391 return;
392 }
393
394 mActivitySurface.setZOrderedOnTop(onTop, true);
395 }
396
397 /** Return a GraphicBuffer with the contents of the ActivityView's underlying surface. */
398 @Nullable SurfaceControl.ScreenshotGraphicBuffer snapshotActivitySurface() {
399 if (mActivitySurface == null) {
400 return null;
401 }
402
403 return SurfaceControl.captureLayers(
404 mActivitySurface.getSurfaceControl(),
405 new Rect(0, 0, mActivityView.getWidth(), mActivityView.getHeight()),
406 1 /* scale */);
407 }
408
409 int[] getActivityViewLocationOnScreen() {
410 if (mActivityView != null) {
411 return mActivityView.getLocationOnScreen();
412 } else {
413 return new int[]{0, 0};
414 }
415 }
416
Joshua Tsuji6855cab2020-04-16 01:05:39 -0400417 void setManageClickListener(OnClickListener manageClickListener) {
418 findViewById(R.id.settings_button).setOnClickListener(manageClickListener);
419 }
420
421 /**
422 * Updates the ActivityView's obscured touchable region. This calls onLocationChanged, which
423 * results in a call to {@link BubbleStackView#subtractObscuredTouchableRegion}. This is useful
424 * if a view has been added or removed from on top of the ActivityView, such as the manage menu.
425 */
426 void updateObscuredTouchableRegion() {
Mady Mellor64db51a2020-05-27 19:00:14 -0700427 if (mActivityView != null) {
428 mActivityView.onLocationChanged();
429 }
Joshua Tsuji6855cab2020-04-16 01:05:39 -0400430 }
431
Mark Renouf34d04f32019-05-13 15:53:18 -0400432 void applyThemeAttrs() {
Lyn Han61bfdb32019-12-10 14:34:12 -0800433 final TypedArray ta = mContext.obtainStyledAttributes(
Joshua Tsujidd082b62020-05-20 22:26:24 -0400434 new int[] {android.R.attr.dialogCornerRadius});
Joshua Tsuji06785ab2020-06-08 11:18:40 -0400435 mCornerRadius = ta.getDimensionPixelSize(0, 0);
Mark Renouf34d04f32019-05-13 15:53:18 -0400436 ta.recycle();
437
Lyn Hanedb55e22020-02-12 18:55:39 -0800438 if (mActivityView != null && ScreenDecorationsUtils.supportsRoundedCornersOnWindows(
439 mContext.getResources())) {
Joshua Tsuji06785ab2020-06-08 11:18:40 -0400440 mActivityView.setCornerRadius(mCornerRadius);
Mark Renouf34d04f32019-05-13 15:53:18 -0400441 }
Lyn Han26bb1982020-06-08 15:38:01 -0700442
443 final int mode =
444 getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
445 switch (mode) {
446 case Configuration.UI_MODE_NIGHT_NO:
Lyn Han79d10bf2020-06-11 14:36:06 -0700447 mPointerDrawable.setTint(getResources().getColor(R.color.bubbles_light));
Lyn Han26bb1982020-06-08 15:38:01 -0700448 break;
449 case Configuration.UI_MODE_NIGHT_YES:
Lyn Han79d10bf2020-06-11 14:36:06 -0700450 mPointerDrawable.setTint(getResources().getColor(R.color.bubbles_dark));
Lyn Han26bb1982020-06-08 15:38:01 -0700451 break;
452 }
453 mPointerView.setBackground(mPointerDrawable);
Mark Renouf34d04f32019-05-13 15:53:18 -0400454 }
455
Joshua Tsuji847b1d422020-06-01 13:42:19 -0400456 /**
457 * Hides the IME if it's showing. This is currently done by dispatching a back press to the AV.
458 */
459 void hideImeIfVisible() {
460 if (mKeyboardVisible) {
461 performBackPressIfNeeded();
462 }
463 }
464
Mady Mellor5d8f1402019-02-21 18:23:52 -0800465 @Override
466 protected void onDetachedFromWindow() {
467 super.onDetachedFromWindow();
Joshua Tsuji847b1d422020-06-01 13:42:19 -0400468 hideImeIfVisible();
Mady Mellor5d8f1402019-02-21 18:23:52 -0800469 mKeyboardVisible = false;
470 mNeedsNewHeight = false;
471 if (mActivityView != null) {
Mady Melloraf02fa42020-03-04 14:22:16 -0800472 if (sNewInsetsMode == NEW_INSETS_MODE_FULL) {
Yunfan Chenb83940c2020-04-06 16:43:09 +0900473 setImeWindowToDisplay(0, 0);
Mady Melloraf02fa42020-03-04 14:22:16 -0800474 } else {
475 mActivityView.setForwardedInsets(Insets.of(0, 0, 0, 0));
476 }
Mady Mellor5d8f1402019-02-21 18:23:52 -0800477 }
Mark Renoufdb861c92019-07-26 13:58:17 -0400478 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
479 Log.d(TAG, "onDetachedFromWindow: bubble=" + getBubbleKey());
480 }
Mady Mellor5d8f1402019-02-21 18:23:52 -0800481 }
482
483 /**
Issei Suzukicac2a502019-04-16 16:52:50 +0200484 * Set visibility of contents in the expanded state.
485 *
486 * @param visibility {@code true} if the contents should be visible on the screen.
487 *
488 * Note that this contents visibility doesn't affect visibility at {@link android.view.View},
489 * and setting {@code false} actually means rendering the contents in transparent.
490 */
491 void setContentVisibility(boolean visibility) {
Mark Renoufdb861c92019-07-26 13:58:17 -0400492 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
493 Log.d(TAG, "setContentVisibility: visibility=" + visibility
494 + " bubble=" + getBubbleKey());
495 }
Issei Suzukicac2a502019-04-16 16:52:50 +0200496 final float alpha = visibility ? 1f : 0f;
497 mPointerView.setAlpha(alpha);
498 if (mActivityView != null) {
499 mActivityView.setAlpha(alpha);
Joshua Tsuji06785ab2020-06-08 11:18:40 -0400500 mActivityView.bringToFront();
Issei Suzukicac2a502019-04-16 16:52:50 +0200501 }
502 }
503
504 /**
Mady Mellor5d8f1402019-02-21 18:23:52 -0800505 * Called by {@link BubbleStackView} when the insets for the expanded state should be updated.
506 * This should be done post-move and post-animation.
507 */
508 void updateInsets(WindowInsets insets) {
509 if (usingActivityView()) {
Mady Mellor8fe411d2019-08-16 11:27:53 -0700510 int[] screenLoc = mActivityView.getLocationOnScreen();
511 final int activityViewBottom = screenLoc[1] + mActivityView.getHeight();
Mady Mellor9be3bed2019-08-21 17:26:26 -0700512 final int keyboardTop = mDisplaySize.y - Math.max(insets.getSystemWindowInsetBottom(),
513 insets.getDisplayCutout() != null
514 ? insets.getDisplayCutout().getSafeInsetBottom()
515 : 0);
Mady Mellor8fe411d2019-08-16 11:27:53 -0700516 final int insetsBottom = Math.max(activityViewBottom - keyboardTop, 0);
Jorim Jaggi924ef752020-01-29 17:26:55 +0100517
Jorim Jaggi924ef752020-01-29 17:26:55 +0100518 if (sNewInsetsMode == NEW_INSETS_MODE_FULL) {
Yunfan Chenb83940c2020-04-06 16:43:09 +0900519 setImeWindowToDisplay(getWidth(), insetsBottom);
Jorim Jaggi924ef752020-01-29 17:26:55 +0100520 } else {
521 mActivityView.setForwardedInsets(Insets.of(0, 0, 0, insetsBottom));
522 }
Mady Mellor5d8f1402019-02-21 18:23:52 -0800523 }
524 }
525
Yunfan Chenb83940c2020-04-06 16:43:09 +0900526 private void setImeWindowToDisplay(int w, int h) {
527 if (getVirtualDisplayId() == INVALID_DISPLAY) {
528 return;
529 }
530 if (h == 0 || w == 0) {
531 if (mImeShowing) {
532 mVirtualImeView.setVisibility(GONE);
533 mImeShowing = false;
534 }
535 return;
536 }
537 final Context virtualDisplayContext = mContext.createDisplayContext(
538 getVirtualDisplay().getDisplay());
539
540 if (mVirtualDisplayWindowManager == null) {
541 mVirtualDisplayWindowManager =
542 (WindowManager) virtualDisplayContext.getSystemService(Context.WINDOW_SERVICE);
543 }
544 if (mVirtualImeView == null) {
545 mVirtualImeView = new View(virtualDisplayContext);
546 mVirtualImeView.setVisibility(VISIBLE);
547 mVirtualDisplayWindowManager.addView(mVirtualImeView,
548 getVirtualImeViewAttrs(w, h));
549 } else {
550 mVirtualDisplayWindowManager.updateViewLayout(mVirtualImeView,
551 getVirtualImeViewAttrs(w, h));
552 mVirtualImeView.setVisibility(VISIBLE);
553 }
554
555 mImeShowing = true;
556 }
557
558 private WindowManager.LayoutParams getVirtualImeViewAttrs(int w, int h) {
559 // To use TYPE_NAVIGATION_BAR_PANEL instead of TYPE_IME_BAR to bypass the IME window type
560 // token check when adding the window.
561 final WindowManager.LayoutParams attrs =
562 new WindowManager.LayoutParams(w, h, TYPE_NAVIGATION_BAR_PANEL,
563 FLAG_LAYOUT_NO_LIMITS | FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCHABLE,
564 TRANSPARENT);
565 attrs.gravity = Gravity.BOTTOM;
566 attrs.setTitle(WINDOW_TITLE);
567 attrs.token = new Binder();
568 attrs.providesInsetsTypes = new int[]{ITYPE_IME};
569 attrs.alpha = 0.0f;
570 return attrs;
571 }
572
Mady Mellor247ca2c2019-12-02 16:18:59 -0800573 void setStackView(BubbleStackView stackView) {
Mady Mellor9801e852019-01-22 14:50:28 -0800574 mStackView = stackView;
Mady Mellor6d002032019-02-13 13:45:17 -0800575 }
576
Lyn Hanb58c7562020-01-07 14:29:20 -0800577 public void setOverflow(boolean overflow) {
578 mIsOverflow = overflow;
579
580 Intent target = new Intent(mContext, BubbleOverflowActivity.class);
581 mPendingIntent = PendingIntent.getActivity(mContext, /* requestCode */ 0,
582 target, PendingIntent.FLAG_UPDATE_CURRENT);
583 mSettingsIcon.setVisibility(GONE);
584 }
585
Mady Mellor6d002032019-02-13 13:45:17 -0800586 /**
Mady Mellor247ca2c2019-12-02 16:18:59 -0800587 * Sets the bubble used to populate this view.
Mady Mellor6d002032019-02-13 13:45:17 -0800588 */
Mady Mellor247ca2c2019-12-02 16:18:59 -0800589 void update(Bubble bubble) {
590 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
591 Log.d(TAG, "update: bubble=" + (bubble != null ? bubble.getKey() : "null"));
592 }
Mady Mellor458a6262020-06-07 21:09:19 -0700593 boolean isNew = mBubble == null || didBackingContentChange(bubble);
Lyn Hanb58c7562020-01-07 14:29:20 -0800594 if (isNew || bubble != null && bubble.getKey().equals(mBubble.getKey())) {
Mady Mellor247ca2c2019-12-02 16:18:59 -0800595 mBubble = bubble;
596 mSettingsIcon.setContentDescription(getResources().getString(
597 R.string.bubbles_settings_button_description, bubble.getAppName()));
598
Lyn Han670fa802020-05-05 12:56:33 -0700599 mSettingsIcon.setAccessibilityDelegate(
600 new AccessibilityDelegate() {
601 @Override
602 public void onInitializeAccessibilityNodeInfo(View host,
603 AccessibilityNodeInfo info) {
604 super.onInitializeAccessibilityNodeInfo(host, info);
605 // On focus, have TalkBack say
606 // "Actions available. Use swipe up then right to view."
607 // in addition to the default "double tap to activate".
608 mStackView.setupLocalMenu(info);
609 }
610 });
611
Mady Mellor247ca2c2019-12-02 16:18:59 -0800612 if (isNew) {
Lyn Hanb58c7562020-01-07 14:29:20 -0800613 mPendingIntent = mBubble.getBubbleIntent();
614 if (mPendingIntent != null || mBubble.getShortcutInfo() != null) {
Mady Mellor247ca2c2019-12-02 16:18:59 -0800615 setContentVisibility(false);
616 mActivityView.setVisibility(VISIBLE);
617 }
618 }
619 applyThemeAttrs();
620 } else {
621 Log.w(TAG, "Trying to update entry with different key, new bubble: "
622 + bubble.getKey() + " old bubble: " + bubble.getKey());
623 }
624 }
625
Mady Mellor458a6262020-06-07 21:09:19 -0700626 private boolean didBackingContentChange(Bubble newBubble) {
627 boolean prevWasIntentBased = mBubble != null && mPendingIntent != null;
628 boolean newIsIntentBased = newBubble.getBubbleIntent() != null;
629 return prevWasIntentBased != newIsIntentBased;
630 }
631
Mady Mellor247ca2c2019-12-02 16:18:59 -0800632 /**
633 * Lets activity view know it should be shown / populated with activity content.
634 */
635 void populateExpandedView() {
Mark Renoufdb861c92019-07-26 13:58:17 -0400636 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
637 Log.d(TAG, "populateExpandedView: "
638 + "bubble=" + getBubbleKey());
639 }
640
Mady Mellor5029fa62019-03-05 12:16:21 -0800641 if (usingActivityView()) {
642 mActivityView.setCallback(mStateCallback);
643 } else {
Issei Suzukia91f3962019-06-07 11:48:23 +0200644 Log.e(TAG, "Cannot populate expanded view.");
Mady Mellor5029fa62019-03-05 12:16:21 -0800645 }
Mady Mellor9801e852019-01-22 14:50:28 -0800646 }
647
Mark Renouf041d7262019-02-06 12:09:41 -0500648 boolean performBackPressIfNeeded() {
Mady Mellor323fb0b2019-03-25 12:15:22 -0700649 if (!usingActivityView()) {
Mark Renouf041d7262019-02-06 12:09:41 -0500650 return false;
651 }
652 mActivityView.performBackPress();
653 return true;
654 }
655
Mady Mellorfe7ec032019-01-30 17:32:49 -0800656 void updateHeight() {
Mark Renoufdb861c92019-07-26 13:58:17 -0400657 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
658 Log.d(TAG, "updateHeight: bubble=" + getBubbleKey());
659 }
Joshua Tsuji06785ab2020-06-08 11:18:40 -0400660
661 if (mExpandedViewContainerLocation == null) {
662 return;
663 }
664
Mady Mellorfe7ec032019-01-30 17:32:49 -0800665 if (usingActivityView()) {
Lyn Hancd4f87e2020-02-19 20:33:45 -0800666 float desiredHeight = mOverflowHeight;
Lyn Hanb58c7562020-01-07 14:29:20 -0800667 if (!mIsOverflow) {
668 desiredHeight = Math.max(mBubble.getDesiredHeight(mContext), mMinHeight);
669 }
Mady Mellor8fe411d2019-08-16 11:27:53 -0700670 float height = Math.min(desiredHeight, getMaxExpandedHeight());
Lyn Han55f53a52020-06-12 01:19:05 -0700671 height = Math.max(height, mMinHeight);
Joshua Tsuji06785ab2020-06-08 11:18:40 -0400672 ViewGroup.LayoutParams lp = mActivityView.getLayoutParams();
Lyn Hancd4f87e2020-02-19 20:33:45 -0800673 mNeedsNewHeight = lp.height != height;
Mady Mellor5d8f1402019-02-21 18:23:52 -0800674 if (!mKeyboardVisible) {
675 // If the keyboard is visible... don't adjust the height because that will cause
676 // a configuration change and the keyboard will be lost.
Mady Mellor7af771a2019-03-07 15:04:54 -0800677 lp.height = (int) height;
Mady Mellor5d8f1402019-02-21 18:23:52 -0800678 mActivityView.setLayoutParams(lp);
679 mNeedsNewHeight = false;
680 }
Mark Renoufdb861c92019-07-26 13:58:17 -0400681 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
Joshua Tsuji06785ab2020-06-08 11:18:40 -0400682 Log.d(TAG, "updateHeight: bubble=" + getBubbleKey()
683 + " height=" + height
Mark Renoufdb861c92019-07-26 13:58:17 -0400684 + " mNeedsNewHeight=" + mNeedsNewHeight);
685 }
Mady Mellorfe7ec032019-01-30 17:32:49 -0800686 }
687 }
688
Mady Mellora96c9ed2019-06-07 12:55:26 -0700689 private int getMaxExpandedHeight() {
Mady Mellor9be3bed2019-08-21 17:26:26 -0700690 mWindowManager.getDefaultDisplay().getRealSize(mDisplaySize);
Mady Mellor8fe411d2019-08-16 11:27:53 -0700691 int bottomInset = getRootWindowInsets() != null
692 ? getRootWindowInsets().getStableInsetBottom()
693 : 0;
Joshua Tsuji06785ab2020-06-08 11:18:40 -0400694
695 return mDisplaySize.y
696 - mExpandedViewContainerLocation[1]
697 - getPaddingTop()
698 - getPaddingBottom()
699 - mSettingsIconHeight
700 - mPointerHeight
Mady Mellor8fe411d2019-08-16 11:27:53 -0700701 - mPointerMargin - bottomInset;
Mady Mellora96c9ed2019-06-07 12:55:26 -0700702 }
703
Mady Mellor47b11e32019-07-11 19:06:21 -0700704 /**
Mady Mellor3dff9e62019-02-05 18:12:53 -0800705 * Update appearance of the expanded view being displayed.
Joshua Tsuji06785ab2020-06-08 11:18:40 -0400706 *
707 * @param containerLocationOnScreen The location on-screen of the container the expanded view is
708 * added to. This allows us to calculate max height without
709 * waiting for layout.
Mady Mellor3dff9e62019-02-05 18:12:53 -0800710 */
Joshua Tsuji06785ab2020-06-08 11:18:40 -0400711 public void updateView(int[] containerLocationOnScreen) {
Mark Renoufdb861c92019-07-26 13:58:17 -0400712 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
713 Log.d(TAG, "updateView: bubble="
714 + getBubbleKey());
715 }
Joshua Tsuji06785ab2020-06-08 11:18:40 -0400716
717 mExpandedViewContainerLocation = containerLocationOnScreen;
718
Mady Mellor3dff9e62019-02-05 18:12:53 -0800719 if (usingActivityView()
720 && mActivityView.getVisibility() == VISIBLE
721 && mActivityView.isAttachedToWindow()) {
722 mActivityView.onLocationChanged();
Joshua Tsuji06785ab2020-06-08 11:18:40 -0400723 updateHeight();
Mady Mellor3dff9e62019-02-05 18:12:53 -0800724 }
725 }
726
727 /**
Mady Mellordea7ecf2018-12-10 15:47:40 -0800728 * Set the x position that the tip of the triangle should point to.
729 */
Mady Mellor3dff9e62019-02-05 18:12:53 -0800730 public void setPointerPosition(float x) {
Lyn Han9a2f5cf2019-05-23 11:01:41 -0700731 float halfPointerWidth = mPointerWidth / 2f;
Lyn Han39892602020-06-18 21:15:28 -0700732 float pointerLeft = x - halfPointerWidth - mExpandedViewPadding;
Lyn Han9a2f5cf2019-05-23 11:01:41 -0700733 mPointerView.setTranslationX(pointerLeft);
Lyn Hanf74ba672019-05-20 16:08:48 -0700734 mPointerView.setVisibility(VISIBLE);
Mady Mellordea7ecf2018-12-10 15:47:40 -0800735 }
736
737 /**
Mady Mellor5a3e94b2020-02-07 12:16:21 -0800738 * Position of the manage button displayed in the expanded view. Used for placing user
739 * education about the manage button.
740 */
Joshua Tsuji6855cab2020-04-16 01:05:39 -0400741 public void getManageButtonBoundsOnScreen(Rect rect) {
742 mSettingsIcon.getBoundsOnScreen(rect);
Mady Mellor5a3e94b2020-02-07 12:16:21 -0800743 }
744
745 /**
Mady Mellor3dff9e62019-02-05 18:12:53 -0800746 * Removes and releases an ActivityView if one was previously created for this bubble.
Mady Mellordea7ecf2018-12-10 15:47:40 -0800747 */
Mady Mellor94d94a72019-03-05 18:16:59 -0800748 public void cleanUpExpandedState() {
Mark Renoufdb861c92019-07-26 13:58:17 -0400749 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
750 Log.d(TAG, "cleanUpExpandedState: mActivityViewStatus=" + mActivityViewStatus
751 + ", bubble=" + getBubbleKey());
752 }
Mady Mellor3dff9e62019-02-05 18:12:53 -0800753 if (mActivityView == null) {
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500754 return;
755 }
shawnlin28062692020-06-15 13:55:51 +0800756 mActivityView.release();
Mark Renouf5c732b32019-06-12 15:14:54 -0400757 if (mTaskId != -1) {
758 try {
759 ActivityTaskManager.getService().removeTask(mTaskId);
760 } catch (RemoteException e) {
761 Log.w(TAG, "Failed to remove taskId " + mTaskId);
762 }
763 mTaskId = -1;
Mady Mellordea7ecf2018-12-10 15:47:40 -0800764 }
Mark Renouf28c250d2019-02-25 16:47:34 -0500765 removeView(mActivityView);
Mark Renouf5c732b32019-06-12 15:14:54 -0400766
Mady Mellor3dff9e62019-02-05 18:12:53 -0800767 mActivityView = null;
Issei Suzuki734bc942019-06-05 13:59:52 +0200768 }
769
770 /**
771 * Called when the last task is removed from a {@link android.hardware.display.VirtualDisplay}
772 * which {@link ActivityView} uses.
773 */
774 void notifyDisplayEmpty() {
Mark Renoufdb861c92019-07-26 13:58:17 -0400775 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
776 Log.d(TAG, "notifyDisplayEmpty: bubble="
777 + getBubbleKey()
778 + " mActivityViewStatus=" + mActivityViewStatus);
779 }
Issei Suzuki734bc942019-06-05 13:59:52 +0200780 if (mActivityViewStatus == ActivityViewStatus.ACTIVITY_STARTED) {
781 mActivityViewStatus = ActivityViewStatus.INITIALIZED;
782 }
Mady Mellordea7ecf2018-12-10 15:47:40 -0800783 }
784
Mady Mellor3dff9e62019-02-05 18:12:53 -0800785 private boolean usingActivityView() {
Lyn Hanb58c7562020-01-07 14:29:20 -0800786 return (mPendingIntent != null || mBubble.getShortcutInfo() != null)
Mady Mellor2ac2d3a2020-01-08 17:18:54 -0800787 && mActivityView != null;
Mady Mellor3dff9e62019-02-05 18:12:53 -0800788 }
789
Mady Mellor390bff42019-04-05 15:09:01 -0700790 /**
791 * @return the display id of the virtual display.
792 */
793 public int getVirtualDisplayId() {
794 if (usingActivityView()) {
795 return mActivityView.getVirtualDisplayId();
796 }
797 return INVALID_DISPLAY;
798 }
Yunfan Chenb83940c2020-04-06 16:43:09 +0900799
800 private VirtualDisplay getVirtualDisplay() {
801 if (usingActivityView()) {
802 return mActivityView.getVirtualDisplay();
803 }
804 return null;
805 }
Mady Mellordea7ecf2018-12-10 15:47:40 -0800806}