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; |
Mady Mellor | 390bff4 | 2019-04-05 15:09:01 -0700 | [diff] [blame] | 23 | import static android.view.Display.INVALID_DISPLAY; |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 24 | |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 25 | import android.animation.LayoutTransition; |
| 26 | import android.animation.ObjectAnimator; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 27 | import android.annotation.Nullable; |
Mady Mellor | 60101c9 | 2019-04-11 19:04:00 -0700 | [diff] [blame] | 28 | import android.app.ActivityOptions; |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 29 | import android.app.ActivityView; |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 30 | import android.app.INotificationManager; |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 31 | import android.app.Notification; |
| 32 | import android.app.PendingIntent; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 33 | import android.content.Context; |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 34 | import android.content.Intent; |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 35 | import android.content.pm.ActivityInfo; |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 36 | import android.content.pm.ApplicationInfo; |
| 37 | import android.content.pm.PackageManager; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 38 | import android.content.res.Resources; |
Mady Mellor | dd49705 | 2019-01-30 17:23:48 -0800 | [diff] [blame] | 39 | import android.content.res.TypedArray; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 40 | import android.graphics.Color; |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 41 | import android.graphics.Insets; |
| 42 | import android.graphics.Point; |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 43 | import android.graphics.drawable.Drawable; |
Mady Mellor | 310c7c6 | 2019-02-21 17:03:08 -0800 | [diff] [blame] | 44 | import android.graphics.drawable.GradientDrawable; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 45 | import android.graphics.drawable.ShapeDrawable; |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 46 | import android.os.RemoteException; |
| 47 | import android.os.ServiceManager; |
Mady Mellor | 7af771a | 2019-03-07 15:04:54 -0800 | [diff] [blame] | 48 | import android.os.UserHandle; |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 49 | import android.provider.Settings; |
Steven Wu | b00225b | 2019-02-08 14:27:42 -0500 | [diff] [blame] | 50 | import android.service.notification.StatusBarNotification; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 51 | import android.util.AttributeSet; |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 52 | import android.util.Log; |
Steven Wu | b00225b | 2019-02-08 14:27:42 -0500 | [diff] [blame] | 53 | import android.util.StatsLog; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 54 | import android.view.View; |
Mady Mellor | 5029fa6 | 2019-03-05 12:16:21 -0800 | [diff] [blame] | 55 | import android.view.ViewGroup; |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 56 | import android.view.WindowInsets; |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 57 | import android.widget.FrameLayout; |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 58 | import android.widget.ImageView; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 59 | import android.widget.LinearLayout; |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 60 | import android.widget.TextView; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 61 | |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 62 | import com.android.systemui.Dependency; |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 63 | import com.android.systemui.Interpolators; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 64 | import com.android.systemui.R; |
| 65 | import com.android.systemui.recents.TriangleShape; |
Lyn Han | 754e77b | 2019-04-30 14:34:49 -0700 | [diff] [blame] | 66 | import com.android.systemui.statusbar.AlphaOptimizedButton; |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 67 | import com.android.systemui.statusbar.notification.collection.NotificationEntry; |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 68 | import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow; |
| 69 | import com.android.systemui.statusbar.notification.stack.ExpandableViewState; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 70 | |
| 71 | /** |
Lyn Han | 02cca81 | 2019-04-02 16:27:32 -0700 | [diff] [blame] | 72 | * Container for the expanded bubble view, handles rendering the caret and settings icon. |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 73 | */ |
Mady Mellor | 3d82e68 | 2019-02-05 13:34:48 -0800 | [diff] [blame] | 74 | public class BubbleExpandedView extends LinearLayout implements View.OnClickListener { |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 75 | private static final String TAG = "BubbleExpandedView"; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 76 | |
| 77 | // The triangle pointing to the expanded view |
| 78 | private View mPointerView; |
Mady Mellor | 44ee2fe | 2019-01-30 17:51:16 -0800 | [diff] [blame] | 79 | private int mPointerMargin; |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 80 | |
Lyn Han | 754e77b | 2019-04-30 14:34:49 -0700 | [diff] [blame] | 81 | private AlphaOptimizedButton mSettingsIcon; |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 82 | |
| 83 | // Permission view |
| 84 | private View mPermissionView; |
Lyn Han | 02cca81 | 2019-04-02 16:27:32 -0700 | [diff] [blame] | 85 | private TextView mPermissionPrompt; |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 86 | |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 87 | // Views for expanded state |
| 88 | private ExpandableNotificationRow mNotifRow; |
| 89 | private ActivityView mActivityView; |
| 90 | |
| 91 | private boolean mActivityViewReady = false; |
| 92 | private PendingIntent mBubbleIntent; |
| 93 | |
Mady Mellor | 5d8f140 | 2019-02-21 18:23:52 -0800 | [diff] [blame] | 94 | private boolean mKeyboardVisible; |
| 95 | private boolean mNeedsNewHeight; |
| 96 | |
Mady Mellor | fe7ec03 | 2019-01-30 17:32:49 -0800 | [diff] [blame] | 97 | private int mMinHeight; |
Lyn Han | 02cca81 | 2019-04-02 16:27:32 -0700 | [diff] [blame] | 98 | private int mSettingsIconHeight; |
Mady Mellor | 44ee2fe | 2019-01-30 17:51:16 -0800 | [diff] [blame] | 99 | private int mBubbleHeight; |
| 100 | private int mPermissionHeight; |
Lyn Han | 02cca81 | 2019-04-02 16:27:32 -0700 | [diff] [blame] | 101 | private int mPointerWidth; |
| 102 | private int mPointerHeight; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 103 | |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 104 | private NotificationEntry mEntry; |
| 105 | private PackageManager mPm; |
| 106 | private String mAppName; |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 107 | private Drawable mAppIcon; |
| 108 | |
| 109 | private INotificationManager mNotificationManagerService; |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 110 | private BubbleController mBubbleController = Dependency.get(BubbleController.class); |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 111 | |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 112 | private BubbleStackView mStackView; |
| 113 | |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 114 | private BubbleExpandedView.OnBubbleBlockedListener mOnBubbleBlockedListener; |
| 115 | |
| 116 | private ActivityView.StateCallback mStateCallback = new ActivityView.StateCallback() { |
| 117 | @Override |
| 118 | public void onActivityViewReady(ActivityView view) { |
Mady Mellor | 6d00203 | 2019-02-13 13:45:17 -0800 | [diff] [blame] | 119 | if (!mActivityViewReady) { |
| 120 | mActivityViewReady = true; |
Mady Mellor | 60101c9 | 2019-04-11 19:04:00 -0700 | [diff] [blame] | 121 | // Custom options so there is no activity transition animation |
| 122 | ActivityOptions options = ActivityOptions.makeCustomAnimation(getContext(), |
| 123 | 0 /* enterResId */, 0 /* exitResId */); |
| 124 | // Post to keep the lifecycle normal |
| 125 | post(() -> mActivityView.startActivity(mBubbleIntent, options)); |
Mady Mellor | 6d00203 | 2019-02-13 13:45:17 -0800 | [diff] [blame] | 126 | } |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public void onActivityViewDestroyed(ActivityView view) { |
| 131 | mActivityViewReady = false; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * This is only called for tasks on this ActivityView, which is also set to |
| 136 | * single-task mode -- meaning never more than one task on this display. If a task |
| 137 | * is being removed, it's the top Activity finishing and this bubble should |
| 138 | * be removed or collapsed. |
| 139 | */ |
| 140 | @Override |
| 141 | public void onTaskRemovalStarted(int taskId) { |
| 142 | if (mEntry != null) { |
| 143 | // Must post because this is called from a binder thread. |
Mark Renouf | 08bc42a | 2019-03-07 13:01:59 -0500 | [diff] [blame] | 144 | post(() -> mBubbleController.removeBubble(mEntry.key, |
| 145 | BubbleController.DISMISS_TASK_FINISHED)); |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | }; |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 149 | |
Mady Mellor | 3d82e68 | 2019-02-05 13:34:48 -0800 | [diff] [blame] | 150 | public BubbleExpandedView(Context context) { |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 151 | this(context, null); |
| 152 | } |
| 153 | |
Mady Mellor | 3d82e68 | 2019-02-05 13:34:48 -0800 | [diff] [blame] | 154 | public BubbleExpandedView(Context context, AttributeSet attrs) { |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 155 | this(context, attrs, 0); |
| 156 | } |
| 157 | |
Mady Mellor | 3d82e68 | 2019-02-05 13:34:48 -0800 | [diff] [blame] | 158 | public BubbleExpandedView(Context context, AttributeSet attrs, int defStyleAttr) { |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 159 | this(context, attrs, defStyleAttr, 0); |
| 160 | } |
| 161 | |
Mady Mellor | 3d82e68 | 2019-02-05 13:34:48 -0800 | [diff] [blame] | 162 | public BubbleExpandedView(Context context, AttributeSet attrs, int defStyleAttr, |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 163 | int defStyleRes) { |
| 164 | super(context, attrs, defStyleAttr, defStyleRes); |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 165 | mPm = context.getPackageManager(); |
Mady Mellor | fe7ec03 | 2019-01-30 17:32:49 -0800 | [diff] [blame] | 166 | mMinHeight = getResources().getDimensionPixelSize( |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 167 | R.dimen.bubble_expanded_default_height); |
Mady Mellor | 44ee2fe | 2019-01-30 17:51:16 -0800 | [diff] [blame] | 168 | mPointerMargin = getResources().getDimensionPixelSize(R.dimen.bubble_pointer_margin); |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 169 | try { |
| 170 | mNotificationManagerService = INotificationManager.Stub.asInterface( |
| 171 | ServiceManager.getServiceOrThrow(Context.NOTIFICATION_SERVICE)); |
| 172 | } catch (ServiceManager.ServiceNotFoundException e) { |
| 173 | Log.w(TAG, e); |
| 174 | } |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | @Override |
| 178 | protected void onFinishInflate() { |
| 179 | super.onFinishInflate(); |
| 180 | |
| 181 | Resources res = getResources(); |
| 182 | mPointerView = findViewById(R.id.pointer_view); |
Lyn Han | 02cca81 | 2019-04-02 16:27:32 -0700 | [diff] [blame] | 183 | mPointerWidth = res.getDimensionPixelSize(R.dimen.bubble_pointer_width); |
| 184 | mPointerHeight = res.getDimensionPixelSize(R.dimen.bubble_pointer_height); |
Mady Mellor | dd49705 | 2019-01-30 17:23:48 -0800 | [diff] [blame] | 185 | |
| 186 | TypedArray ta = getContext().obtainStyledAttributes( |
| 187 | new int[] {android.R.attr.colorBackgroundFloating}); |
| 188 | int bgColor = ta.getColor(0, Color.WHITE); |
| 189 | ta.recycle(); |
| 190 | |
Lyn Han | 02cca81 | 2019-04-02 16:27:32 -0700 | [diff] [blame] | 191 | ShapeDrawable triangleDrawable = new ShapeDrawable(TriangleShape.create( |
| 192 | mPointerWidth, mPointerHeight, false /* pointUp */)); |
| 193 | |
Mady Mellor | dd49705 | 2019-01-30 17:23:48 -0800 | [diff] [blame] | 194 | triangleDrawable.setTint(bgColor); |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 195 | mPointerView.setBackground(triangleDrawable); |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 196 | |
Lyn Han | 02cca81 | 2019-04-02 16:27:32 -0700 | [diff] [blame] | 197 | FrameLayout permissionOrSettings = findViewById(R.id.permission_or_settings); |
Mady Mellor | 310c7c6 | 2019-02-21 17:03:08 -0800 | [diff] [blame] | 198 | |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 199 | LayoutTransition transition = new LayoutTransition(); |
| 200 | transition.setDuration(200); |
| 201 | |
| 202 | ObjectAnimator appearAnimator = ObjectAnimator.ofFloat(null, View.ALPHA, 0f, 1f); |
| 203 | transition.setAnimator(LayoutTransition.APPEARING, appearAnimator); |
| 204 | transition.setInterpolator(LayoutTransition.APPEARING, Interpolators.ALPHA_IN); |
| 205 | |
| 206 | ObjectAnimator disappearAnimator = ObjectAnimator.ofFloat(null, View.ALPHA, 1f, 0f); |
| 207 | transition.setAnimator(LayoutTransition.DISAPPEARING, disappearAnimator); |
| 208 | transition.setInterpolator(LayoutTransition.DISAPPEARING, Interpolators.ALPHA_OUT); |
| 209 | |
| 210 | transition.setAnimateParentHierarchy(false); |
Lyn Han | 02cca81 | 2019-04-02 16:27:32 -0700 | [diff] [blame] | 211 | permissionOrSettings.setLayoutTransition(transition); |
| 212 | permissionOrSettings.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING); |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 213 | |
Lyn Han | 02cca81 | 2019-04-02 16:27:32 -0700 | [diff] [blame] | 214 | mSettingsIconHeight = getContext().getResources().getDimensionPixelSize( |
Mady Mellor | fe7ec03 | 2019-01-30 17:32:49 -0800 | [diff] [blame] | 215 | R.dimen.bubble_expanded_header_height); |
Lyn Han | 02cca81 | 2019-04-02 16:27:32 -0700 | [diff] [blame] | 216 | mSettingsIcon = findViewById(R.id.settings_button); |
Lyn Han | 02cca81 | 2019-04-02 16:27:32 -0700 | [diff] [blame] | 217 | mSettingsIcon.setOnClickListener(this); |
Lyn Han | 02cca81 | 2019-04-02 16:27:32 -0700 | [diff] [blame] | 218 | |
Mady Mellor | 44ee2fe | 2019-01-30 17:51:16 -0800 | [diff] [blame] | 219 | mPermissionHeight = getContext().getResources().getDimensionPixelSize( |
| 220 | R.dimen.bubble_permission_height); |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 221 | mPermissionView = findViewById(R.id.permission_layout); |
Lyn Han | 02cca81 | 2019-04-02 16:27:32 -0700 | [diff] [blame] | 222 | mPermissionPrompt = mPermissionView.findViewById(R.id.prompt); |
Lyn Han | c26ff12 | 2019-03-29 16:46:07 -0700 | [diff] [blame] | 223 | |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 224 | findViewById(R.id.no_bubbles_button).setOnClickListener(this); |
| 225 | findViewById(R.id.yes_bubbles_button).setOnClickListener(this); |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 226 | |
| 227 | mActivityView = new ActivityView(mContext, null /* attrs */, 0 /* defStyle */, |
| 228 | true /* singleTaskInstance */); |
| 229 | addView(mActivityView); |
| 230 | |
Mady Mellor | 52b1ac6 | 2019-04-10 16:59:03 -0700 | [diff] [blame] | 231 | // Make sure pointer is below activity view |
| 232 | bringChildToFront(mPointerView); |
| 233 | |
Mady Mellor | 5d8f140 | 2019-02-21 18:23:52 -0800 | [diff] [blame] | 234 | 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 Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 237 | final int keyboardHeight = insets.getSystemWindowInsetBottom() |
| 238 | - insets.getStableInsetBottom(); |
Mady Mellor | 5d8f140 | 2019-02-21 18:23:52 -0800 | [diff] [blame] | 239 | mKeyboardVisible = keyboardHeight != 0; |
| 240 | if (!mKeyboardVisible && mNeedsNewHeight) { |
| 241 | updateHeight(); |
| 242 | } |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 243 | return view.onApplyWindowInsets(insets); |
| 244 | }); |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 245 | } |
| 246 | |
Mady Mellor | 5d8f140 | 2019-02-21 18:23:52 -0800 | [diff] [blame] | 247 | @Override |
| 248 | protected void onDetachedFromWindow() { |
| 249 | super.onDetachedFromWindow(); |
| 250 | mKeyboardVisible = false; |
| 251 | mNeedsNewHeight = false; |
| 252 | if (mActivityView != null) { |
| 253 | mActivityView.setForwardedInsets(Insets.of(0, 0, 0, 0)); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Called by {@link BubbleStackView} when the insets for the expanded state should be updated. |
| 259 | * This should be done post-move and post-animation. |
| 260 | */ |
| 261 | void updateInsets(WindowInsets insets) { |
| 262 | if (usingActivityView()) { |
| 263 | Point displaySize = new Point(); |
| 264 | mActivityView.getContext().getDisplay().getSize(displaySize); |
| 265 | int[] windowLocation = mActivityView.getLocationOnScreen(); |
| 266 | final int windowBottom = windowLocation[1] + mActivityView.getHeight(); |
| 267 | final int keyboardHeight = insets.getSystemWindowInsetBottom() |
| 268 | - insets.getStableInsetBottom(); |
| 269 | final int insetsBottom = Math.max(0, |
| 270 | windowBottom + keyboardHeight - displaySize.y); |
| 271 | mActivityView.setForwardedInsets(Insets.of(0, 0, 0, insetsBottom)); |
| 272 | } |
| 273 | } |
| 274 | |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 275 | /** |
Mady Mellor | 310c7c6 | 2019-02-21 17:03:08 -0800 | [diff] [blame] | 276 | * Creates a background with corners rounded based on how the view is configured to display |
| 277 | */ |
Lyn Han | 02cca81 | 2019-04-02 16:27:32 -0700 | [diff] [blame] | 278 | private Drawable createPermissionBackground(int bgColor) { |
Mady Mellor | 310c7c6 | 2019-02-21 17:03:08 -0800 | [diff] [blame] | 279 | TypedArray ta2 = getContext().obtainStyledAttributes( |
| 280 | new int[] {android.R.attr.dialogCornerRadius}); |
| 281 | final float cr = ta2.getDimension(0, 0f); |
| 282 | ta2.recycle(); |
| 283 | |
Lyn Han | 3f5c3a4 | 2019-04-01 15:59:56 -0700 | [diff] [blame] | 284 | float[] radii = new float[] {cr, cr, cr, cr, 0, 0, 0, 0}; |
Mady Mellor | 310c7c6 | 2019-02-21 17:03:08 -0800 | [diff] [blame] | 285 | GradientDrawable chromeBackground = new GradientDrawable(); |
| 286 | chromeBackground.setShape(GradientDrawable.RECTANGLE); |
| 287 | chromeBackground.setCornerRadii(radii); |
| 288 | chromeBackground.setColor(bgColor); |
| 289 | return chromeBackground; |
| 290 | } |
| 291 | |
| 292 | /** |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 293 | * Sets the listener to notify when a bubble has been blocked. |
| 294 | */ |
| 295 | public void setOnBlockedListener(OnBubbleBlockedListener listener) { |
| 296 | mOnBubbleBlockedListener = listener; |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | /** |
| 300 | * Sets the notification entry used to populate this view. |
| 301 | */ |
| 302 | public void setEntry(NotificationEntry entry, BubbleStackView stackView) { |
| 303 | mStackView = stackView; |
| 304 | mEntry = entry; |
| 305 | |
| 306 | ApplicationInfo info; |
| 307 | try { |
| 308 | info = mPm.getApplicationInfo( |
| 309 | entry.notification.getPackageName(), |
| 310 | PackageManager.MATCH_UNINSTALLED_PACKAGES |
| 311 | | PackageManager.MATCH_DISABLED_COMPONENTS |
| 312 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE |
| 313 | | PackageManager.MATCH_DIRECT_BOOT_AWARE); |
| 314 | if (info != null) { |
| 315 | mAppName = String.valueOf(mPm.getApplicationLabel(info)); |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 316 | mAppIcon = mPm.getApplicationIcon(info); |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 317 | } |
| 318 | } catch (PackageManager.NameNotFoundException e) { |
| 319 | // Ahh... just use package name |
| 320 | mAppName = entry.notification.getPackageName(); |
| 321 | } |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 322 | if (mAppIcon == null) { |
| 323 | mAppIcon = mPm.getDefaultActivityIcon(); |
| 324 | } |
Lyn Han | 02cca81 | 2019-04-02 16:27:32 -0700 | [diff] [blame] | 325 | updateTheme(); |
| 326 | togglePermissionOrSettings(); |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 327 | updateExpandedView(); |
Mady Mellor | 6d00203 | 2019-02-13 13:45:17 -0800 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | /** |
| 331 | * Lets activity view know it should be shown / populated. |
| 332 | */ |
Mady Mellor | 5029fa6 | 2019-03-05 12:16:21 -0800 | [diff] [blame] | 333 | public void populateExpandedView() { |
| 334 | if (usingActivityView()) { |
| 335 | mActivityView.setCallback(mStateCallback); |
| 336 | } else { |
| 337 | // We're using notification template |
| 338 | ViewGroup parent = (ViewGroup) mNotifRow.getParent(); |
| 339 | if (parent == this) { |
| 340 | // Already added |
| 341 | return; |
| 342 | } else if (parent != null) { |
| 343 | // Still in the shade... remove it |
| 344 | parent.removeView(mNotifRow); |
| 345 | } |
Lyn Han | 3f5c3a4 | 2019-04-01 15:59:56 -0700 | [diff] [blame] | 346 | addView(mNotifRow, 1 /* index */); |
Mady Mellor | 5029fa6 | 2019-03-05 12:16:21 -0800 | [diff] [blame] | 347 | } |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 348 | } |
| 349 | |
Mady Mellor | fe7ec03 | 2019-01-30 17:32:49 -0800 | [diff] [blame] | 350 | /** |
| 351 | * Updates the entry backing this view. This will not re-populate ActivityView, it will |
Lyn Han | 02cca81 | 2019-04-02 16:27:32 -0700 | [diff] [blame] | 352 | * only update the deep-links in the title, and the height of the view. |
Mady Mellor | fe7ec03 | 2019-01-30 17:32:49 -0800 | [diff] [blame] | 353 | */ |
| 354 | public void update(NotificationEntry entry) { |
| 355 | if (entry.key.equals(mEntry.key)) { |
| 356 | mEntry = entry; |
Lyn Han | 02cca81 | 2019-04-02 16:27:32 -0700 | [diff] [blame] | 357 | updateSettingsContentDescription(); |
Mady Mellor | fe7ec03 | 2019-01-30 17:32:49 -0800 | [diff] [blame] | 358 | updateHeight(); |
| 359 | } else { |
| 360 | Log.w(TAG, "Trying to update entry with different key, new entry: " |
| 361 | + entry.key + " old entry: " + mEntry.key); |
| 362 | } |
| 363 | } |
| 364 | |
Lyn Han | 02cca81 | 2019-04-02 16:27:32 -0700 | [diff] [blame] | 365 | void updateTheme() { |
| 366 | // Get new colors. |
Lyn Han | 76e803d | 2019-03-26 17:31:33 -0700 | [diff] [blame] | 367 | TypedArray ta = mContext.obtainStyledAttributes( |
Lyn Han | 02cca81 | 2019-04-02 16:27:32 -0700 | [diff] [blame] | 368 | new int[]{android.R.attr.colorBackgroundFloating, android.R.attr.colorForeground}); |
Lyn Han | c26ff12 | 2019-03-29 16:46:07 -0700 | [diff] [blame] | 369 | int backgroundColor = ta.getColor(0, Color.WHITE /* default */); |
| 370 | int foregroundColor = ta.getColor(1, Color.BLACK /* default */); |
Lyn Han | 76e803d | 2019-03-26 17:31:33 -0700 | [diff] [blame] | 371 | ta.recycle(); |
| 372 | |
Lyn Han | 02cca81 | 2019-04-02 16:27:32 -0700 | [diff] [blame] | 373 | // Update permission prompt color. |
| 374 | mPermissionView.setBackground(createPermissionBackground(backgroundColor)); |
| 375 | mPermissionPrompt.setTextColor(foregroundColor); |
| 376 | |
| 377 | // Update triangle color. |
| 378 | ShapeDrawable triangleDrawable = new ShapeDrawable( |
| 379 | TriangleShape.create(mPointerWidth, mPointerHeight, false /* pointUp */)); |
| 380 | triangleDrawable.setTint(backgroundColor); |
| 381 | mPointerView.setBackground(triangleDrawable); |
Lyn Han | f1c9b8b | 2019-03-14 16:49:48 -0700 | [diff] [blame] | 382 | } |
| 383 | |
Lyn Han | 02cca81 | 2019-04-02 16:27:32 -0700 | [diff] [blame] | 384 | void togglePermissionOrSettings() { |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 385 | boolean hasUserApprovedBubblesForPackage = false; |
| 386 | try { |
| 387 | hasUserApprovedBubblesForPackage = |
| 388 | mNotificationManagerService.hasUserApprovedBubblesForPackage( |
| 389 | mEntry.notification.getPackageName(), mEntry.notification.getUid()); |
| 390 | } catch (RemoteException e) { |
| 391 | Log.w(TAG, e); |
| 392 | } |
| 393 | if (hasUserApprovedBubblesForPackage) { |
Lyn Han | c26ff12 | 2019-03-29 16:46:07 -0700 | [diff] [blame] | 394 | showSettingsIcon(); |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 395 | } else { |
Lyn Han | c26ff12 | 2019-03-29 16:46:07 -0700 | [diff] [blame] | 396 | showPermissionView(); |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 397 | } |
| 398 | } |
| 399 | |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 400 | private void updateExpandedView() { |
| 401 | mBubbleIntent = getBubbleIntent(mEntry); |
| 402 | if (mBubbleIntent != null) { |
| 403 | if (mNotifRow != null) { |
| 404 | // Clear out the row if we had it previously |
| 405 | removeView(mNotifRow); |
| 406 | mNotifRow = null; |
| 407 | } |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 408 | mActivityView.setVisibility(VISIBLE); |
| 409 | } else { |
| 410 | // Hide activity view if we had it previously |
| 411 | mActivityView.setVisibility(GONE); |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 412 | mNotifRow = mEntry.getRow(); |
Mady Mellor | 5029fa6 | 2019-03-05 12:16:21 -0800 | [diff] [blame] | 413 | |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 414 | } |
| 415 | updateView(); |
| 416 | } |
| 417 | |
Mark Renouf | 041d726 | 2019-02-06 12:09:41 -0500 | [diff] [blame] | 418 | boolean performBackPressIfNeeded() { |
Mady Mellor | 323fb0b | 2019-03-25 12:15:22 -0700 | [diff] [blame] | 419 | if (!usingActivityView()) { |
Mark Renouf | 041d726 | 2019-02-06 12:09:41 -0500 | [diff] [blame] | 420 | return false; |
| 421 | } |
| 422 | mActivityView.performBackPress(); |
| 423 | return true; |
| 424 | } |
| 425 | |
Mady Mellor | 44ee2fe | 2019-01-30 17:51:16 -0800 | [diff] [blame] | 426 | /** |
| 427 | * @return total height that the expanded view occupies. |
| 428 | */ |
| 429 | int getExpandedSize() { |
| 430 | int chromeHeight = mPermissionView.getVisibility() != View.VISIBLE |
Lyn Han | 02cca81 | 2019-04-02 16:27:32 -0700 | [diff] [blame] | 431 | ? mSettingsIconHeight |
Mady Mellor | 44ee2fe | 2019-01-30 17:51:16 -0800 | [diff] [blame] | 432 | : mPermissionHeight; |
| 433 | return mBubbleHeight + mPointerView.getHeight() + mPointerMargin |
| 434 | + chromeHeight; |
| 435 | } |
| 436 | |
Mady Mellor | fe7ec03 | 2019-01-30 17:32:49 -0800 | [diff] [blame] | 437 | void updateHeight() { |
| 438 | if (usingActivityView()) { |
| 439 | Notification.BubbleMetadata data = mEntry.getBubbleMetadata(); |
Mady Mellor | 7af771a | 2019-03-07 15:04:54 -0800 | [diff] [blame] | 440 | float desiredHeight; |
Mady Mellor | fe7ec03 | 2019-01-30 17:32:49 -0800 | [diff] [blame] | 441 | if (data == null) { |
| 442 | // This is a contentIntent based bubble, lets allow it to be the max height |
| 443 | // as it was forced into this mode and not prepared to be small |
| 444 | desiredHeight = mStackView.getMaxExpandedHeight(); |
| 445 | } else { |
Mady Mellor | 7af771a | 2019-03-07 15:04:54 -0800 | [diff] [blame] | 446 | boolean useRes = data.getDesiredHeightResId() != 0; |
| 447 | float desiredPx; |
| 448 | if (useRes) { |
| 449 | desiredPx = getDimenForPackageUser(data.getDesiredHeightResId(), |
| 450 | mEntry.notification.getPackageName(), |
| 451 | mEntry.notification.getUser().getIdentifier()); |
| 452 | } else { |
| 453 | desiredPx = data.getDesiredHeight() |
| 454 | * getContext().getResources().getDisplayMetrics().density; |
| 455 | } |
| 456 | desiredHeight = desiredPx > 0 ? desiredPx : mMinHeight; |
Mady Mellor | fe7ec03 | 2019-01-30 17:32:49 -0800 | [diff] [blame] | 457 | } |
Mady Mellor | 44ee2fe | 2019-01-30 17:51:16 -0800 | [diff] [blame] | 458 | int chromeHeight = mPermissionView.getVisibility() != View.VISIBLE |
Lyn Han | 02cca81 | 2019-04-02 16:27:32 -0700 | [diff] [blame] | 459 | ? mSettingsIconHeight |
Mady Mellor | 44ee2fe | 2019-01-30 17:51:16 -0800 | [diff] [blame] | 460 | : mPermissionHeight; |
| 461 | int max = mStackView.getMaxExpandedHeight() - chromeHeight - mPointerView.getHeight() |
| 462 | - mPointerMargin; |
Mady Mellor | 7af771a | 2019-03-07 15:04:54 -0800 | [diff] [blame] | 463 | float height = Math.min(desiredHeight, max); |
Mady Mellor | fe7ec03 | 2019-01-30 17:32:49 -0800 | [diff] [blame] | 464 | height = Math.max(height, mMinHeight); |
| 465 | LayoutParams lp = (LayoutParams) mActivityView.getLayoutParams(); |
Mady Mellor | 5d8f140 | 2019-02-21 18:23:52 -0800 | [diff] [blame] | 466 | mNeedsNewHeight = lp.height != height; |
| 467 | if (!mKeyboardVisible) { |
| 468 | // If the keyboard is visible... don't adjust the height because that will cause |
| 469 | // a configuration change and the keyboard will be lost. |
Mady Mellor | 7af771a | 2019-03-07 15:04:54 -0800 | [diff] [blame] | 470 | lp.height = (int) height; |
| 471 | mBubbleHeight = (int) height; |
Mady Mellor | 5d8f140 | 2019-02-21 18:23:52 -0800 | [diff] [blame] | 472 | mActivityView.setLayoutParams(lp); |
| 473 | mNeedsNewHeight = false; |
| 474 | } |
Mady Mellor | 44ee2fe | 2019-01-30 17:51:16 -0800 | [diff] [blame] | 475 | } else { |
| 476 | mBubbleHeight = mNotifRow != null ? mNotifRow.getIntrinsicHeight() : mMinHeight; |
Mady Mellor | fe7ec03 | 2019-01-30 17:32:49 -0800 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 480 | @Override |
| 481 | public void onClick(View view) { |
| 482 | if (mEntry == null) { |
| 483 | return; |
| 484 | } |
| 485 | Notification n = mEntry.notification.getNotification(); |
| 486 | int id = view.getId(); |
Lyn Han | c26ff12 | 2019-03-29 16:46:07 -0700 | [diff] [blame] | 487 | if (id == R.id.settings_button) { |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 488 | Intent intent = getSettingsIntent(mEntry.notification.getPackageName(), |
| 489 | mEntry.notification.getUid()); |
Steven Wu | b00225b | 2019-02-08 14:27:42 -0500 | [diff] [blame] | 490 | mStackView.collapseStack(() -> { |
| 491 | mContext.startActivity(intent); |
Steven Wu | 45e38ae | 2019-03-25 16:16:59 -0400 | [diff] [blame] | 492 | logBubbleClickEvent(mEntry, |
Steven Wu | b00225b | 2019-02-08 14:27:42 -0500 | [diff] [blame] | 493 | StatsLog.BUBBLE_UICHANGED__ACTION__HEADER_GO_TO_SETTINGS); |
| 494 | }); |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 495 | } else if (id == R.id.no_bubbles_button) { |
| 496 | setBubblesAllowed(false); |
| 497 | } else if (id == R.id.yes_bubbles_button) { |
| 498 | setBubblesAllowed(true); |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | private void setBubblesAllowed(boolean allowed) { |
| 503 | try { |
| 504 | mNotificationManagerService.setBubblesAllowed( |
| 505 | mEntry.notification.getPackageName(), |
| 506 | mEntry.notification.getUid(), |
| 507 | allowed); |
| 508 | if (allowed) { |
Lyn Han | c26ff12 | 2019-03-29 16:46:07 -0700 | [diff] [blame] | 509 | showSettingsIcon(); |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 510 | } else if (mOnBubbleBlockedListener != null) { |
| 511 | mOnBubbleBlockedListener.onBubbleBlocked(mEntry); |
| 512 | } |
Mady Mellor | 44ee2fe | 2019-01-30 17:51:16 -0800 | [diff] [blame] | 513 | mStackView.onExpandedHeightChanged(); |
Steven Wu | 45e38ae | 2019-03-25 16:16:59 -0400 | [diff] [blame] | 514 | logBubbleClickEvent(mEntry, |
Steven Wu | b00225b | 2019-02-08 14:27:42 -0500 | [diff] [blame] | 515 | allowed ? StatsLog.BUBBLE_UICHANGED__ACTION__PERMISSION_OPT_IN : |
| 516 | StatsLog.BUBBLE_UICHANGED__ACTION__PERMISSION_OPT_OUT); |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 517 | } catch (RemoteException e) { |
| 518 | Log.w(TAG, e); |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 519 | } |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 520 | } |
| 521 | |
Lyn Han | 02cca81 | 2019-04-02 16:27:32 -0700 | [diff] [blame] | 522 | private void updateSettingsContentDescription() { |
| 523 | mSettingsIcon.setContentDescription(getResources().getString( |
| 524 | R.string.bubbles_settings_button_description, mAppName)); |
| 525 | } |
| 526 | |
Lyn Han | c26ff12 | 2019-03-29 16:46:07 -0700 | [diff] [blame] | 527 | void showSettingsIcon() { |
Lyn Han | 02cca81 | 2019-04-02 16:27:32 -0700 | [diff] [blame] | 528 | updateSettingsContentDescription(); |
| 529 | |
Lyn Han | c26ff12 | 2019-03-29 16:46:07 -0700 | [diff] [blame] | 530 | mPermissionView.setVisibility(GONE); |
| 531 | mSettingsIcon.setVisibility(VISIBLE); |
| 532 | } |
| 533 | |
| 534 | void showPermissionView() { |
Lyn Han | 02cca81 | 2019-04-02 16:27:32 -0700 | [diff] [blame] | 535 | ((ImageView) mPermissionView.findViewById(R.id.pkgicon)).setImageDrawable(mAppIcon); |
| 536 | ((TextView) mPermissionView.findViewById(R.id.pkgname)).setText(mAppName); |
| 537 | mPermissionPrompt.setText( |
| 538 | getResources().getString(R.string.bubbles_prompt, mAppName)); |
| 539 | logBubbleClickEvent(mEntry, |
| 540 | StatsLog.BUBBLE_UICHANGED__ACTION__PERMISSION_DIALOG_SHOWN); |
| 541 | |
Lyn Han | c26ff12 | 2019-03-29 16:46:07 -0700 | [diff] [blame] | 542 | mSettingsIcon.setVisibility(GONE); |
| 543 | mPermissionView.setVisibility(VISIBLE); |
Lyn Han | c26ff12 | 2019-03-29 16:46:07 -0700 | [diff] [blame] | 544 | } |
| 545 | |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 546 | /** |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 547 | * Update appearance of the expanded view being displayed. |
| 548 | */ |
| 549 | public void updateView() { |
| 550 | if (usingActivityView() |
| 551 | && mActivityView.getVisibility() == VISIBLE |
| 552 | && mActivityView.isAttachedToWindow()) { |
| 553 | mActivityView.onLocationChanged(); |
| 554 | } else if (mNotifRow != null) { |
| 555 | applyRowState(mNotifRow); |
| 556 | } |
Mady Mellor | fe7ec03 | 2019-01-30 17:32:49 -0800 | [diff] [blame] | 557 | updateHeight(); |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | /** |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 561 | * Set the x position that the tip of the triangle should point to. |
| 562 | */ |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 563 | public void setPointerPosition(float x) { |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 564 | // Adjust for the pointer size |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 565 | x -= (mPointerView.getWidth() / 2f); |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 566 | mPointerView.setTranslationX(x); |
| 567 | } |
| 568 | |
| 569 | /** |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 570 | * 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] | 571 | */ |
Mady Mellor | 94d94a7 | 2019-03-05 18:16:59 -0800 | [diff] [blame] | 572 | public void cleanUpExpandedState() { |
| 573 | removeView(mNotifRow); |
| 574 | |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 575 | if (mActivityView == null) { |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 576 | return; |
| 577 | } |
Mark Renouf | 28c250d | 2019-02-25 16:47:34 -0500 | [diff] [blame] | 578 | if (mActivityViewReady) { |
| 579 | mActivityView.release(); |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 580 | } |
Mark Renouf | 28c250d | 2019-02-25 16:47:34 -0500 | [diff] [blame] | 581 | removeView(mActivityView); |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 582 | mActivityView = null; |
| 583 | mActivityViewReady = false; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 584 | } |
| 585 | |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 586 | private boolean usingActivityView() { |
Mady Mellor | 323fb0b | 2019-03-25 12:15:22 -0700 | [diff] [blame] | 587 | return mBubbleIntent != null && mActivityView != null; |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 588 | } |
| 589 | |
Mady Mellor | 390bff4 | 2019-04-05 15:09:01 -0700 | [diff] [blame] | 590 | /** |
| 591 | * @return the display id of the virtual display. |
| 592 | */ |
| 593 | public int getVirtualDisplayId() { |
| 594 | if (usingActivityView()) { |
| 595 | return mActivityView.getVirtualDisplayId(); |
| 596 | } |
| 597 | return INVALID_DISPLAY; |
| 598 | } |
| 599 | |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 600 | private void applyRowState(ExpandableNotificationRow view) { |
| 601 | view.reset(); |
| 602 | view.setHeadsUp(false); |
| 603 | view.resetTranslation(); |
| 604 | view.setOnKeyguard(false); |
| 605 | view.setOnAmbient(false); |
| 606 | view.setClipBottomAmount(0); |
| 607 | view.setClipTopAmount(0); |
| 608 | view.setContentTransformationAmount(0, false); |
| 609 | view.setIconsVisible(true); |
| 610 | |
| 611 | // TODO - Need to reset this (and others) when view goes back in shade, leave for now |
| 612 | // view.setTopRoundness(1, false); |
| 613 | // view.setBottomRoundness(1, false); |
| 614 | |
| 615 | ExpandableViewState viewState = view.getViewState(); |
| 616 | viewState = viewState == null ? new ExpandableViewState() : viewState; |
| 617 | viewState.height = view.getIntrinsicHeight(); |
| 618 | viewState.gone = false; |
| 619 | viewState.hidden = false; |
| 620 | viewState.dimmed = false; |
| 621 | viewState.dark = false; |
| 622 | viewState.alpha = 1f; |
| 623 | viewState.notGoneIndex = -1; |
| 624 | viewState.xTranslation = 0; |
| 625 | viewState.yTranslation = 0; |
| 626 | viewState.zTranslation = 0; |
| 627 | viewState.scaleX = 1; |
| 628 | viewState.scaleY = 1; |
| 629 | viewState.inShelf = true; |
| 630 | viewState.headsUpIsVisible = false; |
| 631 | viewState.applyToView(view); |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 632 | } |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 633 | |
| 634 | private Intent getSettingsIntent(String packageName, final int appUid) { |
Lyn Han | 754e77b | 2019-04-30 14:34:49 -0700 | [diff] [blame] | 635 | final Intent intent = new Intent(Settings.ACTION_APP_NOTIFICATION_BUBBLE_SETTINGS); |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 636 | intent.putExtra(Settings.EXTRA_APP_PACKAGE, packageName); |
| 637 | intent.putExtra(Settings.EXTRA_APP_UID, appUid); |
| 638 | intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK); |
| 639 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
Lyn Han | 754e77b | 2019-04-30 14:34:49 -0700 | [diff] [blame] | 640 | intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 641 | return intent; |
| 642 | } |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 643 | |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 644 | @Nullable |
| 645 | private PendingIntent getBubbleIntent(NotificationEntry entry) { |
| 646 | Notification notif = entry.notification.getNotification(); |
| 647 | String packageName = entry.notification.getPackageName(); |
| 648 | Notification.BubbleMetadata data = notif.getBubbleMetadata(); |
| 649 | if (data != null && canLaunchInActivityView(data.getIntent(), true /* enableLogging */, |
| 650 | packageName)) { |
| 651 | return data.getIntent(); |
| 652 | } else if (BubbleController.shouldUseContentIntent(mContext) |
| 653 | && canLaunchInActivityView(notif.contentIntent, false /* enableLogging */, |
| 654 | packageName)) { |
| 655 | return notif.contentIntent; |
| 656 | } |
| 657 | return null; |
| 658 | } |
| 659 | |
| 660 | /** |
| 661 | * Whether an intent is properly configured to display in an {@link android.app.ActivityView}. |
| 662 | * |
| 663 | * @param intent the pending intent of the bubble. |
| 664 | * @param enableLogging whether bubble developer error should be logged. |
| 665 | * @param packageName the notification package name for this bubble. |
| 666 | * @return |
| 667 | */ |
| 668 | private boolean canLaunchInActivityView(PendingIntent intent, boolean enableLogging, |
| 669 | String packageName) { |
| 670 | if (intent == null) { |
| 671 | return false; |
| 672 | } |
| 673 | ActivityInfo info = |
| 674 | intent.getIntent().resolveActivityInfo(mContext.getPackageManager(), 0); |
| 675 | if (info == null) { |
| 676 | if (enableLogging) { |
| 677 | StatsLog.write(StatsLog.BUBBLE_DEVELOPER_ERROR_REPORTED, packageName, |
| 678 | BUBBLE_DEVELOPER_ERROR_REPORTED__ERROR__ACTIVITY_INFO_MISSING); |
| 679 | } |
| 680 | return false; |
| 681 | } |
| 682 | if (!ActivityInfo.isResizeableMode(info.resizeMode)) { |
| 683 | if (enableLogging) { |
| 684 | StatsLog.write(StatsLog.BUBBLE_DEVELOPER_ERROR_REPORTED, packageName, |
| 685 | BUBBLE_DEVELOPER_ERROR_REPORTED__ERROR__ACTIVITY_INFO_NOT_RESIZABLE); |
| 686 | } |
| 687 | return false; |
| 688 | } |
| 689 | if (info.documentLaunchMode != DOCUMENT_LAUNCH_ALWAYS) { |
| 690 | if (enableLogging) { |
| 691 | StatsLog.write(StatsLog.BUBBLE_DEVELOPER_ERROR_REPORTED, packageName, |
| 692 | BUBBLE_DEVELOPER_ERROR_REPORTED__ERROR__DOCUMENT_LAUNCH_NOT_ALWAYS); |
| 693 | } |
| 694 | return false; |
| 695 | } |
| 696 | return (info.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) != 0; |
| 697 | } |
| 698 | |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 699 | /** |
| 700 | * Listener that is notified when a bubble is blocked. |
| 701 | */ |
| 702 | public interface OnBubbleBlockedListener { |
| 703 | /** |
| 704 | * Called when a bubble is blocked for the provided entry. |
| 705 | */ |
| 706 | void onBubbleBlocked(NotificationEntry entry); |
| 707 | } |
Steven Wu | b00225b | 2019-02-08 14:27:42 -0500 | [diff] [blame] | 708 | |
| 709 | /** |
| 710 | * Logs bubble UI click event. |
| 711 | * |
Steven Wu | 45e38ae | 2019-03-25 16:16:59 -0400 | [diff] [blame] | 712 | * @param entry the bubble notification entry that user is interacting with. |
Steven Wu | b00225b | 2019-02-08 14:27:42 -0500 | [diff] [blame] | 713 | * @param action the user interaction enum. |
| 714 | */ |
Steven Wu | 45e38ae | 2019-03-25 16:16:59 -0400 | [diff] [blame] | 715 | private void logBubbleClickEvent(NotificationEntry entry, int action) { |
| 716 | StatusBarNotification notification = entry.notification; |
Steven Wu | b00225b | 2019-02-08 14:27:42 -0500 | [diff] [blame] | 717 | StatsLog.write(StatsLog.BUBBLE_UI_CHANGED, |
| 718 | notification.getPackageName(), |
| 719 | notification.getNotification().getChannelId(), |
| 720 | notification.getId(), |
| 721 | mStackView.getBubbleIndex(mStackView.getExpandedBubble()), |
| 722 | mStackView.getBubbleCount(), |
| 723 | action, |
| 724 | mStackView.getNormalizedXPosition(), |
Steven Wu | 45e38ae | 2019-03-25 16:16:59 -0400 | [diff] [blame] | 725 | mStackView.getNormalizedYPosition(), |
Steven Wu | 8ba8ca9 | 2019-04-11 10:47:42 -0400 | [diff] [blame] | 726 | entry.showInShadeWhenBubble(), |
| 727 | entry.isForegroundService(), |
| 728 | BubbleController.isForegroundApp(mContext, notification.getPackageName())); |
Steven Wu | b00225b | 2019-02-08 14:27:42 -0500 | [diff] [blame] | 729 | } |
Mady Mellor | 7af771a | 2019-03-07 15:04:54 -0800 | [diff] [blame] | 730 | |
| 731 | private int getDimenForPackageUser(int resId, String pkg, int userId) { |
| 732 | Resources r; |
| 733 | if (pkg != null) { |
| 734 | try { |
| 735 | if (userId == UserHandle.USER_ALL) { |
| 736 | userId = UserHandle.USER_SYSTEM; |
| 737 | } |
| 738 | r = mPm.getResourcesForApplicationAsUser(pkg, userId); |
| 739 | return r.getDimensionPixelSize(resId); |
| 740 | } catch (PackageManager.NameNotFoundException ex) { |
| 741 | // Uninstalled, don't care |
| 742 | } catch (Resources.NotFoundException e) { |
| 743 | // Invalid res id, return 0 and user our default |
| 744 | Log.e(TAG, "Couldn't find desired height res id", e); |
| 745 | } |
| 746 | } |
| 747 | return 0; |
| 748 | } |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 749 | } |