blob: 1ab28abcaebf5e07c657baa879dc25046a88be76 [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);
Mady Mellor81f734c2019-03-20 18:42:50 -0700414 ((TextView) mPermissionView.findViewById(R.id.prompt)).setText(
415 getResources().getString(R.string.bubbles_prompt, mAppName));
Steven Wu45e38ae2019-03-25 16:16:59 -0400416 logBubbleClickEvent(mEntry,
Steven Wua62cb6a2019-02-15 17:12:51 -0500417 StatsLog.BUBBLE_UICHANGED__ACTION__PERMISSION_DIALOG_SHOWN);
Mady Mellor9801e852019-01-22 14:50:28 -0800418 }
419 }
420
Mady Mellor3dff9e62019-02-05 18:12:53 -0800421 private void updateExpandedView() {
422 mBubbleIntent = getBubbleIntent(mEntry);
423 if (mBubbleIntent != null) {
424 if (mNotifRow != null) {
425 // Clear out the row if we had it previously
426 removeView(mNotifRow);
427 mNotifRow = null;
428 }
Mady Mellor3dff9e62019-02-05 18:12:53 -0800429 mActivityView.setVisibility(VISIBLE);
430 } else {
431 // Hide activity view if we had it previously
432 mActivityView.setVisibility(GONE);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800433 mNotifRow = mEntry.getRow();
Mady Mellor5029fa62019-03-05 12:16:21 -0800434
Mady Mellor3dff9e62019-02-05 18:12:53 -0800435 }
436 updateView();
437 }
438
Mark Renouf041d7262019-02-06 12:09:41 -0500439 boolean performBackPressIfNeeded() {
440 if (mActivityView == null || !usingActivityView()) {
441 return false;
442 }
443 mActivityView.performBackPress();
444 return true;
445 }
446
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800447 /**
448 * @return total height that the expanded view occupies.
449 */
450 int getExpandedSize() {
451 int chromeHeight = mPermissionView.getVisibility() != View.VISIBLE
452 ? mHeaderHeight
453 : mPermissionHeight;
454 return mBubbleHeight + mPointerView.getHeight() + mPointerMargin
455 + chromeHeight;
456 }
457
Mady Mellorfe7ec032019-01-30 17:32:49 -0800458 void updateHeight() {
459 if (usingActivityView()) {
460 Notification.BubbleMetadata data = mEntry.getBubbleMetadata();
Mady Mellor7af771a2019-03-07 15:04:54 -0800461 float desiredHeight;
Mady Mellorfe7ec032019-01-30 17:32:49 -0800462 if (data == null) {
463 // This is a contentIntent based bubble, lets allow it to be the max height
464 // as it was forced into this mode and not prepared to be small
465 desiredHeight = mStackView.getMaxExpandedHeight();
466 } else {
Mady Mellor7af771a2019-03-07 15:04:54 -0800467 boolean useRes = data.getDesiredHeightResId() != 0;
468 float desiredPx;
469 if (useRes) {
470 desiredPx = getDimenForPackageUser(data.getDesiredHeightResId(),
471 mEntry.notification.getPackageName(),
472 mEntry.notification.getUser().getIdentifier());
473 } else {
474 desiredPx = data.getDesiredHeight()
475 * getContext().getResources().getDisplayMetrics().density;
476 }
477 desiredHeight = desiredPx > 0 ? desiredPx : mMinHeight;
Mady Mellorfe7ec032019-01-30 17:32:49 -0800478 }
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800479 int chromeHeight = mPermissionView.getVisibility() != View.VISIBLE
480 ? mHeaderHeight
481 : mPermissionHeight;
482 int max = mStackView.getMaxExpandedHeight() - chromeHeight - mPointerView.getHeight()
483 - mPointerMargin;
Mady Mellor7af771a2019-03-07 15:04:54 -0800484 float height = Math.min(desiredHeight, max);
Mady Mellorfe7ec032019-01-30 17:32:49 -0800485 height = Math.max(height, mMinHeight);
486 LayoutParams lp = (LayoutParams) mActivityView.getLayoutParams();
Mady Mellor5d8f1402019-02-21 18:23:52 -0800487 mNeedsNewHeight = lp.height != height;
488 if (!mKeyboardVisible) {
489 // If the keyboard is visible... don't adjust the height because that will cause
490 // a configuration change and the keyboard will be lost.
Mady Mellor7af771a2019-03-07 15:04:54 -0800491 lp.height = (int) height;
492 mBubbleHeight = (int) height;
Mady Mellor5d8f1402019-02-21 18:23:52 -0800493 mActivityView.setLayoutParams(lp);
494 mNeedsNewHeight = false;
495 }
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800496 } else {
497 mBubbleHeight = mNotifRow != null ? mNotifRow.getIntrinsicHeight() : mMinHeight;
Mady Mellorfe7ec032019-01-30 17:32:49 -0800498 }
499 }
500
Mady Mellor9801e852019-01-22 14:50:28 -0800501 @Override
502 public void onClick(View view) {
503 if (mEntry == null) {
504 return;
505 }
506 Notification n = mEntry.notification.getNotification();
507 int id = view.getId();
508 if (id == R.id.deep_link_button) {
509 mStackView.collapseStack(() -> {
510 try {
511 n.contentIntent.send();
Steven Wu45e38ae2019-03-25 16:16:59 -0400512 logBubbleClickEvent(mEntry,
Steven Wub00225b2019-02-08 14:27:42 -0500513 StatsLog.BUBBLE_UICHANGED__ACTION__HEADER_GO_TO_APP);
Mady Mellor9801e852019-01-22 14:50:28 -0800514 } catch (PendingIntent.CanceledException e) {
515 Log.w(TAG, "Failed to send intent for bubble with key: "
516 + (mEntry != null ? mEntry.key : " null entry"));
517 }
518 });
519 } else if (id == R.id.settings_button) {
520 Intent intent = getSettingsIntent(mEntry.notification.getPackageName(),
521 mEntry.notification.getUid());
Steven Wub00225b2019-02-08 14:27:42 -0500522 mStackView.collapseStack(() -> {
523 mContext.startActivity(intent);
Steven Wu45e38ae2019-03-25 16:16:59 -0400524 logBubbleClickEvent(mEntry,
Steven Wub00225b2019-02-08 14:27:42 -0500525 StatsLog.BUBBLE_UICHANGED__ACTION__HEADER_GO_TO_SETTINGS);
526 });
Mady Mellore8e07712019-01-23 12:45:33 -0800527 } else if (id == R.id.no_bubbles_button) {
528 setBubblesAllowed(false);
529 } else if (id == R.id.yes_bubbles_button) {
530 setBubblesAllowed(true);
531 }
532 }
533
534 private void setBubblesAllowed(boolean allowed) {
535 try {
536 mNotificationManagerService.setBubblesAllowed(
537 mEntry.notification.getPackageName(),
538 mEntry.notification.getUid(),
539 allowed);
540 if (allowed) {
541 mPermissionView.setVisibility(GONE);
542 mHeaderView.setVisibility(VISIBLE);
543 } else if (mOnBubbleBlockedListener != null) {
544 mOnBubbleBlockedListener.onBubbleBlocked(mEntry);
545 }
Mady Mellor44ee2fe2019-01-30 17:51:16 -0800546 mStackView.onExpandedHeightChanged();
Steven Wu45e38ae2019-03-25 16:16:59 -0400547 logBubbleClickEvent(mEntry,
Steven Wub00225b2019-02-08 14:27:42 -0500548 allowed ? StatsLog.BUBBLE_UICHANGED__ACTION__PERMISSION_OPT_IN :
549 StatsLog.BUBBLE_UICHANGED__ACTION__PERMISSION_OPT_OUT);
Mady Mellore8e07712019-01-23 12:45:33 -0800550 } catch (RemoteException e) {
551 Log.w(TAG, e);
Mady Mellor9801e852019-01-22 14:50:28 -0800552 }
Mady Mellordea7ecf2018-12-10 15:47:40 -0800553 }
554
555 /**
Mady Mellor3dff9e62019-02-05 18:12:53 -0800556 * Update appearance of the expanded view being displayed.
557 */
558 public void updateView() {
559 if (usingActivityView()
560 && mActivityView.getVisibility() == VISIBLE
561 && mActivityView.isAttachedToWindow()) {
562 mActivityView.onLocationChanged();
563 } else if (mNotifRow != null) {
564 applyRowState(mNotifRow);
565 }
Mady Mellorfe7ec032019-01-30 17:32:49 -0800566 updateHeight();
Mady Mellor3dff9e62019-02-05 18:12:53 -0800567 }
568
569 /**
Mady Mellordea7ecf2018-12-10 15:47:40 -0800570 * Set the x position that the tip of the triangle should point to.
571 */
Mady Mellor3dff9e62019-02-05 18:12:53 -0800572 public void setPointerPosition(float x) {
Mady Mellordea7ecf2018-12-10 15:47:40 -0800573 // Adjust for the pointer size
Mady Mellor3dff9e62019-02-05 18:12:53 -0800574 x -= (mPointerView.getWidth() / 2f);
Mady Mellordea7ecf2018-12-10 15:47:40 -0800575 mPointerView.setTranslationX(x);
576 }
577
578 /**
Mady Mellor3dff9e62019-02-05 18:12:53 -0800579 * Removes and releases an ActivityView if one was previously created for this bubble.
Mady Mellordea7ecf2018-12-10 15:47:40 -0800580 */
Mady Mellor94d94a72019-03-05 18:16:59 -0800581 public void cleanUpExpandedState() {
582 removeView(mNotifRow);
583
Mady Mellor3dff9e62019-02-05 18:12:53 -0800584 if (mActivityView == null) {
Mark Renouf89b1a4a2018-12-04 14:59:45 -0500585 return;
586 }
Mark Renouf28c250d2019-02-25 16:47:34 -0500587 if (mActivityViewReady) {
588 mActivityView.release();
Mady Mellordea7ecf2018-12-10 15:47:40 -0800589 }
Mark Renouf28c250d2019-02-25 16:47:34 -0500590 removeView(mActivityView);
Mady Mellor3dff9e62019-02-05 18:12:53 -0800591 mActivityView = null;
592 mActivityViewReady = false;
Mady Mellordea7ecf2018-12-10 15:47:40 -0800593 }
594
Mady Mellor3dff9e62019-02-05 18:12:53 -0800595 private boolean usingActivityView() {
596 return mBubbleIntent != null;
597 }
598
599 private void applyRowState(ExpandableNotificationRow view) {
600 view.reset();
601 view.setHeadsUp(false);
602 view.resetTranslation();
603 view.setOnKeyguard(false);
604 view.setOnAmbient(false);
605 view.setClipBottomAmount(0);
606 view.setClipTopAmount(0);
607 view.setContentTransformationAmount(0, false);
608 view.setIconsVisible(true);
609
610 // TODO - Need to reset this (and others) when view goes back in shade, leave for now
611 // view.setTopRoundness(1, false);
612 // view.setBottomRoundness(1, false);
613
614 ExpandableViewState viewState = view.getViewState();
615 viewState = viewState == null ? new ExpandableViewState() : viewState;
616 viewState.height = view.getIntrinsicHeight();
617 viewState.gone = false;
618 viewState.hidden = false;
619 viewState.dimmed = false;
620 viewState.dark = false;
621 viewState.alpha = 1f;
622 viewState.notGoneIndex = -1;
623 viewState.xTranslation = 0;
624 viewState.yTranslation = 0;
625 viewState.zTranslation = 0;
626 viewState.scaleX = 1;
627 viewState.scaleY = 1;
628 viewState.inShelf = true;
629 viewState.headsUpIsVisible = false;
630 viewState.applyToView(view);
Mady Mellordea7ecf2018-12-10 15:47:40 -0800631 }
Mady Mellor9801e852019-01-22 14:50:28 -0800632
633 private Intent getSettingsIntent(String packageName, final int appUid) {
634 final Intent intent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
635 intent.putExtra(Settings.EXTRA_APP_PACKAGE, packageName);
636 intent.putExtra(Settings.EXTRA_APP_UID, appUid);
637 intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
638 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
639 return intent;
640 }
Mady Mellore8e07712019-01-23 12:45:33 -0800641
Mady Mellor3dff9e62019-02-05 18:12:53 -0800642 @Nullable
643 private PendingIntent getBubbleIntent(NotificationEntry entry) {
644 Notification notif = entry.notification.getNotification();
645 String packageName = entry.notification.getPackageName();
646 Notification.BubbleMetadata data = notif.getBubbleMetadata();
647 if (data != null && canLaunchInActivityView(data.getIntent(), true /* enableLogging */,
648 packageName)) {
649 return data.getIntent();
650 } else if (BubbleController.shouldUseContentIntent(mContext)
651 && canLaunchInActivityView(notif.contentIntent, false /* enableLogging */,
652 packageName)) {
653 return notif.contentIntent;
654 }
655 return null;
656 }
657
658 /**
659 * Whether an intent is properly configured to display in an {@link android.app.ActivityView}.
660 *
661 * @param intent the pending intent of the bubble.
662 * @param enableLogging whether bubble developer error should be logged.
663 * @param packageName the notification package name for this bubble.
664 * @return
665 */
666 private boolean canLaunchInActivityView(PendingIntent intent, boolean enableLogging,
667 String packageName) {
668 if (intent == null) {
669 return false;
670 }
671 ActivityInfo info =
672 intent.getIntent().resolveActivityInfo(mContext.getPackageManager(), 0);
673 if (info == null) {
674 if (enableLogging) {
675 StatsLog.write(StatsLog.BUBBLE_DEVELOPER_ERROR_REPORTED, packageName,
676 BUBBLE_DEVELOPER_ERROR_REPORTED__ERROR__ACTIVITY_INFO_MISSING);
677 }
678 return false;
679 }
680 if (!ActivityInfo.isResizeableMode(info.resizeMode)) {
681 if (enableLogging) {
682 StatsLog.write(StatsLog.BUBBLE_DEVELOPER_ERROR_REPORTED, packageName,
683 BUBBLE_DEVELOPER_ERROR_REPORTED__ERROR__ACTIVITY_INFO_NOT_RESIZABLE);
684 }
685 return false;
686 }
687 if (info.documentLaunchMode != DOCUMENT_LAUNCH_ALWAYS) {
688 if (enableLogging) {
689 StatsLog.write(StatsLog.BUBBLE_DEVELOPER_ERROR_REPORTED, packageName,
690 BUBBLE_DEVELOPER_ERROR_REPORTED__ERROR__DOCUMENT_LAUNCH_NOT_ALWAYS);
691 }
692 return false;
693 }
694 return (info.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) != 0;
695 }
696
Mady Mellore8e07712019-01-23 12:45:33 -0800697 /**
698 * Listener that is notified when a bubble is blocked.
699 */
700 public interface OnBubbleBlockedListener {
701 /**
702 * Called when a bubble is blocked for the provided entry.
703 */
704 void onBubbleBlocked(NotificationEntry entry);
705 }
Steven Wub00225b2019-02-08 14:27:42 -0500706
707 /**
708 * Logs bubble UI click event.
709 *
Steven Wu45e38ae2019-03-25 16:16:59 -0400710 * @param entry the bubble notification entry that user is interacting with.
Steven Wub00225b2019-02-08 14:27:42 -0500711 * @param action the user interaction enum.
712 */
Steven Wu45e38ae2019-03-25 16:16:59 -0400713 private void logBubbleClickEvent(NotificationEntry entry, int action) {
714 StatusBarNotification notification = entry.notification;
Steven Wub00225b2019-02-08 14:27:42 -0500715 StatsLog.write(StatsLog.BUBBLE_UI_CHANGED,
716 notification.getPackageName(),
717 notification.getNotification().getChannelId(),
718 notification.getId(),
719 mStackView.getBubbleIndex(mStackView.getExpandedBubble()),
720 mStackView.getBubbleCount(),
721 action,
722 mStackView.getNormalizedXPosition(),
Steven Wu45e38ae2019-03-25 16:16:59 -0400723 mStackView.getNormalizedYPosition(),
724 entry.showInShadeWhenBubble());
Steven Wub00225b2019-02-08 14:27:42 -0500725 }
Mady Mellor7af771a2019-03-07 15:04:54 -0800726
727 private int getDimenForPackageUser(int resId, String pkg, int userId) {
728 Resources r;
729 if (pkg != null) {
730 try {
731 if (userId == UserHandle.USER_ALL) {
732 userId = UserHandle.USER_SYSTEM;
733 }
734 r = mPm.getResourcesForApplicationAsUser(pkg, userId);
735 return r.getDimensionPixelSize(resId);
736 } catch (PackageManager.NameNotFoundException ex) {
737 // Uninstalled, don't care
738 } catch (Resources.NotFoundException e) {
739 // Invalid res id, return 0 and user our default
740 Log.e(TAG, "Couldn't find desired height res id", e);
741 }
742 }
743 return 0;
744 }
Mady Mellordea7ecf2018-12-10 15:47:40 -0800745}