blob: 496456decceec188ca323e2cb79656cd93a33442 [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
Mady Mellorc41ed322019-09-25 11:19:26 -070019import static android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
20import static android.content.Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
Mady Mellor390bff42019-04-05 15:09:01 -070021import static android.view.Display.INVALID_DISPLAY;
Jorim Jaggi924ef752020-01-29 17:26:55 +010022import static android.view.ViewRootImpl.NEW_INSETS_MODE_FULL;
23import static android.view.ViewRootImpl.sNewInsetsMode;
Beverlya53fb0d2020-01-29 15:26:13 -050024
Mark Renoufdb861c92019-07-26 13:58:17 -040025import static com.android.systemui.bubbles.BubbleDebugConfig.DEBUG_BUBBLE_EXPANDED_VIEW;
Issei Suzukia8d07312019-06-07 12:56:19 +020026import static com.android.systemui.bubbles.BubbleDebugConfig.TAG_BUBBLES;
27import static com.android.systemui.bubbles.BubbleDebugConfig.TAG_WITH_CLASS_NAME;
Mady Mellorca0c24c2019-05-16 16:14:32 -070028
Lyn Hanb58c7562020-01-07 14:29:20 -080029import android.annotation.Nullable;
Mady Mellor60101c92019-04-11 19:04:00 -070030import android.app.ActivityOptions;
Mark Renouf5c732b32019-06-12 15:14:54 -040031import android.app.ActivityTaskManager;
Mady Mellor3dff9e62019-02-05 18:12:53 -080032import android.app.ActivityView;
Mady Mellor9801e852019-01-22 14:50:28 -080033import android.app.PendingIntent;
Mark Renouf5c732b32019-06-12 15:14:54 -040034import android.content.ComponentName;
Mady Mellordea7ecf2018-12-10 15:47:40 -080035import android.content.Context;
Mady Mellor9801e852019-01-22 14:50:28 -080036import android.content.Intent;
Mady Mellordea7ecf2018-12-10 15:47:40 -080037import android.content.res.Resources;
Mady Mellordd497052019-01-30 17:23:48 -080038import android.content.res.TypedArray;
Mady Mellordea7ecf2018-12-10 15:47:40 -080039import android.graphics.Color;
Mady Mellor3dff9e62019-02-05 18:12:53 -080040import android.graphics.Insets;
41import android.graphics.Point;
Mady Mellor47b11e32019-07-11 19:06:21 -070042import android.graphics.Rect;
Mady Mellordea7ecf2018-12-10 15:47:40 -080043import android.graphics.drawable.ShapeDrawable;
Mark Renouf5c732b32019-06-12 15:14:54 -040044import android.os.RemoteException;
Steven Wub00225b2019-02-08 14:27:42 -050045import android.service.notification.StatusBarNotification;
Mady Mellordea7ecf2018-12-10 15:47:40 -080046import android.util.AttributeSet;
Mady Mellor9801e852019-01-22 14:50:28 -080047import android.util.Log;
Mady Mellordea7ecf2018-12-10 15:47:40 -080048import android.view.View;
Mady Mellor3dff9e62019-02-05 18:12:53 -080049import android.view.WindowInsets;
Mady Mellora96c9ed2019-06-07 12:55:26 -070050import android.view.WindowManager;
Mady Mellordea7ecf2018-12-10 15:47:40 -080051import android.widget.LinearLayout;
52
Mark Renouf34d04f32019-05-13 15:53:18 -040053import com.android.internal.policy.ScreenDecorationsUtils;
Mady Mellor3dff9e62019-02-05 18:12:53 -080054import com.android.systemui.Dependency;
Mady Mellordea7ecf2018-12-10 15:47:40 -080055import com.android.systemui.R;
56import com.android.systemui.recents.TriangleShape;
Muhammad Qureshi9bced7d2020-01-16 15:22:12 -080057import com.android.systemui.shared.system.SysUiStatsLog;
Lyn Han754e77b2019-04-30 14:34:49 -070058import com.android.systemui.statusbar.AlphaOptimizedButton;
Beverlya53fb0d2020-01-29 15:26:13 -050059import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Mady Mellordea7ecf2018-12-10 15:47:40 -080060
61/**
Lyn Han02cca812019-04-02 16:27:32 -070062 * Container for the expanded bubble view, handles rendering the caret and settings icon.
Mady Mellordea7ecf2018-12-10 15:47:40 -080063 */
Mady Mellor3d82e682019-02-05 13:34:48 -080064public class BubbleExpandedView extends LinearLayout implements View.OnClickListener {
Issei Suzukia8d07312019-06-07 12:56:19 +020065 private static final String TAG = TAG_WITH_CLASS_NAME ? "BubbleExpandedView" : TAG_BUBBLES;
Mady Mellordea7ecf2018-12-10 15:47:40 -080066
Issei Suzuki734bc942019-06-05 13:59:52 +020067 private enum ActivityViewStatus {
68 // ActivityView is being initialized, cannot start an activity yet.
69 INITIALIZING,
70 // ActivityView is initialized, and ready to start an activity.
71 INITIALIZED,
72 // Activity runs in the ActivityView.
73 ACTIVITY_STARTED,
74 // ActivityView is released, so activity launching will no longer be permitted.
75 RELEASED,
76 }
Mady Mellordea7ecf2018-12-10 15:47:40 -080077
78 // The triangle pointing to the expanded view
79 private View mPointerView;
Mady Mellor44ee2fe2019-01-30 17:51:16 -080080 private int mPointerMargin;
Mady Mellore8e07712019-01-23 12:45:33 -080081
Lyn Han754e77b2019-04-30 14:34:49 -070082 private AlphaOptimizedButton mSettingsIcon;
Mady Mellore8e07712019-01-23 12:45:33 -080083
Mady Mellor3dff9e62019-02-05 18:12:53 -080084 // Views for expanded state
Mady Mellor3dff9e62019-02-05 18:12:53 -080085 private ActivityView mActivityView;
86
Issei Suzuki734bc942019-06-05 13:59:52 +020087 private ActivityViewStatus mActivityViewStatus = ActivityViewStatus.INITIALIZING;
Mark Renouf5c732b32019-06-12 15:14:54 -040088 private int mTaskId = -1;
89
Lyn Hanb58c7562020-01-07 14:29:20 -080090 private PendingIntent mPendingIntent;
Mady Mellor3dff9e62019-02-05 18:12:53 -080091
Mady Mellor5d8f1402019-02-21 18:23:52 -080092 private boolean mKeyboardVisible;
93 private boolean mNeedsNewHeight;
94
Mady Mellora96c9ed2019-06-07 12:55:26 -070095 private Point mDisplaySize;
Mady Mellorfe7ec032019-01-30 17:32:49 -080096 private int mMinHeight;
Lyn Hancd4f87e2020-02-19 20:33:45 -080097 private int mOverflowHeight;
Lyn Han02cca812019-04-02 16:27:32 -070098 private int mSettingsIconHeight;
Lyn Han02cca812019-04-02 16:27:32 -070099 private int mPointerWidth;
100 private int mPointerHeight;
Mark Renouf34d04f32019-05-13 15:53:18 -0400101 private ShapeDrawable mPointerDrawable;
Mady Mellor47b11e32019-07-11 19:06:21 -0700102 private Rect mTempRect = new Rect();
103 private int[] mTempLoc = new int[2];
104 private int mExpandedViewTouchSlop;
Mady Mellordea7ecf2018-12-10 15:47:40 -0800105
Lyn Hanb58c7562020-01-07 14:29:20 -0800106 @Nullable private Bubble mBubble;
107
108 private boolean mIsOverflow;
Mady Mellore8e07712019-01-23 12:45:33 -0800109
Mady Mellor3dff9e62019-02-05 18:12:53 -0800110 private BubbleController mBubbleController = Dependency.get(BubbleController.class);
Mady Mellor9be3bed2019-08-21 17:26:26 -0700111 private WindowManager mWindowManager;
Mady Mellor9801e852019-01-22 14:50:28 -0800112
Mady Mellor9801e852019-01-22 14:50:28 -0800113 private BubbleStackView mStackView;
114
Mady Mellor3dff9e62019-02-05 18:12:53 -0800115 private ActivityView.StateCallback mStateCallback = new ActivityView.StateCallback() {
116 @Override
117 public void onActivityViewReady(ActivityView view) {
Mark Renoufdb861c92019-07-26 13:58:17 -0400118 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
119 Log.d(TAG, "onActivityViewReady: mActivityViewStatus=" + mActivityViewStatus
120 + " bubble=" + getBubbleKey());
121 }
Issei Suzuki734bc942019-06-05 13:59:52 +0200122 switch (mActivityViewStatus) {
123 case INITIALIZING:
124 case INITIALIZED:
125 // Custom options so there is no activity transition animation
126 ActivityOptions options = ActivityOptions.makeCustomAnimation(getContext(),
127 0 /* enterResId */, 0 /* exitResId */);
Winson Chung06200522020-03-31 22:47:02 -0700128 options.setTaskAlwaysOnTop(true);
Issei Suzuki734bc942019-06-05 13:59:52 +0200129 // Post to keep the lifecycle normal
Mark Renoufdb861c92019-07-26 13:58:17 -0400130 post(() -> {
131 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
132 Log.d(TAG, "onActivityViewReady: calling startActivity, "
133 + "bubble=" + getBubbleKey());
134 }
Mady Mellor8454ddf2019-08-15 11:16:23 -0700135 try {
Lyn Hanb58c7562020-01-07 14:29:20 -0800136 if (!mIsOverflow && mBubble.usingShortcutInfo()) {
Mady Mellorb547e5e2019-12-02 10:15:56 -0800137 mActivityView.startShortcutActivity(mBubble.getShortcutInfo(),
138 options, null /* sourceBounds */);
139 } else {
140 Intent fillInIntent = new Intent();
141 // Apply flags to make behaviour match documentLaunchMode=always.
142 fillInIntent.addFlags(FLAG_ACTIVITY_NEW_DOCUMENT);
143 fillInIntent.addFlags(FLAG_ACTIVITY_MULTIPLE_TASK);
Lyn Hanb58c7562020-01-07 14:29:20 -0800144 mActivityView.startActivity(mPendingIntent, fillInIntent, options);
Mady Mellorb547e5e2019-12-02 10:15:56 -0800145 }
Mady Mellor8454ddf2019-08-15 11:16:23 -0700146 } catch (RuntimeException e) {
147 // If there's a runtime exception here then there's something
148 // wrong with the intent, we can't really recover / try to populate
149 // the bubble again so we'll just remove it.
150 Log.w(TAG, "Exception while displaying bubble: " + getBubbleKey()
151 + ", " + e.getMessage() + "; removing bubble");
Beverlya53fb0d2020-01-29 15:26:13 -0500152 mBubbleController.removeBubble(getBubbleEntry(),
Mady Mellor8454ddf2019-08-15 11:16:23 -0700153 BubbleController.DISMISS_INVALID_INTENT);
154 }
Mark Renoufdb861c92019-07-26 13:58:17 -0400155 });
Issei Suzuki734bc942019-06-05 13:59:52 +0200156 mActivityViewStatus = ActivityViewStatus.ACTIVITY_STARTED;
Mady Mellor6d002032019-02-13 13:45:17 -0800157 }
Mady Mellor3dff9e62019-02-05 18:12:53 -0800158 }
159
160 @Override
161 public void onActivityViewDestroyed(ActivityView view) {
Mark Renoufdb861c92019-07-26 13:58:17 -0400162 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
163 Log.d(TAG, "onActivityViewDestroyed: mActivityViewStatus=" + mActivityViewStatus
164 + " bubble=" + getBubbleKey());
165 }
Issei Suzuki734bc942019-06-05 13:59:52 +0200166 mActivityViewStatus = ActivityViewStatus.RELEASED;
Mady Mellor3dff9e62019-02-05 18:12:53 -0800167 }
168
Mark Renouf5c732b32019-06-12 15:14:54 -0400169 @Override
170 public void onTaskCreated(int taskId, ComponentName componentName) {
Mark Renoufdb861c92019-07-26 13:58:17 -0400171 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
172 Log.d(TAG, "onTaskCreated: taskId=" + taskId
173 + " bubble=" + getBubbleKey());
174 }
Mark Renouf5c732b32019-06-12 15:14:54 -0400175 // Since Bubble ActivityView applies singleTaskDisplay this is
176 // guaranteed to only be called once per ActivityView. The taskId is
177 // saved to use for removeTask, preventing appearance in recent tasks.
178 mTaskId = taskId;
Mady Mellor3dff9e62019-02-05 18:12:53 -0800179 }
180
181 /**
182 * This is only called for tasks on this ActivityView, which is also set to
183 * single-task mode -- meaning never more than one task on this display. If a task
184 * is being removed, it's the top Activity finishing and this bubble should
185 * be removed or collapsed.
186 */
187 @Override
188 public void onTaskRemovalStarted(int taskId) {
Mark Renoufdb861c92019-07-26 13:58:17 -0400189 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
190 Log.d(TAG, "onTaskRemovalStarted: taskId=" + taskId
191 + " mActivityViewStatus=" + mActivityViewStatus
192 + " bubble=" + getBubbleKey());
193 }
Mady Mellorff076eb2019-11-13 10:12:06 -0800194 if (mBubble != null && !mBubbleController.isUserCreatedBubble(mBubble.getKey())) {
195 // Must post because this is called from a binder thread.
Beverlya53fb0d2020-01-29 15:26:13 -0500196 post(() -> mBubbleController.removeBubble(mBubble.getEntry(),
Mady Mellorff076eb2019-11-13 10:12:06 -0800197 BubbleController.DISMISS_TASK_FINISHED));
Mady Mellor3dff9e62019-02-05 18:12:53 -0800198 }
199 }
200 };
Mady Mellore8e07712019-01-23 12:45:33 -0800201
Mady Mellor3d82e682019-02-05 13:34:48 -0800202 public BubbleExpandedView(Context context) {
Mady Mellordea7ecf2018-12-10 15:47:40 -0800203 this(context, null);
204 }
205
Mady Mellor3d82e682019-02-05 13:34:48 -0800206 public BubbleExpandedView(Context context, AttributeSet attrs) {
Mady Mellordea7ecf2018-12-10 15:47:40 -0800207 this(context, attrs, 0);
208 }
209
Mady Mellor3d82e682019-02-05 13:34:48 -0800210 public BubbleExpandedView(Context context, AttributeSet attrs, int defStyleAttr) {
Mady Mellordea7ecf2018-12-10 15:47:40 -0800211 this(context, attrs, defStyleAttr, 0);
212 }
213
Mady Mellor3d82e682019-02-05 13:34:48 -0800214 public BubbleExpandedView(Context context, AttributeSet attrs, int defStyleAttr,
Mady Mellordea7ecf2018-12-10 15:47:40 -0800215 int defStyleRes) {
216 super(context, attrs, defStyleAttr, defStyleRes);
Mady Mellora96c9ed2019-06-07 12:55:26 -0700217 mDisplaySize = new Point();
Mady Mellor9be3bed2019-08-21 17:26:26 -0700218 mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Mady Mellor8fe411d2019-08-16 11:27:53 -0700219 // Get the real size -- this includes screen decorations (notches, statusbar, navbar).
Mady Mellor9be3bed2019-08-21 17:26:26 -0700220 mWindowManager.getDefaultDisplay().getRealSize(mDisplaySize);
Mady Mellor47b11e32019-07-11 19:06:21 -0700221 Resources res = getResources();
222 mMinHeight = res.getDimensionPixelSize(R.dimen.bubble_expanded_default_height);
Lyn Hancd4f87e2020-02-19 20:33:45 -0800223 mOverflowHeight = res.getDimensionPixelSize(R.dimen.bubble_overflow_height);
Mady Mellor47b11e32019-07-11 19:06:21 -0700224 mPointerMargin = res.getDimensionPixelSize(R.dimen.bubble_pointer_margin);
225 mExpandedViewTouchSlop = res.getDimensionPixelSize(R.dimen.bubble_expanded_view_slop);
Mady Mellordea7ecf2018-12-10 15:47:40 -0800226 }
227
228 @Override
229 protected void onFinishInflate() {
230 super.onFinishInflate();
Mark Renoufdb861c92019-07-26 13:58:17 -0400231 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
232 Log.d(TAG, "onFinishInflate: bubble=" + getBubbleKey());
233 }
Mady Mellordea7ecf2018-12-10 15:47:40 -0800234
235 Resources res = getResources();
236 mPointerView = findViewById(R.id.pointer_view);
Lyn Han02cca812019-04-02 16:27:32 -0700237 mPointerWidth = res.getDimensionPixelSize(R.dimen.bubble_pointer_width);
238 mPointerHeight = res.getDimensionPixelSize(R.dimen.bubble_pointer_height);
Mady Mellordd497052019-01-30 17:23:48 -0800239
Mady Mellordd497052019-01-30 17:23:48 -0800240
Mark Renouf34d04f32019-05-13 15:53:18 -0400241 mPointerDrawable = new ShapeDrawable(TriangleShape.create(
Lyn Han5aa27e22019-05-15 10:55:07 -0700242 mPointerWidth, mPointerHeight, true /* pointUp */));
Mark Renouf34d04f32019-05-13 15:53:18 -0400243 mPointerView.setBackground(mPointerDrawable);
Mady Mellora96c9ed2019-06-07 12:55:26 -0700244 mPointerView.setVisibility(INVISIBLE);
Mady Mellor9801e852019-01-22 14:50:28 -0800245
Lyn Han02cca812019-04-02 16:27:32 -0700246 mSettingsIconHeight = getContext().getResources().getDimensionPixelSize(
Mady Mellor5a3e94b2020-02-07 12:16:21 -0800247 R.dimen.bubble_manage_button_height);
Lyn Han02cca812019-04-02 16:27:32 -0700248 mSettingsIcon = findViewById(R.id.settings_button);
Lyn Han02cca812019-04-02 16:27:32 -0700249 mSettingsIcon.setOnClickListener(this);
Lyn Han02cca812019-04-02 16:27:32 -0700250
Mady Mellor3dff9e62019-02-05 18:12:53 -0800251 mActivityView = new ActivityView(mContext, null /* attrs */, 0 /* defStyle */,
252 true /* singleTaskInstance */);
Lyn Hanb58c7562020-01-07 14:29:20 -0800253
lumarkdc9b3192019-07-23 21:18:17 +0800254 // Set ActivityView's alpha value as zero, since there is no view content to be shown.
Issei Suzukicac2a502019-04-16 16:52:50 +0200255 setContentVisibility(false);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800256 addView(mActivityView);
257
Lyn Han5aa27e22019-05-15 10:55:07 -0700258 // Expanded stack layout, top to bottom:
259 // Expanded view container
260 // ==> bubble row
261 // ==> expanded view
262 // ==> activity view
263 // ==> manage button
264 bringChildToFront(mActivityView);
265 bringChildToFront(mSettingsIcon);
Mady Mellor52b1ac62019-04-10 16:59:03 -0700266
Mark Renouf34d04f32019-05-13 15:53:18 -0400267 applyThemeAttrs();
268
Mady Mellor5d8f1402019-02-21 18:23:52 -0800269 setOnApplyWindowInsetsListener((View view, WindowInsets insets) -> {
270 // Keep track of IME displaying because we should not make any adjustments that might
271 // cause a config change while the IME is displayed otherwise it'll loose focus.
Mady Mellor3dff9e62019-02-05 18:12:53 -0800272 final int keyboardHeight = insets.getSystemWindowInsetBottom()
273 - insets.getStableInsetBottom();
Mady Mellor5d8f1402019-02-21 18:23:52 -0800274 mKeyboardVisible = keyboardHeight != 0;
275 if (!mKeyboardVisible && mNeedsNewHeight) {
276 updateHeight();
277 }
Mady Mellor3dff9e62019-02-05 18:12:53 -0800278 return view.onApplyWindowInsets(insets);
279 });
Mady Mellore8e07712019-01-23 12:45:33 -0800280 }
281
Mark Renoufdb861c92019-07-26 13:58:17 -0400282 private String getBubbleKey() {
283 return mBubble != null ? mBubble.getKey() : "null";
284 }
285
Beverlya53fb0d2020-01-29 15:26:13 -0500286 private NotificationEntry getBubbleEntry() {
287 return mBubble != null ? mBubble.getEntry() : null;
288 }
289
Mark Renouf34d04f32019-05-13 15:53:18 -0400290 void applyThemeAttrs() {
Lyn Han61bfdb32019-12-10 14:34:12 -0800291 final TypedArray ta = mContext.obtainStyledAttributes(
292 new int[] {
293 android.R.attr.colorBackgroundFloating,
294 android.R.attr.dialogCornerRadius});
295 int bgColor = ta.getColor(0, Color.WHITE);
296 float cornerRadius = ta.getDimensionPixelSize(1, 0);
Mark Renouf34d04f32019-05-13 15:53:18 -0400297 ta.recycle();
298
Mark Renouf34d04f32019-05-13 15:53:18 -0400299 mPointerDrawable.setTint(bgColor);
Lyn Hanedb55e22020-02-12 18:55:39 -0800300 if (mActivityView != null && ScreenDecorationsUtils.supportsRoundedCornersOnWindows(
301 mContext.getResources())) {
Mark Renouf34d04f32019-05-13 15:53:18 -0400302 mActivityView.setCornerRadius(cornerRadius);
303 }
304 }
305
Mady Mellor5d8f1402019-02-21 18:23:52 -0800306 @Override
307 protected void onDetachedFromWindow() {
308 super.onDetachedFromWindow();
309 mKeyboardVisible = false;
310 mNeedsNewHeight = false;
311 if (mActivityView != null) {
Mady Melloraf02fa42020-03-04 14:22:16 -0800312 // TODO: Temporary hack to offset the view until we can properly inset Bubbles again.
313 if (sNewInsetsMode == NEW_INSETS_MODE_FULL) {
314 mStackView.animate()
315 .setDuration(100)
316 .translationY(0);
317 } else {
318 mActivityView.setForwardedInsets(Insets.of(0, 0, 0, 0));
319 }
Mady Mellor5d8f1402019-02-21 18:23:52 -0800320 }
Mark Renoufdb861c92019-07-26 13:58:17 -0400321 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
322 Log.d(TAG, "onDetachedFromWindow: bubble=" + getBubbleKey());
323 }
Mady Mellor5d8f1402019-02-21 18:23:52 -0800324 }
325
326 /**
Issei Suzukicac2a502019-04-16 16:52:50 +0200327 * Set visibility of contents in the expanded state.
328 *
329 * @param visibility {@code true} if the contents should be visible on the screen.
330 *
331 * Note that this contents visibility doesn't affect visibility at {@link android.view.View},
332 * and setting {@code false} actually means rendering the contents in transparent.
333 */
334 void setContentVisibility(boolean visibility) {
Mark Renoufdb861c92019-07-26 13:58:17 -0400335 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
336 Log.d(TAG, "setContentVisibility: visibility=" + visibility
337 + " bubble=" + getBubbleKey());
338 }
Issei Suzukicac2a502019-04-16 16:52:50 +0200339 final float alpha = visibility ? 1f : 0f;
340 mPointerView.setAlpha(alpha);
341 if (mActivityView != null) {
342 mActivityView.setAlpha(alpha);
343 }
344 }
345
346 /**
Mady Mellor5d8f1402019-02-21 18:23:52 -0800347 * Called by {@link BubbleStackView} when the insets for the expanded state should be updated.
348 * This should be done post-move and post-animation.
349 */
350 void updateInsets(WindowInsets insets) {
351 if (usingActivityView()) {
Mady Mellor8fe411d2019-08-16 11:27:53 -0700352 int[] screenLoc = mActivityView.getLocationOnScreen();
353 final int activityViewBottom = screenLoc[1] + mActivityView.getHeight();
Mady Mellor9be3bed2019-08-21 17:26:26 -0700354 final int keyboardTop = mDisplaySize.y - Math.max(insets.getSystemWindowInsetBottom(),
355 insets.getDisplayCutout() != null
356 ? insets.getDisplayCutout().getSafeInsetBottom()
357 : 0);
Mady Mellor8fe411d2019-08-16 11:27:53 -0700358 final int insetsBottom = Math.max(activityViewBottom - keyboardTop, 0);
Jorim Jaggi924ef752020-01-29 17:26:55 +0100359
360 // TODO: Temporary hack to offset the view until we can properly inset Bubbles again.
361 if (sNewInsetsMode == NEW_INSETS_MODE_FULL) {
Mady Melloraf02fa42020-03-04 14:22:16 -0800362 mStackView.animate()
363 .setDuration(100)
364 .translationY(-insetsBottom)
365 .withEndAction(() -> mActivityView.onLocationChanged());
Jorim Jaggi924ef752020-01-29 17:26:55 +0100366 } else {
367 mActivityView.setForwardedInsets(Insets.of(0, 0, 0, insetsBottom));
368 }
Mady Mellor5d8f1402019-02-21 18:23:52 -0800369 }
370 }
371
Mady Mellor247ca2c2019-12-02 16:18:59 -0800372 void setStackView(BubbleStackView stackView) {
Mady Mellor9801e852019-01-22 14:50:28 -0800373 mStackView = stackView;
Mady Mellor6d002032019-02-13 13:45:17 -0800374 }
375
Lyn Hanb58c7562020-01-07 14:29:20 -0800376 public void setOverflow(boolean overflow) {
377 mIsOverflow = overflow;
378
379 Intent target = new Intent(mContext, BubbleOverflowActivity.class);
380 mPendingIntent = PendingIntent.getActivity(mContext, /* requestCode */ 0,
381 target, PendingIntent.FLAG_UPDATE_CURRENT);
382 mSettingsIcon.setVisibility(GONE);
383 }
384
Mady Mellor6d002032019-02-13 13:45:17 -0800385 /**
Mady Mellor247ca2c2019-12-02 16:18:59 -0800386 * Sets the bubble used to populate this view.
Mady Mellor6d002032019-02-13 13:45:17 -0800387 */
Mady Mellor247ca2c2019-12-02 16:18:59 -0800388 void update(Bubble bubble) {
389 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
390 Log.d(TAG, "update: bubble=" + (bubble != null ? bubble.getKey() : "null"));
391 }
392 boolean isNew = mBubble == null;
Lyn Hanb58c7562020-01-07 14:29:20 -0800393 if (isNew || bubble != null && bubble.getKey().equals(mBubble.getKey())) {
Mady Mellor247ca2c2019-12-02 16:18:59 -0800394 mBubble = bubble;
395 mSettingsIcon.setContentDescription(getResources().getString(
396 R.string.bubbles_settings_button_description, bubble.getAppName()));
397
398 if (isNew) {
Lyn Hanb58c7562020-01-07 14:29:20 -0800399 mPendingIntent = mBubble.getBubbleIntent();
400 if (mPendingIntent != null || mBubble.getShortcutInfo() != null) {
Mady Mellor247ca2c2019-12-02 16:18:59 -0800401 setContentVisibility(false);
402 mActivityView.setVisibility(VISIBLE);
403 }
404 }
405 applyThemeAttrs();
406 } else {
407 Log.w(TAG, "Trying to update entry with different key, new bubble: "
408 + bubble.getKey() + " old bubble: " + bubble.getKey());
409 }
410 }
411
412 /**
413 * Lets activity view know it should be shown / populated with activity content.
414 */
415 void populateExpandedView() {
Mark Renoufdb861c92019-07-26 13:58:17 -0400416 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
417 Log.d(TAG, "populateExpandedView: "
418 + "bubble=" + getBubbleKey());
419 }
420
Mady Mellor5029fa62019-03-05 12:16:21 -0800421 if (usingActivityView()) {
422 mActivityView.setCallback(mStateCallback);
423 } else {
Issei Suzukia91f3962019-06-07 11:48:23 +0200424 Log.e(TAG, "Cannot populate expanded view.");
Mady Mellor5029fa62019-03-05 12:16:21 -0800425 }
Mady Mellor9801e852019-01-22 14:50:28 -0800426 }
427
Mark Renouf041d7262019-02-06 12:09:41 -0500428 boolean performBackPressIfNeeded() {
Mady Mellor323fb0b2019-03-25 12:15:22 -0700429 if (!usingActivityView()) {
Mark Renouf041d7262019-02-06 12:09:41 -0500430 return false;
431 }
432 mActivityView.performBackPress();
433 return true;
434 }
435
Mady Mellorfe7ec032019-01-30 17:32:49 -0800436 void updateHeight() {
Mark Renoufdb861c92019-07-26 13:58:17 -0400437 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
438 Log.d(TAG, "updateHeight: bubble=" + getBubbleKey());
439 }
Mady Mellorfe7ec032019-01-30 17:32:49 -0800440 if (usingActivityView()) {
Lyn Hancd4f87e2020-02-19 20:33:45 -0800441 float desiredHeight = mOverflowHeight;
Lyn Hanb58c7562020-01-07 14:29:20 -0800442 if (!mIsOverflow) {
443 desiredHeight = Math.max(mBubble.getDesiredHeight(mContext), mMinHeight);
444 }
Mady Mellor8fe411d2019-08-16 11:27:53 -0700445 float height = Math.min(desiredHeight, getMaxExpandedHeight());
Lyn Hancd4f87e2020-02-19 20:33:45 -0800446 height = Math.max(height, mIsOverflow? mOverflowHeight : mMinHeight);
Mady Mellorfe7ec032019-01-30 17:32:49 -0800447 LayoutParams lp = (LayoutParams) mActivityView.getLayoutParams();
Lyn Hancd4f87e2020-02-19 20:33:45 -0800448 mNeedsNewHeight = lp.height != height;
Mady Mellor5d8f1402019-02-21 18:23:52 -0800449 if (!mKeyboardVisible) {
450 // If the keyboard is visible... don't adjust the height because that will cause
451 // a configuration change and the keyboard will be lost.
Mady Mellor7af771a2019-03-07 15:04:54 -0800452 lp.height = (int) height;
Mady Mellor5d8f1402019-02-21 18:23:52 -0800453 mActivityView.setLayoutParams(lp);
454 mNeedsNewHeight = false;
455 }
Mark Renoufdb861c92019-07-26 13:58:17 -0400456 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
457 Log.d(TAG, "updateHeight: bubble=" + getBubbleKey() + " height=" + height
458 + " mNeedsNewHeight=" + mNeedsNewHeight);
459 }
Mady Mellorfe7ec032019-01-30 17:32:49 -0800460 }
461 }
462
Mady Mellora96c9ed2019-06-07 12:55:26 -0700463 private int getMaxExpandedHeight() {
Mady Mellor9be3bed2019-08-21 17:26:26 -0700464 mWindowManager.getDefaultDisplay().getRealSize(mDisplaySize);
Mady Mellora96c9ed2019-06-07 12:55:26 -0700465 int[] windowLocation = mActivityView.getLocationOnScreen();
Mady Mellor8fe411d2019-08-16 11:27:53 -0700466 int bottomInset = getRootWindowInsets() != null
467 ? getRootWindowInsets().getStableInsetBottom()
468 : 0;
Lyn Han0b955ed2020-01-31 09:47:35 -0800469 return mDisplaySize.y - windowLocation[1] - mSettingsIconHeight - mPointerHeight
Mady Mellor8fe411d2019-08-16 11:27:53 -0700470 - mPointerMargin - bottomInset;
Mady Mellora96c9ed2019-06-07 12:55:26 -0700471 }
472
Mady Mellor47b11e32019-07-11 19:06:21 -0700473 /**
474 * Whether the provided x, y values (in raw coordinates) are in a touchable area of the
475 * expanded view.
476 *
477 * The touchable areas are the ActivityView (plus some slop around it) and the manage button.
478 */
479 boolean intersectingTouchableContent(int rawX, int rawY) {
480 mTempRect.setEmpty();
481 if (mActivityView != null) {
482 mTempLoc = mActivityView.getLocationOnScreen();
483 mTempRect.set(mTempLoc[0] - mExpandedViewTouchSlop,
484 mTempLoc[1] - mExpandedViewTouchSlop,
485 mTempLoc[0] + mActivityView.getWidth() + mExpandedViewTouchSlop,
486 mTempLoc[1] + mActivityView.getHeight() + mExpandedViewTouchSlop);
487 }
488 if (mTempRect.contains(rawX, rawY)) {
489 return true;
490 }
491 mTempLoc = mSettingsIcon.getLocationOnScreen();
492 mTempRect.set(mTempLoc[0],
493 mTempLoc[1],
494 mTempLoc[0] + mSettingsIcon.getWidth(),
495 mTempLoc[1] + mSettingsIcon.getHeight());
496 if (mTempRect.contains(rawX, rawY)) {
497 return true;
498 }
499 return false;
500 }
501
Mady Mellor9801e852019-01-22 14:50:28 -0800502 @Override
503 public void onClick(View view) {
Mady Mellor99a302602019-06-14 11:39:56 -0700504 if (mBubble == null) {
Mady Mellor9801e852019-01-22 14:50:28 -0800505 return;
506 }
Mady Mellor9801e852019-01-22 14:50:28 -0800507 int id = view.getId();
Lyn Hanc26ff122019-03-29 16:46:07 -0700508 if (id == R.id.settings_button) {
Mady Mellor99a302602019-06-14 11:39:56 -0700509 Intent intent = mBubble.getSettingsIntent();
Steven Wub00225b2019-02-08 14:27:42 -0500510 mStackView.collapseStack(() -> {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400511 mContext.startActivityAsUser(intent, mBubble.getEntry().getSbn().getUser());
Mady Mellor99a302602019-06-14 11:39:56 -0700512 logBubbleClickEvent(mBubble,
Muhammad Qureshi9bced7d2020-01-16 15:22:12 -0800513 SysUiStatsLog.BUBBLE_UICHANGED__ACTION__HEADER_GO_TO_SETTINGS);
Steven Wub00225b2019-02-08 14:27:42 -0500514 });
Mady Mellor9801e852019-01-22 14:50:28 -0800515 }
Mady Mellordea7ecf2018-12-10 15:47:40 -0800516 }
517
518 /**
Mady Mellor3dff9e62019-02-05 18:12:53 -0800519 * Update appearance of the expanded view being displayed.
520 */
521 public void updateView() {
Mark Renoufdb861c92019-07-26 13:58:17 -0400522 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
523 Log.d(TAG, "updateView: bubble="
524 + getBubbleKey());
525 }
Mady Mellor3dff9e62019-02-05 18:12:53 -0800526 if (usingActivityView()
527 && mActivityView.getVisibility() == VISIBLE
528 && mActivityView.isAttachedToWindow()) {
529 mActivityView.onLocationChanged();
Mady Mellor3dff9e62019-02-05 18:12:53 -0800530 }
Mady Mellorfe7ec032019-01-30 17:32:49 -0800531 updateHeight();
Mady Mellor3dff9e62019-02-05 18:12:53 -0800532 }
533
534 /**
Mady Mellordea7ecf2018-12-10 15:47:40 -0800535 * Set the x position that the tip of the triangle should point to.
536 */
Mady Mellor3dff9e62019-02-05 18:12:53 -0800537 public void setPointerPosition(float x) {
Lyn Han9a2f5cf2019-05-23 11:01:41 -0700538 float halfPointerWidth = mPointerWidth / 2f;
539 float pointerLeft = x - halfPointerWidth;
540 mPointerView.setTranslationX(pointerLeft);
Lyn Hanf74ba672019-05-20 16:08:48 -0700541 mPointerView.setVisibility(VISIBLE);
Mady Mellordea7ecf2018-12-10 15:47:40 -0800542 }
543
544 /**
Mady Mellor5a3e94b2020-02-07 12:16:21 -0800545 * Position of the manage button displayed in the expanded view. Used for placing user
546 * education about the manage button.
547 */
548 public Rect getManageButtonLocationOnScreen() {
549 mTempLoc = mSettingsIcon.getLocationOnScreen();
550 return new Rect(mTempLoc[0], mTempLoc[1], mTempLoc[0] + mSettingsIcon.getWidth(),
551 mTempLoc[1] + mSettingsIcon.getHeight());
552 }
553
554 /**
Mady Mellor3dff9e62019-02-05 18:12:53 -0800555 * Removes and releases an ActivityView if one was previously created for this bubble.
Mady Mellordea7ecf2018-12-10 15:47:40 -0800556 */
Mady Mellor94d94a72019-03-05 18:16:59 -0800557 public void cleanUpExpandedState() {
Mark Renoufdb861c92019-07-26 13:58:17 -0400558 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
559 Log.d(TAG, "cleanUpExpandedState: mActivityViewStatus=" + mActivityViewStatus
560 + ", bubble=" + getBubbleKey());
561 }
Mady Mellor3dff9e62019-02-05 18:12:53 -0800562 if (mActivityView == null) {
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500563 return;
564 }
Issei Suzuki734bc942019-06-05 13:59:52 +0200565 switch (mActivityViewStatus) {
566 case INITIALIZED:
567 case ACTIVITY_STARTED:
568 mActivityView.release();
Mady Mellordea7ecf2018-12-10 15:47:40 -0800569 }
Mark Renouf5c732b32019-06-12 15:14:54 -0400570 if (mTaskId != -1) {
571 try {
572 ActivityTaskManager.getService().removeTask(mTaskId);
573 } catch (RemoteException e) {
574 Log.w(TAG, "Failed to remove taskId " + mTaskId);
575 }
576 mTaskId = -1;
Mady Mellordea7ecf2018-12-10 15:47:40 -0800577 }
Mark Renouf28c250d2019-02-25 16:47:34 -0500578 removeView(mActivityView);
Mark Renouf5c732b32019-06-12 15:14:54 -0400579
Mady Mellor3dff9e62019-02-05 18:12:53 -0800580 mActivityView = null;
Issei Suzuki734bc942019-06-05 13:59:52 +0200581 }
582
583 /**
584 * Called when the last task is removed from a {@link android.hardware.display.VirtualDisplay}
585 * which {@link ActivityView} uses.
586 */
587 void notifyDisplayEmpty() {
Mark Renoufdb861c92019-07-26 13:58:17 -0400588 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
589 Log.d(TAG, "notifyDisplayEmpty: bubble="
590 + getBubbleKey()
591 + " mActivityViewStatus=" + mActivityViewStatus);
592 }
Issei Suzuki734bc942019-06-05 13:59:52 +0200593 if (mActivityViewStatus == ActivityViewStatus.ACTIVITY_STARTED) {
594 mActivityViewStatus = ActivityViewStatus.INITIALIZED;
595 }
Mady Mellordea7ecf2018-12-10 15:47:40 -0800596 }
597
Mady Mellor3dff9e62019-02-05 18:12:53 -0800598 private boolean usingActivityView() {
Lyn Hanb58c7562020-01-07 14:29:20 -0800599 return (mPendingIntent != null || mBubble.getShortcutInfo() != null)
Mady Mellor2ac2d3a2020-01-08 17:18:54 -0800600 && mActivityView != null;
Mady Mellor3dff9e62019-02-05 18:12:53 -0800601 }
602
Mady Mellor390bff42019-04-05 15:09:01 -0700603 /**
604 * @return the display id of the virtual display.
605 */
606 public int getVirtualDisplayId() {
607 if (usingActivityView()) {
608 return mActivityView.getVirtualDisplayId();
609 }
610 return INVALID_DISPLAY;
611 }
612
Steven Wub00225b2019-02-08 14:27:42 -0500613 /**
614 * Logs bubble UI click event.
615 *
Mady Mellor99a302602019-06-14 11:39:56 -0700616 * @param bubble the bubble notification entry that user is interacting with.
Steven Wub00225b2019-02-08 14:27:42 -0500617 * @param action the user interaction enum.
618 */
Mady Mellor99a302602019-06-14 11:39:56 -0700619 private void logBubbleClickEvent(Bubble bubble, int action) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400620 StatusBarNotification notification = bubble.getEntry().getSbn();
Muhammad Qureshi9bced7d2020-01-16 15:22:12 -0800621 SysUiStatsLog.write(SysUiStatsLog.BUBBLE_UI_CHANGED,
Steven Wub00225b2019-02-08 14:27:42 -0500622 notification.getPackageName(),
623 notification.getNotification().getChannelId(),
624 notification.getId(),
625 mStackView.getBubbleIndex(mStackView.getExpandedBubble()),
626 mStackView.getBubbleCount(),
627 action,
628 mStackView.getNormalizedXPosition(),
Steven Wu45e38ae2019-03-25 16:16:59 -0400629 mStackView.getNormalizedYPosition(),
Mady Mellorb8aaf972019-11-26 10:28:00 -0800630 bubble.showInShade(),
Mady Mellor99a302602019-06-14 11:39:56 -0700631 bubble.isOngoing(),
Mady Mellor6cec3502019-06-17 19:20:49 -0700632 false /* isAppForeground (unused) */);
Mady Mellor7af771a2019-03-07 15:04:54 -0800633 }
Mady Mellordea7ecf2018-12-10 15:47:40 -0800634}