blob: 22e904fa152be5e714c5e34c218b3d4d24c352e2 [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 Mellor3dff9e62019-02-05 18:12:53 -080019import static android.content.pm.ActivityInfo.DOCUMENT_LAUNCH_ALWAYS;
20import static android.util.StatsLogInternal.BUBBLE_DEVELOPER_ERROR_REPORTED__ERROR__ACTIVITY_INFO_MISSING;
21import static android.util.StatsLogInternal.BUBBLE_DEVELOPER_ERROR_REPORTED__ERROR__ACTIVITY_INFO_NOT_RESIZABLE;
22import static android.util.StatsLogInternal.BUBBLE_DEVELOPER_ERROR_REPORTED__ERROR__DOCUMENT_LAUNCH_NOT_ALWAYS;
23
Mady Mellore8e07712019-01-23 12:45:33 -080024import android.animation.LayoutTransition;
25import android.animation.ObjectAnimator;
Mady Mellordea7ecf2018-12-10 15:47:40 -080026import android.annotation.Nullable;
Mady Mellor3dff9e62019-02-05 18:12:53 -080027import android.app.ActivityView;
Mady Mellore8e07712019-01-23 12:45:33 -080028import android.app.INotificationManager;
Mady Mellor9801e852019-01-22 14:50:28 -080029import android.app.Notification;
30import android.app.PendingIntent;
Mady Mellordea7ecf2018-12-10 15:47:40 -080031import android.content.Context;
Mady Mellor9801e852019-01-22 14:50:28 -080032import android.content.Intent;
Mady Mellor3dff9e62019-02-05 18:12:53 -080033import android.content.pm.ActivityInfo;
Mady Mellor9801e852019-01-22 14:50:28 -080034import android.content.pm.ApplicationInfo;
35import android.content.pm.PackageManager;
Mady Mellordea7ecf2018-12-10 15:47:40 -080036import android.content.res.Resources;
Mady Mellordd497052019-01-30 17:23:48 -080037import android.content.res.TypedArray;
Mady Mellordea7ecf2018-12-10 15:47:40 -080038import android.graphics.Color;
Mady Mellor3dff9e62019-02-05 18:12:53 -080039import android.graphics.Insets;
40import android.graphics.Point;
Mady Mellore8e07712019-01-23 12:45:33 -080041import android.graphics.drawable.Drawable;
Mady Mellor310c7c62019-02-21 17:03:08 -080042import android.graphics.drawable.GradientDrawable;
Mady Mellordea7ecf2018-12-10 15:47:40 -080043import android.graphics.drawable.ShapeDrawable;
Mady Mellore8e07712019-01-23 12:45:33 -080044import android.os.RemoteException;
45import android.os.ServiceManager;
Mady Mellor9801e852019-01-22 14:50:28 -080046import android.provider.Settings;
Steven Wub00225b2019-02-08 14:27:42 -050047import android.service.notification.StatusBarNotification;
Mady Mellordea7ecf2018-12-10 15:47:40 -080048import android.util.AttributeSet;
Mady Mellor9801e852019-01-22 14:50:28 -080049import android.util.Log;
Steven Wub00225b2019-02-08 14:27:42 -050050import android.util.StatsLog;
Mady Mellordea7ecf2018-12-10 15:47:40 -080051import android.view.View;
Mady Mellor3dff9e62019-02-05 18:12:53 -080052import android.view.ViewGroup;
53import android.view.WindowInsets;
Mady Mellore8e07712019-01-23 12:45:33 -080054import android.widget.FrameLayout;
Mady Mellor9801e852019-01-22 14:50:28 -080055import android.widget.ImageButton;
Mady Mellore8e07712019-01-23 12:45:33 -080056import android.widget.ImageView;
Mady Mellordea7ecf2018-12-10 15:47:40 -080057import android.widget.LinearLayout;
Mark Renouf89b1a4a2018-12-04 14:59:45 -050058import android.widget.TextView;
Mady Mellordea7ecf2018-12-10 15:47:40 -080059
Mady Mellor3dff9e62019-02-05 18:12:53 -080060import com.android.systemui.Dependency;
Mady Mellore8e07712019-01-23 12:45:33 -080061import com.android.systemui.Interpolators;
Mady Mellordea7ecf2018-12-10 15:47:40 -080062import com.android.systemui.R;
63import com.android.systemui.recents.TriangleShape;
Mady Mellor9801e852019-01-22 14:50:28 -080064import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Mady Mellor3dff9e62019-02-05 18:12:53 -080065import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow;
66import com.android.systemui.statusbar.notification.stack.ExpandableViewState;
Mady Mellordea7ecf2018-12-10 15:47:40 -080067
68/**
69 * Container for the expanded bubble view, handles rendering the caret and header of the view.
70 */
Mady Mellor3d82e682019-02-05 13:34:48 -080071public class BubbleExpandedView extends LinearLayout implements View.OnClickListener {
Mady Mellor9801e852019-01-22 14:50:28 -080072 private static final String TAG = "BubbleExpandedView";
Mady Mellordea7ecf2018-12-10 15:47:40 -080073
Mady Mellor44ee2fe2019-01-30 17:51:16 -080074 // Configurable via bubble settings; just for testing
75 private boolean mUseFooter;
76 private boolean mShowOnTop;
77
Mady Mellordea7ecf2018-12-10 15:47:40 -080078 // The triangle pointing to the expanded view
79 private View mPointerView;
Mady Mellor44ee2fe2019-01-30 17:51:16 -080080 private int mPointerMargin;
Mady Mellore8e07712019-01-23 12:45:33 -080081
82 // Header
83 private View mHeaderView;
Mady Mellor9801e852019-01-22 14:50:28 -080084 private ImageButton mDeepLinkIcon;
Mady Mellor9801e852019-01-22 14:50:28 -080085 private ImageButton mSettingsIcon;
Mady Mellore8e07712019-01-23 12:45:33 -080086
87 // Permission view
88 private View mPermissionView;
89
Mady Mellor3dff9e62019-02-05 18:12:53 -080090 // Views for expanded state
91 private ExpandableNotificationRow mNotifRow;
92 private ActivityView mActivityView;
93
94 private boolean mActivityViewReady = false;
95 private PendingIntent mBubbleIntent;
96
Mady Mellorfe7ec032019-01-30 17:32:49 -080097 private int mMinHeight;
98 private int mHeaderHeight;
Mady Mellor44ee2fe2019-01-30 17:51:16 -080099 private int mBubbleHeight;
100 private int mPermissionHeight;
Mady Mellordea7ecf2018-12-10 15:47:40 -0800101
Mady Mellor9801e852019-01-22 14:50:28 -0800102 private NotificationEntry mEntry;
103 private PackageManager mPm;
104 private String mAppName;
Mady Mellore8e07712019-01-23 12:45:33 -0800105 private Drawable mAppIcon;
106
107 private INotificationManager mNotificationManagerService;
Mady Mellor3dff9e62019-02-05 18:12:53 -0800108 private BubbleController mBubbleController = Dependency.get(BubbleController.class);
Mady Mellor9801e852019-01-22 14:50:28 -0800109
Mady Mellor9801e852019-01-22 14:50:28 -0800110 private BubbleStackView mStackView;
111
Mady Mellor3dff9e62019-02-05 18:12:53 -0800112 private BubbleExpandedView.OnBubbleBlockedListener mOnBubbleBlockedListener;
113
114 private ActivityView.StateCallback mStateCallback = new ActivityView.StateCallback() {
115 @Override
116 public void onActivityViewReady(ActivityView view) {
Mady Mellor6d002032019-02-13 13:45:17 -0800117 if (!mActivityViewReady) {
118 mActivityViewReady = true;
119 mActivityView.startActivity(mBubbleIntent);
120 }
Mady Mellor3dff9e62019-02-05 18:12:53 -0800121 }
122
123 @Override
124 public void onActivityViewDestroyed(ActivityView view) {
125 mActivityViewReady = false;
126 }
127
128 /**
129 * This is only called for tasks on this ActivityView, which is also set to
130 * single-task mode -- meaning never more than one task on this display. If a task
131 * is being removed, it's the top Activity finishing and this bubble should
132 * be removed or collapsed.
133 */
134 @Override
135 public void onTaskRemovalStarted(int taskId) {
136 if (mEntry != null) {
137 // Must post because this is called from a binder thread.
138 post(() -> mBubbleController.removeBubble(mEntry.key));
139 }
140 }
141 };
Mady Mellore8e07712019-01-23 12:45:33 -0800142
Mady Mellor3d82e682019-02-05 13:34:48 -0800143 public BubbleExpandedView(Context context) {
Mady Mellordea7ecf2018-12-10 15:47:40 -0800144 this(context, null);
145 }
146
Mady Mellor3d82e682019-02-05 13:34:48 -0800147 public BubbleExpandedView(Context context, AttributeSet attrs) {
Mady Mellordea7ecf2018-12-10 15:47:40 -0800148 this(context, attrs, 0);
149 }
150
Mady Mellor3d82e682019-02-05 13:34:48 -0800151 public BubbleExpandedView(Context context, AttributeSet attrs, int defStyleAttr) {
Mady Mellordea7ecf2018-12-10 15:47:40 -0800152 this(context, attrs, defStyleAttr, 0);
153 }
154
Mady Mellor3d82e682019-02-05 13:34:48 -0800155 public BubbleExpandedView(Context context, AttributeSet attrs, int defStyleAttr,
Mady Mellordea7ecf2018-12-10 15:47:40 -0800156 int defStyleRes) {
157 super(context, attrs, defStyleAttr, defStyleRes);
Mady Mellor9801e852019-01-22 14:50:28 -0800158 mPm = context.getPackageManager();
Mady Mellorfe7ec032019-01-30 17:32:49 -0800159 mMinHeight = getResources().getDimensionPixelSize(
Mady Mellor3dff9e62019-02-05 18:12:53 -0800160 R.dimen.bubble_expanded_default_height);
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800161 mPointerMargin = getResources().getDimensionPixelSize(R.dimen.bubble_pointer_margin);
Mady Mellore8e07712019-01-23 12:45:33 -0800162 try {
163 mNotificationManagerService = INotificationManager.Stub.asInterface(
164 ServiceManager.getServiceOrThrow(Context.NOTIFICATION_SERVICE));
165 } catch (ServiceManager.ServiceNotFoundException e) {
166 Log.w(TAG, e);
167 }
Mady Mellordea7ecf2018-12-10 15:47:40 -0800168 }
169
170 @Override
171 protected void onFinishInflate() {
172 super.onFinishInflate();
173
174 Resources res = getResources();
175 mPointerView = findViewById(R.id.pointer_view);
176 int width = res.getDimensionPixelSize(R.dimen.bubble_pointer_width);
177 int height = res.getDimensionPixelSize(R.dimen.bubble_pointer_height);
Mady Mellordd497052019-01-30 17:23:48 -0800178
179 TypedArray ta = getContext().obtainStyledAttributes(
180 new int[] {android.R.attr.colorBackgroundFloating});
181 int bgColor = ta.getColor(0, Color.WHITE);
182 ta.recycle();
183
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800184 mShowOnTop = BubbleController.showBubblesAtTop(getContext());
185 mUseFooter = BubbleController.useFooter(getContext());
186
Mady Mellordea7ecf2018-12-10 15:47:40 -0800187 ShapeDrawable triangleDrawable = new ShapeDrawable(
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800188 TriangleShape.create(width, height, mShowOnTop /* pointUp */));
Mady Mellordd497052019-01-30 17:23:48 -0800189 triangleDrawable.setTint(bgColor);
Mady Mellordea7ecf2018-12-10 15:47:40 -0800190 mPointerView.setBackground(triangleDrawable);
Mady Mellor9801e852019-01-22 14:50:28 -0800191
Mady Mellore8e07712019-01-23 12:45:33 -0800192 FrameLayout viewWrapper = findViewById(R.id.header_permission_wrapper);
Mady Mellor310c7c62019-02-21 17:03:08 -0800193 viewWrapper.setBackground(createHeaderPermissionBackground(bgColor));
194
Mady Mellore8e07712019-01-23 12:45:33 -0800195 LayoutTransition transition = new LayoutTransition();
196 transition.setDuration(200);
197
198 ObjectAnimator appearAnimator = ObjectAnimator.ofFloat(null, View.ALPHA, 0f, 1f);
199 transition.setAnimator(LayoutTransition.APPEARING, appearAnimator);
200 transition.setInterpolator(LayoutTransition.APPEARING, Interpolators.ALPHA_IN);
201
202 ObjectAnimator disappearAnimator = ObjectAnimator.ofFloat(null, View.ALPHA, 1f, 0f);
203 transition.setAnimator(LayoutTransition.DISAPPEARING, disappearAnimator);
204 transition.setInterpolator(LayoutTransition.DISAPPEARING, Interpolators.ALPHA_OUT);
205
206 transition.setAnimateParentHierarchy(false);
207 viewWrapper.setLayoutTransition(transition);
208 viewWrapper.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
209
Mady Mellor310c7c62019-02-21 17:03:08 -0800210
Mady Mellorfe7ec032019-01-30 17:32:49 -0800211 mHeaderHeight = getContext().getResources().getDimensionPixelSize(
212 R.dimen.bubble_expanded_header_height);
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800213 mPermissionHeight = getContext().getResources().getDimensionPixelSize(
214 R.dimen.bubble_permission_height);
Mady Mellore8e07712019-01-23 12:45:33 -0800215 mHeaderView = findViewById(R.id.header_layout);
Mady Mellor9801e852019-01-22 14:50:28 -0800216 mDeepLinkIcon = findViewById(R.id.deep_link_button);
217 mSettingsIcon = findViewById(R.id.settings_button);
218 mDeepLinkIcon.setOnClickListener(this);
219 mSettingsIcon.setOnClickListener(this);
Mady Mellore8e07712019-01-23 12:45:33 -0800220
221 mPermissionView = findViewById(R.id.permission_layout);
222 findViewById(R.id.no_bubbles_button).setOnClickListener(this);
223 findViewById(R.id.yes_bubbles_button).setOnClickListener(this);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800224
225 mActivityView = new ActivityView(mContext, null /* attrs */, 0 /* defStyle */,
226 true /* singleTaskInstance */);
227 addView(mActivityView);
228
229 mActivityView.setOnApplyWindowInsetsListener((View view, WindowInsets insets) -> {
230 ActivityView activityView = (ActivityView) view;
231 // Here we assume that the position of the ActivityView on the screen
232 // remains regardless of IME status. When we move ActivityView, the
233 // forwardedInsets should be computed not against the current location
234 // and size, but against the post-moved location and size.
235 Point displaySize = new Point();
236 view.getContext().getDisplay().getSize(displaySize);
237 int[] windowLocation = view.getLocationOnScreen();
238 final int windowBottom = windowLocation[1] + view.getHeight();
239 final int keyboardHeight = insets.getSystemWindowInsetBottom()
240 - insets.getStableInsetBottom();
241 final int insetsBottom = Math.max(0,
242 windowBottom + keyboardHeight - displaySize.y);
243 activityView.setForwardedInsets(Insets.of(0, 0, 0, insetsBottom));
244 return view.onApplyWindowInsets(insets);
245 });
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800246
247 if (!mShowOnTop) {
248 removeView(mPointerView);
249 if (mUseFooter) {
Mady Mellor310c7c62019-02-21 17:03:08 -0800250 View divider = findViewById(R.id.divider);
251 viewWrapper.removeView(divider);
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800252 removeView(viewWrapper);
Mady Mellor310c7c62019-02-21 17:03:08 -0800253 addView(divider);
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800254 addView(viewWrapper);
255 }
256 addView(mPointerView);
257 }
Mady Mellore8e07712019-01-23 12:45:33 -0800258 }
259
260 /**
Mady Mellor310c7c62019-02-21 17:03:08 -0800261 * Creates a background with corners rounded based on how the view is configured to display
262 */
263 private Drawable createHeaderPermissionBackground(int bgColor) {
264 TypedArray ta2 = getContext().obtainStyledAttributes(
265 new int[] {android.R.attr.dialogCornerRadius});
266 final float cr = ta2.getDimension(0, 0f);
267 ta2.recycle();
268
269 float[] radii = mUseFooter
270 ? new float[] {0, 0, 0, 0, cr, cr, cr, cr}
271 : new float[] {cr, cr, cr, cr, 0, 0, 0, 0};
272 GradientDrawable chromeBackground = new GradientDrawable();
273 chromeBackground.setShape(GradientDrawable.RECTANGLE);
274 chromeBackground.setCornerRadii(radii);
275 chromeBackground.setColor(bgColor);
276 return chromeBackground;
277 }
278
279 /**
Mady Mellore8e07712019-01-23 12:45:33 -0800280 * Sets the listener to notify when a bubble has been blocked.
281 */
282 public void setOnBlockedListener(OnBubbleBlockedListener listener) {
283 mOnBubbleBlockedListener = listener;
Mady Mellor9801e852019-01-22 14:50:28 -0800284 }
285
286 /**
287 * Sets the notification entry used to populate this view.
288 */
289 public void setEntry(NotificationEntry entry, BubbleStackView stackView) {
290 mStackView = stackView;
291 mEntry = entry;
292
293 ApplicationInfo info;
294 try {
295 info = mPm.getApplicationInfo(
296 entry.notification.getPackageName(),
297 PackageManager.MATCH_UNINSTALLED_PACKAGES
298 | PackageManager.MATCH_DISABLED_COMPONENTS
299 | PackageManager.MATCH_DIRECT_BOOT_UNAWARE
300 | PackageManager.MATCH_DIRECT_BOOT_AWARE);
301 if (info != null) {
302 mAppName = String.valueOf(mPm.getApplicationLabel(info));
Mady Mellore8e07712019-01-23 12:45:33 -0800303 mAppIcon = mPm.getApplicationIcon(info);
Mady Mellor9801e852019-01-22 14:50:28 -0800304 }
305 } catch (PackageManager.NameNotFoundException e) {
306 // Ahh... just use package name
307 mAppName = entry.notification.getPackageName();
308 }
Mady Mellore8e07712019-01-23 12:45:33 -0800309 if (mAppIcon == null) {
310 mAppIcon = mPm.getDefaultActivityIcon();
311 }
Mady Mellor9801e852019-01-22 14:50:28 -0800312 updateHeaderView();
Mady Mellore8e07712019-01-23 12:45:33 -0800313 updatePermissionView();
Mady Mellor3dff9e62019-02-05 18:12:53 -0800314 updateExpandedView();
Mady Mellor6d002032019-02-13 13:45:17 -0800315 }
316
317 /**
318 * Lets activity view know it should be shown / populated.
319 */
320 public void populateActivityView() {
Mady Mellor3dff9e62019-02-05 18:12:53 -0800321 mActivityView.setCallback(mStateCallback);
Mady Mellor9801e852019-01-22 14:50:28 -0800322 }
323
Mady Mellorfe7ec032019-01-30 17:32:49 -0800324 /**
325 * Updates the entry backing this view. This will not re-populate ActivityView, it will
326 * only update the deep-links in the header, the title, and the height of the view.
327 */
328 public void update(NotificationEntry entry) {
329 if (entry.key.equals(mEntry.key)) {
330 mEntry = entry;
331 updateHeaderView();
332 updateHeight();
333 } else {
334 Log.w(TAG, "Trying to update entry with different key, new entry: "
335 + entry.key + " old entry: " + mEntry.key);
336 }
337 }
338
Mady Mellor9801e852019-01-22 14:50:28 -0800339 private void updateHeaderView() {
340 mSettingsIcon.setContentDescription(getResources().getString(
341 R.string.bubbles_settings_button_description, mAppName));
342 mDeepLinkIcon.setContentDescription(getResources().getString(
343 R.string.bubbles_deep_link_button_description, mAppName));
Mady Mellore8e07712019-01-23 12:45:33 -0800344 }
345
346 private void updatePermissionView() {
347 boolean hasUserApprovedBubblesForPackage = false;
348 try {
349 hasUserApprovedBubblesForPackage =
350 mNotificationManagerService.hasUserApprovedBubblesForPackage(
351 mEntry.notification.getPackageName(), mEntry.notification.getUid());
352 } catch (RemoteException e) {
353 Log.w(TAG, e);
354 }
355 if (hasUserApprovedBubblesForPackage) {
356 mHeaderView.setVisibility(VISIBLE);
357 mPermissionView.setVisibility(GONE);
358 } else {
359 mHeaderView.setVisibility(GONE);
360 mPermissionView.setVisibility(VISIBLE);
361 ((ImageView) mPermissionView.findViewById(R.id.pkgicon)).setImageDrawable(mAppIcon);
362 ((TextView) mPermissionView.findViewById(R.id.pkgname)).setText(mAppName);
Steven Wua62cb6a2019-02-15 17:12:51 -0500363 logBubbleClickEvent(mEntry.notification,
364 StatsLog.BUBBLE_UICHANGED__ACTION__PERMISSION_DIALOG_SHOWN);
Mady Mellor9801e852019-01-22 14:50:28 -0800365 }
366 }
367
Mady Mellor3dff9e62019-02-05 18:12:53 -0800368 private void updateExpandedView() {
369 mBubbleIntent = getBubbleIntent(mEntry);
370 if (mBubbleIntent != null) {
371 if (mNotifRow != null) {
372 // Clear out the row if we had it previously
373 removeView(mNotifRow);
374 mNotifRow = null;
375 }
Mady Mellor3dff9e62019-02-05 18:12:53 -0800376 mActivityView.setVisibility(VISIBLE);
377 } else {
378 // Hide activity view if we had it previously
379 mActivityView.setVisibility(GONE);
380
381 // Use notification view
382 mNotifRow = mEntry.getRow();
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800383 if (mShowOnTop) {
384 addView(mNotifRow);
385 } else {
386 addView(mNotifRow, mUseFooter ? 0 : 1);
387 }
Mady Mellor3dff9e62019-02-05 18:12:53 -0800388 }
389 updateView();
390 }
391
Mark Renouf041d7262019-02-06 12:09:41 -0500392 boolean performBackPressIfNeeded() {
393 if (mActivityView == null || !usingActivityView()) {
394 return false;
395 }
396 mActivityView.performBackPress();
397 return true;
398 }
399
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800400 /**
401 * @return total height that the expanded view occupies.
402 */
403 int getExpandedSize() {
404 int chromeHeight = mPermissionView.getVisibility() != View.VISIBLE
405 ? mHeaderHeight
406 : mPermissionHeight;
407 return mBubbleHeight + mPointerView.getHeight() + mPointerMargin
408 + chromeHeight;
409 }
410
Mady Mellorfe7ec032019-01-30 17:32:49 -0800411 void updateHeight() {
412 if (usingActivityView()) {
413 Notification.BubbleMetadata data = mEntry.getBubbleMetadata();
414 int desiredHeight;
415 if (data == null) {
416 // This is a contentIntent based bubble, lets allow it to be the max height
417 // as it was forced into this mode and not prepared to be small
418 desiredHeight = mStackView.getMaxExpandedHeight();
419 } else {
420 desiredHeight = data.getDesiredHeight() > 0
421 ? data.getDesiredHeight()
422 : mMinHeight;
423 }
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800424 int chromeHeight = mPermissionView.getVisibility() != View.VISIBLE
425 ? mHeaderHeight
426 : mPermissionHeight;
427 int max = mStackView.getMaxExpandedHeight() - chromeHeight - mPointerView.getHeight()
428 - mPointerMargin;
Mady Mellorfe7ec032019-01-30 17:32:49 -0800429 int height = Math.min(desiredHeight, max);
430 height = Math.max(height, mMinHeight);
431 LayoutParams lp = (LayoutParams) mActivityView.getLayoutParams();
432 lp.height = height;
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800433 mBubbleHeight = height;
Mady Mellorfe7ec032019-01-30 17:32:49 -0800434 mActivityView.setLayoutParams(lp);
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800435 } else {
436 mBubbleHeight = mNotifRow != null ? mNotifRow.getIntrinsicHeight() : mMinHeight;
Mady Mellorfe7ec032019-01-30 17:32:49 -0800437 }
438 }
439
Mady Mellor9801e852019-01-22 14:50:28 -0800440 @Override
441 public void onClick(View view) {
442 if (mEntry == null) {
443 return;
444 }
445 Notification n = mEntry.notification.getNotification();
446 int id = view.getId();
447 if (id == R.id.deep_link_button) {
448 mStackView.collapseStack(() -> {
449 try {
450 n.contentIntent.send();
Steven Wub00225b2019-02-08 14:27:42 -0500451 logBubbleClickEvent(mEntry.notification,
452 StatsLog.BUBBLE_UICHANGED__ACTION__HEADER_GO_TO_APP);
Mady Mellor9801e852019-01-22 14:50:28 -0800453 } catch (PendingIntent.CanceledException e) {
454 Log.w(TAG, "Failed to send intent for bubble with key: "
455 + (mEntry != null ? mEntry.key : " null entry"));
456 }
457 });
458 } else if (id == R.id.settings_button) {
459 Intent intent = getSettingsIntent(mEntry.notification.getPackageName(),
460 mEntry.notification.getUid());
Steven Wub00225b2019-02-08 14:27:42 -0500461 mStackView.collapseStack(() -> {
462 mContext.startActivity(intent);
463 logBubbleClickEvent(mEntry.notification,
464 StatsLog.BUBBLE_UICHANGED__ACTION__HEADER_GO_TO_SETTINGS);
465 });
Mady Mellore8e07712019-01-23 12:45:33 -0800466 } else if (id == R.id.no_bubbles_button) {
467 setBubblesAllowed(false);
468 } else if (id == R.id.yes_bubbles_button) {
469 setBubblesAllowed(true);
470 }
471 }
472
473 private void setBubblesAllowed(boolean allowed) {
474 try {
475 mNotificationManagerService.setBubblesAllowed(
476 mEntry.notification.getPackageName(),
477 mEntry.notification.getUid(),
478 allowed);
479 if (allowed) {
480 mPermissionView.setVisibility(GONE);
481 mHeaderView.setVisibility(VISIBLE);
482 } else if (mOnBubbleBlockedListener != null) {
483 mOnBubbleBlockedListener.onBubbleBlocked(mEntry);
484 }
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800485 mStackView.onExpandedHeightChanged();
Steven Wub00225b2019-02-08 14:27:42 -0500486 logBubbleClickEvent(mEntry.notification,
487 allowed ? StatsLog.BUBBLE_UICHANGED__ACTION__PERMISSION_OPT_IN :
488 StatsLog.BUBBLE_UICHANGED__ACTION__PERMISSION_OPT_OUT);
Mady Mellore8e07712019-01-23 12:45:33 -0800489 } catch (RemoteException e) {
490 Log.w(TAG, e);
Mady Mellor9801e852019-01-22 14:50:28 -0800491 }
Mady Mellordea7ecf2018-12-10 15:47:40 -0800492 }
493
494 /**
Mady Mellor3dff9e62019-02-05 18:12:53 -0800495 * Update appearance of the expanded view being displayed.
496 */
497 public void updateView() {
498 if (usingActivityView()
499 && mActivityView.getVisibility() == VISIBLE
500 && mActivityView.isAttachedToWindow()) {
501 mActivityView.onLocationChanged();
502 } else if (mNotifRow != null) {
503 applyRowState(mNotifRow);
504 }
Mady Mellorfe7ec032019-01-30 17:32:49 -0800505 updateHeight();
Mady Mellor3dff9e62019-02-05 18:12:53 -0800506 }
507
508 /**
Mady Mellordea7ecf2018-12-10 15:47:40 -0800509 * Set the x position that the tip of the triangle should point to.
510 */
Mady Mellor3dff9e62019-02-05 18:12:53 -0800511 public void setPointerPosition(float x) {
Mady Mellordea7ecf2018-12-10 15:47:40 -0800512 // Adjust for the pointer size
Mady Mellor3dff9e62019-02-05 18:12:53 -0800513 x -= (mPointerView.getWidth() / 2f);
Mady Mellordea7ecf2018-12-10 15:47:40 -0800514 mPointerView.setTranslationX(x);
515 }
516
517 /**
Mady Mellor3dff9e62019-02-05 18:12:53 -0800518 * Removes and releases an ActivityView if one was previously created for this bubble.
Mady Mellordea7ecf2018-12-10 15:47:40 -0800519 */
Mady Mellor3dff9e62019-02-05 18:12:53 -0800520 public void destroyActivityView(ViewGroup tmpParent) {
521 if (mActivityView == null) {
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500522 return;
523 }
Mady Mellor3dff9e62019-02-05 18:12:53 -0800524 if (!mActivityViewReady) {
525 // release not needed, never initialized?
526 mActivityView = null;
527 return;
Mady Mellordea7ecf2018-12-10 15:47:40 -0800528 }
Mady Mellor3dff9e62019-02-05 18:12:53 -0800529 // HACK: release() will crash if the view is not attached.
530 if (!isAttachedToWindow()) {
531 mActivityView.setVisibility(View.GONE);
532 tmpParent.addView(this);
Mady Mellordea7ecf2018-12-10 15:47:40 -0800533 }
Mady Mellor3dff9e62019-02-05 18:12:53 -0800534
535 mActivityView.release();
536 ((ViewGroup) getParent()).removeView(this);
537 mActivityView = null;
538 mActivityViewReady = false;
Mady Mellordea7ecf2018-12-10 15:47:40 -0800539 }
540
Mady Mellor3dff9e62019-02-05 18:12:53 -0800541 private boolean usingActivityView() {
542 return mBubbleIntent != null;
543 }
544
545 private void applyRowState(ExpandableNotificationRow view) {
546 view.reset();
547 view.setHeadsUp(false);
548 view.resetTranslation();
549 view.setOnKeyguard(false);
550 view.setOnAmbient(false);
551 view.setClipBottomAmount(0);
552 view.setClipTopAmount(0);
553 view.setContentTransformationAmount(0, false);
554 view.setIconsVisible(true);
555
556 // TODO - Need to reset this (and others) when view goes back in shade, leave for now
557 // view.setTopRoundness(1, false);
558 // view.setBottomRoundness(1, false);
559
560 ExpandableViewState viewState = view.getViewState();
561 viewState = viewState == null ? new ExpandableViewState() : viewState;
562 viewState.height = view.getIntrinsicHeight();
563 viewState.gone = false;
564 viewState.hidden = false;
565 viewState.dimmed = false;
566 viewState.dark = false;
567 viewState.alpha = 1f;
568 viewState.notGoneIndex = -1;
569 viewState.xTranslation = 0;
570 viewState.yTranslation = 0;
571 viewState.zTranslation = 0;
572 viewState.scaleX = 1;
573 viewState.scaleY = 1;
574 viewState.inShelf = true;
575 viewState.headsUpIsVisible = false;
576 viewState.applyToView(view);
Mady Mellordea7ecf2018-12-10 15:47:40 -0800577 }
Mady Mellor9801e852019-01-22 14:50:28 -0800578
579 private Intent getSettingsIntent(String packageName, final int appUid) {
580 final Intent intent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
581 intent.putExtra(Settings.EXTRA_APP_PACKAGE, packageName);
582 intent.putExtra(Settings.EXTRA_APP_UID, appUid);
583 intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
584 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
585 return intent;
586 }
Mady Mellore8e07712019-01-23 12:45:33 -0800587
Mady Mellor3dff9e62019-02-05 18:12:53 -0800588 @Nullable
589 private PendingIntent getBubbleIntent(NotificationEntry entry) {
590 Notification notif = entry.notification.getNotification();
591 String packageName = entry.notification.getPackageName();
592 Notification.BubbleMetadata data = notif.getBubbleMetadata();
593 if (data != null && canLaunchInActivityView(data.getIntent(), true /* enableLogging */,
594 packageName)) {
595 return data.getIntent();
596 } else if (BubbleController.shouldUseContentIntent(mContext)
597 && canLaunchInActivityView(notif.contentIntent, false /* enableLogging */,
598 packageName)) {
599 return notif.contentIntent;
600 }
601 return null;
602 }
603
604 /**
605 * Whether an intent is properly configured to display in an {@link android.app.ActivityView}.
606 *
607 * @param intent the pending intent of the bubble.
608 * @param enableLogging whether bubble developer error should be logged.
609 * @param packageName the notification package name for this bubble.
610 * @return
611 */
612 private boolean canLaunchInActivityView(PendingIntent intent, boolean enableLogging,
613 String packageName) {
614 if (intent == null) {
615 return false;
616 }
617 ActivityInfo info =
618 intent.getIntent().resolveActivityInfo(mContext.getPackageManager(), 0);
619 if (info == null) {
620 if (enableLogging) {
621 StatsLog.write(StatsLog.BUBBLE_DEVELOPER_ERROR_REPORTED, packageName,
622 BUBBLE_DEVELOPER_ERROR_REPORTED__ERROR__ACTIVITY_INFO_MISSING);
623 }
624 return false;
625 }
626 if (!ActivityInfo.isResizeableMode(info.resizeMode)) {
627 if (enableLogging) {
628 StatsLog.write(StatsLog.BUBBLE_DEVELOPER_ERROR_REPORTED, packageName,
629 BUBBLE_DEVELOPER_ERROR_REPORTED__ERROR__ACTIVITY_INFO_NOT_RESIZABLE);
630 }
631 return false;
632 }
633 if (info.documentLaunchMode != DOCUMENT_LAUNCH_ALWAYS) {
634 if (enableLogging) {
635 StatsLog.write(StatsLog.BUBBLE_DEVELOPER_ERROR_REPORTED, packageName,
636 BUBBLE_DEVELOPER_ERROR_REPORTED__ERROR__DOCUMENT_LAUNCH_NOT_ALWAYS);
637 }
638 return false;
639 }
640 return (info.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) != 0;
641 }
642
Mady Mellore8e07712019-01-23 12:45:33 -0800643 /**
644 * Listener that is notified when a bubble is blocked.
645 */
646 public interface OnBubbleBlockedListener {
647 /**
648 * Called when a bubble is blocked for the provided entry.
649 */
650 void onBubbleBlocked(NotificationEntry entry);
651 }
Steven Wub00225b2019-02-08 14:27:42 -0500652
653 /**
654 * Logs bubble UI click event.
655 *
656 * @param notification the bubble notification that user is interacting with.
657 * @param action the user interaction enum.
658 */
659 private void logBubbleClickEvent(StatusBarNotification notification, int action) {
660 StatsLog.write(StatsLog.BUBBLE_UI_CHANGED,
661 notification.getPackageName(),
662 notification.getNotification().getChannelId(),
663 notification.getId(),
664 mStackView.getBubbleIndex(mStackView.getExpandedBubble()),
665 mStackView.getBubbleCount(),
666 action,
667 mStackView.getNormalizedXPosition(),
668 mStackView.getNormalizedYPosition());
669 }
Mady Mellordea7ecf2018-12-10 15:47:40 -0800670}