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.animation.Animator;
20 import android.animation.AnimatorInflater;
21 import android.app.Activity;
22 import android.app.ActivityOptions;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.os.Bundle;
26 import android.os.Handler;
27 import android.view.View;
28 import android.widget.ImageView;
29 
30 import com.android.systemui.R;
31 
32 import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
33 
34 /**
35  * Activity to show an overlay on top of PIP activity to show how to pop up PIP menu.
36  */
37 public class PipOverlayActivity extends Activity implements PipManager.Listener {
38     private static final long SHOW_GUIDE_OVERLAY_VIEW_DURATION_MS = 4000;
39 
40     /**
41      * A flag to ensure the single instance of PipOverlayActivity to prevent it from restarting.
42      * Note that {@link PipManager} moves the PIPed activity to fullscreen if the activity is
43      * restarted. It's because the activity may be started by the Launcher or an intent again,
44      * but we don't want do so for the PipOverlayActivity.
45      */
46     private static boolean sActivityCreated;
47 
48     private final PipManager mPipManager = PipManager.getInstance();
49     private final Handler mHandler = new Handler();
50     private View mGuideOverlayView;
51     private View mGuideButtonsView;
52     private ImageView mGuideButtonPlayPauseImageView;
53     private final Runnable mHideGuideOverlayRunnable = new Runnable() {
54         public void run() {
55             mFadeOutAnimation.start();
56         }
57     };
58     private Animator mFadeInAnimation;
59     private Animator mFadeOutAnimation;
60 
61     /**
62      * Shows PIP overlay UI only if it's not there.
63      */
showPipOverlay(Context context)64     static void showPipOverlay(Context context) {
65         if (!sActivityCreated) {
66             Intent intent = new Intent(context, PipOverlayActivity.class);
67             intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
68             final ActivityOptions options = ActivityOptions.makeBasic();
69             options.setLaunchStackId(PINNED_STACK_ID);
70             context.startActivity(intent, options.toBundle());
71         }
72     }
73 
74     @Override
onCreate(Bundle bundle)75     protected void onCreate(Bundle bundle) {
76         super.onCreate(bundle);
77         sActivityCreated = true;
78         setContentView(R.layout.tv_pip_overlay);
79         mGuideOverlayView = findViewById(R.id.guide_overlay);
80         mPipManager.addListener(this);
81         mFadeInAnimation = AnimatorInflater.loadAnimator(
82                 this, R.anim.tv_pip_overlay_fade_in_animation);
83         mFadeInAnimation.setTarget(mGuideOverlayView);
84         mFadeOutAnimation = AnimatorInflater.loadAnimator(
85                 this, R.anim.tv_pip_overlay_fade_out_animation);
86         mFadeOutAnimation.setTarget(mGuideOverlayView);
87     }
88 
89     @Override
onResume()90     protected void onResume() {
91         super.onResume();
92         mFadeInAnimation.start();
93         mHandler.removeCallbacks(mHideGuideOverlayRunnable);
94         mHandler.postDelayed(mHideGuideOverlayRunnable, SHOW_GUIDE_OVERLAY_VIEW_DURATION_MS);
95     }
96 
97     @Override
onStop()98     protected void onStop() {
99         super.onStop();
100         mHandler.removeCallbacks(mHideGuideOverlayRunnable);
101         finish();
102     }
103 
104     @Override
onDestroy()105     protected void onDestroy() {
106         super.onDestroy();
107         sActivityCreated = false;
108         mHandler.removeCallbacksAndMessages(null);
109         mPipManager.removeListener(this);
110         mPipManager.resumePipResizing(
111                 PipManager.SUSPEND_PIP_RESIZE_REASON_WAITING_FOR_OVERLAY_ACTIVITY_FINISH);
112     }
113 
114     @Override
onPipEntered()115     public void onPipEntered() { }
116 
117     @Override
onPipActivityClosed()118     public void onPipActivityClosed() {
119         finish();
120     }
121 
122     @Override
onShowPipMenu()123     public void onShowPipMenu() {
124         finish();
125     }
126 
127     @Override
onMoveToFullscreen()128     public void onMoveToFullscreen() {
129         finish();
130     }
131 
132     @Override
onPipResizeAboutToStart()133     public void onPipResizeAboutToStart() {
134         finish();
135         mPipManager.suspendPipResizing(
136                 PipManager.SUSPEND_PIP_RESIZE_REASON_WAITING_FOR_OVERLAY_ACTIVITY_FINISH);
137     }
138 }
139