blob: 25ee87a13b74dbdc34187bd0416ea2fbbb2c731f [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.
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 */
Mady Mellor5029fa62019-03-05 12:16:21 -0800320 public void populateExpandedView() {
321 if (usingActivityView()) {
322 mActivityView.setCallback(mStateCallback);
323 } else {
324 // We're using notification template
325 ViewGroup parent = (ViewGroup) mNotifRow.getParent();
326 if (parent == this) {
327 // Already added
328 return;
329 } else if (parent != null) {
330 // Still in the shade... remove it
331 parent.removeView(mNotifRow);
332 }
333 if (mShowOnTop) {
334 addView(mNotifRow);
335 } else {
336 addView(mNotifRow, mUseFooter ? 0 : 1);
337 }
338 }
Mady Mellor9801e852019-01-22 14:50:28 -0800339 }
340
Mady Mellorfe7ec032019-01-30 17:32:49 -0800341 /**
342 * Updates the entry backing this view. This will not re-populate ActivityView, it will
343 * only update the deep-links in the header, the title, and the height of the view.
344 */
345 public void update(NotificationEntry entry) {
346 if (entry.key.equals(mEntry.key)) {
347 mEntry = entry;
348 updateHeaderView();
349 updateHeight();
350 } else {
351 Log.w(TAG, "Trying to update entry with different key, new entry: "
352 + entry.key + " old entry: " + mEntry.key);
353 }
354 }
355
Mady Mellor9801e852019-01-22 14:50:28 -0800356 private void updateHeaderView() {
357 mSettingsIcon.setContentDescription(getResources().getString(
358 R.string.bubbles_settings_button_description, mAppName));
359 mDeepLinkIcon.setContentDescription(getResources().getString(
360 R.string.bubbles_deep_link_button_description, mAppName));
Mady Mellore8e07712019-01-23 12:45:33 -0800361 }
362
363 private void updatePermissionView() {
364 boolean hasUserApprovedBubblesForPackage = false;
365 try {
366 hasUserApprovedBubblesForPackage =
367 mNotificationManagerService.hasUserApprovedBubblesForPackage(
368 mEntry.notification.getPackageName(), mEntry.notification.getUid());
369 } catch (RemoteException e) {
370 Log.w(TAG, e);
371 }
372 if (hasUserApprovedBubblesForPackage) {
373 mHeaderView.setVisibility(VISIBLE);
374 mPermissionView.setVisibility(GONE);
375 } else {
376 mHeaderView.setVisibility(GONE);
377 mPermissionView.setVisibility(VISIBLE);
378 ((ImageView) mPermissionView.findViewById(R.id.pkgicon)).setImageDrawable(mAppIcon);
379 ((TextView) mPermissionView.findViewById(R.id.pkgname)).setText(mAppName);
Steven Wua62cb6a2019-02-15 17:12:51 -0500380 logBubbleClickEvent(mEntry.notification,
381 StatsLog.BUBBLE_UICHANGED__ACTION__PERMISSION_DIALOG_SHOWN);
Mady Mellor9801e852019-01-22 14:50:28 -0800382 }
383 }
384
Mady Mellor3dff9e62019-02-05 18:12:53 -0800385 private void updateExpandedView() {
386 mBubbleIntent = getBubbleIntent(mEntry);
387 if (mBubbleIntent != null) {
388 if (mNotifRow != null) {
389 // Clear out the row if we had it previously
390 removeView(mNotifRow);
391 mNotifRow = null;
392 }
Mady Mellor3dff9e62019-02-05 18:12:53 -0800393 mActivityView.setVisibility(VISIBLE);
394 } else {
395 // Hide activity view if we had it previously
396 mActivityView.setVisibility(GONE);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800397 mNotifRow = mEntry.getRow();
Mady Mellor5029fa62019-03-05 12:16:21 -0800398
Mady Mellor3dff9e62019-02-05 18:12:53 -0800399 }
400 updateView();
401 }
402
Mark Renouf041d7262019-02-06 12:09:41 -0500403 boolean performBackPressIfNeeded() {
404 if (mActivityView == null || !usingActivityView()) {
405 return false;
406 }
407 mActivityView.performBackPress();
408 return true;
409 }
410
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800411 /**
412 * @return total height that the expanded view occupies.
413 */
414 int getExpandedSize() {
415 int chromeHeight = mPermissionView.getVisibility() != View.VISIBLE
416 ? mHeaderHeight
417 : mPermissionHeight;
418 return mBubbleHeight + mPointerView.getHeight() + mPointerMargin
419 + chromeHeight;
420 }
421
Mady Mellorfe7ec032019-01-30 17:32:49 -0800422 void updateHeight() {
423 if (usingActivityView()) {
424 Notification.BubbleMetadata data = mEntry.getBubbleMetadata();
425 int desiredHeight;
426 if (data == null) {
427 // This is a contentIntent based bubble, lets allow it to be the max height
428 // as it was forced into this mode and not prepared to be small
429 desiredHeight = mStackView.getMaxExpandedHeight();
430 } else {
431 desiredHeight = data.getDesiredHeight() > 0
432 ? data.getDesiredHeight()
433 : mMinHeight;
434 }
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800435 int chromeHeight = mPermissionView.getVisibility() != View.VISIBLE
436 ? mHeaderHeight
437 : mPermissionHeight;
438 int max = mStackView.getMaxExpandedHeight() - chromeHeight - mPointerView.getHeight()
439 - mPointerMargin;
Mady Mellorfe7ec032019-01-30 17:32:49 -0800440 int height = Math.min(desiredHeight, max);
441 height = Math.max(height, mMinHeight);
442 LayoutParams lp = (LayoutParams) mActivityView.getLayoutParams();
443 lp.height = height;
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800444 mBubbleHeight = height;
Mady Mellorfe7ec032019-01-30 17:32:49 -0800445 mActivityView.setLayoutParams(lp);
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800446 } else {
447 mBubbleHeight = mNotifRow != null ? mNotifRow.getIntrinsicHeight() : mMinHeight;
Mady Mellorfe7ec032019-01-30 17:32:49 -0800448 }
449 }
450
Mady Mellor9801e852019-01-22 14:50:28 -0800451 @Override
452 public void onClick(View view) {
453 if (mEntry == null) {
454 return;
455 }
456 Notification n = mEntry.notification.getNotification();
457 int id = view.getId();
458 if (id == R.id.deep_link_button) {
459 mStackView.collapseStack(() -> {
460 try {
461 n.contentIntent.send();
Steven Wub00225b2019-02-08 14:27:42 -0500462 logBubbleClickEvent(mEntry.notification,
463 StatsLog.BUBBLE_UICHANGED__ACTION__HEADER_GO_TO_APP);
Mady Mellor9801e852019-01-22 14:50:28 -0800464 } catch (PendingIntent.CanceledException e) {
465 Log.w(TAG, "Failed to send intent for bubble with key: "
466 + (mEntry != null ? mEntry.key : " null entry"));
467 }
468 });
469 } else if (id == R.id.settings_button) {
470 Intent intent = getSettingsIntent(mEntry.notification.getPackageName(),
471 mEntry.notification.getUid());
Steven Wub00225b2019-02-08 14:27:42 -0500472 mStackView.collapseStack(() -> {
473 mContext.startActivity(intent);
474 logBubbleClickEvent(mEntry.notification,
475 StatsLog.BUBBLE_UICHANGED__ACTION__HEADER_GO_TO_SETTINGS);
476 });
Mady Mellore8e07712019-01-23 12:45:33 -0800477 } else if (id == R.id.no_bubbles_button) {
478 setBubblesAllowed(false);
479 } else if (id == R.id.yes_bubbles_button) {
480 setBubblesAllowed(true);
481 }
482 }
483
484 private void setBubblesAllowed(boolean allowed) {
485 try {
486 mNotificationManagerService.setBubblesAllowed(
487 mEntry.notification.getPackageName(),
488 mEntry.notification.getUid(),
489 allowed);
490 if (allowed) {
491 mPermissionView.setVisibility(GONE);
492 mHeaderView.setVisibility(VISIBLE);
493 } else if (mOnBubbleBlockedListener != null) {
494 mOnBubbleBlockedListener.onBubbleBlocked(mEntry);
495 }
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800496 mStackView.onExpandedHeightChanged();
Steven Wub00225b2019-02-08 14:27:42 -0500497 logBubbleClickEvent(mEntry.notification,
498 allowed ? StatsLog.BUBBLE_UICHANGED__ACTION__PERMISSION_OPT_IN :
499 StatsLog.BUBBLE_UICHANGED__ACTION__PERMISSION_OPT_OUT);
Mady Mellore8e07712019-01-23 12:45:33 -0800500 } catch (RemoteException e) {
501 Log.w(TAG, e);
Mady Mellor9801e852019-01-22 14:50:28 -0800502 }
Mady Mellordea7ecf2018-12-10 15:47:40 -0800503 }
504
505 /**
Mady Mellor3dff9e62019-02-05 18:12:53 -0800506 * Update appearance of the expanded view being displayed.
507 */
508 public void updateView() {
509 if (usingActivityView()
510 && mActivityView.getVisibility() == VISIBLE
511 && mActivityView.isAttachedToWindow()) {
512 mActivityView.onLocationChanged();
513 } else if (mNotifRow != null) {
514 applyRowState(mNotifRow);
515 }
Mady Mellorfe7ec032019-01-30 17:32:49 -0800516 updateHeight();
Mady Mellor3dff9e62019-02-05 18:12:53 -0800517 }
518
519 /**
Mady Mellordea7ecf2018-12-10 15:47:40 -0800520 * Set the x position that the tip of the triangle should point to.
521 */
Mady Mellor3dff9e62019-02-05 18:12:53 -0800522 public void setPointerPosition(float x) {
Mady Mellordea7ecf2018-12-10 15:47:40 -0800523 // Adjust for the pointer size
Mady Mellor3dff9e62019-02-05 18:12:53 -0800524 x -= (mPointerView.getWidth() / 2f);
Mady Mellordea7ecf2018-12-10 15:47:40 -0800525 mPointerView.setTranslationX(x);
526 }
527
528 /**
Mady Mellor3dff9e62019-02-05 18:12:53 -0800529 * Removes and releases an ActivityView if one was previously created for this bubble.
Mady Mellordea7ecf2018-12-10 15:47:40 -0800530 */
Mady Mellor94d94a72019-03-05 18:16:59 -0800531 public void cleanUpExpandedState() {
532 removeView(mNotifRow);
533
Mady Mellor3dff9e62019-02-05 18:12:53 -0800534 if (mActivityView == null) {
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500535 return;
536 }
Mark Renouf28c250d2019-02-25 16:47:34 -0500537 if (mActivityViewReady) {
538 mActivityView.release();
Mady Mellordea7ecf2018-12-10 15:47:40 -0800539 }
Mark Renouf28c250d2019-02-25 16:47:34 -0500540 removeView(mActivityView);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800541 mActivityView = null;
542 mActivityViewReady = false;
Mady Mellordea7ecf2018-12-10 15:47:40 -0800543 }
544
Mady Mellor3dff9e62019-02-05 18:12:53 -0800545 private boolean usingActivityView() {
546 return mBubbleIntent != null;
547 }
548
549 private void applyRowState(ExpandableNotificationRow view) {
550 view.reset();
551 view.setHeadsUp(false);
552 view.resetTranslation();
553 view.setOnKeyguard(false);
554 view.setOnAmbient(false);
555 view.setClipBottomAmount(0);
556 view.setClipTopAmount(0);
557 view.setContentTransformationAmount(0, false);
558 view.setIconsVisible(true);
559
560 // TODO - Need to reset this (and others) when view goes back in shade, leave for now
561 // view.setTopRoundness(1, false);
562 // view.setBottomRoundness(1, false);
563
564 ExpandableViewState viewState = view.getViewState();
565 viewState = viewState == null ? new ExpandableViewState() : viewState;
566 viewState.height = view.getIntrinsicHeight();
567 viewState.gone = false;
568 viewState.hidden = false;
569 viewState.dimmed = false;
570 viewState.dark = false;
571 viewState.alpha = 1f;
572 viewState.notGoneIndex = -1;
573 viewState.xTranslation = 0;
574 viewState.yTranslation = 0;
575 viewState.zTranslation = 0;
576 viewState.scaleX = 1;
577 viewState.scaleY = 1;
578 viewState.inShelf = true;
579 viewState.headsUpIsVisible = false;
580 viewState.applyToView(view);
Mady Mellordea7ecf2018-12-10 15:47:40 -0800581 }
Mady Mellor9801e852019-01-22 14:50:28 -0800582
583 private Intent getSettingsIntent(String packageName, final int appUid) {
584 final Intent intent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
585 intent.putExtra(Settings.EXTRA_APP_PACKAGE, packageName);
586 intent.putExtra(Settings.EXTRA_APP_UID, appUid);
587 intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
588 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
589 return intent;
590 }
Mady Mellore8e07712019-01-23 12:45:33 -0800591
Mady Mellor3dff9e62019-02-05 18:12:53 -0800592 @Nullable
593 private PendingIntent getBubbleIntent(NotificationEntry entry) {
594 Notification notif = entry.notification.getNotification();
595 String packageName = entry.notification.getPackageName();
596 Notification.BubbleMetadata data = notif.getBubbleMetadata();
597 if (data != null && canLaunchInActivityView(data.getIntent(), true /* enableLogging */,
598 packageName)) {
599 return data.getIntent();
600 } else if (BubbleController.shouldUseContentIntent(mContext)
601 && canLaunchInActivityView(notif.contentIntent, false /* enableLogging */,
602 packageName)) {
603 return notif.contentIntent;
604 }
605 return null;
606 }
607
608 /**
609 * Whether an intent is properly configured to display in an {@link android.app.ActivityView}.
610 *
611 * @param intent the pending intent of the bubble.
612 * @param enableLogging whether bubble developer error should be logged.
613 * @param packageName the notification package name for this bubble.
614 * @return
615 */
616 private boolean canLaunchInActivityView(PendingIntent intent, boolean enableLogging,
617 String packageName) {
618 if (intent == null) {
619 return false;
620 }
621 ActivityInfo info =
622 intent.getIntent().resolveActivityInfo(mContext.getPackageManager(), 0);
623 if (info == null) {
624 if (enableLogging) {
625 StatsLog.write(StatsLog.BUBBLE_DEVELOPER_ERROR_REPORTED, packageName,
626 BUBBLE_DEVELOPER_ERROR_REPORTED__ERROR__ACTIVITY_INFO_MISSING);
627 }
628 return false;
629 }
630 if (!ActivityInfo.isResizeableMode(info.resizeMode)) {
631 if (enableLogging) {
632 StatsLog.write(StatsLog.BUBBLE_DEVELOPER_ERROR_REPORTED, packageName,
633 BUBBLE_DEVELOPER_ERROR_REPORTED__ERROR__ACTIVITY_INFO_NOT_RESIZABLE);
634 }
635 return false;
636 }
637 if (info.documentLaunchMode != DOCUMENT_LAUNCH_ALWAYS) {
638 if (enableLogging) {
639 StatsLog.write(StatsLog.BUBBLE_DEVELOPER_ERROR_REPORTED, packageName,
640 BUBBLE_DEVELOPER_ERROR_REPORTED__ERROR__DOCUMENT_LAUNCH_NOT_ALWAYS);
641 }
642 return false;
643 }
644 return (info.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) != 0;
645 }
646
Mady Mellore8e07712019-01-23 12:45:33 -0800647 /**
648 * Listener that is notified when a bubble is blocked.
649 */
650 public interface OnBubbleBlockedListener {
651 /**
652 * Called when a bubble is blocked for the provided entry.
653 */
654 void onBubbleBlocked(NotificationEntry entry);
655 }
Steven Wub00225b2019-02-08 14:27:42 -0500656
657 /**
658 * Logs bubble UI click event.
659 *
660 * @param notification the bubble notification that user is interacting with.
661 * @param action the user interaction enum.
662 */
663 private void logBubbleClickEvent(StatusBarNotification notification, int action) {
664 StatsLog.write(StatsLog.BUBBLE_UI_CHANGED,
665 notification.getPackageName(),
666 notification.getNotification().getChannelId(),
667 notification.getId(),
668 mStackView.getBubbleIndex(mStackView.getExpandedBubble()),
669 mStackView.getBubbleCount(),
670 action,
671 mStackView.getNormalizedXPosition(),
672 mStackView.getNormalizedYPosition());
673 }
Mady Mellordea7ecf2018-12-10 15:47:40 -0800674}