blob: 59ee46de16fe6d5f48b5627442712a0a69bbbd19 [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 }
Mady Mellor8454ddf2019-08-15 11:16:23 -0700130 try {
131 mActivityView.startActivity(mBubbleIntent, options);
132 } catch (RuntimeException e) {
133 // If there's a runtime exception here then there's something
134 // wrong with the intent, we can't really recover / try to populate
135 // the bubble again so we'll just remove it.
136 Log.w(TAG, "Exception while displaying bubble: " + getBubbleKey()
137 + ", " + e.getMessage() + "; removing bubble");
138 mBubbleController.removeBubble(mBubble.getKey(),
139 BubbleController.DISMISS_INVALID_INTENT);
140 }
Mark Renoufdb861c92019-07-26 13:58:17 -0400141 });
Issei Suzuki734bc942019-06-05 13:59:52 +0200142 mActivityViewStatus = ActivityViewStatus.ACTIVITY_STARTED;
Mady Mellor6d002032019-02-13 13:45:17 -0800143 }
Mady Mellor3dff9e62019-02-05 18:12:53 -0800144 }
145
146 @Override
147 public void onActivityViewDestroyed(ActivityView view) {
Mark Renoufdb861c92019-07-26 13:58:17 -0400148 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
149 Log.d(TAG, "onActivityViewDestroyed: mActivityViewStatus=" + mActivityViewStatus
150 + " bubble=" + getBubbleKey());
151 }
Issei Suzuki734bc942019-06-05 13:59:52 +0200152 mActivityViewStatus = ActivityViewStatus.RELEASED;
Mady Mellor3dff9e62019-02-05 18:12:53 -0800153 }
154
Mark Renouf5c732b32019-06-12 15:14:54 -0400155 @Override
156 public void onTaskCreated(int taskId, ComponentName componentName) {
Mark Renoufdb861c92019-07-26 13:58:17 -0400157 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
158 Log.d(TAG, "onTaskCreated: taskId=" + taskId
159 + " bubble=" + getBubbleKey());
160 }
Mark Renouf5c732b32019-06-12 15:14:54 -0400161 // Since Bubble ActivityView applies singleTaskDisplay this is
162 // guaranteed to only be called once per ActivityView. The taskId is
163 // saved to use for removeTask, preventing appearance in recent tasks.
164 mTaskId = taskId;
165 }
166
Mady Mellor3dff9e62019-02-05 18:12:53 -0800167 /**
168 * This is only called for tasks on this ActivityView, which is also set to
169 * single-task mode -- meaning never more than one task on this display. If a task
170 * is being removed, it's the top Activity finishing and this bubble should
171 * be removed or collapsed.
172 */
173 @Override
174 public void onTaskRemovalStarted(int taskId) {
Mark Renoufdb861c92019-07-26 13:58:17 -0400175 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
176 Log.d(TAG, "onTaskRemovalStarted: taskId=" + taskId
177 + " mActivityViewStatus=" + mActivityViewStatus
178 + " bubble=" + getBubbleKey());
179 }
Mady Mellor99a302602019-06-14 11:39:56 -0700180 if (mBubble != null) {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800181 // Must post because this is called from a binder thread.
Mady Mellor99a302602019-06-14 11:39:56 -0700182 post(() -> mBubbleController.removeBubble(mBubble.getKey(),
Mark Renouf08bc42a2019-03-07 13:01:59 -0500183 BubbleController.DISMISS_TASK_FINISHED));
Mady Mellor3dff9e62019-02-05 18:12:53 -0800184 }
185 }
186 };
Mady Mellore8e07712019-01-23 12:45:33 -0800187
Mady Mellor3d82e682019-02-05 13:34:48 -0800188 public BubbleExpandedView(Context context) {
Mady Mellordea7ecf2018-12-10 15:47:40 -0800189 this(context, null);
190 }
191
Mady Mellor3d82e682019-02-05 13:34:48 -0800192 public BubbleExpandedView(Context context, AttributeSet attrs) {
Mady Mellordea7ecf2018-12-10 15:47:40 -0800193 this(context, attrs, 0);
194 }
195
Mady Mellor3d82e682019-02-05 13:34:48 -0800196 public BubbleExpandedView(Context context, AttributeSet attrs, int defStyleAttr) {
Mady Mellordea7ecf2018-12-10 15:47:40 -0800197 this(context, attrs, defStyleAttr, 0);
198 }
199
Mady Mellor3d82e682019-02-05 13:34:48 -0800200 public BubbleExpandedView(Context context, AttributeSet attrs, int defStyleAttr,
Mady Mellordea7ecf2018-12-10 15:47:40 -0800201 int defStyleRes) {
202 super(context, attrs, defStyleAttr, defStyleRes);
Mady Mellor9801e852019-01-22 14:50:28 -0800203 mPm = context.getPackageManager();
Mady Mellora96c9ed2019-06-07 12:55:26 -0700204 mDisplaySize = new Point();
205 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Mady Mellor8fe411d2019-08-16 11:27:53 -0700206 // Get the real size -- this includes screen decorations (notches, statusbar, navbar).
207 wm.getDefaultDisplay().getRealSize(mDisplaySize);
Mady Mellor47b11e32019-07-11 19:06:21 -0700208 Resources res = getResources();
209 mMinHeight = res.getDimensionPixelSize(R.dimen.bubble_expanded_default_height);
210 mPointerMargin = res.getDimensionPixelSize(R.dimen.bubble_pointer_margin);
211 mExpandedViewTouchSlop = res.getDimensionPixelSize(R.dimen.bubble_expanded_view_slop);
Mady Mellordea7ecf2018-12-10 15:47:40 -0800212 }
213
214 @Override
215 protected void onFinishInflate() {
216 super.onFinishInflate();
Mark Renoufdb861c92019-07-26 13:58:17 -0400217 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
218 Log.d(TAG, "onFinishInflate: bubble=" + getBubbleKey());
219 }
Mady Mellordea7ecf2018-12-10 15:47:40 -0800220
221 Resources res = getResources();
222 mPointerView = findViewById(R.id.pointer_view);
Lyn Han02cca812019-04-02 16:27:32 -0700223 mPointerWidth = res.getDimensionPixelSize(R.dimen.bubble_pointer_width);
224 mPointerHeight = res.getDimensionPixelSize(R.dimen.bubble_pointer_height);
Mady Mellordd497052019-01-30 17:23:48 -0800225
Mady Mellordd497052019-01-30 17:23:48 -0800226
Mark Renouf34d04f32019-05-13 15:53:18 -0400227 mPointerDrawable = new ShapeDrawable(TriangleShape.create(
Lyn Han5aa27e22019-05-15 10:55:07 -0700228 mPointerWidth, mPointerHeight, true /* pointUp */));
Mark Renouf34d04f32019-05-13 15:53:18 -0400229 mPointerView.setBackground(mPointerDrawable);
Mady Mellora96c9ed2019-06-07 12:55:26 -0700230 mPointerView.setVisibility(INVISIBLE);
Mady Mellor9801e852019-01-22 14:50:28 -0800231
Lyn Han02cca812019-04-02 16:27:32 -0700232 mSettingsIconHeight = getContext().getResources().getDimensionPixelSize(
Mady Mellor818eef02019-08-16 16:12:29 -0700233 R.dimen.bubble_settings_size);
Lyn Han02cca812019-04-02 16:27:32 -0700234 mSettingsIcon = findViewById(R.id.settings_button);
Lyn Han02cca812019-04-02 16:27:32 -0700235 mSettingsIcon.setOnClickListener(this);
Lyn Han02cca812019-04-02 16:27:32 -0700236
Mady Mellor3dff9e62019-02-05 18:12:53 -0800237 mActivityView = new ActivityView(mContext, null /* attrs */, 0 /* defStyle */,
238 true /* singleTaskInstance */);
Issei Suzukicac2a502019-04-16 16:52:50 +0200239
240 setContentVisibility(false);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800241 addView(mActivityView);
242
Lyn Han5aa27e22019-05-15 10:55:07 -0700243 // Expanded stack layout, top to bottom:
244 // Expanded view container
245 // ==> bubble row
246 // ==> expanded view
247 // ==> activity view
248 // ==> manage button
249 bringChildToFront(mActivityView);
250 bringChildToFront(mSettingsIcon);
Mady Mellor52b1ac62019-04-10 16:59:03 -0700251
Mark Renouf34d04f32019-05-13 15:53:18 -0400252 applyThemeAttrs();
253
Mady Mellor5d8f1402019-02-21 18:23:52 -0800254 setOnApplyWindowInsetsListener((View view, WindowInsets insets) -> {
255 // Keep track of IME displaying because we should not make any adjustments that might
256 // cause a config change while the IME is displayed otherwise it'll loose focus.
Mady Mellor3dff9e62019-02-05 18:12:53 -0800257 final int keyboardHeight = insets.getSystemWindowInsetBottom()
258 - insets.getStableInsetBottom();
Mady Mellor5d8f1402019-02-21 18:23:52 -0800259 mKeyboardVisible = keyboardHeight != 0;
260 if (!mKeyboardVisible && mNeedsNewHeight) {
261 updateHeight();
262 }
Mady Mellor3dff9e62019-02-05 18:12:53 -0800263 return view.onApplyWindowInsets(insets);
264 });
Mady Mellore8e07712019-01-23 12:45:33 -0800265 }
266
Mark Renoufdb861c92019-07-26 13:58:17 -0400267 private String getBubbleKey() {
268 return mBubble != null ? mBubble.getKey() : "null";
269 }
270
Mark Renouf34d04f32019-05-13 15:53:18 -0400271 void applyThemeAttrs() {
272 TypedArray ta = getContext().obtainStyledAttributes(R.styleable.BubbleExpandedView);
273 int bgColor = ta.getColor(
274 R.styleable.BubbleExpandedView_android_colorBackgroundFloating, Color.WHITE);
275 float cornerRadius = ta.getDimension(
276 R.styleable.BubbleExpandedView_android_dialogCornerRadius, 0);
277 ta.recycle();
278
279 // Update triangle color.
280 mPointerDrawable.setTint(bgColor);
281
282 // Update ActivityView cornerRadius
283 if (ScreenDecorationsUtils.supportsRoundedCornersOnWindows(mContext.getResources())) {
284 mActivityView.setCornerRadius(cornerRadius);
285 }
286 }
287
Mady Mellor5d8f1402019-02-21 18:23:52 -0800288 @Override
289 protected void onDetachedFromWindow() {
290 super.onDetachedFromWindow();
291 mKeyboardVisible = false;
292 mNeedsNewHeight = false;
293 if (mActivityView != null) {
294 mActivityView.setForwardedInsets(Insets.of(0, 0, 0, 0));
295 }
Mark Renoufdb861c92019-07-26 13:58:17 -0400296 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
297 Log.d(TAG, "onDetachedFromWindow: bubble=" + getBubbleKey());
298 }
Mady Mellor5d8f1402019-02-21 18:23:52 -0800299 }
300
301 /**
Issei Suzukicac2a502019-04-16 16:52:50 +0200302 * Set visibility of contents in the expanded state.
303 *
304 * @param visibility {@code true} if the contents should be visible on the screen.
305 *
306 * Note that this contents visibility doesn't affect visibility at {@link android.view.View},
307 * and setting {@code false} actually means rendering the contents in transparent.
308 */
309 void setContentVisibility(boolean visibility) {
Mark Renoufdb861c92019-07-26 13:58:17 -0400310 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
311 Log.d(TAG, "setContentVisibility: visibility=" + visibility
312 + " bubble=" + getBubbleKey());
313 }
Issei Suzukicac2a502019-04-16 16:52:50 +0200314 final float alpha = visibility ? 1f : 0f;
315 mPointerView.setAlpha(alpha);
316 if (mActivityView != null) {
317 mActivityView.setAlpha(alpha);
318 }
319 }
320
321 /**
Mady Mellor5d8f1402019-02-21 18:23:52 -0800322 * Called by {@link BubbleStackView} when the insets for the expanded state should be updated.
323 * This should be done post-move and post-animation.
324 */
325 void updateInsets(WindowInsets insets) {
326 if (usingActivityView()) {
Mady Mellor8fe411d2019-08-16 11:27:53 -0700327 int[] screenLoc = mActivityView.getLocationOnScreen();
328 final int activityViewBottom = screenLoc[1] + mActivityView.getHeight();
329 final int keyboardTop = mDisplaySize.y - insets.getSystemWindowInsetBottom();
330 final int insetsBottom = Math.max(activityViewBottom - keyboardTop, 0);
Mady Mellor5d8f1402019-02-21 18:23:52 -0800331 mActivityView.setForwardedInsets(Insets.of(0, 0, 0, insetsBottom));
332 }
333 }
334
Mady Mellore8e07712019-01-23 12:45:33 -0800335 /**
Mady Mellor99a302602019-06-14 11:39:56 -0700336 * Sets the bubble used to populate this view.
Mady Mellor9801e852019-01-22 14:50:28 -0800337 */
Mady Mellor99a302602019-06-14 11:39:56 -0700338 public void setBubble(Bubble bubble, BubbleStackView stackView, String appName) {
Mark Renoufdb861c92019-07-26 13:58:17 -0400339 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
340 Log.d(TAG, "setBubble: bubble=" + (bubble != null ? bubble.getKey() : "null"));
341 }
342
Mady Mellor9801e852019-01-22 14:50:28 -0800343 mStackView = stackView;
Mady Mellor99a302602019-06-14 11:39:56 -0700344 mBubble = bubble;
Lyn Han6c40fe72019-05-08 14:06:33 -0700345 mAppName = appName;
Mady Mellor9801e852019-01-22 14:50:28 -0800346
Mady Mellor9801e852019-01-22 14:50:28 -0800347 try {
Issei Suzukia91f3962019-06-07 11:48:23 +0200348 ApplicationInfo info = mPm.getApplicationInfo(
Mady Mellor99a302602019-06-14 11:39:56 -0700349 bubble.getPackageName(),
Mady Mellor9801e852019-01-22 14:50:28 -0800350 PackageManager.MATCH_UNINSTALLED_PACKAGES
351 | PackageManager.MATCH_DISABLED_COMPONENTS
352 | PackageManager.MATCH_DIRECT_BOOT_UNAWARE
353 | PackageManager.MATCH_DIRECT_BOOT_AWARE);
Issei Suzukia91f3962019-06-07 11:48:23 +0200354 mAppIcon = mPm.getApplicationIcon(info);
Mady Mellor9801e852019-01-22 14:50:28 -0800355 } catch (PackageManager.NameNotFoundException e) {
Lyn Han6c40fe72019-05-08 14:06:33 -0700356 // Do nothing.
Mady Mellor9801e852019-01-22 14:50:28 -0800357 }
Mady Mellore8e07712019-01-23 12:45:33 -0800358 if (mAppIcon == null) {
359 mAppIcon = mPm.getDefaultActivityIcon();
360 }
Mark Renouf34d04f32019-05-13 15:53:18 -0400361 applyThemeAttrs();
Lyn Han69149122019-04-30 12:03:12 -0700362 showSettingsIcon();
Mady Mellor3dff9e62019-02-05 18:12:53 -0800363 updateExpandedView();
Mady Mellor6d002032019-02-13 13:45:17 -0800364 }
365
366 /**
367 * Lets activity view know it should be shown / populated.
368 */
Mady Mellor5029fa62019-03-05 12:16:21 -0800369 public void populateExpandedView() {
Mark Renoufdb861c92019-07-26 13:58:17 -0400370 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
371 Log.d(TAG, "populateExpandedView: "
372 + "bubble=" + getBubbleKey());
373 }
374
Mady Mellor5029fa62019-03-05 12:16:21 -0800375 if (usingActivityView()) {
376 mActivityView.setCallback(mStateCallback);
377 } else {
Issei Suzukia91f3962019-06-07 11:48:23 +0200378 Log.e(TAG, "Cannot populate expanded view.");
Mady Mellor5029fa62019-03-05 12:16:21 -0800379 }
Mady Mellor9801e852019-01-22 14:50:28 -0800380 }
381
Mady Mellorfe7ec032019-01-30 17:32:49 -0800382 /**
Mady Mellor99a302602019-06-14 11:39:56 -0700383 * Updates the bubble backing this view. This will not re-populate ActivityView, it will
Lyn Han02cca812019-04-02 16:27:32 -0700384 * only update the deep-links in the title, and the height of the view.
Mady Mellorfe7ec032019-01-30 17:32:49 -0800385 */
Mady Mellor99a302602019-06-14 11:39:56 -0700386 public void update(Bubble bubble) {
Mark Renoufdb861c92019-07-26 13:58:17 -0400387 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
388 Log.d(TAG, "update: bubble=" + (bubble != null ? bubble.getKey() : "null"));
389 }
Mady Mellor99a302602019-06-14 11:39:56 -0700390 if (bubble.getKey().equals(mBubble.getKey())) {
391 mBubble = bubble;
Lyn Han02cca812019-04-02 16:27:32 -0700392 updateSettingsContentDescription();
Mady Mellorfe7ec032019-01-30 17:32:49 -0800393 updateHeight();
394 } else {
Mady Mellor99a302602019-06-14 11:39:56 -0700395 Log.w(TAG, "Trying to update entry with different key, new bubble: "
396 + bubble.getKey() + " old bubble: " + bubble.getKey());
Mady Mellorfe7ec032019-01-30 17:32:49 -0800397 }
398 }
399
Mady Mellor3dff9e62019-02-05 18:12:53 -0800400 private void updateExpandedView() {
Mark Renoufdb861c92019-07-26 13:58:17 -0400401 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
402 Log.d(TAG, "updateExpandedView: bubble="
403 + getBubbleKey());
404 }
405
Mady Mellor99a302602019-06-14 11:39:56 -0700406 mBubbleIntent = mBubble.getBubbleIntent(mContext);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800407 if (mBubbleIntent != null) {
Issei Suzukicac2a502019-04-16 16:52:50 +0200408 setContentVisibility(false);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800409 mActivityView.setVisibility(VISIBLE);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800410 }
411 updateView();
412 }
413
Mark Renouf041d7262019-02-06 12:09:41 -0500414 boolean performBackPressIfNeeded() {
Mady Mellor323fb0b2019-03-25 12:15:22 -0700415 if (!usingActivityView()) {
Mark Renouf041d7262019-02-06 12:09:41 -0500416 return false;
417 }
418 mActivityView.performBackPress();
419 return true;
420 }
421
Mady Mellorfe7ec032019-01-30 17:32:49 -0800422 void updateHeight() {
Mark Renoufdb861c92019-07-26 13:58:17 -0400423 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
424 Log.d(TAG, "updateHeight: bubble=" + getBubbleKey());
425 }
Mady Mellorfe7ec032019-01-30 17:32:49 -0800426 if (usingActivityView()) {
Mady Mellor99a302602019-06-14 11:39:56 -0700427 float desiredHeight = Math.max(mBubble.getDesiredHeight(mContext), mMinHeight);
Mady Mellor8fe411d2019-08-16 11:27:53 -0700428 float height = Math.min(desiredHeight, getMaxExpandedHeight());
Mady Mellorfe7ec032019-01-30 17:32:49 -0800429 height = Math.max(height, mMinHeight);
430 LayoutParams lp = (LayoutParams) mActivityView.getLayoutParams();
Mady Mellor5d8f1402019-02-21 18:23:52 -0800431 mNeedsNewHeight = lp.height != height;
432 if (!mKeyboardVisible) {
433 // If the keyboard is visible... don't adjust the height because that will cause
434 // a configuration change and the keyboard will be lost.
Mady Mellor7af771a2019-03-07 15:04:54 -0800435 lp.height = (int) height;
Mady Mellor5d8f1402019-02-21 18:23:52 -0800436 mActivityView.setLayoutParams(lp);
437 mNeedsNewHeight = false;
438 }
Mark Renoufdb861c92019-07-26 13:58:17 -0400439 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
440 Log.d(TAG, "updateHeight: bubble=" + getBubbleKey() + " height=" + height
441 + " mNeedsNewHeight=" + mNeedsNewHeight);
442 }
Mady Mellorfe7ec032019-01-30 17:32:49 -0800443 }
444 }
445
Mady Mellora96c9ed2019-06-07 12:55:26 -0700446 private int getMaxExpandedHeight() {
447 int[] windowLocation = mActivityView.getLocationOnScreen();
Mady Mellor8fe411d2019-08-16 11:27:53 -0700448 int bottomInset = getRootWindowInsets() != null
449 ? getRootWindowInsets().getStableInsetBottom()
450 : 0;
451 return mDisplaySize.y - windowLocation[1] - mSettingsIconHeight - mPointerHeight
452 - 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(() -> {
Mady Mellor99a302602019-06-14 11:39:56 -0700493 mContext.startActivityAsUser(intent, mBubble.getEntry().notification.getUser());
494 logBubbleClickEvent(mBubble,
Steven Wub00225b2019-02-08 14:27:42 -0500495 StatsLog.BUBBLE_UICHANGED__ACTION__HEADER_GO_TO_SETTINGS);
496 });
Mady Mellor9801e852019-01-22 14:50:28 -0800497 }
Mady Mellordea7ecf2018-12-10 15:47:40 -0800498 }
499
Lyn Han02cca812019-04-02 16:27:32 -0700500 private void updateSettingsContentDescription() {
501 mSettingsIcon.setContentDescription(getResources().getString(
502 R.string.bubbles_settings_button_description, mAppName));
503 }
504
Lyn Hanc26ff122019-03-29 16:46:07 -0700505 void showSettingsIcon() {
Lyn Han02cca812019-04-02 16:27:32 -0700506 updateSettingsContentDescription();
Lyn Hanc26ff122019-03-29 16:46:07 -0700507 mSettingsIcon.setVisibility(VISIBLE);
508 }
509
Mady Mellordea7ecf2018-12-10 15:47:40 -0800510 /**
Mady Mellor3dff9e62019-02-05 18:12:53 -0800511 * Update appearance of the expanded view being displayed.
512 */
513 public void updateView() {
Mark Renoufdb861c92019-07-26 13:58:17 -0400514 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
515 Log.d(TAG, "updateView: bubble="
516 + getBubbleKey());
517 }
Mady Mellor3dff9e62019-02-05 18:12:53 -0800518 if (usingActivityView()
519 && mActivityView.getVisibility() == VISIBLE
520 && mActivityView.isAttachedToWindow()) {
521 mActivityView.onLocationChanged();
Mady Mellor3dff9e62019-02-05 18:12:53 -0800522 }
Mady Mellorfe7ec032019-01-30 17:32:49 -0800523 updateHeight();
Mady Mellor3dff9e62019-02-05 18:12:53 -0800524 }
525
526 /**
Mady Mellordea7ecf2018-12-10 15:47:40 -0800527 * Set the x position that the tip of the triangle should point to.
528 */
Mady Mellor3dff9e62019-02-05 18:12:53 -0800529 public void setPointerPosition(float x) {
Lyn Han9a2f5cf2019-05-23 11:01:41 -0700530 float halfPointerWidth = mPointerWidth / 2f;
531 float pointerLeft = x - halfPointerWidth;
532 mPointerView.setTranslationX(pointerLeft);
Lyn Hanf74ba672019-05-20 16:08:48 -0700533 mPointerView.setVisibility(VISIBLE);
Mady Mellordea7ecf2018-12-10 15:47:40 -0800534 }
535
536 /**
Mady Mellor3dff9e62019-02-05 18:12:53 -0800537 * Removes and releases an ActivityView if one was previously created for this bubble.
Mady Mellordea7ecf2018-12-10 15:47:40 -0800538 */
Mady Mellor94d94a72019-03-05 18:16:59 -0800539 public void cleanUpExpandedState() {
Mark Renoufdb861c92019-07-26 13:58:17 -0400540 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
541 Log.d(TAG, "cleanUpExpandedState: mActivityViewStatus=" + mActivityViewStatus
542 + ", bubble=" + getBubbleKey());
543 }
Mady Mellor3dff9e62019-02-05 18:12:53 -0800544 if (mActivityView == null) {
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500545 return;
546 }
Issei Suzuki734bc942019-06-05 13:59:52 +0200547 switch (mActivityViewStatus) {
548 case INITIALIZED:
549 case ACTIVITY_STARTED:
550 mActivityView.release();
Mady Mellordea7ecf2018-12-10 15:47:40 -0800551 }
Mark Renouf5c732b32019-06-12 15:14:54 -0400552 if (mTaskId != -1) {
553 try {
554 ActivityTaskManager.getService().removeTask(mTaskId);
555 } catch (RemoteException e) {
556 Log.w(TAG, "Failed to remove taskId " + mTaskId);
557 }
558 mTaskId = -1;
559 }
Mark Renouf28c250d2019-02-25 16:47:34 -0500560 removeView(mActivityView);
Mark Renouf5c732b32019-06-12 15:14:54 -0400561
Mady Mellor3dff9e62019-02-05 18:12:53 -0800562 mActivityView = null;
Issei Suzuki734bc942019-06-05 13:59:52 +0200563 }
564
565 /**
566 * Called when the last task is removed from a {@link android.hardware.display.VirtualDisplay}
567 * which {@link ActivityView} uses.
568 */
569 void notifyDisplayEmpty() {
Mark Renoufdb861c92019-07-26 13:58:17 -0400570 if (DEBUG_BUBBLE_EXPANDED_VIEW) {
571 Log.d(TAG, "notifyDisplayEmpty: bubble="
572 + getBubbleKey()
573 + " mActivityViewStatus=" + mActivityViewStatus);
574 }
Issei Suzuki734bc942019-06-05 13:59:52 +0200575 if (mActivityViewStatus == ActivityViewStatus.ACTIVITY_STARTED) {
576 mActivityViewStatus = ActivityViewStatus.INITIALIZED;
577 }
Mady Mellordea7ecf2018-12-10 15:47:40 -0800578 }
579
Mady Mellor3dff9e62019-02-05 18:12:53 -0800580 private boolean usingActivityView() {
Mady Mellor323fb0b2019-03-25 12:15:22 -0700581 return mBubbleIntent != null && mActivityView != null;
Mady Mellor3dff9e62019-02-05 18:12:53 -0800582 }
583
Mady Mellor390bff42019-04-05 15:09:01 -0700584 /**
585 * @return the display id of the virtual display.
586 */
587 public int getVirtualDisplayId() {
588 if (usingActivityView()) {
589 return mActivityView.getVirtualDisplayId();
590 }
591 return INVALID_DISPLAY;
592 }
593
Mady Mellor3dff9e62019-02-05 18:12:53 -0800594 /**
Steven Wub00225b2019-02-08 14:27:42 -0500595 * Logs bubble UI click event.
596 *
Mady Mellor99a302602019-06-14 11:39:56 -0700597 * @param bubble the bubble notification entry that user is interacting with.
Steven Wub00225b2019-02-08 14:27:42 -0500598 * @param action the user interaction enum.
599 */
Mady Mellor99a302602019-06-14 11:39:56 -0700600 private void logBubbleClickEvent(Bubble bubble, int action) {
601 StatusBarNotification notification = bubble.getEntry().notification;
Steven Wub00225b2019-02-08 14:27:42 -0500602 StatsLog.write(StatsLog.BUBBLE_UI_CHANGED,
603 notification.getPackageName(),
604 notification.getNotification().getChannelId(),
605 notification.getId(),
606 mStackView.getBubbleIndex(mStackView.getExpandedBubble()),
607 mStackView.getBubbleCount(),
608 action,
609 mStackView.getNormalizedXPosition(),
Steven Wu45e38ae2019-03-25 16:16:59 -0400610 mStackView.getNormalizedYPosition(),
Mady Mellor99a302602019-06-14 11:39:56 -0700611 bubble.showInShadeWhenBubble(),
612 bubble.isOngoing(),
Mady Mellor6cec3502019-06-17 19:20:49 -0700613 false /* isAppForeground (unused) */);
Steven Wub00225b2019-02-08 14:27:42 -0500614 }
Mady Mellordea7ecf2018-12-10 15:47:40 -0800615}