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 | 390bff4 | 2019-04-05 15:09:01 -0700 | [diff] [blame] | 19 | import static android.view.Display.INVALID_DISPLAY; |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 20 | |
Issei Suzuki | a8d0731 | 2019-06-07 12:56:19 +0200 | [diff] [blame] | 21 | import static com.android.systemui.bubbles.BubbleDebugConfig.TAG_BUBBLES; |
| 22 | import static com.android.systemui.bubbles.BubbleDebugConfig.TAG_WITH_CLASS_NAME; |
Mady Mellor | ca0c24c | 2019-05-16 16:14:32 -0700 | [diff] [blame] | 23 | |
Mady Mellor | 60101c9 | 2019-04-11 19:04:00 -0700 | [diff] [blame] | 24 | import android.app.ActivityOptions; |
Mark Renouf | 5c732b3 | 2019-06-12 15:14:54 -0400 | [diff] [blame] | 25 | import android.app.ActivityTaskManager; |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 26 | import android.app.ActivityView; |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 27 | import android.app.PendingIntent; |
Mark Renouf | 5c732b3 | 2019-06-12 15:14:54 -0400 | [diff] [blame] | 28 | import android.content.ComponentName; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 29 | import android.content.Context; |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 30 | import android.content.Intent; |
| 31 | import android.content.pm.ApplicationInfo; |
| 32 | import android.content.pm.PackageManager; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 33 | import android.content.res.Resources; |
Mady Mellor | dd49705 | 2019-01-30 17:23:48 -0800 | [diff] [blame] | 34 | import android.content.res.TypedArray; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 35 | import android.graphics.Color; |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 36 | import android.graphics.Insets; |
| 37 | import android.graphics.Point; |
Mady Mellor | 47b11e3 | 2019-07-11 19:06:21 -0700 | [diff] [blame^] | 38 | import android.graphics.Rect; |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 39 | import android.graphics.drawable.Drawable; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 40 | import android.graphics.drawable.ShapeDrawable; |
Mark Renouf | 5c732b3 | 2019-06-12 15:14:54 -0400 | [diff] [blame] | 41 | import android.os.RemoteException; |
Steven Wu | b00225b | 2019-02-08 14:27:42 -0500 | [diff] [blame] | 42 | import android.service.notification.StatusBarNotification; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 43 | import android.util.AttributeSet; |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 44 | import android.util.Log; |
Steven Wu | b00225b | 2019-02-08 14:27:42 -0500 | [diff] [blame] | 45 | import android.util.StatsLog; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 46 | import android.view.View; |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 47 | import android.view.WindowInsets; |
Mady Mellor | a96c9ed | 2019-06-07 12:55:26 -0700 | [diff] [blame] | 48 | import android.view.WindowManager; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 49 | import android.widget.LinearLayout; |
| 50 | |
Mark Renouf | 34d04f3 | 2019-05-13 15:53:18 -0400 | [diff] [blame] | 51 | import com.android.internal.policy.ScreenDecorationsUtils; |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 52 | import com.android.systemui.Dependency; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 53 | import com.android.systemui.R; |
| 54 | import com.android.systemui.recents.TriangleShape; |
Lyn Han | 754e77b | 2019-04-30 14:34:49 -0700 | [diff] [blame] | 55 | import com.android.systemui.statusbar.AlphaOptimizedButton; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 56 | |
| 57 | /** |
Lyn Han | 02cca81 | 2019-04-02 16:27:32 -0700 | [diff] [blame] | 58 | * 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] | 59 | */ |
Mady Mellor | 3d82e68 | 2019-02-05 13:34:48 -0800 | [diff] [blame] | 60 | public class BubbleExpandedView extends LinearLayout implements View.OnClickListener { |
Issei Suzuki | a8d0731 | 2019-06-07 12:56:19 +0200 | [diff] [blame] | 61 | private static final String TAG = TAG_WITH_CLASS_NAME ? "BubbleExpandedView" : TAG_BUBBLES; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 62 | |
Issei Suzuki | 734bc94 | 2019-06-05 13:59:52 +0200 | [diff] [blame] | 63 | private enum ActivityViewStatus { |
| 64 | // ActivityView is being initialized, cannot start an activity yet. |
| 65 | INITIALIZING, |
| 66 | // ActivityView is initialized, and ready to start an activity. |
| 67 | INITIALIZED, |
| 68 | // Activity runs in the ActivityView. |
| 69 | ACTIVITY_STARTED, |
| 70 | // ActivityView is released, so activity launching will no longer be permitted. |
| 71 | RELEASED, |
| 72 | } |
| 73 | |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 74 | // The triangle pointing to the expanded view |
| 75 | private View mPointerView; |
Mady Mellor | 44ee2fe | 2019-01-30 17:51:16 -0800 | [diff] [blame] | 76 | private int mPointerMargin; |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 77 | |
Lyn Han | 754e77b | 2019-04-30 14:34:49 -0700 | [diff] [blame] | 78 | private AlphaOptimizedButton mSettingsIcon; |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 79 | |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 80 | // Views for expanded state |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 81 | private ActivityView mActivityView; |
| 82 | |
Issei Suzuki | 734bc94 | 2019-06-05 13:59:52 +0200 | [diff] [blame] | 83 | private ActivityViewStatus mActivityViewStatus = ActivityViewStatus.INITIALIZING; |
Mark Renouf | 5c732b3 | 2019-06-12 15:14:54 -0400 | [diff] [blame] | 84 | private int mTaskId = -1; |
| 85 | |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 86 | private PendingIntent mBubbleIntent; |
| 87 | |
Mady Mellor | 5d8f140 | 2019-02-21 18:23:52 -0800 | [diff] [blame] | 88 | private boolean mKeyboardVisible; |
| 89 | private boolean mNeedsNewHeight; |
| 90 | |
Mady Mellor | a96c9ed | 2019-06-07 12:55:26 -0700 | [diff] [blame] | 91 | private Point mDisplaySize; |
Mady Mellor | fe7ec03 | 2019-01-30 17:32:49 -0800 | [diff] [blame] | 92 | private int mMinHeight; |
Lyn Han | 02cca81 | 2019-04-02 16:27:32 -0700 | [diff] [blame] | 93 | private int mSettingsIconHeight; |
Lyn Han | 02cca81 | 2019-04-02 16:27:32 -0700 | [diff] [blame] | 94 | private int mPointerWidth; |
| 95 | private int mPointerHeight; |
Mark Renouf | 34d04f3 | 2019-05-13 15:53:18 -0400 | [diff] [blame] | 96 | private ShapeDrawable mPointerDrawable; |
Mady Mellor | 47b11e3 | 2019-07-11 19:06:21 -0700 | [diff] [blame^] | 97 | private Rect mTempRect = new Rect(); |
| 98 | private int[] mTempLoc = new int[2]; |
| 99 | private int mExpandedViewTouchSlop; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 100 | |
Mady Mellor | 99a30260 | 2019-06-14 11:39:56 -0700 | [diff] [blame] | 101 | private Bubble mBubble; |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 102 | private PackageManager mPm; |
| 103 | private String mAppName; |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 104 | private Drawable mAppIcon; |
| 105 | |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 106 | private BubbleController mBubbleController = Dependency.get(BubbleController.class); |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 107 | |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 108 | private BubbleStackView mStackView; |
| 109 | |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 110 | private ActivityView.StateCallback mStateCallback = new ActivityView.StateCallback() { |
| 111 | @Override |
| 112 | public void onActivityViewReady(ActivityView view) { |
Issei Suzuki | 734bc94 | 2019-06-05 13:59:52 +0200 | [diff] [blame] | 113 | switch (mActivityViewStatus) { |
| 114 | case INITIALIZING: |
| 115 | case INITIALIZED: |
| 116 | // Custom options so there is no activity transition animation |
| 117 | ActivityOptions options = ActivityOptions.makeCustomAnimation(getContext(), |
| 118 | 0 /* enterResId */, 0 /* exitResId */); |
| 119 | // Post to keep the lifecycle normal |
| 120 | post(() -> mActivityView.startActivity(mBubbleIntent, options)); |
| 121 | mActivityViewStatus = ActivityViewStatus.ACTIVITY_STARTED; |
Mady Mellor | 6d00203 | 2019-02-13 13:45:17 -0800 | [diff] [blame] | 122 | } |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public void onActivityViewDestroyed(ActivityView view) { |
Issei Suzuki | 734bc94 | 2019-06-05 13:59:52 +0200 | [diff] [blame] | 127 | mActivityViewStatus = ActivityViewStatus.RELEASED; |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 128 | } |
| 129 | |
Mark Renouf | 5c732b3 | 2019-06-12 15:14:54 -0400 | [diff] [blame] | 130 | @Override |
| 131 | public void onTaskCreated(int taskId, ComponentName componentName) { |
| 132 | // Since Bubble ActivityView applies singleTaskDisplay this is |
| 133 | // guaranteed to only be called once per ActivityView. The taskId is |
| 134 | // saved to use for removeTask, preventing appearance in recent tasks. |
| 135 | mTaskId = taskId; |
| 136 | } |
| 137 | |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 138 | /** |
| 139 | * This is only called for tasks on this ActivityView, which is also set to |
| 140 | * single-task mode -- meaning never more than one task on this display. If a task |
| 141 | * is being removed, it's the top Activity finishing and this bubble should |
| 142 | * be removed or collapsed. |
| 143 | */ |
| 144 | @Override |
| 145 | public void onTaskRemovalStarted(int taskId) { |
Mady Mellor | 99a30260 | 2019-06-14 11:39:56 -0700 | [diff] [blame] | 146 | if (mBubble != null) { |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 147 | // Must post because this is called from a binder thread. |
Mady Mellor | 99a30260 | 2019-06-14 11:39:56 -0700 | [diff] [blame] | 148 | post(() -> mBubbleController.removeBubble(mBubble.getKey(), |
Mark Renouf | 08bc42a | 2019-03-07 13:01:59 -0500 | [diff] [blame] | 149 | BubbleController.DISMISS_TASK_FINISHED)); |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | }; |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 153 | |
Mady Mellor | 3d82e68 | 2019-02-05 13:34:48 -0800 | [diff] [blame] | 154 | public BubbleExpandedView(Context context) { |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 155 | this(context, null); |
| 156 | } |
| 157 | |
Mady Mellor | 3d82e68 | 2019-02-05 13:34:48 -0800 | [diff] [blame] | 158 | public BubbleExpandedView(Context context, AttributeSet attrs) { |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 159 | this(context, attrs, 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 | this(context, attrs, defStyleAttr, 0); |
| 164 | } |
| 165 | |
Mady Mellor | 3d82e68 | 2019-02-05 13:34:48 -0800 | [diff] [blame] | 166 | public BubbleExpandedView(Context context, AttributeSet attrs, int defStyleAttr, |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 167 | int defStyleRes) { |
| 168 | super(context, attrs, defStyleAttr, defStyleRes); |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 169 | mPm = context.getPackageManager(); |
Mady Mellor | a96c9ed | 2019-06-07 12:55:26 -0700 | [diff] [blame] | 170 | mDisplaySize = new Point(); |
| 171 | WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); |
| 172 | wm.getDefaultDisplay().getSize(mDisplaySize); |
Mady Mellor | 47b11e3 | 2019-07-11 19:06:21 -0700 | [diff] [blame^] | 173 | Resources res = getResources(); |
| 174 | mMinHeight = res.getDimensionPixelSize(R.dimen.bubble_expanded_default_height); |
| 175 | mPointerMargin = res.getDimensionPixelSize(R.dimen.bubble_pointer_margin); |
| 176 | mExpandedViewTouchSlop = res.getDimensionPixelSize(R.dimen.bubble_expanded_view_slop); |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | @Override |
| 180 | protected void onFinishInflate() { |
| 181 | super.onFinishInflate(); |
| 182 | |
| 183 | Resources res = getResources(); |
| 184 | mPointerView = findViewById(R.id.pointer_view); |
Lyn Han | 02cca81 | 2019-04-02 16:27:32 -0700 | [diff] [blame] | 185 | mPointerWidth = res.getDimensionPixelSize(R.dimen.bubble_pointer_width); |
| 186 | mPointerHeight = res.getDimensionPixelSize(R.dimen.bubble_pointer_height); |
Mady Mellor | dd49705 | 2019-01-30 17:23:48 -0800 | [diff] [blame] | 187 | |
Mady Mellor | dd49705 | 2019-01-30 17:23:48 -0800 | [diff] [blame] | 188 | |
Mark Renouf | 34d04f3 | 2019-05-13 15:53:18 -0400 | [diff] [blame] | 189 | mPointerDrawable = new ShapeDrawable(TriangleShape.create( |
Lyn Han | 5aa27e2 | 2019-05-15 10:55:07 -0700 | [diff] [blame] | 190 | mPointerWidth, mPointerHeight, true /* pointUp */)); |
Mark Renouf | 34d04f3 | 2019-05-13 15:53:18 -0400 | [diff] [blame] | 191 | mPointerView.setBackground(mPointerDrawable); |
Mady Mellor | a96c9ed | 2019-06-07 12:55:26 -0700 | [diff] [blame] | 192 | mPointerView.setVisibility(INVISIBLE); |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 193 | |
Lyn Han | 02cca81 | 2019-04-02 16:27:32 -0700 | [diff] [blame] | 194 | mSettingsIconHeight = getContext().getResources().getDimensionPixelSize( |
Mady Mellor | fe7ec03 | 2019-01-30 17:32:49 -0800 | [diff] [blame] | 195 | R.dimen.bubble_expanded_header_height); |
Lyn Han | 02cca81 | 2019-04-02 16:27:32 -0700 | [diff] [blame] | 196 | mSettingsIcon = findViewById(R.id.settings_button); |
Lyn Han | 02cca81 | 2019-04-02 16:27:32 -0700 | [diff] [blame] | 197 | mSettingsIcon.setOnClickListener(this); |
Lyn Han | 02cca81 | 2019-04-02 16:27:32 -0700 | [diff] [blame] | 198 | |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 199 | mActivityView = new ActivityView(mContext, null /* attrs */, 0 /* defStyle */, |
| 200 | true /* singleTaskInstance */); |
Issei Suzuki | cac2a50 | 2019-04-16 16:52:50 +0200 | [diff] [blame] | 201 | |
| 202 | setContentVisibility(false); |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 203 | addView(mActivityView); |
| 204 | |
Lyn Han | 5aa27e2 | 2019-05-15 10:55:07 -0700 | [diff] [blame] | 205 | // Expanded stack layout, top to bottom: |
| 206 | // Expanded view container |
| 207 | // ==> bubble row |
| 208 | // ==> expanded view |
| 209 | // ==> activity view |
| 210 | // ==> manage button |
| 211 | bringChildToFront(mActivityView); |
| 212 | bringChildToFront(mSettingsIcon); |
Mady Mellor | 52b1ac6 | 2019-04-10 16:59:03 -0700 | [diff] [blame] | 213 | |
Mark Renouf | 34d04f3 | 2019-05-13 15:53:18 -0400 | [diff] [blame] | 214 | applyThemeAttrs(); |
| 215 | |
Mady Mellor | 5d8f140 | 2019-02-21 18:23:52 -0800 | [diff] [blame] | 216 | setOnApplyWindowInsetsListener((View view, WindowInsets insets) -> { |
| 217 | // Keep track of IME displaying because we should not make any adjustments that might |
| 218 | // 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] | 219 | final int keyboardHeight = insets.getSystemWindowInsetBottom() |
| 220 | - insets.getStableInsetBottom(); |
Mady Mellor | 5d8f140 | 2019-02-21 18:23:52 -0800 | [diff] [blame] | 221 | mKeyboardVisible = keyboardHeight != 0; |
| 222 | if (!mKeyboardVisible && mNeedsNewHeight) { |
| 223 | updateHeight(); |
| 224 | } |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 225 | return view.onApplyWindowInsets(insets); |
| 226 | }); |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 227 | } |
| 228 | |
Mark Renouf | 34d04f3 | 2019-05-13 15:53:18 -0400 | [diff] [blame] | 229 | void applyThemeAttrs() { |
| 230 | TypedArray ta = getContext().obtainStyledAttributes(R.styleable.BubbleExpandedView); |
| 231 | int bgColor = ta.getColor( |
| 232 | R.styleable.BubbleExpandedView_android_colorBackgroundFloating, Color.WHITE); |
| 233 | float cornerRadius = ta.getDimension( |
| 234 | R.styleable.BubbleExpandedView_android_dialogCornerRadius, 0); |
| 235 | ta.recycle(); |
| 236 | |
| 237 | // Update triangle color. |
| 238 | mPointerDrawable.setTint(bgColor); |
| 239 | |
| 240 | // Update ActivityView cornerRadius |
| 241 | if (ScreenDecorationsUtils.supportsRoundedCornersOnWindows(mContext.getResources())) { |
| 242 | mActivityView.setCornerRadius(cornerRadius); |
| 243 | } |
| 244 | } |
| 245 | |
Mady Mellor | 5d8f140 | 2019-02-21 18:23:52 -0800 | [diff] [blame] | 246 | @Override |
| 247 | protected void onDetachedFromWindow() { |
| 248 | super.onDetachedFromWindow(); |
| 249 | mKeyboardVisible = false; |
| 250 | mNeedsNewHeight = false; |
| 251 | if (mActivityView != null) { |
| 252 | mActivityView.setForwardedInsets(Insets.of(0, 0, 0, 0)); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | /** |
Issei Suzuki | cac2a50 | 2019-04-16 16:52:50 +0200 | [diff] [blame] | 257 | * Set visibility of contents in the expanded state. |
| 258 | * |
| 259 | * @param visibility {@code true} if the contents should be visible on the screen. |
| 260 | * |
| 261 | * Note that this contents visibility doesn't affect visibility at {@link android.view.View}, |
| 262 | * and setting {@code false} actually means rendering the contents in transparent. |
| 263 | */ |
| 264 | void setContentVisibility(boolean visibility) { |
| 265 | final float alpha = visibility ? 1f : 0f; |
| 266 | mPointerView.setAlpha(alpha); |
| 267 | if (mActivityView != null) { |
| 268 | mActivityView.setAlpha(alpha); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | /** |
Mady Mellor | 5d8f140 | 2019-02-21 18:23:52 -0800 | [diff] [blame] | 273 | * Called by {@link BubbleStackView} when the insets for the expanded state should be updated. |
| 274 | * This should be done post-move and post-animation. |
| 275 | */ |
| 276 | void updateInsets(WindowInsets insets) { |
| 277 | if (usingActivityView()) { |
Mady Mellor | 5d8f140 | 2019-02-21 18:23:52 -0800 | [diff] [blame] | 278 | int[] windowLocation = mActivityView.getLocationOnScreen(); |
| 279 | final int windowBottom = windowLocation[1] + mActivityView.getHeight(); |
| 280 | final int keyboardHeight = insets.getSystemWindowInsetBottom() |
| 281 | - insets.getStableInsetBottom(); |
| 282 | final int insetsBottom = Math.max(0, |
Mady Mellor | a96c9ed | 2019-06-07 12:55:26 -0700 | [diff] [blame] | 283 | windowBottom + keyboardHeight - mDisplaySize.y); |
Mady Mellor | 5d8f140 | 2019-02-21 18:23:52 -0800 | [diff] [blame] | 284 | mActivityView.setForwardedInsets(Insets.of(0, 0, 0, insetsBottom)); |
| 285 | } |
| 286 | } |
| 287 | |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 288 | /** |
Mady Mellor | 99a30260 | 2019-06-14 11:39:56 -0700 | [diff] [blame] | 289 | * Sets the bubble used to populate this view. |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 290 | */ |
Mady Mellor | 99a30260 | 2019-06-14 11:39:56 -0700 | [diff] [blame] | 291 | public void setBubble(Bubble bubble, BubbleStackView stackView, String appName) { |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 292 | mStackView = stackView; |
Mady Mellor | 99a30260 | 2019-06-14 11:39:56 -0700 | [diff] [blame] | 293 | mBubble = bubble; |
Lyn Han | 6c40fe7 | 2019-05-08 14:06:33 -0700 | [diff] [blame] | 294 | mAppName = appName; |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 295 | |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 296 | try { |
Issei Suzuki | a91f396 | 2019-06-07 11:48:23 +0200 | [diff] [blame] | 297 | ApplicationInfo info = mPm.getApplicationInfo( |
Mady Mellor | 99a30260 | 2019-06-14 11:39:56 -0700 | [diff] [blame] | 298 | bubble.getPackageName(), |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 299 | PackageManager.MATCH_UNINSTALLED_PACKAGES |
| 300 | | PackageManager.MATCH_DISABLED_COMPONENTS |
| 301 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE |
| 302 | | PackageManager.MATCH_DIRECT_BOOT_AWARE); |
Issei Suzuki | a91f396 | 2019-06-07 11:48:23 +0200 | [diff] [blame] | 303 | mAppIcon = mPm.getApplicationIcon(info); |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 304 | } catch (PackageManager.NameNotFoundException e) { |
Lyn Han | 6c40fe7 | 2019-05-08 14:06:33 -0700 | [diff] [blame] | 305 | // Do nothing. |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 306 | } |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 307 | if (mAppIcon == null) { |
| 308 | mAppIcon = mPm.getDefaultActivityIcon(); |
| 309 | } |
Mark Renouf | 34d04f3 | 2019-05-13 15:53:18 -0400 | [diff] [blame] | 310 | applyThemeAttrs(); |
Lyn Han | 6914912 | 2019-04-30 12:03:12 -0700 | [diff] [blame] | 311 | showSettingsIcon(); |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 312 | updateExpandedView(); |
Mady Mellor | 6d00203 | 2019-02-13 13:45:17 -0800 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | /** |
| 316 | * Lets activity view know it should be shown / populated. |
| 317 | */ |
Mady Mellor | 5029fa6 | 2019-03-05 12:16:21 -0800 | [diff] [blame] | 318 | public void populateExpandedView() { |
| 319 | if (usingActivityView()) { |
| 320 | mActivityView.setCallback(mStateCallback); |
| 321 | } else { |
Issei Suzuki | a91f396 | 2019-06-07 11:48:23 +0200 | [diff] [blame] | 322 | Log.e(TAG, "Cannot populate expanded view."); |
Mady Mellor | 5029fa6 | 2019-03-05 12:16:21 -0800 | [diff] [blame] | 323 | } |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 324 | } |
| 325 | |
Mady Mellor | fe7ec03 | 2019-01-30 17:32:49 -0800 | [diff] [blame] | 326 | /** |
Mady Mellor | 99a30260 | 2019-06-14 11:39:56 -0700 | [diff] [blame] | 327 | * Updates the bubble backing this view. This will not re-populate ActivityView, it will |
Lyn Han | 02cca81 | 2019-04-02 16:27:32 -0700 | [diff] [blame] | 328 | * 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] | 329 | */ |
Mady Mellor | 99a30260 | 2019-06-14 11:39:56 -0700 | [diff] [blame] | 330 | public void update(Bubble bubble) { |
| 331 | if (bubble.getKey().equals(mBubble.getKey())) { |
| 332 | mBubble = bubble; |
Lyn Han | 02cca81 | 2019-04-02 16:27:32 -0700 | [diff] [blame] | 333 | updateSettingsContentDescription(); |
Mady Mellor | fe7ec03 | 2019-01-30 17:32:49 -0800 | [diff] [blame] | 334 | updateHeight(); |
| 335 | } else { |
Mady Mellor | 99a30260 | 2019-06-14 11:39:56 -0700 | [diff] [blame] | 336 | Log.w(TAG, "Trying to update entry with different key, new bubble: " |
| 337 | + bubble.getKey() + " old bubble: " + bubble.getKey()); |
Mady Mellor | fe7ec03 | 2019-01-30 17:32:49 -0800 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 341 | private void updateExpandedView() { |
Mady Mellor | 99a30260 | 2019-06-14 11:39:56 -0700 | [diff] [blame] | 342 | mBubbleIntent = mBubble.getBubbleIntent(mContext); |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 343 | if (mBubbleIntent != null) { |
Issei Suzuki | cac2a50 | 2019-04-16 16:52:50 +0200 | [diff] [blame] | 344 | setContentVisibility(false); |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 345 | mActivityView.setVisibility(VISIBLE); |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 346 | } |
| 347 | updateView(); |
| 348 | } |
| 349 | |
Mark Renouf | 041d726 | 2019-02-06 12:09:41 -0500 | [diff] [blame] | 350 | boolean performBackPressIfNeeded() { |
Mady Mellor | 323fb0b | 2019-03-25 12:15:22 -0700 | [diff] [blame] | 351 | if (!usingActivityView()) { |
Mark Renouf | 041d726 | 2019-02-06 12:09:41 -0500 | [diff] [blame] | 352 | return false; |
| 353 | } |
| 354 | mActivityView.performBackPress(); |
| 355 | return true; |
| 356 | } |
| 357 | |
Mady Mellor | fe7ec03 | 2019-01-30 17:32:49 -0800 | [diff] [blame] | 358 | void updateHeight() { |
| 359 | if (usingActivityView()) { |
Mady Mellor | 99a30260 | 2019-06-14 11:39:56 -0700 | [diff] [blame] | 360 | int max = getMaxExpandedHeight() - mSettingsIconHeight - mPointerHeight |
| 361 | - mPointerMargin; |
| 362 | float desiredHeight = Math.max(mBubble.getDesiredHeight(mContext), mMinHeight); |
| 363 | float height = Math.min(desiredHeight, max); |
Mady Mellor | fe7ec03 | 2019-01-30 17:32:49 -0800 | [diff] [blame] | 364 | height = Math.max(height, mMinHeight); |
| 365 | LayoutParams lp = (LayoutParams) mActivityView.getLayoutParams(); |
Mady Mellor | 5d8f140 | 2019-02-21 18:23:52 -0800 | [diff] [blame] | 366 | mNeedsNewHeight = lp.height != height; |
| 367 | if (!mKeyboardVisible) { |
| 368 | // If the keyboard is visible... don't adjust the height because that will cause |
| 369 | // a configuration change and the keyboard will be lost. |
Mady Mellor | 7af771a | 2019-03-07 15:04:54 -0800 | [diff] [blame] | 370 | lp.height = (int) height; |
Mady Mellor | 5d8f140 | 2019-02-21 18:23:52 -0800 | [diff] [blame] | 371 | mActivityView.setLayoutParams(lp); |
| 372 | mNeedsNewHeight = false; |
| 373 | } |
Mady Mellor | fe7ec03 | 2019-01-30 17:32:49 -0800 | [diff] [blame] | 374 | } |
| 375 | } |
| 376 | |
Mady Mellor | a96c9ed | 2019-06-07 12:55:26 -0700 | [diff] [blame] | 377 | private int getMaxExpandedHeight() { |
| 378 | int[] windowLocation = mActivityView.getLocationOnScreen(); |
| 379 | return mDisplaySize.y - windowLocation[1] - mSettingsIconHeight; |
| 380 | } |
| 381 | |
Mady Mellor | 47b11e3 | 2019-07-11 19:06:21 -0700 | [diff] [blame^] | 382 | /** |
| 383 | * Whether the provided x, y values (in raw coordinates) are in a touchable area of the |
| 384 | * expanded view. |
| 385 | * |
| 386 | * The touchable areas are the ActivityView (plus some slop around it) and the manage button. |
| 387 | */ |
| 388 | boolean intersectingTouchableContent(int rawX, int rawY) { |
| 389 | mTempRect.setEmpty(); |
| 390 | if (mActivityView != null) { |
| 391 | mTempLoc = mActivityView.getLocationOnScreen(); |
| 392 | mTempRect.set(mTempLoc[0] - mExpandedViewTouchSlop, |
| 393 | mTempLoc[1] - mExpandedViewTouchSlop, |
| 394 | mTempLoc[0] + mActivityView.getWidth() + mExpandedViewTouchSlop, |
| 395 | mTempLoc[1] + mActivityView.getHeight() + mExpandedViewTouchSlop); |
| 396 | } |
| 397 | if (mTempRect.contains(rawX, rawY)) { |
| 398 | return true; |
| 399 | } |
| 400 | mTempLoc = mSettingsIcon.getLocationOnScreen(); |
| 401 | mTempRect.set(mTempLoc[0], |
| 402 | mTempLoc[1], |
| 403 | mTempLoc[0] + mSettingsIcon.getWidth(), |
| 404 | mTempLoc[1] + mSettingsIcon.getHeight()); |
| 405 | if (mTempRect.contains(rawX, rawY)) { |
| 406 | return true; |
| 407 | } |
| 408 | return false; |
| 409 | } |
| 410 | |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 411 | @Override |
| 412 | public void onClick(View view) { |
Mady Mellor | 99a30260 | 2019-06-14 11:39:56 -0700 | [diff] [blame] | 413 | if (mBubble == null) { |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 414 | return; |
| 415 | } |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 416 | int id = view.getId(); |
Lyn Han | c26ff12 | 2019-03-29 16:46:07 -0700 | [diff] [blame] | 417 | if (id == R.id.settings_button) { |
Mady Mellor | 99a30260 | 2019-06-14 11:39:56 -0700 | [diff] [blame] | 418 | Intent intent = mBubble.getSettingsIntent(); |
Steven Wu | b00225b | 2019-02-08 14:27:42 -0500 | [diff] [blame] | 419 | mStackView.collapseStack(() -> { |
Mady Mellor | 99a30260 | 2019-06-14 11:39:56 -0700 | [diff] [blame] | 420 | mContext.startActivityAsUser(intent, mBubble.getEntry().notification.getUser()); |
| 421 | logBubbleClickEvent(mBubble, |
Steven Wu | b00225b | 2019-02-08 14:27:42 -0500 | [diff] [blame] | 422 | StatsLog.BUBBLE_UICHANGED__ACTION__HEADER_GO_TO_SETTINGS); |
| 423 | }); |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 424 | } |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 425 | } |
| 426 | |
Lyn Han | 02cca81 | 2019-04-02 16:27:32 -0700 | [diff] [blame] | 427 | private void updateSettingsContentDescription() { |
| 428 | mSettingsIcon.setContentDescription(getResources().getString( |
| 429 | R.string.bubbles_settings_button_description, mAppName)); |
| 430 | } |
| 431 | |
Lyn Han | c26ff12 | 2019-03-29 16:46:07 -0700 | [diff] [blame] | 432 | void showSettingsIcon() { |
Lyn Han | 02cca81 | 2019-04-02 16:27:32 -0700 | [diff] [blame] | 433 | updateSettingsContentDescription(); |
Lyn Han | c26ff12 | 2019-03-29 16:46:07 -0700 | [diff] [blame] | 434 | mSettingsIcon.setVisibility(VISIBLE); |
| 435 | } |
| 436 | |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 437 | /** |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 438 | * Update appearance of the expanded view being displayed. |
| 439 | */ |
| 440 | public void updateView() { |
| 441 | if (usingActivityView() |
| 442 | && mActivityView.getVisibility() == VISIBLE |
| 443 | && mActivityView.isAttachedToWindow()) { |
| 444 | mActivityView.onLocationChanged(); |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 445 | } |
Mady Mellor | fe7ec03 | 2019-01-30 17:32:49 -0800 | [diff] [blame] | 446 | updateHeight(); |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | /** |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 450 | * Set the x position that the tip of the triangle should point to. |
| 451 | */ |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 452 | public void setPointerPosition(float x) { |
Lyn Han | 9a2f5cf | 2019-05-23 11:01:41 -0700 | [diff] [blame] | 453 | float halfPointerWidth = mPointerWidth / 2f; |
| 454 | float pointerLeft = x - halfPointerWidth; |
| 455 | mPointerView.setTranslationX(pointerLeft); |
Lyn Han | f74ba67 | 2019-05-20 16:08:48 -0700 | [diff] [blame] | 456 | mPointerView.setVisibility(VISIBLE); |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | /** |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 460 | * 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] | 461 | */ |
Mady Mellor | 94d94a7 | 2019-03-05 18:16:59 -0800 | [diff] [blame] | 462 | public void cleanUpExpandedState() { |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 463 | if (mActivityView == null) { |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 464 | return; |
| 465 | } |
Issei Suzuki | 734bc94 | 2019-06-05 13:59:52 +0200 | [diff] [blame] | 466 | switch (mActivityViewStatus) { |
| 467 | case INITIALIZED: |
| 468 | case ACTIVITY_STARTED: |
| 469 | mActivityView.release(); |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 470 | } |
Mark Renouf | 5c732b3 | 2019-06-12 15:14:54 -0400 | [diff] [blame] | 471 | if (mTaskId != -1) { |
| 472 | try { |
| 473 | ActivityTaskManager.getService().removeTask(mTaskId); |
| 474 | } catch (RemoteException e) { |
| 475 | Log.w(TAG, "Failed to remove taskId " + mTaskId); |
| 476 | } |
| 477 | mTaskId = -1; |
| 478 | } |
Mark Renouf | 28c250d | 2019-02-25 16:47:34 -0500 | [diff] [blame] | 479 | removeView(mActivityView); |
Mark Renouf | 5c732b3 | 2019-06-12 15:14:54 -0400 | [diff] [blame] | 480 | |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 481 | mActivityView = null; |
Issei Suzuki | 734bc94 | 2019-06-05 13:59:52 +0200 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | /** |
| 485 | * Called when the last task is removed from a {@link android.hardware.display.VirtualDisplay} |
| 486 | * which {@link ActivityView} uses. |
| 487 | */ |
| 488 | void notifyDisplayEmpty() { |
| 489 | if (mActivityViewStatus == ActivityViewStatus.ACTIVITY_STARTED) { |
| 490 | mActivityViewStatus = ActivityViewStatus.INITIALIZED; |
| 491 | } |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 492 | } |
| 493 | |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 494 | private boolean usingActivityView() { |
Mady Mellor | 323fb0b | 2019-03-25 12:15:22 -0700 | [diff] [blame] | 495 | return mBubbleIntent != null && mActivityView != null; |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 496 | } |
| 497 | |
Mady Mellor | 390bff4 | 2019-04-05 15:09:01 -0700 | [diff] [blame] | 498 | /** |
| 499 | * @return the display id of the virtual display. |
| 500 | */ |
| 501 | public int getVirtualDisplayId() { |
| 502 | if (usingActivityView()) { |
| 503 | return mActivityView.getVirtualDisplayId(); |
| 504 | } |
| 505 | return INVALID_DISPLAY; |
| 506 | } |
| 507 | |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 508 | /** |
Steven Wu | b00225b | 2019-02-08 14:27:42 -0500 | [diff] [blame] | 509 | * Logs bubble UI click event. |
| 510 | * |
Mady Mellor | 99a30260 | 2019-06-14 11:39:56 -0700 | [diff] [blame] | 511 | * @param bubble the bubble notification entry that user is interacting with. |
Steven Wu | b00225b | 2019-02-08 14:27:42 -0500 | [diff] [blame] | 512 | * @param action the user interaction enum. |
| 513 | */ |
Mady Mellor | 99a30260 | 2019-06-14 11:39:56 -0700 | [diff] [blame] | 514 | private void logBubbleClickEvent(Bubble bubble, int action) { |
| 515 | StatusBarNotification notification = bubble.getEntry().notification; |
Steven Wu | b00225b | 2019-02-08 14:27:42 -0500 | [diff] [blame] | 516 | StatsLog.write(StatsLog.BUBBLE_UI_CHANGED, |
| 517 | notification.getPackageName(), |
| 518 | notification.getNotification().getChannelId(), |
| 519 | notification.getId(), |
| 520 | mStackView.getBubbleIndex(mStackView.getExpandedBubble()), |
| 521 | mStackView.getBubbleCount(), |
| 522 | action, |
| 523 | mStackView.getNormalizedXPosition(), |
Steven Wu | 45e38ae | 2019-03-25 16:16:59 -0400 | [diff] [blame] | 524 | mStackView.getNormalizedYPosition(), |
Mady Mellor | 99a30260 | 2019-06-14 11:39:56 -0700 | [diff] [blame] | 525 | bubble.showInShadeWhenBubble(), |
| 526 | bubble.isOngoing(), |
Mady Mellor | 6cec350 | 2019-06-17 19:20:49 -0700 | [diff] [blame] | 527 | false /* isAppForeground (unused) */); |
Steven Wu | b00225b | 2019-02-08 14:27:42 -0500 | [diff] [blame] | 528 | } |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 529 | } |