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