blob: 74db2a929f6118747c5cea3cfd19b562d7c3fad4 [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
Issei Suzukia8d07312019-06-07 12:56:19 +020021import static com.android.systemui.bubbles.BubbleDebugConfig.TAG_BUBBLES;
22import static com.android.systemui.bubbles.BubbleDebugConfig.TAG_WITH_CLASS_NAME;
Mady Mellorca0c24c2019-05-16 16:14:32 -070023
Mady Mellor60101c92019-04-11 19:04:00 -070024import android.app.ActivityOptions;
Mark Renouf5c732b32019-06-12 15:14:54 -040025import android.app.ActivityTaskManager;
Mady Mellor3dff9e62019-02-05 18:12:53 -080026import android.app.ActivityView;
Mady Mellor9801e852019-01-22 14:50:28 -080027import android.app.PendingIntent;
Mark Renouf5c732b32019-06-12 15:14:54 -040028import android.content.ComponentName;
Mady Mellordea7ecf2018-12-10 15:47:40 -080029import android.content.Context;
Mady Mellor9801e852019-01-22 14:50:28 -080030import android.content.Intent;
31import android.content.pm.ApplicationInfo;
32import android.content.pm.PackageManager;
Mady Mellordea7ecf2018-12-10 15:47:40 -080033import android.content.res.Resources;
Mady Mellordd497052019-01-30 17:23:48 -080034import android.content.res.TypedArray;
Mady Mellordea7ecf2018-12-10 15:47:40 -080035import android.graphics.Color;
Mady Mellor3dff9e62019-02-05 18:12:53 -080036import android.graphics.Insets;
37import android.graphics.Point;
Mady Mellore8e07712019-01-23 12:45:33 -080038import android.graphics.drawable.Drawable;
Mady Mellordea7ecf2018-12-10 15:47:40 -080039import android.graphics.drawable.ShapeDrawable;
Mark Renouf5c732b32019-06-12 15:14:54 -040040import android.os.RemoteException;
Steven Wub00225b2019-02-08 14:27:42 -050041import android.service.notification.StatusBarNotification;
Mady Mellordea7ecf2018-12-10 15:47:40 -080042import android.util.AttributeSet;
Mady Mellor9801e852019-01-22 14:50:28 -080043import android.util.Log;
Steven Wub00225b2019-02-08 14:27:42 -050044import android.util.StatsLog;
Mady Mellordea7ecf2018-12-10 15:47:40 -080045import android.view.View;
Mady Mellor3dff9e62019-02-05 18:12:53 -080046import android.view.WindowInsets;
Mady Mellora96c9ed2019-06-07 12:55:26 -070047import android.view.WindowManager;
Mady Mellordea7ecf2018-12-10 15:47:40 -080048import android.widget.LinearLayout;
49
Mark Renouf34d04f32019-05-13 15:53:18 -040050import com.android.internal.policy.ScreenDecorationsUtils;
Mady Mellor3dff9e62019-02-05 18:12:53 -080051import com.android.systemui.Dependency;
Mady Mellordea7ecf2018-12-10 15:47:40 -080052import com.android.systemui.R;
53import com.android.systemui.recents.TriangleShape;
Lyn Han754e77b2019-04-30 14:34:49 -070054import com.android.systemui.statusbar.AlphaOptimizedButton;
Mady Mellordea7ecf2018-12-10 15:47:40 -080055
56/**
Lyn Han02cca812019-04-02 16:27:32 -070057 * Container for the expanded bubble view, handles rendering the caret and settings icon.
Mady Mellordea7ecf2018-12-10 15:47:40 -080058 */
Mady Mellor3d82e682019-02-05 13:34:48 -080059public class BubbleExpandedView extends LinearLayout implements View.OnClickListener {
Issei Suzukia8d07312019-06-07 12:56:19 +020060 private static final String TAG = TAG_WITH_CLASS_NAME ? "BubbleExpandedView" : TAG_BUBBLES;
Mady Mellordea7ecf2018-12-10 15:47:40 -080061
Issei Suzuki734bc942019-06-05 13:59:52 +020062 private enum ActivityViewStatus {
63 // ActivityView is being initialized, cannot start an activity yet.
64 INITIALIZING,
65 // ActivityView is initialized, and ready to start an activity.
66 INITIALIZED,
67 // Activity runs in the ActivityView.
68 ACTIVITY_STARTED,
69 // ActivityView is released, so activity launching will no longer be permitted.
70 RELEASED,
71 }
72
Mady Mellordea7ecf2018-12-10 15:47:40 -080073 // The triangle pointing to the expanded view
74 private View mPointerView;
Mady Mellor44ee2fe2019-01-30 17:51:16 -080075 private int mPointerMargin;
Mady Mellore8e07712019-01-23 12:45:33 -080076
Lyn Han754e77b2019-04-30 14:34:49 -070077 private AlphaOptimizedButton mSettingsIcon;
Mady Mellore8e07712019-01-23 12:45:33 -080078
Mady Mellor3dff9e62019-02-05 18:12:53 -080079 // Views for expanded state
Mady Mellor3dff9e62019-02-05 18:12:53 -080080 private ActivityView mActivityView;
81
Issei Suzuki734bc942019-06-05 13:59:52 +020082 private ActivityViewStatus mActivityViewStatus = ActivityViewStatus.INITIALIZING;
Mark Renouf5c732b32019-06-12 15:14:54 -040083 private int mTaskId = -1;
84
Mady Mellor3dff9e62019-02-05 18:12:53 -080085 private PendingIntent mBubbleIntent;
86
Mady Mellor5d8f1402019-02-21 18:23:52 -080087 private boolean mKeyboardVisible;
88 private boolean mNeedsNewHeight;
89
Mady Mellora96c9ed2019-06-07 12:55:26 -070090 private Point mDisplaySize;
Mady Mellorfe7ec032019-01-30 17:32:49 -080091 private int mMinHeight;
Lyn Han02cca812019-04-02 16:27:32 -070092 private int mSettingsIconHeight;
Lyn Han02cca812019-04-02 16:27:32 -070093 private int mPointerWidth;
94 private int mPointerHeight;
Mark Renouf34d04f32019-05-13 15:53:18 -040095 private ShapeDrawable mPointerDrawable;
Mady Mellordea7ecf2018-12-10 15:47:40 -080096
Mady Mellor99a302602019-06-14 11:39:56 -070097 private Bubble mBubble;
Mady Mellor9801e852019-01-22 14:50:28 -080098 private PackageManager mPm;
99 private String mAppName;
Mady Mellore8e07712019-01-23 12:45:33 -0800100 private Drawable mAppIcon;
101
Mady Mellor3dff9e62019-02-05 18:12:53 -0800102 private BubbleController mBubbleController = Dependency.get(BubbleController.class);
Mady Mellor9801e852019-01-22 14:50:28 -0800103
Mady Mellor9801e852019-01-22 14:50:28 -0800104 private BubbleStackView mStackView;
105
Mady Mellor3dff9e62019-02-05 18:12:53 -0800106 private ActivityView.StateCallback mStateCallback = new ActivityView.StateCallback() {
107 @Override
108 public void onActivityViewReady(ActivityView view) {
Issei Suzuki734bc942019-06-05 13:59:52 +0200109 switch (mActivityViewStatus) {
110 case INITIALIZING:
111 case INITIALIZED:
112 // Custom options so there is no activity transition animation
113 ActivityOptions options = ActivityOptions.makeCustomAnimation(getContext(),
114 0 /* enterResId */, 0 /* exitResId */);
115 // Post to keep the lifecycle normal
116 post(() -> mActivityView.startActivity(mBubbleIntent, options));
117 mActivityViewStatus = ActivityViewStatus.ACTIVITY_STARTED;
Mady Mellor6d002032019-02-13 13:45:17 -0800118 }
Mady Mellor3dff9e62019-02-05 18:12:53 -0800119 }
120
121 @Override
122 public void onActivityViewDestroyed(ActivityView view) {
Issei Suzuki734bc942019-06-05 13:59:52 +0200123 mActivityViewStatus = ActivityViewStatus.RELEASED;
Mady Mellor3dff9e62019-02-05 18:12:53 -0800124 }
125
Mark Renouf5c732b32019-06-12 15:14:54 -0400126 @Override
127 public void onTaskCreated(int taskId, ComponentName componentName) {
128 // Since Bubble ActivityView applies singleTaskDisplay this is
129 // guaranteed to only be called once per ActivityView. The taskId is
130 // saved to use for removeTask, preventing appearance in recent tasks.
131 mTaskId = taskId;
132 }
133
Mady Mellor3dff9e62019-02-05 18:12:53 -0800134 /**
135 * This is only called for tasks on this ActivityView, which is also set to
136 * single-task mode -- meaning never more than one task on this display. If a task
137 * is being removed, it's the top Activity finishing and this bubble should
138 * be removed or collapsed.
139 */
140 @Override
141 public void onTaskRemovalStarted(int taskId) {
Mady Mellor99a302602019-06-14 11:39:56 -0700142 if (mBubble != null) {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800143 // Must post because this is called from a binder thread.
Mady Mellor99a302602019-06-14 11:39:56 -0700144 post(() -> mBubbleController.removeBubble(mBubble.getKey(),
Mark Renouf08bc42a2019-03-07 13:01:59 -0500145 BubbleController.DISMISS_TASK_FINISHED));
Mady Mellor3dff9e62019-02-05 18:12:53 -0800146 }
147 }
148 };
Mady Mellore8e07712019-01-23 12:45:33 -0800149
Mady Mellor3d82e682019-02-05 13:34:48 -0800150 public BubbleExpandedView(Context context) {
Mady Mellordea7ecf2018-12-10 15:47:40 -0800151 this(context, null);
152 }
153
Mady Mellor3d82e682019-02-05 13:34:48 -0800154 public BubbleExpandedView(Context context, AttributeSet attrs) {
Mady Mellordea7ecf2018-12-10 15:47:40 -0800155 this(context, attrs, 0);
156 }
157
Mady Mellor3d82e682019-02-05 13:34:48 -0800158 public BubbleExpandedView(Context context, AttributeSet attrs, int defStyleAttr) {
Mady Mellordea7ecf2018-12-10 15:47:40 -0800159 this(context, attrs, defStyleAttr, 0);
160 }
161
Mady Mellor3d82e682019-02-05 13:34:48 -0800162 public BubbleExpandedView(Context context, AttributeSet attrs, int defStyleAttr,
Mady Mellordea7ecf2018-12-10 15:47:40 -0800163 int defStyleRes) {
164 super(context, attrs, defStyleAttr, defStyleRes);
Mady Mellor9801e852019-01-22 14:50:28 -0800165 mPm = context.getPackageManager();
Mady Mellora96c9ed2019-06-07 12:55:26 -0700166 mDisplaySize = new Point();
167 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
168 wm.getDefaultDisplay().getSize(mDisplaySize);
Mady Mellorfe7ec032019-01-30 17:32:49 -0800169 mMinHeight = getResources().getDimensionPixelSize(
Mady Mellor3dff9e62019-02-05 18:12:53 -0800170 R.dimen.bubble_expanded_default_height);
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800171 mPointerMargin = getResources().getDimensionPixelSize(R.dimen.bubble_pointer_margin);
Mady Mellordea7ecf2018-12-10 15:47:40 -0800172 }
173
174 @Override
175 protected void onFinishInflate() {
176 super.onFinishInflate();
177
178 Resources res = getResources();
179 mPointerView = findViewById(R.id.pointer_view);
Lyn Han02cca812019-04-02 16:27:32 -0700180 mPointerWidth = res.getDimensionPixelSize(R.dimen.bubble_pointer_width);
181 mPointerHeight = res.getDimensionPixelSize(R.dimen.bubble_pointer_height);
Mady Mellordd497052019-01-30 17:23:48 -0800182
Mady Mellordd497052019-01-30 17:23:48 -0800183
Mark Renouf34d04f32019-05-13 15:53:18 -0400184 mPointerDrawable = new ShapeDrawable(TriangleShape.create(
Lyn Han5aa27e22019-05-15 10:55:07 -0700185 mPointerWidth, mPointerHeight, true /* pointUp */));
Mark Renouf34d04f32019-05-13 15:53:18 -0400186 mPointerView.setBackground(mPointerDrawable);
Mady Mellora96c9ed2019-06-07 12:55:26 -0700187 mPointerView.setVisibility(INVISIBLE);
Mady Mellor9801e852019-01-22 14:50:28 -0800188
Lyn Han02cca812019-04-02 16:27:32 -0700189 mSettingsIconHeight = getContext().getResources().getDimensionPixelSize(
Mady Mellorfe7ec032019-01-30 17:32:49 -0800190 R.dimen.bubble_expanded_header_height);
Lyn Han02cca812019-04-02 16:27:32 -0700191 mSettingsIcon = findViewById(R.id.settings_button);
Lyn Han02cca812019-04-02 16:27:32 -0700192 mSettingsIcon.setOnClickListener(this);
Lyn Han02cca812019-04-02 16:27:32 -0700193
Mady Mellor3dff9e62019-02-05 18:12:53 -0800194 mActivityView = new ActivityView(mContext, null /* attrs */, 0 /* defStyle */,
195 true /* singleTaskInstance */);
Issei Suzukicac2a502019-04-16 16:52:50 +0200196
197 setContentVisibility(false);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800198 addView(mActivityView);
199
Lyn Han5aa27e22019-05-15 10:55:07 -0700200 // Expanded stack layout, top to bottom:
201 // Expanded view container
202 // ==> bubble row
203 // ==> expanded view
204 // ==> activity view
205 // ==> manage button
206 bringChildToFront(mActivityView);
207 bringChildToFront(mSettingsIcon);
Mady Mellor52b1ac62019-04-10 16:59:03 -0700208
Mark Renouf34d04f32019-05-13 15:53:18 -0400209 applyThemeAttrs();
210
Mady Mellor5d8f1402019-02-21 18:23:52 -0800211 setOnApplyWindowInsetsListener((View view, WindowInsets insets) -> {
212 // Keep track of IME displaying because we should not make any adjustments that might
213 // cause a config change while the IME is displayed otherwise it'll loose focus.
Mady Mellor3dff9e62019-02-05 18:12:53 -0800214 final int keyboardHeight = insets.getSystemWindowInsetBottom()
215 - insets.getStableInsetBottom();
Mady Mellor5d8f1402019-02-21 18:23:52 -0800216 mKeyboardVisible = keyboardHeight != 0;
217 if (!mKeyboardVisible && mNeedsNewHeight) {
218 updateHeight();
219 }
Mady Mellor3dff9e62019-02-05 18:12:53 -0800220 return view.onApplyWindowInsets(insets);
221 });
Mady Mellore8e07712019-01-23 12:45:33 -0800222 }
223
Mark Renouf34d04f32019-05-13 15:53:18 -0400224 void applyThemeAttrs() {
225 TypedArray ta = getContext().obtainStyledAttributes(R.styleable.BubbleExpandedView);
226 int bgColor = ta.getColor(
227 R.styleable.BubbleExpandedView_android_colorBackgroundFloating, Color.WHITE);
228 float cornerRadius = ta.getDimension(
229 R.styleable.BubbleExpandedView_android_dialogCornerRadius, 0);
230 ta.recycle();
231
232 // Update triangle color.
233 mPointerDrawable.setTint(bgColor);
234
235 // Update ActivityView cornerRadius
236 if (ScreenDecorationsUtils.supportsRoundedCornersOnWindows(mContext.getResources())) {
237 mActivityView.setCornerRadius(cornerRadius);
238 }
239 }
240
Mady Mellor5d8f1402019-02-21 18:23:52 -0800241 @Override
242 protected void onDetachedFromWindow() {
243 super.onDetachedFromWindow();
244 mKeyboardVisible = false;
245 mNeedsNewHeight = false;
246 if (mActivityView != null) {
247 mActivityView.setForwardedInsets(Insets.of(0, 0, 0, 0));
248 }
249 }
250
251 /**
Issei Suzukicac2a502019-04-16 16:52:50 +0200252 * Set visibility of contents in the expanded state.
253 *
254 * @param visibility {@code true} if the contents should be visible on the screen.
255 *
256 * Note that this contents visibility doesn't affect visibility at {@link android.view.View},
257 * and setting {@code false} actually means rendering the contents in transparent.
258 */
259 void setContentVisibility(boolean visibility) {
260 final float alpha = visibility ? 1f : 0f;
261 mPointerView.setAlpha(alpha);
262 if (mActivityView != null) {
263 mActivityView.setAlpha(alpha);
264 }
265 }
266
267 /**
Mady Mellor5d8f1402019-02-21 18:23:52 -0800268 * Called by {@link BubbleStackView} when the insets for the expanded state should be updated.
269 * This should be done post-move and post-animation.
270 */
271 void updateInsets(WindowInsets insets) {
272 if (usingActivityView()) {
Mady Mellor5d8f1402019-02-21 18:23:52 -0800273 int[] windowLocation = mActivityView.getLocationOnScreen();
274 final int windowBottom = windowLocation[1] + mActivityView.getHeight();
275 final int keyboardHeight = insets.getSystemWindowInsetBottom()
276 - insets.getStableInsetBottom();
277 final int insetsBottom = Math.max(0,
Mady Mellora96c9ed2019-06-07 12:55:26 -0700278 windowBottom + keyboardHeight - mDisplaySize.y);
Mady Mellor5d8f1402019-02-21 18:23:52 -0800279 mActivityView.setForwardedInsets(Insets.of(0, 0, 0, insetsBottom));
280 }
281 }
282
Mady Mellore8e07712019-01-23 12:45:33 -0800283 /**
Mady Mellor99a302602019-06-14 11:39:56 -0700284 * Sets the bubble used to populate this view.
Mady Mellor9801e852019-01-22 14:50:28 -0800285 */
Mady Mellor99a302602019-06-14 11:39:56 -0700286 public void setBubble(Bubble bubble, BubbleStackView stackView, String appName) {
Mady Mellor9801e852019-01-22 14:50:28 -0800287 mStackView = stackView;
Mady Mellor99a302602019-06-14 11:39:56 -0700288 mBubble = bubble;
Lyn Han6c40fe72019-05-08 14:06:33 -0700289 mAppName = appName;
Mady Mellor9801e852019-01-22 14:50:28 -0800290
Mady Mellor9801e852019-01-22 14:50:28 -0800291 try {
Issei Suzukia91f3962019-06-07 11:48:23 +0200292 ApplicationInfo info = mPm.getApplicationInfo(
Mady Mellor99a302602019-06-14 11:39:56 -0700293 bubble.getPackageName(),
Mady Mellor9801e852019-01-22 14:50:28 -0800294 PackageManager.MATCH_UNINSTALLED_PACKAGES
295 | PackageManager.MATCH_DISABLED_COMPONENTS
296 | PackageManager.MATCH_DIRECT_BOOT_UNAWARE
297 | PackageManager.MATCH_DIRECT_BOOT_AWARE);
Issei Suzukia91f3962019-06-07 11:48:23 +0200298 mAppIcon = mPm.getApplicationIcon(info);
Mady Mellor9801e852019-01-22 14:50:28 -0800299 } catch (PackageManager.NameNotFoundException e) {
Lyn Han6c40fe72019-05-08 14:06:33 -0700300 // Do nothing.
Mady Mellor9801e852019-01-22 14:50:28 -0800301 }
Mady Mellore8e07712019-01-23 12:45:33 -0800302 if (mAppIcon == null) {
303 mAppIcon = mPm.getDefaultActivityIcon();
304 }
Mark Renouf34d04f32019-05-13 15:53:18 -0400305 applyThemeAttrs();
Lyn Han69149122019-04-30 12:03:12 -0700306 showSettingsIcon();
Mady Mellor3dff9e62019-02-05 18:12:53 -0800307 updateExpandedView();
Mady Mellor6d002032019-02-13 13:45:17 -0800308 }
309
310 /**
311 * Lets activity view know it should be shown / populated.
312 */
Mady Mellor5029fa62019-03-05 12:16:21 -0800313 public void populateExpandedView() {
314 if (usingActivityView()) {
315 mActivityView.setCallback(mStateCallback);
316 } else {
Issei Suzukia91f3962019-06-07 11:48:23 +0200317 Log.e(TAG, "Cannot populate expanded view.");
Mady Mellor5029fa62019-03-05 12:16:21 -0800318 }
Mady Mellor9801e852019-01-22 14:50:28 -0800319 }
320
Mady Mellorfe7ec032019-01-30 17:32:49 -0800321 /**
Mady Mellor99a302602019-06-14 11:39:56 -0700322 * Updates the bubble backing this view. This will not re-populate ActivityView, it will
Lyn Han02cca812019-04-02 16:27:32 -0700323 * only update the deep-links in the title, and the height of the view.
Mady Mellorfe7ec032019-01-30 17:32:49 -0800324 */
Mady Mellor99a302602019-06-14 11:39:56 -0700325 public void update(Bubble bubble) {
326 if (bubble.getKey().equals(mBubble.getKey())) {
327 mBubble = bubble;
Lyn Han02cca812019-04-02 16:27:32 -0700328 updateSettingsContentDescription();
Mady Mellorfe7ec032019-01-30 17:32:49 -0800329 updateHeight();
330 } else {
Mady Mellor99a302602019-06-14 11:39:56 -0700331 Log.w(TAG, "Trying to update entry with different key, new bubble: "
332 + bubble.getKey() + " old bubble: " + bubble.getKey());
Mady Mellorfe7ec032019-01-30 17:32:49 -0800333 }
334 }
335
Mady Mellor3dff9e62019-02-05 18:12:53 -0800336 private void updateExpandedView() {
Mady Mellor99a302602019-06-14 11:39:56 -0700337 mBubbleIntent = mBubble.getBubbleIntent(mContext);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800338 if (mBubbleIntent != null) {
Issei Suzukicac2a502019-04-16 16:52:50 +0200339 setContentVisibility(false);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800340 mActivityView.setVisibility(VISIBLE);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800341 }
342 updateView();
343 }
344
Mark Renouf041d7262019-02-06 12:09:41 -0500345 boolean performBackPressIfNeeded() {
Mady Mellor323fb0b2019-03-25 12:15:22 -0700346 if (!usingActivityView()) {
Mark Renouf041d7262019-02-06 12:09:41 -0500347 return false;
348 }
349 mActivityView.performBackPress();
350 return true;
351 }
352
Mady Mellorfe7ec032019-01-30 17:32:49 -0800353 void updateHeight() {
354 if (usingActivityView()) {
Mady Mellor99a302602019-06-14 11:39:56 -0700355 int max = getMaxExpandedHeight() - mSettingsIconHeight - mPointerHeight
356 - mPointerMargin;
357 float desiredHeight = Math.max(mBubble.getDesiredHeight(mContext), mMinHeight);
358 float height = Math.min(desiredHeight, max);
Mady Mellorfe7ec032019-01-30 17:32:49 -0800359 height = Math.max(height, mMinHeight);
360 LayoutParams lp = (LayoutParams) mActivityView.getLayoutParams();
Mady Mellor5d8f1402019-02-21 18:23:52 -0800361 mNeedsNewHeight = lp.height != height;
362 if (!mKeyboardVisible) {
363 // If the keyboard is visible... don't adjust the height because that will cause
364 // a configuration change and the keyboard will be lost.
Mady Mellor7af771a2019-03-07 15:04:54 -0800365 lp.height = (int) height;
Mady Mellor5d8f1402019-02-21 18:23:52 -0800366 mActivityView.setLayoutParams(lp);
367 mNeedsNewHeight = false;
368 }
Mady Mellorfe7ec032019-01-30 17:32:49 -0800369 }
370 }
371
Mady Mellora96c9ed2019-06-07 12:55:26 -0700372 private int getMaxExpandedHeight() {
373 int[] windowLocation = mActivityView.getLocationOnScreen();
374 return mDisplaySize.y - windowLocation[1] - mSettingsIconHeight;
375 }
376
Mady Mellor9801e852019-01-22 14:50:28 -0800377 @Override
378 public void onClick(View view) {
Mady Mellor99a302602019-06-14 11:39:56 -0700379 if (mBubble == null) {
Mady Mellor9801e852019-01-22 14:50:28 -0800380 return;
381 }
Mady Mellor9801e852019-01-22 14:50:28 -0800382 int id = view.getId();
Lyn Hanc26ff122019-03-29 16:46:07 -0700383 if (id == R.id.settings_button) {
Mady Mellor99a302602019-06-14 11:39:56 -0700384 Intent intent = mBubble.getSettingsIntent();
Steven Wub00225b2019-02-08 14:27:42 -0500385 mStackView.collapseStack(() -> {
Mady Mellor99a302602019-06-14 11:39:56 -0700386 mContext.startActivityAsUser(intent, mBubble.getEntry().notification.getUser());
387 logBubbleClickEvent(mBubble,
Steven Wub00225b2019-02-08 14:27:42 -0500388 StatsLog.BUBBLE_UICHANGED__ACTION__HEADER_GO_TO_SETTINGS);
389 });
Mady Mellor9801e852019-01-22 14:50:28 -0800390 }
Mady Mellordea7ecf2018-12-10 15:47:40 -0800391 }
392
Lyn Han02cca812019-04-02 16:27:32 -0700393 private void updateSettingsContentDescription() {
394 mSettingsIcon.setContentDescription(getResources().getString(
395 R.string.bubbles_settings_button_description, mAppName));
396 }
397
Lyn Hanc26ff122019-03-29 16:46:07 -0700398 void showSettingsIcon() {
Lyn Han02cca812019-04-02 16:27:32 -0700399 updateSettingsContentDescription();
Lyn Hanc26ff122019-03-29 16:46:07 -0700400 mSettingsIcon.setVisibility(VISIBLE);
401 }
402
Mady Mellordea7ecf2018-12-10 15:47:40 -0800403 /**
Mady Mellor3dff9e62019-02-05 18:12:53 -0800404 * Update appearance of the expanded view being displayed.
405 */
406 public void updateView() {
407 if (usingActivityView()
408 && mActivityView.getVisibility() == VISIBLE
409 && mActivityView.isAttachedToWindow()) {
410 mActivityView.onLocationChanged();
Mady Mellor3dff9e62019-02-05 18:12:53 -0800411 }
Mady Mellorfe7ec032019-01-30 17:32:49 -0800412 updateHeight();
Mady Mellor3dff9e62019-02-05 18:12:53 -0800413 }
414
415 /**
Mady Mellordea7ecf2018-12-10 15:47:40 -0800416 * Set the x position that the tip of the triangle should point to.
417 */
Mady Mellor3dff9e62019-02-05 18:12:53 -0800418 public void setPointerPosition(float x) {
Lyn Han9a2f5cf2019-05-23 11:01:41 -0700419 float halfPointerWidth = mPointerWidth / 2f;
420 float pointerLeft = x - halfPointerWidth;
421 mPointerView.setTranslationX(pointerLeft);
Lyn Hanf74ba672019-05-20 16:08:48 -0700422 mPointerView.setVisibility(VISIBLE);
Mady Mellordea7ecf2018-12-10 15:47:40 -0800423 }
424
425 /**
Mady Mellor3dff9e62019-02-05 18:12:53 -0800426 * Removes and releases an ActivityView if one was previously created for this bubble.
Mady Mellordea7ecf2018-12-10 15:47:40 -0800427 */
Mady Mellor94d94a72019-03-05 18:16:59 -0800428 public void cleanUpExpandedState() {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800429 if (mActivityView == null) {
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500430 return;
431 }
Issei Suzuki734bc942019-06-05 13:59:52 +0200432 switch (mActivityViewStatus) {
433 case INITIALIZED:
434 case ACTIVITY_STARTED:
435 mActivityView.release();
Mady Mellordea7ecf2018-12-10 15:47:40 -0800436 }
Mark Renouf5c732b32019-06-12 15:14:54 -0400437 if (mTaskId != -1) {
438 try {
439 ActivityTaskManager.getService().removeTask(mTaskId);
440 } catch (RemoteException e) {
441 Log.w(TAG, "Failed to remove taskId " + mTaskId);
442 }
443 mTaskId = -1;
444 }
Mark Renouf28c250d2019-02-25 16:47:34 -0500445 removeView(mActivityView);
Mark Renouf5c732b32019-06-12 15:14:54 -0400446
Mady Mellor3dff9e62019-02-05 18:12:53 -0800447 mActivityView = null;
Issei Suzuki734bc942019-06-05 13:59:52 +0200448 }
449
450 /**
451 * Called when the last task is removed from a {@link android.hardware.display.VirtualDisplay}
452 * which {@link ActivityView} uses.
453 */
454 void notifyDisplayEmpty() {
455 if (mActivityViewStatus == ActivityViewStatus.ACTIVITY_STARTED) {
456 mActivityViewStatus = ActivityViewStatus.INITIALIZED;
457 }
Mady Mellordea7ecf2018-12-10 15:47:40 -0800458 }
459
Mady Mellor3dff9e62019-02-05 18:12:53 -0800460 private boolean usingActivityView() {
Mady Mellor323fb0b2019-03-25 12:15:22 -0700461 return mBubbleIntent != null && mActivityView != null;
Mady Mellor3dff9e62019-02-05 18:12:53 -0800462 }
463
Mady Mellor390bff42019-04-05 15:09:01 -0700464 /**
465 * @return the display id of the virtual display.
466 */
467 public int getVirtualDisplayId() {
468 if (usingActivityView()) {
469 return mActivityView.getVirtualDisplayId();
470 }
471 return INVALID_DISPLAY;
472 }
473
Mady Mellor3dff9e62019-02-05 18:12:53 -0800474 /**
Steven Wub00225b2019-02-08 14:27:42 -0500475 * Logs bubble UI click event.
476 *
Mady Mellor99a302602019-06-14 11:39:56 -0700477 * @param bubble the bubble notification entry that user is interacting with.
Steven Wub00225b2019-02-08 14:27:42 -0500478 * @param action the user interaction enum.
479 */
Mady Mellor99a302602019-06-14 11:39:56 -0700480 private void logBubbleClickEvent(Bubble bubble, int action) {
481 StatusBarNotification notification = bubble.getEntry().notification;
Steven Wub00225b2019-02-08 14:27:42 -0500482 StatsLog.write(StatsLog.BUBBLE_UI_CHANGED,
483 notification.getPackageName(),
484 notification.getNotification().getChannelId(),
485 notification.getId(),
486 mStackView.getBubbleIndex(mStackView.getExpandedBubble()),
487 mStackView.getBubbleCount(),
488 action,
489 mStackView.getNormalizedXPosition(),
Steven Wu45e38ae2019-03-25 16:16:59 -0400490 mStackView.getNormalizedYPosition(),
Mady Mellor99a302602019-06-14 11:39:56 -0700491 bubble.showInShadeWhenBubble(),
492 bubble.isOngoing(),
Mady Mellor6cec3502019-06-17 19:20:49 -0700493 false /* isAppForeground (unused) */);
Steven Wub00225b2019-02-08 14:27:42 -0500494 }
Mady Mellordea7ecf2018-12-10 15:47:40 -0800495}