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 | |
| 17 | package com.android.systemui.tv.pip; |
| 18 | |
Winson | c0d7058 | 2016-01-29 10:24:39 -0800 | [diff] [blame] | 19 | import android.app.ActivityManager.RunningTaskInfo; |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 20 | import android.app.ActivityManager.StackInfo; |
| 21 | import android.app.ActivityManagerNative; |
| 22 | import android.app.ActivityOptions; |
| 23 | import android.app.IActivityManager; |
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; |
| 29 | import android.content.res.Resources; |
| 30 | import android.graphics.Rect; |
Jaewan Kim | 6233819 | 2016-02-25 10:00:05 -0800 | [diff] [blame] | 31 | import android.media.session.MediaController; |
| 32 | import android.media.session.MediaSessionManager; |
Jaewan Kim | 8f584b8 | 2016-03-22 22:16:59 +0900 | [diff] [blame] | 33 | import android.media.session.PlaybackState; |
Wale Ogunwale | 480dca0 | 2016-02-06 13:58:29 -0800 | [diff] [blame] | 34 | import android.os.Debug; |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 35 | import android.os.Handler; |
| 36 | import android.os.RemoteException; |
Jaewan Kim | c64b0ee | 2016-03-04 18:20:53 +0900 | [diff] [blame] | 37 | import android.os.SystemProperties; |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 38 | import android.util.Log; |
| 39 | |
Jaewan Kim | 977dcdc | 2016-01-20 19:21:08 +0900 | [diff] [blame] | 40 | import com.android.systemui.Prefs; |
Jaewan Kim | 8f584b8 | 2016-03-22 22:16:59 +0900 | [diff] [blame] | 41 | import com.android.systemui.R; |
Jaewan Kim | 938a50b | 2016-03-14 17:35:43 +0900 | [diff] [blame] | 42 | import com.android.systemui.recents.misc.SystemServicesProxy; |
| 43 | import com.android.systemui.recents.misc.SystemServicesProxy.TaskStackListener; |
Jaewan Kim | 977dcdc | 2016-01-20 19:21:08 +0900 | [diff] [blame] | 44 | |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 45 | import java.util.ArrayList; |
| 46 | import java.util.List; |
| 47 | |
| 48 | import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID; |
| 49 | import static android.app.ActivityManager.StackId.PINNED_STACK_ID; |
Jaewan Kim | 977dcdc | 2016-01-20 19:21:08 +0900 | [diff] [blame] | 50 | import static com.android.systemui.Prefs.Key.TV_PICTURE_IN_PICTURE_ONBOARDING_SHOWN; |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 51 | |
| 52 | /** |
| 53 | * Manages the picture-in-picture (PIP) UI and states. |
| 54 | */ |
| 55 | public class PipManager { |
| 56 | private static final String TAG = "PipManager"; |
| 57 | private static final boolean DEBUG = false; |
Jaewan Kim | c64b0ee | 2016-03-04 18:20:53 +0900 | [diff] [blame] | 58 | private static final boolean DEBUG_FORCE_ONBOARDING = |
| 59 | SystemProperties.getBoolean("debug.tv.pip_force_onboarding", false); |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 60 | |
| 61 | private static PipManager sPipManager; |
| 62 | |
Youngsang Cho | ad8ceb0 | 2016-01-15 16:59:27 -0800 | [diff] [blame] | 63 | private static final int MAX_RUNNING_TASKS_COUNT = 10; |
| 64 | |
Wale Ogunwale | 480dca0 | 2016-02-06 13:58:29 -0800 | [diff] [blame] | 65 | public static final int STATE_NO_PIP = 0; |
| 66 | public static final int STATE_PIP_OVERLAY = 1; |
| 67 | public static final int STATE_PIP_MENU = 2; |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 68 | |
Youngsang Cho | ad8ceb0 | 2016-01-15 16:59:27 -0800 | [diff] [blame] | 69 | private static final int TASK_ID_NO_PIP = -1; |
| 70 | private static final int INVALID_RESOURCE_TYPE = -1; |
| 71 | |
Wale Ogunwale | 480dca0 | 2016-02-06 13:58:29 -0800 | [diff] [blame] | 72 | public static final int SUSPEND_PIP_RESIZE_REASON_WAITING_FOR_MENU_ACTIVITY_FINISH = 0x1; |
| 73 | 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] | 74 | |
Jaewan Kim | 8f584b8 | 2016-03-22 22:16:59 +0900 | [diff] [blame] | 75 | /** |
| 76 | * PIPed activity is playing a media and it can be paused. |
| 77 | */ |
| 78 | static final int PLAYBACK_STATE_PLAYING = 0; |
| 79 | /** |
| 80 | * PIPed activity has a paused media and it can be played. |
| 81 | */ |
| 82 | static final int PLAYBACK_STATE_PAUSED = 1; |
| 83 | /** |
| 84 | * Users are unable to control PIPed activity's media playback. |
| 85 | */ |
| 86 | static final int PLAYBACK_STATE_UNAVAILABLE = 2; |
| 87 | |
Jaewan Kim | f40fcdc | 2016-03-04 17:58:22 +0900 | [diff] [blame] | 88 | private static final int CLOSE_PIP_WHEN_MEDIA_SESSION_GONE_TIMEOUT_MS = 3000; |
| 89 | |
Wale Ogunwale | 480dca0 | 2016-02-06 13:58:29 -0800 | [diff] [blame] | 90 | private int mSuspendPipResizingReason; |
| 91 | |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 92 | private Context mContext; |
| 93 | private IActivityManager mActivityManager; |
Jaewan Kim | 6233819 | 2016-02-25 10:00:05 -0800 | [diff] [blame] | 94 | private MediaSessionManager mMediaSessionManager; |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 95 | private int mState = STATE_NO_PIP; |
| 96 | private final Handler mHandler = new Handler(); |
| 97 | private List<Listener> mListeners = new ArrayList<>(); |
Jaewan Kim | c92a7d1 | 2016-02-15 17:33:25 -0800 | [diff] [blame] | 98 | private Rect mCurrentPipBounds; |
| 99 | private Rect mPipBounds; |
| 100 | private Rect mMenuModePipBounds; |
| 101 | private Rect mRecentsPipBounds; |
| 102 | private Rect mRecentsFocusedPipBounds; |
Jaewan Kim | 8f584b8 | 2016-03-22 22:16:59 +0900 | [diff] [blame] | 103 | private int mRecentsFocusChangedAnimationDurationMs; |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 104 | private boolean mInitialized; |
Youngsang Cho | ad8ceb0 | 2016-01-15 16:59:27 -0800 | [diff] [blame] | 105 | private int mPipTaskId = TASK_ID_NO_PIP; |
Jaewan Kim | 6233819 | 2016-02-25 10:00:05 -0800 | [diff] [blame] | 106 | private ComponentName mPipComponentName; |
| 107 | private MediaController mPipMediaController; |
Jaewan Kim | 977dcdc | 2016-01-20 19:21:08 +0900 | [diff] [blame] | 108 | private boolean mOnboardingShown; |
Youngsang Cho | ad8ceb0 | 2016-01-15 16:59:27 -0800 | [diff] [blame] | 109 | |
Jaewan Kim | c92a7d1 | 2016-02-15 17:33:25 -0800 | [diff] [blame] | 110 | private boolean mIsRecentsShown; |
| 111 | private boolean mIsPipFocusedInRecent; |
| 112 | |
Wale Ogunwale | 480dca0 | 2016-02-06 13:58:29 -0800 | [diff] [blame] | 113 | private final Runnable mResizePinnedStackRunnable = new Runnable() { |
| 114 | @Override |
| 115 | public void run() { |
| 116 | resizePinnedStack(mState); |
| 117 | } |
| 118 | }; |
Jaewan Kim | f40fcdc | 2016-03-04 17:58:22 +0900 | [diff] [blame] | 119 | private final Runnable mClosePipRunnable = new Runnable() { |
| 120 | @Override |
| 121 | public void run() { |
| 122 | closePip(); |
| 123 | } |
| 124 | }; |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 125 | |
Youngsang Cho | ad8ceb0 | 2016-01-15 16:59:27 -0800 | [diff] [blame] | 126 | private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 127 | @Override |
| 128 | public void onReceive(Context context, Intent intent) { |
Youngsang Cho | ad8ceb0 | 2016-01-15 16:59:27 -0800 | [diff] [blame] | 129 | String action = intent.getAction(); |
Jaewan Kim | c552b04 | 2016-01-18 16:08:45 +0900 | [diff] [blame] | 130 | if (Intent.ACTION_MEDIA_RESOURCE_GRANTED.equals(action)) { |
Youngsang Cho | ad8ceb0 | 2016-01-15 16:59:27 -0800 | [diff] [blame] | 131 | String[] packageNames = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES); |
| 132 | int resourceType = intent.getIntExtra(Intent.EXTRA_MEDIA_RESOURCE_TYPE, |
| 133 | INVALID_RESOURCE_TYPE); |
| 134 | if (mState != STATE_NO_PIP && packageNames != null && packageNames.length > 0 |
| 135 | && resourceType == Intent.EXTRA_MEDIA_RESOURCE_TYPE_VIDEO_CODEC) { |
| 136 | handleMediaResourceGranted(packageNames); |
| 137 | } |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 138 | } |
Youngsang Cho | ad8ceb0 | 2016-01-15 16:59:27 -0800 | [diff] [blame] | 139 | |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 140 | } |
| 141 | }; |
Jaewan Kim | 6233819 | 2016-02-25 10:00:05 -0800 | [diff] [blame] | 142 | private final MediaSessionManager.OnActiveSessionsChangedListener mActiveMediaSessionListener = |
| 143 | new MediaSessionManager.OnActiveSessionsChangedListener() { |
| 144 | @Override |
| 145 | public void onActiveSessionsChanged(List<MediaController> controllers) { |
| 146 | updateMediaController(controllers); |
| 147 | } |
| 148 | }; |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 149 | |
| 150 | private PipManager() { } |
| 151 | |
| 152 | /** |
| 153 | * Initializes {@link PipManager}. |
| 154 | */ |
| 155 | public void initialize(Context context) { |
| 156 | if (mInitialized) { |
| 157 | return; |
| 158 | } |
| 159 | mInitialized = true; |
| 160 | mContext = context; |
| 161 | Resources res = context.getResources(); |
Jaewan Kim | c92a7d1 | 2016-02-15 17:33:25 -0800 | [diff] [blame] | 162 | mPipBounds = Rect.unflattenFromString(res.getString( |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 163 | com.android.internal.R.string.config_defaultPictureInPictureBounds)); |
Jaewan Kim | c92a7d1 | 2016-02-15 17:33:25 -0800 | [diff] [blame] | 164 | mMenuModePipBounds = Rect.unflattenFromString(res.getString( |
Jaewan Kim | 8f584b8 | 2016-03-22 22:16:59 +0900 | [diff] [blame] | 165 | R.string.pip_menu_bounds)); |
Jaewan Kim | c92a7d1 | 2016-02-15 17:33:25 -0800 | [diff] [blame] | 166 | mRecentsPipBounds = Rect.unflattenFromString(res.getString( |
Jaewan Kim | 8f584b8 | 2016-03-22 22:16:59 +0900 | [diff] [blame] | 167 | R.string.pip_recents_bounds)); |
| 168 | mRecentsFocusedPipBounds = Rect.unflattenFromString(res.getString( |
| 169 | R.string.pip_recents_focused_bounds)); |
| 170 | mRecentsFocusChangedAnimationDurationMs = res.getInteger( |
| 171 | R.integer.recents_tv_pip_focus_anim_duration); |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 172 | |
| 173 | mActivityManager = ActivityManagerNative.getDefault(); |
Jaewan Kim | 938a50b | 2016-03-14 17:35:43 +0900 | [diff] [blame] | 174 | SystemServicesProxy.getInstance(context).registerTaskStackListener(mTaskStackListener); |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 175 | IntentFilter intentFilter = new IntentFilter(); |
Youngsang Cho | ad8ceb0 | 2016-01-15 16:59:27 -0800 | [diff] [blame] | 176 | intentFilter.addAction(Intent.ACTION_MEDIA_RESOURCE_GRANTED); |
| 177 | mContext.registerReceiver(mBroadcastReceiver, intentFilter); |
Jaewan Kim | 977dcdc | 2016-01-20 19:21:08 +0900 | [diff] [blame] | 178 | mOnboardingShown = Prefs.getBoolean( |
| 179 | mContext, TV_PICTURE_IN_PICTURE_ONBOARDING_SHOWN, false); |
Jaewan Kim | 6233819 | 2016-02-25 10:00:05 -0800 | [diff] [blame] | 180 | |
| 181 | mMediaSessionManager = |
| 182 | (MediaSessionManager) mContext.getSystemService(Context.MEDIA_SESSION_SERVICE); |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 183 | } |
| 184 | |
Jaewan Kim | c552b04 | 2016-01-18 16:08:45 +0900 | [diff] [blame] | 185 | /** |
| 186 | * Request PIP. |
| 187 | * It could either start PIP if there's none, and show PIP menu otherwise. |
| 188 | */ |
| 189 | public void requestTvPictureInPicture() { |
| 190 | if (DEBUG) Log.d(TAG, "requestTvPictureInPicture()"); |
Jaewan Kim | c92a7d1 | 2016-02-15 17:33:25 -0800 | [diff] [blame] | 191 | if (!isPipShown()) { |
Jaewan Kim | c552b04 | 2016-01-18 16:08:45 +0900 | [diff] [blame] | 192 | startPip(); |
| 193 | } else if (mState == STATE_PIP_OVERLAY) { |
Wale Ogunwale | 480dca0 | 2016-02-06 13:58:29 -0800 | [diff] [blame] | 194 | resizePinnedStack(STATE_PIP_MENU); |
Jaewan Kim | c552b04 | 2016-01-18 16:08:45 +0900 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 198 | private void startPip() { |
| 199 | try { |
Jaewan Kim | c92a7d1 | 2016-02-15 17:33:25 -0800 | [diff] [blame] | 200 | mActivityManager.moveTopActivityToPinnedStack(FULLSCREEN_WORKSPACE_STACK_ID, mPipBounds); |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 201 | } catch (RemoteException|IllegalArgumentException e) { |
| 202 | Log.e(TAG, "moveTopActivityToPinnedStack failed", e); |
| 203 | } |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | /** |
Jaewan Kim | 977dcdc | 2016-01-20 19:21:08 +0900 | [diff] [blame] | 207 | * Closes PIP (PIPed activity and PIP system UI). |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 208 | */ |
| 209 | public void closePip() { |
Youngsang Cho | 336007b | 2016-02-22 11:17:29 -0800 | [diff] [blame] | 210 | closePipInternal(true); |
Youngsang Cho | 23df699 | 2016-01-26 17:51:33 -0800 | [diff] [blame] | 211 | } |
| 212 | |
Youngsang Cho | 336007b | 2016-02-22 11:17:29 -0800 | [diff] [blame] | 213 | private void closePipInternal(boolean removePipStack) { |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 214 | mState = STATE_NO_PIP; |
Youngsang Cho | ad8ceb0 | 2016-01-15 16:59:27 -0800 | [diff] [blame] | 215 | mPipTaskId = TASK_ID_NO_PIP; |
Jaewan Kim | 6233819 | 2016-02-25 10:00:05 -0800 | [diff] [blame] | 216 | mPipMediaController = null; |
| 217 | mMediaSessionManager.removeOnActiveSessionsChangedListener(mActiveMediaSessionListener); |
Youngsang Cho | 23df699 | 2016-01-26 17:51:33 -0800 | [diff] [blame] | 218 | if (removePipStack) { |
| 219 | try { |
| 220 | mActivityManager.removeStack(PINNED_STACK_ID); |
| 221 | } catch (RemoteException e) { |
| 222 | Log.e(TAG, "removeStack failed", e); |
| 223 | } |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 224 | } |
Jaewan Kim | c92a7d1 | 2016-02-15 17:33:25 -0800 | [diff] [blame] | 225 | for (int i = mListeners.size() - 1; i >= 0; --i) { |
| 226 | mListeners.get(i).onPipActivityClosed(); |
| 227 | } |
Jaewan Kim | f40fcdc | 2016-03-04 17:58:22 +0900 | [diff] [blame] | 228 | mHandler.removeCallbacks(mClosePipRunnable); |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | /** |
Jaewan Kim | 977dcdc | 2016-01-20 19:21:08 +0900 | [diff] [blame] | 232 | * Moves the PIPed activity to the fullscreen and closes PIP system UI. |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 233 | */ |
| 234 | public void movePipToFullscreen() { |
| 235 | mState = STATE_NO_PIP; |
Youngsang Cho | ad8ceb0 | 2016-01-15 16:59:27 -0800 | [diff] [blame] | 236 | mPipTaskId = TASK_ID_NO_PIP; |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 237 | for (int i = mListeners.size() - 1; i >= 0; --i) { |
| 238 | mListeners.get(i).onMoveToFullscreen(); |
| 239 | } |
Wale Ogunwale | 480dca0 | 2016-02-06 13:58:29 -0800 | [diff] [blame] | 240 | resizePinnedStack(mState); |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Shows PIP overlay UI by launching {@link PipOverlayActivity}. It also locates the pinned |
| 245 | * stack to the default PIP bound {@link com.android.internal.R.string |
| 246 | * .config_defaultPictureInPictureBounds}. |
| 247 | */ |
Wale Ogunwale | 480dca0 | 2016-02-06 13:58:29 -0800 | [diff] [blame] | 248 | private void showPipOverlay() { |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 249 | if (DEBUG) Log.d(TAG, "showPipOverlay()"); |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 250 | mState = STATE_PIP_OVERLAY; |
Jaewan Kim | 21e0421 | 2016-03-21 14:22:33 +0900 | [diff] [blame] | 251 | PipOverlayActivity.showPipOverlay(mContext); |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | /** |
Wale Ogunwale | 480dca0 | 2016-02-06 13:58:29 -0800 | [diff] [blame] | 255 | * Suspends resizing operation on the Pip until {@link #resumePipResizing} is called |
| 256 | * @param reason The reason for suspending resizing operations on the Pip. |
| 257 | */ |
| 258 | public void suspendPipResizing(int reason) { |
| 259 | if (DEBUG) Log.d(TAG, |
| 260 | "suspendPipResizing() reason=" + reason + " callers=" + Debug.getCallers(2)); |
| 261 | mSuspendPipResizingReason |= reason; |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Resumes resizing operation on the Pip that was previously suspended. |
| 266 | * @param reason The reason resizing operations on the Pip was suspended. |
| 267 | */ |
| 268 | public void resumePipResizing(int reason) { |
| 269 | if ((mSuspendPipResizingReason & reason) == 0) { |
| 270 | return; |
| 271 | } |
| 272 | if (DEBUG) Log.d(TAG, |
| 273 | "resumePipResizing() reason=" + reason + " callers=" + Debug.getCallers(2)); |
| 274 | mSuspendPipResizingReason &= ~reason; |
| 275 | mHandler.post(mResizePinnedStackRunnable); |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Resize the Pip to the appropriate size for the input state. |
| 280 | * @param state In Pip state also used to determine the new size for the Pip. |
| 281 | */ |
| 282 | public void resizePinnedStack(int state) { |
| 283 | if (DEBUG) Log.d(TAG, "resizePinnedStack() state=" + state); |
| 284 | mState = state; |
Wale Ogunwale | 480dca0 | 2016-02-06 13:58:29 -0800 | [diff] [blame] | 285 | for (int i = mListeners.size() - 1; i >= 0; --i) { |
| 286 | mListeners.get(i).onPipResizeAboutToStart(); |
| 287 | } |
Wale Ogunwale | 480dca0 | 2016-02-06 13:58:29 -0800 | [diff] [blame] | 288 | if (mSuspendPipResizingReason != 0) { |
| 289 | if (DEBUG) Log.d(TAG, |
| 290 | "resizePinnedStack() deferring mSuspendPipResizingReason=" + |
| 291 | mSuspendPipResizingReason); |
| 292 | return; |
| 293 | } |
Jaewan Kim | 8f584b8 | 2016-03-22 22:16:59 +0900 | [diff] [blame] | 294 | int animationDurationMs = -1; |
Jaewan Kim | c92a7d1 | 2016-02-15 17:33:25 -0800 | [diff] [blame] | 295 | switch (mState) { |
| 296 | case STATE_NO_PIP: |
| 297 | mCurrentPipBounds = null; |
| 298 | break; |
| 299 | case STATE_PIP_MENU: |
| 300 | mCurrentPipBounds = mMenuModePipBounds; |
| 301 | break; |
| 302 | case STATE_PIP_OVERLAY: |
| 303 | if (mIsRecentsShown) { |
Jaewan Kim | 8f584b8 | 2016-03-22 22:16:59 +0900 | [diff] [blame] | 304 | if (mCurrentPipBounds == mRecentsFocusedPipBounds |
| 305 | || mCurrentPipBounds == mRecentsFocusedPipBounds) { |
| 306 | animationDurationMs = mRecentsFocusChangedAnimationDurationMs; |
| 307 | } |
Jaewan Kim | c92a7d1 | 2016-02-15 17:33:25 -0800 | [diff] [blame] | 308 | if (mIsPipFocusedInRecent) { |
| 309 | mCurrentPipBounds = mRecentsFocusedPipBounds; |
| 310 | } else { |
| 311 | mCurrentPipBounds = mRecentsPipBounds; |
| 312 | } |
| 313 | } else { |
| 314 | mCurrentPipBounds = mPipBounds; |
| 315 | } |
| 316 | break; |
| 317 | default: |
| 318 | mCurrentPipBounds = mPipBounds; |
| 319 | break; |
| 320 | } |
Wale Ogunwale | 480dca0 | 2016-02-06 13:58:29 -0800 | [diff] [blame] | 321 | try { |
Jaewan Kim | 8f584b8 | 2016-03-22 22:16:59 +0900 | [diff] [blame] | 322 | mActivityManager.resizeStack(PINNED_STACK_ID, mCurrentPipBounds, |
| 323 | true, true, true, animationDurationMs); |
Wale Ogunwale | 480dca0 | 2016-02-06 13:58:29 -0800 | [diff] [blame] | 324 | } catch (RemoteException e) { |
Jaewan Kim | 82ac50d | 2016-03-21 17:34:28 +0900 | [diff] [blame] | 325 | Log.e(TAG, "resizeStack failed", e); |
Wale Ogunwale | 480dca0 | 2016-02-06 13:58:29 -0800 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | |
| 329 | /** |
Jaewan Kim | c92a7d1 | 2016-02-15 17:33:25 -0800 | [diff] [blame] | 330 | * Returns the current PIP bound for activities to sync their UI with PIP. |
| 331 | */ |
| 332 | public Rect getPipBounds() { |
| 333 | return mCurrentPipBounds; |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * Called when Recents is started. |
| 338 | * PIPed activity will be resized accordingly and overlay will show available buttons. |
| 339 | */ |
| 340 | public void onRecentsStarted() { |
| 341 | mIsRecentsShown = true; |
| 342 | mIsPipFocusedInRecent = false; |
| 343 | if (mState == STATE_NO_PIP) { |
| 344 | return; |
| 345 | } |
| 346 | resizePinnedStack(STATE_PIP_OVERLAY); |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * Called when Recents is stopped. |
| 351 | * PIPed activity will be resized accordingly and overlay will hide available buttons. |
| 352 | */ |
| 353 | public void onRecentsStopped() { |
| 354 | mIsRecentsShown = false; |
| 355 | mIsPipFocusedInRecent = false; |
| 356 | if (mState == STATE_NO_PIP) { |
| 357 | return; |
| 358 | } |
| 359 | resizePinnedStack(STATE_PIP_OVERLAY); |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * Returns {@code true} if recents is shown. |
| 364 | */ |
| 365 | boolean isRecentsShown() { |
| 366 | return mIsRecentsShown; |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * Called when the PIP view in {@link com.android.systemui.recents.tv.RecentsTvActivity} |
| 371 | * is focused. |
| 372 | * This only resizes pinned stack so it looks like it's in Recents. |
| 373 | * This should be called only by {@link com.android.systemui.recents.tv.RecentsTvActivity}. |
| 374 | */ |
| 375 | public void onPipViewFocusChangedInRecents(boolean hasFocus) { |
| 376 | mIsPipFocusedInRecent = hasFocus; |
| 377 | if (mState != STATE_PIP_OVERLAY) { |
| 378 | Log.w(TAG, "There is no pinned stack to handle focus change."); |
| 379 | return; |
| 380 | } |
| 381 | resizePinnedStack(STATE_PIP_OVERLAY); |
| 382 | } |
| 383 | |
| 384 | /** |
Jaewan Kim | 8f584b8 | 2016-03-22 22:16:59 +0900 | [diff] [blame] | 385 | * Returns {@code true} if the PIP view in |
| 386 | * {@link com.android.systemui.recents.tv.RecentsTvActivity} is focused in Recents. |
| 387 | * This API is valid only when {@link isRecentsShown()} returns {@code true}. |
| 388 | */ |
| 389 | boolean isPipViewFocusdInRecents() { |
| 390 | return mIsPipFocusedInRecent; |
| 391 | } |
| 392 | |
| 393 | /** |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 394 | * 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] | 395 | * stack to the centered PIP bound {@link R.config_centeredPictureInPictureBounds}. |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 396 | */ |
Wale Ogunwale | 480dca0 | 2016-02-06 13:58:29 -0800 | [diff] [blame] | 397 | private void showPipMenu() { |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 398 | if (DEBUG) Log.d(TAG, "showPipMenu()"); |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 399 | mState = STATE_PIP_MENU; |
| 400 | for (int i = mListeners.size() - 1; i >= 0; --i) { |
| 401 | mListeners.get(i).onShowPipMenu(); |
| 402 | } |
| 403 | Intent intent = new Intent(mContext, PipMenuActivity.class); |
| 404 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
Jaewan Kim | 1a9dc56 | 2016-02-17 13:41:51 -0800 | [diff] [blame] | 405 | mContext.startActivity(intent); |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 406 | } |
| 407 | |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 408 | public void addListener(Listener listener) { |
| 409 | mListeners.add(listener); |
| 410 | } |
| 411 | |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 412 | public void removeListener(Listener listener) { |
| 413 | mListeners.remove(listener); |
| 414 | } |
| 415 | |
Jaewan Kim | 977dcdc | 2016-01-20 19:21:08 +0900 | [diff] [blame] | 416 | private void launchPipOnboardingActivityIfNeeded() { |
| 417 | if (DEBUG_FORCE_ONBOARDING || !mOnboardingShown) { |
| 418 | mOnboardingShown = true; |
| 419 | Prefs.putBoolean(mContext, TV_PICTURE_IN_PICTURE_ONBOARDING_SHOWN, true); |
| 420 | |
| 421 | Intent intent = new Intent(mContext, PipOnboardingActivity.class); |
| 422 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 423 | mContext.startActivity(intent); |
| 424 | } |
| 425 | } |
| 426 | |
Jaewan Kim | c92a7d1 | 2016-02-15 17:33:25 -0800 | [diff] [blame] | 427 | /** |
| 428 | * Returns {@code true} if PIP is shown. |
| 429 | */ |
| 430 | public boolean isPipShown() { |
Jaewan Kim | 82ac50d | 2016-03-21 17:34:28 +0900 | [diff] [blame] | 431 | return mState != STATE_NO_PIP; |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 432 | } |
| 433 | |
Youngsang Cho | ad8ceb0 | 2016-01-15 16:59:27 -0800 | [diff] [blame] | 434 | private void handleMediaResourceGranted(String[] packageNames) { |
| 435 | StackInfo fullscreenStack = null; |
| 436 | try { |
| 437 | fullscreenStack = mActivityManager.getStackInfo(FULLSCREEN_WORKSPACE_STACK_ID); |
| 438 | } catch (RemoteException e) { |
| 439 | Log.e(TAG, "getStackInfo failed", e); |
| 440 | } |
| 441 | if (fullscreenStack == null) { |
| 442 | return; |
| 443 | } |
| 444 | int fullscreenTopTaskId = fullscreenStack.taskIds[fullscreenStack.taskIds.length - 1]; |
| 445 | List<RunningTaskInfo> tasks = null; |
| 446 | try { |
| 447 | tasks = mActivityManager.getTasks(MAX_RUNNING_TASKS_COUNT, 0); |
| 448 | } catch (RemoteException e) { |
| 449 | Log.e(TAG, "getTasks failed", e); |
| 450 | } |
| 451 | if (tasks == null) { |
| 452 | return; |
| 453 | } |
| 454 | boolean wasGrantedInFullscreen = false; |
| 455 | boolean wasGrantedInPip = false; |
| 456 | for (int i = tasks.size() - 1; i >= 0; --i) { |
| 457 | RunningTaskInfo task = tasks.get(i); |
| 458 | for (int j = packageNames.length - 1; j >= 0; --j) { |
| 459 | if (task.topActivity.getPackageName().equals(packageNames[j])) { |
| 460 | if (task.id == fullscreenTopTaskId) { |
| 461 | wasGrantedInFullscreen = true; |
| 462 | } else if (task.id == mPipTaskId) { |
| 463 | wasGrantedInPip= true; |
| 464 | } |
| 465 | } |
| 466 | } |
| 467 | } |
| 468 | if (wasGrantedInFullscreen && !wasGrantedInPip) { |
| 469 | closePip(); |
| 470 | } |
| 471 | } |
| 472 | |
Jaewan Kim | 6233819 | 2016-02-25 10:00:05 -0800 | [diff] [blame] | 473 | private void updateMediaController(List<MediaController> controllers) { |
| 474 | MediaController mediaController = null; |
| 475 | if (controllers != null && mState != STATE_NO_PIP && mPipComponentName != null) { |
| 476 | for (int i = controllers.size() - 1; i >= 0; i--) { |
| 477 | MediaController controller = controllers.get(i); |
| 478 | // We assumes that an app with PIPable activity |
| 479 | // keeps the single instance of media controller especially when PIP is on. |
| 480 | if (controller.getPackageName().equals(mPipComponentName.getPackageName())) { |
| 481 | mediaController = controller; |
| 482 | break; |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | if (mPipMediaController != mediaController) { |
| 487 | mPipMediaController = mediaController; |
| 488 | for (int i = mListeners.size() - 1; i >= 0; i--) { |
| 489 | mListeners.get(i).onMediaControllerChanged(); |
| 490 | } |
Jaewan Kim | f40fcdc | 2016-03-04 17:58:22 +0900 | [diff] [blame] | 491 | if (mPipMediaController == null) { |
| 492 | mHandler.postDelayed(mClosePipRunnable, |
| 493 | CLOSE_PIP_WHEN_MEDIA_SESSION_GONE_TIMEOUT_MS); |
| 494 | } else { |
| 495 | mHandler.removeCallbacks(mClosePipRunnable); |
| 496 | } |
Jaewan Kim | 6233819 | 2016-02-25 10:00:05 -0800 | [diff] [blame] | 497 | } |
| 498 | } |
| 499 | |
| 500 | /** |
| 501 | * Gets the {@link android.media.session.MediaController} for the PIPed activity. |
| 502 | */ |
| 503 | MediaController getMediaController() { |
| 504 | return mPipMediaController; |
| 505 | } |
| 506 | |
Jaewan Kim | 8f584b8 | 2016-03-22 22:16:59 +0900 | [diff] [blame] | 507 | /** |
| 508 | * Returns the PIPed activity's playback state. |
| 509 | * This returns one of {@link PLAYBACK_STATE_PLAYING}, {@link PLAYBACK_STATE_PAUSED}, |
| 510 | * or {@link PLAYBACK_STATE_UNAVAILABLE}. |
| 511 | */ |
| 512 | int getPlaybackState() { |
| 513 | if (mPipMediaController == null || mPipMediaController.getPlaybackState() == null) { |
| 514 | return PLAYBACK_STATE_UNAVAILABLE; |
| 515 | } |
| 516 | int state = mPipMediaController.getPlaybackState().getState(); |
| 517 | boolean isPlaying = (state == PlaybackState.STATE_BUFFERING |
| 518 | || state == PlaybackState.STATE_CONNECTING |
| 519 | || state == PlaybackState.STATE_PLAYING |
| 520 | || state == PlaybackState.STATE_FAST_FORWARDING |
| 521 | || state == PlaybackState.STATE_REWINDING |
| 522 | || state == PlaybackState.STATE_SKIPPING_TO_PREVIOUS |
| 523 | || state == PlaybackState.STATE_SKIPPING_TO_NEXT); |
| 524 | long actions = mPipMediaController.getPlaybackState().getActions(); |
| 525 | if (!isPlaying && ((actions & PlaybackState.ACTION_PLAY) != 0)) { |
| 526 | return PLAYBACK_STATE_PAUSED; |
| 527 | } else if (isPlaying && ((actions & PlaybackState.ACTION_PAUSE) != 0)) { |
| 528 | return PLAYBACK_STATE_PLAYING; |
| 529 | } |
| 530 | return PLAYBACK_STATE_UNAVAILABLE; |
| 531 | } |
| 532 | |
Jaewan Kim | 938a50b | 2016-03-14 17:35:43 +0900 | [diff] [blame] | 533 | TaskStackListener mTaskStackListener = new TaskStackListener() { |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 534 | @Override |
Jaewan Kim | 938a50b | 2016-03-14 17:35:43 +0900 | [diff] [blame] | 535 | public void onTaskStackChanged() { |
| 536 | if (mState != STATE_NO_PIP) { |
| 537 | StackInfo stackInfo = null; |
| 538 | try { |
| 539 | stackInfo = mActivityManager.getStackInfo(PINNED_STACK_ID); |
| 540 | if (stackInfo == null) { |
| 541 | Log.w(TAG, "There is no pinned stack"); |
| 542 | closePipInternal(false); |
| 543 | return; |
| 544 | } |
| 545 | } catch (RemoteException e) { |
| 546 | Log.e(TAG, "getStackInfo failed", e); |
| 547 | return; |
| 548 | } |
| 549 | for (int i = stackInfo.taskIds.length - 1; i >= 0; --i) { |
| 550 | if (stackInfo.taskIds[i] == mPipTaskId) { |
| 551 | // PIP task is still alive. |
| 552 | return; |
| 553 | } |
| 554 | } |
| 555 | // PIP task doesn't exist anymore in PINNED_STACK. |
| 556 | closePipInternal(true); |
| 557 | } |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | @Override |
Jaewan Kim | 938a50b | 2016-03-14 17:35:43 +0900 | [diff] [blame] | 561 | public void onActivityPinned() { |
Wale Ogunwale | 480dca0 | 2016-02-06 13:58:29 -0800 | [diff] [blame] | 562 | if (DEBUG) Log.d(TAG, "onActivityPinned()"); |
Jaewan Kim | 938a50b | 2016-03-14 17:35:43 +0900 | [diff] [blame] | 563 | StackInfo stackInfo = null; |
| 564 | try { |
| 565 | stackInfo = mActivityManager.getStackInfo(PINNED_STACK_ID); |
| 566 | if (stackInfo == null) { |
| 567 | Log.w(TAG, "Cannot find pinned stack"); |
| 568 | return; |
| 569 | } |
| 570 | } catch (RemoteException e) { |
| 571 | Log.e(TAG, "getStackInfo failed", e); |
| 572 | return; |
| 573 | } |
| 574 | if (DEBUG) Log.d(TAG, "PINNED_STACK:" + stackInfo); |
| 575 | mPipTaskId = stackInfo.taskIds[stackInfo.taskIds.length - 1]; |
| 576 | mPipComponentName = ComponentName.unflattenFromString( |
| 577 | stackInfo.taskNames[stackInfo.taskNames.length - 1]); |
| 578 | // Set state to overlay so we show it when the pinned stack animation ends. |
| 579 | mState = STATE_PIP_OVERLAY; |
| 580 | mCurrentPipBounds = mPipBounds; |
| 581 | launchPipOnboardingActivityIfNeeded(); |
| 582 | mMediaSessionManager.addOnActiveSessionsChangedListener( |
| 583 | mActiveMediaSessionListener, null); |
| 584 | updateMediaController(mMediaSessionManager.getActiveSessions(null)); |
| 585 | if (mIsRecentsShown) { |
| 586 | // If an activity becomes PIPed again after the fullscreen, the Recents is shown |
| 587 | // behind so we need to resize the pinned stack and show the correct overlay. |
| 588 | resizePinnedStack(STATE_PIP_OVERLAY); |
| 589 | } |
| 590 | for (int i = mListeners.size() - 1; i >= 0; i--) { |
| 591 | mListeners.get(i).onPipEntered(); |
| 592 | } |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 593 | } |
Wale Ogunwale | cc25a8a | 2016-01-23 14:31:37 -0800 | [diff] [blame] | 594 | |
| 595 | @Override |
| 596 | public void onPinnedActivityRestartAttempt() { |
Wale Ogunwale | 480dca0 | 2016-02-06 13:58:29 -0800 | [diff] [blame] | 597 | if (DEBUG) Log.d(TAG, "onPinnedActivityRestartAttempt()"); |
Jaewan Kim | 938a50b | 2016-03-14 17:35:43 +0900 | [diff] [blame] | 598 | // If PIPed activity is launched again by Launcher or intent, make it fullscreen. |
| 599 | movePipToFullscreen(); |
Wale Ogunwale | cc25a8a | 2016-01-23 14:31:37 -0800 | [diff] [blame] | 600 | } |
Wale Ogunwale | 480dca0 | 2016-02-06 13:58:29 -0800 | [diff] [blame] | 601 | |
| 602 | @Override |
| 603 | public void onPinnedStackAnimationEnded() { |
| 604 | if (DEBUG) Log.d(TAG, "onPinnedStackAnimationEnded()"); |
Jaewan Kim | 938a50b | 2016-03-14 17:35:43 +0900 | [diff] [blame] | 605 | switch (mState) { |
| 606 | case STATE_PIP_OVERLAY: |
| 607 | showPipOverlay(); |
| 608 | break; |
| 609 | case STATE_PIP_MENU: |
| 610 | showPipMenu(); |
| 611 | break; |
| 612 | } |
Wale Ogunwale | 480dca0 | 2016-02-06 13:58:29 -0800 | [diff] [blame] | 613 | } |
Jaewan Kim | 938a50b | 2016-03-14 17:35:43 +0900 | [diff] [blame] | 614 | }; |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 615 | |
| 616 | /** |
| 617 | * A listener interface to receive notification on changes in PIP. |
| 618 | */ |
| 619 | public interface Listener { |
Jaewan Kim | 82ac50d | 2016-03-21 17:34:28 +0900 | [diff] [blame] | 620 | /** |
| 621 | * Invoked when an activity is pinned and PIP manager is set corresponding information. |
| 622 | * Classes must use this instead of {@link android.app.ITaskStackListener.onActivityPinned} |
| 623 | * because there's no guarantee for the PIP manager be return relavent information |
| 624 | * correctly. (e.g. {@link isPipShown}, {@link getPipBounds}) |
| 625 | */ |
| 626 | void onPipEntered(); |
Wale Ogunwale | 480dca0 | 2016-02-06 13:58:29 -0800 | [diff] [blame] | 627 | /** Invoked when a PIPed activity is closed. */ |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 628 | void onPipActivityClosed(); |
Wale Ogunwale | 480dca0 | 2016-02-06 13:58:29 -0800 | [diff] [blame] | 629 | /** Invoked when the PIP menu gets shown. */ |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 630 | void onShowPipMenu(); |
Wale Ogunwale | 480dca0 | 2016-02-06 13:58:29 -0800 | [diff] [blame] | 631 | /** Invoked when the PIPed activity is returned back to the fullscreen. */ |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 632 | void onMoveToFullscreen(); |
Wale Ogunwale | 480dca0 | 2016-02-06 13:58:29 -0800 | [diff] [blame] | 633 | /** Invoked when we are above to start resizing the Pip. */ |
| 634 | void onPipResizeAboutToStart(); |
Jaewan Kim | 6233819 | 2016-02-25 10:00:05 -0800 | [diff] [blame] | 635 | /** Invoked when the MediaController on PIPed activity is changed. */ |
| 636 | void onMediaControllerChanged(); |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | /** |
| 640 | * Gets an instance of {@link PipManager}. |
| 641 | */ |
| 642 | public static PipManager getInstance() { |
| 643 | if (sPipManager == null) { |
| 644 | sPipManager = new PipManager(); |
| 645 | } |
| 646 | return sPipManager; |
| 647 | } |
| 648 | } |