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