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