blob: cf8b2be1becbffd005dee1550c0d6fd8e912ecda [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
Mark Renoufdb861c92019-07-26 13:58:17 -040023import static com.android.systemui.bubbles.BubbleDebugConfig.DEBUG_BUBBLE_EXPANDED_VIEW;
Issei Suzukia8d07312019-06-07 12:56:19 +020024import static com.android.systemui.bubbles.BubbleDebugConfig.TAG_BUBBLES;
25import static com.android.systemui.bubbles.BubbleDebugConfig.TAG_WITH_CLASS_NAME;
Mady Mellorca0c24c2019-05-16 16:14:32 -070026
Lyn Hanb58c7562020-01-07 14:29:20 -080027import android.annotation.Nullable;
Mady Mellor60101c92019-04-11 19:04:00 -070028import android.app.ActivityOptions;
Mark Renouf5c732b32019-06-12 15:14:54 -040029import android.app.ActivityTaskManager;
Mady Mellor3dff9e62019-02-05 18:12:53 -080030import android.app.ActivityView;
Mady Mellor9801e852019-01-22 14:50:28 -080031import android.app.PendingIntent;
Mark Renouf5c732b32019-06-12 15:14:54 -040032import android.content.ComponentName;
Mady Mellordea7ecf2018-12-10 15:47:40 -080033import android.content.Context;
Mady Mellor9801e852019-01-22 14:50:28 -080034import android.content.Intent;
Mady Mellordea7ecf2018-12-10 15:47:40 -080035import android.content.res.Resources;
Mady Mellordd497052019-01-30 17:23:48 -080036import android.content.res.TypedArray;
Mady Mellordea7ecf2018-12-10 15:47:40 -080037import android.graphics.Color;
Mady Mellor3dff9e62019-02-05 18:12:53 -080038import android.graphics.Insets;
39import android.graphics.Point;
Mady Mellor47b11e32019-07-11 19:06:21 -070040import android.graphics.Rect;
Mady Mellordea7ecf2018-12-10 15:47:40 -080041import android.graphics.drawable.ShapeDrawable;
Mark Renouf5c732b32019-06-12 15:14:54 -040042import android.os.RemoteException;
Steven Wub00225b2019-02-08 14:27:42 -050043import android.service.notification.StatusBarNotification;
Mady Mellordea7ecf2018-12-10 15:47:40 -080044import android.util.AttributeSet;
Mady Mellor9801e852019-01-22 14:50:28 -080045import android.util.Log;
Mady Mellordea7ecf2018-12-10 15:47:40 -080046import android.view.View;
Mady Mellor3dff9e62019-02-05 18:12:53 -080047import android.view.WindowInsets;
Mady Mellora96c9ed2019-06-07 12:55:26 -070048import android.view.WindowManager;
Mady Mellordea7ecf2018-12-10 15:47:40 -080049import android.widget.LinearLayout;
50
Mark Renouf34d04f32019-05-13 15:53:18 -040051import com.android.internal.policy.ScreenDecorationsUtils;
Mady Mellor3dff9e62019-02-05 18:12:53 -080052import com.android.systemui.Dependency;
Mady Mellordea7ecf2018-12-10 15:47:40 -080053import com.android.systemui.R;
54import com.android.systemui.recents.TriangleShape;
Muhammad Qureshi9bced7d2020-01-16 15:22:12 -080055import com.android.systemui.shared.system.SysUiStatsLog;
Lyn Han754e77b2019-04-30 14:34:49 -070056import com.android.systemui.statusbar.AlphaOptimizedButton;
Mady Mellordea7ecf2018-12-10 15:47:40 -080057
58/**
Lyn Han02cca812019-04-02 16:27:32 -070059 * Container for the expanded bubble view, handles rendering the caret and settings icon.
Mady Mellordea7ecf2018-12-10 15:47:40 -080060 */
Mady Mellor3d82e682019-02-05 13:34:48 -080061public class BubbleExpandedView extends LinearLayout implements View.OnClickListener {
Issei Suzukia8d07312019-06-07 12:56:19 +020062 private static final String TAG = TAG_WITH_CLASS_NAME ? "BubbleExpandedView" : TAG_BUBBLES;
Mady Mellordea7ecf2018-12-10 15:47:40 -080063
Issei Suzuki734bc942019-06-05 13:59:52 +020064 private enum ActivityViewStatus {
65 // ActivityView is being initialized, cannot start an activity yet.
66 INITIALIZING,
67 // ActivityView is initialized, and ready to start an activity.
68 INITIALIZED,
69 // Activity runs in the ActivityView.
70 ACTIVITY_STARTED,
71 // ActivityView is released, so activity launching will no longer be permitted.
72 RELEASED,
73 }
Mady Mellordea7ecf2018-12-10 15:47:40 -080074
75 // The triangle pointing to the expanded view
76 private View mPointerView;
Mady Mellor44ee2fe2019-01-30 17:51:16 -080077 private int mPointerMargin;
Mady Mellore8e07712019-01-23 12:45:33 -080078
Lyn Han754e77b2019-04-30 14:34:49 -070079 private AlphaOptimizedButton mSettingsIcon;
Mady Mellore8e07712019-01-23 12:45:33 -080080
Mady Mellor3dff9e62019-02-05 18:12:53 -080081 // Views for expanded state
Mady Mellor3dff9e62019-02-05 18:12:53 -080082 private ActivityView mActivityView;
83
Issei Suzuki734bc942019-06-05 13:59:52 +020084 private ActivityViewStatus mActivityViewStatus = ActivityViewStatus.INITIALIZING;
Mark Renouf5c732b32019-06-12 15:14:54 -040085 private int mTaskId = -1;
86
Lyn Hanb58c7562020-01-07 14:29:20 -080087 private PendingIntent mPendingIntent;
Mady Mellor3dff9e62019-02-05 18:12:53 -080088
Mady Mellor5d8f1402019-02-21 18:23:52 -080089 private boolean mKeyboardVisible;
90 private boolean mNeedsNewHeight;
91
Mady Mellora96c9ed2019-06-07 12:55:26 -070092 private Point mDisplaySize;
Mady Mellorfe7ec032019-01-30 17:32:49 -080093 private int mMinHeight;
Lyn Han02cca812019-04-02 16:27:32 -070094 private int mSettingsIconHeight;
Lyn Han02cca812019-04-02 16:27:32 -070095 private int mPointerWidth;
96 private int mPointerHeight;
Mark Renouf34d04f32019-05-13 15:53:18 -040097 private ShapeDrawable mPointerDrawable;
Mady Mellor47b11e32019-07-11 19:06:21 -070098 private Rect mTempRect = new Rect();
99 private int[] mTempLoc = new int[2];
100 private int mExpandedViewTouchSlop;
Mady Mellordea7ecf2018-12-10 15:47:40 -0800101
Lyn Hanb58c7562020-01-07 14:29:20 -0800102 @Nullable private Bubble mBubble;
103
104 private boolean mIsOverflow;
Mady Mellore8e07712019-01-23 12:45:33 -0800105
Mady Mellor3dff9e62019-02-05 18:12:53 -0800106 private BubbleController mBubbleController = Dependency.get(BubbleController.class);
Mady Mellor9be3bed2019-08-21 17:26:26 -0700107 private WindowManager mWindowManager;
Mady Mellor9801e852019-01-22 14:50:28 -0800108
Mady Mellor9801e852019-01-22 14:50:28 -0800109 private BubbleStackView mStackView;
110
Mady Mellor3dff9e62019-02-05 18:12:53 -0800111 private ActivityView.StateCallback mStateCallback = new ActivityView.StateCallback() {
112 @Override
113 public void onActivityViewReady(ActivityView view) {
Mark Renoufdb861c92019-07-26 13:58:17 -0400114 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
115 Log.d(TAG, "onActivityViewReady: mActivityViewStatus=" + mActivityViewStatus
116 + " bubble=" + getBubbleKey());
117 }
Issei Suzuki734bc942019-06-05 13:59:52 +0200118 switch (mActivityViewStatus) {
119 case INITIALIZING:
120 case INITIALIZED:
121 // Custom options so there is no activity transition animation
122 ActivityOptions options = ActivityOptions.makeCustomAnimation(getContext(),
123 0 /* enterResId */, 0 /* exitResId */);
124 // Post to keep the lifecycle normal
Mark Renoufdb861c92019-07-26 13:58:17 -0400125 post(() -> {
126 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
127 Log.d(TAG, "onActivityViewReady: calling startActivity, "
128 + "bubble=" + getBubbleKey());
129 }
Mady Mellor8454ddf2019-08-15 11:16:23 -0700130 try {
Lyn Hanb58c7562020-01-07 14:29:20 -0800131 if (!mIsOverflow && mBubble.usingShortcutInfo()) {
Mady Mellorb547e5e2019-12-02 10:15:56 -0800132 mActivityView.startShortcutActivity(mBubble.getShortcutInfo(),
133 options, null /* sourceBounds */);
134 } else {
135 Intent fillInIntent = new Intent();
136 // Apply flags to make behaviour match documentLaunchMode=always.
137 fillInIntent.addFlags(FLAG_ACTIVITY_NEW_DOCUMENT);
138 fillInIntent.addFlags(FLAG_ACTIVITY_MULTIPLE_TASK);
Lyn Hanb58c7562020-01-07 14:29:20 -0800139 mActivityView.startActivity(mPendingIntent, fillInIntent, options);
Mady Mellorb547e5e2019-12-02 10:15:56 -0800140 }
Mady Mellor8454ddf2019-08-15 11:16:23 -0700141 } catch (RuntimeException e) {
142 // If there's a runtime exception here then there's something
143 // wrong with the intent, we can't really recover / try to populate
144 // the bubble again so we'll just remove it.
145 Log.w(TAG, "Exception while displaying bubble: " + getBubbleKey()
146 + ", " + e.getMessage() + "; removing bubble");
Lyn Hanb58c7562020-01-07 14:29:20 -0800147 mBubbleController.removeBubble(getBubbleKey(),
Mady Mellor8454ddf2019-08-15 11:16:23 -0700148 BubbleController.DISMISS_INVALID_INTENT);
149 }
Mark Renoufdb861c92019-07-26 13:58:17 -0400150 });
Issei Suzuki734bc942019-06-05 13:59:52 +0200151 mActivityViewStatus = ActivityViewStatus.ACTIVITY_STARTED;
Mady Mellor6d002032019-02-13 13:45:17 -0800152 }
Mady Mellor3dff9e62019-02-05 18:12:53 -0800153 }
154
155 @Override
156 public void onActivityViewDestroyed(ActivityView view) {
Mark Renoufdb861c92019-07-26 13:58:17 -0400157 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
158 Log.d(TAG, "onActivityViewDestroyed: mActivityViewStatus=" + mActivityViewStatus
159 + " bubble=" + getBubbleKey());
160 }
Issei Suzuki734bc942019-06-05 13:59:52 +0200161 mActivityViewStatus = ActivityViewStatus.RELEASED;
Mady Mellor3dff9e62019-02-05 18:12:53 -0800162 }
163
Mark Renouf5c732b32019-06-12 15:14:54 -0400164 @Override
165 public void onTaskCreated(int taskId, ComponentName componentName) {
Mark Renoufdb861c92019-07-26 13:58:17 -0400166 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
167 Log.d(TAG, "onTaskCreated: taskId=" + taskId
168 + " bubble=" + getBubbleKey());
169 }
Mark Renouf5c732b32019-06-12 15:14:54 -0400170 // Since Bubble ActivityView applies singleTaskDisplay this is
171 // guaranteed to only be called once per ActivityView. The taskId is
172 // saved to use for removeTask, preventing appearance in recent tasks.
173 mTaskId = taskId;
Mady Mellor3dff9e62019-02-05 18:12:53 -0800174 }
175
176 /**
177 * This is only called for tasks on this ActivityView, which is also set to
178 * single-task mode -- meaning never more than one task on this display. If a task
179 * is being removed, it's the top Activity finishing and this bubble should
180 * be removed or collapsed.
181 */
182 @Override
183 public void onTaskRemovalStarted(int taskId) {
Mark Renoufdb861c92019-07-26 13:58:17 -0400184 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
185 Log.d(TAG, "onTaskRemovalStarted: taskId=" + taskId
186 + " mActivityViewStatus=" + mActivityViewStatus
187 + " bubble=" + getBubbleKey());
188 }
Mady Mellorff076eb2019-11-13 10:12:06 -0800189 if (mBubble != null && !mBubbleController.isUserCreatedBubble(mBubble.getKey())) {
190 // Must post because this is called from a binder thread.
191 post(() -> mBubbleController.removeBubble(mBubble.getKey(),
192 BubbleController.DISMISS_TASK_FINISHED));
Mady Mellor3dff9e62019-02-05 18:12:53 -0800193 }
194 }
195 };
Mady Mellore8e07712019-01-23 12:45:33 -0800196
Mady Mellor3d82e682019-02-05 13:34:48 -0800197 public BubbleExpandedView(Context context) {
Mady Mellordea7ecf2018-12-10 15:47:40 -0800198 this(context, null);
199 }
200
Mady Mellor3d82e682019-02-05 13:34:48 -0800201 public BubbleExpandedView(Context context, AttributeSet attrs) {
Mady Mellordea7ecf2018-12-10 15:47:40 -0800202 this(context, attrs, 0);
203 }
204
Mady Mellor3d82e682019-02-05 13:34:48 -0800205 public BubbleExpandedView(Context context, AttributeSet attrs, int defStyleAttr) {
Mady Mellordea7ecf2018-12-10 15:47:40 -0800206 this(context, attrs, defStyleAttr, 0);
207 }
208
Mady Mellor3d82e682019-02-05 13:34:48 -0800209 public BubbleExpandedView(Context context, AttributeSet attrs, int defStyleAttr,
Mady Mellordea7ecf2018-12-10 15:47:40 -0800210 int defStyleRes) {
211 super(context, attrs, defStyleAttr, defStyleRes);
Mady Mellora96c9ed2019-06-07 12:55:26 -0700212 mDisplaySize = new Point();
Mady Mellor9be3bed2019-08-21 17:26:26 -0700213 mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Mady Mellor8fe411d2019-08-16 11:27:53 -0700214 // Get the real size -- this includes screen decorations (notches, statusbar, navbar).
Mady Mellor9be3bed2019-08-21 17:26:26 -0700215 mWindowManager.getDefaultDisplay().getRealSize(mDisplaySize);
Mady Mellor47b11e32019-07-11 19:06:21 -0700216 Resources res = getResources();
217 mMinHeight = res.getDimensionPixelSize(R.dimen.bubble_expanded_default_height);
218 mPointerMargin = res.getDimensionPixelSize(R.dimen.bubble_pointer_margin);
219 mExpandedViewTouchSlop = res.getDimensionPixelSize(R.dimen.bubble_expanded_view_slop);
Mady Mellordea7ecf2018-12-10 15:47:40 -0800220 }
221
222 @Override
223 protected void onFinishInflate() {
224 super.onFinishInflate();
Mark Renoufdb861c92019-07-26 13:58:17 -0400225 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
226 Log.d(TAG, "onFinishInflate: bubble=" + getBubbleKey());
227 }
Mady Mellordea7ecf2018-12-10 15:47:40 -0800228
229 Resources res = getResources();
230 mPointerView = findViewById(R.id.pointer_view);
Lyn Han02cca812019-04-02 16:27:32 -0700231 mPointerWidth = res.getDimensionPixelSize(R.dimen.bubble_pointer_width);
232 mPointerHeight = res.getDimensionPixelSize(R.dimen.bubble_pointer_height);
Mady Mellordd497052019-01-30 17:23:48 -0800233
Mady Mellordd497052019-01-30 17:23:48 -0800234
Mark Renouf34d04f32019-05-13 15:53:18 -0400235 mPointerDrawable = new ShapeDrawable(TriangleShape.create(
Lyn Han5aa27e22019-05-15 10:55:07 -0700236 mPointerWidth, mPointerHeight, true /* pointUp */));
Mark Renouf34d04f32019-05-13 15:53:18 -0400237 mPointerView.setBackground(mPointerDrawable);
Mady Mellora96c9ed2019-06-07 12:55:26 -0700238 mPointerView.setVisibility(INVISIBLE);
Mady Mellor9801e852019-01-22 14:50:28 -0800239
Lyn Han02cca812019-04-02 16:27:32 -0700240 mSettingsIconHeight = getContext().getResources().getDimensionPixelSize(
Mady Mellor818eef02019-08-16 16:12:29 -0700241 R.dimen.bubble_settings_size);
Lyn Han02cca812019-04-02 16:27:32 -0700242 mSettingsIcon = findViewById(R.id.settings_button);
Lyn Han02cca812019-04-02 16:27:32 -0700243 mSettingsIcon.setOnClickListener(this);
Lyn Han02cca812019-04-02 16:27:32 -0700244
Mady Mellor3dff9e62019-02-05 18:12:53 -0800245 mActivityView = new ActivityView(mContext, null /* attrs */, 0 /* defStyle */,
246 true /* singleTaskInstance */);
Lyn Hanb58c7562020-01-07 14:29:20 -0800247
lumarkdc9b3192019-07-23 21:18:17 +0800248 // Set ActivityView's alpha value as zero, since there is no view content to be shown.
Issei Suzukicac2a502019-04-16 16:52:50 +0200249 setContentVisibility(false);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800250 addView(mActivityView);
251
Lyn Han5aa27e22019-05-15 10:55:07 -0700252 // Expanded stack layout, top to bottom:
253 // Expanded view container
254 // ==> bubble row
255 // ==> expanded view
256 // ==> activity view
257 // ==> manage button
258 bringChildToFront(mActivityView);
259 bringChildToFront(mSettingsIcon);
Mady Mellor52b1ac62019-04-10 16:59:03 -0700260
Mark Renouf34d04f32019-05-13 15:53:18 -0400261 applyThemeAttrs();
262
Mady Mellor5d8f1402019-02-21 18:23:52 -0800263 setOnApplyWindowInsetsListener((View view, WindowInsets insets) -> {
264 // Keep track of IME displaying because we should not make any adjustments that might
265 // cause a config change while the IME is displayed otherwise it'll loose focus.
Mady Mellor3dff9e62019-02-05 18:12:53 -0800266 final int keyboardHeight = insets.getSystemWindowInsetBottom()
267 - insets.getStableInsetBottom();
Mady Mellor5d8f1402019-02-21 18:23:52 -0800268 mKeyboardVisible = keyboardHeight != 0;
269 if (!mKeyboardVisible && mNeedsNewHeight) {
270 updateHeight();
271 }
Mady Mellor3dff9e62019-02-05 18:12:53 -0800272 return view.onApplyWindowInsets(insets);
273 });
Mady Mellore8e07712019-01-23 12:45:33 -0800274 }
275
Mark Renoufdb861c92019-07-26 13:58:17 -0400276 private String getBubbleKey() {
277 return mBubble != null ? mBubble.getKey() : "null";
278 }
279
Mark Renouf34d04f32019-05-13 15:53:18 -0400280 void applyThemeAttrs() {
Lyn Han61bfdb32019-12-10 14:34:12 -0800281 final TypedArray ta = mContext.obtainStyledAttributes(
282 new int[] {
283 android.R.attr.colorBackgroundFloating,
284 android.R.attr.dialogCornerRadius});
285 int bgColor = ta.getColor(0, Color.WHITE);
286 float cornerRadius = ta.getDimensionPixelSize(1, 0);
Mark Renouf34d04f32019-05-13 15:53:18 -0400287 ta.recycle();
288
Mark Renouf34d04f32019-05-13 15:53:18 -0400289 mPointerDrawable.setTint(bgColor);
Mark Renouf34d04f32019-05-13 15:53:18 -0400290 if (ScreenDecorationsUtils.supportsRoundedCornersOnWindows(mContext.getResources())) {
291 mActivityView.setCornerRadius(cornerRadius);
292 }
293 }
294
Mady Mellor5d8f1402019-02-21 18:23:52 -0800295 @Override
296 protected void onDetachedFromWindow() {
297 super.onDetachedFromWindow();
298 mKeyboardVisible = false;
299 mNeedsNewHeight = false;
300 if (mActivityView != null) {
301 mActivityView.setForwardedInsets(Insets.of(0, 0, 0, 0));
302 }
Mark Renoufdb861c92019-07-26 13:58:17 -0400303 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
304 Log.d(TAG, "onDetachedFromWindow: bubble=" + getBubbleKey());
305 }
Mady Mellor5d8f1402019-02-21 18:23:52 -0800306 }
307
308 /**
Issei Suzukicac2a502019-04-16 16:52:50 +0200309 * Set visibility of contents in the expanded state.
310 *
311 * @param visibility {@code true} if the contents should be visible on the screen.
312 *
313 * Note that this contents visibility doesn't affect visibility at {@link android.view.View},
314 * and setting {@code false} actually means rendering the contents in transparent.
315 */
316 void setContentVisibility(boolean visibility) {
Mark Renoufdb861c92019-07-26 13:58:17 -0400317 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
318 Log.d(TAG, "setContentVisibility: visibility=" + visibility
319 + " bubble=" + getBubbleKey());
320 }
Issei Suzukicac2a502019-04-16 16:52:50 +0200321 final float alpha = visibility ? 1f : 0f;
322 mPointerView.setAlpha(alpha);
323 if (mActivityView != null) {
324 mActivityView.setAlpha(alpha);
325 }
326 }
327
328 /**
Mady Mellor5d8f1402019-02-21 18:23:52 -0800329 * Called by {@link BubbleStackView} when the insets for the expanded state should be updated.
330 * This should be done post-move and post-animation.
331 */
332 void updateInsets(WindowInsets insets) {
333 if (usingActivityView()) {
Mady Mellor8fe411d2019-08-16 11:27:53 -0700334 int[] screenLoc = mActivityView.getLocationOnScreen();
335 final int activityViewBottom = screenLoc[1] + mActivityView.getHeight();
Mady Mellor9be3bed2019-08-21 17:26:26 -0700336 final int keyboardTop = mDisplaySize.y - Math.max(insets.getSystemWindowInsetBottom(),
337 insets.getDisplayCutout() != null
338 ? insets.getDisplayCutout().getSafeInsetBottom()
339 : 0);
Mady Mellor8fe411d2019-08-16 11:27:53 -0700340 final int insetsBottom = Math.max(activityViewBottom - keyboardTop, 0);
Mady Mellor5d8f1402019-02-21 18:23:52 -0800341 mActivityView.setForwardedInsets(Insets.of(0, 0, 0, insetsBottom));
342 }
343 }
344
Mady Mellor247ca2c2019-12-02 16:18:59 -0800345 void setStackView(BubbleStackView stackView) {
Mady Mellor9801e852019-01-22 14:50:28 -0800346 mStackView = stackView;
Mady Mellor6d002032019-02-13 13:45:17 -0800347 }
348
Lyn Hanb58c7562020-01-07 14:29:20 -0800349 public void setOverflow(boolean overflow) {
350 mIsOverflow = overflow;
351
352 Intent target = new Intent(mContext, BubbleOverflowActivity.class);
353 mPendingIntent = PendingIntent.getActivity(mContext, /* requestCode */ 0,
354 target, PendingIntent.FLAG_UPDATE_CURRENT);
355 mSettingsIcon.setVisibility(GONE);
356 }
357
Mady Mellor6d002032019-02-13 13:45:17 -0800358 /**
Mady Mellor247ca2c2019-12-02 16:18:59 -0800359 * Sets the bubble used to populate this view.
Mady Mellor6d002032019-02-13 13:45:17 -0800360 */
Mady Mellor247ca2c2019-12-02 16:18:59 -0800361 void update(Bubble bubble) {
362 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
363 Log.d(TAG, "update: bubble=" + (bubble != null ? bubble.getKey() : "null"));
364 }
365 boolean isNew = mBubble == null;
Lyn Hanb58c7562020-01-07 14:29:20 -0800366 if (isNew || bubble != null && bubble.getKey().equals(mBubble.getKey())) {
Mady Mellor247ca2c2019-12-02 16:18:59 -0800367 mBubble = bubble;
368 mSettingsIcon.setContentDescription(getResources().getString(
369 R.string.bubbles_settings_button_description, bubble.getAppName()));
370
371 if (isNew) {
Lyn Hanb58c7562020-01-07 14:29:20 -0800372 mPendingIntent = mBubble.getBubbleIntent();
373 if (mPendingIntent != null || mBubble.getShortcutInfo() != null) {
Mady Mellor247ca2c2019-12-02 16:18:59 -0800374 setContentVisibility(false);
375 mActivityView.setVisibility(VISIBLE);
376 }
377 }
378 applyThemeAttrs();
379 } else {
380 Log.w(TAG, "Trying to update entry with different key, new bubble: "
381 + bubble.getKey() + " old bubble: " + bubble.getKey());
382 }
383 }
384
385 /**
386 * Lets activity view know it should be shown / populated with activity content.
387 */
388 void populateExpandedView() {
Mark Renoufdb861c92019-07-26 13:58:17 -0400389 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
390 Log.d(TAG, "populateExpandedView: "
391 + "bubble=" + getBubbleKey());
392 }
393
Mady Mellor5029fa62019-03-05 12:16:21 -0800394 if (usingActivityView()) {
395 mActivityView.setCallback(mStateCallback);
396 } else {
Issei Suzukia91f3962019-06-07 11:48:23 +0200397 Log.e(TAG, "Cannot populate expanded view.");
Mady Mellor5029fa62019-03-05 12:16:21 -0800398 }
Mady Mellor9801e852019-01-22 14:50:28 -0800399 }
400
Mark Renouf041d7262019-02-06 12:09:41 -0500401 boolean performBackPressIfNeeded() {
Mady Mellor323fb0b2019-03-25 12:15:22 -0700402 if (!usingActivityView()) {
Mark Renouf041d7262019-02-06 12:09:41 -0500403 return false;
404 }
405 mActivityView.performBackPress();
406 return true;
407 }
408
Lyn Hanb58c7562020-01-07 14:29:20 -0800409 // TODO(138116789) Fix overflow height.
Mady Mellorfe7ec032019-01-30 17:32:49 -0800410 void updateHeight() {
Mark Renoufdb861c92019-07-26 13:58:17 -0400411 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
412 Log.d(TAG, "updateHeight: bubble=" + getBubbleKey());
413 }
Mady Mellorfe7ec032019-01-30 17:32:49 -0800414 if (usingActivityView()) {
Lyn Hanb58c7562020-01-07 14:29:20 -0800415 float desiredHeight = mMinHeight;
416 if (!mIsOverflow) {
417 desiredHeight = Math.max(mBubble.getDesiredHeight(mContext), mMinHeight);
418 }
Mady Mellor8fe411d2019-08-16 11:27:53 -0700419 float height = Math.min(desiredHeight, getMaxExpandedHeight());
Mady Mellorfe7ec032019-01-30 17:32:49 -0800420 height = Math.max(height, mMinHeight);
421 LayoutParams lp = (LayoutParams) mActivityView.getLayoutParams();
Mady Mellor5d8f1402019-02-21 18:23:52 -0800422 mNeedsNewHeight = lp.height != height;
423 if (!mKeyboardVisible) {
424 // If the keyboard is visible... don't adjust the height because that will cause
425 // a configuration change and the keyboard will be lost.
Mady Mellor7af771a2019-03-07 15:04:54 -0800426 lp.height = (int) height;
Mady Mellor5d8f1402019-02-21 18:23:52 -0800427 mActivityView.setLayoutParams(lp);
428 mNeedsNewHeight = false;
429 }
Mark Renoufdb861c92019-07-26 13:58:17 -0400430 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
431 Log.d(TAG, "updateHeight: bubble=" + getBubbleKey() + " height=" + height
432 + " mNeedsNewHeight=" + mNeedsNewHeight);
433 }
Mady Mellorfe7ec032019-01-30 17:32:49 -0800434 }
435 }
436
Mady Mellora96c9ed2019-06-07 12:55:26 -0700437 private int getMaxExpandedHeight() {
Mady Mellor9be3bed2019-08-21 17:26:26 -0700438 mWindowManager.getDefaultDisplay().getRealSize(mDisplaySize);
Mady Mellora96c9ed2019-06-07 12:55:26 -0700439 int[] windowLocation = mActivityView.getLocationOnScreen();
Mady Mellor8fe411d2019-08-16 11:27:53 -0700440 int bottomInset = getRootWindowInsets() != null
441 ? getRootWindowInsets().getStableInsetBottom()
442 : 0;
Lyn Hanb58c7562020-01-07 14:29:20 -0800443 int mh = mDisplaySize.y - windowLocation[1] - mSettingsIconHeight - mPointerHeight
Mady Mellor8fe411d2019-08-16 11:27:53 -0700444 - mPointerMargin - bottomInset;
Lyn Hanb58c7562020-01-07 14:29:20 -0800445 Log.i(TAG, "max exp height: " + mh);
446// return mDisplaySize.y - windowLocation[1] - mSettingsIconHeight - mPointerHeight
447// - mPointerMargin - bottomInset;
448 return mh;
Mady Mellora96c9ed2019-06-07 12:55:26 -0700449 }
450
Mady Mellor47b11e32019-07-11 19:06:21 -0700451 /**
452 * Whether the provided x, y values (in raw coordinates) are in a touchable area of the
453 * expanded view.
454 *
455 * The touchable areas are the ActivityView (plus some slop around it) and the manage button.
456 */
457 boolean intersectingTouchableContent(int rawX, int rawY) {
458 mTempRect.setEmpty();
459 if (mActivityView != null) {
460 mTempLoc = mActivityView.getLocationOnScreen();
461 mTempRect.set(mTempLoc[0] - mExpandedViewTouchSlop,
462 mTempLoc[1] - mExpandedViewTouchSlop,
463 mTempLoc[0] + mActivityView.getWidth() + mExpandedViewTouchSlop,
464 mTempLoc[1] + mActivityView.getHeight() + mExpandedViewTouchSlop);
465 }
466 if (mTempRect.contains(rawX, rawY)) {
467 return true;
468 }
469 mTempLoc = mSettingsIcon.getLocationOnScreen();
470 mTempRect.set(mTempLoc[0],
471 mTempLoc[1],
472 mTempLoc[0] + mSettingsIcon.getWidth(),
473 mTempLoc[1] + mSettingsIcon.getHeight());
474 if (mTempRect.contains(rawX, rawY)) {
475 return true;
476 }
477 return false;
478 }
479
Mady Mellor9801e852019-01-22 14:50:28 -0800480 @Override
481 public void onClick(View view) {
Mady Mellor99a302602019-06-14 11:39:56 -0700482 if (mBubble == null) {
Mady Mellor9801e852019-01-22 14:50:28 -0800483 return;
484 }
Mady Mellor9801e852019-01-22 14:50:28 -0800485 int id = view.getId();
Lyn Hanc26ff122019-03-29 16:46:07 -0700486 if (id == R.id.settings_button) {
Mady Mellor99a302602019-06-14 11:39:56 -0700487 Intent intent = mBubble.getSettingsIntent();
Steven Wub00225b2019-02-08 14:27:42 -0500488 mStackView.collapseStack(() -> {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400489 mContext.startActivityAsUser(intent, mBubble.getEntry().getSbn().getUser());
Mady Mellor99a302602019-06-14 11:39:56 -0700490 logBubbleClickEvent(mBubble,
Muhammad Qureshi9bced7d2020-01-16 15:22:12 -0800491 SysUiStatsLog.BUBBLE_UICHANGED__ACTION__HEADER_GO_TO_SETTINGS);
Steven Wub00225b2019-02-08 14:27:42 -0500492 });
Mady Mellor9801e852019-01-22 14:50:28 -0800493 }
Mady Mellordea7ecf2018-12-10 15:47:40 -0800494 }
495
496 /**
Mady Mellor3dff9e62019-02-05 18:12:53 -0800497 * Update appearance of the expanded view being displayed.
498 */
499 public void updateView() {
Mark Renoufdb861c92019-07-26 13:58:17 -0400500 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
501 Log.d(TAG, "updateView: bubble="
502 + getBubbleKey());
503 }
Mady Mellor3dff9e62019-02-05 18:12:53 -0800504 if (usingActivityView()
505 && mActivityView.getVisibility() == VISIBLE
506 && mActivityView.isAttachedToWindow()) {
507 mActivityView.onLocationChanged();
Mady Mellor3dff9e62019-02-05 18:12:53 -0800508 }
Mady Mellorfe7ec032019-01-30 17:32:49 -0800509 updateHeight();
Mady Mellor3dff9e62019-02-05 18:12:53 -0800510 }
511
512 /**
Mady Mellordea7ecf2018-12-10 15:47:40 -0800513 * Set the x position that the tip of the triangle should point to.
514 */
Mady Mellor3dff9e62019-02-05 18:12:53 -0800515 public void setPointerPosition(float x) {
Lyn Han9a2f5cf2019-05-23 11:01:41 -0700516 float halfPointerWidth = mPointerWidth / 2f;
517 float pointerLeft = x - halfPointerWidth;
518 mPointerView.setTranslationX(pointerLeft);
Lyn Hanf74ba672019-05-20 16:08:48 -0700519 mPointerView.setVisibility(VISIBLE);
Mady Mellordea7ecf2018-12-10 15:47:40 -0800520 }
521
522 /**
Mady Mellor3dff9e62019-02-05 18:12:53 -0800523 * Removes and releases an ActivityView if one was previously created for this bubble.
Mady Mellordea7ecf2018-12-10 15:47:40 -0800524 */
Mady Mellor94d94a72019-03-05 18:16:59 -0800525 public void cleanUpExpandedState() {
Mark Renoufdb861c92019-07-26 13:58:17 -0400526 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
527 Log.d(TAG, "cleanUpExpandedState: mActivityViewStatus=" + mActivityViewStatus
528 + ", bubble=" + getBubbleKey());
529 }
Mady Mellor3dff9e62019-02-05 18:12:53 -0800530 if (mActivityView == null) {
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500531 return;
532 }
Issei Suzuki734bc942019-06-05 13:59:52 +0200533 switch (mActivityViewStatus) {
534 case INITIALIZED:
535 case ACTIVITY_STARTED:
536 mActivityView.release();
Mady Mellordea7ecf2018-12-10 15:47:40 -0800537 }
Mark Renouf5c732b32019-06-12 15:14:54 -0400538 if (mTaskId != -1) {
539 try {
540 ActivityTaskManager.getService().removeTask(mTaskId);
541 } catch (RemoteException e) {
542 Log.w(TAG, "Failed to remove taskId " + mTaskId);
543 }
544 mTaskId = -1;
Mady Mellordea7ecf2018-12-10 15:47:40 -0800545 }
Mark Renouf28c250d2019-02-25 16:47:34 -0500546 removeView(mActivityView);
Mark Renouf5c732b32019-06-12 15:14:54 -0400547
Mady Mellor3dff9e62019-02-05 18:12:53 -0800548 mActivityView = null;
Issei Suzuki734bc942019-06-05 13:59:52 +0200549 }
550
551 /**
552 * Called when the last task is removed from a {@link android.hardware.display.VirtualDisplay}
553 * which {@link ActivityView} uses.
554 */
555 void notifyDisplayEmpty() {
Mark Renoufdb861c92019-07-26 13:58:17 -0400556 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
557 Log.d(TAG, "notifyDisplayEmpty: bubble="
558 + getBubbleKey()
559 + " mActivityViewStatus=" + mActivityViewStatus);
560 }
Issei Suzuki734bc942019-06-05 13:59:52 +0200561 if (mActivityViewStatus == ActivityViewStatus.ACTIVITY_STARTED) {
562 mActivityViewStatus = ActivityViewStatus.INITIALIZED;
563 }
Mady Mellordea7ecf2018-12-10 15:47:40 -0800564 }
565
Mady Mellor3dff9e62019-02-05 18:12:53 -0800566 private boolean usingActivityView() {
Lyn Hanb58c7562020-01-07 14:29:20 -0800567 return (mPendingIntent != null || mBubble.getShortcutInfo() != null)
Mady Mellor2ac2d3a2020-01-08 17:18:54 -0800568 && mActivityView != null;
Mady Mellor3dff9e62019-02-05 18:12:53 -0800569 }
570
Mady Mellor390bff42019-04-05 15:09:01 -0700571 /**
572 * @return the display id of the virtual display.
573 */
574 public int getVirtualDisplayId() {
575 if (usingActivityView()) {
576 return mActivityView.getVirtualDisplayId();
577 }
578 return INVALID_DISPLAY;
579 }
580
Steven Wub00225b2019-02-08 14:27:42 -0500581 /**
582 * Logs bubble UI click event.
583 *
Mady Mellor99a302602019-06-14 11:39:56 -0700584 * @param bubble the bubble notification entry that user is interacting with.
Steven Wub00225b2019-02-08 14:27:42 -0500585 * @param action the user interaction enum.
586 */
Mady Mellor99a302602019-06-14 11:39:56 -0700587 private void logBubbleClickEvent(Bubble bubble, int action) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400588 StatusBarNotification notification = bubble.getEntry().getSbn();
Muhammad Qureshi9bced7d2020-01-16 15:22:12 -0800589 SysUiStatsLog.write(SysUiStatsLog.BUBBLE_UI_CHANGED,
Steven Wub00225b2019-02-08 14:27:42 -0500590 notification.getPackageName(),
591 notification.getNotification().getChannelId(),
592 notification.getId(),
593 mStackView.getBubbleIndex(mStackView.getExpandedBubble()),
594 mStackView.getBubbleCount(),
595 action,
596 mStackView.getNormalizedXPosition(),
Steven Wu45e38ae2019-03-25 16:16:59 -0400597 mStackView.getNormalizedYPosition(),
Mady Mellorb8aaf972019-11-26 10:28:00 -0800598 bubble.showInShade(),
Mady Mellor99a302602019-06-14 11:39:56 -0700599 bubble.isOngoing(),
Mady Mellor6cec3502019-06-17 19:20:49 -0700600 false /* isAppForeground (unused) */);
Mady Mellor7af771a2019-03-07 15:04:54 -0800601 }
Mady Mellordea7ecf2018-12-10 15:47:40 -0800602}