blob: 18d9fe75d876629845d2dd59875381cf389aef13 [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 Mellor390bff42019-04-05 15:09:01 -070019import static android.view.Display.INVALID_DISPLAY;
Mady Mellor3dff9e62019-02-05 18:12:53 -080020
Mark Renoufdb861c92019-07-26 13:58:17 -040021import static com.android.systemui.bubbles.BubbleDebugConfig.DEBUG_BUBBLE_EXPANDED_VIEW;
Issei Suzukia8d07312019-06-07 12:56:19 +020022import static com.android.systemui.bubbles.BubbleDebugConfig.TAG_BUBBLES;
23import static com.android.systemui.bubbles.BubbleDebugConfig.TAG_WITH_CLASS_NAME;
Mady Mellorca0c24c2019-05-16 16:14:32 -070024
Mady Mellor60101c92019-04-11 19:04:00 -070025import android.app.ActivityOptions;
Mark Renouf5c732b32019-06-12 15:14:54 -040026import android.app.ActivityTaskManager;
Mady Mellor3dff9e62019-02-05 18:12:53 -080027import android.app.ActivityView;
Mady Mellor9801e852019-01-22 14:50:28 -080028import android.app.PendingIntent;
Mark Renouf5c732b32019-06-12 15:14:54 -040029import android.content.ComponentName;
Mady Mellordea7ecf2018-12-10 15:47:40 -080030import android.content.Context;
Mady Mellor9801e852019-01-22 14:50:28 -080031import android.content.Intent;
32import android.content.pm.ApplicationInfo;
33import android.content.pm.PackageManager;
Mady Mellordea7ecf2018-12-10 15:47:40 -080034import android.content.res.Resources;
Mady Mellordd497052019-01-30 17:23:48 -080035import android.content.res.TypedArray;
Mady Mellordea7ecf2018-12-10 15:47:40 -080036import android.graphics.Color;
Mady Mellor3dff9e62019-02-05 18:12:53 -080037import android.graphics.Insets;
38import android.graphics.Point;
Mady Mellor47b11e32019-07-11 19:06:21 -070039import android.graphics.Rect;
Mady Mellore8e07712019-01-23 12:45:33 -080040import android.graphics.drawable.Drawable;
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;
Steven Wub00225b2019-02-08 14:27:42 -050046import android.util.StatsLog;
Mady Mellordea7ecf2018-12-10 15:47:40 -080047import android.view.View;
Mady Mellor3dff9e62019-02-05 18:12:53 -080048import android.view.WindowInsets;
Mady Mellora96c9ed2019-06-07 12:55:26 -070049import android.view.WindowManager;
Mady Mellordea7ecf2018-12-10 15:47:40 -080050import android.widget.LinearLayout;
51
Mark Renouf34d04f32019-05-13 15:53:18 -040052import com.android.internal.policy.ScreenDecorationsUtils;
Mady Mellor3dff9e62019-02-05 18:12:53 -080053import com.android.systemui.Dependency;
Mady Mellordea7ecf2018-12-10 15:47:40 -080054import com.android.systemui.R;
55import com.android.systemui.recents.TriangleShape;
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 }
74
Mady Mellordea7ecf2018-12-10 15:47:40 -080075 // 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
Mady Mellor3dff9e62019-02-05 18:12:53 -080087 private PendingIntent mBubbleIntent;
88
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
Mady Mellor99a302602019-06-14 11:39:56 -0700102 private Bubble mBubble;
Mady Mellor9801e852019-01-22 14:50:28 -0800103 private PackageManager mPm;
104 private String mAppName;
Mady Mellore8e07712019-01-23 12:45:33 -0800105 private Drawable mAppIcon;
106
Mady Mellor3dff9e62019-02-05 18:12:53 -0800107 private BubbleController mBubbleController = Dependency.get(BubbleController.class);
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 }
130 mActivityView.startActivity(mBubbleIntent, options);
131 });
Issei Suzuki734bc942019-06-05 13:59:52 +0200132 mActivityViewStatus = ActivityViewStatus.ACTIVITY_STARTED;
Mady Mellor6d002032019-02-13 13:45:17 -0800133 }
Mady Mellor3dff9e62019-02-05 18:12:53 -0800134 }
135
136 @Override
137 public void onActivityViewDestroyed(ActivityView view) {
Mark Renoufdb861c92019-07-26 13:58:17 -0400138 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
139 Log.d(TAG, "onActivityViewDestroyed: mActivityViewStatus=" + mActivityViewStatus
140 + " bubble=" + getBubbleKey());
141 }
Issei Suzuki734bc942019-06-05 13:59:52 +0200142 mActivityViewStatus = ActivityViewStatus.RELEASED;
Mady Mellor3dff9e62019-02-05 18:12:53 -0800143 }
144
Mark Renouf5c732b32019-06-12 15:14:54 -0400145 @Override
146 public void onTaskCreated(int taskId, ComponentName componentName) {
Mark Renoufdb861c92019-07-26 13:58:17 -0400147 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
148 Log.d(TAG, "onTaskCreated: taskId=" + taskId
149 + " bubble=" + getBubbleKey());
150 }
Mark Renouf5c732b32019-06-12 15:14:54 -0400151 // Since Bubble ActivityView applies singleTaskDisplay this is
152 // guaranteed to only be called once per ActivityView. The taskId is
153 // saved to use for removeTask, preventing appearance in recent tasks.
154 mTaskId = taskId;
155 }
156
Mady Mellor3dff9e62019-02-05 18:12:53 -0800157 /**
158 * This is only called for tasks on this ActivityView, which is also set to
159 * single-task mode -- meaning never more than one task on this display. If a task
160 * is being removed, it's the top Activity finishing and this bubble should
161 * be removed or collapsed.
162 */
163 @Override
164 public void onTaskRemovalStarted(int taskId) {
Mark Renoufdb861c92019-07-26 13:58:17 -0400165 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
166 Log.d(TAG, "onTaskRemovalStarted: taskId=" + taskId
167 + " mActivityViewStatus=" + mActivityViewStatus
168 + " bubble=" + getBubbleKey());
169 }
Mady Mellor99a302602019-06-14 11:39:56 -0700170 if (mBubble != null) {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800171 // Must post because this is called from a binder thread.
Mady Mellor99a302602019-06-14 11:39:56 -0700172 post(() -> mBubbleController.removeBubble(mBubble.getKey(),
Mark Renouf08bc42a2019-03-07 13:01:59 -0500173 BubbleController.DISMISS_TASK_FINISHED));
Mady Mellor3dff9e62019-02-05 18:12:53 -0800174 }
175 }
176 };
Mady Mellore8e07712019-01-23 12:45:33 -0800177
Mady Mellor3d82e682019-02-05 13:34:48 -0800178 public BubbleExpandedView(Context context) {
Mady Mellordea7ecf2018-12-10 15:47:40 -0800179 this(context, null);
180 }
181
Mady Mellor3d82e682019-02-05 13:34:48 -0800182 public BubbleExpandedView(Context context, AttributeSet attrs) {
Mady Mellordea7ecf2018-12-10 15:47:40 -0800183 this(context, attrs, 0);
184 }
185
Mady Mellor3d82e682019-02-05 13:34:48 -0800186 public BubbleExpandedView(Context context, AttributeSet attrs, int defStyleAttr) {
Mady Mellordea7ecf2018-12-10 15:47:40 -0800187 this(context, attrs, defStyleAttr, 0);
188 }
189
Mady Mellor3d82e682019-02-05 13:34:48 -0800190 public BubbleExpandedView(Context context, AttributeSet attrs, int defStyleAttr,
Mady Mellordea7ecf2018-12-10 15:47:40 -0800191 int defStyleRes) {
192 super(context, attrs, defStyleAttr, defStyleRes);
Mady Mellor9801e852019-01-22 14:50:28 -0800193 mPm = context.getPackageManager();
Mady Mellora96c9ed2019-06-07 12:55:26 -0700194 mDisplaySize = new Point();
195 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
196 wm.getDefaultDisplay().getSize(mDisplaySize);
Mady Mellor47b11e32019-07-11 19:06:21 -0700197 Resources res = getResources();
198 mMinHeight = res.getDimensionPixelSize(R.dimen.bubble_expanded_default_height);
199 mPointerMargin = res.getDimensionPixelSize(R.dimen.bubble_pointer_margin);
200 mExpandedViewTouchSlop = res.getDimensionPixelSize(R.dimen.bubble_expanded_view_slop);
Mady Mellordea7ecf2018-12-10 15:47:40 -0800201 }
202
203 @Override
204 protected void onFinishInflate() {
205 super.onFinishInflate();
Mark Renoufdb861c92019-07-26 13:58:17 -0400206 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
207 Log.d(TAG, "onFinishInflate: bubble=" + getBubbleKey());
208 }
Mady Mellordea7ecf2018-12-10 15:47:40 -0800209
210 Resources res = getResources();
211 mPointerView = findViewById(R.id.pointer_view);
Lyn Han02cca812019-04-02 16:27:32 -0700212 mPointerWidth = res.getDimensionPixelSize(R.dimen.bubble_pointer_width);
213 mPointerHeight = res.getDimensionPixelSize(R.dimen.bubble_pointer_height);
Mady Mellordd497052019-01-30 17:23:48 -0800214
Mady Mellordd497052019-01-30 17:23:48 -0800215
Mark Renouf34d04f32019-05-13 15:53:18 -0400216 mPointerDrawable = new ShapeDrawable(TriangleShape.create(
Lyn Han5aa27e22019-05-15 10:55:07 -0700217 mPointerWidth, mPointerHeight, true /* pointUp */));
Mark Renouf34d04f32019-05-13 15:53:18 -0400218 mPointerView.setBackground(mPointerDrawable);
Mady Mellora96c9ed2019-06-07 12:55:26 -0700219 mPointerView.setVisibility(INVISIBLE);
Mady Mellor9801e852019-01-22 14:50:28 -0800220
Lyn Han02cca812019-04-02 16:27:32 -0700221 mSettingsIconHeight = getContext().getResources().getDimensionPixelSize(
Mady Mellorfe7ec032019-01-30 17:32:49 -0800222 R.dimen.bubble_expanded_header_height);
Lyn Han02cca812019-04-02 16:27:32 -0700223 mSettingsIcon = findViewById(R.id.settings_button);
Lyn Han02cca812019-04-02 16:27:32 -0700224 mSettingsIcon.setOnClickListener(this);
Lyn Han02cca812019-04-02 16:27:32 -0700225
Mady Mellor3dff9e62019-02-05 18:12:53 -0800226 mActivityView = new ActivityView(mContext, null /* attrs */, 0 /* defStyle */,
227 true /* singleTaskInstance */);
Issei Suzukicac2a502019-04-16 16:52:50 +0200228
229 setContentVisibility(false);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800230 addView(mActivityView);
231
Lyn Han5aa27e22019-05-15 10:55:07 -0700232 // Expanded stack layout, top to bottom:
233 // Expanded view container
234 // ==> bubble row
235 // ==> expanded view
236 // ==> activity view
237 // ==> manage button
238 bringChildToFront(mActivityView);
239 bringChildToFront(mSettingsIcon);
Mady Mellor52b1ac62019-04-10 16:59:03 -0700240
Mark Renouf34d04f32019-05-13 15:53:18 -0400241 applyThemeAttrs();
242
Mady Mellor5d8f1402019-02-21 18:23:52 -0800243 setOnApplyWindowInsetsListener((View view, WindowInsets insets) -> {
244 // Keep track of IME displaying because we should not make any adjustments that might
245 // cause a config change while the IME is displayed otherwise it'll loose focus.
Mady Mellor3dff9e62019-02-05 18:12:53 -0800246 final int keyboardHeight = insets.getSystemWindowInsetBottom()
247 - insets.getStableInsetBottom();
Mady Mellor5d8f1402019-02-21 18:23:52 -0800248 mKeyboardVisible = keyboardHeight != 0;
249 if (!mKeyboardVisible && mNeedsNewHeight) {
250 updateHeight();
251 }
Mady Mellor3dff9e62019-02-05 18:12:53 -0800252 return view.onApplyWindowInsets(insets);
253 });
Mady Mellore8e07712019-01-23 12:45:33 -0800254 }
255
Mark Renoufdb861c92019-07-26 13:58:17 -0400256 private String getBubbleKey() {
257 return mBubble != null ? mBubble.getKey() : "null";
258 }
259
Mark Renouf34d04f32019-05-13 15:53:18 -0400260 void applyThemeAttrs() {
261 TypedArray ta = getContext().obtainStyledAttributes(R.styleable.BubbleExpandedView);
262 int bgColor = ta.getColor(
263 R.styleable.BubbleExpandedView_android_colorBackgroundFloating, Color.WHITE);
264 float cornerRadius = ta.getDimension(
265 R.styleable.BubbleExpandedView_android_dialogCornerRadius, 0);
266 ta.recycle();
267
268 // Update triangle color.
269 mPointerDrawable.setTint(bgColor);
270
271 // Update ActivityView cornerRadius
272 if (ScreenDecorationsUtils.supportsRoundedCornersOnWindows(mContext.getResources())) {
273 mActivityView.setCornerRadius(cornerRadius);
274 }
275 }
276
Mady Mellor5d8f1402019-02-21 18:23:52 -0800277 @Override
278 protected void onDetachedFromWindow() {
279 super.onDetachedFromWindow();
280 mKeyboardVisible = false;
281 mNeedsNewHeight = false;
282 if (mActivityView != null) {
283 mActivityView.setForwardedInsets(Insets.of(0, 0, 0, 0));
284 }
Mark Renoufdb861c92019-07-26 13:58:17 -0400285 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
286 Log.d(TAG, "onDetachedFromWindow: bubble=" + getBubbleKey());
287 }
Mady Mellor5d8f1402019-02-21 18:23:52 -0800288 }
289
290 /**
Issei Suzukicac2a502019-04-16 16:52:50 +0200291 * Set visibility of contents in the expanded state.
292 *
293 * @param visibility {@code true} if the contents should be visible on the screen.
294 *
295 * Note that this contents visibility doesn't affect visibility at {@link android.view.View},
296 * and setting {@code false} actually means rendering the contents in transparent.
297 */
298 void setContentVisibility(boolean visibility) {
Mark Renoufdb861c92019-07-26 13:58:17 -0400299 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
300 Log.d(TAG, "setContentVisibility: visibility=" + visibility
301 + " bubble=" + getBubbleKey());
302 }
Issei Suzukicac2a502019-04-16 16:52:50 +0200303 final float alpha = visibility ? 1f : 0f;
304 mPointerView.setAlpha(alpha);
305 if (mActivityView != null) {
306 mActivityView.setAlpha(alpha);
307 }
308 }
309
310 /**
Mady Mellor5d8f1402019-02-21 18:23:52 -0800311 * Called by {@link BubbleStackView} when the insets for the expanded state should be updated.
312 * This should be done post-move and post-animation.
313 */
314 void updateInsets(WindowInsets insets) {
315 if (usingActivityView()) {
Mady Mellor5d8f1402019-02-21 18:23:52 -0800316 int[] windowLocation = mActivityView.getLocationOnScreen();
317 final int windowBottom = windowLocation[1] + mActivityView.getHeight();
318 final int keyboardHeight = insets.getSystemWindowInsetBottom()
319 - insets.getStableInsetBottom();
320 final int insetsBottom = Math.max(0,
Mady Mellora96c9ed2019-06-07 12:55:26 -0700321 windowBottom + keyboardHeight - mDisplaySize.y);
Mady Mellor5d8f1402019-02-21 18:23:52 -0800322 mActivityView.setForwardedInsets(Insets.of(0, 0, 0, insetsBottom));
323 }
324 }
325
Mady Mellore8e07712019-01-23 12:45:33 -0800326 /**
Mady Mellor99a302602019-06-14 11:39:56 -0700327 * Sets the bubble used to populate this view.
Mady Mellor9801e852019-01-22 14:50:28 -0800328 */
Mady Mellor99a302602019-06-14 11:39:56 -0700329 public void setBubble(Bubble bubble, BubbleStackView stackView, String appName) {
Mark Renoufdb861c92019-07-26 13:58:17 -0400330 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
331 Log.d(TAG, "setBubble: bubble=" + (bubble != null ? bubble.getKey() : "null"));
332 }
333
Mady Mellor9801e852019-01-22 14:50:28 -0800334 mStackView = stackView;
Mady Mellor99a302602019-06-14 11:39:56 -0700335 mBubble = bubble;
Lyn Han6c40fe72019-05-08 14:06:33 -0700336 mAppName = appName;
Mady Mellor9801e852019-01-22 14:50:28 -0800337
Mady Mellor9801e852019-01-22 14:50:28 -0800338 try {
Issei Suzukia91f3962019-06-07 11:48:23 +0200339 ApplicationInfo info = mPm.getApplicationInfo(
Mady Mellor99a302602019-06-14 11:39:56 -0700340 bubble.getPackageName(),
Mady Mellor9801e852019-01-22 14:50:28 -0800341 PackageManager.MATCH_UNINSTALLED_PACKAGES
342 | PackageManager.MATCH_DISABLED_COMPONENTS
343 | PackageManager.MATCH_DIRECT_BOOT_UNAWARE
344 | PackageManager.MATCH_DIRECT_BOOT_AWARE);
Issei Suzukia91f3962019-06-07 11:48:23 +0200345 mAppIcon = mPm.getApplicationIcon(info);
Mady Mellor9801e852019-01-22 14:50:28 -0800346 } catch (PackageManager.NameNotFoundException e) {
Lyn Han6c40fe72019-05-08 14:06:33 -0700347 // Do nothing.
Mady Mellor9801e852019-01-22 14:50:28 -0800348 }
Mady Mellore8e07712019-01-23 12:45:33 -0800349 if (mAppIcon == null) {
350 mAppIcon = mPm.getDefaultActivityIcon();
351 }
Mark Renouf34d04f32019-05-13 15:53:18 -0400352 applyThemeAttrs();
Lyn Han69149122019-04-30 12:03:12 -0700353 showSettingsIcon();
Mady Mellor3dff9e62019-02-05 18:12:53 -0800354 updateExpandedView();
Mady Mellor6d002032019-02-13 13:45:17 -0800355 }
356
357 /**
358 * Lets activity view know it should be shown / populated.
359 */
Mady Mellor5029fa62019-03-05 12:16:21 -0800360 public void populateExpandedView() {
Mark Renoufdb861c92019-07-26 13:58:17 -0400361 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
362 Log.d(TAG, "populateExpandedView: "
363 + "bubble=" + getBubbleKey());
364 }
365
Mady Mellor5029fa62019-03-05 12:16:21 -0800366 if (usingActivityView()) {
367 mActivityView.setCallback(mStateCallback);
368 } else {
Issei Suzukia91f3962019-06-07 11:48:23 +0200369 Log.e(TAG, "Cannot populate expanded view.");
Mady Mellor5029fa62019-03-05 12:16:21 -0800370 }
Mady Mellor9801e852019-01-22 14:50:28 -0800371 }
372
Mady Mellorfe7ec032019-01-30 17:32:49 -0800373 /**
Mady Mellor99a302602019-06-14 11:39:56 -0700374 * Updates the bubble backing this view. This will not re-populate ActivityView, it will
Lyn Han02cca812019-04-02 16:27:32 -0700375 * only update the deep-links in the title, and the height of the view.
Mady Mellorfe7ec032019-01-30 17:32:49 -0800376 */
Mady Mellor99a302602019-06-14 11:39:56 -0700377 public void update(Bubble bubble) {
Mark Renoufdb861c92019-07-26 13:58:17 -0400378 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
379 Log.d(TAG, "update: bubble=" + (bubble != null ? bubble.getKey() : "null"));
380 }
Mady Mellor99a302602019-06-14 11:39:56 -0700381 if (bubble.getKey().equals(mBubble.getKey())) {
382 mBubble = bubble;
Lyn Han02cca812019-04-02 16:27:32 -0700383 updateSettingsContentDescription();
Mady Mellorfe7ec032019-01-30 17:32:49 -0800384 updateHeight();
385 } else {
Mady Mellor99a302602019-06-14 11:39:56 -0700386 Log.w(TAG, "Trying to update entry with different key, new bubble: "
387 + bubble.getKey() + " old bubble: " + bubble.getKey());
Mady Mellorfe7ec032019-01-30 17:32:49 -0800388 }
389 }
390
Mady Mellor3dff9e62019-02-05 18:12:53 -0800391 private void updateExpandedView() {
Mark Renoufdb861c92019-07-26 13:58:17 -0400392 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
393 Log.d(TAG, "updateExpandedView: bubble="
394 + getBubbleKey());
395 }
396
Mady Mellor99a302602019-06-14 11:39:56 -0700397 mBubbleIntent = mBubble.getBubbleIntent(mContext);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800398 if (mBubbleIntent != null) {
Issei Suzukicac2a502019-04-16 16:52:50 +0200399 setContentVisibility(false);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800400 mActivityView.setVisibility(VISIBLE);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800401 }
402 updateView();
403 }
404
Mark Renouf041d7262019-02-06 12:09:41 -0500405 boolean performBackPressIfNeeded() {
Mady Mellor323fb0b2019-03-25 12:15:22 -0700406 if (!usingActivityView()) {
Mark Renouf041d7262019-02-06 12:09:41 -0500407 return false;
408 }
409 mActivityView.performBackPress();
410 return true;
411 }
412
Mady Mellorfe7ec032019-01-30 17:32:49 -0800413 void updateHeight() {
Mark Renoufdb861c92019-07-26 13:58:17 -0400414 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
415 Log.d(TAG, "updateHeight: bubble=" + getBubbleKey());
416 }
Mady Mellorfe7ec032019-01-30 17:32:49 -0800417 if (usingActivityView()) {
Mady Mellor99a302602019-06-14 11:39:56 -0700418 int max = getMaxExpandedHeight() - mSettingsIconHeight - mPointerHeight
419 - mPointerMargin;
420 float desiredHeight = Math.max(mBubble.getDesiredHeight(mContext), mMinHeight);
421 float height = Math.min(desiredHeight, max);
Mady Mellorfe7ec032019-01-30 17:32:49 -0800422 height = Math.max(height, mMinHeight);
423 LayoutParams lp = (LayoutParams) mActivityView.getLayoutParams();
Mady Mellor5d8f1402019-02-21 18:23:52 -0800424 mNeedsNewHeight = lp.height != height;
425 if (!mKeyboardVisible) {
426 // If the keyboard is visible... don't adjust the height because that will cause
427 // a configuration change and the keyboard will be lost.
Mady Mellor7af771a2019-03-07 15:04:54 -0800428 lp.height = (int) height;
Mady Mellor5d8f1402019-02-21 18:23:52 -0800429 mActivityView.setLayoutParams(lp);
430 mNeedsNewHeight = false;
431 }
Mark Renoufdb861c92019-07-26 13:58:17 -0400432 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
433 Log.d(TAG, "updateHeight: bubble=" + getBubbleKey() + " height=" + height
434 + " mNeedsNewHeight=" + mNeedsNewHeight);
435 }
Mady Mellorfe7ec032019-01-30 17:32:49 -0800436 }
437 }
438
Mady Mellora96c9ed2019-06-07 12:55:26 -0700439 private int getMaxExpandedHeight() {
440 int[] windowLocation = mActivityView.getLocationOnScreen();
441 return mDisplaySize.y - windowLocation[1] - mSettingsIconHeight;
442 }
443
Mady Mellor47b11e32019-07-11 19:06:21 -0700444 /**
445 * Whether the provided x, y values (in raw coordinates) are in a touchable area of the
446 * expanded view.
447 *
448 * The touchable areas are the ActivityView (plus some slop around it) and the manage button.
449 */
450 boolean intersectingTouchableContent(int rawX, int rawY) {
451 mTempRect.setEmpty();
452 if (mActivityView != null) {
453 mTempLoc = mActivityView.getLocationOnScreen();
454 mTempRect.set(mTempLoc[0] - mExpandedViewTouchSlop,
455 mTempLoc[1] - mExpandedViewTouchSlop,
456 mTempLoc[0] + mActivityView.getWidth() + mExpandedViewTouchSlop,
457 mTempLoc[1] + mActivityView.getHeight() + mExpandedViewTouchSlop);
458 }
459 if (mTempRect.contains(rawX, rawY)) {
460 return true;
461 }
462 mTempLoc = mSettingsIcon.getLocationOnScreen();
463 mTempRect.set(mTempLoc[0],
464 mTempLoc[1],
465 mTempLoc[0] + mSettingsIcon.getWidth(),
466 mTempLoc[1] + mSettingsIcon.getHeight());
467 if (mTempRect.contains(rawX, rawY)) {
468 return true;
469 }
470 return false;
471 }
472
Mady Mellor9801e852019-01-22 14:50:28 -0800473 @Override
474 public void onClick(View view) {
Mady Mellor99a302602019-06-14 11:39:56 -0700475 if (mBubble == null) {
Mady Mellor9801e852019-01-22 14:50:28 -0800476 return;
477 }
Mady Mellor9801e852019-01-22 14:50:28 -0800478 int id = view.getId();
Lyn Hanc26ff122019-03-29 16:46:07 -0700479 if (id == R.id.settings_button) {
Mady Mellor99a302602019-06-14 11:39:56 -0700480 Intent intent = mBubble.getSettingsIntent();
Steven Wub00225b2019-02-08 14:27:42 -0500481 mStackView.collapseStack(() -> {
Mady Mellor99a302602019-06-14 11:39:56 -0700482 mContext.startActivityAsUser(intent, mBubble.getEntry().notification.getUser());
483 logBubbleClickEvent(mBubble,
Steven Wub00225b2019-02-08 14:27:42 -0500484 StatsLog.BUBBLE_UICHANGED__ACTION__HEADER_GO_TO_SETTINGS);
485 });
Mady Mellor9801e852019-01-22 14:50:28 -0800486 }
Mady Mellordea7ecf2018-12-10 15:47:40 -0800487 }
488
Lyn Han02cca812019-04-02 16:27:32 -0700489 private void updateSettingsContentDescription() {
490 mSettingsIcon.setContentDescription(getResources().getString(
491 R.string.bubbles_settings_button_description, mAppName));
492 }
493
Lyn Hanc26ff122019-03-29 16:46:07 -0700494 void showSettingsIcon() {
Lyn Han02cca812019-04-02 16:27:32 -0700495 updateSettingsContentDescription();
Lyn Hanc26ff122019-03-29 16:46:07 -0700496 mSettingsIcon.setVisibility(VISIBLE);
497 }
498
Mady Mellordea7ecf2018-12-10 15:47:40 -0800499 /**
Mady Mellor3dff9e62019-02-05 18:12:53 -0800500 * Update appearance of the expanded view being displayed.
501 */
502 public void updateView() {
Mark Renoufdb861c92019-07-26 13:58:17 -0400503 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
504 Log.d(TAG, "updateView: bubble="
505 + getBubbleKey());
506 }
Mady Mellor3dff9e62019-02-05 18:12:53 -0800507 if (usingActivityView()
508 && mActivityView.getVisibility() == VISIBLE
509 && mActivityView.isAttachedToWindow()) {
510 mActivityView.onLocationChanged();
Mady Mellor3dff9e62019-02-05 18:12:53 -0800511 }
Mady Mellorfe7ec032019-01-30 17:32:49 -0800512 updateHeight();
Mady Mellor3dff9e62019-02-05 18:12:53 -0800513 }
514
515 /**
Mady Mellordea7ecf2018-12-10 15:47:40 -0800516 * Set the x position that the tip of the triangle should point to.
517 */
Mady Mellor3dff9e62019-02-05 18:12:53 -0800518 public void setPointerPosition(float x) {
Lyn Han9a2f5cf2019-05-23 11:01:41 -0700519 float halfPointerWidth = mPointerWidth / 2f;
520 float pointerLeft = x - halfPointerWidth;
521 mPointerView.setTranslationX(pointerLeft);
Lyn Hanf74ba672019-05-20 16:08:48 -0700522 mPointerView.setVisibility(VISIBLE);
Mady Mellordea7ecf2018-12-10 15:47:40 -0800523 }
524
525 /**
Mady Mellor3dff9e62019-02-05 18:12:53 -0800526 * Removes and releases an ActivityView if one was previously created for this bubble.
Mady Mellordea7ecf2018-12-10 15:47:40 -0800527 */
Mady Mellor94d94a72019-03-05 18:16:59 -0800528 public void cleanUpExpandedState() {
Mark Renoufdb861c92019-07-26 13:58:17 -0400529 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
530 Log.d(TAG, "cleanUpExpandedState: mActivityViewStatus=" + mActivityViewStatus
531 + ", bubble=" + getBubbleKey());
532 }
Mady Mellor3dff9e62019-02-05 18:12:53 -0800533 if (mActivityView == null) {
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500534 return;
535 }
Issei Suzuki734bc942019-06-05 13:59:52 +0200536 switch (mActivityViewStatus) {
537 case INITIALIZED:
538 case ACTIVITY_STARTED:
539 mActivityView.release();
Mady Mellordea7ecf2018-12-10 15:47:40 -0800540 }
Mark Renouf5c732b32019-06-12 15:14:54 -0400541 if (mTaskId != -1) {
542 try {
543 ActivityTaskManager.getService().removeTask(mTaskId);
544 } catch (RemoteException e) {
545 Log.w(TAG, "Failed to remove taskId " + mTaskId);
546 }
547 mTaskId = -1;
548 }
Mark Renouf28c250d2019-02-25 16:47:34 -0500549 removeView(mActivityView);
Mark Renouf5c732b32019-06-12 15:14:54 -0400550
Mady Mellor3dff9e62019-02-05 18:12:53 -0800551 mActivityView = null;
Issei Suzuki734bc942019-06-05 13:59:52 +0200552 }
553
554 /**
555 * Called when the last task is removed from a {@link android.hardware.display.VirtualDisplay}
556 * which {@link ActivityView} uses.
557 */
558 void notifyDisplayEmpty() {
Mark Renoufdb861c92019-07-26 13:58:17 -0400559 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
560 Log.d(TAG, "notifyDisplayEmpty: bubble="
561 + getBubbleKey()
562 + " mActivityViewStatus=" + mActivityViewStatus);
563 }
Issei Suzuki734bc942019-06-05 13:59:52 +0200564 if (mActivityViewStatus == ActivityViewStatus.ACTIVITY_STARTED) {
565 mActivityViewStatus = ActivityViewStatus.INITIALIZED;
566 }
Mady Mellordea7ecf2018-12-10 15:47:40 -0800567 }
568
Mady Mellor3dff9e62019-02-05 18:12:53 -0800569 private boolean usingActivityView() {
Mady Mellor323fb0b2019-03-25 12:15:22 -0700570 return mBubbleIntent != null && mActivityView != null;
Mady Mellor3dff9e62019-02-05 18:12:53 -0800571 }
572
Mady Mellor390bff42019-04-05 15:09:01 -0700573 /**
574 * @return the display id of the virtual display.
575 */
576 public int getVirtualDisplayId() {
577 if (usingActivityView()) {
578 return mActivityView.getVirtualDisplayId();
579 }
580 return INVALID_DISPLAY;
581 }
582
Mady Mellor3dff9e62019-02-05 18:12:53 -0800583 /**
Steven Wub00225b2019-02-08 14:27:42 -0500584 * Logs bubble UI click event.
585 *
Mady Mellor99a302602019-06-14 11:39:56 -0700586 * @param bubble the bubble notification entry that user is interacting with.
Steven Wub00225b2019-02-08 14:27:42 -0500587 * @param action the user interaction enum.
588 */
Mady Mellor99a302602019-06-14 11:39:56 -0700589 private void logBubbleClickEvent(Bubble bubble, int action) {
590 StatusBarNotification notification = bubble.getEntry().notification;
Steven Wub00225b2019-02-08 14:27:42 -0500591 StatsLog.write(StatsLog.BUBBLE_UI_CHANGED,
592 notification.getPackageName(),
593 notification.getNotification().getChannelId(),
594 notification.getId(),
595 mStackView.getBubbleIndex(mStackView.getExpandedBubble()),
596 mStackView.getBubbleCount(),
597 action,
598 mStackView.getNormalizedXPosition(),
Steven Wu45e38ae2019-03-25 16:16:59 -0400599 mStackView.getNormalizedYPosition(),
Mady Mellor99a302602019-06-14 11:39:56 -0700600 bubble.showInShadeWhenBubble(),
601 bubble.isOngoing(),
Mady Mellor6cec3502019-06-17 19:20:49 -0700602 false /* isAppForeground (unused) */);
Steven Wub00225b2019-02-08 14:27:42 -0500603 }
Mady Mellordea7ecf2018-12-10 15:47:40 -0800604}