blob: 12cb4cd77824a7f14f02a87c83f6b56b62f8a76b [file] [log] [blame]
Youngsang Chof1647922015-12-17 13:39:39 -08001/*
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
17package com.android.systemui.tv.pip;
18
19import android.app.Activity;
Jaewan Kim21e04212016-03-21 14:22:33 +090020import android.app.ActivityOptions;
21import android.content.Context;
22import android.content.Intent;
Youngsang Chof1647922015-12-17 13:39:39 -080023import android.os.Bundle;
24import android.os.Handler;
Wale Ogunwale480dca02016-02-06 13:58:29 -080025import android.view.View;
Jaewan Kim8f584b82016-03-22 22:16:59 +090026import android.widget.ImageView;
Jaewan Kimedd02dc2016-02-23 06:16:57 -080027
Youngsang Chof1647922015-12-17 13:39:39 -080028import com.android.systemui.R;
29
Jaewan Kim21e04212016-03-21 14:22:33 +090030import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
31
Youngsang Chof1647922015-12-17 13:39:39 -080032/**
33 * Activity to show an overlay on top of PIP activity to show how to pop up PIP menu.
34 */
35public class PipOverlayActivity extends Activity implements PipManager.Listener {
Wale Ogunwale480dca02016-02-06 13:58:29 -080036 private static final long SHOW_GUIDE_OVERLAY_VIEW_DURATION_MS = 4000;
Youngsang Chof1647922015-12-17 13:39:39 -080037
Jaewan Kim21e04212016-03-21 14:22:33 +090038 /**
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 Chof1647922015-12-17 13:39:39 -080046 private final PipManager mPipManager = PipManager.getInstance();
47 private final Handler mHandler = new Handler();
Wale Ogunwale480dca02016-02-06 13:58:29 -080048 private View mGuideOverlayView;
Jaewan Kimc92a7d12016-02-15 17:33:25 -080049 private View mGuideButtonsView;
Jaewan Kim8f584b82016-03-22 22:16:59 +090050 private ImageView mGuideButtonPlayPauseImageView;
Wale Ogunwale480dca02016-02-06 13:58:29 -080051 private final Runnable mHideGuideOverlayRunnable = new Runnable() {
52 public void run() {
Jaewan Kim8f584b82016-03-22 22:16:59 +090053 mGuideOverlayView.setVisibility(View.GONE);
Wale Ogunwale480dca02016-02-06 13:58:29 -080054 }
55 };
Youngsang Chof1647922015-12-17 13:39:39 -080056
Jaewan Kim21e04212016-03-21 14:22:33 +090057 /**
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 Chof1647922015-12-17 13:39:39 -080070 @Override
71 protected void onCreate(Bundle bundle) {
72 super.onCreate(bundle);
73 setContentView(R.layout.tv_pip_overlay);
Wale Ogunwale480dca02016-02-06 13:58:29 -080074 mGuideOverlayView = findViewById(R.id.guide_overlay);
Jaewan Kimc92a7d12016-02-15 17:33:25 -080075 mGuideButtonsView = findViewById(R.id.guide_buttons);
Jaewan Kim8f584b82016-03-22 22:16:59 +090076 mGuideButtonPlayPauseImageView = (ImageView) findViewById(R.id.guide_button_play_pause);
Youngsang Chof1647922015-12-17 13:39:39 -080077 mPipManager.addListener(this);
Jaewan Kim21e04212016-03-21 14:22:33 +090078
79 sPipOverlayActivity = this;
Wale Ogunwale480dca02016-02-06 13:58:29 -080080 }
81
82 @Override
Wale Ogunwaleed0f7e22016-02-17 16:04:58 -080083 protected void onResume() {
84 super.onResume();
Jaewan Kimc92a7d12016-02-15 17:33:25 -080085 // TODO: Implement animation for this
Jaewan Kim8f584b82016-03-22 22:16:59 +090086 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 Kimc92a7d12016-02-15 17:33:25 -080094 } else {
Jaewan Kim8f584b82016-03-22 22:16:59 +090095 mGuideOverlayView.setVisibility(View.VISIBLE);
96 mGuideButtonsView.setVisibility(View.GONE);
Jaewan Kimc92a7d12016-02-15 17:33:25 -080097 }
Wale Ogunwale480dca02016-02-06 13:58:29 -080098 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 Chof1647922015-12-17 13:39:39 -0800107 }
108
109 @Override
110 protected void onDestroy() {
111 super.onDestroy();
Jaewan Kim21e04212016-03-21 14:22:33 +0900112 sPipOverlayActivity = null;
Youngsang Chof1647922015-12-17 13:39:39 -0800113 mHandler.removeCallbacksAndMessages(null);
114 mPipManager.removeListener(this);
Wale Ogunwale480dca02016-02-06 13:58:29 -0800115 mPipManager.resumePipResizing(
116 PipManager.SUSPEND_PIP_RESIZE_REASON_WAITING_FOR_OVERLAY_ACTIVITY_FINISH);
Youngsang Chof1647922015-12-17 13:39:39 -0800117 }
118
119 @Override
Jaewan Kim82ac50d2016-03-21 17:34:28 +0900120 public void onPipEntered() { }
121
122 @Override
Youngsang Chof1647922015-12-17 13:39:39 -0800123 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 Ogunwale480dca02016-02-06 13:58:29 -0800136
137 @Override
138 public void onPipResizeAboutToStart() {
139 finish();
140 mPipManager.suspendPipResizing(
141 PipManager.SUSPEND_PIP_RESIZE_REASON_WAITING_FOR_OVERLAY_ACTIVITY_FINISH);
142 }
Jaewan Kim62338192016-02-25 10:00:05 -0800143
144 @Override
Jaewan Kim8f584b82016-03-22 22:16:59 +0900145 public void onMediaControllerChanged() {
146 updateGuideButtonsView();
147 }
Jaewan Kim21e04212016-03-21 14:22:33 +0900148
149 @Override
150 public void finish() {
151 sPipOverlayActivity = null;
152 super.finish();
153 }
Jaewan Kim8f584b82016-03-22 22:16:59 +0900154
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 Chof1647922015-12-17 13:39:39 -0800171}