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