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 | |
Yuncheol Heo | 5d62e75 | 2020-04-08 14:54:24 -0700 | [diff] [blame] | 19 | import static android.app.WindowConfiguration.WINDOWING_MODE_MULTI_WINDOW; |
Mady Mellor | c41ed32 | 2019-09-25 11:19:26 -0700 | [diff] [blame] | 20 | import static android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK; |
| 21 | import static android.content.Intent.FLAG_ACTIVITY_NEW_DOCUMENT; |
Yunfan Chen | b83940c | 2020-04-06 16:43:09 +0900 | [diff] [blame] | 22 | import static android.graphics.PixelFormat.TRANSPARENT; |
Mady Mellor | 390bff4 | 2019-04-05 15:09:01 -0700 | [diff] [blame] | 23 | import static android.view.Display.INVALID_DISPLAY; |
Yunfan Chen | b83940c | 2020-04-06 16:43:09 +0900 | [diff] [blame] | 24 | import static android.view.InsetsState.ITYPE_IME; |
Joshua Tsuji | 06785ab | 2020-06-08 11:18:40 -0400 | [diff] [blame] | 25 | import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT; |
Jorim Jaggi | 924ef75 | 2020-01-29 17:26:55 +0100 | [diff] [blame] | 26 | import static android.view.ViewRootImpl.NEW_INSETS_MODE_FULL; |
| 27 | import static android.view.ViewRootImpl.sNewInsetsMode; |
Yunfan Chen | b83940c | 2020-04-06 16:43:09 +0900 | [diff] [blame] | 28 | import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS; |
| 29 | import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; |
| 30 | import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE; |
| 31 | import static android.view.WindowManager.LayoutParams.TYPE_NAVIGATION_BAR_PANEL; |
Beverly | a53fb0d | 2020-01-29 15:26:13 -0500 | [diff] [blame] | 32 | |
Mark Renouf | db861c9 | 2019-07-26 13:58:17 -0400 | [diff] [blame] | 33 | import static com.android.systemui.bubbles.BubbleDebugConfig.DEBUG_BUBBLE_EXPANDED_VIEW; |
Issei Suzuki | a8d0731 | 2019-06-07 12:56:19 +0200 | [diff] [blame] | 34 | import static com.android.systemui.bubbles.BubbleDebugConfig.TAG_BUBBLES; |
| 35 | import static com.android.systemui.bubbles.BubbleDebugConfig.TAG_WITH_CLASS_NAME; |
Mady Mellor | ca0c24c | 2019-05-16 16:14:32 -0700 | [diff] [blame] | 36 | |
Joshua Tsuji | 847b1d42 | 2020-06-01 13:42:19 -0400 | [diff] [blame] | 37 | import android.annotation.SuppressLint; |
Tony Huang | 8672410 | 2020-06-11 17:51:55 +0800 | [diff] [blame] | 38 | import android.app.ActivityManager; |
Mady Mellor | 60101c9 | 2019-04-11 19:04:00 -0700 | [diff] [blame] | 39 | import android.app.ActivityOptions; |
Mark Renouf | 5c732b3 | 2019-06-12 15:14:54 -0400 | [diff] [blame] | 40 | import android.app.ActivityTaskManager; |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 41 | import android.app.ActivityView; |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 42 | import android.app.PendingIntent; |
Mark Renouf | 5c732b3 | 2019-06-12 15:14:54 -0400 | [diff] [blame] | 43 | import android.content.ComponentName; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 44 | import android.content.Context; |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 45 | import android.content.Intent; |
Lyn Han | 26bb198 | 2020-06-08 15:38:01 -0700 | [diff] [blame] | 46 | import android.content.res.Configuration; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 47 | import android.content.res.Resources; |
Mady Mellor | dd49705 | 2019-01-30 17:23:48 -0800 | [diff] [blame] | 48 | import android.content.res.TypedArray; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 49 | import android.graphics.Color; |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 50 | import android.graphics.Insets; |
Joshua Tsuji | 06785ab | 2020-06-08 11:18:40 -0400 | [diff] [blame] | 51 | import android.graphics.Outline; |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 52 | import android.graphics.Point; |
Mady Mellor | 47b11e3 | 2019-07-11 19:06:21 -0700 | [diff] [blame] | 53 | import android.graphics.Rect; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 54 | import android.graphics.drawable.ShapeDrawable; |
Yunfan Chen | b83940c | 2020-04-06 16:43:09 +0900 | [diff] [blame] | 55 | import android.hardware.display.VirtualDisplay; |
| 56 | import android.os.Binder; |
Mark Renouf | 5c732b3 | 2019-06-12 15:14:54 -0400 | [diff] [blame] | 57 | import android.os.RemoteException; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 58 | import android.util.AttributeSet; |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 59 | import android.util.Log; |
Yunfan Chen | b83940c | 2020-04-06 16:43:09 +0900 | [diff] [blame] | 60 | import android.view.Gravity; |
Joshua Tsuji | 06785ab | 2020-06-08 11:18:40 -0400 | [diff] [blame] | 61 | import android.view.SurfaceControl; |
| 62 | import android.view.SurfaceView; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 63 | import android.view.View; |
Joshua Tsuji | 06785ab | 2020-06-08 11:18:40 -0400 | [diff] [blame] | 64 | import android.view.ViewGroup; |
| 65 | import android.view.ViewOutlineProvider; |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 66 | import android.view.WindowInsets; |
Mady Mellor | a96c9ed | 2019-06-07 12:55:26 -0700 | [diff] [blame] | 67 | import android.view.WindowManager; |
Lyn Han | 670fa80 | 2020-05-05 12:56:33 -0700 | [diff] [blame] | 68 | import android.view.accessibility.AccessibilityNodeInfo; |
Joshua Tsuji | 06785ab | 2020-06-08 11:18:40 -0400 | [diff] [blame] | 69 | import android.widget.FrameLayout; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 70 | import android.widget.LinearLayout; |
| 71 | |
Joshua Tsuji | 06785ab | 2020-06-08 11:18:40 -0400 | [diff] [blame] | 72 | import androidx.annotation.Nullable; |
| 73 | |
Mark Renouf | 34d04f3 | 2019-05-13 15:53:18 -0400 | [diff] [blame] | 74 | import com.android.internal.policy.ScreenDecorationsUtils; |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 75 | import com.android.systemui.Dependency; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 76 | import com.android.systemui.R; |
| 77 | import com.android.systemui.recents.TriangleShape; |
Lyn Han | 754e77b | 2019-04-30 14:34:49 -0700 | [diff] [blame] | 78 | import com.android.systemui.statusbar.AlphaOptimizedButton; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 79 | |
| 80 | /** |
Lyn Han | 02cca81 | 2019-04-02 16:27:32 -0700 | [diff] [blame] | 81 | * 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] | 82 | */ |
Joshua Tsuji | 6855cab | 2020-04-16 01:05:39 -0400 | [diff] [blame] | 83 | public class BubbleExpandedView extends LinearLayout { |
Issei Suzuki | a8d0731 | 2019-06-07 12:56:19 +0200 | [diff] [blame] | 84 | private static final String TAG = TAG_WITH_CLASS_NAME ? "BubbleExpandedView" : TAG_BUBBLES; |
Yunfan Chen | b83940c | 2020-04-06 16:43:09 +0900 | [diff] [blame] | 85 | private static final String WINDOW_TITLE = "ImeInsetsWindowWithoutContent"; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 86 | |
Issei Suzuki | 734bc94 | 2019-06-05 13:59:52 +0200 | [diff] [blame] | 87 | private enum ActivityViewStatus { |
| 88 | // ActivityView is being initialized, cannot start an activity yet. |
| 89 | INITIALIZING, |
| 90 | // ActivityView is initialized, and ready to start an activity. |
| 91 | INITIALIZED, |
| 92 | // Activity runs in the ActivityView. |
| 93 | ACTIVITY_STARTED, |
| 94 | // ActivityView is released, so activity launching will no longer be permitted. |
| 95 | RELEASED, |
| 96 | } |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 97 | |
| 98 | // The triangle pointing to the expanded view |
| 99 | private View mPointerView; |
Mady Mellor | 44ee2fe | 2019-01-30 17:51:16 -0800 | [diff] [blame] | 100 | private int mPointerMargin; |
Joshua Tsuji | 06785ab | 2020-06-08 11:18:40 -0400 | [diff] [blame] | 101 | @Nullable private int[] mExpandedViewContainerLocation; |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 102 | |
Lyn Han | 754e77b | 2019-04-30 14:34:49 -0700 | [diff] [blame] | 103 | private AlphaOptimizedButton mSettingsIcon; |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 104 | |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 105 | // Views for expanded state |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 106 | private ActivityView mActivityView; |
| 107 | |
Issei Suzuki | 734bc94 | 2019-06-05 13:59:52 +0200 | [diff] [blame] | 108 | private ActivityViewStatus mActivityViewStatus = ActivityViewStatus.INITIALIZING; |
Mark Renouf | 5c732b3 | 2019-06-12 15:14:54 -0400 | [diff] [blame] | 109 | private int mTaskId = -1; |
| 110 | |
Lyn Han | b58c756 | 2020-01-07 14:29:20 -0800 | [diff] [blame] | 111 | private PendingIntent mPendingIntent; |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 112 | |
Mady Mellor | 5d8f140 | 2019-02-21 18:23:52 -0800 | [diff] [blame] | 113 | private boolean mKeyboardVisible; |
| 114 | private boolean mNeedsNewHeight; |
| 115 | |
Mady Mellor | a96c9ed | 2019-06-07 12:55:26 -0700 | [diff] [blame] | 116 | private Point mDisplaySize; |
Mady Mellor | fe7ec03 | 2019-01-30 17:32:49 -0800 | [diff] [blame] | 117 | private int mMinHeight; |
Lyn Han | cd4f87e | 2020-02-19 20:33:45 -0800 | [diff] [blame] | 118 | private int mOverflowHeight; |
Lyn Han | 02cca81 | 2019-04-02 16:27:32 -0700 | [diff] [blame] | 119 | private int mSettingsIconHeight; |
Lyn Han | 02cca81 | 2019-04-02 16:27:32 -0700 | [diff] [blame] | 120 | private int mPointerWidth; |
| 121 | private int mPointerHeight; |
Mark Renouf | 34d04f3 | 2019-05-13 15:53:18 -0400 | [diff] [blame] | 122 | private ShapeDrawable mPointerDrawable; |
Lyn Han | 3989260 | 2020-06-18 21:15:28 -0700 | [diff] [blame] | 123 | private int mExpandedViewPadding; |
| 124 | |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 125 | |
Lyn Han | b58c756 | 2020-01-07 14:29:20 -0800 | [diff] [blame] | 126 | @Nullable private Bubble mBubble; |
| 127 | |
| 128 | private boolean mIsOverflow; |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 129 | |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 130 | private BubbleController mBubbleController = Dependency.get(BubbleController.class); |
Mady Mellor | 9be3bed | 2019-08-21 17:26:26 -0700 | [diff] [blame] | 131 | private WindowManager mWindowManager; |
Tony Huang | 8672410 | 2020-06-11 17:51:55 +0800 | [diff] [blame] | 132 | private ActivityManager mActivityManager; |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 133 | |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 134 | private BubbleStackView mStackView; |
Yunfan Chen | b83940c | 2020-04-06 16:43:09 +0900 | [diff] [blame] | 135 | private View mVirtualImeView; |
| 136 | private WindowManager mVirtualDisplayWindowManager; |
| 137 | private boolean mImeShowing = false; |
Joshua Tsuji | 06785ab | 2020-06-08 11:18:40 -0400 | [diff] [blame] | 138 | private float mCornerRadius = 0f; |
| 139 | |
| 140 | /** |
| 141 | * Container for the ActivityView that has a solid, round-rect background that shows if the |
| 142 | * ActivityView hasn't loaded. |
| 143 | */ |
| 144 | private FrameLayout mActivityViewContainer = new FrameLayout(getContext()); |
| 145 | |
| 146 | /** The SurfaceView that the ActivityView draws to. */ |
| 147 | @Nullable private SurfaceView mActivitySurface; |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 148 | |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 149 | private ActivityView.StateCallback mStateCallback = new ActivityView.StateCallback() { |
| 150 | @Override |
| 151 | public void onActivityViewReady(ActivityView view) { |
Mark Renouf | db861c9 | 2019-07-26 13:58:17 -0400 | [diff] [blame] | 152 | if (DEBUG_BUBBLE_EXPANDED_VIEW) { |
| 153 | Log.d(TAG, "onActivityViewReady: mActivityViewStatus=" + mActivityViewStatus |
| 154 | + " bubble=" + getBubbleKey()); |
| 155 | } |
Issei Suzuki | 734bc94 | 2019-06-05 13:59:52 +0200 | [diff] [blame] | 156 | switch (mActivityViewStatus) { |
| 157 | case INITIALIZING: |
| 158 | case INITIALIZED: |
| 159 | // Custom options so there is no activity transition animation |
| 160 | ActivityOptions options = ActivityOptions.makeCustomAnimation(getContext(), |
| 161 | 0 /* enterResId */, 0 /* exitResId */); |
Winson Chung | 0620052 | 2020-03-31 22:47:02 -0700 | [diff] [blame] | 162 | options.setTaskAlwaysOnTop(true); |
Yuncheol Heo | 5d62e75 | 2020-04-08 14:54:24 -0700 | [diff] [blame] | 163 | options.setLaunchWindowingMode(WINDOWING_MODE_MULTI_WINDOW); |
Issei Suzuki | 734bc94 | 2019-06-05 13:59:52 +0200 | [diff] [blame] | 164 | // Post to keep the lifecycle normal |
Mark Renouf | db861c9 | 2019-07-26 13:58:17 -0400 | [diff] [blame] | 165 | post(() -> { |
| 166 | if (DEBUG_BUBBLE_EXPANDED_VIEW) { |
| 167 | Log.d(TAG, "onActivityViewReady: calling startActivity, " |
| 168 | + "bubble=" + getBubbleKey()); |
| 169 | } |
Mady Mellor | c1a75a9 | 2020-06-11 22:24:24 -0700 | [diff] [blame] | 170 | if (mActivityView == null) { |
| 171 | mBubbleController.removeBubble(getBubbleKey(), |
| 172 | BubbleController.DISMISS_INVALID_INTENT); |
| 173 | return; |
| 174 | } |
Mady Mellor | 8454ddf | 2019-08-15 11:16:23 -0700 | [diff] [blame] | 175 | try { |
Lyn Han | efbaa97 | 2020-06-23 14:59:39 -0700 | [diff] [blame] | 176 | if (!mIsOverflow && mBubble.hasMetadataShortcutId() |
| 177 | && mBubble.getShortcutInfo() != null) { |
Mady Mellor | 80d5ed1 | 2020-04-10 21:17:30 -0700 | [diff] [blame] | 178 | options.setApplyActivityFlagsForBubbles(true); |
Mady Mellor | b547e5e | 2019-12-02 10:15:56 -0800 | [diff] [blame] | 179 | mActivityView.startShortcutActivity(mBubble.getShortcutInfo(), |
| 180 | options, null /* sourceBounds */); |
| 181 | } else { |
| 182 | Intent fillInIntent = new Intent(); |
| 183 | // Apply flags to make behaviour match documentLaunchMode=always. |
| 184 | fillInIntent.addFlags(FLAG_ACTIVITY_NEW_DOCUMENT); |
| 185 | fillInIntent.addFlags(FLAG_ACTIVITY_MULTIPLE_TASK); |
Lyn Han | b58c756 | 2020-01-07 14:29:20 -0800 | [diff] [blame] | 186 | mActivityView.startActivity(mPendingIntent, fillInIntent, options); |
Mady Mellor | b547e5e | 2019-12-02 10:15:56 -0800 | [diff] [blame] | 187 | } |
Mady Mellor | 8454ddf | 2019-08-15 11:16:23 -0700 | [diff] [blame] | 188 | } catch (RuntimeException e) { |
| 189 | // If there's a runtime exception here then there's something |
| 190 | // wrong with the intent, we can't really recover / try to populate |
| 191 | // the bubble again so we'll just remove it. |
| 192 | Log.w(TAG, "Exception while displaying bubble: " + getBubbleKey() |
| 193 | + ", " + e.getMessage() + "; removing bubble"); |
Pinyao Ting | 3c93061 | 2020-05-19 00:26:03 +0000 | [diff] [blame] | 194 | mBubbleController.removeBubble(getBubbleKey(), |
Mady Mellor | 8454ddf | 2019-08-15 11:16:23 -0700 | [diff] [blame] | 195 | BubbleController.DISMISS_INVALID_INTENT); |
| 196 | } |
Mark Renouf | db861c9 | 2019-07-26 13:58:17 -0400 | [diff] [blame] | 197 | }); |
Issei Suzuki | 734bc94 | 2019-06-05 13:59:52 +0200 | [diff] [blame] | 198 | mActivityViewStatus = ActivityViewStatus.ACTIVITY_STARTED; |
Tony Huang | 8672410 | 2020-06-11 17:51:55 +0800 | [diff] [blame] | 199 | break; |
| 200 | case ACTIVITY_STARTED: |
| 201 | post(() -> mActivityManager.moveTaskToFront(mTaskId, 0)); |
| 202 | break; |
Mady Mellor | 6d00203 | 2019-02-13 13:45:17 -0800 | [diff] [blame] | 203 | } |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public void onActivityViewDestroyed(ActivityView view) { |
Mark Renouf | db861c9 | 2019-07-26 13:58:17 -0400 | [diff] [blame] | 208 | if (DEBUG_BUBBLE_EXPANDED_VIEW) { |
| 209 | Log.d(TAG, "onActivityViewDestroyed: mActivityViewStatus=" + mActivityViewStatus |
| 210 | + " bubble=" + getBubbleKey()); |
| 211 | } |
Issei Suzuki | 734bc94 | 2019-06-05 13:59:52 +0200 | [diff] [blame] | 212 | mActivityViewStatus = ActivityViewStatus.RELEASED; |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 213 | } |
| 214 | |
Mark Renouf | 5c732b3 | 2019-06-12 15:14:54 -0400 | [diff] [blame] | 215 | @Override |
| 216 | public void onTaskCreated(int taskId, ComponentName componentName) { |
Mark Renouf | db861c9 | 2019-07-26 13:58:17 -0400 | [diff] [blame] | 217 | if (DEBUG_BUBBLE_EXPANDED_VIEW) { |
| 218 | Log.d(TAG, "onTaskCreated: taskId=" + taskId |
| 219 | + " bubble=" + getBubbleKey()); |
| 220 | } |
Mark Renouf | 5c732b3 | 2019-06-12 15:14:54 -0400 | [diff] [blame] | 221 | // Since Bubble ActivityView applies singleTaskDisplay this is |
| 222 | // guaranteed to only be called once per ActivityView. The taskId is |
| 223 | // saved to use for removeTask, preventing appearance in recent tasks. |
| 224 | mTaskId = taskId; |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | /** |
| 228 | * This is only called for tasks on this ActivityView, which is also set to |
| 229 | * single-task mode -- meaning never more than one task on this display. If a task |
| 230 | * is being removed, it's the top Activity finishing and this bubble should |
| 231 | * be removed or collapsed. |
| 232 | */ |
| 233 | @Override |
| 234 | public void onTaskRemovalStarted(int taskId) { |
Mark Renouf | db861c9 | 2019-07-26 13:58:17 -0400 | [diff] [blame] | 235 | if (DEBUG_BUBBLE_EXPANDED_VIEW) { |
| 236 | Log.d(TAG, "onTaskRemovalStarted: taskId=" + taskId |
| 237 | + " mActivityViewStatus=" + mActivityViewStatus |
| 238 | + " bubble=" + getBubbleKey()); |
| 239 | } |
Mady Mellor | dd6fe61 | 2020-04-15 11:47:55 -0700 | [diff] [blame] | 240 | if (mBubble != null) { |
Mady Mellor | ff076eb | 2019-11-13 10:12:06 -0800 | [diff] [blame] | 241 | // Must post because this is called from a binder thread. |
Pinyao Ting | 3c93061 | 2020-05-19 00:26:03 +0000 | [diff] [blame] | 242 | post(() -> mBubbleController.removeBubble(mBubble.getKey(), |
Mady Mellor | ff076eb | 2019-11-13 10:12:06 -0800 | [diff] [blame] | 243 | BubbleController.DISMISS_TASK_FINISHED)); |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | }; |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 247 | |
Mady Mellor | 3d82e68 | 2019-02-05 13:34:48 -0800 | [diff] [blame] | 248 | public BubbleExpandedView(Context context) { |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 249 | this(context, null); |
| 250 | } |
| 251 | |
Mady Mellor | 3d82e68 | 2019-02-05 13:34:48 -0800 | [diff] [blame] | 252 | public BubbleExpandedView(Context context, AttributeSet attrs) { |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 253 | this(context, attrs, 0); |
| 254 | } |
| 255 | |
Mady Mellor | 3d82e68 | 2019-02-05 13:34:48 -0800 | [diff] [blame] | 256 | public BubbleExpandedView(Context context, AttributeSet attrs, int defStyleAttr) { |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 257 | this(context, attrs, defStyleAttr, 0); |
| 258 | } |
| 259 | |
Mady Mellor | 3d82e68 | 2019-02-05 13:34:48 -0800 | [diff] [blame] | 260 | public BubbleExpandedView(Context context, AttributeSet attrs, int defStyleAttr, |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 261 | int defStyleRes) { |
| 262 | super(context, attrs, defStyleAttr, defStyleRes); |
Lyn Han | bc985c5 | 2020-05-18 17:57:58 -0700 | [diff] [blame] | 263 | updateDimensions(); |
Tony Huang | 8672410 | 2020-06-11 17:51:55 +0800 | [diff] [blame] | 264 | mActivityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE); |
Lyn Han | bc985c5 | 2020-05-18 17:57:58 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | void updateDimensions() { |
Mady Mellor | a96c9ed | 2019-06-07 12:55:26 -0700 | [diff] [blame] | 268 | mDisplaySize = new Point(); |
Lyn Han | bc985c5 | 2020-05-18 17:57:58 -0700 | [diff] [blame] | 269 | mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE); |
Mady Mellor | 8fe411d | 2019-08-16 11:27:53 -0700 | [diff] [blame] | 270 | // Get the real size -- this includes screen decorations (notches, statusbar, navbar). |
Mady Mellor | 9be3bed | 2019-08-21 17:26:26 -0700 | [diff] [blame] | 271 | mWindowManager.getDefaultDisplay().getRealSize(mDisplaySize); |
Mady Mellor | 47b11e3 | 2019-07-11 19:06:21 -0700 | [diff] [blame] | 272 | Resources res = getResources(); |
| 273 | mMinHeight = res.getDimensionPixelSize(R.dimen.bubble_expanded_default_height); |
Lyn Han | cd4f87e | 2020-02-19 20:33:45 -0800 | [diff] [blame] | 274 | mOverflowHeight = res.getDimensionPixelSize(R.dimen.bubble_overflow_height); |
Mady Mellor | 47b11e3 | 2019-07-11 19:06:21 -0700 | [diff] [blame] | 275 | mPointerMargin = res.getDimensionPixelSize(R.dimen.bubble_pointer_margin); |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 276 | } |
| 277 | |
Joshua Tsuji | 847b1d42 | 2020-06-01 13:42:19 -0400 | [diff] [blame] | 278 | @SuppressLint("ClickableViewAccessibility") |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 279 | @Override |
| 280 | protected void onFinishInflate() { |
| 281 | super.onFinishInflate(); |
Mark Renouf | db861c9 | 2019-07-26 13:58:17 -0400 | [diff] [blame] | 282 | if (DEBUG_BUBBLE_EXPANDED_VIEW) { |
| 283 | Log.d(TAG, "onFinishInflate: bubble=" + getBubbleKey()); |
| 284 | } |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 285 | |
| 286 | Resources res = getResources(); |
| 287 | mPointerView = findViewById(R.id.pointer_view); |
Lyn Han | 02cca81 | 2019-04-02 16:27:32 -0700 | [diff] [blame] | 288 | mPointerWidth = res.getDimensionPixelSize(R.dimen.bubble_pointer_width); |
| 289 | mPointerHeight = res.getDimensionPixelSize(R.dimen.bubble_pointer_height); |
Mady Mellor | dd49705 | 2019-01-30 17:23:48 -0800 | [diff] [blame] | 290 | |
Mark Renouf | 34d04f3 | 2019-05-13 15:53:18 -0400 | [diff] [blame] | 291 | mPointerDrawable = new ShapeDrawable(TriangleShape.create( |
Lyn Han | 5aa27e2 | 2019-05-15 10:55:07 -0700 | [diff] [blame] | 292 | mPointerWidth, mPointerHeight, true /* pointUp */)); |
Mady Mellor | a96c9ed | 2019-06-07 12:55:26 -0700 | [diff] [blame] | 293 | mPointerView.setVisibility(INVISIBLE); |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 294 | |
Lyn Han | 02cca81 | 2019-04-02 16:27:32 -0700 | [diff] [blame] | 295 | mSettingsIconHeight = getContext().getResources().getDimensionPixelSize( |
Mady Mellor | 5a3e94b | 2020-02-07 12:16:21 -0800 | [diff] [blame] | 296 | R.dimen.bubble_manage_button_height); |
Lyn Han | 02cca81 | 2019-04-02 16:27:32 -0700 | [diff] [blame] | 297 | mSettingsIcon = findViewById(R.id.settings_button); |
Lyn Han | 02cca81 | 2019-04-02 16:27:32 -0700 | [diff] [blame] | 298 | |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 299 | mActivityView = new ActivityView(mContext, null /* attrs */, 0 /* defStyle */, |
shawnlin | ba532af | 2020-06-02 17:48:37 +0800 | [diff] [blame] | 300 | true /* singleTaskInstance */, false /* usePublicVirtualDisplay*/, |
| 301 | true /* disableSurfaceViewBackgroundLayer */); |
Lyn Han | b58c756 | 2020-01-07 14:29:20 -0800 | [diff] [blame] | 302 | |
lumark | dc9b319 | 2019-07-23 21:18:17 +0800 | [diff] [blame] | 303 | // Set ActivityView's alpha value as zero, since there is no view content to be shown. |
Issei Suzuki | cac2a50 | 2019-04-16 16:52:50 +0200 | [diff] [blame] | 304 | setContentVisibility(false); |
Joshua Tsuji | 06785ab | 2020-06-08 11:18:40 -0400 | [diff] [blame] | 305 | |
| 306 | mActivityViewContainer.setBackgroundColor(Color.WHITE); |
| 307 | mActivityViewContainer.setOutlineProvider(new ViewOutlineProvider() { |
| 308 | @Override |
| 309 | public void getOutline(View view, Outline outline) { |
| 310 | outline.setRoundRect(0, 0, view.getWidth(), view.getHeight(), mCornerRadius); |
| 311 | } |
| 312 | }); |
| 313 | mActivityViewContainer.setClipToOutline(true); |
| 314 | mActivityViewContainer.addView(mActivityView); |
| 315 | mActivityViewContainer.setLayoutParams( |
| 316 | new ViewGroup.LayoutParams(WRAP_CONTENT, WRAP_CONTENT)); |
| 317 | addView(mActivityViewContainer); |
| 318 | |
| 319 | if (mActivityView != null |
| 320 | && mActivityView.getChildCount() > 0 |
| 321 | && mActivityView.getChildAt(0) instanceof SurfaceView) { |
| 322 | // Retrieve the surface from the ActivityView so we can screenshot it and change its |
| 323 | // z-ordering. This should always be possible, since ActivityView's constructor adds the |
| 324 | // SurfaceView as its first child. |
| 325 | mActivitySurface = (SurfaceView) mActivityView.getChildAt(0); |
| 326 | } |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 327 | |
Lyn Han | 5aa27e2 | 2019-05-15 10:55:07 -0700 | [diff] [blame] | 328 | // Expanded stack layout, top to bottom: |
| 329 | // Expanded view container |
| 330 | // ==> bubble row |
| 331 | // ==> expanded view |
| 332 | // ==> activity view |
| 333 | // ==> manage button |
| 334 | bringChildToFront(mActivityView); |
| 335 | bringChildToFront(mSettingsIcon); |
Mady Mellor | 52b1ac6 | 2019-04-10 16:59:03 -0700 | [diff] [blame] | 336 | |
Mark Renouf | 34d04f3 | 2019-05-13 15:53:18 -0400 | [diff] [blame] | 337 | applyThemeAttrs(); |
| 338 | |
Mady Mellor | 5d8f140 | 2019-02-21 18:23:52 -0800 | [diff] [blame] | 339 | setOnApplyWindowInsetsListener((View view, WindowInsets insets) -> { |
| 340 | // Keep track of IME displaying because we should not make any adjustments that might |
| 341 | // 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] | 342 | final int keyboardHeight = insets.getSystemWindowInsetBottom() |
| 343 | - insets.getStableInsetBottom(); |
Mady Mellor | 5d8f140 | 2019-02-21 18:23:52 -0800 | [diff] [blame] | 344 | mKeyboardVisible = keyboardHeight != 0; |
| 345 | if (!mKeyboardVisible && mNeedsNewHeight) { |
| 346 | updateHeight(); |
| 347 | } |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 348 | return view.onApplyWindowInsets(insets); |
| 349 | }); |
Joshua Tsuji | 847b1d42 | 2020-06-01 13:42:19 -0400 | [diff] [blame] | 350 | |
Lyn Han | 3989260 | 2020-06-18 21:15:28 -0700 | [diff] [blame] | 351 | mExpandedViewPadding = res.getDimensionPixelSize(R.dimen.bubble_expanded_view_padding); |
| 352 | setPadding(mExpandedViewPadding, mExpandedViewPadding, mExpandedViewPadding, |
| 353 | mExpandedViewPadding); |
Joshua Tsuji | 847b1d42 | 2020-06-01 13:42:19 -0400 | [diff] [blame] | 354 | setOnTouchListener((view, motionEvent) -> { |
| 355 | if (!usingActivityView()) { |
| 356 | return false; |
| 357 | } |
| 358 | |
| 359 | final Rect avBounds = new Rect(); |
| 360 | mActivityView.getBoundsOnScreen(avBounds); |
| 361 | |
| 362 | // Consume and ignore events on the expanded view padding that are within the |
| 363 | // ActivityView's vertical bounds. These events are part of a back gesture, and so they |
| 364 | // should not collapse the stack (which all other touches on areas around the AV would |
| 365 | // do). |
Josh Tsuji | 5421eb6 | 2020-06-10 18:24:56 -0400 | [diff] [blame] | 366 | if (motionEvent.getRawY() >= avBounds.top |
| 367 | && motionEvent.getRawY() <= avBounds.bottom |
| 368 | && (motionEvent.getRawX() < avBounds.left |
| 369 | || motionEvent.getRawX() > avBounds.right)) { |
Joshua Tsuji | 847b1d42 | 2020-06-01 13:42:19 -0400 | [diff] [blame] | 370 | return true; |
| 371 | } |
| 372 | |
| 373 | return false; |
| 374 | }); |
Joshua Tsuji | 519d4c1 | 2020-05-29 15:22:31 -0400 | [diff] [blame] | 375 | |
| 376 | // BubbleStackView is forced LTR, but we want to respect the locale for expanded view layout |
| 377 | // so the Manage button appears on the right. |
| 378 | setLayoutDirection(LAYOUT_DIRECTION_LOCALE); |
Mady Mellor | e8e0771 | 2019-01-23 12:45:33 -0800 | [diff] [blame] | 379 | } |
| 380 | |
Mark Renouf | db861c9 | 2019-07-26 13:58:17 -0400 | [diff] [blame] | 381 | private String getBubbleKey() { |
| 382 | return mBubble != null ? mBubble.getKey() : "null"; |
| 383 | } |
| 384 | |
Joshua Tsuji | 06785ab | 2020-06-08 11:18:40 -0400 | [diff] [blame] | 385 | /** |
| 386 | * Asks the ActivityView's surface to draw on top of all other views in the window. This is |
| 387 | * useful for ordering surfaces during animations, but should otherwise be set to false so that |
| 388 | * bubbles and menus can draw over the ActivityView. |
| 389 | */ |
| 390 | void setSurfaceZOrderedOnTop(boolean onTop) { |
| 391 | if (mActivitySurface == null) { |
| 392 | return; |
| 393 | } |
| 394 | |
| 395 | mActivitySurface.setZOrderedOnTop(onTop, true); |
| 396 | } |
| 397 | |
| 398 | /** Return a GraphicBuffer with the contents of the ActivityView's underlying surface. */ |
| 399 | @Nullable SurfaceControl.ScreenshotGraphicBuffer snapshotActivitySurface() { |
| 400 | if (mActivitySurface == null) { |
| 401 | return null; |
| 402 | } |
| 403 | |
| 404 | return SurfaceControl.captureLayers( |
| 405 | mActivitySurface.getSurfaceControl(), |
| 406 | new Rect(0, 0, mActivityView.getWidth(), mActivityView.getHeight()), |
| 407 | 1 /* scale */); |
| 408 | } |
| 409 | |
| 410 | int[] getActivityViewLocationOnScreen() { |
| 411 | if (mActivityView != null) { |
| 412 | return mActivityView.getLocationOnScreen(); |
| 413 | } else { |
| 414 | return new int[]{0, 0}; |
| 415 | } |
| 416 | } |
| 417 | |
Joshua Tsuji | 6855cab | 2020-04-16 01:05:39 -0400 | [diff] [blame] | 418 | void setManageClickListener(OnClickListener manageClickListener) { |
| 419 | findViewById(R.id.settings_button).setOnClickListener(manageClickListener); |
| 420 | } |
| 421 | |
| 422 | /** |
| 423 | * Updates the ActivityView's obscured touchable region. This calls onLocationChanged, which |
| 424 | * results in a call to {@link BubbleStackView#subtractObscuredTouchableRegion}. This is useful |
| 425 | * if a view has been added or removed from on top of the ActivityView, such as the manage menu. |
| 426 | */ |
| 427 | void updateObscuredTouchableRegion() { |
Mady Mellor | 64db51a | 2020-05-27 19:00:14 -0700 | [diff] [blame] | 428 | if (mActivityView != null) { |
| 429 | mActivityView.onLocationChanged(); |
| 430 | } |
Joshua Tsuji | 6855cab | 2020-04-16 01:05:39 -0400 | [diff] [blame] | 431 | } |
| 432 | |
Mark Renouf | 34d04f3 | 2019-05-13 15:53:18 -0400 | [diff] [blame] | 433 | void applyThemeAttrs() { |
Lyn Han | 61bfdb3 | 2019-12-10 14:34:12 -0800 | [diff] [blame] | 434 | final TypedArray ta = mContext.obtainStyledAttributes( |
Joshua Tsuji | dd082b6 | 2020-05-20 22:26:24 -0400 | [diff] [blame] | 435 | new int[] {android.R.attr.dialogCornerRadius}); |
Joshua Tsuji | 06785ab | 2020-06-08 11:18:40 -0400 | [diff] [blame] | 436 | mCornerRadius = ta.getDimensionPixelSize(0, 0); |
Mark Renouf | 34d04f3 | 2019-05-13 15:53:18 -0400 | [diff] [blame] | 437 | ta.recycle(); |
| 438 | |
Lyn Han | edb55e2 | 2020-02-12 18:55:39 -0800 | [diff] [blame] | 439 | if (mActivityView != null && ScreenDecorationsUtils.supportsRoundedCornersOnWindows( |
| 440 | mContext.getResources())) { |
Joshua Tsuji | 06785ab | 2020-06-08 11:18:40 -0400 | [diff] [blame] | 441 | mActivityView.setCornerRadius(mCornerRadius); |
Mark Renouf | 34d04f3 | 2019-05-13 15:53:18 -0400 | [diff] [blame] | 442 | } |
Lyn Han | 26bb198 | 2020-06-08 15:38:01 -0700 | [diff] [blame] | 443 | |
| 444 | final int mode = |
| 445 | getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK; |
| 446 | switch (mode) { |
| 447 | case Configuration.UI_MODE_NIGHT_NO: |
Lyn Han | 79d10bf | 2020-06-11 14:36:06 -0700 | [diff] [blame] | 448 | mPointerDrawable.setTint(getResources().getColor(R.color.bubbles_light)); |
Lyn Han | 26bb198 | 2020-06-08 15:38:01 -0700 | [diff] [blame] | 449 | break; |
| 450 | case Configuration.UI_MODE_NIGHT_YES: |
Lyn Han | 79d10bf | 2020-06-11 14:36:06 -0700 | [diff] [blame] | 451 | mPointerDrawable.setTint(getResources().getColor(R.color.bubbles_dark)); |
Lyn Han | 26bb198 | 2020-06-08 15:38:01 -0700 | [diff] [blame] | 452 | break; |
| 453 | } |
| 454 | mPointerView.setBackground(mPointerDrawable); |
Mark Renouf | 34d04f3 | 2019-05-13 15:53:18 -0400 | [diff] [blame] | 455 | } |
| 456 | |
Joshua Tsuji | 847b1d42 | 2020-06-01 13:42:19 -0400 | [diff] [blame] | 457 | /** |
| 458 | * Hides the IME if it's showing. This is currently done by dispatching a back press to the AV. |
| 459 | */ |
| 460 | void hideImeIfVisible() { |
| 461 | if (mKeyboardVisible) { |
| 462 | performBackPressIfNeeded(); |
| 463 | } |
| 464 | } |
| 465 | |
Mady Mellor | 5d8f140 | 2019-02-21 18:23:52 -0800 | [diff] [blame] | 466 | @Override |
| 467 | protected void onDetachedFromWindow() { |
| 468 | super.onDetachedFromWindow(); |
| 469 | mKeyboardVisible = false; |
| 470 | mNeedsNewHeight = false; |
| 471 | if (mActivityView != null) { |
Mady Mellor | af02fa4 | 2020-03-04 14:22:16 -0800 | [diff] [blame] | 472 | if (sNewInsetsMode == NEW_INSETS_MODE_FULL) { |
Yunfan Chen | b83940c | 2020-04-06 16:43:09 +0900 | [diff] [blame] | 473 | setImeWindowToDisplay(0, 0); |
Mady Mellor | af02fa4 | 2020-03-04 14:22:16 -0800 | [diff] [blame] | 474 | } else { |
| 475 | mActivityView.setForwardedInsets(Insets.of(0, 0, 0, 0)); |
| 476 | } |
Mady Mellor | 5d8f140 | 2019-02-21 18:23:52 -0800 | [diff] [blame] | 477 | } |
Mark Renouf | db861c9 | 2019-07-26 13:58:17 -0400 | [diff] [blame] | 478 | if (DEBUG_BUBBLE_EXPANDED_VIEW) { |
| 479 | Log.d(TAG, "onDetachedFromWindow: bubble=" + getBubbleKey()); |
| 480 | } |
Mady Mellor | 5d8f140 | 2019-02-21 18:23:52 -0800 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | /** |
Issei Suzuki | cac2a50 | 2019-04-16 16:52:50 +0200 | [diff] [blame] | 484 | * Set visibility of contents in the expanded state. |
| 485 | * |
| 486 | * @param visibility {@code true} if the contents should be visible on the screen. |
| 487 | * |
| 488 | * Note that this contents visibility doesn't affect visibility at {@link android.view.View}, |
| 489 | * and setting {@code false} actually means rendering the contents in transparent. |
| 490 | */ |
| 491 | void setContentVisibility(boolean visibility) { |
Mark Renouf | db861c9 | 2019-07-26 13:58:17 -0400 | [diff] [blame] | 492 | if (DEBUG_BUBBLE_EXPANDED_VIEW) { |
| 493 | Log.d(TAG, "setContentVisibility: visibility=" + visibility |
| 494 | + " bubble=" + getBubbleKey()); |
| 495 | } |
Issei Suzuki | cac2a50 | 2019-04-16 16:52:50 +0200 | [diff] [blame] | 496 | final float alpha = visibility ? 1f : 0f; |
Joshua Tsuji | ffa7603 | 2020-06-19 17:11:10 -0400 | [diff] [blame] | 497 | |
| 498 | if (alpha == mActivityView.getAlpha()) { |
| 499 | return; |
| 500 | } |
| 501 | |
Issei Suzuki | cac2a50 | 2019-04-16 16:52:50 +0200 | [diff] [blame] | 502 | mPointerView.setAlpha(alpha); |
| 503 | if (mActivityView != null) { |
| 504 | mActivityView.setAlpha(alpha); |
Joshua Tsuji | 06785ab | 2020-06-08 11:18:40 -0400 | [diff] [blame] | 505 | mActivityView.bringToFront(); |
Issei Suzuki | cac2a50 | 2019-04-16 16:52:50 +0200 | [diff] [blame] | 506 | } |
| 507 | } |
| 508 | |
Josh Tsuji | 7da996a | 2020-06-25 11:45:26 -0400 | [diff] [blame] | 509 | @Nullable ActivityView getActivityView() { |
| 510 | return mActivityView; |
| 511 | } |
| 512 | |
| 513 | int getTaskId() { |
| 514 | return mTaskId; |
| 515 | } |
| 516 | |
Issei Suzuki | cac2a50 | 2019-04-16 16:52:50 +0200 | [diff] [blame] | 517 | /** |
Mady Mellor | 5d8f140 | 2019-02-21 18:23:52 -0800 | [diff] [blame] | 518 | * Called by {@link BubbleStackView} when the insets for the expanded state should be updated. |
| 519 | * This should be done post-move and post-animation. |
| 520 | */ |
| 521 | void updateInsets(WindowInsets insets) { |
| 522 | if (usingActivityView()) { |
Mady Mellor | 8fe411d | 2019-08-16 11:27:53 -0700 | [diff] [blame] | 523 | int[] screenLoc = mActivityView.getLocationOnScreen(); |
| 524 | final int activityViewBottom = screenLoc[1] + mActivityView.getHeight(); |
Mady Mellor | 9be3bed | 2019-08-21 17:26:26 -0700 | [diff] [blame] | 525 | final int keyboardTop = mDisplaySize.y - Math.max(insets.getSystemWindowInsetBottom(), |
| 526 | insets.getDisplayCutout() != null |
| 527 | ? insets.getDisplayCutout().getSafeInsetBottom() |
| 528 | : 0); |
Mady Mellor | 8fe411d | 2019-08-16 11:27:53 -0700 | [diff] [blame] | 529 | final int insetsBottom = Math.max(activityViewBottom - keyboardTop, 0); |
Jorim Jaggi | 924ef75 | 2020-01-29 17:26:55 +0100 | [diff] [blame] | 530 | |
Jorim Jaggi | 924ef75 | 2020-01-29 17:26:55 +0100 | [diff] [blame] | 531 | if (sNewInsetsMode == NEW_INSETS_MODE_FULL) { |
Yunfan Chen | b83940c | 2020-04-06 16:43:09 +0900 | [diff] [blame] | 532 | setImeWindowToDisplay(getWidth(), insetsBottom); |
Jorim Jaggi | 924ef75 | 2020-01-29 17:26:55 +0100 | [diff] [blame] | 533 | } else { |
| 534 | mActivityView.setForwardedInsets(Insets.of(0, 0, 0, insetsBottom)); |
| 535 | } |
Mady Mellor | 5d8f140 | 2019-02-21 18:23:52 -0800 | [diff] [blame] | 536 | } |
| 537 | } |
| 538 | |
Yunfan Chen | b83940c | 2020-04-06 16:43:09 +0900 | [diff] [blame] | 539 | private void setImeWindowToDisplay(int w, int h) { |
| 540 | if (getVirtualDisplayId() == INVALID_DISPLAY) { |
| 541 | return; |
| 542 | } |
| 543 | if (h == 0 || w == 0) { |
| 544 | if (mImeShowing) { |
| 545 | mVirtualImeView.setVisibility(GONE); |
| 546 | mImeShowing = false; |
| 547 | } |
| 548 | return; |
| 549 | } |
| 550 | final Context virtualDisplayContext = mContext.createDisplayContext( |
| 551 | getVirtualDisplay().getDisplay()); |
| 552 | |
| 553 | if (mVirtualDisplayWindowManager == null) { |
| 554 | mVirtualDisplayWindowManager = |
| 555 | (WindowManager) virtualDisplayContext.getSystemService(Context.WINDOW_SERVICE); |
| 556 | } |
| 557 | if (mVirtualImeView == null) { |
| 558 | mVirtualImeView = new View(virtualDisplayContext); |
| 559 | mVirtualImeView.setVisibility(VISIBLE); |
| 560 | mVirtualDisplayWindowManager.addView(mVirtualImeView, |
| 561 | getVirtualImeViewAttrs(w, h)); |
| 562 | } else { |
| 563 | mVirtualDisplayWindowManager.updateViewLayout(mVirtualImeView, |
| 564 | getVirtualImeViewAttrs(w, h)); |
| 565 | mVirtualImeView.setVisibility(VISIBLE); |
| 566 | } |
| 567 | |
| 568 | mImeShowing = true; |
| 569 | } |
| 570 | |
| 571 | private WindowManager.LayoutParams getVirtualImeViewAttrs(int w, int h) { |
| 572 | // To use TYPE_NAVIGATION_BAR_PANEL instead of TYPE_IME_BAR to bypass the IME window type |
| 573 | // token check when adding the window. |
| 574 | final WindowManager.LayoutParams attrs = |
| 575 | new WindowManager.LayoutParams(w, h, TYPE_NAVIGATION_BAR_PANEL, |
| 576 | FLAG_LAYOUT_NO_LIMITS | FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCHABLE, |
| 577 | TRANSPARENT); |
| 578 | attrs.gravity = Gravity.BOTTOM; |
| 579 | attrs.setTitle(WINDOW_TITLE); |
| 580 | attrs.token = new Binder(); |
| 581 | attrs.providesInsetsTypes = new int[]{ITYPE_IME}; |
| 582 | attrs.alpha = 0.0f; |
| 583 | return attrs; |
| 584 | } |
| 585 | |
Mady Mellor | 247ca2c | 2019-12-02 16:18:59 -0800 | [diff] [blame] | 586 | void setStackView(BubbleStackView stackView) { |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 587 | mStackView = stackView; |
Mady Mellor | 6d00203 | 2019-02-13 13:45:17 -0800 | [diff] [blame] | 588 | } |
| 589 | |
Lyn Han | b58c756 | 2020-01-07 14:29:20 -0800 | [diff] [blame] | 590 | public void setOverflow(boolean overflow) { |
| 591 | mIsOverflow = overflow; |
| 592 | |
| 593 | Intent target = new Intent(mContext, BubbleOverflowActivity.class); |
| 594 | mPendingIntent = PendingIntent.getActivity(mContext, /* requestCode */ 0, |
| 595 | target, PendingIntent.FLAG_UPDATE_CURRENT); |
| 596 | mSettingsIcon.setVisibility(GONE); |
| 597 | } |
| 598 | |
Mady Mellor | 6d00203 | 2019-02-13 13:45:17 -0800 | [diff] [blame] | 599 | /** |
Mady Mellor | 247ca2c | 2019-12-02 16:18:59 -0800 | [diff] [blame] | 600 | * Sets the bubble used to populate this view. |
Mady Mellor | 6d00203 | 2019-02-13 13:45:17 -0800 | [diff] [blame] | 601 | */ |
Mady Mellor | 247ca2c | 2019-12-02 16:18:59 -0800 | [diff] [blame] | 602 | void update(Bubble bubble) { |
| 603 | if (DEBUG_BUBBLE_EXPANDED_VIEW) { |
| 604 | Log.d(TAG, "update: bubble=" + (bubble != null ? bubble.getKey() : "null")); |
| 605 | } |
Mady Mellor | 458a626 | 2020-06-07 21:09:19 -0700 | [diff] [blame] | 606 | boolean isNew = mBubble == null || didBackingContentChange(bubble); |
Lyn Han | b58c756 | 2020-01-07 14:29:20 -0800 | [diff] [blame] | 607 | if (isNew || bubble != null && bubble.getKey().equals(mBubble.getKey())) { |
Mady Mellor | 247ca2c | 2019-12-02 16:18:59 -0800 | [diff] [blame] | 608 | mBubble = bubble; |
| 609 | mSettingsIcon.setContentDescription(getResources().getString( |
| 610 | R.string.bubbles_settings_button_description, bubble.getAppName())); |
| 611 | |
Lyn Han | 670fa80 | 2020-05-05 12:56:33 -0700 | [diff] [blame] | 612 | mSettingsIcon.setAccessibilityDelegate( |
| 613 | new AccessibilityDelegate() { |
| 614 | @Override |
| 615 | public void onInitializeAccessibilityNodeInfo(View host, |
| 616 | AccessibilityNodeInfo info) { |
| 617 | super.onInitializeAccessibilityNodeInfo(host, info); |
| 618 | // On focus, have TalkBack say |
| 619 | // "Actions available. Use swipe up then right to view." |
| 620 | // in addition to the default "double tap to activate". |
| 621 | mStackView.setupLocalMenu(info); |
| 622 | } |
| 623 | }); |
| 624 | |
Mady Mellor | 247ca2c | 2019-12-02 16:18:59 -0800 | [diff] [blame] | 625 | if (isNew) { |
Lyn Han | b58c756 | 2020-01-07 14:29:20 -0800 | [diff] [blame] | 626 | mPendingIntent = mBubble.getBubbleIntent(); |
Lyn Han | efbaa97 | 2020-06-23 14:59:39 -0700 | [diff] [blame] | 627 | if (mPendingIntent != null || mBubble.hasMetadataShortcutId()) { |
Mady Mellor | 247ca2c | 2019-12-02 16:18:59 -0800 | [diff] [blame] | 628 | setContentVisibility(false); |
| 629 | mActivityView.setVisibility(VISIBLE); |
| 630 | } |
| 631 | } |
| 632 | applyThemeAttrs(); |
| 633 | } else { |
| 634 | Log.w(TAG, "Trying to update entry with different key, new bubble: " |
| 635 | + bubble.getKey() + " old bubble: " + bubble.getKey()); |
| 636 | } |
| 637 | } |
| 638 | |
Mady Mellor | 458a626 | 2020-06-07 21:09:19 -0700 | [diff] [blame] | 639 | private boolean didBackingContentChange(Bubble newBubble) { |
| 640 | boolean prevWasIntentBased = mBubble != null && mPendingIntent != null; |
| 641 | boolean newIsIntentBased = newBubble.getBubbleIntent() != null; |
| 642 | return prevWasIntentBased != newIsIntentBased; |
| 643 | } |
| 644 | |
Mady Mellor | 247ca2c | 2019-12-02 16:18:59 -0800 | [diff] [blame] | 645 | /** |
| 646 | * Lets activity view know it should be shown / populated with activity content. |
| 647 | */ |
| 648 | void populateExpandedView() { |
Mark Renouf | db861c9 | 2019-07-26 13:58:17 -0400 | [diff] [blame] | 649 | if (DEBUG_BUBBLE_EXPANDED_VIEW) { |
| 650 | Log.d(TAG, "populateExpandedView: " |
| 651 | + "bubble=" + getBubbleKey()); |
| 652 | } |
| 653 | |
Mady Mellor | 5029fa6 | 2019-03-05 12:16:21 -0800 | [diff] [blame] | 654 | if (usingActivityView()) { |
| 655 | mActivityView.setCallback(mStateCallback); |
| 656 | } else { |
Issei Suzuki | a91f396 | 2019-06-07 11:48:23 +0200 | [diff] [blame] | 657 | Log.e(TAG, "Cannot populate expanded view."); |
Mady Mellor | 5029fa6 | 2019-03-05 12:16:21 -0800 | [diff] [blame] | 658 | } |
Mady Mellor | 9801e85 | 2019-01-22 14:50:28 -0800 | [diff] [blame] | 659 | } |
| 660 | |
Mark Renouf | 041d726 | 2019-02-06 12:09:41 -0500 | [diff] [blame] | 661 | boolean performBackPressIfNeeded() { |
Mady Mellor | 323fb0b | 2019-03-25 12:15:22 -0700 | [diff] [blame] | 662 | if (!usingActivityView()) { |
Mark Renouf | 041d726 | 2019-02-06 12:09:41 -0500 | [diff] [blame] | 663 | return false; |
| 664 | } |
| 665 | mActivityView.performBackPress(); |
| 666 | return true; |
| 667 | } |
| 668 | |
Mady Mellor | fe7ec03 | 2019-01-30 17:32:49 -0800 | [diff] [blame] | 669 | void updateHeight() { |
Mark Renouf | db861c9 | 2019-07-26 13:58:17 -0400 | [diff] [blame] | 670 | if (DEBUG_BUBBLE_EXPANDED_VIEW) { |
| 671 | Log.d(TAG, "updateHeight: bubble=" + getBubbleKey()); |
| 672 | } |
Joshua Tsuji | 06785ab | 2020-06-08 11:18:40 -0400 | [diff] [blame] | 673 | |
| 674 | if (mExpandedViewContainerLocation == null) { |
| 675 | return; |
| 676 | } |
| 677 | |
Mady Mellor | fe7ec03 | 2019-01-30 17:32:49 -0800 | [diff] [blame] | 678 | if (usingActivityView()) { |
Lyn Han | cd4f87e | 2020-02-19 20:33:45 -0800 | [diff] [blame] | 679 | float desiredHeight = mOverflowHeight; |
Lyn Han | b58c756 | 2020-01-07 14:29:20 -0800 | [diff] [blame] | 680 | if (!mIsOverflow) { |
| 681 | desiredHeight = Math.max(mBubble.getDesiredHeight(mContext), mMinHeight); |
| 682 | } |
Mady Mellor | 8fe411d | 2019-08-16 11:27:53 -0700 | [diff] [blame] | 683 | float height = Math.min(desiredHeight, getMaxExpandedHeight()); |
Lyn Han | 55f53a5 | 2020-06-12 01:19:05 -0700 | [diff] [blame] | 684 | height = Math.max(height, mMinHeight); |
Joshua Tsuji | 06785ab | 2020-06-08 11:18:40 -0400 | [diff] [blame] | 685 | ViewGroup.LayoutParams lp = mActivityView.getLayoutParams(); |
Lyn Han | cd4f87e | 2020-02-19 20:33:45 -0800 | [diff] [blame] | 686 | mNeedsNewHeight = lp.height != height; |
Mady Mellor | 5d8f140 | 2019-02-21 18:23:52 -0800 | [diff] [blame] | 687 | if (!mKeyboardVisible) { |
| 688 | // If the keyboard is visible... don't adjust the height because that will cause |
| 689 | // a configuration change and the keyboard will be lost. |
Mady Mellor | 7af771a | 2019-03-07 15:04:54 -0800 | [diff] [blame] | 690 | lp.height = (int) height; |
Mady Mellor | 5d8f140 | 2019-02-21 18:23:52 -0800 | [diff] [blame] | 691 | mActivityView.setLayoutParams(lp); |
| 692 | mNeedsNewHeight = false; |
| 693 | } |
Mark Renouf | db861c9 | 2019-07-26 13:58:17 -0400 | [diff] [blame] | 694 | if (DEBUG_BUBBLE_EXPANDED_VIEW) { |
Joshua Tsuji | 06785ab | 2020-06-08 11:18:40 -0400 | [diff] [blame] | 695 | Log.d(TAG, "updateHeight: bubble=" + getBubbleKey() |
| 696 | + " height=" + height |
Mark Renouf | db861c9 | 2019-07-26 13:58:17 -0400 | [diff] [blame] | 697 | + " mNeedsNewHeight=" + mNeedsNewHeight); |
| 698 | } |
Mady Mellor | fe7ec03 | 2019-01-30 17:32:49 -0800 | [diff] [blame] | 699 | } |
| 700 | } |
| 701 | |
Mady Mellor | a96c9ed | 2019-06-07 12:55:26 -0700 | [diff] [blame] | 702 | private int getMaxExpandedHeight() { |
Mady Mellor | 9be3bed | 2019-08-21 17:26:26 -0700 | [diff] [blame] | 703 | mWindowManager.getDefaultDisplay().getRealSize(mDisplaySize); |
Mady Mellor | 8fe411d | 2019-08-16 11:27:53 -0700 | [diff] [blame] | 704 | int bottomInset = getRootWindowInsets() != null |
| 705 | ? getRootWindowInsets().getStableInsetBottom() |
| 706 | : 0; |
Joshua Tsuji | 06785ab | 2020-06-08 11:18:40 -0400 | [diff] [blame] | 707 | |
| 708 | return mDisplaySize.y |
| 709 | - mExpandedViewContainerLocation[1] |
| 710 | - getPaddingTop() |
| 711 | - getPaddingBottom() |
| 712 | - mSettingsIconHeight |
| 713 | - mPointerHeight |
Mady Mellor | 8fe411d | 2019-08-16 11:27:53 -0700 | [diff] [blame] | 714 | - mPointerMargin - bottomInset; |
Mady Mellor | a96c9ed | 2019-06-07 12:55:26 -0700 | [diff] [blame] | 715 | } |
| 716 | |
Mady Mellor | 47b11e3 | 2019-07-11 19:06:21 -0700 | [diff] [blame] | 717 | /** |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 718 | * Update appearance of the expanded view being displayed. |
Joshua Tsuji | 06785ab | 2020-06-08 11:18:40 -0400 | [diff] [blame] | 719 | * |
| 720 | * @param containerLocationOnScreen The location on-screen of the container the expanded view is |
| 721 | * added to. This allows us to calculate max height without |
| 722 | * waiting for layout. |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 723 | */ |
Joshua Tsuji | 06785ab | 2020-06-08 11:18:40 -0400 | [diff] [blame] | 724 | public void updateView(int[] containerLocationOnScreen) { |
Mark Renouf | db861c9 | 2019-07-26 13:58:17 -0400 | [diff] [blame] | 725 | if (DEBUG_BUBBLE_EXPANDED_VIEW) { |
| 726 | Log.d(TAG, "updateView: bubble=" |
| 727 | + getBubbleKey()); |
| 728 | } |
Joshua Tsuji | 06785ab | 2020-06-08 11:18:40 -0400 | [diff] [blame] | 729 | |
| 730 | mExpandedViewContainerLocation = containerLocationOnScreen; |
| 731 | |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 732 | if (usingActivityView() |
| 733 | && mActivityView.getVisibility() == VISIBLE |
| 734 | && mActivityView.isAttachedToWindow()) { |
| 735 | mActivityView.onLocationChanged(); |
Joshua Tsuji | 06785ab | 2020-06-08 11:18:40 -0400 | [diff] [blame] | 736 | updateHeight(); |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 737 | } |
| 738 | } |
| 739 | |
| 740 | /** |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 741 | * Set the x position that the tip of the triangle should point to. |
| 742 | */ |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 743 | public void setPointerPosition(float x) { |
Lyn Han | 9a2f5cf | 2019-05-23 11:01:41 -0700 | [diff] [blame] | 744 | float halfPointerWidth = mPointerWidth / 2f; |
Lyn Han | 3989260 | 2020-06-18 21:15:28 -0700 | [diff] [blame] | 745 | float pointerLeft = x - halfPointerWidth - mExpandedViewPadding; |
Lyn Han | 9a2f5cf | 2019-05-23 11:01:41 -0700 | [diff] [blame] | 746 | mPointerView.setTranslationX(pointerLeft); |
Lyn Han | f74ba67 | 2019-05-20 16:08:48 -0700 | [diff] [blame] | 747 | mPointerView.setVisibility(VISIBLE); |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | /** |
Mady Mellor | 5a3e94b | 2020-02-07 12:16:21 -0800 | [diff] [blame] | 751 | * Position of the manage button displayed in the expanded view. Used for placing user |
| 752 | * education about the manage button. |
| 753 | */ |
Joshua Tsuji | 6855cab | 2020-04-16 01:05:39 -0400 | [diff] [blame] | 754 | public void getManageButtonBoundsOnScreen(Rect rect) { |
| 755 | mSettingsIcon.getBoundsOnScreen(rect); |
Mady Mellor | 5a3e94b | 2020-02-07 12:16:21 -0800 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | /** |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 759 | * 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] | 760 | */ |
Mady Mellor | 94d94a7 | 2019-03-05 18:16:59 -0800 | [diff] [blame] | 761 | public void cleanUpExpandedState() { |
Mark Renouf | db861c9 | 2019-07-26 13:58:17 -0400 | [diff] [blame] | 762 | if (DEBUG_BUBBLE_EXPANDED_VIEW) { |
| 763 | Log.d(TAG, "cleanUpExpandedState: mActivityViewStatus=" + mActivityViewStatus |
| 764 | + ", bubble=" + getBubbleKey()); |
| 765 | } |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 766 | if (mActivityView == null) { |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 767 | return; |
| 768 | } |
shawnlin | 2806269 | 2020-06-15 13:55:51 +0800 | [diff] [blame] | 769 | mActivityView.release(); |
Mark Renouf | 5c732b3 | 2019-06-12 15:14:54 -0400 | [diff] [blame] | 770 | if (mTaskId != -1) { |
| 771 | try { |
| 772 | ActivityTaskManager.getService().removeTask(mTaskId); |
| 773 | } catch (RemoteException e) { |
| 774 | Log.w(TAG, "Failed to remove taskId " + mTaskId); |
| 775 | } |
| 776 | mTaskId = -1; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 777 | } |
Mark Renouf | 28c250d | 2019-02-25 16:47:34 -0500 | [diff] [blame] | 778 | removeView(mActivityView); |
Mark Renouf | 5c732b3 | 2019-06-12 15:14:54 -0400 | [diff] [blame] | 779 | |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 780 | mActivityView = null; |
Issei Suzuki | 734bc94 | 2019-06-05 13:59:52 +0200 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | /** |
| 784 | * Called when the last task is removed from a {@link android.hardware.display.VirtualDisplay} |
| 785 | * which {@link ActivityView} uses. |
| 786 | */ |
| 787 | void notifyDisplayEmpty() { |
Mark Renouf | db861c9 | 2019-07-26 13:58:17 -0400 | [diff] [blame] | 788 | if (DEBUG_BUBBLE_EXPANDED_VIEW) { |
| 789 | Log.d(TAG, "notifyDisplayEmpty: bubble=" |
| 790 | + getBubbleKey() |
| 791 | + " mActivityViewStatus=" + mActivityViewStatus); |
| 792 | } |
Issei Suzuki | 734bc94 | 2019-06-05 13:59:52 +0200 | [diff] [blame] | 793 | if (mActivityViewStatus == ActivityViewStatus.ACTIVITY_STARTED) { |
| 794 | mActivityViewStatus = ActivityViewStatus.INITIALIZED; |
| 795 | } |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 796 | } |
| 797 | |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 798 | private boolean usingActivityView() { |
Lyn Han | efbaa97 | 2020-06-23 14:59:39 -0700 | [diff] [blame] | 799 | return (mPendingIntent != null || mBubble.hasMetadataShortcutId()) |
Mady Mellor | 2ac2d3a | 2020-01-08 17:18:54 -0800 | [diff] [blame] | 800 | && mActivityView != null; |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 801 | } |
| 802 | |
Mady Mellor | 390bff4 | 2019-04-05 15:09:01 -0700 | [diff] [blame] | 803 | /** |
| 804 | * @return the display id of the virtual display. |
| 805 | */ |
| 806 | public int getVirtualDisplayId() { |
| 807 | if (usingActivityView()) { |
| 808 | return mActivityView.getVirtualDisplayId(); |
| 809 | } |
| 810 | return INVALID_DISPLAY; |
| 811 | } |
Yunfan Chen | b83940c | 2020-04-06 16:43:09 +0900 | [diff] [blame] | 812 | |
| 813 | private VirtualDisplay getVirtualDisplay() { |
| 814 | if (usingActivityView()) { |
| 815 | return mActivityView.getVirtualDisplay(); |
| 816 | } |
| 817 | return null; |
| 818 | } |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 819 | } |