blob: 50a50633f43c1e9a257b5d0e9149087eddbab072 [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;
Mady Mellor3dff9e62019-02-05 18:12:53 -080022
Jorim Jaggi924ef752020-01-29 17:26:55 +010023import static android.view.ViewRootImpl.NEW_INSETS_MODE_FULL;
24import static android.view.ViewRootImpl.sNewInsetsMode;
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;
Mady Mellordea7ecf2018-12-10 15:47:40 -080059
60/**
Lyn Han02cca812019-04-02 16:27:32 -070061 * Container for the expanded bubble view, handles rendering the caret and settings icon.
Mady Mellordea7ecf2018-12-10 15:47:40 -080062 */
Mady Mellor3d82e682019-02-05 13:34:48 -080063public class BubbleExpandedView extends LinearLayout implements View.OnClickListener {
Issei Suzukia8d07312019-06-07 12:56:19 +020064 private static final String TAG = TAG_WITH_CLASS_NAME ? "BubbleExpandedView" : TAG_BUBBLES;
Mady Mellordea7ecf2018-12-10 15:47:40 -080065
Issei Suzuki734bc942019-06-05 13:59:52 +020066 private enum ActivityViewStatus {
67 // ActivityView is being initialized, cannot start an activity yet.
68 INITIALIZING,
69 // ActivityView is initialized, and ready to start an activity.
70 INITIALIZED,
71 // Activity runs in the ActivityView.
72 ACTIVITY_STARTED,
73 // ActivityView is released, so activity launching will no longer be permitted.
74 RELEASED,
75 }
Mady Mellordea7ecf2018-12-10 15:47:40 -080076
77 // The triangle pointing to the expanded view
78 private View mPointerView;
Mady Mellor44ee2fe2019-01-30 17:51:16 -080079 private int mPointerMargin;
Mady Mellore8e07712019-01-23 12:45:33 -080080
Lyn Han754e77b2019-04-30 14:34:49 -070081 private AlphaOptimizedButton mSettingsIcon;
Mady Mellore8e07712019-01-23 12:45:33 -080082
Mady Mellor3dff9e62019-02-05 18:12:53 -080083 // Views for expanded state
Mady Mellor3dff9e62019-02-05 18:12:53 -080084 private ActivityView mActivityView;
85
Issei Suzuki734bc942019-06-05 13:59:52 +020086 private ActivityViewStatus mActivityViewStatus = ActivityViewStatus.INITIALIZING;
Mark Renouf5c732b32019-06-12 15:14:54 -040087 private int mTaskId = -1;
88
Lyn Hanb58c7562020-01-07 14:29:20 -080089 private PendingIntent mPendingIntent;
Mady Mellor3dff9e62019-02-05 18:12:53 -080090
Mady Mellor5d8f1402019-02-21 18:23:52 -080091 private boolean mKeyboardVisible;
92 private boolean mNeedsNewHeight;
93
Mady Mellora96c9ed2019-06-07 12:55:26 -070094 private Point mDisplaySize;
Mady Mellorfe7ec032019-01-30 17:32:49 -080095 private int mMinHeight;
Lyn Han02cca812019-04-02 16:27:32 -070096 private int mSettingsIconHeight;
Lyn Han02cca812019-04-02 16:27:32 -070097 private int mPointerWidth;
98 private int mPointerHeight;
Mark Renouf34d04f32019-05-13 15:53:18 -040099 private ShapeDrawable mPointerDrawable;
Mady Mellor47b11e32019-07-11 19:06:21 -0700100 private Rect mTempRect = new Rect();
101 private int[] mTempLoc = new int[2];
102 private int mExpandedViewTouchSlop;
Mady Mellordea7ecf2018-12-10 15:47:40 -0800103
Lyn Hanb58c7562020-01-07 14:29:20 -0800104 @Nullable private Bubble mBubble;
105
106 private boolean mIsOverflow;
Mady Mellore8e07712019-01-23 12:45:33 -0800107
Mady Mellor3dff9e62019-02-05 18:12:53 -0800108 private BubbleController mBubbleController = Dependency.get(BubbleController.class);
Mady Mellor9be3bed2019-08-21 17:26:26 -0700109 private WindowManager mWindowManager;
Mady Mellor9801e852019-01-22 14:50:28 -0800110
Mady Mellor9801e852019-01-22 14:50:28 -0800111 private BubbleStackView mStackView;
112
Mady Mellor3dff9e62019-02-05 18:12:53 -0800113 private ActivityView.StateCallback mStateCallback = new ActivityView.StateCallback() {
114 @Override
115 public void onActivityViewReady(ActivityView view) {
Mark Renoufdb861c92019-07-26 13:58:17 -0400116 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
117 Log.d(TAG, "onActivityViewReady: mActivityViewStatus=" + mActivityViewStatus
118 + " bubble=" + getBubbleKey());
119 }
Issei Suzuki734bc942019-06-05 13:59:52 +0200120 switch (mActivityViewStatus) {
121 case INITIALIZING:
122 case INITIALIZED:
123 // Custom options so there is no activity transition animation
124 ActivityOptions options = ActivityOptions.makeCustomAnimation(getContext(),
125 0 /* enterResId */, 0 /* exitResId */);
126 // Post to keep the lifecycle normal
Mark Renoufdb861c92019-07-26 13:58:17 -0400127 post(() -> {
128 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
129 Log.d(TAG, "onActivityViewReady: calling startActivity, "
130 + "bubble=" + getBubbleKey());
131 }
Mady Mellor8454ddf2019-08-15 11:16:23 -0700132 try {
Lyn Hanb58c7562020-01-07 14:29:20 -0800133 if (!mIsOverflow && mBubble.usingShortcutInfo()) {
Mady Mellorb547e5e2019-12-02 10:15:56 -0800134 mActivityView.startShortcutActivity(mBubble.getShortcutInfo(),
135 options, null /* sourceBounds */);
136 } else {
137 Intent fillInIntent = new Intent();
138 // Apply flags to make behaviour match documentLaunchMode=always.
139 fillInIntent.addFlags(FLAG_ACTIVITY_NEW_DOCUMENT);
140 fillInIntent.addFlags(FLAG_ACTIVITY_MULTIPLE_TASK);
Lyn Hanb58c7562020-01-07 14:29:20 -0800141 mActivityView.startActivity(mPendingIntent, fillInIntent, options);
Mady Mellorb547e5e2019-12-02 10:15:56 -0800142 }
Mady Mellor8454ddf2019-08-15 11:16:23 -0700143 } catch (RuntimeException e) {
144 // If there's a runtime exception here then there's something
145 // wrong with the intent, we can't really recover / try to populate
146 // the bubble again so we'll just remove it.
147 Log.w(TAG, "Exception while displaying bubble: " + getBubbleKey()
148 + ", " + e.getMessage() + "; removing bubble");
Lyn Hanb58c7562020-01-07 14:29:20 -0800149 mBubbleController.removeBubble(getBubbleKey(),
Mady Mellor8454ddf2019-08-15 11:16:23 -0700150 BubbleController.DISMISS_INVALID_INTENT);
151 }
Mark Renoufdb861c92019-07-26 13:58:17 -0400152 });
Issei Suzuki734bc942019-06-05 13:59:52 +0200153 mActivityViewStatus = ActivityViewStatus.ACTIVITY_STARTED;
Mady Mellor6d002032019-02-13 13:45:17 -0800154 }
Mady Mellor3dff9e62019-02-05 18:12:53 -0800155 }
156
157 @Override
158 public void onActivityViewDestroyed(ActivityView view) {
Mark Renoufdb861c92019-07-26 13:58:17 -0400159 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
160 Log.d(TAG, "onActivityViewDestroyed: mActivityViewStatus=" + mActivityViewStatus
161 + " bubble=" + getBubbleKey());
162 }
Issei Suzuki734bc942019-06-05 13:59:52 +0200163 mActivityViewStatus = ActivityViewStatus.RELEASED;
Mady Mellor3dff9e62019-02-05 18:12:53 -0800164 }
165
Mark Renouf5c732b32019-06-12 15:14:54 -0400166 @Override
167 public void onTaskCreated(int taskId, ComponentName componentName) {
Mark Renoufdb861c92019-07-26 13:58:17 -0400168 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
169 Log.d(TAG, "onTaskCreated: taskId=" + taskId
170 + " bubble=" + getBubbleKey());
171 }
Mark Renouf5c732b32019-06-12 15:14:54 -0400172 // Since Bubble ActivityView applies singleTaskDisplay this is
173 // guaranteed to only be called once per ActivityView. The taskId is
174 // saved to use for removeTask, preventing appearance in recent tasks.
175 mTaskId = taskId;
Mady Mellor3dff9e62019-02-05 18:12:53 -0800176 }
177
178 /**
179 * This is only called for tasks on this ActivityView, which is also set to
180 * single-task mode -- meaning never more than one task on this display. If a task
181 * is being removed, it's the top Activity finishing and this bubble should
182 * be removed or collapsed.
183 */
184 @Override
185 public void onTaskRemovalStarted(int taskId) {
Mark Renoufdb861c92019-07-26 13:58:17 -0400186 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
187 Log.d(TAG, "onTaskRemovalStarted: taskId=" + taskId
188 + " mActivityViewStatus=" + mActivityViewStatus
189 + " bubble=" + getBubbleKey());
190 }
Mady Mellorff076eb2019-11-13 10:12:06 -0800191 if (mBubble != null && !mBubbleController.isUserCreatedBubble(mBubble.getKey())) {
192 // Must post because this is called from a binder thread.
193 post(() -> mBubbleController.removeBubble(mBubble.getKey(),
194 BubbleController.DISMISS_TASK_FINISHED));
Mady Mellor3dff9e62019-02-05 18:12:53 -0800195 }
196 }
197 };
Mady Mellore8e07712019-01-23 12:45:33 -0800198
Mady Mellor3d82e682019-02-05 13:34:48 -0800199 public BubbleExpandedView(Context context) {
Mady Mellordea7ecf2018-12-10 15:47:40 -0800200 this(context, null);
201 }
202
Mady Mellor3d82e682019-02-05 13:34:48 -0800203 public BubbleExpandedView(Context context, AttributeSet attrs) {
Mady Mellordea7ecf2018-12-10 15:47:40 -0800204 this(context, attrs, 0);
205 }
206
Mady Mellor3d82e682019-02-05 13:34:48 -0800207 public BubbleExpandedView(Context context, AttributeSet attrs, int defStyleAttr) {
Mady Mellordea7ecf2018-12-10 15:47:40 -0800208 this(context, attrs, defStyleAttr, 0);
209 }
210
Mady Mellor3d82e682019-02-05 13:34:48 -0800211 public BubbleExpandedView(Context context, AttributeSet attrs, int defStyleAttr,
Mady Mellordea7ecf2018-12-10 15:47:40 -0800212 int defStyleRes) {
213 super(context, attrs, defStyleAttr, defStyleRes);
Mady Mellora96c9ed2019-06-07 12:55:26 -0700214 mDisplaySize = new Point();
Mady Mellor9be3bed2019-08-21 17:26:26 -0700215 mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Mady Mellor8fe411d2019-08-16 11:27:53 -0700216 // Get the real size -- this includes screen decorations (notches, statusbar, navbar).
Mady Mellor9be3bed2019-08-21 17:26:26 -0700217 mWindowManager.getDefaultDisplay().getRealSize(mDisplaySize);
Mady Mellor47b11e32019-07-11 19:06:21 -0700218 Resources res = getResources();
219 mMinHeight = res.getDimensionPixelSize(R.dimen.bubble_expanded_default_height);
220 mPointerMargin = res.getDimensionPixelSize(R.dimen.bubble_pointer_margin);
221 mExpandedViewTouchSlop = res.getDimensionPixelSize(R.dimen.bubble_expanded_view_slop);
Mady Mellordea7ecf2018-12-10 15:47:40 -0800222 }
223
224 @Override
225 protected void onFinishInflate() {
226 super.onFinishInflate();
Mark Renoufdb861c92019-07-26 13:58:17 -0400227 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
228 Log.d(TAG, "onFinishInflate: bubble=" + getBubbleKey());
229 }
Mady Mellordea7ecf2018-12-10 15:47:40 -0800230
231 Resources res = getResources();
232 mPointerView = findViewById(R.id.pointer_view);
Lyn Han02cca812019-04-02 16:27:32 -0700233 mPointerWidth = res.getDimensionPixelSize(R.dimen.bubble_pointer_width);
234 mPointerHeight = res.getDimensionPixelSize(R.dimen.bubble_pointer_height);
Mady Mellordd497052019-01-30 17:23:48 -0800235
Mady Mellordd497052019-01-30 17:23:48 -0800236
Mark Renouf34d04f32019-05-13 15:53:18 -0400237 mPointerDrawable = new ShapeDrawable(TriangleShape.create(
Lyn Han5aa27e22019-05-15 10:55:07 -0700238 mPointerWidth, mPointerHeight, true /* pointUp */));
Mark Renouf34d04f32019-05-13 15:53:18 -0400239 mPointerView.setBackground(mPointerDrawable);
Mady Mellora96c9ed2019-06-07 12:55:26 -0700240 mPointerView.setVisibility(INVISIBLE);
Mady Mellor9801e852019-01-22 14:50:28 -0800241
Lyn Han02cca812019-04-02 16:27:32 -0700242 mSettingsIconHeight = getContext().getResources().getDimensionPixelSize(
Mady Mellor818eef02019-08-16 16:12:29 -0700243 R.dimen.bubble_settings_size);
Lyn Han02cca812019-04-02 16:27:32 -0700244 mSettingsIcon = findViewById(R.id.settings_button);
Lyn Han02cca812019-04-02 16:27:32 -0700245 mSettingsIcon.setOnClickListener(this);
Lyn Han02cca812019-04-02 16:27:32 -0700246
Mady Mellor3dff9e62019-02-05 18:12:53 -0800247 mActivityView = new ActivityView(mContext, null /* attrs */, 0 /* defStyle */,
248 true /* singleTaskInstance */);
Lyn Hanb58c7562020-01-07 14:29:20 -0800249
lumarkdc9b3192019-07-23 21:18:17 +0800250 // Set ActivityView's alpha value as zero, since there is no view content to be shown.
Issei Suzukicac2a502019-04-16 16:52:50 +0200251 setContentVisibility(false);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800252 addView(mActivityView);
253
Lyn Han5aa27e22019-05-15 10:55:07 -0700254 // Expanded stack layout, top to bottom:
255 // Expanded view container
256 // ==> bubble row
257 // ==> expanded view
258 // ==> activity view
259 // ==> manage button
260 bringChildToFront(mActivityView);
261 bringChildToFront(mSettingsIcon);
Mady Mellor52b1ac62019-04-10 16:59:03 -0700262
Mark Renouf34d04f32019-05-13 15:53:18 -0400263 applyThemeAttrs();
264
Mady Mellor5d8f1402019-02-21 18:23:52 -0800265 setOnApplyWindowInsetsListener((View view, WindowInsets insets) -> {
266 // Keep track of IME displaying because we should not make any adjustments that might
267 // cause a config change while the IME is displayed otherwise it'll loose focus.
Mady Mellor3dff9e62019-02-05 18:12:53 -0800268 final int keyboardHeight = insets.getSystemWindowInsetBottom()
269 - insets.getStableInsetBottom();
Mady Mellor5d8f1402019-02-21 18:23:52 -0800270 mKeyboardVisible = keyboardHeight != 0;
271 if (!mKeyboardVisible && mNeedsNewHeight) {
272 updateHeight();
273 }
Mady Mellor3dff9e62019-02-05 18:12:53 -0800274 return view.onApplyWindowInsets(insets);
275 });
Mady Mellore8e07712019-01-23 12:45:33 -0800276 }
277
Mark Renoufdb861c92019-07-26 13:58:17 -0400278 private String getBubbleKey() {
279 return mBubble != null ? mBubble.getKey() : "null";
280 }
281
Mark Renouf34d04f32019-05-13 15:53:18 -0400282 void applyThemeAttrs() {
Lyn Han61bfdb32019-12-10 14:34:12 -0800283 final TypedArray ta = mContext.obtainStyledAttributes(
284 new int[] {
285 android.R.attr.colorBackgroundFloating,
286 android.R.attr.dialogCornerRadius});
287 int bgColor = ta.getColor(0, Color.WHITE);
288 float cornerRadius = ta.getDimensionPixelSize(1, 0);
Mark Renouf34d04f32019-05-13 15:53:18 -0400289 ta.recycle();
290
Mark Renouf34d04f32019-05-13 15:53:18 -0400291 mPointerDrawable.setTint(bgColor);
Mark Renouf34d04f32019-05-13 15:53:18 -0400292 if (ScreenDecorationsUtils.supportsRoundedCornersOnWindows(mContext.getResources())) {
293 mActivityView.setCornerRadius(cornerRadius);
294 }
295 }
296
Mady Mellor5d8f1402019-02-21 18:23:52 -0800297 @Override
298 protected void onDetachedFromWindow() {
299 super.onDetachedFromWindow();
300 mKeyboardVisible = false;
301 mNeedsNewHeight = false;
302 if (mActivityView != null) {
303 mActivityView.setForwardedInsets(Insets.of(0, 0, 0, 0));
304 }
Mark Renoufdb861c92019-07-26 13:58:17 -0400305 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
306 Log.d(TAG, "onDetachedFromWindow: bubble=" + getBubbleKey());
307 }
Mady Mellor5d8f1402019-02-21 18:23:52 -0800308 }
309
310 /**
Issei Suzukicac2a502019-04-16 16:52:50 +0200311 * Set visibility of contents in the expanded state.
312 *
313 * @param visibility {@code true} if the contents should be visible on the screen.
314 *
315 * Note that this contents visibility doesn't affect visibility at {@link android.view.View},
316 * and setting {@code false} actually means rendering the contents in transparent.
317 */
318 void setContentVisibility(boolean visibility) {
Mark Renoufdb861c92019-07-26 13:58:17 -0400319 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
320 Log.d(TAG, "setContentVisibility: visibility=" + visibility
321 + " bubble=" + getBubbleKey());
322 }
Issei Suzukicac2a502019-04-16 16:52:50 +0200323 final float alpha = visibility ? 1f : 0f;
324 mPointerView.setAlpha(alpha);
325 if (mActivityView != null) {
326 mActivityView.setAlpha(alpha);
327 }
328 }
329
330 /**
Mady Mellor5d8f1402019-02-21 18:23:52 -0800331 * Called by {@link BubbleStackView} when the insets for the expanded state should be updated.
332 * This should be done post-move and post-animation.
333 */
334 void updateInsets(WindowInsets insets) {
335 if (usingActivityView()) {
Mady Mellor8fe411d2019-08-16 11:27:53 -0700336 int[] screenLoc = mActivityView.getLocationOnScreen();
337 final int activityViewBottom = screenLoc[1] + mActivityView.getHeight();
Mady Mellor9be3bed2019-08-21 17:26:26 -0700338 final int keyboardTop = mDisplaySize.y - Math.max(insets.getSystemWindowInsetBottom(),
339 insets.getDisplayCutout() != null
340 ? insets.getDisplayCutout().getSafeInsetBottom()
341 : 0);
Mady Mellor8fe411d2019-08-16 11:27:53 -0700342 final int insetsBottom = Math.max(activityViewBottom - keyboardTop, 0);
Jorim Jaggi924ef752020-01-29 17:26:55 +0100343
344 // TODO: Temporary hack to offset the view until we can properly inset Bubbles again.
345 if (sNewInsetsMode == NEW_INSETS_MODE_FULL) {
346 mStackView.animate().translationY(-insetsBottom);
347 } else {
348 mActivityView.setForwardedInsets(Insets.of(0, 0, 0, insetsBottom));
349 }
Mady Mellor5d8f1402019-02-21 18:23:52 -0800350 }
351 }
352
Mady Mellor247ca2c2019-12-02 16:18:59 -0800353 void setStackView(BubbleStackView stackView) {
Mady Mellor9801e852019-01-22 14:50:28 -0800354 mStackView = stackView;
Mady Mellor6d002032019-02-13 13:45:17 -0800355 }
356
Lyn Hanb58c7562020-01-07 14:29:20 -0800357 public void setOverflow(boolean overflow) {
358 mIsOverflow = overflow;
359
360 Intent target = new Intent(mContext, BubbleOverflowActivity.class);
361 mPendingIntent = PendingIntent.getActivity(mContext, /* requestCode */ 0,
362 target, PendingIntent.FLAG_UPDATE_CURRENT);
363 mSettingsIcon.setVisibility(GONE);
364 }
365
Mady Mellor6d002032019-02-13 13:45:17 -0800366 /**
Mady Mellor247ca2c2019-12-02 16:18:59 -0800367 * Sets the bubble used to populate this view.
Mady Mellor6d002032019-02-13 13:45:17 -0800368 */
Mady Mellor247ca2c2019-12-02 16:18:59 -0800369 void update(Bubble bubble) {
370 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
371 Log.d(TAG, "update: bubble=" + (bubble != null ? bubble.getKey() : "null"));
372 }
373 boolean isNew = mBubble == null;
Lyn Hanb58c7562020-01-07 14:29:20 -0800374 if (isNew || bubble != null && bubble.getKey().equals(mBubble.getKey())) {
Mady Mellor247ca2c2019-12-02 16:18:59 -0800375 mBubble = bubble;
376 mSettingsIcon.setContentDescription(getResources().getString(
377 R.string.bubbles_settings_button_description, bubble.getAppName()));
378
379 if (isNew) {
Lyn Hanb58c7562020-01-07 14:29:20 -0800380 mPendingIntent = mBubble.getBubbleIntent();
381 if (mPendingIntent != null || mBubble.getShortcutInfo() != null) {
Mady Mellor247ca2c2019-12-02 16:18:59 -0800382 setContentVisibility(false);
383 mActivityView.setVisibility(VISIBLE);
384 }
385 }
386 applyThemeAttrs();
387 } else {
388 Log.w(TAG, "Trying to update entry with different key, new bubble: "
389 + bubble.getKey() + " old bubble: " + bubble.getKey());
390 }
391 }
392
393 /**
394 * Lets activity view know it should be shown / populated with activity content.
395 */
396 void populateExpandedView() {
Mark Renoufdb861c92019-07-26 13:58:17 -0400397 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
398 Log.d(TAG, "populateExpandedView: "
399 + "bubble=" + getBubbleKey());
400 }
401
Mady Mellor5029fa62019-03-05 12:16:21 -0800402 if (usingActivityView()) {
403 mActivityView.setCallback(mStateCallback);
404 } else {
Issei Suzukia91f3962019-06-07 11:48:23 +0200405 Log.e(TAG, "Cannot populate expanded view.");
Mady Mellor5029fa62019-03-05 12:16:21 -0800406 }
Mady Mellor9801e852019-01-22 14:50:28 -0800407 }
408
Mark Renouf041d7262019-02-06 12:09:41 -0500409 boolean performBackPressIfNeeded() {
Mady Mellor323fb0b2019-03-25 12:15:22 -0700410 if (!usingActivityView()) {
Mark Renouf041d7262019-02-06 12:09:41 -0500411 return false;
412 }
413 mActivityView.performBackPress();
414 return true;
415 }
416
Lyn Hanb58c7562020-01-07 14:29:20 -0800417 // TODO(138116789) Fix overflow height.
Mady Mellorfe7ec032019-01-30 17:32:49 -0800418 void updateHeight() {
Mark Renoufdb861c92019-07-26 13:58:17 -0400419 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
420 Log.d(TAG, "updateHeight: bubble=" + getBubbleKey());
421 }
Mady Mellorfe7ec032019-01-30 17:32:49 -0800422 if (usingActivityView()) {
Lyn Hanb58c7562020-01-07 14:29:20 -0800423 float desiredHeight = mMinHeight;
424 if (!mIsOverflow) {
425 desiredHeight = Math.max(mBubble.getDesiredHeight(mContext), mMinHeight);
426 }
Mady Mellor8fe411d2019-08-16 11:27:53 -0700427 float height = Math.min(desiredHeight, getMaxExpandedHeight());
Mady Mellorfe7ec032019-01-30 17:32:49 -0800428 height = Math.max(height, mMinHeight);
429 LayoutParams lp = (LayoutParams) mActivityView.getLayoutParams();
Mady Mellor5d8f1402019-02-21 18:23:52 -0800430 mNeedsNewHeight = lp.height != height;
431 if (!mKeyboardVisible) {
432 // If the keyboard is visible... don't adjust the height because that will cause
433 // a configuration change and the keyboard will be lost.
Mady Mellor7af771a2019-03-07 15:04:54 -0800434 lp.height = (int) height;
Mady Mellor5d8f1402019-02-21 18:23:52 -0800435 mActivityView.setLayoutParams(lp);
436 mNeedsNewHeight = false;
437 }
Mark Renoufdb861c92019-07-26 13:58:17 -0400438 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
439 Log.d(TAG, "updateHeight: bubble=" + getBubbleKey() + " height=" + height
440 + " mNeedsNewHeight=" + mNeedsNewHeight);
441 }
Mady Mellorfe7ec032019-01-30 17:32:49 -0800442 }
443 }
444
Mady Mellora96c9ed2019-06-07 12:55:26 -0700445 private int getMaxExpandedHeight() {
Mady Mellor9be3bed2019-08-21 17:26:26 -0700446 mWindowManager.getDefaultDisplay().getRealSize(mDisplaySize);
Mady Mellora96c9ed2019-06-07 12:55:26 -0700447 int[] windowLocation = mActivityView.getLocationOnScreen();
Mady Mellor8fe411d2019-08-16 11:27:53 -0700448 int bottomInset = getRootWindowInsets() != null
449 ? getRootWindowInsets().getStableInsetBottom()
450 : 0;
Lyn Han0b955ed2020-01-31 09:47:35 -0800451 return mDisplaySize.y - windowLocation[1] - mSettingsIconHeight - mPointerHeight
Mady Mellor8fe411d2019-08-16 11:27:53 -0700452 - mPointerMargin - bottomInset;
Mady Mellora96c9ed2019-06-07 12:55:26 -0700453 }
454
Mady Mellor47b11e32019-07-11 19:06:21 -0700455 /**
456 * Whether the provided x, y values (in raw coordinates) are in a touchable area of the
457 * expanded view.
458 *
459 * The touchable areas are the ActivityView (plus some slop around it) and the manage button.
460 */
461 boolean intersectingTouchableContent(int rawX, int rawY) {
462 mTempRect.setEmpty();
463 if (mActivityView != null) {
464 mTempLoc = mActivityView.getLocationOnScreen();
465 mTempRect.set(mTempLoc[0] - mExpandedViewTouchSlop,
466 mTempLoc[1] - mExpandedViewTouchSlop,
467 mTempLoc[0] + mActivityView.getWidth() + mExpandedViewTouchSlop,
468 mTempLoc[1] + mActivityView.getHeight() + mExpandedViewTouchSlop);
469 }
470 if (mTempRect.contains(rawX, rawY)) {
471 return true;
472 }
473 mTempLoc = mSettingsIcon.getLocationOnScreen();
474 mTempRect.set(mTempLoc[0],
475 mTempLoc[1],
476 mTempLoc[0] + mSettingsIcon.getWidth(),
477 mTempLoc[1] + mSettingsIcon.getHeight());
478 if (mTempRect.contains(rawX, rawY)) {
479 return true;
480 }
481 return false;
482 }
483
Mady Mellor9801e852019-01-22 14:50:28 -0800484 @Override
485 public void onClick(View view) {
Mady Mellor99a302602019-06-14 11:39:56 -0700486 if (mBubble == null) {
Mady Mellor9801e852019-01-22 14:50:28 -0800487 return;
488 }
Mady Mellor9801e852019-01-22 14:50:28 -0800489 int id = view.getId();
Lyn Hanc26ff122019-03-29 16:46:07 -0700490 if (id == R.id.settings_button) {
Mady Mellor99a302602019-06-14 11:39:56 -0700491 Intent intent = mBubble.getSettingsIntent();
Steven Wub00225b2019-02-08 14:27:42 -0500492 mStackView.collapseStack(() -> {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400493 mContext.startActivityAsUser(intent, mBubble.getEntry().getSbn().getUser());
Mady Mellor99a302602019-06-14 11:39:56 -0700494 logBubbleClickEvent(mBubble,
Muhammad Qureshi9bced7d2020-01-16 15:22:12 -0800495 SysUiStatsLog.BUBBLE_UICHANGED__ACTION__HEADER_GO_TO_SETTINGS);
Steven Wub00225b2019-02-08 14:27:42 -0500496 });
Mady Mellor9801e852019-01-22 14:50:28 -0800497 }
Mady Mellordea7ecf2018-12-10 15:47:40 -0800498 }
499
500 /**
Mady Mellor3dff9e62019-02-05 18:12:53 -0800501 * Update appearance of the expanded view being displayed.
502 */
503 public void updateView() {
Mark Renoufdb861c92019-07-26 13:58:17 -0400504 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
505 Log.d(TAG, "updateView: bubble="
506 + getBubbleKey());
507 }
Mady Mellor3dff9e62019-02-05 18:12:53 -0800508 if (usingActivityView()
509 && mActivityView.getVisibility() == VISIBLE
510 && mActivityView.isAttachedToWindow()) {
511 mActivityView.onLocationChanged();
Mady Mellor3dff9e62019-02-05 18:12:53 -0800512 }
Mady Mellorfe7ec032019-01-30 17:32:49 -0800513 updateHeight();
Mady Mellor3dff9e62019-02-05 18:12:53 -0800514 }
515
516 /**
Mady Mellordea7ecf2018-12-10 15:47:40 -0800517 * Set the x position that the tip of the triangle should point to.
518 */
Mady Mellor3dff9e62019-02-05 18:12:53 -0800519 public void setPointerPosition(float x) {
Lyn Han9a2f5cf2019-05-23 11:01:41 -0700520 float halfPointerWidth = mPointerWidth / 2f;
521 float pointerLeft = x - halfPointerWidth;
522 mPointerView.setTranslationX(pointerLeft);
Lyn Hanf74ba672019-05-20 16:08:48 -0700523 mPointerView.setVisibility(VISIBLE);
Mady Mellordea7ecf2018-12-10 15:47:40 -0800524 }
525
526 /**
Mady Mellor3dff9e62019-02-05 18:12:53 -0800527 * Removes and releases an ActivityView if one was previously created for this bubble.
Mady Mellordea7ecf2018-12-10 15:47:40 -0800528 */
Mady Mellor94d94a72019-03-05 18:16:59 -0800529 public void cleanUpExpandedState() {
Mark Renoufdb861c92019-07-26 13:58:17 -0400530 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
531 Log.d(TAG, "cleanUpExpandedState: mActivityViewStatus=" + mActivityViewStatus
532 + ", bubble=" + getBubbleKey());
533 }
Mady Mellor3dff9e62019-02-05 18:12:53 -0800534 if (mActivityView == null) {
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500535 return;
536 }
Issei Suzuki734bc942019-06-05 13:59:52 +0200537 switch (mActivityViewStatus) {
538 case INITIALIZED:
539 case ACTIVITY_STARTED:
540 mActivityView.release();
Mady Mellordea7ecf2018-12-10 15:47:40 -0800541 }
Mark Renouf5c732b32019-06-12 15:14:54 -0400542 if (mTaskId != -1) {
543 try {
544 ActivityTaskManager.getService().removeTask(mTaskId);
545 } catch (RemoteException e) {
546 Log.w(TAG, "Failed to remove taskId " + mTaskId);
547 }
548 mTaskId = -1;
Mady Mellordea7ecf2018-12-10 15:47:40 -0800549 }
Mark Renouf28c250d2019-02-25 16:47:34 -0500550 removeView(mActivityView);
Mark Renouf5c732b32019-06-12 15:14:54 -0400551
Mady Mellor3dff9e62019-02-05 18:12:53 -0800552 mActivityView = null;
Issei Suzuki734bc942019-06-05 13:59:52 +0200553 }
554
555 /**
556 * Called when the last task is removed from a {@link android.hardware.display.VirtualDisplay}
557 * which {@link ActivityView} uses.
558 */
559 void notifyDisplayEmpty() {
Mark Renoufdb861c92019-07-26 13:58:17 -0400560 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
561 Log.d(TAG, "notifyDisplayEmpty: bubble="
562 + getBubbleKey()
563 + " mActivityViewStatus=" + mActivityViewStatus);
564 }
Issei Suzuki734bc942019-06-05 13:59:52 +0200565 if (mActivityViewStatus == ActivityViewStatus.ACTIVITY_STARTED) {
566 mActivityViewStatus = ActivityViewStatus.INITIALIZED;
567 }
Mady Mellordea7ecf2018-12-10 15:47:40 -0800568 }
569
Mady Mellor3dff9e62019-02-05 18:12:53 -0800570 private boolean usingActivityView() {
Lyn Hanb58c7562020-01-07 14:29:20 -0800571 return (mPendingIntent != null || mBubble.getShortcutInfo() != null)
Mady Mellor2ac2d3a2020-01-08 17:18:54 -0800572 && mActivityView != null;
Mady Mellor3dff9e62019-02-05 18:12:53 -0800573 }
574
Mady Mellor390bff42019-04-05 15:09:01 -0700575 /**
576 * @return the display id of the virtual display.
577 */
578 public int getVirtualDisplayId() {
579 if (usingActivityView()) {
580 return mActivityView.getVirtualDisplayId();
581 }
582 return INVALID_DISPLAY;
583 }
584
Steven Wub00225b2019-02-08 14:27:42 -0500585 /**
586 * Logs bubble UI click event.
587 *
Mady Mellor99a302602019-06-14 11:39:56 -0700588 * @param bubble the bubble notification entry that user is interacting with.
Steven Wub00225b2019-02-08 14:27:42 -0500589 * @param action the user interaction enum.
590 */
Mady Mellor99a302602019-06-14 11:39:56 -0700591 private void logBubbleClickEvent(Bubble bubble, int action) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400592 StatusBarNotification notification = bubble.getEntry().getSbn();
Muhammad Qureshi9bced7d2020-01-16 15:22:12 -0800593 SysUiStatsLog.write(SysUiStatsLog.BUBBLE_UI_CHANGED,
Steven Wub00225b2019-02-08 14:27:42 -0500594 notification.getPackageName(),
595 notification.getNotification().getChannelId(),
596 notification.getId(),
597 mStackView.getBubbleIndex(mStackView.getExpandedBubble()),
598 mStackView.getBubbleCount(),
599 action,
600 mStackView.getNormalizedXPosition(),
Steven Wu45e38ae2019-03-25 16:16:59 -0400601 mStackView.getNormalizedYPosition(),
Mady Mellorb8aaf972019-11-26 10:28:00 -0800602 bubble.showInShade(),
Mady Mellor99a302602019-06-14 11:39:56 -0700603 bubble.isOngoing(),
Mady Mellor6cec3502019-06-17 19:20:49 -0700604 false /* isAppForeground (unused) */);
Mady Mellor7af771a2019-03-07 15:04:54 -0800605 }
Mady Mellordea7ecf2018-12-10 15:47:40 -0800606}