Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.bubbles; |
| 18 | |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 19 | import static android.content.pm.ActivityInfo.DOCUMENT_LAUNCH_ALWAYS; |
| 20 | import static android.util.StatsLogInternal.BUBBLE_DEVELOPER_ERROR_REPORTED__ERROR__ACTIVITY_INFO_MISSING; |
| 21 | import static android.util.StatsLogInternal.BUBBLE_DEVELOPER_ERROR_REPORTED__ERROR__ACTIVITY_INFO_NOT_RESIZABLE; |
| 22 | import static android.util.StatsLogInternal.BUBBLE_DEVELOPER_ERROR_REPORTED__ERROR__DOCUMENT_LAUNCH_NOT_ALWAYS; |
| 23 | |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 24 | import android.animation.LayoutTransition; |
| 25 | import android.animation.ObjectAnimator; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 26 | import android.annotation.Nullable; |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 27 | import android.app.ActivityView; |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 28 | import android.app.INotificationManager; |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 29 | import android.app.Notification; |
| 30 | import android.app.PendingIntent; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 31 | import android.content.Context; |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 32 | import android.content.Intent; |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 33 | import android.content.pm.ActivityInfo; |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 34 | import android.content.pm.ApplicationInfo; |
| 35 | import android.content.pm.PackageManager; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 36 | import android.content.res.Resources; |
Mady Mellor | dd49705 | 2019-01-30 17:23:48 -0800 | [diff] [blame] | 37 | import android.content.res.TypedArray; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 38 | import android.graphics.Color; |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 39 | import android.graphics.Insets; |
| 40 | import android.graphics.Point; |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 41 | import android.graphics.drawable.Drawable; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 42 | import android.graphics.drawable.ShapeDrawable; |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 43 | import android.os.RemoteException; |
| 44 | import android.os.ServiceManager; |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 45 | import android.provider.Settings; |
Steven Wu | b00225b | 2019-02-08 14:27:42 -0500 | [diff] [blame] | 46 | import android.service.notification.StatusBarNotification; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 47 | import android.util.AttributeSet; |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 48 | import android.util.Log; |
Steven Wu | b00225b | 2019-02-08 14:27:42 -0500 | [diff] [blame] | 49 | import android.util.StatsLog; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 50 | import android.view.View; |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 51 | import android.view.ViewGroup; |
| 52 | import android.view.WindowInsets; |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 53 | import android.widget.FrameLayout; |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 54 | import android.widget.ImageButton; |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 55 | import android.widget.ImageView; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 56 | import android.widget.LinearLayout; |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 57 | import android.widget.TextView; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 58 | |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 59 | import com.android.systemui.Dependency; |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 60 | import com.android.systemui.Interpolators; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 61 | import com.android.systemui.R; |
| 62 | import com.android.systemui.recents.TriangleShape; |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 63 | import com.android.systemui.statusbar.notification.collection.NotificationEntry; |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 64 | import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow; |
| 65 | import com.android.systemui.statusbar.notification.stack.ExpandableViewState; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 66 | |
| 67 | /** |
| 68 | * Container for the expanded bubble view, handles rendering the caret and header of the view. |
| 69 | */ |
Mady Mellor | 3d82e68 | 2019-02-05 13:34:48 -0800 | [diff] [blame] | 70 | public class BubbleExpandedView extends LinearLayout implements View.OnClickListener { |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 71 | private static final String TAG = "BubbleExpandedView"; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 72 | |
| 73 | // The triangle pointing to the expanded view |
| 74 | private View mPointerView; |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 75 | |
| 76 | // Header |
| 77 | private View mHeaderView; |
| 78 | private TextView mHeaderTextView; |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 79 | private ImageButton mDeepLinkIcon; |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 80 | private ImageButton mSettingsIcon; |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 81 | |
| 82 | // Permission view |
| 83 | private View mPermissionView; |
| 84 | |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 85 | // Views for expanded state |
| 86 | private ExpandableNotificationRow mNotifRow; |
| 87 | private ActivityView mActivityView; |
| 88 | |
| 89 | private boolean mActivityViewReady = false; |
| 90 | private PendingIntent mBubbleIntent; |
| 91 | |
| 92 | private int mBubbleHeight; |
| 93 | private int mDefaultHeight; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 94 | |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 95 | private NotificationEntry mEntry; |
| 96 | private PackageManager mPm; |
| 97 | private String mAppName; |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 98 | private Drawable mAppIcon; |
| 99 | |
| 100 | private INotificationManager mNotificationManagerService; |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 101 | private BubbleController mBubbleController = Dependency.get(BubbleController.class); |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 102 | |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 103 | private BubbleStackView mStackView; |
| 104 | |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 105 | private BubbleExpandedView.OnBubbleBlockedListener mOnBubbleBlockedListener; |
| 106 | |
| 107 | private ActivityView.StateCallback mStateCallback = new ActivityView.StateCallback() { |
| 108 | @Override |
| 109 | public void onActivityViewReady(ActivityView view) { |
Mady Mellor | 6d00203 | 2019-02-13 13:45:17 -0800 | [diff] [blame] | 110 | if (!mActivityViewReady) { |
| 111 | mActivityViewReady = true; |
| 112 | mActivityView.startActivity(mBubbleIntent); |
| 113 | } |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public void onActivityViewDestroyed(ActivityView view) { |
| 118 | mActivityViewReady = false; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * This is only called for tasks on this ActivityView, which is also set to |
| 123 | * single-task mode -- meaning never more than one task on this display. If a task |
| 124 | * is being removed, it's the top Activity finishing and this bubble should |
| 125 | * be removed or collapsed. |
| 126 | */ |
| 127 | @Override |
| 128 | public void onTaskRemovalStarted(int taskId) { |
| 129 | if (mEntry != null) { |
| 130 | // Must post because this is called from a binder thread. |
| 131 | post(() -> mBubbleController.removeBubble(mEntry.key)); |
| 132 | } |
| 133 | } |
| 134 | }; |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 135 | |
Mady Mellor | 3d82e68 | 2019-02-05 13:34:48 -0800 | [diff] [blame] | 136 | public BubbleExpandedView(Context context) { |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 137 | this(context, null); |
| 138 | } |
| 139 | |
Mady Mellor | 3d82e68 | 2019-02-05 13:34:48 -0800 | [diff] [blame] | 140 | public BubbleExpandedView(Context context, AttributeSet attrs) { |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 141 | this(context, attrs, 0); |
| 142 | } |
| 143 | |
Mady Mellor | 3d82e68 | 2019-02-05 13:34:48 -0800 | [diff] [blame] | 144 | public BubbleExpandedView(Context context, AttributeSet attrs, int defStyleAttr) { |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 145 | this(context, attrs, defStyleAttr, 0); |
| 146 | } |
| 147 | |
Mady Mellor | 3d82e68 | 2019-02-05 13:34:48 -0800 | [diff] [blame] | 148 | public BubbleExpandedView(Context context, AttributeSet attrs, int defStyleAttr, |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 149 | int defStyleRes) { |
| 150 | super(context, attrs, defStyleAttr, defStyleRes); |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 151 | mPm = context.getPackageManager(); |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 152 | mDefaultHeight = getResources().getDimensionPixelSize( |
| 153 | R.dimen.bubble_expanded_default_height); |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 154 | try { |
| 155 | mNotificationManagerService = INotificationManager.Stub.asInterface( |
| 156 | ServiceManager.getServiceOrThrow(Context.NOTIFICATION_SERVICE)); |
| 157 | } catch (ServiceManager.ServiceNotFoundException e) { |
| 158 | Log.w(TAG, e); |
| 159 | } |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | @Override |
| 163 | protected void onFinishInflate() { |
| 164 | super.onFinishInflate(); |
| 165 | |
| 166 | Resources res = getResources(); |
| 167 | mPointerView = findViewById(R.id.pointer_view); |
| 168 | int width = res.getDimensionPixelSize(R.dimen.bubble_pointer_width); |
| 169 | int height = res.getDimensionPixelSize(R.dimen.bubble_pointer_height); |
Mady Mellor | dd49705 | 2019-01-30 17:23:48 -0800 | [diff] [blame] | 170 | |
| 171 | TypedArray ta = getContext().obtainStyledAttributes( |
| 172 | new int[] {android.R.attr.colorBackgroundFloating}); |
| 173 | int bgColor = ta.getColor(0, Color.WHITE); |
| 174 | ta.recycle(); |
| 175 | |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 176 | ShapeDrawable triangleDrawable = new ShapeDrawable( |
| 177 | TriangleShape.create(width, height, true /* pointUp */)); |
Mady Mellor | dd49705 | 2019-01-30 17:23:48 -0800 | [diff] [blame] | 178 | triangleDrawable.setTint(bgColor); |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 179 | mPointerView.setBackground(triangleDrawable); |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 180 | |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 181 | FrameLayout viewWrapper = findViewById(R.id.header_permission_wrapper); |
| 182 | LayoutTransition transition = new LayoutTransition(); |
| 183 | transition.setDuration(200); |
| 184 | |
| 185 | ObjectAnimator appearAnimator = ObjectAnimator.ofFloat(null, View.ALPHA, 0f, 1f); |
| 186 | transition.setAnimator(LayoutTransition.APPEARING, appearAnimator); |
| 187 | transition.setInterpolator(LayoutTransition.APPEARING, Interpolators.ALPHA_IN); |
| 188 | |
| 189 | ObjectAnimator disappearAnimator = ObjectAnimator.ofFloat(null, View.ALPHA, 1f, 0f); |
| 190 | transition.setAnimator(LayoutTransition.DISAPPEARING, disappearAnimator); |
| 191 | transition.setInterpolator(LayoutTransition.DISAPPEARING, Interpolators.ALPHA_OUT); |
| 192 | |
| 193 | transition.setAnimateParentHierarchy(false); |
| 194 | viewWrapper.setLayoutTransition(transition); |
| 195 | viewWrapper.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING); |
| 196 | |
| 197 | mHeaderView = findViewById(R.id.header_layout); |
| 198 | mHeaderTextView = findViewById(R.id.header_text); |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 199 | mDeepLinkIcon = findViewById(R.id.deep_link_button); |
| 200 | mSettingsIcon = findViewById(R.id.settings_button); |
| 201 | mDeepLinkIcon.setOnClickListener(this); |
| 202 | mSettingsIcon.setOnClickListener(this); |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 203 | |
| 204 | mPermissionView = findViewById(R.id.permission_layout); |
| 205 | findViewById(R.id.no_bubbles_button).setOnClickListener(this); |
| 206 | findViewById(R.id.yes_bubbles_button).setOnClickListener(this); |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 207 | |
| 208 | mActivityView = new ActivityView(mContext, null /* attrs */, 0 /* defStyle */, |
| 209 | true /* singleTaskInstance */); |
| 210 | addView(mActivityView); |
| 211 | |
| 212 | mActivityView.setOnApplyWindowInsetsListener((View view, WindowInsets insets) -> { |
| 213 | ActivityView activityView = (ActivityView) view; |
| 214 | // Here we assume that the position of the ActivityView on the screen |
| 215 | // remains regardless of IME status. When we move ActivityView, the |
| 216 | // forwardedInsets should be computed not against the current location |
| 217 | // and size, but against the post-moved location and size. |
| 218 | Point displaySize = new Point(); |
| 219 | view.getContext().getDisplay().getSize(displaySize); |
| 220 | int[] windowLocation = view.getLocationOnScreen(); |
| 221 | final int windowBottom = windowLocation[1] + view.getHeight(); |
| 222 | final int keyboardHeight = insets.getSystemWindowInsetBottom() |
| 223 | - insets.getStableInsetBottom(); |
| 224 | final int insetsBottom = Math.max(0, |
| 225 | windowBottom + keyboardHeight - displaySize.y); |
| 226 | activityView.setForwardedInsets(Insets.of(0, 0, 0, insetsBottom)); |
| 227 | return view.onApplyWindowInsets(insets); |
| 228 | }); |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Sets the listener to notify when a bubble has been blocked. |
| 233 | */ |
| 234 | public void setOnBlockedListener(OnBubbleBlockedListener listener) { |
| 235 | mOnBubbleBlockedListener = listener; |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Sets the notification entry used to populate this view. |
| 240 | */ |
| 241 | public void setEntry(NotificationEntry entry, BubbleStackView stackView) { |
| 242 | mStackView = stackView; |
| 243 | mEntry = entry; |
| 244 | |
| 245 | ApplicationInfo info; |
| 246 | try { |
| 247 | info = mPm.getApplicationInfo( |
| 248 | entry.notification.getPackageName(), |
| 249 | PackageManager.MATCH_UNINSTALLED_PACKAGES |
| 250 | | PackageManager.MATCH_DISABLED_COMPONENTS |
| 251 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE |
| 252 | | PackageManager.MATCH_DIRECT_BOOT_AWARE); |
| 253 | if (info != null) { |
| 254 | mAppName = String.valueOf(mPm.getApplicationLabel(info)); |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 255 | mAppIcon = mPm.getApplicationIcon(info); |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 256 | } |
| 257 | } catch (PackageManager.NameNotFoundException e) { |
| 258 | // Ahh... just use package name |
| 259 | mAppName = entry.notification.getPackageName(); |
| 260 | } |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 261 | if (mAppIcon == null) { |
| 262 | mAppIcon = mPm.getDefaultActivityIcon(); |
| 263 | } |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 264 | updateHeaderView(); |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 265 | updatePermissionView(); |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 266 | updateExpandedView(); |
Mady Mellor | 6d00203 | 2019-02-13 13:45:17 -0800 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Lets activity view know it should be shown / populated. |
| 271 | */ |
| 272 | public void populateActivityView() { |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 273 | mActivityView.setCallback(mStateCallback); |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | private void updateHeaderView() { |
| 277 | mSettingsIcon.setContentDescription(getResources().getString( |
| 278 | R.string.bubbles_settings_button_description, mAppName)); |
| 279 | mDeepLinkIcon.setContentDescription(getResources().getString( |
| 280 | R.string.bubbles_deep_link_button_description, mAppName)); |
| 281 | if (mEntry != null && mEntry.getBubbleMetadata() != null) { |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 282 | mHeaderTextView.setText(mEntry.getBubbleMetadata().getTitle()); |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 283 | } else { |
| 284 | // This should only happen if we're auto-bubbling notification content that isn't |
| 285 | // explicitly a bubble |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 286 | mHeaderTextView.setText(mAppName); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | private void updatePermissionView() { |
| 291 | boolean hasUserApprovedBubblesForPackage = false; |
| 292 | try { |
| 293 | hasUserApprovedBubblesForPackage = |
| 294 | mNotificationManagerService.hasUserApprovedBubblesForPackage( |
| 295 | mEntry.notification.getPackageName(), mEntry.notification.getUid()); |
| 296 | } catch (RemoteException e) { |
| 297 | Log.w(TAG, e); |
| 298 | } |
| 299 | if (hasUserApprovedBubblesForPackage) { |
| 300 | mHeaderView.setVisibility(VISIBLE); |
| 301 | mPermissionView.setVisibility(GONE); |
| 302 | } else { |
| 303 | mHeaderView.setVisibility(GONE); |
| 304 | mPermissionView.setVisibility(VISIBLE); |
| 305 | ((ImageView) mPermissionView.findViewById(R.id.pkgicon)).setImageDrawable(mAppIcon); |
| 306 | ((TextView) mPermissionView.findViewById(R.id.pkgname)).setText(mAppName); |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 310 | private void updateExpandedView() { |
| 311 | mBubbleIntent = getBubbleIntent(mEntry); |
| 312 | if (mBubbleIntent != null) { |
| 313 | if (mNotifRow != null) { |
| 314 | // Clear out the row if we had it previously |
| 315 | removeView(mNotifRow); |
| 316 | mNotifRow = null; |
| 317 | } |
| 318 | Notification.BubbleMetadata data = mEntry.getBubbleMetadata(); |
| 319 | mBubbleHeight = data != null && data.getDesiredHeight() > 0 |
| 320 | ? data.getDesiredHeight() |
| 321 | : mDefaultHeight; |
| 322 | // XXX: enforce max / min height |
| 323 | LayoutParams lp = (LayoutParams) mActivityView.getLayoutParams(); |
| 324 | lp.height = mBubbleHeight; |
| 325 | mActivityView.setLayoutParams(lp); |
| 326 | mActivityView.setVisibility(VISIBLE); |
| 327 | } else { |
| 328 | // Hide activity view if we had it previously |
| 329 | mActivityView.setVisibility(GONE); |
| 330 | |
| 331 | // Use notification view |
| 332 | mNotifRow = mEntry.getRow(); |
| 333 | addView(mNotifRow); |
| 334 | } |
| 335 | updateView(); |
| 336 | } |
| 337 | |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 338 | @Override |
| 339 | public void onClick(View view) { |
| 340 | if (mEntry == null) { |
| 341 | return; |
| 342 | } |
| 343 | Notification n = mEntry.notification.getNotification(); |
| 344 | int id = view.getId(); |
| 345 | if (id == R.id.deep_link_button) { |
| 346 | mStackView.collapseStack(() -> { |
| 347 | try { |
| 348 | n.contentIntent.send(); |
Steven Wu | b00225b | 2019-02-08 14:27:42 -0500 | [diff] [blame] | 349 | logBubbleClickEvent(mEntry.notification, |
| 350 | StatsLog.BUBBLE_UICHANGED__ACTION__HEADER_GO_TO_APP); |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 351 | } catch (PendingIntent.CanceledException e) { |
| 352 | Log.w(TAG, "Failed to send intent for bubble with key: " |
| 353 | + (mEntry != null ? mEntry.key : " null entry")); |
| 354 | } |
| 355 | }); |
| 356 | } else if (id == R.id.settings_button) { |
| 357 | Intent intent = getSettingsIntent(mEntry.notification.getPackageName(), |
| 358 | mEntry.notification.getUid()); |
Steven Wu | b00225b | 2019-02-08 14:27:42 -0500 | [diff] [blame] | 359 | mStackView.collapseStack(() -> { |
| 360 | mContext.startActivity(intent); |
| 361 | logBubbleClickEvent(mEntry.notification, |
| 362 | StatsLog.BUBBLE_UICHANGED__ACTION__HEADER_GO_TO_SETTINGS); |
| 363 | }); |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 364 | } else if (id == R.id.no_bubbles_button) { |
| 365 | setBubblesAllowed(false); |
| 366 | } else if (id == R.id.yes_bubbles_button) { |
| 367 | setBubblesAllowed(true); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | private void setBubblesAllowed(boolean allowed) { |
| 372 | try { |
| 373 | mNotificationManagerService.setBubblesAllowed( |
| 374 | mEntry.notification.getPackageName(), |
| 375 | mEntry.notification.getUid(), |
| 376 | allowed); |
| 377 | if (allowed) { |
| 378 | mPermissionView.setVisibility(GONE); |
| 379 | mHeaderView.setVisibility(VISIBLE); |
| 380 | } else if (mOnBubbleBlockedListener != null) { |
| 381 | mOnBubbleBlockedListener.onBubbleBlocked(mEntry); |
| 382 | } |
Steven Wu | b00225b | 2019-02-08 14:27:42 -0500 | [diff] [blame] | 383 | logBubbleClickEvent(mEntry.notification, |
| 384 | allowed ? StatsLog.BUBBLE_UICHANGED__ACTION__PERMISSION_OPT_IN : |
| 385 | StatsLog.BUBBLE_UICHANGED__ACTION__PERMISSION_OPT_OUT); |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 386 | } catch (RemoteException e) { |
| 387 | Log.w(TAG, e); |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 388 | } |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | /** |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 392 | * Update appearance of the expanded view being displayed. |
| 393 | */ |
| 394 | public void updateView() { |
| 395 | if (usingActivityView() |
| 396 | && mActivityView.getVisibility() == VISIBLE |
| 397 | && mActivityView.isAttachedToWindow()) { |
| 398 | mActivityView.onLocationChanged(); |
| 399 | } else if (mNotifRow != null) { |
| 400 | applyRowState(mNotifRow); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | /** |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 405 | * Set the x position that the tip of the triangle should point to. |
| 406 | */ |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 407 | public void setPointerPosition(float x) { |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 408 | // Adjust for the pointer size |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 409 | x -= (mPointerView.getWidth() / 2f); |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 410 | mPointerView.setTranslationX(x); |
| 411 | } |
| 412 | |
| 413 | /** |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 414 | * Removes and releases an ActivityView if one was previously created for this bubble. |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 415 | */ |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 416 | public void destroyActivityView(ViewGroup tmpParent) { |
| 417 | if (mActivityView == null) { |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 418 | return; |
| 419 | } |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 420 | if (!mActivityViewReady) { |
| 421 | // release not needed, never initialized? |
| 422 | mActivityView = null; |
| 423 | return; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 424 | } |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 425 | // HACK: release() will crash if the view is not attached. |
| 426 | if (!isAttachedToWindow()) { |
| 427 | mActivityView.setVisibility(View.GONE); |
| 428 | tmpParent.addView(this); |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 429 | } |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 430 | |
| 431 | mActivityView.release(); |
| 432 | ((ViewGroup) getParent()).removeView(this); |
| 433 | mActivityView = null; |
| 434 | mActivityViewReady = false; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 435 | } |
| 436 | |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 437 | private boolean usingActivityView() { |
| 438 | return mBubbleIntent != null; |
| 439 | } |
| 440 | |
| 441 | private void applyRowState(ExpandableNotificationRow view) { |
| 442 | view.reset(); |
| 443 | view.setHeadsUp(false); |
| 444 | view.resetTranslation(); |
| 445 | view.setOnKeyguard(false); |
| 446 | view.setOnAmbient(false); |
| 447 | view.setClipBottomAmount(0); |
| 448 | view.setClipTopAmount(0); |
| 449 | view.setContentTransformationAmount(0, false); |
| 450 | view.setIconsVisible(true); |
| 451 | |
| 452 | // TODO - Need to reset this (and others) when view goes back in shade, leave for now |
| 453 | // view.setTopRoundness(1, false); |
| 454 | // view.setBottomRoundness(1, false); |
| 455 | |
| 456 | ExpandableViewState viewState = view.getViewState(); |
| 457 | viewState = viewState == null ? new ExpandableViewState() : viewState; |
| 458 | viewState.height = view.getIntrinsicHeight(); |
| 459 | viewState.gone = false; |
| 460 | viewState.hidden = false; |
| 461 | viewState.dimmed = false; |
| 462 | viewState.dark = false; |
| 463 | viewState.alpha = 1f; |
| 464 | viewState.notGoneIndex = -1; |
| 465 | viewState.xTranslation = 0; |
| 466 | viewState.yTranslation = 0; |
| 467 | viewState.zTranslation = 0; |
| 468 | viewState.scaleX = 1; |
| 469 | viewState.scaleY = 1; |
| 470 | viewState.inShelf = true; |
| 471 | viewState.headsUpIsVisible = false; |
| 472 | viewState.applyToView(view); |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 473 | } |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 474 | |
| 475 | private Intent getSettingsIntent(String packageName, final int appUid) { |
| 476 | final Intent intent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS); |
| 477 | intent.putExtra(Settings.EXTRA_APP_PACKAGE, packageName); |
| 478 | intent.putExtra(Settings.EXTRA_APP_UID, appUid); |
| 479 | intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK); |
| 480 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 481 | return intent; |
| 482 | } |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 483 | |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 484 | @Nullable |
| 485 | private PendingIntent getBubbleIntent(NotificationEntry entry) { |
| 486 | Notification notif = entry.notification.getNotification(); |
| 487 | String packageName = entry.notification.getPackageName(); |
| 488 | Notification.BubbleMetadata data = notif.getBubbleMetadata(); |
| 489 | if (data != null && canLaunchInActivityView(data.getIntent(), true /* enableLogging */, |
| 490 | packageName)) { |
| 491 | return data.getIntent(); |
| 492 | } else if (BubbleController.shouldUseContentIntent(mContext) |
| 493 | && canLaunchInActivityView(notif.contentIntent, false /* enableLogging */, |
| 494 | packageName)) { |
| 495 | return notif.contentIntent; |
| 496 | } |
| 497 | return null; |
| 498 | } |
| 499 | |
| 500 | /** |
| 501 | * Whether an intent is properly configured to display in an {@link android.app.ActivityView}. |
| 502 | * |
| 503 | * @param intent the pending intent of the bubble. |
| 504 | * @param enableLogging whether bubble developer error should be logged. |
| 505 | * @param packageName the notification package name for this bubble. |
| 506 | * @return |
| 507 | */ |
| 508 | private boolean canLaunchInActivityView(PendingIntent intent, boolean enableLogging, |
| 509 | String packageName) { |
| 510 | if (intent == null) { |
| 511 | return false; |
| 512 | } |
| 513 | ActivityInfo info = |
| 514 | intent.getIntent().resolveActivityInfo(mContext.getPackageManager(), 0); |
| 515 | if (info == null) { |
| 516 | if (enableLogging) { |
| 517 | StatsLog.write(StatsLog.BUBBLE_DEVELOPER_ERROR_REPORTED, packageName, |
| 518 | BUBBLE_DEVELOPER_ERROR_REPORTED__ERROR__ACTIVITY_INFO_MISSING); |
| 519 | } |
| 520 | return false; |
| 521 | } |
| 522 | if (!ActivityInfo.isResizeableMode(info.resizeMode)) { |
| 523 | if (enableLogging) { |
| 524 | StatsLog.write(StatsLog.BUBBLE_DEVELOPER_ERROR_REPORTED, packageName, |
| 525 | BUBBLE_DEVELOPER_ERROR_REPORTED__ERROR__ACTIVITY_INFO_NOT_RESIZABLE); |
| 526 | } |
| 527 | return false; |
| 528 | } |
| 529 | if (info.documentLaunchMode != DOCUMENT_LAUNCH_ALWAYS) { |
| 530 | if (enableLogging) { |
| 531 | StatsLog.write(StatsLog.BUBBLE_DEVELOPER_ERROR_REPORTED, packageName, |
| 532 | BUBBLE_DEVELOPER_ERROR_REPORTED__ERROR__DOCUMENT_LAUNCH_NOT_ALWAYS); |
| 533 | } |
| 534 | return false; |
| 535 | } |
| 536 | return (info.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) != 0; |
| 537 | } |
| 538 | |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 539 | /** |
| 540 | * Listener that is notified when a bubble is blocked. |
| 541 | */ |
| 542 | public interface OnBubbleBlockedListener { |
| 543 | /** |
| 544 | * Called when a bubble is blocked for the provided entry. |
| 545 | */ |
| 546 | void onBubbleBlocked(NotificationEntry entry); |
| 547 | } |
Steven Wu | b00225b | 2019-02-08 14:27:42 -0500 | [diff] [blame] | 548 | |
| 549 | /** |
| 550 | * Logs bubble UI click event. |
| 551 | * |
| 552 | * @param notification the bubble notification that user is interacting with. |
| 553 | * @param action the user interaction enum. |
| 554 | */ |
| 555 | private void logBubbleClickEvent(StatusBarNotification notification, int action) { |
| 556 | StatsLog.write(StatsLog.BUBBLE_UI_CHANGED, |
| 557 | notification.getPackageName(), |
| 558 | notification.getNotification().getChannelId(), |
| 559 | notification.getId(), |
| 560 | mStackView.getBubbleIndex(mStackView.getExpandedBubble()), |
| 561 | mStackView.getBubbleCount(), |
| 562 | action, |
| 563 | mStackView.getNormalizedXPosition(), |
| 564 | mStackView.getNormalizedYPosition()); |
| 565 | } |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 566 | } |