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