blob: 856b9d6c27e46548e620a3e23bbfa0c848e45055 [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 Mellor5029fa62019-03-05 12:16:21 -080052import android.view.ViewGroup;
Mady Mellor3dff9e62019-02-05 18:12:53 -080053import 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.
Mark Renouf08bc42a2019-03-07 13:01:59 -0500138 post(() -> mBubbleController.removeBubble(mEntry.key,
139 BubbleController.DISMISS_TASK_FINISHED));
Mady Mellor3dff9e62019-02-05 18:12:53 -0800140 }
141 }
142 };
Mady Mellore8e07712019-01-23 12:45:33 -0800143
Mady Mellor3d82e682019-02-05 13:34:48 -0800144 public BubbleExpandedView(Context context) {
Mady Mellordea7ecf2018-12-10 15:47:40 -0800145 this(context, null);
146 }
147
Mady Mellor3d82e682019-02-05 13:34:48 -0800148 public BubbleExpandedView(Context context, AttributeSet attrs) {
Mady Mellordea7ecf2018-12-10 15:47:40 -0800149 this(context, attrs, 0);
150 }
151
Mady Mellor3d82e682019-02-05 13:34:48 -0800152 public BubbleExpandedView(Context context, AttributeSet attrs, int defStyleAttr) {
Mady Mellordea7ecf2018-12-10 15:47:40 -0800153 this(context, attrs, defStyleAttr, 0);
154 }
155
Mady Mellor3d82e682019-02-05 13:34:48 -0800156 public BubbleExpandedView(Context context, AttributeSet attrs, int defStyleAttr,
Mady Mellordea7ecf2018-12-10 15:47:40 -0800157 int defStyleRes) {
158 super(context, attrs, defStyleAttr, defStyleRes);
Mady Mellor9801e852019-01-22 14:50:28 -0800159 mPm = context.getPackageManager();
Mady Mellorfe7ec032019-01-30 17:32:49 -0800160 mMinHeight = getResources().getDimensionPixelSize(
Mady Mellor3dff9e62019-02-05 18:12:53 -0800161 R.dimen.bubble_expanded_default_height);
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800162 mPointerMargin = getResources().getDimensionPixelSize(R.dimen.bubble_pointer_margin);
Mady Mellore8e07712019-01-23 12:45:33 -0800163 try {
164 mNotificationManagerService = INotificationManager.Stub.asInterface(
165 ServiceManager.getServiceOrThrow(Context.NOTIFICATION_SERVICE));
166 } catch (ServiceManager.ServiceNotFoundException e) {
167 Log.w(TAG, e);
168 }
Mady Mellordea7ecf2018-12-10 15:47:40 -0800169 }
170
171 @Override
172 protected void onFinishInflate() {
173 super.onFinishInflate();
174
175 Resources res = getResources();
176 mPointerView = findViewById(R.id.pointer_view);
177 int width = res.getDimensionPixelSize(R.dimen.bubble_pointer_width);
178 int height = res.getDimensionPixelSize(R.dimen.bubble_pointer_height);
Mady Mellordd497052019-01-30 17:23:48 -0800179
180 TypedArray ta = getContext().obtainStyledAttributes(
181 new int[] {android.R.attr.colorBackgroundFloating});
182 int bgColor = ta.getColor(0, Color.WHITE);
183 ta.recycle();
184
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800185 mShowOnTop = BubbleController.showBubblesAtTop(getContext());
186 mUseFooter = BubbleController.useFooter(getContext());
187
Mady Mellordea7ecf2018-12-10 15:47:40 -0800188 ShapeDrawable triangleDrawable = new ShapeDrawable(
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800189 TriangleShape.create(width, height, mShowOnTop /* pointUp */));
Mady Mellordd497052019-01-30 17:23:48 -0800190 triangleDrawable.setTint(bgColor);
Mady Mellordea7ecf2018-12-10 15:47:40 -0800191 mPointerView.setBackground(triangleDrawable);
Mady Mellor9801e852019-01-22 14:50:28 -0800192
Mady Mellore8e07712019-01-23 12:45:33 -0800193 FrameLayout viewWrapper = findViewById(R.id.header_permission_wrapper);
Mady Mellor310c7c62019-02-21 17:03:08 -0800194 viewWrapper.setBackground(createHeaderPermissionBackground(bgColor));
195
Mady Mellore8e07712019-01-23 12:45:33 -0800196 LayoutTransition transition = new LayoutTransition();
197 transition.setDuration(200);
198
199 ObjectAnimator appearAnimator = ObjectAnimator.ofFloat(null, View.ALPHA, 0f, 1f);
200 transition.setAnimator(LayoutTransition.APPEARING, appearAnimator);
201 transition.setInterpolator(LayoutTransition.APPEARING, Interpolators.ALPHA_IN);
202
203 ObjectAnimator disappearAnimator = ObjectAnimator.ofFloat(null, View.ALPHA, 1f, 0f);
204 transition.setAnimator(LayoutTransition.DISAPPEARING, disappearAnimator);
205 transition.setInterpolator(LayoutTransition.DISAPPEARING, Interpolators.ALPHA_OUT);
206
207 transition.setAnimateParentHierarchy(false);
208 viewWrapper.setLayoutTransition(transition);
209 viewWrapper.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
210
Mady Mellor310c7c62019-02-21 17:03:08 -0800211
Mady Mellorfe7ec032019-01-30 17:32:49 -0800212 mHeaderHeight = getContext().getResources().getDimensionPixelSize(
213 R.dimen.bubble_expanded_header_height);
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800214 mPermissionHeight = getContext().getResources().getDimensionPixelSize(
215 R.dimen.bubble_permission_height);
Mady Mellore8e07712019-01-23 12:45:33 -0800216 mHeaderView = findViewById(R.id.header_layout);
Mady Mellor9801e852019-01-22 14:50:28 -0800217 mDeepLinkIcon = findViewById(R.id.deep_link_button);
218 mSettingsIcon = findViewById(R.id.settings_button);
219 mDeepLinkIcon.setOnClickListener(this);
220 mSettingsIcon.setOnClickListener(this);
Mady Mellore8e07712019-01-23 12:45:33 -0800221
222 mPermissionView = findViewById(R.id.permission_layout);
223 findViewById(R.id.no_bubbles_button).setOnClickListener(this);
224 findViewById(R.id.yes_bubbles_button).setOnClickListener(this);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800225
226 mActivityView = new ActivityView(mContext, null /* attrs */, 0 /* defStyle */,
227 true /* singleTaskInstance */);
228 addView(mActivityView);
229
230 mActivityView.setOnApplyWindowInsetsListener((View view, WindowInsets insets) -> {
231 ActivityView activityView = (ActivityView) view;
232 // Here we assume that the position of the ActivityView on the screen
233 // remains regardless of IME status. When we move ActivityView, the
234 // forwardedInsets should be computed not against the current location
235 // and size, but against the post-moved location and size.
236 Point displaySize = new Point();
237 view.getContext().getDisplay().getSize(displaySize);
238 int[] windowLocation = view.getLocationOnScreen();
239 final int windowBottom = windowLocation[1] + view.getHeight();
240 final int keyboardHeight = insets.getSystemWindowInsetBottom()
241 - insets.getStableInsetBottom();
242 final int insetsBottom = Math.max(0,
243 windowBottom + keyboardHeight - displaySize.y);
244 activityView.setForwardedInsets(Insets.of(0, 0, 0, insetsBottom));
245 return view.onApplyWindowInsets(insets);
246 });
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800247
248 if (!mShowOnTop) {
249 removeView(mPointerView);
250 if (mUseFooter) {
Mady Mellor310c7c62019-02-21 17:03:08 -0800251 View divider = findViewById(R.id.divider);
252 viewWrapper.removeView(divider);
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800253 removeView(viewWrapper);
Mady Mellor310c7c62019-02-21 17:03:08 -0800254 addView(divider);
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800255 addView(viewWrapper);
256 }
257 addView(mPointerView);
258 }
Mady Mellore8e07712019-01-23 12:45:33 -0800259 }
260
261 /**
Mady Mellor310c7c62019-02-21 17:03:08 -0800262 * Creates a background with corners rounded based on how the view is configured to display
263 */
264 private Drawable createHeaderPermissionBackground(int bgColor) {
265 TypedArray ta2 = getContext().obtainStyledAttributes(
266 new int[] {android.R.attr.dialogCornerRadius});
267 final float cr = ta2.getDimension(0, 0f);
268 ta2.recycle();
269
270 float[] radii = mUseFooter
271 ? new float[] {0, 0, 0, 0, cr, cr, cr, cr}
272 : new float[] {cr, cr, cr, cr, 0, 0, 0, 0};
273 GradientDrawable chromeBackground = new GradientDrawable();
274 chromeBackground.setShape(GradientDrawable.RECTANGLE);
275 chromeBackground.setCornerRadii(radii);
276 chromeBackground.setColor(bgColor);
277 return chromeBackground;
278 }
279
280 /**
Mady Mellore8e07712019-01-23 12:45:33 -0800281 * Sets the listener to notify when a bubble has been blocked.
282 */
283 public void setOnBlockedListener(OnBubbleBlockedListener listener) {
284 mOnBubbleBlockedListener = listener;
Mady Mellor9801e852019-01-22 14:50:28 -0800285 }
286
287 /**
288 * Sets the notification entry used to populate this view.
289 */
290 public void setEntry(NotificationEntry entry, BubbleStackView stackView) {
291 mStackView = stackView;
292 mEntry = entry;
293
294 ApplicationInfo info;
295 try {
296 info = mPm.getApplicationInfo(
297 entry.notification.getPackageName(),
298 PackageManager.MATCH_UNINSTALLED_PACKAGES
299 | PackageManager.MATCH_DISABLED_COMPONENTS
300 | PackageManager.MATCH_DIRECT_BOOT_UNAWARE
301 | PackageManager.MATCH_DIRECT_BOOT_AWARE);
302 if (info != null) {
303 mAppName = String.valueOf(mPm.getApplicationLabel(info));
Mady Mellore8e07712019-01-23 12:45:33 -0800304 mAppIcon = mPm.getApplicationIcon(info);
Mady Mellor9801e852019-01-22 14:50:28 -0800305 }
306 } catch (PackageManager.NameNotFoundException e) {
307 // Ahh... just use package name
308 mAppName = entry.notification.getPackageName();
309 }
Mady Mellore8e07712019-01-23 12:45:33 -0800310 if (mAppIcon == null) {
311 mAppIcon = mPm.getDefaultActivityIcon();
312 }
Mady Mellor9801e852019-01-22 14:50:28 -0800313 updateHeaderView();
Mady Mellore8e07712019-01-23 12:45:33 -0800314 updatePermissionView();
Mady Mellor3dff9e62019-02-05 18:12:53 -0800315 updateExpandedView();
Mady Mellor6d002032019-02-13 13:45:17 -0800316 }
317
318 /**
319 * Lets activity view know it should be shown / populated.
320 */
Mady Mellor5029fa62019-03-05 12:16:21 -0800321 public void populateExpandedView() {
322 if (usingActivityView()) {
323 mActivityView.setCallback(mStateCallback);
324 } else {
325 // We're using notification template
326 ViewGroup parent = (ViewGroup) mNotifRow.getParent();
327 if (parent == this) {
328 // Already added
329 return;
330 } else if (parent != null) {
331 // Still in the shade... remove it
332 parent.removeView(mNotifRow);
333 }
334 if (mShowOnTop) {
335 addView(mNotifRow);
336 } else {
337 addView(mNotifRow, mUseFooter ? 0 : 1);
338 }
339 }
Mady Mellor9801e852019-01-22 14:50:28 -0800340 }
341
Mady Mellorfe7ec032019-01-30 17:32:49 -0800342 /**
343 * Updates the entry backing this view. This will not re-populate ActivityView, it will
344 * only update the deep-links in the header, the title, and the height of the view.
345 */
346 public void update(NotificationEntry entry) {
347 if (entry.key.equals(mEntry.key)) {
348 mEntry = entry;
349 updateHeaderView();
350 updateHeight();
351 } else {
352 Log.w(TAG, "Trying to update entry with different key, new entry: "
353 + entry.key + " old entry: " + mEntry.key);
354 }
355 }
356
Mady Mellor9801e852019-01-22 14:50:28 -0800357 private void updateHeaderView() {
358 mSettingsIcon.setContentDescription(getResources().getString(
359 R.string.bubbles_settings_button_description, mAppName));
360 mDeepLinkIcon.setContentDescription(getResources().getString(
361 R.string.bubbles_deep_link_button_description, mAppName));
Mady Mellore8e07712019-01-23 12:45:33 -0800362 }
363
364 private void updatePermissionView() {
365 boolean hasUserApprovedBubblesForPackage = false;
366 try {
367 hasUserApprovedBubblesForPackage =
368 mNotificationManagerService.hasUserApprovedBubblesForPackage(
369 mEntry.notification.getPackageName(), mEntry.notification.getUid());
370 } catch (RemoteException e) {
371 Log.w(TAG, e);
372 }
373 if (hasUserApprovedBubblesForPackage) {
374 mHeaderView.setVisibility(VISIBLE);
375 mPermissionView.setVisibility(GONE);
376 } else {
377 mHeaderView.setVisibility(GONE);
378 mPermissionView.setVisibility(VISIBLE);
379 ((ImageView) mPermissionView.findViewById(R.id.pkgicon)).setImageDrawable(mAppIcon);
380 ((TextView) mPermissionView.findViewById(R.id.pkgname)).setText(mAppName);
Steven Wua62cb6a2019-02-15 17:12:51 -0500381 logBubbleClickEvent(mEntry.notification,
382 StatsLog.BUBBLE_UICHANGED__ACTION__PERMISSION_DIALOG_SHOWN);
Mady Mellor9801e852019-01-22 14:50:28 -0800383 }
384 }
385
Mady Mellor3dff9e62019-02-05 18:12:53 -0800386 private void updateExpandedView() {
387 mBubbleIntent = getBubbleIntent(mEntry);
388 if (mBubbleIntent != null) {
389 if (mNotifRow != null) {
390 // Clear out the row if we had it previously
391 removeView(mNotifRow);
392 mNotifRow = null;
393 }
Mady Mellor3dff9e62019-02-05 18:12:53 -0800394 mActivityView.setVisibility(VISIBLE);
395 } else {
396 // Hide activity view if we had it previously
397 mActivityView.setVisibility(GONE);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800398 mNotifRow = mEntry.getRow();
Mady Mellor5029fa62019-03-05 12:16:21 -0800399
Mady Mellor3dff9e62019-02-05 18:12:53 -0800400 }
401 updateView();
402 }
403
Mark Renouf041d7262019-02-06 12:09:41 -0500404 boolean performBackPressIfNeeded() {
405 if (mActivityView == null || !usingActivityView()) {
406 return false;
407 }
408 mActivityView.performBackPress();
409 return true;
410 }
411
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800412 /**
413 * @return total height that the expanded view occupies.
414 */
415 int getExpandedSize() {
416 int chromeHeight = mPermissionView.getVisibility() != View.VISIBLE
417 ? mHeaderHeight
418 : mPermissionHeight;
419 return mBubbleHeight + mPointerView.getHeight() + mPointerMargin
420 + chromeHeight;
421 }
422
Mady Mellorfe7ec032019-01-30 17:32:49 -0800423 void updateHeight() {
424 if (usingActivityView()) {
425 Notification.BubbleMetadata data = mEntry.getBubbleMetadata();
426 int desiredHeight;
427 if (data == null) {
428 // This is a contentIntent based bubble, lets allow it to be the max height
429 // as it was forced into this mode and not prepared to be small
430 desiredHeight = mStackView.getMaxExpandedHeight();
431 } else {
432 desiredHeight = data.getDesiredHeight() > 0
433 ? data.getDesiredHeight()
434 : mMinHeight;
435 }
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800436 int chromeHeight = mPermissionView.getVisibility() != View.VISIBLE
437 ? mHeaderHeight
438 : mPermissionHeight;
439 int max = mStackView.getMaxExpandedHeight() - chromeHeight - mPointerView.getHeight()
440 - mPointerMargin;
Mady Mellorfe7ec032019-01-30 17:32:49 -0800441 int height = Math.min(desiredHeight, max);
442 height = Math.max(height, mMinHeight);
443 LayoutParams lp = (LayoutParams) mActivityView.getLayoutParams();
444 lp.height = height;
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800445 mBubbleHeight = height;
Mady Mellorfe7ec032019-01-30 17:32:49 -0800446 mActivityView.setLayoutParams(lp);
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800447 } else {
448 mBubbleHeight = mNotifRow != null ? mNotifRow.getIntrinsicHeight() : mMinHeight;
Mady Mellorfe7ec032019-01-30 17:32:49 -0800449 }
450 }
451
Mady Mellor9801e852019-01-22 14:50:28 -0800452 @Override
453 public void onClick(View view) {
454 if (mEntry == null) {
455 return;
456 }
457 Notification n = mEntry.notification.getNotification();
458 int id = view.getId();
459 if (id == R.id.deep_link_button) {
460 mStackView.collapseStack(() -> {
461 try {
462 n.contentIntent.send();
Steven Wub00225b2019-02-08 14:27:42 -0500463 logBubbleClickEvent(mEntry.notification,
464 StatsLog.BUBBLE_UICHANGED__ACTION__HEADER_GO_TO_APP);
Mady Mellor9801e852019-01-22 14:50:28 -0800465 } catch (PendingIntent.CanceledException e) {
466 Log.w(TAG, "Failed to send intent for bubble with key: "
467 + (mEntry != null ? mEntry.key : " null entry"));
468 }
469 });
470 } else if (id == R.id.settings_button) {
471 Intent intent = getSettingsIntent(mEntry.notification.getPackageName(),
472 mEntry.notification.getUid());
Steven Wub00225b2019-02-08 14:27:42 -0500473 mStackView.collapseStack(() -> {
474 mContext.startActivity(intent);
475 logBubbleClickEvent(mEntry.notification,
476 StatsLog.BUBBLE_UICHANGED__ACTION__HEADER_GO_TO_SETTINGS);
477 });
Mady Mellore8e07712019-01-23 12:45:33 -0800478 } else if (id == R.id.no_bubbles_button) {
479 setBubblesAllowed(false);
480 } else if (id == R.id.yes_bubbles_button) {
481 setBubblesAllowed(true);
482 }
483 }
484
485 private void setBubblesAllowed(boolean allowed) {
486 try {
487 mNotificationManagerService.setBubblesAllowed(
488 mEntry.notification.getPackageName(),
489 mEntry.notification.getUid(),
490 allowed);
491 if (allowed) {
492 mPermissionView.setVisibility(GONE);
493 mHeaderView.setVisibility(VISIBLE);
494 } else if (mOnBubbleBlockedListener != null) {
495 mOnBubbleBlockedListener.onBubbleBlocked(mEntry);
496 }
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800497 mStackView.onExpandedHeightChanged();
Steven Wub00225b2019-02-08 14:27:42 -0500498 logBubbleClickEvent(mEntry.notification,
499 allowed ? StatsLog.BUBBLE_UICHANGED__ACTION__PERMISSION_OPT_IN :
500 StatsLog.BUBBLE_UICHANGED__ACTION__PERMISSION_OPT_OUT);
Mady Mellore8e07712019-01-23 12:45:33 -0800501 } catch (RemoteException e) {
502 Log.w(TAG, e);
Mady Mellor9801e852019-01-22 14:50:28 -0800503 }
Mady Mellordea7ecf2018-12-10 15:47:40 -0800504 }
505
506 /**
Mady Mellor3dff9e62019-02-05 18:12:53 -0800507 * Update appearance of the expanded view being displayed.
508 */
509 public void updateView() {
510 if (usingActivityView()
511 && mActivityView.getVisibility() == VISIBLE
512 && mActivityView.isAttachedToWindow()) {
513 mActivityView.onLocationChanged();
514 } else if (mNotifRow != null) {
515 applyRowState(mNotifRow);
516 }
Mady Mellorfe7ec032019-01-30 17:32:49 -0800517 updateHeight();
Mady Mellor3dff9e62019-02-05 18:12:53 -0800518 }
519
520 /**
Mady Mellordea7ecf2018-12-10 15:47:40 -0800521 * Set the x position that the tip of the triangle should point to.
522 */
Mady Mellor3dff9e62019-02-05 18:12:53 -0800523 public void setPointerPosition(float x) {
Mady Mellordea7ecf2018-12-10 15:47:40 -0800524 // Adjust for the pointer size
Mady Mellor3dff9e62019-02-05 18:12:53 -0800525 x -= (mPointerView.getWidth() / 2f);
Mady Mellordea7ecf2018-12-10 15:47:40 -0800526 mPointerView.setTranslationX(x);
527 }
528
529 /**
Mady Mellor3dff9e62019-02-05 18:12:53 -0800530 * Removes and releases an ActivityView if one was previously created for this bubble.
Mady Mellordea7ecf2018-12-10 15:47:40 -0800531 */
Mady Mellor94d94a72019-03-05 18:16:59 -0800532 public void cleanUpExpandedState() {
533 removeView(mNotifRow);
534
Mady Mellor3dff9e62019-02-05 18:12:53 -0800535 if (mActivityView == null) {
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500536 return;
537 }
Mark Renouf28c250d2019-02-25 16:47:34 -0500538 if (mActivityViewReady) {
539 mActivityView.release();
Mady Mellordea7ecf2018-12-10 15:47:40 -0800540 }
Mark Renouf28c250d2019-02-25 16:47:34 -0500541 removeView(mActivityView);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800542 mActivityView = null;
543 mActivityViewReady = false;
Mady Mellordea7ecf2018-12-10 15:47:40 -0800544 }
545
Mady Mellor3dff9e62019-02-05 18:12:53 -0800546 private boolean usingActivityView() {
547 return mBubbleIntent != null;
548 }
549
550 private void applyRowState(ExpandableNotificationRow view) {
551 view.reset();
552 view.setHeadsUp(false);
553 view.resetTranslation();
554 view.setOnKeyguard(false);
555 view.setOnAmbient(false);
556 view.setClipBottomAmount(0);
557 view.setClipTopAmount(0);
558 view.setContentTransformationAmount(0, false);
559 view.setIconsVisible(true);
560
561 // TODO - Need to reset this (and others) when view goes back in shade, leave for now
562 // view.setTopRoundness(1, false);
563 // view.setBottomRoundness(1, false);
564
565 ExpandableViewState viewState = view.getViewState();
566 viewState = viewState == null ? new ExpandableViewState() : viewState;
567 viewState.height = view.getIntrinsicHeight();
568 viewState.gone = false;
569 viewState.hidden = false;
570 viewState.dimmed = false;
571 viewState.dark = false;
572 viewState.alpha = 1f;
573 viewState.notGoneIndex = -1;
574 viewState.xTranslation = 0;
575 viewState.yTranslation = 0;
576 viewState.zTranslation = 0;
577 viewState.scaleX = 1;
578 viewState.scaleY = 1;
579 viewState.inShelf = true;
580 viewState.headsUpIsVisible = false;
581 viewState.applyToView(view);
Mady Mellordea7ecf2018-12-10 15:47:40 -0800582 }
Mady Mellor9801e852019-01-22 14:50:28 -0800583
584 private Intent getSettingsIntent(String packageName, final int appUid) {
585 final Intent intent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
586 intent.putExtra(Settings.EXTRA_APP_PACKAGE, packageName);
587 intent.putExtra(Settings.EXTRA_APP_UID, appUid);
588 intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
589 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
590 return intent;
591 }
Mady Mellore8e07712019-01-23 12:45:33 -0800592
Mady Mellor3dff9e62019-02-05 18:12:53 -0800593 @Nullable
594 private PendingIntent getBubbleIntent(NotificationEntry entry) {
595 Notification notif = entry.notification.getNotification();
596 String packageName = entry.notification.getPackageName();
597 Notification.BubbleMetadata data = notif.getBubbleMetadata();
598 if (data != null && canLaunchInActivityView(data.getIntent(), true /* enableLogging */,
599 packageName)) {
600 return data.getIntent();
601 } else if (BubbleController.shouldUseContentIntent(mContext)
602 && canLaunchInActivityView(notif.contentIntent, false /* enableLogging */,
603 packageName)) {
604 return notif.contentIntent;
605 }
606 return null;
607 }
608
609 /**
610 * Whether an intent is properly configured to display in an {@link android.app.ActivityView}.
611 *
612 * @param intent the pending intent of the bubble.
613 * @param enableLogging whether bubble developer error should be logged.
614 * @param packageName the notification package name for this bubble.
615 * @return
616 */
617 private boolean canLaunchInActivityView(PendingIntent intent, boolean enableLogging,
618 String packageName) {
619 if (intent == null) {
620 return false;
621 }
622 ActivityInfo info =
623 intent.getIntent().resolveActivityInfo(mContext.getPackageManager(), 0);
624 if (info == null) {
625 if (enableLogging) {
626 StatsLog.write(StatsLog.BUBBLE_DEVELOPER_ERROR_REPORTED, packageName,
627 BUBBLE_DEVELOPER_ERROR_REPORTED__ERROR__ACTIVITY_INFO_MISSING);
628 }
629 return false;
630 }
631 if (!ActivityInfo.isResizeableMode(info.resizeMode)) {
632 if (enableLogging) {
633 StatsLog.write(StatsLog.BUBBLE_DEVELOPER_ERROR_REPORTED, packageName,
634 BUBBLE_DEVELOPER_ERROR_REPORTED__ERROR__ACTIVITY_INFO_NOT_RESIZABLE);
635 }
636 return false;
637 }
638 if (info.documentLaunchMode != DOCUMENT_LAUNCH_ALWAYS) {
639 if (enableLogging) {
640 StatsLog.write(StatsLog.BUBBLE_DEVELOPER_ERROR_REPORTED, packageName,
641 BUBBLE_DEVELOPER_ERROR_REPORTED__ERROR__DOCUMENT_LAUNCH_NOT_ALWAYS);
642 }
643 return false;
644 }
645 return (info.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) != 0;
646 }
647
Mady Mellore8e07712019-01-23 12:45:33 -0800648 /**
649 * Listener that is notified when a bubble is blocked.
650 */
651 public interface OnBubbleBlockedListener {
652 /**
653 * Called when a bubble is blocked for the provided entry.
654 */
655 void onBubbleBlocked(NotificationEntry entry);
656 }
Steven Wub00225b2019-02-08 14:27:42 -0500657
658 /**
659 * Logs bubble UI click event.
660 *
661 * @param notification the bubble notification that user is interacting with.
662 * @param action the user interaction enum.
663 */
664 private void logBubbleClickEvent(StatusBarNotification notification, int action) {
665 StatsLog.write(StatsLog.BUBBLE_UI_CHANGED,
666 notification.getPackageName(),
667 notification.getNotification().getChannelId(),
668 notification.getId(),
669 mStackView.getBubbleIndex(mStackView.getExpandedBubble()),
670 mStackView.getBubbleCount(),
671 action,
672 mStackView.getNormalizedXPosition(),
673 mStackView.getNormalizedYPosition());
674 }
Mady Mellordea7ecf2018-12-10 15:47:40 -0800675}