Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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.pip.phone; |
| 18 | |
Winson Chung | a556fe6 | 2017-12-19 14:46:23 -0800 | [diff] [blame] | 19 | import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK; |
| 20 | import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK; |
| 21 | import static android.provider.Settings.ACTION_PICTURE_IN_PICTURE_SETTINGS; |
Rhed Jao | 5e00c98 | 2018-11-27 19:41:05 +0800 | [diff] [blame] | 22 | import static android.view.accessibility.AccessibilityManager.FLAG_CONTENT_CONTROLS; |
| 23 | import static android.view.accessibility.AccessibilityManager.FLAG_CONTENT_ICONS; |
Winson Chung | a556fe6 | 2017-12-19 14:46:23 -0800 | [diff] [blame] | 24 | |
Winson Chung | e7a3d22 | 2017-03-09 13:26:45 -0800 | [diff] [blame] | 25 | import static com.android.systemui.pip.phone.PipMenuActivityController.EXTRA_ACTIONS; |
Winson Chung | 0f873de | 2017-03-30 10:26:48 -0700 | [diff] [blame] | 26 | import static com.android.systemui.pip.phone.PipMenuActivityController.EXTRA_ALLOW_TIMEOUT; |
Winson Chung | e7a3d22 | 2017-03-09 13:26:45 -0800 | [diff] [blame] | 27 | import static com.android.systemui.pip.phone.PipMenuActivityController.EXTRA_CONTROLLER_MESSENGER; |
Winson Chung | 853c99a | 2017-03-21 22:16:42 -0700 | [diff] [blame] | 28 | import static com.android.systemui.pip.phone.PipMenuActivityController.EXTRA_DISMISS_FRACTION; |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 29 | import static com.android.systemui.pip.phone.PipMenuActivityController.EXTRA_MENU_STATE; |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 30 | import static com.android.systemui.pip.phone.PipMenuActivityController.EXTRA_MOVEMENT_BOUNDS; |
Winson Chung | 6b88baf | 2017-03-16 17:10:21 -0700 | [diff] [blame] | 31 | import static com.android.systemui.pip.phone.PipMenuActivityController.EXTRA_STACK_BOUNDS; |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 32 | import static com.android.systemui.pip.phone.PipMenuActivityController.EXTRA_WILL_RESIZE_MENU; |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 33 | import static com.android.systemui.pip.phone.PipMenuActivityController.MENU_STATE_CLOSE; |
| 34 | import static com.android.systemui.pip.phone.PipMenuActivityController.MENU_STATE_FULL; |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 35 | import static com.android.systemui.pip.phone.PipMenuActivityController.MENU_STATE_NONE; |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 36 | |
Winson Chung | c75ffe8 | 2016-12-16 16:20:16 -0800 | [diff] [blame] | 37 | import android.animation.Animator; |
| 38 | import android.animation.AnimatorListenerAdapter; |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 39 | import android.animation.AnimatorSet; |
Winson Chung | c75ffe8 | 2016-12-16 16:20:16 -0800 | [diff] [blame] | 40 | import android.animation.ObjectAnimator; |
Mady Mellor | 81d4061 | 2017-03-10 15:14:10 -0800 | [diff] [blame] | 41 | import android.animation.ValueAnimator; |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 42 | import android.annotation.Nullable; |
| 43 | import android.app.Activity; |
| 44 | import android.app.ActivityManager; |
Winson Chung | 9c2a686 | 2017-02-03 12:11:00 -0800 | [diff] [blame] | 45 | import android.app.PendingIntent.CanceledException; |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 46 | import android.app.RemoteAction; |
Winson Chung | a556fe6 | 2017-12-19 14:46:23 -0800 | [diff] [blame] | 47 | import android.content.ComponentName; |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 48 | import android.content.Intent; |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 49 | import android.content.pm.ParceledListSlice; |
Winson Chung | 56fe883 | 2017-02-03 14:37:53 -0800 | [diff] [blame] | 50 | import android.graphics.Color; |
Winson Chung | e7a3d22 | 2017-03-09 13:26:45 -0800 | [diff] [blame] | 51 | import android.graphics.Rect; |
Mady Mellor | 81d4061 | 2017-03-10 15:14:10 -0800 | [diff] [blame] | 52 | import android.graphics.drawable.ColorDrawable; |
| 53 | import android.graphics.drawable.Drawable; |
Winson Chung | a556fe6 | 2017-12-19 14:46:23 -0800 | [diff] [blame] | 54 | import android.net.Uri; |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 55 | import android.os.Bundle; |
| 56 | import android.os.Handler; |
| 57 | import android.os.Message; |
| 58 | import android.os.Messenger; |
| 59 | import android.os.RemoteException; |
Winson Chung | a556fe6 | 2017-12-19 14:46:23 -0800 | [diff] [blame] | 60 | import android.os.UserHandle; |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 61 | import android.util.Log; |
Winson Chung | a556fe6 | 2017-12-19 14:46:23 -0800 | [diff] [blame] | 62 | import android.util.Pair; |
Ben Lin | 8e90d0a | 2020-02-26 14:57:14 -0800 | [diff] [blame] | 63 | import android.view.KeyEvent; |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 64 | import android.view.LayoutInflater; |
Winson Chung | c75ffe8 | 2016-12-16 16:20:16 -0800 | [diff] [blame] | 65 | import android.view.MotionEvent; |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 66 | import android.view.View; |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 67 | import android.view.ViewGroup; |
Winson Chung | c75ffe8 | 2016-12-16 16:20:16 -0800 | [diff] [blame] | 68 | import android.view.WindowManager.LayoutParams; |
Rhed Jao | 5e00c98 | 2018-11-27 19:41:05 +0800 | [diff] [blame] | 69 | import android.view.accessibility.AccessibilityManager; |
Mady Mellor | a7f6974 | 2017-02-03 11:00:20 -0800 | [diff] [blame] | 70 | import android.widget.FrameLayout; |
Hongwei Wang | a6e034e | 2019-06-20 13:18:43 -0700 | [diff] [blame] | 71 | import android.widget.ImageButton; |
Mady Mellor | a7f6974 | 2017-02-03 11:00:20 -0800 | [diff] [blame] | 72 | import android.widget.LinearLayout; |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 73 | |
Winson Chung | c75ffe8 | 2016-12-16 16:20:16 -0800 | [diff] [blame] | 74 | import com.android.systemui.Interpolators; |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 75 | import com.android.systemui.R; |
| 76 | |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 77 | import java.util.ArrayList; |
Winson Chung | 26db397 | 2017-03-23 15:17:01 -0700 | [diff] [blame] | 78 | import java.util.Collections; |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 79 | import java.util.List; |
| 80 | |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 81 | /** |
| 82 | * Translucent activity that gets started on top of a task in PIP to allow the user to control it. |
| 83 | */ |
| 84 | public class PipMenuActivity extends Activity { |
| 85 | |
| 86 | private static final String TAG = "PipMenuActivity"; |
| 87 | |
Winson Chung | c75ffe8 | 2016-12-16 16:20:16 -0800 | [diff] [blame] | 88 | public static final int MESSAGE_SHOW_MENU = 1; |
Mady Mellor | a7f6974 | 2017-02-03 11:00:20 -0800 | [diff] [blame] | 89 | public static final int MESSAGE_POKE_MENU = 2; |
| 90 | public static final int MESSAGE_HIDE_MENU = 3; |
| 91 | public static final int MESSAGE_UPDATE_ACTIONS = 4; |
Mady Mellor | 81d4061 | 2017-03-10 15:14:10 -0800 | [diff] [blame] | 92 | public static final int MESSAGE_UPDATE_DISMISS_FRACTION = 5; |
Winson Chung | 3448824 | 2017-04-26 15:53:51 -0700 | [diff] [blame] | 93 | public static final int MESSAGE_ANIMATION_ENDED = 6; |
Arthur Hung | d896197 | 2019-10-14 17:00:17 +0800 | [diff] [blame] | 94 | public static final int MESSAGE_POINTER_EVENT = 7; |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 95 | |
Rhed Jao | 5e00c98 | 2018-11-27 19:41:05 +0800 | [diff] [blame] | 96 | private static final int INITIAL_DISMISS_DELAY = 3500; |
| 97 | private static final int POST_INTERACTION_DISMISS_DELAY = 2000; |
Winson Chung | c75ffe8 | 2016-12-16 16:20:16 -0800 | [diff] [blame] | 98 | private static final long MENU_FADE_DURATION = 125; |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 99 | |
Mady Mellor | 81d4061 | 2017-03-10 15:14:10 -0800 | [diff] [blame] | 100 | private static final float MENU_BACKGROUND_ALPHA = 0.3f; |
Mady Mellor | 60421c9 | 2017-03-29 15:27:37 -0700 | [diff] [blame] | 101 | private static final float DISMISS_BACKGROUND_ALPHA = 0.6f; |
Mady Mellor | 81d4061 | 2017-03-10 15:14:10 -0800 | [diff] [blame] | 102 | |
Winson Chung | 6b88baf | 2017-03-16 17:10:21 -0700 | [diff] [blame] | 103 | private static final float DISABLED_ACTION_ALPHA = 0.54f; |
| 104 | |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 105 | private int mMenuState; |
Ben Lin | 559ea4d | 2019-11-06 12:36:41 -0800 | [diff] [blame] | 106 | private boolean mResize = true; |
Winson Chung | 379b9ab | 2017-04-04 11:57:36 -0700 | [diff] [blame] | 107 | private boolean mAllowMenuTimeout = true; |
Winson Chung | 3448824 | 2017-04-26 15:53:51 -0700 | [diff] [blame] | 108 | private boolean mAllowTouches = true; |
Winson Chung | 87e5d55 | 2017-04-05 11:49:38 -0700 | [diff] [blame] | 109 | |
Winson Chung | c75ffe8 | 2016-12-16 16:20:16 -0800 | [diff] [blame] | 110 | private final List<RemoteAction> mActions = new ArrayList<>(); |
Winson Chung | 87e5d55 | 2017-04-05 11:49:38 -0700 | [diff] [blame] | 111 | |
Rhed Jao | 5e00c98 | 2018-11-27 19:41:05 +0800 | [diff] [blame] | 112 | private AccessibilityManager mAccessibilityManager; |
Mady Mellor | 81d4061 | 2017-03-10 15:14:10 -0800 | [diff] [blame] | 113 | private View mViewRoot; |
| 114 | private Drawable mBackgroundDrawable; |
Winson Chung | c75ffe8 | 2016-12-16 16:20:16 -0800 | [diff] [blame] | 115 | private View mMenuContainer; |
Mady Mellor | a7f6974 | 2017-02-03 11:00:20 -0800 | [diff] [blame] | 116 | private LinearLayout mActionsGroup; |
Winson Chung | a556fe6 | 2017-12-19 14:46:23 -0800 | [diff] [blame] | 117 | private View mSettingsButton; |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 118 | private View mDismissButton; |
Mady Mellor | a7f6974 | 2017-02-03 11:00:20 -0800 | [diff] [blame] | 119 | private int mBetweenActionPaddingLand; |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 120 | |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 121 | private AnimatorSet mMenuContainerAnimator; |
| 122 | |
Mady Mellor | 81d4061 | 2017-03-10 15:14:10 -0800 | [diff] [blame] | 123 | private ValueAnimator.AnimatorUpdateListener mMenuBgUpdateListener = |
| 124 | new ValueAnimator.AnimatorUpdateListener() { |
| 125 | @Override |
| 126 | public void onAnimationUpdate(ValueAnimator animation) { |
| 127 | final float alpha = (float) animation.getAnimatedValue(); |
| 128 | mBackgroundDrawable.setAlpha((int) (MENU_BACKGROUND_ALPHA*alpha*255)); |
| 129 | } |
| 130 | }; |
Winson Chung | c75ffe8 | 2016-12-16 16:20:16 -0800 | [diff] [blame] | 131 | |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 132 | private Handler mHandler = new Handler(); |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 133 | private Messenger mToControllerMessenger; |
| 134 | private Messenger mMessenger = new Messenger(new Handler() { |
| 135 | @Override |
| 136 | public void handleMessage(Message msg) { |
| 137 | switch (msg.what) { |
Winson Chung | 853c99a | 2017-03-21 22:16:42 -0700 | [diff] [blame] | 138 | case MESSAGE_SHOW_MENU: { |
| 139 | final Bundle data = (Bundle) msg.obj; |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 140 | showMenu(data.getInt(EXTRA_MENU_STATE), |
| 141 | data.getParcelable(EXTRA_STACK_BOUNDS), |
Winson Chung | 0f873de | 2017-03-30 10:26:48 -0700 | [diff] [blame] | 142 | data.getParcelable(EXTRA_MOVEMENT_BOUNDS), |
Winson Chung | bb78744 | 2017-09-01 11:33:47 -0700 | [diff] [blame] | 143 | data.getBoolean(EXTRA_ALLOW_TIMEOUT), |
| 144 | data.getBoolean(EXTRA_WILL_RESIZE_MENU)); |
Winson Chung | c75ffe8 | 2016-12-16 16:20:16 -0800 | [diff] [blame] | 145 | break; |
Winson Chung | 853c99a | 2017-03-21 22:16:42 -0700 | [diff] [blame] | 146 | } |
Mady Mellor | a7f6974 | 2017-02-03 11:00:20 -0800 | [diff] [blame] | 147 | case MESSAGE_POKE_MENU: |
| 148 | cancelDelayedFinish(); |
| 149 | break; |
Winson Chung | c75ffe8 | 2016-12-16 16:20:16 -0800 | [diff] [blame] | 150 | case MESSAGE_HIDE_MENU: |
Winson Chung | 2dbcf09 | 2018-10-24 13:00:41 -0700 | [diff] [blame] | 151 | hideMenu((Runnable) msg.obj); |
Winson Chung | c75ffe8 | 2016-12-16 16:20:16 -0800 | [diff] [blame] | 152 | break; |
Winson Chung | 853c99a | 2017-03-21 22:16:42 -0700 | [diff] [blame] | 153 | case MESSAGE_UPDATE_ACTIONS: { |
| 154 | final Bundle data = (Bundle) msg.obj; |
Winson Chung | 26db397 | 2017-03-23 15:17:01 -0700 | [diff] [blame] | 155 | final ParceledListSlice actions = data.getParcelable(EXTRA_ACTIONS); |
| 156 | setActions(data.getParcelable(EXTRA_STACK_BOUNDS), actions != null |
| 157 | ? actions.getList() : Collections.EMPTY_LIST); |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 158 | break; |
Winson Chung | 853c99a | 2017-03-21 22:16:42 -0700 | [diff] [blame] | 159 | } |
| 160 | case MESSAGE_UPDATE_DISMISS_FRACTION: { |
| 161 | final Bundle data = (Bundle) msg.obj; |
| 162 | updateDismissFraction(data.getFloat(EXTRA_DISMISS_FRACTION)); |
Mady Mellor | 81d4061 | 2017-03-10 15:14:10 -0800 | [diff] [blame] | 163 | break; |
Winson Chung | 853c99a | 2017-03-21 22:16:42 -0700 | [diff] [blame] | 164 | } |
Winson Chung | 3448824 | 2017-04-26 15:53:51 -0700 | [diff] [blame] | 165 | case MESSAGE_ANIMATION_ENDED: { |
| 166 | mAllowTouches = true; |
| 167 | break; |
| 168 | } |
Arthur Hung | d52d119 | 2019-08-30 16:06:59 +0800 | [diff] [blame] | 169 | |
Arthur Hung | d896197 | 2019-10-14 17:00:17 +0800 | [diff] [blame] | 170 | case MESSAGE_POINTER_EVENT: { |
Arthur Hung | d52d119 | 2019-08-30 16:06:59 +0800 | [diff] [blame] | 171 | final MotionEvent ev = (MotionEvent) msg.obj; |
Arthur Hung | d896197 | 2019-10-14 17:00:17 +0800 | [diff] [blame] | 172 | dispatchPointerEvent(ev); |
Arthur Hung | d52d119 | 2019-08-30 16:06:59 +0800 | [diff] [blame] | 173 | break; |
| 174 | } |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | }); |
| 178 | |
| 179 | private final Runnable mFinishRunnable = new Runnable() { |
| 180 | @Override |
| 181 | public void run() { |
Winson Chung | c75ffe8 | 2016-12-16 16:20:16 -0800 | [diff] [blame] | 182 | hideMenu(); |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 183 | } |
| 184 | }; |
| 185 | |
| 186 | @Override |
| 187 | protected void onCreate(@Nullable Bundle savedInstanceState) { |
Winson Chung | c75ffe8 | 2016-12-16 16:20:16 -0800 | [diff] [blame] | 188 | // Set the flags to allow us to watch for outside touches and also hide the menu and start |
| 189 | // manipulating the PIP in the same touch gesture |
Arthur Hung | d52d119 | 2019-08-30 16:06:59 +0800 | [diff] [blame] | 190 | getWindow().addFlags(LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH); |
Winson Chung | c75ffe8 | 2016-12-16 16:20:16 -0800 | [diff] [blame] | 191 | |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 192 | super.onCreate(savedInstanceState); |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 193 | setContentView(R.layout.pip_menu_activity); |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 194 | |
Rhed Jao | 5e00c98 | 2018-11-27 19:41:05 +0800 | [diff] [blame] | 195 | mAccessibilityManager = getSystemService(AccessibilityManager.class); |
Mady Mellor | 81d4061 | 2017-03-10 15:14:10 -0800 | [diff] [blame] | 196 | mBackgroundDrawable = new ColorDrawable(Color.BLACK); |
| 197 | mBackgroundDrawable.setAlpha(0); |
| 198 | mViewRoot = findViewById(R.id.background); |
| 199 | mViewRoot.setBackground(mBackgroundDrawable); |
| 200 | mMenuContainer = findViewById(R.id.menu_container); |
| 201 | mMenuContainer.setAlpha(0); |
Winson Chung | a556fe6 | 2017-12-19 14:46:23 -0800 | [diff] [blame] | 202 | mSettingsButton = findViewById(R.id.settings); |
| 203 | mSettingsButton.setAlpha(0); |
| 204 | mSettingsButton.setOnClickListener((v) -> { |
Tracy Zhou | 2c6117a | 2018-06-21 11:32:43 -0700 | [diff] [blame] | 205 | if (v.getAlpha() != 0) { |
| 206 | showSettings(); |
| 207 | } |
Winson Chung | a556fe6 | 2017-12-19 14:46:23 -0800 | [diff] [blame] | 208 | }); |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 209 | mDismissButton = findViewById(R.id.dismiss); |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 210 | mDismissButton.setAlpha(0); |
Hongwei Wang | 1064950 | 2019-06-26 13:39:24 -0700 | [diff] [blame] | 211 | mDismissButton.setOnClickListener(v -> dismissPip()); |
Arthur Hung | d52d119 | 2019-08-30 16:06:59 +0800 | [diff] [blame] | 212 | findViewById(R.id.expand_button).setOnClickListener(v -> { |
| 213 | if (mMenuContainer.getAlpha() != 0) { |
| 214 | expandPip(); |
| 215 | } |
| 216 | }); |
Alan Viverette | 51efddb | 2017-04-05 10:00:01 -0400 | [diff] [blame] | 217 | mActionsGroup = findViewById(R.id.actions_group); |
Mady Mellor | a7f6974 | 2017-02-03 11:00:20 -0800 | [diff] [blame] | 218 | mBetweenActionPaddingLand = getResources().getDimensionPixelSize( |
| 219 | R.dimen.pip_between_action_padding_land); |
Winson Chung | c75ffe8 | 2016-12-16 16:20:16 -0800 | [diff] [blame] | 220 | |
Winson Chung | e7a3d22 | 2017-03-09 13:26:45 -0800 | [diff] [blame] | 221 | updateFromIntent(getIntent()); |
Phil Weaver | f00cd14 | 2017-03-03 13:44:00 -0800 | [diff] [blame] | 222 | setTitle(R.string.pip_menu_title); |
Wale Ogunwale | 5e8ecb0 | 2017-04-14 13:15:55 -0700 | [diff] [blame] | 223 | setDisablePreviewScreenshots(true); |
Arthur Hung | d896197 | 2019-10-14 17:00:17 +0800 | [diff] [blame] | 224 | |
| 225 | // Hide without an animation. |
| 226 | getWindow().setExitTransition(null); |
Winson Chung | c75ffe8 | 2016-12-16 16:20:16 -0800 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | @Override |
Ben Lin | 8e90d0a | 2020-02-26 14:57:14 -0800 | [diff] [blame] | 230 | public boolean onKeyUp(int keyCode, KeyEvent event) { |
| 231 | if (keyCode == KeyEvent.KEYCODE_ESCAPE) { |
| 232 | hideMenu(); |
| 233 | return true; |
| 234 | } |
| 235 | return super.onKeyUp(keyCode, event); |
| 236 | } |
| 237 | |
| 238 | @Override |
Winson Chung | c75ffe8 | 2016-12-16 16:20:16 -0800 | [diff] [blame] | 239 | protected void onNewIntent(Intent intent) { |
| 240 | super.onNewIntent(intent); |
Winson Chung | e7a3d22 | 2017-03-09 13:26:45 -0800 | [diff] [blame] | 241 | updateFromIntent(intent); |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | @Override |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 245 | public void onUserInteraction() { |
Winson Chung | 379b9ab | 2017-04-04 11:57:36 -0700 | [diff] [blame] | 246 | if (mAllowMenuTimeout) { |
| 247 | repostDelayedFinish(POST_INTERACTION_DISMISS_DELAY); |
| 248 | } |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | @Override |
Winson Chung | 61ecc1b | 2017-02-17 10:46:17 -0800 | [diff] [blame] | 252 | protected void onUserLeaveHint() { |
| 253 | super.onUserLeaveHint(); |
| 254 | |
Winson Chung | 9f69b78 | 2017-05-16 15:27:49 -0700 | [diff] [blame] | 255 | // If another task is starting on top of the menu, then hide and finish it so that it can be |
Winson Chung | 61ecc1b | 2017-02-17 10:46:17 -0800 | [diff] [blame] | 256 | // recreated on the top next time it starts |
Winson Chung | 9f69b78 | 2017-05-16 15:27:49 -0700 | [diff] [blame] | 257 | hideMenu(); |
Winson Chung | 61ecc1b | 2017-02-17 10:46:17 -0800 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | @Override |
Winson Chung | ef4dc81 | 2017-04-11 13:31:44 -0700 | [diff] [blame] | 261 | protected void onStop() { |
| 262 | super.onStop(); |
| 263 | |
Hongwei Wang | 9cdf452 | 2019-09-26 10:42:38 -0700 | [diff] [blame] | 264 | // In cases such as device lock, hide and finish it so that it can be recreated on the top |
| 265 | // next time it starts, see also {@link #onUserLeaveHint} |
| 266 | hideMenu(); |
Winson Chung | ef4dc81 | 2017-04-11 13:31:44 -0700 | [diff] [blame] | 267 | cancelDelayedFinish(); |
| 268 | } |
| 269 | |
| 270 | @Override |
Winson Chung | 61ecc1b | 2017-02-17 10:46:17 -0800 | [diff] [blame] | 271 | protected void onDestroy() { |
| 272 | super.onDestroy(); |
| 273 | |
| 274 | // Fallback, if we are destroyed for any other reason (like when the task is being reset), |
| 275 | // also reset the callback. |
| 276 | notifyActivityCallback(null); |
| 277 | } |
| 278 | |
| 279 | @Override |
Winson Chung | c75ffe8 | 2016-12-16 16:20:16 -0800 | [diff] [blame] | 280 | public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode) { |
| 281 | if (!isInPictureInPictureMode) { |
| 282 | finish(); |
| 283 | } |
| 284 | } |
| 285 | |
Arthur Hung | d896197 | 2019-10-14 17:00:17 +0800 | [diff] [blame] | 286 | /** |
| 287 | * Dispatch a pointer event from {@link PipTouchHandler}. |
| 288 | */ |
| 289 | private void dispatchPointerEvent(MotionEvent event) { |
| 290 | if (event.isTouchEvent()) { |
| 291 | dispatchTouchEvent(event); |
| 292 | } else { |
| 293 | dispatchGenericMotionEvent(event); |
| 294 | } |
| 295 | } |
| 296 | |
Winson Chung | c75ffe8 | 2016-12-16 16:20:16 -0800 | [diff] [blame] | 297 | @Override |
Winson Chung | c75ffe8 | 2016-12-16 16:20:16 -0800 | [diff] [blame] | 298 | public boolean dispatchTouchEvent(MotionEvent ev) { |
Winson Chung | 3448824 | 2017-04-26 15:53:51 -0700 | [diff] [blame] | 299 | if (!mAllowTouches) { |
Arthur Hung | d52d119 | 2019-08-30 16:06:59 +0800 | [diff] [blame] | 300 | return false; |
Winson Chung | 3448824 | 2017-04-26 15:53:51 -0700 | [diff] [blame] | 301 | } |
| 302 | |
Winson Chung | c75ffe8 | 2016-12-16 16:20:16 -0800 | [diff] [blame] | 303 | // On the first action outside the window, hide the menu |
| 304 | switch (ev.getAction()) { |
| 305 | case MotionEvent.ACTION_OUTSIDE: |
| 306 | hideMenu(); |
Arthur Hung | d52d119 | 2019-08-30 16:06:59 +0800 | [diff] [blame] | 307 | return true; |
Winson Chung | c75ffe8 | 2016-12-16 16:20:16 -0800 | [diff] [blame] | 308 | } |
| 309 | return super.dispatchTouchEvent(ev); |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | @Override |
| 313 | public void finish() { |
Winson Chung | c75ffe8 | 2016-12-16 16:20:16 -0800 | [diff] [blame] | 314 | notifyActivityCallback(null); |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 315 | super.finish(); |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | @Override |
| 319 | public void setTaskDescription(ActivityManager.TaskDescription taskDescription) { |
| 320 | // Do nothing |
| 321 | } |
| 322 | |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 323 | private void showMenu(int menuState, Rect stackBounds, Rect movementBounds, |
Winson Chung | bb78744 | 2017-09-01 11:33:47 -0700 | [diff] [blame] | 324 | boolean allowMenuTimeout, boolean resizeMenuOnShow) { |
Winson Chung | 379b9ab | 2017-04-04 11:57:36 -0700 | [diff] [blame] | 325 | mAllowMenuTimeout = allowMenuTimeout; |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 326 | if (mMenuState != menuState) { |
Winson Chung | bb78744 | 2017-09-01 11:33:47 -0700 | [diff] [blame] | 327 | // Disallow touches if the menu needs to resize while showing, and we are transitioning |
| 328 | // to/from a full menu state. |
| 329 | boolean disallowTouchesUntilAnimationEnd = resizeMenuOnShow && |
| 330 | (mMenuState == MENU_STATE_FULL || menuState == MENU_STATE_FULL); |
| 331 | mAllowTouches = !disallowTouchesUntilAnimationEnd; |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 332 | cancelDelayedFinish(); |
Mady Mellor | 81d4061 | 2017-03-10 15:14:10 -0800 | [diff] [blame] | 333 | updateActionViews(stackBounds); |
Winson Chung | c75ffe8 | 2016-12-16 16:20:16 -0800 | [diff] [blame] | 334 | if (mMenuContainerAnimator != null) { |
| 335 | mMenuContainerAnimator.cancel(); |
| 336 | } |
Ben Lin | 559ea4d | 2019-11-06 12:36:41 -0800 | [diff] [blame] | 337 | notifyMenuStateChange(menuState, resizeMenuOnShow); |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 338 | mMenuContainerAnimator = new AnimatorSet(); |
| 339 | ObjectAnimator menuAnim = ObjectAnimator.ofFloat(mMenuContainer, View.ALPHA, |
Winson Chung | c75ffe8 | 2016-12-16 16:20:16 -0800 | [diff] [blame] | 340 | mMenuContainer.getAlpha(), 1f); |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 341 | menuAnim.addUpdateListener(mMenuBgUpdateListener); |
Winson Chung | a556fe6 | 2017-12-19 14:46:23 -0800 | [diff] [blame] | 342 | ObjectAnimator settingsAnim = ObjectAnimator.ofFloat(mSettingsButton, View.ALPHA, |
| 343 | mSettingsButton.getAlpha(), 1f); |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 344 | ObjectAnimator dismissAnim = ObjectAnimator.ofFloat(mDismissButton, View.ALPHA, |
| 345 | mDismissButton.getAlpha(), 1f); |
| 346 | if (menuState == MENU_STATE_FULL) { |
Winson Chung | a556fe6 | 2017-12-19 14:46:23 -0800 | [diff] [blame] | 347 | mMenuContainerAnimator.playTogether(menuAnim, settingsAnim, dismissAnim); |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 348 | } else { |
Winson Chung | 2a35e6d | 2018-01-13 14:27:50 -0800 | [diff] [blame] | 349 | mMenuContainerAnimator.playTogether(dismissAnim); |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 350 | } |
Winson Chung | c75ffe8 | 2016-12-16 16:20:16 -0800 | [diff] [blame] | 351 | mMenuContainerAnimator.setInterpolator(Interpolators.ALPHA_IN); |
| 352 | mMenuContainerAnimator.setDuration(MENU_FADE_DURATION); |
Winson Chung | 0f873de | 2017-03-30 10:26:48 -0700 | [diff] [blame] | 353 | if (allowMenuTimeout) { |
| 354 | mMenuContainerAnimator.addListener(new AnimatorListenerAdapter() { |
| 355 | @Override |
| 356 | public void onAnimationEnd(Animator animation) { |
| 357 | repostDelayedFinish(INITIAL_DISMISS_DELAY); |
| 358 | } |
| 359 | }); |
| 360 | } |
Winson Chung | c75ffe8 | 2016-12-16 16:20:16 -0800 | [diff] [blame] | 361 | mMenuContainerAnimator.start(); |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 362 | } else { |
Winson Chung | 2824d7c | 2017-03-15 19:43:00 -0700 | [diff] [blame] | 363 | // If we are already visible, then just start the delayed dismiss and unregister any |
| 364 | // existing input consumers from the previous drag |
Winson Chung | 0f873de | 2017-03-30 10:26:48 -0700 | [diff] [blame] | 365 | if (allowMenuTimeout) { |
| 366 | repostDelayedFinish(POST_INTERACTION_DISMISS_DELAY); |
| 367 | } |
Winson Chung | c75ffe8 | 2016-12-16 16:20:16 -0800 | [diff] [blame] | 368 | } |
| 369 | } |
| 370 | |
| 371 | private void hideMenu() { |
Winson Chung | 2dbcf09 | 2018-10-24 13:00:41 -0700 | [diff] [blame] | 372 | hideMenu(null); |
| 373 | } |
| 374 | |
| 375 | private void hideMenu(Runnable animationEndCallback) { |
| 376 | hideMenu(animationEndCallback, true /* notifyMenuVisibility */, false /* isDismissing */); |
Winson Chung | c75ffe8 | 2016-12-16 16:20:16 -0800 | [diff] [blame] | 377 | } |
| 378 | |
Winson Chung | 21004d5 | 2018-06-04 17:09:32 -0700 | [diff] [blame] | 379 | private void hideMenu(final Runnable animationFinishedRunnable, boolean notifyMenuVisibility, |
| 380 | boolean isDismissing) { |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 381 | if (mMenuState != MENU_STATE_NONE) { |
Winson Chung | c75ffe8 | 2016-12-16 16:20:16 -0800 | [diff] [blame] | 382 | cancelDelayedFinish(); |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 383 | if (notifyMenuVisibility) { |
Ben Lin | 559ea4d | 2019-11-06 12:36:41 -0800 | [diff] [blame] | 384 | notifyMenuStateChange(MENU_STATE_NONE, mResize); |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 385 | } |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 386 | mMenuContainerAnimator = new AnimatorSet(); |
| 387 | ObjectAnimator menuAnim = ObjectAnimator.ofFloat(mMenuContainer, View.ALPHA, |
Winson Chung | c75ffe8 | 2016-12-16 16:20:16 -0800 | [diff] [blame] | 388 | mMenuContainer.getAlpha(), 0f); |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 389 | menuAnim.addUpdateListener(mMenuBgUpdateListener); |
Winson Chung | a556fe6 | 2017-12-19 14:46:23 -0800 | [diff] [blame] | 390 | ObjectAnimator settingsAnim = ObjectAnimator.ofFloat(mSettingsButton, View.ALPHA, |
| 391 | mSettingsButton.getAlpha(), 0f); |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 392 | ObjectAnimator dismissAnim = ObjectAnimator.ofFloat(mDismissButton, View.ALPHA, |
| 393 | mDismissButton.getAlpha(), 0f); |
Winson Chung | a556fe6 | 2017-12-19 14:46:23 -0800 | [diff] [blame] | 394 | mMenuContainerAnimator.playTogether(menuAnim, settingsAnim, dismissAnim); |
Winson Chung | c75ffe8 | 2016-12-16 16:20:16 -0800 | [diff] [blame] | 395 | mMenuContainerAnimator.setInterpolator(Interpolators.ALPHA_OUT); |
| 396 | mMenuContainerAnimator.setDuration(MENU_FADE_DURATION); |
| 397 | mMenuContainerAnimator.addListener(new AnimatorListenerAdapter() { |
| 398 | @Override |
| 399 | public void onAnimationEnd(Animator animation) { |
| 400 | if (animationFinishedRunnable != null) { |
| 401 | animationFinishedRunnable.run(); |
| 402 | } |
Winson Chung | 21004d5 | 2018-06-04 17:09:32 -0700 | [diff] [blame] | 403 | |
| 404 | if (!isDismissing) { |
| 405 | // If we are dismissing the PiP, then don't try to pre-emptively finish the |
| 406 | // menu activity |
| 407 | finish(); |
| 408 | } |
Winson Chung | c75ffe8 | 2016-12-16 16:20:16 -0800 | [diff] [blame] | 409 | } |
| 410 | }); |
| 411 | mMenuContainerAnimator.start(); |
Winson Chung | 87e5d55 | 2017-04-05 11:49:38 -0700 | [diff] [blame] | 412 | } else { |
| 413 | // If the menu is not visible, just finish now |
| 414 | finish(); |
Winson Chung | c75ffe8 | 2016-12-16 16:20:16 -0800 | [diff] [blame] | 415 | } |
| 416 | } |
| 417 | |
Winson Chung | e7a3d22 | 2017-03-09 13:26:45 -0800 | [diff] [blame] | 418 | private void updateFromIntent(Intent intent) { |
Winson Chung | e7a3d22 | 2017-03-09 13:26:45 -0800 | [diff] [blame] | 419 | mToControllerMessenger = intent.getParcelableExtra(EXTRA_CONTROLLER_MESSENGER); |
Jaewan Kim | c1d1524 | 2017-07-07 18:12:04 +0900 | [diff] [blame] | 420 | if (mToControllerMessenger == null) { |
| 421 | Log.w(TAG, "Controller messenger is null. Stopping."); |
| 422 | finish(); |
| 423 | return; |
| 424 | } |
Winson Chung | ccbc4b9 | 2017-03-20 14:01:36 -0700 | [diff] [blame] | 425 | notifyActivityCallback(mMessenger); |
Winson Chung | b502690 | 2017-05-03 12:45:13 -0700 | [diff] [blame] | 426 | |
Winson Chung | e7a3d22 | 2017-03-09 13:26:45 -0800 | [diff] [blame] | 427 | ParceledListSlice actions = intent.getParcelableExtra(EXTRA_ACTIONS); |
| 428 | if (actions != null) { |
Mady Mellor | 81d4061 | 2017-03-10 15:14:10 -0800 | [diff] [blame] | 429 | mActions.clear(); |
| 430 | mActions.addAll(actions.getList()); |
Winson Chung | e7a3d22 | 2017-03-09 13:26:45 -0800 | [diff] [blame] | 431 | } |
Winson Chung | b502690 | 2017-05-03 12:45:13 -0700 | [diff] [blame] | 432 | |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 433 | final int menuState = intent.getIntExtra(EXTRA_MENU_STATE, MENU_STATE_NONE); |
| 434 | if (menuState != MENU_STATE_NONE) { |
Winson Chung | 853c99a | 2017-03-21 22:16:42 -0700 | [diff] [blame] | 435 | Rect stackBounds = intent.getParcelableExtra(EXTRA_STACK_BOUNDS); |
| 436 | Rect movementBounds = intent.getParcelableExtra(EXTRA_MOVEMENT_BOUNDS); |
Winson Chung | 0f873de | 2017-03-30 10:26:48 -0700 | [diff] [blame] | 437 | boolean allowMenuTimeout = intent.getBooleanExtra(EXTRA_ALLOW_TIMEOUT, true); |
Winson Chung | bb78744 | 2017-09-01 11:33:47 -0700 | [diff] [blame] | 438 | boolean willResizeMenu = intent.getBooleanExtra(EXTRA_WILL_RESIZE_MENU, false); |
| 439 | showMenu(menuState, stackBounds, movementBounds, allowMenuTimeout, willResizeMenu); |
Mady Mellor | 81d4061 | 2017-03-10 15:14:10 -0800 | [diff] [blame] | 440 | } |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 441 | } |
| 442 | |
Winson Chung | e7a3d22 | 2017-03-09 13:26:45 -0800 | [diff] [blame] | 443 | private void setActions(Rect stackBounds, List<RemoteAction> actions) { |
| 444 | mActions.clear(); |
| 445 | mActions.addAll(actions); |
| 446 | updateActionViews(stackBounds); |
| 447 | } |
| 448 | |
| 449 | private void updateActionViews(Rect stackBounds) { |
Alan Viverette | 51efddb | 2017-04-05 10:00:01 -0400 | [diff] [blame] | 450 | ViewGroup expandContainer = findViewById(R.id.expand_container); |
| 451 | ViewGroup actionsContainer = findViewById(R.id.actions_container); |
Winson Chung | a5acf18 | 2017-01-05 16:02:27 -0800 | [diff] [blame] | 452 | actionsContainer.setOnTouchListener((v, ev) -> { |
| 453 | // Do nothing, prevent click through to parent |
| 454 | return true; |
| 455 | }); |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 456 | |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 457 | if (mActions.isEmpty() || mMenuState == MENU_STATE_CLOSE) { |
Winson Chung | a5acf18 | 2017-01-05 16:02:27 -0800 | [diff] [blame] | 458 | actionsContainer.setVisibility(View.INVISIBLE); |
| 459 | } else { |
| 460 | actionsContainer.setVisibility(View.VISIBLE); |
Mady Mellor | a7f6974 | 2017-02-03 11:00:20 -0800 | [diff] [blame] | 461 | if (mActionsGroup != null) { |
Winson Chung | b6de872 | 2017-06-02 12:45:51 -0700 | [diff] [blame] | 462 | // Ensure we have as many buttons as actions |
Winson Chung | 7aab0c1 | 2017-04-18 12:54:25 -0700 | [diff] [blame] | 463 | final LayoutInflater inflater = LayoutInflater.from(this); |
| 464 | while (mActionsGroup.getChildCount() < mActions.size()) { |
Hongwei Wang | a6e034e | 2019-06-20 13:18:43 -0700 | [diff] [blame] | 465 | final ImageButton actionView = (ImageButton) inflater.inflate( |
Winson Chung | 7aab0c1 | 2017-04-18 12:54:25 -0700 | [diff] [blame] | 466 | R.layout.pip_menu_action, mActionsGroup, false); |
| 467 | mActionsGroup.addView(actionView); |
| 468 | } |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 469 | |
Winson Chung | b6de872 | 2017-06-02 12:45:51 -0700 | [diff] [blame] | 470 | // Update the visibility of all views |
| 471 | for (int i = 0; i < mActionsGroup.getChildCount(); i++) { |
| 472 | mActionsGroup.getChildAt(i).setVisibility(i < mActions.size() |
| 473 | ? View.VISIBLE |
| 474 | : View.GONE); |
| 475 | } |
| 476 | |
Winson Chung | a5acf18 | 2017-01-05 16:02:27 -0800 | [diff] [blame] | 477 | // Recreate the layout |
Winson Chung | e7a3d22 | 2017-03-09 13:26:45 -0800 | [diff] [blame] | 478 | final boolean isLandscapePip = stackBounds != null && |
| 479 | (stackBounds.width() > stackBounds.height()); |
Winson Chung | a5acf18 | 2017-01-05 16:02:27 -0800 | [diff] [blame] | 480 | for (int i = 0; i < mActions.size(); i++) { |
| 481 | final RemoteAction action = mActions.get(i); |
Hongwei Wang | a6e034e | 2019-06-20 13:18:43 -0700 | [diff] [blame] | 482 | final ImageButton actionView = (ImageButton) mActionsGroup.getChildAt(i); |
Winson Chung | 7aab0c1 | 2017-04-18 12:54:25 -0700 | [diff] [blame] | 483 | |
| 484 | // TODO: Check if the action drawable has changed before we reload it |
Winson Chung | a5acf18 | 2017-01-05 16:02:27 -0800 | [diff] [blame] | 485 | action.getIcon().loadDrawableAsync(this, d -> { |
Winson Chung | 56fe883 | 2017-02-03 14:37:53 -0800 | [diff] [blame] | 486 | d.setTint(Color.WHITE); |
Winson Chung | a5acf18 | 2017-01-05 16:02:27 -0800 | [diff] [blame] | 487 | actionView.setImageDrawable(d); |
| 488 | }, mHandler); |
| 489 | actionView.setContentDescription(action.getContentDescription()); |
Winson Chung | 6b88baf | 2017-03-16 17:10:21 -0700 | [diff] [blame] | 490 | if (action.isEnabled()) { |
| 491 | actionView.setOnClickListener(v -> { |
Arthur Hung | d52d119 | 2019-08-30 16:06:59 +0800 | [diff] [blame] | 492 | mHandler.post(() -> { |
| 493 | try { |
| 494 | action.getActionIntent().send(); |
| 495 | } catch (CanceledException e) { |
| 496 | Log.w(TAG, "Failed to send action", e); |
| 497 | } |
| 498 | }); |
Winson Chung | 6b88baf | 2017-03-16 17:10:21 -0700 | [diff] [blame] | 499 | }); |
Winson Chung | 6b88baf | 2017-03-16 17:10:21 -0700 | [diff] [blame] | 500 | } |
Winson Chung | d61a19e | 2017-04-28 16:36:51 -0700 | [diff] [blame] | 501 | actionView.setEnabled(action.isEnabled()); |
Winson Chung | b6de872 | 2017-06-02 12:45:51 -0700 | [diff] [blame] | 502 | actionView.setAlpha(action.isEnabled() ? 1f : DISABLED_ACTION_ALPHA); |
Winson Chung | 7aab0c1 | 2017-04-18 12:54:25 -0700 | [diff] [blame] | 503 | |
| 504 | // Update the margin between actions |
| 505 | LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) |
| 506 | actionView.getLayoutParams(); |
| 507 | lp.leftMargin = (isLandscapePip && i > 0) ? mBetweenActionPaddingLand : 0; |
Winson Chung | a5acf18 | 2017-01-05 16:02:27 -0800 | [diff] [blame] | 508 | } |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 509 | } |
Mady Mellor | a7f6974 | 2017-02-03 11:00:20 -0800 | [diff] [blame] | 510 | |
Winson Chung | e7a3d22 | 2017-03-09 13:26:45 -0800 | [diff] [blame] | 511 | // Update the expand container margin to adjust the center of the expand button to |
| 512 | // account for the existence of the action container |
| 513 | FrameLayout.LayoutParams expandedLp = |
| 514 | (FrameLayout.LayoutParams) expandContainer.getLayoutParams(); |
| 515 | expandedLp.topMargin = getResources().getDimensionPixelSize( |
| 516 | R.dimen.pip_action_padding); |
| 517 | expandedLp.bottomMargin = getResources().getDimensionPixelSize( |
| 518 | R.dimen.pip_expand_container_edge_margin); |
| 519 | expandContainer.requestLayout(); |
| 520 | } |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 521 | } |
| 522 | |
Mady Mellor | 81d4061 | 2017-03-10 15:14:10 -0800 | [diff] [blame] | 523 | private void updateDismissFraction(float fraction) { |
| 524 | int alpha; |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 525 | final float menuAlpha = 1 - fraction; |
| 526 | if (mMenuState == MENU_STATE_FULL) { |
| 527 | mMenuContainer.setAlpha(menuAlpha); |
Winson Chung | a556fe6 | 2017-12-19 14:46:23 -0800 | [diff] [blame] | 528 | mSettingsButton.setAlpha(menuAlpha); |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 529 | mDismissButton.setAlpha(menuAlpha); |
Mady Mellor | 81d4061 | 2017-03-10 15:14:10 -0800 | [diff] [blame] | 530 | final float interpolatedAlpha = |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 531 | MENU_BACKGROUND_ALPHA * menuAlpha + DISMISS_BACKGROUND_ALPHA * fraction; |
Winson Chung | c3545d3 | 2017-04-03 10:44:57 -0700 | [diff] [blame] | 532 | alpha = (int) (interpolatedAlpha * 255); |
Mady Mellor | 81d4061 | 2017-03-10 15:14:10 -0800 | [diff] [blame] | 533 | } else { |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 534 | if (mMenuState == MENU_STATE_CLOSE) { |
| 535 | mDismissButton.setAlpha(menuAlpha); |
| 536 | } |
Winson Chung | c3545d3 | 2017-04-03 10:44:57 -0700 | [diff] [blame] | 537 | alpha = (int) (fraction * DISMISS_BACKGROUND_ALPHA * 255); |
Mady Mellor | 81d4061 | 2017-03-10 15:14:10 -0800 | [diff] [blame] | 538 | } |
| 539 | mBackgroundDrawable.setAlpha(alpha); |
| 540 | } |
| 541 | |
Ben Lin | 559ea4d | 2019-11-06 12:36:41 -0800 | [diff] [blame] | 542 | private void notifyMenuStateChange(int menuState, boolean resize) { |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 543 | mMenuState = menuState; |
Ben Lin | 559ea4d | 2019-11-06 12:36:41 -0800 | [diff] [blame] | 544 | mResize = resize; |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 545 | Message m = Message.obtain(); |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 546 | m.what = PipMenuActivityController.MESSAGE_MENU_STATE_CHANGED; |
| 547 | m.arg1 = menuState; |
Ben Lin | 559ea4d | 2019-11-06 12:36:41 -0800 | [diff] [blame] | 548 | m.arg2 = resize ? 1 : 0; |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 549 | sendMessage(m, "Could not notify controller of PIP menu visibility"); |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 550 | } |
| 551 | |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 552 | private void expandPip() { |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 553 | // Do not notify menu visibility when hiding the menu, the controller will do this when it |
| 554 | // handles the message |
Winson Chung | c75ffe8 | 2016-12-16 16:20:16 -0800 | [diff] [blame] | 555 | hideMenu(() -> { |
| 556 | sendEmptyMessage(PipMenuActivityController.MESSAGE_EXPAND_PIP, |
| 557 | "Could not notify controller to expand PIP"); |
Winson Chung | 21004d5 | 2018-06-04 17:09:32 -0700 | [diff] [blame] | 558 | }, false /* notifyMenuVisibility */, false /* isDismissing */); |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 559 | } |
| 560 | |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 561 | private void dismissPip() { |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 562 | // Do not notify menu visibility when hiding the menu, the controller will do this when it |
| 563 | // handles the message |
Winson Chung | c75ffe8 | 2016-12-16 16:20:16 -0800 | [diff] [blame] | 564 | hideMenu(() -> { |
| 565 | sendEmptyMessage(PipMenuActivityController.MESSAGE_DISMISS_PIP, |
| 566 | "Could not notify controller to dismiss PIP"); |
Winson Chung | 21004d5 | 2018-06-04 17:09:32 -0700 | [diff] [blame] | 567 | }, false /* notifyMenuVisibility */, true /* isDismissing */); |
Winson Chung | c75ffe8 | 2016-12-16 16:20:16 -0800 | [diff] [blame] | 568 | } |
| 569 | |
Winson Chung | a556fe6 | 2017-12-19 14:46:23 -0800 | [diff] [blame] | 570 | private void showSettings() { |
| 571 | final Pair<ComponentName, Integer> topPipActivityInfo = |
Winson Chung | be00a83 | 2020-03-10 11:08:35 -0700 | [diff] [blame] | 572 | PipUtils.getTopPipActivity(this, ActivityManager.getService()); |
Winson Chung | a556fe6 | 2017-12-19 14:46:23 -0800 | [diff] [blame] | 573 | if (topPipActivityInfo.first != null) { |
| 574 | final UserHandle user = UserHandle.of(topPipActivityInfo.second); |
| 575 | final Intent settingsIntent = new Intent(ACTION_PICTURE_IN_PICTURE_SETTINGS, |
| 576 | Uri.fromParts("package", topPipActivityInfo.first.getPackageName(), null)); |
| 577 | settingsIntent.putExtra(Intent.EXTRA_USER_HANDLE, user); |
| 578 | settingsIntent.setFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK); |
| 579 | startActivity(settingsIntent); |
| 580 | } |
| 581 | } |
| 582 | |
Winson Chung | c75ffe8 | 2016-12-16 16:20:16 -0800 | [diff] [blame] | 583 | private void notifyActivityCallback(Messenger callback) { |
| 584 | Message m = Message.obtain(); |
| 585 | m.what = PipMenuActivityController.MESSAGE_UPDATE_ACTIVITY_CALLBACK; |
| 586 | m.replyTo = callback; |
Ben Lin | 559ea4d | 2019-11-06 12:36:41 -0800 | [diff] [blame] | 587 | m.arg1 = mResize ? 1 : 0; |
Winson Chung | c75ffe8 | 2016-12-16 16:20:16 -0800 | [diff] [blame] | 588 | sendMessage(m, "Could not notify controller of activity finished"); |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | private void sendEmptyMessage(int what, String errorMsg) { |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 592 | Message m = Message.obtain(); |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 593 | m.what = what; |
| 594 | sendMessage(m, errorMsg); |
| 595 | } |
| 596 | |
| 597 | private void sendMessage(Message m, String errorMsg) { |
Jaewan Kim | c1d1524 | 2017-07-07 18:12:04 +0900 | [diff] [blame] | 598 | if (mToControllerMessenger == null) { |
| 599 | return; |
| 600 | } |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 601 | try { |
| 602 | mToControllerMessenger.send(m); |
| 603 | } catch (RemoteException e) { |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 604 | Log.e(TAG, errorMsg, e); |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 605 | } |
| 606 | } |
| 607 | |
Winson Chung | c75ffe8 | 2016-12-16 16:20:16 -0800 | [diff] [blame] | 608 | private void cancelDelayedFinish() { |
Winson Chung | 4a526c1 | 2017-05-16 13:35:43 -0700 | [diff] [blame] | 609 | mHandler.removeCallbacks(mFinishRunnable); |
Winson Chung | c75ffe8 | 2016-12-16 16:20:16 -0800 | [diff] [blame] | 610 | } |
| 611 | |
Rhed Jao | 5e00c98 | 2018-11-27 19:41:05 +0800 | [diff] [blame] | 612 | private void repostDelayedFinish(int delay) { |
| 613 | int recommendedTimeout = mAccessibilityManager.getRecommendedTimeoutMillis(delay, |
| 614 | FLAG_CONTENT_ICONS | FLAG_CONTENT_CONTROLS); |
Winson Chung | 4a526c1 | 2017-05-16 13:35:43 -0700 | [diff] [blame] | 615 | mHandler.removeCallbacks(mFinishRunnable); |
Rhed Jao | 5e00c98 | 2018-11-27 19:41:05 +0800 | [diff] [blame] | 616 | mHandler.postDelayed(mFinishRunnable, recommendedTimeout); |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 617 | } |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 618 | } |