blob: 89bcea461ce4907b3fdda84b10cc0ce27f482f00 [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 Mellor7af771a2019-03-07 15:04:54 -080046import android.os.UserHandle;
Mady Mellor9801e852019-01-22 14:50:28 -080047import android.provider.Settings;
Steven Wub00225b2019-02-08 14:27:42 -050048import android.service.notification.StatusBarNotification;
Mady Mellordea7ecf2018-12-10 15:47:40 -080049import android.util.AttributeSet;
Mady Mellor9801e852019-01-22 14:50:28 -080050import android.util.Log;
Steven Wub00225b2019-02-08 14:27:42 -050051import android.util.StatsLog;
Mady Mellordea7ecf2018-12-10 15:47:40 -080052import android.view.View;
Mady Mellor5029fa62019-03-05 12:16:21 -080053import android.view.ViewGroup;
Mady Mellor3dff9e62019-02-05 18:12:53 -080054import android.view.WindowInsets;
Mady Mellore8e07712019-01-23 12:45:33 -080055import android.widget.FrameLayout;
Mady Mellor9801e852019-01-22 14:50:28 -080056import android.widget.ImageButton;
Mady Mellore8e07712019-01-23 12:45:33 -080057import android.widget.ImageView;
Mady Mellordea7ecf2018-12-10 15:47:40 -080058import android.widget.LinearLayout;
Mark Renouf89b1a4a2018-12-04 14:59:45 -050059import android.widget.TextView;
Mady Mellordea7ecf2018-12-10 15:47:40 -080060
Mady Mellor3dff9e62019-02-05 18:12:53 -080061import com.android.systemui.Dependency;
Mady Mellore8e07712019-01-23 12:45:33 -080062import com.android.systemui.Interpolators;
Mady Mellordea7ecf2018-12-10 15:47:40 -080063import com.android.systemui.R;
64import com.android.systemui.recents.TriangleShape;
Mady Mellor9801e852019-01-22 14:50:28 -080065import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Mady Mellor3dff9e62019-02-05 18:12:53 -080066import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow;
67import com.android.systemui.statusbar.notification.stack.ExpandableViewState;
Mady Mellordea7ecf2018-12-10 15:47:40 -080068
69/**
70 * Container for the expanded bubble view, handles rendering the caret and header of the view.
71 */
Mady Mellor3d82e682019-02-05 13:34:48 -080072public class BubbleExpandedView extends LinearLayout implements View.OnClickListener {
Mady Mellor9801e852019-01-22 14:50:28 -080073 private static final String TAG = "BubbleExpandedView";
Mady Mellordea7ecf2018-12-10 15:47:40 -080074
Mady Mellor44ee2fe2019-01-30 17:51:16 -080075 // Configurable via bubble settings; just for testing
76 private boolean mUseFooter;
77 private boolean mShowOnTop;
78
Mady Mellordea7ecf2018-12-10 15:47:40 -080079 // The triangle pointing to the expanded view
80 private View mPointerView;
Mady Mellor44ee2fe2019-01-30 17:51:16 -080081 private int mPointerMargin;
Mady Mellore8e07712019-01-23 12:45:33 -080082
83 // Header
84 private View mHeaderView;
Mady Mellor9801e852019-01-22 14:50:28 -080085 private ImageButton mDeepLinkIcon;
Mady Mellor9801e852019-01-22 14:50:28 -080086 private ImageButton mSettingsIcon;
Mady Mellore8e07712019-01-23 12:45:33 -080087
88 // Permission view
89 private View mPermissionView;
90
Mady Mellor3dff9e62019-02-05 18:12:53 -080091 // Views for expanded state
92 private ExpandableNotificationRow mNotifRow;
93 private ActivityView mActivityView;
94
95 private boolean mActivityViewReady = false;
96 private PendingIntent mBubbleIntent;
97
Mady Mellor5d8f1402019-02-21 18:23:52 -080098 private boolean mKeyboardVisible;
99 private boolean mNeedsNewHeight;
100
Mady Mellorfe7ec032019-01-30 17:32:49 -0800101 private int mMinHeight;
102 private int mHeaderHeight;
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800103 private int mBubbleHeight;
104 private int mPermissionHeight;
Mady Mellordea7ecf2018-12-10 15:47:40 -0800105
Mady Mellor9801e852019-01-22 14:50:28 -0800106 private NotificationEntry mEntry;
107 private PackageManager mPm;
108 private String mAppName;
Mady Mellore8e07712019-01-23 12:45:33 -0800109 private Drawable mAppIcon;
110
111 private INotificationManager mNotificationManagerService;
Mady Mellor3dff9e62019-02-05 18:12:53 -0800112 private BubbleController mBubbleController = Dependency.get(BubbleController.class);
Mady Mellor9801e852019-01-22 14:50:28 -0800113
Mady Mellor9801e852019-01-22 14:50:28 -0800114 private BubbleStackView mStackView;
115
Mady Mellor3dff9e62019-02-05 18:12:53 -0800116 private BubbleExpandedView.OnBubbleBlockedListener mOnBubbleBlockedListener;
117
118 private ActivityView.StateCallback mStateCallback = new ActivityView.StateCallback() {
119 @Override
120 public void onActivityViewReady(ActivityView view) {
Mady Mellor6d002032019-02-13 13:45:17 -0800121 if (!mActivityViewReady) {
122 mActivityViewReady = true;
123 mActivityView.startActivity(mBubbleIntent);
124 }
Mady Mellor3dff9e62019-02-05 18:12:53 -0800125 }
126
127 @Override
128 public void onActivityViewDestroyed(ActivityView view) {
129 mActivityViewReady = false;
130 }
131
132 /**
133 * This is only called for tasks on this ActivityView, which is also set to
134 * single-task mode -- meaning never more than one task on this display. If a task
135 * is being removed, it's the top Activity finishing and this bubble should
136 * be removed or collapsed.
137 */
138 @Override
139 public void onTaskRemovalStarted(int taskId) {
140 if (mEntry != null) {
141 // Must post because this is called from a binder thread.
Mark Renouf08bc42a2019-03-07 13:01:59 -0500142 post(() -> mBubbleController.removeBubble(mEntry.key,
143 BubbleController.DISMISS_TASK_FINISHED));
Mady Mellor3dff9e62019-02-05 18:12:53 -0800144 }
145 }
146 };
Mady Mellore8e07712019-01-23 12:45:33 -0800147
Mady Mellor3d82e682019-02-05 13:34:48 -0800148 public BubbleExpandedView(Context context) {
Mady Mellordea7ecf2018-12-10 15:47:40 -0800149 this(context, null);
150 }
151
Mady Mellor3d82e682019-02-05 13:34:48 -0800152 public BubbleExpandedView(Context context, AttributeSet attrs) {
Mady Mellordea7ecf2018-12-10 15:47:40 -0800153 this(context, attrs, 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 this(context, attrs, defStyleAttr, 0);
158 }
159
Mady Mellor3d82e682019-02-05 13:34:48 -0800160 public BubbleExpandedView(Context context, AttributeSet attrs, int defStyleAttr,
Mady Mellordea7ecf2018-12-10 15:47:40 -0800161 int defStyleRes) {
162 super(context, attrs, defStyleAttr, defStyleRes);
Mady Mellor9801e852019-01-22 14:50:28 -0800163 mPm = context.getPackageManager();
Mady Mellorfe7ec032019-01-30 17:32:49 -0800164 mMinHeight = getResources().getDimensionPixelSize(
Mady Mellor3dff9e62019-02-05 18:12:53 -0800165 R.dimen.bubble_expanded_default_height);
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800166 mPointerMargin = getResources().getDimensionPixelSize(R.dimen.bubble_pointer_margin);
Mady Mellore8e07712019-01-23 12:45:33 -0800167 try {
168 mNotificationManagerService = INotificationManager.Stub.asInterface(
169 ServiceManager.getServiceOrThrow(Context.NOTIFICATION_SERVICE));
170 } catch (ServiceManager.ServiceNotFoundException e) {
171 Log.w(TAG, e);
172 }
Mady Mellordea7ecf2018-12-10 15:47:40 -0800173 }
174
175 @Override
176 protected void onFinishInflate() {
177 super.onFinishInflate();
178
179 Resources res = getResources();
180 mPointerView = findViewById(R.id.pointer_view);
181 int width = res.getDimensionPixelSize(R.dimen.bubble_pointer_width);
182 int height = res.getDimensionPixelSize(R.dimen.bubble_pointer_height);
Mady Mellordd497052019-01-30 17:23:48 -0800183
184 TypedArray ta = getContext().obtainStyledAttributes(
185 new int[] {android.R.attr.colorBackgroundFloating});
186 int bgColor = ta.getColor(0, Color.WHITE);
187 ta.recycle();
188
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800189 mShowOnTop = BubbleController.showBubblesAtTop(getContext());
190 mUseFooter = BubbleController.useFooter(getContext());
191
Mady Mellordea7ecf2018-12-10 15:47:40 -0800192 ShapeDrawable triangleDrawable = new ShapeDrawable(
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800193 TriangleShape.create(width, height, mShowOnTop /* pointUp */));
Mady Mellordd497052019-01-30 17:23:48 -0800194 triangleDrawable.setTint(bgColor);
Mady Mellordea7ecf2018-12-10 15:47:40 -0800195 mPointerView.setBackground(triangleDrawable);
Mady Mellor9801e852019-01-22 14:50:28 -0800196
Mady Mellore8e07712019-01-23 12:45:33 -0800197 FrameLayout viewWrapper = findViewById(R.id.header_permission_wrapper);
Mady Mellor310c7c62019-02-21 17:03:08 -0800198 viewWrapper.setBackground(createHeaderPermissionBackground(bgColor));
199
Mady Mellore8e07712019-01-23 12:45:33 -0800200 LayoutTransition transition = new LayoutTransition();
201 transition.setDuration(200);
202
203 ObjectAnimator appearAnimator = ObjectAnimator.ofFloat(null, View.ALPHA, 0f, 1f);
204 transition.setAnimator(LayoutTransition.APPEARING, appearAnimator);
205 transition.setInterpolator(LayoutTransition.APPEARING, Interpolators.ALPHA_IN);
206
207 ObjectAnimator disappearAnimator = ObjectAnimator.ofFloat(null, View.ALPHA, 1f, 0f);
208 transition.setAnimator(LayoutTransition.DISAPPEARING, disappearAnimator);
209 transition.setInterpolator(LayoutTransition.DISAPPEARING, Interpolators.ALPHA_OUT);
210
211 transition.setAnimateParentHierarchy(false);
212 viewWrapper.setLayoutTransition(transition);
213 viewWrapper.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
214
Mady Mellor310c7c62019-02-21 17:03:08 -0800215
Mady Mellorfe7ec032019-01-30 17:32:49 -0800216 mHeaderHeight = getContext().getResources().getDimensionPixelSize(
217 R.dimen.bubble_expanded_header_height);
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800218 mPermissionHeight = getContext().getResources().getDimensionPixelSize(
219 R.dimen.bubble_permission_height);
Mady Mellore8e07712019-01-23 12:45:33 -0800220 mHeaderView = findViewById(R.id.header_layout);
Mady Mellor9801e852019-01-22 14:50:28 -0800221 mDeepLinkIcon = findViewById(R.id.deep_link_button);
222 mSettingsIcon = findViewById(R.id.settings_button);
223 mDeepLinkIcon.setOnClickListener(this);
224 mSettingsIcon.setOnClickListener(this);
Mady Mellore8e07712019-01-23 12:45:33 -0800225
226 mPermissionView = findViewById(R.id.permission_layout);
227 findViewById(R.id.no_bubbles_button).setOnClickListener(this);
228 findViewById(R.id.yes_bubbles_button).setOnClickListener(this);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800229
230 mActivityView = new ActivityView(mContext, null /* attrs */, 0 /* defStyle */,
231 true /* singleTaskInstance */);
232 addView(mActivityView);
233
Mady Mellor5d8f1402019-02-21 18:23:52 -0800234 setOnApplyWindowInsetsListener((View view, WindowInsets insets) -> {
235 // Keep track of IME displaying because we should not make any adjustments that might
236 // cause a config change while the IME is displayed otherwise it'll loose focus.
Mady Mellor3dff9e62019-02-05 18:12:53 -0800237 final int keyboardHeight = insets.getSystemWindowInsetBottom()
238 - insets.getStableInsetBottom();
Mady Mellor5d8f1402019-02-21 18:23:52 -0800239 mKeyboardVisible = keyboardHeight != 0;
240 if (!mKeyboardVisible && mNeedsNewHeight) {
241 updateHeight();
242 }
Mady Mellor3dff9e62019-02-05 18:12:53 -0800243 return view.onApplyWindowInsets(insets);
244 });
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800245
246 if (!mShowOnTop) {
247 removeView(mPointerView);
248 if (mUseFooter) {
Mady Mellor310c7c62019-02-21 17:03:08 -0800249 View divider = findViewById(R.id.divider);
250 viewWrapper.removeView(divider);
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800251 removeView(viewWrapper);
Mady Mellor310c7c62019-02-21 17:03:08 -0800252 addView(divider);
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800253 addView(viewWrapper);
254 }
255 addView(mPointerView);
256 }
Mady Mellore8e07712019-01-23 12:45:33 -0800257 }
258
Mady Mellor5d8f1402019-02-21 18:23:52 -0800259 @Override
260 protected void onDetachedFromWindow() {
261 super.onDetachedFromWindow();
262 mKeyboardVisible = false;
263 mNeedsNewHeight = false;
264 if (mActivityView != null) {
265 mActivityView.setForwardedInsets(Insets.of(0, 0, 0, 0));
266 }
267 }
268
269 /**
270 * Called by {@link BubbleStackView} when the insets for the expanded state should be updated.
271 * This should be done post-move and post-animation.
272 */
273 void updateInsets(WindowInsets insets) {
274 if (usingActivityView()) {
275 Point displaySize = new Point();
276 mActivityView.getContext().getDisplay().getSize(displaySize);
277 int[] windowLocation = mActivityView.getLocationOnScreen();
278 final int windowBottom = windowLocation[1] + mActivityView.getHeight();
279 final int keyboardHeight = insets.getSystemWindowInsetBottom()
280 - insets.getStableInsetBottom();
281 final int insetsBottom = Math.max(0,
282 windowBottom + keyboardHeight - displaySize.y);
283 mActivityView.setForwardedInsets(Insets.of(0, 0, 0, insetsBottom));
284 }
285 }
286
Mady Mellore8e07712019-01-23 12:45:33 -0800287 /**
Mady Mellor310c7c62019-02-21 17:03:08 -0800288 * Creates a background with corners rounded based on how the view is configured to display
289 */
290 private Drawable createHeaderPermissionBackground(int bgColor) {
291 TypedArray ta2 = getContext().obtainStyledAttributes(
292 new int[] {android.R.attr.dialogCornerRadius});
293 final float cr = ta2.getDimension(0, 0f);
294 ta2.recycle();
295
296 float[] radii = mUseFooter
297 ? new float[] {0, 0, 0, 0, cr, cr, cr, cr}
298 : new float[] {cr, cr, cr, cr, 0, 0, 0, 0};
299 GradientDrawable chromeBackground = new GradientDrawable();
300 chromeBackground.setShape(GradientDrawable.RECTANGLE);
301 chromeBackground.setCornerRadii(radii);
302 chromeBackground.setColor(bgColor);
303 return chromeBackground;
304 }
305
306 /**
Mady Mellore8e07712019-01-23 12:45:33 -0800307 * Sets the listener to notify when a bubble has been blocked.
308 */
309 public void setOnBlockedListener(OnBubbleBlockedListener listener) {
310 mOnBubbleBlockedListener = listener;
Mady Mellor9801e852019-01-22 14:50:28 -0800311 }
312
313 /**
314 * Sets the notification entry used to populate this view.
315 */
316 public void setEntry(NotificationEntry entry, BubbleStackView stackView) {
317 mStackView = stackView;
318 mEntry = entry;
319
320 ApplicationInfo info;
321 try {
322 info = mPm.getApplicationInfo(
323 entry.notification.getPackageName(),
324 PackageManager.MATCH_UNINSTALLED_PACKAGES
325 | PackageManager.MATCH_DISABLED_COMPONENTS
326 | PackageManager.MATCH_DIRECT_BOOT_UNAWARE
327 | PackageManager.MATCH_DIRECT_BOOT_AWARE);
328 if (info != null) {
329 mAppName = String.valueOf(mPm.getApplicationLabel(info));
Mady Mellore8e07712019-01-23 12:45:33 -0800330 mAppIcon = mPm.getApplicationIcon(info);
Mady Mellor9801e852019-01-22 14:50:28 -0800331 }
332 } catch (PackageManager.NameNotFoundException e) {
333 // Ahh... just use package name
334 mAppName = entry.notification.getPackageName();
335 }
Mady Mellore8e07712019-01-23 12:45:33 -0800336 if (mAppIcon == null) {
337 mAppIcon = mPm.getDefaultActivityIcon();
338 }
Mady Mellor9801e852019-01-22 14:50:28 -0800339 updateHeaderView();
Mady Mellore8e07712019-01-23 12:45:33 -0800340 updatePermissionView();
Mady Mellor3dff9e62019-02-05 18:12:53 -0800341 updateExpandedView();
Mady Mellor6d002032019-02-13 13:45:17 -0800342 }
343
344 /**
345 * Lets activity view know it should be shown / populated.
346 */
Mady Mellor5029fa62019-03-05 12:16:21 -0800347 public void populateExpandedView() {
348 if (usingActivityView()) {
349 mActivityView.setCallback(mStateCallback);
350 } else {
351 // We're using notification template
352 ViewGroup parent = (ViewGroup) mNotifRow.getParent();
353 if (parent == this) {
354 // Already added
355 return;
356 } else if (parent != null) {
357 // Still in the shade... remove it
358 parent.removeView(mNotifRow);
359 }
360 if (mShowOnTop) {
361 addView(mNotifRow);
362 } else {
363 addView(mNotifRow, mUseFooter ? 0 : 1);
364 }
365 }
Mady Mellor9801e852019-01-22 14:50:28 -0800366 }
367
Mady Mellorfe7ec032019-01-30 17:32:49 -0800368 /**
369 * Updates the entry backing this view. This will not re-populate ActivityView, it will
370 * only update the deep-links in the header, the title, and the height of the view.
371 */
372 public void update(NotificationEntry entry) {
373 if (entry.key.equals(mEntry.key)) {
374 mEntry = entry;
375 updateHeaderView();
376 updateHeight();
377 } else {
378 Log.w(TAG, "Trying to update entry with different key, new entry: "
379 + entry.key + " old entry: " + mEntry.key);
380 }
381 }
382
Lyn Hanf1c9b8b2019-03-14 16:49:48 -0700383 /**
384 * Update bubble expanded view header when user toggles dark mode.
385 */
386 void updateHeaderColor() {
387 mHeaderView.setBackgroundColor(mContext.getColor(R.attr.colorAccent));
388 }
389
Mady Mellor9801e852019-01-22 14:50:28 -0800390 private void updateHeaderView() {
391 mSettingsIcon.setContentDescription(getResources().getString(
392 R.string.bubbles_settings_button_description, mAppName));
393 mDeepLinkIcon.setContentDescription(getResources().getString(
394 R.string.bubbles_deep_link_button_description, mAppName));
Mady Mellore8e07712019-01-23 12:45:33 -0800395 }
396
397 private void updatePermissionView() {
398 boolean hasUserApprovedBubblesForPackage = false;
399 try {
400 hasUserApprovedBubblesForPackage =
401 mNotificationManagerService.hasUserApprovedBubblesForPackage(
402 mEntry.notification.getPackageName(), mEntry.notification.getUid());
403 } catch (RemoteException e) {
404 Log.w(TAG, e);
405 }
406 if (hasUserApprovedBubblesForPackage) {
407 mHeaderView.setVisibility(VISIBLE);
408 mPermissionView.setVisibility(GONE);
409 } else {
410 mHeaderView.setVisibility(GONE);
411 mPermissionView.setVisibility(VISIBLE);
412 ((ImageView) mPermissionView.findViewById(R.id.pkgicon)).setImageDrawable(mAppIcon);
413 ((TextView) mPermissionView.findViewById(R.id.pkgname)).setText(mAppName);
Steven Wua62cb6a2019-02-15 17:12:51 -0500414 logBubbleClickEvent(mEntry.notification,
415 StatsLog.BUBBLE_UICHANGED__ACTION__PERMISSION_DIALOG_SHOWN);
Mady Mellor9801e852019-01-22 14:50:28 -0800416 }
417 }
418
Mady Mellor3dff9e62019-02-05 18:12:53 -0800419 private void updateExpandedView() {
420 mBubbleIntent = getBubbleIntent(mEntry);
421 if (mBubbleIntent != null) {
422 if (mNotifRow != null) {
423 // Clear out the row if we had it previously
424 removeView(mNotifRow);
425 mNotifRow = null;
426 }
Mady Mellor3dff9e62019-02-05 18:12:53 -0800427 mActivityView.setVisibility(VISIBLE);
428 } else {
429 // Hide activity view if we had it previously
430 mActivityView.setVisibility(GONE);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800431 mNotifRow = mEntry.getRow();
Mady Mellor5029fa62019-03-05 12:16:21 -0800432
Mady Mellor3dff9e62019-02-05 18:12:53 -0800433 }
434 updateView();
435 }
436
Mark Renouf041d7262019-02-06 12:09:41 -0500437 boolean performBackPressIfNeeded() {
438 if (mActivityView == null || !usingActivityView()) {
439 return false;
440 }
441 mActivityView.performBackPress();
442 return true;
443 }
444
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800445 /**
446 * @return total height that the expanded view occupies.
447 */
448 int getExpandedSize() {
449 int chromeHeight = mPermissionView.getVisibility() != View.VISIBLE
450 ? mHeaderHeight
451 : mPermissionHeight;
452 return mBubbleHeight + mPointerView.getHeight() + mPointerMargin
453 + chromeHeight;
454 }
455
Mady Mellorfe7ec032019-01-30 17:32:49 -0800456 void updateHeight() {
457 if (usingActivityView()) {
458 Notification.BubbleMetadata data = mEntry.getBubbleMetadata();
Mady Mellor7af771a2019-03-07 15:04:54 -0800459 float desiredHeight;
Mady Mellorfe7ec032019-01-30 17:32:49 -0800460 if (data == null) {
461 // This is a contentIntent based bubble, lets allow it to be the max height
462 // as it was forced into this mode and not prepared to be small
463 desiredHeight = mStackView.getMaxExpandedHeight();
464 } else {
Mady Mellor7af771a2019-03-07 15:04:54 -0800465 boolean useRes = data.getDesiredHeightResId() != 0;
466 float desiredPx;
467 if (useRes) {
468 desiredPx = getDimenForPackageUser(data.getDesiredHeightResId(),
469 mEntry.notification.getPackageName(),
470 mEntry.notification.getUser().getIdentifier());
471 } else {
472 desiredPx = data.getDesiredHeight()
473 * getContext().getResources().getDisplayMetrics().density;
474 }
475 desiredHeight = desiredPx > 0 ? desiredPx : mMinHeight;
Mady Mellorfe7ec032019-01-30 17:32:49 -0800476 }
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800477 int chromeHeight = mPermissionView.getVisibility() != View.VISIBLE
478 ? mHeaderHeight
479 : mPermissionHeight;
480 int max = mStackView.getMaxExpandedHeight() - chromeHeight - mPointerView.getHeight()
481 - mPointerMargin;
Mady Mellor7af771a2019-03-07 15:04:54 -0800482 float height = Math.min(desiredHeight, max);
Mady Mellorfe7ec032019-01-30 17:32:49 -0800483 height = Math.max(height, mMinHeight);
484 LayoutParams lp = (LayoutParams) mActivityView.getLayoutParams();
Mady Mellor5d8f1402019-02-21 18:23:52 -0800485 mNeedsNewHeight = lp.height != height;
486 if (!mKeyboardVisible) {
487 // If the keyboard is visible... don't adjust the height because that will cause
488 // a configuration change and the keyboard will be lost.
Mady Mellor7af771a2019-03-07 15:04:54 -0800489 lp.height = (int) height;
490 mBubbleHeight = (int) height;
Mady Mellor5d8f1402019-02-21 18:23:52 -0800491 mActivityView.setLayoutParams(lp);
492 mNeedsNewHeight = false;
493 }
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800494 } else {
495 mBubbleHeight = mNotifRow != null ? mNotifRow.getIntrinsicHeight() : mMinHeight;
Mady Mellorfe7ec032019-01-30 17:32:49 -0800496 }
497 }
498
Mady Mellor9801e852019-01-22 14:50:28 -0800499 @Override
500 public void onClick(View view) {
501 if (mEntry == null) {
502 return;
503 }
504 Notification n = mEntry.notification.getNotification();
505 int id = view.getId();
506 if (id == R.id.deep_link_button) {
507 mStackView.collapseStack(() -> {
508 try {
509 n.contentIntent.send();
Steven Wub00225b2019-02-08 14:27:42 -0500510 logBubbleClickEvent(mEntry.notification,
511 StatsLog.BUBBLE_UICHANGED__ACTION__HEADER_GO_TO_APP);
Mady Mellor9801e852019-01-22 14:50:28 -0800512 } catch (PendingIntent.CanceledException e) {
513 Log.w(TAG, "Failed to send intent for bubble with key: "
514 + (mEntry != null ? mEntry.key : " null entry"));
515 }
516 });
517 } else if (id == R.id.settings_button) {
518 Intent intent = getSettingsIntent(mEntry.notification.getPackageName(),
519 mEntry.notification.getUid());
Steven Wub00225b2019-02-08 14:27:42 -0500520 mStackView.collapseStack(() -> {
521 mContext.startActivity(intent);
522 logBubbleClickEvent(mEntry.notification,
523 StatsLog.BUBBLE_UICHANGED__ACTION__HEADER_GO_TO_SETTINGS);
524 });
Mady Mellore8e07712019-01-23 12:45:33 -0800525 } else if (id == R.id.no_bubbles_button) {
526 setBubblesAllowed(false);
527 } else if (id == R.id.yes_bubbles_button) {
528 setBubblesAllowed(true);
529 }
530 }
531
532 private void setBubblesAllowed(boolean allowed) {
533 try {
534 mNotificationManagerService.setBubblesAllowed(
535 mEntry.notification.getPackageName(),
536 mEntry.notification.getUid(),
537 allowed);
538 if (allowed) {
539 mPermissionView.setVisibility(GONE);
540 mHeaderView.setVisibility(VISIBLE);
541 } else if (mOnBubbleBlockedListener != null) {
542 mOnBubbleBlockedListener.onBubbleBlocked(mEntry);
543 }
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800544 mStackView.onExpandedHeightChanged();
Steven Wub00225b2019-02-08 14:27:42 -0500545 logBubbleClickEvent(mEntry.notification,
546 allowed ? StatsLog.BUBBLE_UICHANGED__ACTION__PERMISSION_OPT_IN :
547 StatsLog.BUBBLE_UICHANGED__ACTION__PERMISSION_OPT_OUT);
Mady Mellore8e07712019-01-23 12:45:33 -0800548 } catch (RemoteException e) {
549 Log.w(TAG, e);
Mady Mellor9801e852019-01-22 14:50:28 -0800550 }
Mady Mellordea7ecf2018-12-10 15:47:40 -0800551 }
552
553 /**
Mady Mellor3dff9e62019-02-05 18:12:53 -0800554 * Update appearance of the expanded view being displayed.
555 */
556 public void updateView() {
557 if (usingActivityView()
558 && mActivityView.getVisibility() == VISIBLE
559 && mActivityView.isAttachedToWindow()) {
560 mActivityView.onLocationChanged();
561 } else if (mNotifRow != null) {
562 applyRowState(mNotifRow);
563 }
Mady Mellorfe7ec032019-01-30 17:32:49 -0800564 updateHeight();
Mady Mellor3dff9e62019-02-05 18:12:53 -0800565 }
566
567 /**
Mady Mellordea7ecf2018-12-10 15:47:40 -0800568 * Set the x position that the tip of the triangle should point to.
569 */
Mady Mellor3dff9e62019-02-05 18:12:53 -0800570 public void setPointerPosition(float x) {
Mady Mellordea7ecf2018-12-10 15:47:40 -0800571 // Adjust for the pointer size
Mady Mellor3dff9e62019-02-05 18:12:53 -0800572 x -= (mPointerView.getWidth() / 2f);
Mady Mellordea7ecf2018-12-10 15:47:40 -0800573 mPointerView.setTranslationX(x);
574 }
575
576 /**
Mady Mellor3dff9e62019-02-05 18:12:53 -0800577 * Removes and releases an ActivityView if one was previously created for this bubble.
Mady Mellordea7ecf2018-12-10 15:47:40 -0800578 */
Mady Mellor94d94a72019-03-05 18:16:59 -0800579 public void cleanUpExpandedState() {
580 removeView(mNotifRow);
581
Mady Mellor3dff9e62019-02-05 18:12:53 -0800582 if (mActivityView == null) {
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500583 return;
584 }
Mark Renouf28c250d2019-02-25 16:47:34 -0500585 if (mActivityViewReady) {
586 mActivityView.release();
Mady Mellordea7ecf2018-12-10 15:47:40 -0800587 }
Mark Renouf28c250d2019-02-25 16:47:34 -0500588 removeView(mActivityView);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800589 mActivityView = null;
590 mActivityViewReady = false;
Mady Mellordea7ecf2018-12-10 15:47:40 -0800591 }
592
Mady Mellor3dff9e62019-02-05 18:12:53 -0800593 private boolean usingActivityView() {
594 return mBubbleIntent != null;
595 }
596
597 private void applyRowState(ExpandableNotificationRow view) {
598 view.reset();
599 view.setHeadsUp(false);
600 view.resetTranslation();
601 view.setOnKeyguard(false);
602 view.setOnAmbient(false);
603 view.setClipBottomAmount(0);
604 view.setClipTopAmount(0);
605 view.setContentTransformationAmount(0, false);
606 view.setIconsVisible(true);
607
608 // TODO - Need to reset this (and others) when view goes back in shade, leave for now
609 // view.setTopRoundness(1, false);
610 // view.setBottomRoundness(1, false);
611
612 ExpandableViewState viewState = view.getViewState();
613 viewState = viewState == null ? new ExpandableViewState() : viewState;
614 viewState.height = view.getIntrinsicHeight();
615 viewState.gone = false;
616 viewState.hidden = false;
617 viewState.dimmed = false;
618 viewState.dark = false;
619 viewState.alpha = 1f;
620 viewState.notGoneIndex = -1;
621 viewState.xTranslation = 0;
622 viewState.yTranslation = 0;
623 viewState.zTranslation = 0;
624 viewState.scaleX = 1;
625 viewState.scaleY = 1;
626 viewState.inShelf = true;
627 viewState.headsUpIsVisible = false;
628 viewState.applyToView(view);
Mady Mellordea7ecf2018-12-10 15:47:40 -0800629 }
Mady Mellor9801e852019-01-22 14:50:28 -0800630
631 private Intent getSettingsIntent(String packageName, final int appUid) {
632 final Intent intent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
633 intent.putExtra(Settings.EXTRA_APP_PACKAGE, packageName);
634 intent.putExtra(Settings.EXTRA_APP_UID, appUid);
635 intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
636 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
637 return intent;
638 }
Mady Mellore8e07712019-01-23 12:45:33 -0800639
Mady Mellor3dff9e62019-02-05 18:12:53 -0800640 @Nullable
641 private PendingIntent getBubbleIntent(NotificationEntry entry) {
642 Notification notif = entry.notification.getNotification();
643 String packageName = entry.notification.getPackageName();
644 Notification.BubbleMetadata data = notif.getBubbleMetadata();
645 if (data != null && canLaunchInActivityView(data.getIntent(), true /* enableLogging */,
646 packageName)) {
647 return data.getIntent();
648 } else if (BubbleController.shouldUseContentIntent(mContext)
649 && canLaunchInActivityView(notif.contentIntent, false /* enableLogging */,
650 packageName)) {
651 return notif.contentIntent;
652 }
653 return null;
654 }
655
656 /**
657 * Whether an intent is properly configured to display in an {@link android.app.ActivityView}.
658 *
659 * @param intent the pending intent of the bubble.
660 * @param enableLogging whether bubble developer error should be logged.
661 * @param packageName the notification package name for this bubble.
662 * @return
663 */
664 private boolean canLaunchInActivityView(PendingIntent intent, boolean enableLogging,
665 String packageName) {
666 if (intent == null) {
667 return false;
668 }
669 ActivityInfo info =
670 intent.getIntent().resolveActivityInfo(mContext.getPackageManager(), 0);
671 if (info == null) {
672 if (enableLogging) {
673 StatsLog.write(StatsLog.BUBBLE_DEVELOPER_ERROR_REPORTED, packageName,
674 BUBBLE_DEVELOPER_ERROR_REPORTED__ERROR__ACTIVITY_INFO_MISSING);
675 }
676 return false;
677 }
678 if (!ActivityInfo.isResizeableMode(info.resizeMode)) {
679 if (enableLogging) {
680 StatsLog.write(StatsLog.BUBBLE_DEVELOPER_ERROR_REPORTED, packageName,
681 BUBBLE_DEVELOPER_ERROR_REPORTED__ERROR__ACTIVITY_INFO_NOT_RESIZABLE);
682 }
683 return false;
684 }
685 if (info.documentLaunchMode != DOCUMENT_LAUNCH_ALWAYS) {
686 if (enableLogging) {
687 StatsLog.write(StatsLog.BUBBLE_DEVELOPER_ERROR_REPORTED, packageName,
688 BUBBLE_DEVELOPER_ERROR_REPORTED__ERROR__DOCUMENT_LAUNCH_NOT_ALWAYS);
689 }
690 return false;
691 }
692 return (info.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) != 0;
693 }
694
Mady Mellore8e07712019-01-23 12:45:33 -0800695 /**
696 * Listener that is notified when a bubble is blocked.
697 */
698 public interface OnBubbleBlockedListener {
699 /**
700 * Called when a bubble is blocked for the provided entry.
701 */
702 void onBubbleBlocked(NotificationEntry entry);
703 }
Steven Wub00225b2019-02-08 14:27:42 -0500704
705 /**
706 * Logs bubble UI click event.
707 *
708 * @param notification the bubble notification that user is interacting with.
709 * @param action the user interaction enum.
710 */
711 private void logBubbleClickEvent(StatusBarNotification notification, int action) {
712 StatsLog.write(StatsLog.BUBBLE_UI_CHANGED,
713 notification.getPackageName(),
714 notification.getNotification().getChannelId(),
715 notification.getId(),
716 mStackView.getBubbleIndex(mStackView.getExpandedBubble()),
717 mStackView.getBubbleCount(),
718 action,
719 mStackView.getNormalizedXPosition(),
720 mStackView.getNormalizedYPosition());
721 }
Mady Mellor7af771a2019-03-07 15:04:54 -0800722
723 private int getDimenForPackageUser(int resId, String pkg, int userId) {
724 Resources r;
725 if (pkg != null) {
726 try {
727 if (userId == UserHandle.USER_ALL) {
728 userId = UserHandle.USER_SYSTEM;
729 }
730 r = mPm.getResourcesForApplicationAsUser(pkg, userId);
731 return r.getDimensionPixelSize(resId);
732 } catch (PackageManager.NameNotFoundException ex) {
733 // Uninstalled, don't care
734 } catch (Resources.NotFoundException e) {
735 // Invalid res id, return 0 and user our default
736 Log.e(TAG, "Couldn't find desired height res id", e);
737 }
738 }
739 return 0;
740 }
Mady Mellordea7ecf2018-12-10 15:47:40 -0800741}