Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [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 | |
Winson | bf8c2c0 | 2016-10-18 18:56:24 -0700 | [diff] [blame] | 17 | package com.android.systemui.pip.tv; |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 18 | |
Winson Chung | 67f5c8b | 2018-09-24 12:09:19 -0700 | [diff] [blame] | 19 | import static android.app.ActivityTaskManager.INVALID_STACK_ID; |
| 20 | import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED; |
| 21 | import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED; |
Winson Chung | 67f5c8b | 2018-09-24 12:09:19 -0700 | [diff] [blame] | 22 | |
Winson | c0d7058 | 2016-01-29 10:24:39 -0800 | [diff] [blame] | 23 | import android.app.ActivityManager.RunningTaskInfo; |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 24 | import android.app.ActivityManager.StackInfo; |
Wale Ogunwale | 65ebd95 | 2018-04-25 15:41:44 -0700 | [diff] [blame] | 25 | import android.app.ActivityTaskManager; |
Wale Ogunwale | 65ebd95 | 2018-04-25 15:41:44 -0700 | [diff] [blame] | 26 | import android.app.IActivityTaskManager; |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 27 | import android.content.BroadcastReceiver; |
Jaewan Kim | 6233819 | 2016-02-25 10:00:05 -0800 | [diff] [blame] | 28 | import android.content.ComponentName; |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 29 | import android.content.Context; |
| 30 | import android.content.Intent; |
| 31 | import android.content.IntentFilter; |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 32 | import android.content.pm.ParceledListSlice; |
Winson Chung | 0c5a592 | 2017-05-22 17:41:06 -0700 | [diff] [blame] | 33 | import android.content.res.Configuration; |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 34 | import android.content.res.Resources; |
| 35 | import android.graphics.Rect; |
Jaewan Kim | 6233819 | 2016-02-25 10:00:05 -0800 | [diff] [blame] | 36 | import android.media.session.MediaController; |
| 37 | import android.media.session.MediaSessionManager; |
Jaewan Kim | 8f584b8 | 2016-03-22 22:16:59 +0900 | [diff] [blame] | 38 | import android.media.session.PlaybackState; |
Wale Ogunwale | 480dca0 | 2016-02-06 13:58:29 -0800 | [diff] [blame] | 39 | import android.os.Debug; |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 40 | import android.os.Handler; |
| 41 | import android.os.RemoteException; |
Kyeongkab.Nam | d61de15 | 2018-10-09 14:55:42 +0900 | [diff] [blame] | 42 | import android.os.UserHandle; |
Dongwon Kang | 353d8d7 | 2016-04-13 21:55:22 -0700 | [diff] [blame] | 43 | import android.text.TextUtils; |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 44 | import android.util.Log; |
Jaewan Kim | d89a694 | 2016-04-08 11:41:45 +0900 | [diff] [blame] | 45 | import android.util.Pair; |
Hongwei Wang | 43a752b | 2019-09-17 20:20:30 +0000 | [diff] [blame] | 46 | import android.view.DisplayInfo; |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 47 | |
Winson Chung | 67f5c8b | 2018-09-24 12:09:19 -0700 | [diff] [blame] | 48 | import com.android.systemui.Dependency; |
Jaewan Kim | 8f584b8 | 2016-03-22 22:16:59 +0900 | [diff] [blame] | 49 | import com.android.systemui.R; |
Winson Chung | 67f5c8b | 2018-09-24 12:09:19 -0700 | [diff] [blame] | 50 | import com.android.systemui.UiOffloadThread; |
Winson Chung | 29a7865 | 2017-02-09 18:35:26 -0800 | [diff] [blame] | 51 | import com.android.systemui.pip.BasePipManager; |
Hongwei Wang | 43a752b | 2019-09-17 20:20:30 +0000 | [diff] [blame] | 52 | import com.android.systemui.pip.PipBoundsHandler; |
Winson Chung | 2cf6ad8 | 2017-11-09 17:36:59 -0800 | [diff] [blame] | 53 | import com.android.systemui.shared.system.ActivityManagerWrapper; |
Hongwei Wang | 43a752b | 2019-09-17 20:20:30 +0000 | [diff] [blame] | 54 | import com.android.systemui.shared.system.PinnedStackListenerForwarder.PinnedStackListener; |
Winson Chung | 67f5c8b | 2018-09-24 12:09:19 -0700 | [diff] [blame] | 55 | import com.android.systemui.shared.system.TaskStackChangeListener; |
| 56 | import com.android.systemui.shared.system.WindowManagerWrapper; |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 57 | |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 58 | import java.util.ArrayList; |
| 59 | import java.util.List; |
| 60 | |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 61 | /** |
| 62 | * Manages the picture-in-picture (PIP) UI and states. |
| 63 | */ |
Winson Chung | 29a7865 | 2017-02-09 18:35:26 -0800 | [diff] [blame] | 64 | public class PipManager implements BasePipManager { |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 65 | private static final String TAG = "PipManager"; |
Jaewan Kim | 26c6356 | 2017-04-26 15:41:43 +0900 | [diff] [blame] | 66 | static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
| 67 | |
Jaewan Kim | 8af250e | 2017-02-14 19:12:01 +0900 | [diff] [blame] | 68 | private static final String SETTINGS_PACKAGE_AND_CLASS_DELIMITER = "/"; |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 69 | |
| 70 | private static PipManager sPipManager; |
Jaewan Kim | 8af250e | 2017-02-14 19:12:01 +0900 | [diff] [blame] | 71 | private static List<Pair<String, String>> sSettingsPackageAndClassNamePairList; |
Jaewan Kim | d89a694 | 2016-04-08 11:41:45 +0900 | [diff] [blame] | 72 | |
| 73 | /** |
Jaewan Kim | a0d4d25 | 2016-03-31 13:37:10 +0900 | [diff] [blame] | 74 | * State when there's no PIP. |
| 75 | */ |
Wale Ogunwale | 480dca0 | 2016-02-06 13:58:29 -0800 | [diff] [blame] | 76 | public static final int STATE_NO_PIP = 0; |
Jaewan Kim | a0d4d25 | 2016-03-31 13:37:10 +0900 | [diff] [blame] | 77 | /** |
Jaewan Kim | e630923 | 2017-04-14 15:26:20 +0900 | [diff] [blame] | 78 | * State when PIP is shown. This is used as default PIP state. |
Jaewan Kim | a0d4d25 | 2016-03-31 13:37:10 +0900 | [diff] [blame] | 79 | */ |
Jaewan Kim | e630923 | 2017-04-14 15:26:20 +0900 | [diff] [blame] | 80 | public static final int STATE_PIP = 1; |
Jaewan Kim | a0d4d25 | 2016-03-31 13:37:10 +0900 | [diff] [blame] | 81 | /** |
| 82 | * State when PIP menu dialog is shown. |
| 83 | */ |
Wale Ogunwale | 480dca0 | 2016-02-06 13:58:29 -0800 | [diff] [blame] | 84 | public static final int STATE_PIP_MENU = 2; |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 85 | |
Youngsang Cho | ad8ceb0 | 2016-01-15 16:59:27 -0800 | [diff] [blame] | 86 | private static final int TASK_ID_NO_PIP = -1; |
| 87 | private static final int INVALID_RESOURCE_TYPE = -1; |
| 88 | |
Wale Ogunwale | 480dca0 | 2016-02-06 13:58:29 -0800 | [diff] [blame] | 89 | public static final int SUSPEND_PIP_RESIZE_REASON_WAITING_FOR_MENU_ACTIVITY_FINISH = 0x1; |
| 90 | public static final int SUSPEND_PIP_RESIZE_REASON_WAITING_FOR_OVERLAY_ACTIVITY_FINISH = 0x2; |
Jaewan Kim | f40fcdc | 2016-03-04 17:58:22 +0900 | [diff] [blame] | 91 | |
Jaewan Kim | 8f584b8 | 2016-03-22 22:16:59 +0900 | [diff] [blame] | 92 | /** |
| 93 | * PIPed activity is playing a media and it can be paused. |
| 94 | */ |
| 95 | static final int PLAYBACK_STATE_PLAYING = 0; |
| 96 | /** |
| 97 | * PIPed activity has a paused media and it can be played. |
| 98 | */ |
| 99 | static final int PLAYBACK_STATE_PAUSED = 1; |
| 100 | /** |
| 101 | * Users are unable to control PIPed activity's media playback. |
| 102 | */ |
| 103 | static final int PLAYBACK_STATE_UNAVAILABLE = 2; |
| 104 | |
Jaewan Kim | f40fcdc | 2016-03-04 17:58:22 +0900 | [diff] [blame] | 105 | private static final int CLOSE_PIP_WHEN_MEDIA_SESSION_GONE_TIMEOUT_MS = 3000; |
| 106 | |
Wale Ogunwale | 480dca0 | 2016-02-06 13:58:29 -0800 | [diff] [blame] | 107 | private int mSuspendPipResizingReason; |
| 108 | |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 109 | private Context mContext; |
Hongwei Wang | 43a752b | 2019-09-17 20:20:30 +0000 | [diff] [blame] | 110 | private PipBoundsHandler mPipBoundsHandler; |
Wale Ogunwale | 65ebd95 | 2018-04-25 15:41:44 -0700 | [diff] [blame] | 111 | private IActivityTaskManager mActivityTaskManager; |
Jaewan Kim | 6233819 | 2016-02-25 10:00:05 -0800 | [diff] [blame] | 112 | private MediaSessionManager mMediaSessionManager; |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 113 | private int mState = STATE_NO_PIP; |
Winson Chung | 0a65737 | 2017-05-12 15:04:54 -0700 | [diff] [blame] | 114 | private int mResumeResizePinnedStackRunnableState = STATE_NO_PIP; |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 115 | private final Handler mHandler = new Handler(); |
| 116 | private List<Listener> mListeners = new ArrayList<>(); |
Jaewan Kim | a0d4d25 | 2016-03-31 13:37:10 +0900 | [diff] [blame] | 117 | private List<MediaListener> mMediaListeners = new ArrayList<>(); |
Jaewan Kim | c92a7d1 | 2016-02-15 17:33:25 -0800 | [diff] [blame] | 118 | private Rect mCurrentPipBounds; |
| 119 | private Rect mPipBounds; |
Winson Chung | db2ad5d | 2017-02-07 17:18:01 -0800 | [diff] [blame] | 120 | private Rect mDefaultPipBounds = new Rect(); |
Jaewan Kim | d89a694 | 2016-04-08 11:41:45 +0900 | [diff] [blame] | 121 | private Rect mSettingsPipBounds; |
Jaewan Kim | c92a7d1 | 2016-02-15 17:33:25 -0800 | [diff] [blame] | 122 | private Rect mMenuModePipBounds; |
Winson Chung | 0c5a592 | 2017-05-22 17:41:06 -0700 | [diff] [blame] | 123 | private int mLastOrientation = Configuration.ORIENTATION_UNDEFINED; |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 124 | private boolean mInitialized; |
Youngsang Cho | ad8ceb0 | 2016-01-15 16:59:27 -0800 | [diff] [blame] | 125 | private int mPipTaskId = TASK_ID_NO_PIP; |
Wale Ogunwale | 89be576 | 2017-10-04 13:27:49 -0700 | [diff] [blame] | 126 | private int mPinnedStackId = INVALID_STACK_ID; |
Jaewan Kim | 6233819 | 2016-02-25 10:00:05 -0800 | [diff] [blame] | 127 | private ComponentName mPipComponentName; |
| 128 | private MediaController mPipMediaController; |
Dongwon Kang | 353d8d7 | 2016-04-13 21:55:22 -0700 | [diff] [blame] | 129 | private String[] mLastPackagesResourceGranted; |
Jaewan Kim | 26c6356 | 2017-04-26 15:41:43 +0900 | [diff] [blame] | 130 | private PipNotification mPipNotification; |
Winson Chung | b6de872 | 2017-06-02 12:45:51 -0700 | [diff] [blame] | 131 | private ParceledListSlice mCustomActions; |
Youngsang Cho | ad8ceb0 | 2016-01-15 16:59:27 -0800 | [diff] [blame] | 132 | |
Hongwei Wang | 43a752b | 2019-09-17 20:20:30 +0000 | [diff] [blame] | 133 | // Used to calculate the movement bounds |
| 134 | private final DisplayInfo mTmpDisplayInfo = new DisplayInfo(); |
| 135 | private final Rect mTmpInsetBounds = new Rect(); |
| 136 | private final Rect mTmpNormalBounds = new Rect(); |
| 137 | |
Winson Chung | 44315c6 | 2017-08-24 13:35:24 -0700 | [diff] [blame] | 138 | // Keeps track of the IME visibility to adjust the PiP when the IME is visible |
| 139 | private boolean mImeVisible; |
| 140 | private int mImeHeightAdjustment; |
| 141 | |
Hongwei Wang | 43a752b | 2019-09-17 20:20:30 +0000 | [diff] [blame] | 142 | private final PinnedStackListener mPinnedStackListener = new PipManagerPinnedStackListener(); |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 143 | |
Wale Ogunwale | 480dca0 | 2016-02-06 13:58:29 -0800 | [diff] [blame] | 144 | private final Runnable mResizePinnedStackRunnable = new Runnable() { |
| 145 | @Override |
| 146 | public void run() { |
Winson Chung | 0a65737 | 2017-05-12 15:04:54 -0700 | [diff] [blame] | 147 | resizePinnedStack(mResumeResizePinnedStackRunnableState); |
Wale Ogunwale | 480dca0 | 2016-02-06 13:58:29 -0800 | [diff] [blame] | 148 | } |
| 149 | }; |
Jaewan Kim | f40fcdc | 2016-03-04 17:58:22 +0900 | [diff] [blame] | 150 | private final Runnable mClosePipRunnable = new Runnable() { |
| 151 | @Override |
| 152 | public void run() { |
| 153 | closePip(); |
| 154 | } |
| 155 | }; |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 156 | |
Youngsang Cho | ad8ceb0 | 2016-01-15 16:59:27 -0800 | [diff] [blame] | 157 | private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 158 | @Override |
| 159 | public void onReceive(Context context, Intent intent) { |
Youngsang Cho | ad8ceb0 | 2016-01-15 16:59:27 -0800 | [diff] [blame] | 160 | String action = intent.getAction(); |
Jaewan Kim | c552b04 | 2016-01-18 16:08:45 +0900 | [diff] [blame] | 161 | if (Intent.ACTION_MEDIA_RESOURCE_GRANTED.equals(action)) { |
Youngsang Cho | ad8ceb0 | 2016-01-15 16:59:27 -0800 | [diff] [blame] | 162 | String[] packageNames = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES); |
| 163 | int resourceType = intent.getIntExtra(Intent.EXTRA_MEDIA_RESOURCE_TYPE, |
| 164 | INVALID_RESOURCE_TYPE); |
Dongwon Kang | 353d8d7 | 2016-04-13 21:55:22 -0700 | [diff] [blame] | 165 | if (packageNames != null && packageNames.length > 0 |
Youngsang Cho | ad8ceb0 | 2016-01-15 16:59:27 -0800 | [diff] [blame] | 166 | && resourceType == Intent.EXTRA_MEDIA_RESOURCE_TYPE_VIDEO_CODEC) { |
| 167 | handleMediaResourceGranted(packageNames); |
| 168 | } |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 169 | } |
Youngsang Cho | ad8ceb0 | 2016-01-15 16:59:27 -0800 | [diff] [blame] | 170 | |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 171 | } |
| 172 | }; |
Jaewan Kim | 6233819 | 2016-02-25 10:00:05 -0800 | [diff] [blame] | 173 | private final MediaSessionManager.OnActiveSessionsChangedListener mActiveMediaSessionListener = |
| 174 | new MediaSessionManager.OnActiveSessionsChangedListener() { |
| 175 | @Override |
| 176 | public void onActiveSessionsChanged(List<MediaController> controllers) { |
| 177 | updateMediaController(controllers); |
| 178 | } |
| 179 | }; |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 180 | |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 181 | /** |
| 182 | * Handler for messages from the PIP controller. |
| 183 | */ |
Hongwei Wang | 43a752b | 2019-09-17 20:20:30 +0000 | [diff] [blame] | 184 | private class PipManagerPinnedStackListener extends PinnedStackListener { |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 185 | @Override |
Winson Chung | 44315c6 | 2017-08-24 13:35:24 -0700 | [diff] [blame] | 186 | public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) { |
| 187 | if (mState == STATE_PIP) { |
| 188 | if (mImeVisible != imeVisible) { |
| 189 | if (imeVisible) { |
| 190 | // Save the IME height adjustment, and offset to not occlude the IME |
| 191 | mPipBounds.offset(0, -imeHeight); |
| 192 | mImeHeightAdjustment = imeHeight; |
| 193 | } else { |
| 194 | // Apply the inverse adjustment when the IME is hidden |
| 195 | mPipBounds.offset(0, mImeHeightAdjustment); |
| 196 | } |
| 197 | mImeVisible = imeVisible; |
| 198 | resizePinnedStack(STATE_PIP); |
| 199 | } |
| 200 | } |
| 201 | } |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 202 | |
| 203 | @Override |
Hongwei Wang | 43a752b | 2019-09-17 20:20:30 +0000 | [diff] [blame] | 204 | public void onMovementBoundsChanged(Rect animatingBounds, boolean fromImeAdjustment, |
| 205 | boolean fromShelfAdjustment) { |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 206 | mHandler.post(() -> { |
Hongwei Wang | 43a752b | 2019-09-17 20:20:30 +0000 | [diff] [blame] | 207 | // Populate the inset / normal bounds and DisplayInfo from mPipBoundsHandler first. |
| 208 | mPipBoundsHandler.onMovementBoundsChanged(mTmpInsetBounds, mTmpNormalBounds, |
| 209 | animatingBounds, mTmpDisplayInfo); |
| 210 | mDefaultPipBounds.set(animatingBounds); |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 211 | }); |
| 212 | } |
| 213 | |
| 214 | @Override |
Winson Chung | b6de872 | 2017-06-02 12:45:51 -0700 | [diff] [blame] | 215 | public void onActionsChanged(ParceledListSlice actions) { |
| 216 | mCustomActions = actions; |
| 217 | mHandler.post(() -> { |
| 218 | for (int i = mListeners.size() - 1; i >= 0; --i) { |
| 219 | mListeners.get(i).onPipMenuActionsChanged(mCustomActions); |
| 220 | } |
| 221 | }); |
| 222 | } |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 223 | } |
| 224 | |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 225 | private PipManager() { } |
| 226 | |
| 227 | /** |
| 228 | * Initializes {@link PipManager}. |
| 229 | */ |
| 230 | public void initialize(Context context) { |
| 231 | if (mInitialized) { |
| 232 | return; |
| 233 | } |
| 234 | mInitialized = true; |
| 235 | mContext = context; |
Hongwei Wang | 43a752b | 2019-09-17 20:20:30 +0000 | [diff] [blame] | 236 | mPipBoundsHandler = new PipBoundsHandler(context); |
Wale Ogunwale | 65ebd95 | 2018-04-25 15:41:44 -0700 | [diff] [blame] | 237 | mActivityTaskManager = ActivityTaskManager.getService(); |
Winson Chung | 2cf6ad8 | 2017-11-09 17:36:59 -0800 | [diff] [blame] | 238 | ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener); |
Jaewan Kim | 73ef351 | 2016-07-18 13:50:33 +0900 | [diff] [blame] | 239 | IntentFilter intentFilter = new IntentFilter(); |
| 240 | intentFilter.addAction(Intent.ACTION_MEDIA_RESOURCE_GRANTED); |
Kyeongkab.Nam | d61de15 | 2018-10-09 14:55:42 +0900 | [diff] [blame] | 241 | mContext.registerReceiverAsUser(mBroadcastReceiver, UserHandle.ALL, intentFilter, |
| 242 | null, null); |
Jaewan Kim | 73ef351 | 2016-07-18 13:50:33 +0900 | [diff] [blame] | 243 | |
Jaewan Kim | 8af250e | 2017-02-14 19:12:01 +0900 | [diff] [blame] | 244 | if (sSettingsPackageAndClassNamePairList == null) { |
| 245 | String[] settings = mContext.getResources().getStringArray( |
| 246 | R.array.tv_pip_settings_class_name); |
| 247 | sSettingsPackageAndClassNamePairList = new ArrayList<>(); |
| 248 | if (settings != null) { |
| 249 | for (int i = 0; i < settings.length; i++) { |
| 250 | Pair<String, String> entry = null; |
| 251 | String[] packageAndClassName = |
| 252 | settings[i].split(SETTINGS_PACKAGE_AND_CLASS_DELIMITER); |
| 253 | switch (packageAndClassName.length) { |
| 254 | case 1: |
| 255 | entry = Pair.<String, String>create(packageAndClassName[0], null); |
| 256 | break; |
| 257 | case 2: |
Shuichi.Noguchi | d206782 | 2018-01-13 21:47:04 +0900 | [diff] [blame] | 258 | if (packageAndClassName[1] != null) { |
| 259 | entry = Pair.<String, String>create(packageAndClassName[0], |
| 260 | packageAndClassName[1].startsWith(".") |
| 261 | ? packageAndClassName[0] + packageAndClassName[1] |
| 262 | : packageAndClassName[1]); |
Jaewan Kim | dc29875 | 2017-02-24 10:45:12 +0900 | [diff] [blame] | 263 | } |
Shuichi.Noguchi | d206782 | 2018-01-13 21:47:04 +0900 | [diff] [blame] | 264 | break; |
Jaewan Kim | 8af250e | 2017-02-14 19:12:01 +0900 | [diff] [blame] | 265 | } |
| 266 | if (entry != null) { |
| 267 | sSettingsPackageAndClassNamePairList.add(entry); |
Jaewan Kim | dc29875 | 2017-02-24 10:45:12 +0900 | [diff] [blame] | 268 | } else { |
| 269 | Log.w(TAG, "Ignoring malformed settings name " + settings[i]); |
Jaewan Kim | 8af250e | 2017-02-14 19:12:01 +0900 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
Winson Chung | 8068ec2 | 2017-06-02 09:58:00 -0700 | [diff] [blame] | 275 | // Initialize the last orientation and apply the current configuration |
| 276 | Configuration initialConfig = mContext.getResources().getConfiguration(); |
| 277 | mLastOrientation = initialConfig.orientation; |
| 278 | loadConfigurationsAndApply(initialConfig); |
| 279 | |
Jaewan Kim | 73ef351 | 2016-07-18 13:50:33 +0900 | [diff] [blame] | 280 | mMediaSessionManager = |
| 281 | (MediaSessionManager) mContext.getSystemService(Context.MEDIA_SESSION_SERVICE); |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 282 | |
| 283 | try { |
Hongwei Wang | 43a752b | 2019-09-17 20:20:30 +0000 | [diff] [blame] | 284 | WindowManagerWrapper.getInstance().addPinnedStackListener(mPinnedStackListener); |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 285 | } catch (RemoteException e) { |
| 286 | Log.e(TAG, "Failed to register pinned stack listener", e); |
| 287 | } |
Jaewan Kim | 26c6356 | 2017-04-26 15:41:43 +0900 | [diff] [blame] | 288 | |
| 289 | mPipNotification = new PipNotification(context); |
Jaewan Kim | 73ef351 | 2016-07-18 13:50:33 +0900 | [diff] [blame] | 290 | } |
| 291 | |
Winson Chung | 0c5a592 | 2017-05-22 17:41:06 -0700 | [diff] [blame] | 292 | private void loadConfigurationsAndApply(Configuration newConfig) { |
| 293 | if (mLastOrientation != newConfig.orientation) { |
| 294 | // Don't resize the pinned stack on orientation change. TV does not care about this case |
| 295 | // and this could clobber the existing animation to the new bounds calculated by WM. |
| 296 | mLastOrientation = newConfig.orientation; |
| 297 | return; |
| 298 | } |
| 299 | |
Jaewan Kim | 73ef351 | 2016-07-18 13:50:33 +0900 | [diff] [blame] | 300 | Resources res = mContext.getResources(); |
Jaewan Kim | d89a694 | 2016-04-08 11:41:45 +0900 | [diff] [blame] | 301 | mSettingsPipBounds = Rect.unflattenFromString(res.getString( |
| 302 | R.string.pip_settings_bounds)); |
Jaewan Kim | c92a7d1 | 2016-02-15 17:33:25 -0800 | [diff] [blame] | 303 | mMenuModePipBounds = Rect.unflattenFromString(res.getString( |
Jaewan Kim | 8f584b8 | 2016-03-22 22:16:59 +0900 | [diff] [blame] | 304 | R.string.pip_menu_bounds)); |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 305 | |
Jaewan Kim | 73ef351 | 2016-07-18 13:50:33 +0900 | [diff] [blame] | 306 | // Reset the PIP bounds and apply. PIP bounds can be changed by two reasons. |
| 307 | // 1. Configuration changed due to the language change (RTL <-> RTL) |
| 308 | // 2. SystemUI restarts after the crash |
| 309 | mPipBounds = isSettingsShown() ? mSettingsPipBounds : mDefaultPipBounds; |
Jaewan Kim | e630923 | 2017-04-14 15:26:20 +0900 | [diff] [blame] | 310 | resizePinnedStack(getPinnedStackInfo() == null ? STATE_NO_PIP : STATE_PIP); |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 311 | } |
| 312 | |
Jaewan Kim | c552b04 | 2016-01-18 16:08:45 +0900 | [diff] [blame] | 313 | /** |
Jaewan Kim | 2d4d07c | 2016-05-20 10:28:17 +0900 | [diff] [blame] | 314 | * Updates the PIP per configuration changed. |
| 315 | */ |
Winson Chung | 0c5a592 | 2017-05-22 17:41:06 -0700 | [diff] [blame] | 316 | public void onConfigurationChanged(Configuration newConfig) { |
| 317 | loadConfigurationsAndApply(newConfig); |
Jaewan Kim | 26c6356 | 2017-04-26 15:41:43 +0900 | [diff] [blame] | 318 | mPipNotification.onConfigurationChanged(mContext); |
Jaewan Kim | 2d4d07c | 2016-05-20 10:28:17 +0900 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | /** |
Jaewan Kim | f0fd218 | 2016-04-20 21:17:58 +0900 | [diff] [blame] | 322 | * Shows the picture-in-picture menu if an activity is in picture-in-picture mode. |
Jaewan Kim | c552b04 | 2016-01-18 16:08:45 +0900 | [diff] [blame] | 323 | */ |
Winson Chung | ac52f28 | 2017-03-30 14:44:52 -0700 | [diff] [blame] | 324 | public void showPictureInPictureMenu() { |
Winson Chung | 0a65737 | 2017-05-12 15:04:54 -0700 | [diff] [blame] | 325 | if (getState() == STATE_PIP) { |
Wale Ogunwale | 480dca0 | 2016-02-06 13:58:29 -0800 | [diff] [blame] | 326 | resizePinnedStack(STATE_PIP_MENU); |
Jaewan Kim | c552b04 | 2016-01-18 16:08:45 +0900 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 330 | /** |
Jaewan Kim | 977dcdc | 2016-01-20 19:21:08 +0900 | [diff] [blame] | 331 | * Closes PIP (PIPed activity and PIP system UI). |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 332 | */ |
| 333 | public void closePip() { |
Youngsang Cho | 336007b | 2016-02-22 11:17:29 -0800 | [diff] [blame] | 334 | closePipInternal(true); |
Youngsang Cho | 23df699 | 2016-01-26 17:51:33 -0800 | [diff] [blame] | 335 | } |
| 336 | |
Youngsang Cho | 336007b | 2016-02-22 11:17:29 -0800 | [diff] [blame] | 337 | private void closePipInternal(boolean removePipStack) { |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 338 | mState = STATE_NO_PIP; |
Youngsang Cho | ad8ceb0 | 2016-01-15 16:59:27 -0800 | [diff] [blame] | 339 | mPipTaskId = TASK_ID_NO_PIP; |
Jaewan Kim | 6233819 | 2016-02-25 10:00:05 -0800 | [diff] [blame] | 340 | mPipMediaController = null; |
| 341 | mMediaSessionManager.removeOnActiveSessionsChangedListener(mActiveMediaSessionListener); |
Youngsang Cho | 23df699 | 2016-01-26 17:51:33 -0800 | [diff] [blame] | 342 | if (removePipStack) { |
| 343 | try { |
Wale Ogunwale | 04d9cb5 | 2018-04-30 13:55:07 -0700 | [diff] [blame] | 344 | mActivityTaskManager.removeStack(mPinnedStackId); |
Youngsang Cho | 23df699 | 2016-01-26 17:51:33 -0800 | [diff] [blame] | 345 | } catch (RemoteException e) { |
| 346 | Log.e(TAG, "removeStack failed", e); |
Wale Ogunwale | 89be576 | 2017-10-04 13:27:49 -0700 | [diff] [blame] | 347 | } finally { |
| 348 | mPinnedStackId = INVALID_STACK_ID; |
Youngsang Cho | 23df699 | 2016-01-26 17:51:33 -0800 | [diff] [blame] | 349 | } |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 350 | } |
Jaewan Kim | c92a7d1 | 2016-02-15 17:33:25 -0800 | [diff] [blame] | 351 | for (int i = mListeners.size() - 1; i >= 0; --i) { |
| 352 | mListeners.get(i).onPipActivityClosed(); |
| 353 | } |
Jaewan Kim | f40fcdc | 2016-03-04 17:58:22 +0900 | [diff] [blame] | 354 | mHandler.removeCallbacks(mClosePipRunnable); |
Jaewan Kim | f0fd218 | 2016-04-20 21:17:58 +0900 | [diff] [blame] | 355 | updatePipVisibility(false); |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | /** |
Jaewan Kim | 977dcdc | 2016-01-20 19:21:08 +0900 | [diff] [blame] | 359 | * Moves the PIPed activity to the fullscreen and closes PIP system UI. |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 360 | */ |
Jaewan Kim | a0d4d25 | 2016-03-31 13:37:10 +0900 | [diff] [blame] | 361 | void movePipToFullscreen() { |
Youngsang Cho | ad8ceb0 | 2016-01-15 16:59:27 -0800 | [diff] [blame] | 362 | mPipTaskId = TASK_ID_NO_PIP; |
Jaewan Kim | b003364 | 2016-04-22 18:41:37 +0900 | [diff] [blame] | 363 | for (int i = mListeners.size() - 1; i >= 0; --i) { |
| 364 | mListeners.get(i).onMoveToFullscreen(); |
| 365 | } |
Winson Chung | f04c1dd | 2017-02-24 16:47:37 -0800 | [diff] [blame] | 366 | resizePinnedStack(STATE_NO_PIP); |
Jaewan Kim | 47fe286 | 2016-10-10 16:23:28 +0900 | [diff] [blame] | 367 | updatePipVisibility(false); |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | /** |
Wale Ogunwale | 480dca0 | 2016-02-06 13:58:29 -0800 | [diff] [blame] | 371 | * Suspends resizing operation on the Pip until {@link #resumePipResizing} is called |
| 372 | * @param reason The reason for suspending resizing operations on the Pip. |
| 373 | */ |
| 374 | public void suspendPipResizing(int reason) { |
| 375 | if (DEBUG) Log.d(TAG, |
| 376 | "suspendPipResizing() reason=" + reason + " callers=" + Debug.getCallers(2)); |
| 377 | mSuspendPipResizingReason |= reason; |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * Resumes resizing operation on the Pip that was previously suspended. |
| 382 | * @param reason The reason resizing operations on the Pip was suspended. |
| 383 | */ |
| 384 | public void resumePipResizing(int reason) { |
| 385 | if ((mSuspendPipResizingReason & reason) == 0) { |
| 386 | return; |
| 387 | } |
| 388 | if (DEBUG) Log.d(TAG, |
| 389 | "resumePipResizing() reason=" + reason + " callers=" + Debug.getCallers(2)); |
| 390 | mSuspendPipResizingReason &= ~reason; |
| 391 | mHandler.post(mResizePinnedStackRunnable); |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * Resize the Pip to the appropriate size for the input state. |
| 396 | * @param state In Pip state also used to determine the new size for the Pip. |
| 397 | */ |
Jaewan Kim | a0d4d25 | 2016-03-31 13:37:10 +0900 | [diff] [blame] | 398 | void resizePinnedStack(int state) { |
Jaewan Kim | 26c6356 | 2017-04-26 15:41:43 +0900 | [diff] [blame] | 399 | if (DEBUG) Log.d(TAG, "resizePinnedStack() state=" + state, new Exception()); |
Winson Chung | f04c1dd | 2017-02-24 16:47:37 -0800 | [diff] [blame] | 400 | boolean wasStateNoPip = (mState == STATE_NO_PIP); |
Wale Ogunwale | 480dca0 | 2016-02-06 13:58:29 -0800 | [diff] [blame] | 401 | for (int i = mListeners.size() - 1; i >= 0; --i) { |
| 402 | mListeners.get(i).onPipResizeAboutToStart(); |
| 403 | } |
Wale Ogunwale | 480dca0 | 2016-02-06 13:58:29 -0800 | [diff] [blame] | 404 | if (mSuspendPipResizingReason != 0) { |
Winson Chung | 0a65737 | 2017-05-12 15:04:54 -0700 | [diff] [blame] | 405 | mResumeResizePinnedStackRunnableState = state; |
Winson Chung | d62acec | 2017-03-31 19:42:32 -0700 | [diff] [blame] | 406 | if (DEBUG) Log.d(TAG, "resizePinnedStack() deferring" |
| 407 | + " mSuspendPipResizingReason=" + mSuspendPipResizingReason |
Winson Chung | 0a65737 | 2017-05-12 15:04:54 -0700 | [diff] [blame] | 408 | + " mResumeResizePinnedStackRunnableState=" |
| 409 | + mResumeResizePinnedStackRunnableState); |
Wale Ogunwale | 480dca0 | 2016-02-06 13:58:29 -0800 | [diff] [blame] | 410 | return; |
| 411 | } |
Winson Chung | d62acec | 2017-03-31 19:42:32 -0700 | [diff] [blame] | 412 | mState = state; |
Jaewan Kim | c92a7d1 | 2016-02-15 17:33:25 -0800 | [diff] [blame] | 413 | switch (mState) { |
| 414 | case STATE_NO_PIP: |
| 415 | mCurrentPipBounds = null; |
Winson Chung | f04c1dd | 2017-02-24 16:47:37 -0800 | [diff] [blame] | 416 | // If the state was already STATE_NO_PIP, then do not resize the stack below as it |
| 417 | // will not exist |
| 418 | if (wasStateNoPip) { |
| 419 | return; |
| 420 | } |
Jaewan Kim | c92a7d1 | 2016-02-15 17:33:25 -0800 | [diff] [blame] | 421 | break; |
| 422 | case STATE_PIP_MENU: |
| 423 | mCurrentPipBounds = mMenuModePipBounds; |
| 424 | break; |
Jaewan Kim | e630923 | 2017-04-14 15:26:20 +0900 | [diff] [blame] | 425 | case STATE_PIP: |
Jaewan Kim | a0d4d25 | 2016-03-31 13:37:10 +0900 | [diff] [blame] | 426 | mCurrentPipBounds = mPipBounds; |
| 427 | break; |
Jaewan Kim | c92a7d1 | 2016-02-15 17:33:25 -0800 | [diff] [blame] | 428 | default: |
| 429 | mCurrentPipBounds = mPipBounds; |
| 430 | break; |
| 431 | } |
Winson Chung | f04c1dd | 2017-02-24 16:47:37 -0800 | [diff] [blame] | 432 | try { |
| 433 | int animationDurationMs = -1; |
Evan Rosky | dbe2ce5 | 2019-07-18 11:13:17 -0700 | [diff] [blame] | 434 | mActivityTaskManager.animateResizePinnedStack(mPinnedStackId, mCurrentPipBounds, |
| 435 | animationDurationMs); |
Winson Chung | f04c1dd | 2017-02-24 16:47:37 -0800 | [diff] [blame] | 436 | } catch (RemoteException e) { |
| 437 | Log.e(TAG, "resizeStack failed", e); |
Wale Ogunwale | 480dca0 | 2016-02-06 13:58:29 -0800 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | |
| 441 | /** |
Winson Chung | 0a65737 | 2017-05-12 15:04:54 -0700 | [diff] [blame] | 442 | * @return the current state, or the pending state if the state change was previously suspended. |
| 443 | */ |
| 444 | private int getState() { |
| 445 | if (mSuspendPipResizingReason != 0) { |
| 446 | return mResumeResizePinnedStackRunnableState; |
| 447 | } |
| 448 | return mState; |
| 449 | } |
| 450 | |
| 451 | /** |
Jaewan Kim | a0d4d25 | 2016-03-31 13:37:10 +0900 | [diff] [blame] | 452 | * Returns the default PIP bound. |
Jaewan Kim | c92a7d1 | 2016-02-15 17:33:25 -0800 | [diff] [blame] | 453 | */ |
| 454 | public Rect getPipBounds() { |
Jaewan Kim | a0d4d25 | 2016-03-31 13:37:10 +0900 | [diff] [blame] | 455 | return mPipBounds; |
Jaewan Kim | c92a7d1 | 2016-02-15 17:33:25 -0800 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | /** |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 459 | * Shows PIP menu UI by launching {@link PipMenuActivity}. It also locates the pinned |
Jaewan Kim | 8f584b8 | 2016-03-22 22:16:59 +0900 | [diff] [blame] | 460 | * stack to the centered PIP bound {@link R.config_centeredPictureInPictureBounds}. |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 461 | */ |
Wale Ogunwale | 480dca0 | 2016-02-06 13:58:29 -0800 | [diff] [blame] | 462 | private void showPipMenu() { |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 463 | if (DEBUG) Log.d(TAG, "showPipMenu()"); |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 464 | mState = STATE_PIP_MENU; |
| 465 | for (int i = mListeners.size() - 1; i >= 0; --i) { |
| 466 | mListeners.get(i).onShowPipMenu(); |
| 467 | } |
| 468 | Intent intent = new Intent(mContext, PipMenuActivity.class); |
| 469 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
Winson Chung | b6de872 | 2017-06-02 12:45:51 -0700 | [diff] [blame] | 470 | intent.putExtra(PipMenuActivity.EXTRA_CUSTOM_ACTIONS, mCustomActions); |
Jaewan Kim | 1a9dc56 | 2016-02-17 13:41:51 -0800 | [diff] [blame] | 471 | mContext.startActivity(intent); |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 472 | } |
| 473 | |
Jaewan Kim | a0d4d25 | 2016-03-31 13:37:10 +0900 | [diff] [blame] | 474 | /** |
| 475 | * Adds a {@link Listener} to PipManager. |
| 476 | */ |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 477 | public void addListener(Listener listener) { |
| 478 | mListeners.add(listener); |
| 479 | } |
| 480 | |
Jaewan Kim | a0d4d25 | 2016-03-31 13:37:10 +0900 | [diff] [blame] | 481 | /** |
| 482 | * Removes a {@link Listener} from PipManager. |
| 483 | */ |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 484 | public void removeListener(Listener listener) { |
| 485 | mListeners.remove(listener); |
| 486 | } |
| 487 | |
Jaewan Kim | a0d4d25 | 2016-03-31 13:37:10 +0900 | [diff] [blame] | 488 | /** |
| 489 | * Adds a {@link MediaListener} to PipManager. |
| 490 | */ |
| 491 | public void addMediaListener(MediaListener listener) { |
| 492 | mMediaListeners.add(listener); |
| 493 | } |
| 494 | |
| 495 | /** |
| 496 | * Removes a {@link MediaListener} from PipManager. |
| 497 | */ |
| 498 | public void removeMediaListener(MediaListener listener) { |
| 499 | mMediaListeners.remove(listener); |
| 500 | } |
| 501 | |
Jaewan Kim | c92a7d1 | 2016-02-15 17:33:25 -0800 | [diff] [blame] | 502 | /** |
| 503 | * Returns {@code true} if PIP is shown. |
| 504 | */ |
| 505 | public boolean isPipShown() { |
Jaewan Kim | 82ac50d | 2016-03-21 17:34:28 +0900 | [diff] [blame] | 506 | return mState != STATE_NO_PIP; |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 507 | } |
| 508 | |
Jaewan Kim | 73ef351 | 2016-07-18 13:50:33 +0900 | [diff] [blame] | 509 | private StackInfo getPinnedStackInfo() { |
| 510 | StackInfo stackInfo = null; |
| 511 | try { |
Wale Ogunwale | 04d9cb5 | 2018-04-30 13:55:07 -0700 | [diff] [blame] | 512 | stackInfo = ActivityTaskManager.getService().getStackInfo( |
Wale Ogunwale | 6827856 | 2017-09-23 17:13:55 -0700 | [diff] [blame] | 513 | WINDOWING_MODE_PINNED, ACTIVITY_TYPE_UNDEFINED); |
Jaewan Kim | 73ef351 | 2016-07-18 13:50:33 +0900 | [diff] [blame] | 514 | } catch (RemoteException e) { |
| 515 | Log.e(TAG, "getStackInfo failed", e); |
| 516 | } |
| 517 | return stackInfo; |
| 518 | } |
| 519 | |
Youngsang Cho | ad8ceb0 | 2016-01-15 16:59:27 -0800 | [diff] [blame] | 520 | private void handleMediaResourceGranted(String[] packageNames) { |
Winson Chung | 0a65737 | 2017-05-12 15:04:54 -0700 | [diff] [blame] | 521 | if (getState() == STATE_NO_PIP) { |
Dongwon Kang | 353d8d7 | 2016-04-13 21:55:22 -0700 | [diff] [blame] | 522 | mLastPackagesResourceGranted = packageNames; |
| 523 | } else { |
| 524 | boolean requestedFromLastPackages = false; |
| 525 | if (mLastPackagesResourceGranted != null) { |
| 526 | for (String packageName : mLastPackagesResourceGranted) { |
| 527 | for (String newPackageName : packageNames) { |
| 528 | if (TextUtils.equals(newPackageName, packageName)) { |
| 529 | requestedFromLastPackages = true; |
| 530 | break; |
| 531 | } |
Youngsang Cho | ad8ceb0 | 2016-01-15 16:59:27 -0800 | [diff] [blame] | 532 | } |
| 533 | } |
| 534 | } |
Dongwon Kang | 353d8d7 | 2016-04-13 21:55:22 -0700 | [diff] [blame] | 535 | mLastPackagesResourceGranted = packageNames; |
| 536 | if (!requestedFromLastPackages) { |
| 537 | closePip(); |
| 538 | } |
Youngsang Cho | ad8ceb0 | 2016-01-15 16:59:27 -0800 | [diff] [blame] | 539 | } |
| 540 | } |
| 541 | |
Jaewan Kim | 6233819 | 2016-02-25 10:00:05 -0800 | [diff] [blame] | 542 | private void updateMediaController(List<MediaController> controllers) { |
| 543 | MediaController mediaController = null; |
Winson Chung | 0a65737 | 2017-05-12 15:04:54 -0700 | [diff] [blame] | 544 | if (controllers != null && getState() != STATE_NO_PIP && mPipComponentName != null) { |
Jaewan Kim | 6233819 | 2016-02-25 10:00:05 -0800 | [diff] [blame] | 545 | for (int i = controllers.size() - 1; i >= 0; i--) { |
| 546 | MediaController controller = controllers.get(i); |
| 547 | // We assumes that an app with PIPable activity |
| 548 | // keeps the single instance of media controller especially when PIP is on. |
| 549 | if (controller.getPackageName().equals(mPipComponentName.getPackageName())) { |
| 550 | mediaController = controller; |
| 551 | break; |
| 552 | } |
| 553 | } |
| 554 | } |
| 555 | if (mPipMediaController != mediaController) { |
| 556 | mPipMediaController = mediaController; |
Jaewan Kim | a0d4d25 | 2016-03-31 13:37:10 +0900 | [diff] [blame] | 557 | for (int i = mMediaListeners.size() - 1; i >= 0; i--) { |
| 558 | mMediaListeners.get(i).onMediaControllerChanged(); |
Jaewan Kim | 6233819 | 2016-02-25 10:00:05 -0800 | [diff] [blame] | 559 | } |
Jaewan Kim | f40fcdc | 2016-03-04 17:58:22 +0900 | [diff] [blame] | 560 | if (mPipMediaController == null) { |
| 561 | mHandler.postDelayed(mClosePipRunnable, |
| 562 | CLOSE_PIP_WHEN_MEDIA_SESSION_GONE_TIMEOUT_MS); |
| 563 | } else { |
| 564 | mHandler.removeCallbacks(mClosePipRunnable); |
| 565 | } |
Jaewan Kim | 6233819 | 2016-02-25 10:00:05 -0800 | [diff] [blame] | 566 | } |
| 567 | } |
| 568 | |
| 569 | /** |
| 570 | * Gets the {@link android.media.session.MediaController} for the PIPed activity. |
| 571 | */ |
| 572 | MediaController getMediaController() { |
| 573 | return mPipMediaController; |
| 574 | } |
| 575 | |
Jaewan Kim | 8f584b8 | 2016-03-22 22:16:59 +0900 | [diff] [blame] | 576 | /** |
| 577 | * Returns the PIPed activity's playback state. |
Jaewan Kim | 26c6356 | 2017-04-26 15:41:43 +0900 | [diff] [blame] | 578 | * This returns one of {@link #PLAYBACK_STATE_PLAYING}, {@link #PLAYBACK_STATE_PAUSED}, |
| 579 | * or {@link #PLAYBACK_STATE_UNAVAILABLE}. |
Jaewan Kim | 8f584b8 | 2016-03-22 22:16:59 +0900 | [diff] [blame] | 580 | */ |
| 581 | int getPlaybackState() { |
| 582 | if (mPipMediaController == null || mPipMediaController.getPlaybackState() == null) { |
| 583 | return PLAYBACK_STATE_UNAVAILABLE; |
| 584 | } |
| 585 | int state = mPipMediaController.getPlaybackState().getState(); |
| 586 | boolean isPlaying = (state == PlaybackState.STATE_BUFFERING |
| 587 | || state == PlaybackState.STATE_CONNECTING |
| 588 | || state == PlaybackState.STATE_PLAYING |
| 589 | || state == PlaybackState.STATE_FAST_FORWARDING |
| 590 | || state == PlaybackState.STATE_REWINDING |
| 591 | || state == PlaybackState.STATE_SKIPPING_TO_PREVIOUS |
| 592 | || state == PlaybackState.STATE_SKIPPING_TO_NEXT); |
| 593 | long actions = mPipMediaController.getPlaybackState().getActions(); |
| 594 | if (!isPlaying && ((actions & PlaybackState.ACTION_PLAY) != 0)) { |
| 595 | return PLAYBACK_STATE_PAUSED; |
| 596 | } else if (isPlaying && ((actions & PlaybackState.ACTION_PAUSE) != 0)) { |
| 597 | return PLAYBACK_STATE_PLAYING; |
| 598 | } |
| 599 | return PLAYBACK_STATE_UNAVAILABLE; |
| 600 | } |
| 601 | |
Jaewan Kim | 73ef351 | 2016-07-18 13:50:33 +0900 | [diff] [blame] | 602 | private boolean isSettingsShown() { |
| 603 | List<RunningTaskInfo> runningTasks; |
| 604 | try { |
Wale Ogunwale | 04d9cb5 | 2018-04-30 13:55:07 -0700 | [diff] [blame] | 605 | runningTasks = mActivityTaskManager.getTasks(1); |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 606 | if (runningTasks.isEmpty()) { |
Jaewan Kim | 73ef351 | 2016-07-18 13:50:33 +0900 | [diff] [blame] | 607 | return false; |
| 608 | } |
| 609 | } catch (RemoteException e) { |
| 610 | Log.d(TAG, "Failed to detect top activity", e); |
| 611 | return false; |
| 612 | } |
| 613 | ComponentName topActivity = runningTasks.get(0).topActivity; |
Jaewan Kim | d89a694 | 2016-04-08 11:41:45 +0900 | [diff] [blame] | 614 | for (Pair<String, String> componentName : sSettingsPackageAndClassNamePairList) { |
| 615 | String packageName = componentName.first; |
Andrii Kulian | 8290f8f | 2016-06-30 17:51:32 -0700 | [diff] [blame] | 616 | if (topActivity.getPackageName().equals(packageName)) { |
Jaewan Kim | d89a694 | 2016-04-08 11:41:45 +0900 | [diff] [blame] | 617 | String className = componentName.second; |
| 618 | if (className == null || topActivity.getClassName().equals(className)) { |
| 619 | return true; |
| 620 | } |
| 621 | } |
| 622 | } |
| 623 | return false; |
| 624 | } |
| 625 | |
Winson Chung | 67f5c8b | 2018-09-24 12:09:19 -0700 | [diff] [blame] | 626 | private TaskStackChangeListener mTaskStackListener = new TaskStackChangeListener() { |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 627 | @Override |
Jaewan Kim | 938a50b | 2016-03-14 17:35:43 +0900 | [diff] [blame] | 628 | public void onTaskStackChanged() { |
Jaewan Kim | 17ca4e3 | 2016-10-31 13:55:43 +0900 | [diff] [blame] | 629 | if (DEBUG) Log.d(TAG, "onTaskStackChanged()"); |
Shuichi.Noguchi | e9d2dd6 | 2017-11-22 17:24:17 +0900 | [diff] [blame] | 630 | |
Winson Chung | 0a65737 | 2017-05-12 15:04:54 -0700 | [diff] [blame] | 631 | if (getState() != STATE_NO_PIP) { |
Jaewan Kim | d89a694 | 2016-04-08 11:41:45 +0900 | [diff] [blame] | 632 | boolean hasPip = false; |
| 633 | |
Jaewan Kim | 73ef351 | 2016-07-18 13:50:33 +0900 | [diff] [blame] | 634 | StackInfo stackInfo = getPinnedStackInfo(); |
| 635 | if (stackInfo == null || stackInfo.taskIds == null) { |
| 636 | Log.w(TAG, "There is nothing in pinned stack"); |
| 637 | closePipInternal(false); |
Jaewan Kim | 938a50b | 2016-03-14 17:35:43 +0900 | [diff] [blame] | 638 | return; |
| 639 | } |
| 640 | for (int i = stackInfo.taskIds.length - 1; i >= 0; --i) { |
| 641 | if (stackInfo.taskIds[i] == mPipTaskId) { |
| 642 | // PIP task is still alive. |
Jaewan Kim | d89a694 | 2016-04-08 11:41:45 +0900 | [diff] [blame] | 643 | hasPip = true; |
| 644 | break; |
Jaewan Kim | 938a50b | 2016-03-14 17:35:43 +0900 | [diff] [blame] | 645 | } |
| 646 | } |
Jaewan Kim | d89a694 | 2016-04-08 11:41:45 +0900 | [diff] [blame] | 647 | if (!hasPip) { |
| 648 | // PIP task doesn't exist anymore in PINNED_STACK. |
| 649 | closePipInternal(true); |
| 650 | return; |
| 651 | } |
| 652 | } |
Winson Chung | 0a65737 | 2017-05-12 15:04:54 -0700 | [diff] [blame] | 653 | if (getState() == STATE_PIP) { |
Jaewan Kim | 73ef351 | 2016-07-18 13:50:33 +0900 | [diff] [blame] | 654 | Rect bounds = isSettingsShown() ? mSettingsPipBounds : mDefaultPipBounds; |
| 655 | if (mPipBounds != bounds) { |
| 656 | mPipBounds = bounds; |
Jaewan Kim | e630923 | 2017-04-14 15:26:20 +0900 | [diff] [blame] | 657 | resizePinnedStack(STATE_PIP); |
Jaewan Kim | d89a694 | 2016-04-08 11:41:45 +0900 | [diff] [blame] | 658 | } |
Jaewan Kim | 938a50b | 2016-03-14 17:35:43 +0900 | [diff] [blame] | 659 | } |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | @Override |
Wale Ogunwale | 89be576 | 2017-10-04 13:27:49 -0700 | [diff] [blame] | 663 | public void onActivityPinned(String packageName, int userId, int taskId, int stackId) { |
Wale Ogunwale | 480dca0 | 2016-02-06 13:58:29 -0800 | [diff] [blame] | 664 | if (DEBUG) Log.d(TAG, "onActivityPinned()"); |
Shuichi.Noguchi | e9d2dd6 | 2017-11-22 17:24:17 +0900 | [diff] [blame] | 665 | |
Jaewan Kim | 73ef351 | 2016-07-18 13:50:33 +0900 | [diff] [blame] | 666 | StackInfo stackInfo = getPinnedStackInfo(); |
| 667 | if (stackInfo == null) { |
| 668 | Log.w(TAG, "Cannot find pinned stack"); |
Jaewan Kim | 938a50b | 2016-03-14 17:35:43 +0900 | [diff] [blame] | 669 | return; |
| 670 | } |
| 671 | if (DEBUG) Log.d(TAG, "PINNED_STACK:" + stackInfo); |
Wale Ogunwale | 89be576 | 2017-10-04 13:27:49 -0700 | [diff] [blame] | 672 | mPinnedStackId = stackInfo.stackId; |
Jaewan Kim | 938a50b | 2016-03-14 17:35:43 +0900 | [diff] [blame] | 673 | mPipTaskId = stackInfo.taskIds[stackInfo.taskIds.length - 1]; |
| 674 | mPipComponentName = ComponentName.unflattenFromString( |
| 675 | stackInfo.taskNames[stackInfo.taskNames.length - 1]); |
Jaewan Kim | e630923 | 2017-04-14 15:26:20 +0900 | [diff] [blame] | 676 | // Set state to STATE_PIP so we show it when the pinned stack animation ends. |
| 677 | mState = STATE_PIP; |
Jaewan Kim | 938a50b | 2016-03-14 17:35:43 +0900 | [diff] [blame] | 678 | mCurrentPipBounds = mPipBounds; |
Jaewan Kim | 938a50b | 2016-03-14 17:35:43 +0900 | [diff] [blame] | 679 | mMediaSessionManager.addOnActiveSessionsChangedListener( |
| 680 | mActiveMediaSessionListener, null); |
| 681 | updateMediaController(mMediaSessionManager.getActiveSessions(null)); |
Jaewan Kim | 938a50b | 2016-03-14 17:35:43 +0900 | [diff] [blame] | 682 | for (int i = mListeners.size() - 1; i >= 0; i--) { |
| 683 | mListeners.get(i).onPipEntered(); |
| 684 | } |
Jaewan Kim | f0fd218 | 2016-04-20 21:17:58 +0900 | [diff] [blame] | 685 | updatePipVisibility(true); |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 686 | } |
Wale Ogunwale | cc25a8a | 2016-01-23 14:31:37 -0800 | [diff] [blame] | 687 | |
| 688 | @Override |
Winson Chung | e6385a2 | 2017-05-02 18:15:16 -0700 | [diff] [blame] | 689 | public void onPinnedActivityRestartAttempt(boolean clearedTask) { |
Wale Ogunwale | 480dca0 | 2016-02-06 13:58:29 -0800 | [diff] [blame] | 690 | if (DEBUG) Log.d(TAG, "onPinnedActivityRestartAttempt()"); |
Shuichi.Noguchi | e9d2dd6 | 2017-11-22 17:24:17 +0900 | [diff] [blame] | 691 | |
Jaewan Kim | 938a50b | 2016-03-14 17:35:43 +0900 | [diff] [blame] | 692 | // If PIPed activity is launched again by Launcher or intent, make it fullscreen. |
| 693 | movePipToFullscreen(); |
Wale Ogunwale | cc25a8a | 2016-01-23 14:31:37 -0800 | [diff] [blame] | 694 | } |
Wale Ogunwale | 480dca0 | 2016-02-06 13:58:29 -0800 | [diff] [blame] | 695 | |
| 696 | @Override |
| 697 | public void onPinnedStackAnimationEnded() { |
| 698 | if (DEBUG) Log.d(TAG, "onPinnedStackAnimationEnded()"); |
Shuichi.Noguchi | e9d2dd6 | 2017-11-22 17:24:17 +0900 | [diff] [blame] | 699 | |
Winson Chung | 0a65737 | 2017-05-12 15:04:54 -0700 | [diff] [blame] | 700 | switch (getState()) { |
Jaewan Kim | 938a50b | 2016-03-14 17:35:43 +0900 | [diff] [blame] | 701 | case STATE_PIP_MENU: |
| 702 | showPipMenu(); |
| 703 | break; |
| 704 | } |
Wale Ogunwale | 480dca0 | 2016-02-06 13:58:29 -0800 | [diff] [blame] | 705 | } |
Jaewan Kim | 938a50b | 2016-03-14 17:35:43 +0900 | [diff] [blame] | 706 | }; |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 707 | |
| 708 | /** |
| 709 | * A listener interface to receive notification on changes in PIP. |
| 710 | */ |
| 711 | public interface Listener { |
Jaewan Kim | 82ac50d | 2016-03-21 17:34:28 +0900 | [diff] [blame] | 712 | /** |
| 713 | * Invoked when an activity is pinned and PIP manager is set corresponding information. |
| 714 | * Classes must use this instead of {@link android.app.ITaskStackListener.onActivityPinned} |
| 715 | * because there's no guarantee for the PIP manager be return relavent information |
Jaewan Kim | a0d4d25 | 2016-03-31 13:37:10 +0900 | [diff] [blame] | 716 | * correctly. (e.g. {@link isPipShown}). |
Jaewan Kim | 82ac50d | 2016-03-21 17:34:28 +0900 | [diff] [blame] | 717 | */ |
| 718 | void onPipEntered(); |
Wale Ogunwale | 480dca0 | 2016-02-06 13:58:29 -0800 | [diff] [blame] | 719 | /** Invoked when a PIPed activity is closed. */ |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 720 | void onPipActivityClosed(); |
Wale Ogunwale | 480dca0 | 2016-02-06 13:58:29 -0800 | [diff] [blame] | 721 | /** Invoked when the PIP menu gets shown. */ |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 722 | void onShowPipMenu(); |
Winson Chung | b6de872 | 2017-06-02 12:45:51 -0700 | [diff] [blame] | 723 | /** Invoked when the PIP menu actions change. */ |
| 724 | void onPipMenuActionsChanged(ParceledListSlice actions); |
Jaewan Kim | 10a8691 | 2016-04-04 16:01:51 +0900 | [diff] [blame] | 725 | /** Invoked when the PIPed activity is about to return back to the fullscreen. */ |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 726 | void onMoveToFullscreen(); |
Wale Ogunwale | 480dca0 | 2016-02-06 13:58:29 -0800 | [diff] [blame] | 727 | /** Invoked when we are above to start resizing the Pip. */ |
| 728 | void onPipResizeAboutToStart(); |
Jaewan Kim | a0d4d25 | 2016-03-31 13:37:10 +0900 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | /** |
| 732 | * A listener interface to receive change in PIP's media controller |
| 733 | */ |
| 734 | public interface MediaListener { |
Jaewan Kim | 6233819 | 2016-02-25 10:00:05 -0800 | [diff] [blame] | 735 | /** Invoked when the MediaController on PIPed activity is changed. */ |
| 736 | void onMediaControllerChanged(); |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | /** |
| 740 | * Gets an instance of {@link PipManager}. |
| 741 | */ |
| 742 | public static PipManager getInstance() { |
| 743 | if (sPipManager == null) { |
| 744 | sPipManager = new PipManager(); |
| 745 | } |
| 746 | return sPipManager; |
| 747 | } |
Jaewan Kim | a0d4d25 | 2016-03-31 13:37:10 +0900 | [diff] [blame] | 748 | |
Winson | ab21660 | 2016-08-09 14:05:20 -0700 | [diff] [blame] | 749 | private void updatePipVisibility(final boolean visible) { |
Winson Chung | 67f5c8b | 2018-09-24 12:09:19 -0700 | [diff] [blame] | 750 | Dependency.get(UiOffloadThread.class).submit(() -> { |
| 751 | WindowManagerWrapper.getInstance().setPipVisibility(visible); |
| 752 | }); |
Jaewan Kim | f0fd218 | 2016-04-20 21:17:58 +0900 | [diff] [blame] | 753 | } |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 754 | } |