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 | |
| 19 | import android.app.Activity; |
Jaewan Kim | 21e0421 | 2016-03-21 14:22:33 +0900 | [diff] [blame] | 20 | import android.app.ActivityOptions; |
| 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 23 | import android.os.Bundle; |
| 24 | import android.os.Handler; |
Wale Ogunwale | 480dca0 | 2016-02-06 13:58:29 -0800 | [diff] [blame] | 25 | import android.view.View; |
Jaewan Kim | 8f584b8 | 2016-03-22 22:16:59 +0900 | [diff] [blame] | 26 | import android.widget.ImageView; |
Jaewan Kim | edd02dc | 2016-02-23 06:16:57 -0800 | [diff] [blame] | 27 | |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 28 | import com.android.systemui.R; |
| 29 | |
Jaewan Kim | 21e0421 | 2016-03-21 14:22:33 +0900 | [diff] [blame] | 30 | import static android.app.ActivityManager.StackId.PINNED_STACK_ID; |
| 31 | |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 32 | /** |
| 33 | * Activity to show an overlay on top of PIP activity to show how to pop up PIP menu. |
| 34 | */ |
| 35 | public class PipOverlayActivity extends Activity implements PipManager.Listener { |
Wale Ogunwale | 480dca0 | 2016-02-06 13:58:29 -0800 | [diff] [blame] | 36 | private static final long SHOW_GUIDE_OVERLAY_VIEW_DURATION_MS = 4000; |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 37 | |
Jaewan Kim | 21e0421 | 2016-03-21 14:22:33 +0900 | [diff] [blame] | 38 | /** |
| 39 | * The single instance of PipOverlayActivity to prevent it from restarting. |
| 40 | * Note that {@link PipManager} moves the PIPed activity to fullscreen if the activity is |
| 41 | * restarted. It's because the activity may be started by the Launcher or an intent again, |
| 42 | * but we don't want do so for the PipOverlayActivity. |
| 43 | */ |
| 44 | private static PipOverlayActivity sPipOverlayActivity; |
| 45 | |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 46 | private final PipManager mPipManager = PipManager.getInstance(); |
| 47 | private final Handler mHandler = new Handler(); |
Wale Ogunwale | 480dca0 | 2016-02-06 13:58:29 -0800 | [diff] [blame] | 48 | private View mGuideOverlayView; |
Jaewan Kim | c92a7d1 | 2016-02-15 17:33:25 -0800 | [diff] [blame] | 49 | private View mGuideButtonsView; |
Jaewan Kim | 8f584b8 | 2016-03-22 22:16:59 +0900 | [diff] [blame] | 50 | private ImageView mGuideButtonPlayPauseImageView; |
Wale Ogunwale | 480dca0 | 2016-02-06 13:58:29 -0800 | [diff] [blame] | 51 | private final Runnable mHideGuideOverlayRunnable = new Runnable() { |
| 52 | public void run() { |
Jaewan Kim | 8f584b8 | 2016-03-22 22:16:59 +0900 | [diff] [blame] | 53 | mGuideOverlayView.setVisibility(View.GONE); |
Wale Ogunwale | 480dca0 | 2016-02-06 13:58:29 -0800 | [diff] [blame] | 54 | } |
| 55 | }; |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 56 | |
Jaewan Kim | 21e0421 | 2016-03-21 14:22:33 +0900 | [diff] [blame] | 57 | /** |
| 58 | * Launches the PIP overlay. This should be only called on the main thread. |
| 59 | */ |
| 60 | public static void showPipOverlay(Context context) { |
| 61 | if (sPipOverlayActivity == null) { |
| 62 | Intent intent = new Intent(context, PipOverlayActivity.class); |
| 63 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 64 | final ActivityOptions options = ActivityOptions.makeBasic(); |
| 65 | options.setLaunchStackId(PINNED_STACK_ID); |
| 66 | context.startActivity(intent, options.toBundle()); |
| 67 | } |
| 68 | } |
| 69 | |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 70 | @Override |
| 71 | protected void onCreate(Bundle bundle) { |
| 72 | super.onCreate(bundle); |
| 73 | setContentView(R.layout.tv_pip_overlay); |
Wale Ogunwale | 480dca0 | 2016-02-06 13:58:29 -0800 | [diff] [blame] | 74 | mGuideOverlayView = findViewById(R.id.guide_overlay); |
Jaewan Kim | c92a7d1 | 2016-02-15 17:33:25 -0800 | [diff] [blame] | 75 | mGuideButtonsView = findViewById(R.id.guide_buttons); |
Jaewan Kim | 8f584b8 | 2016-03-22 22:16:59 +0900 | [diff] [blame] | 76 | mGuideButtonPlayPauseImageView = (ImageView) findViewById(R.id.guide_button_play_pause); |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 77 | mPipManager.addListener(this); |
Jaewan Kim | 21e0421 | 2016-03-21 14:22:33 +0900 | [diff] [blame] | 78 | |
| 79 | sPipOverlayActivity = this; |
Wale Ogunwale | 480dca0 | 2016-02-06 13:58:29 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | @Override |
Wale Ogunwale | ed0f7e2 | 2016-02-17 16:04:58 -0800 | [diff] [blame] | 83 | protected void onResume() { |
| 84 | super.onResume(); |
Jaewan Kim | c92a7d1 | 2016-02-15 17:33:25 -0800 | [diff] [blame] | 85 | // TODO: Implement animation for this |
Jaewan Kim | 8f584b8 | 2016-03-22 22:16:59 +0900 | [diff] [blame] | 86 | if (mPipManager.isRecentsShown()) { |
| 87 | mGuideOverlayView.setVisibility(View.GONE); |
| 88 | if (mPipManager.isPipViewFocusdInRecents()) { |
| 89 | mGuideButtonsView.setVisibility(View.GONE); |
| 90 | } else { |
| 91 | mGuideButtonsView.setVisibility(View.VISIBLE); |
| 92 | updateGuideButtonsView(); |
| 93 | } |
Jaewan Kim | c92a7d1 | 2016-02-15 17:33:25 -0800 | [diff] [blame] | 94 | } else { |
Jaewan Kim | 8f584b8 | 2016-03-22 22:16:59 +0900 | [diff] [blame] | 95 | mGuideOverlayView.setVisibility(View.VISIBLE); |
| 96 | mGuideButtonsView.setVisibility(View.GONE); |
Jaewan Kim | c92a7d1 | 2016-02-15 17:33:25 -0800 | [diff] [blame] | 97 | } |
Wale Ogunwale | 480dca0 | 2016-02-06 13:58:29 -0800 | [diff] [blame] | 98 | mHandler.removeCallbacks(mHideGuideOverlayRunnable); |
| 99 | mHandler.postDelayed(mHideGuideOverlayRunnable, SHOW_GUIDE_OVERLAY_VIEW_DURATION_MS); |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | protected void onStop() { |
| 104 | super.onStop(); |
| 105 | mHandler.removeCallbacks(mHideGuideOverlayRunnable); |
| 106 | finish(); |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | @Override |
| 110 | protected void onDestroy() { |
| 111 | super.onDestroy(); |
Jaewan Kim | 21e0421 | 2016-03-21 14:22:33 +0900 | [diff] [blame] | 112 | sPipOverlayActivity = null; |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 113 | mHandler.removeCallbacksAndMessages(null); |
| 114 | mPipManager.removeListener(this); |
Wale Ogunwale | 480dca0 | 2016-02-06 13:58:29 -0800 | [diff] [blame] | 115 | mPipManager.resumePipResizing( |
| 116 | PipManager.SUSPEND_PIP_RESIZE_REASON_WAITING_FOR_OVERLAY_ACTIVITY_FINISH); |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | @Override |
Jaewan Kim | 82ac50d | 2016-03-21 17:34:28 +0900 | [diff] [blame] | 120 | public void onPipEntered() { } |
| 121 | |
| 122 | @Override |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 123 | public void onPipActivityClosed() { |
| 124 | finish(); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public void onShowPipMenu() { |
| 129 | finish(); |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public void onMoveToFullscreen() { |
| 134 | finish(); |
| 135 | } |
Wale Ogunwale | 480dca0 | 2016-02-06 13:58:29 -0800 | [diff] [blame] | 136 | |
| 137 | @Override |
| 138 | public void onPipResizeAboutToStart() { |
| 139 | finish(); |
| 140 | mPipManager.suspendPipResizing( |
| 141 | PipManager.SUSPEND_PIP_RESIZE_REASON_WAITING_FOR_OVERLAY_ACTIVITY_FINISH); |
| 142 | } |
Jaewan Kim | 6233819 | 2016-02-25 10:00:05 -0800 | [diff] [blame] | 143 | |
| 144 | @Override |
Jaewan Kim | 8f584b8 | 2016-03-22 22:16:59 +0900 | [diff] [blame] | 145 | public void onMediaControllerChanged() { |
| 146 | updateGuideButtonsView(); |
| 147 | } |
Jaewan Kim | 21e0421 | 2016-03-21 14:22:33 +0900 | [diff] [blame] | 148 | |
| 149 | @Override |
| 150 | public void finish() { |
| 151 | sPipOverlayActivity = null; |
| 152 | super.finish(); |
| 153 | } |
Jaewan Kim | 8f584b8 | 2016-03-22 22:16:59 +0900 | [diff] [blame] | 154 | |
| 155 | private void updateGuideButtonsView() { |
| 156 | switch (mPipManager.getPlaybackState()) { |
| 157 | case PipManager.PLAYBACK_STATE_PLAYING: |
| 158 | mGuideButtonPlayPauseImageView.setVisibility(View.VISIBLE); |
| 159 | mGuideButtonPlayPauseImageView.setImageResource(R.drawable.ic_pause_white_24dp); |
| 160 | break; |
| 161 | case PipManager.PLAYBACK_STATE_PAUSED: |
| 162 | mGuideButtonPlayPauseImageView.setVisibility(View.VISIBLE); |
| 163 | mGuideButtonPlayPauseImageView.setImageResource( |
| 164 | R.drawable.ic_play_arrow_white_24dp); |
| 165 | break; |
| 166 | case PipManager.PLAYBACK_STATE_UNAVAILABLE: |
| 167 | mGuideButtonPlayPauseImageView.setVisibility(View.GONE); |
| 168 | break; |
| 169 | } |
| 170 | } |
Youngsang Cho | f164792 | 2015-12-17 13:39:39 -0800 | [diff] [blame] | 171 | } |