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