Home | History | Annotate | Download | only in tv
      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.pip.tv;
     18 
     19 import android.animation.Animator;
     20 import android.animation.AnimatorInflater;
     21 import android.app.Activity;
     22 import android.content.Intent;
     23 import android.content.pm.ParceledListSlice;
     24 import android.os.Bundle;
     25 import android.view.View;
     26 
     27 import com.android.systemui.R;
     28 
     29 import java.util.Collections;
     30 /**
     31  * Activity to show the PIP menu to control PIP.
     32  */
     33 public class PipMenuActivity extends Activity implements PipManager.Listener {
     34     private static final String TAG = "PipMenuActivity";
     35 
     36     static final String EXTRA_CUSTOM_ACTIONS = "custom_actions";
     37 
     38     private final PipManager mPipManager = PipManager.getInstance();
     39 
     40     private Animator mFadeInAnimation;
     41     private Animator mFadeOutAnimation;
     42     private PipControlsView mPipControlsView;
     43     private boolean mRestorePipSizeWhenClose;
     44 
     45     @Override
     46     protected void onCreate(Bundle bundle) {
     47         super.onCreate(bundle);
     48         if (!mPipManager.isPipShown()) {
     49             finish();
     50         }
     51         setContentView(R.layout.tv_pip_menu);
     52         mPipManager.addListener(this);
     53 
     54         mRestorePipSizeWhenClose = true;
     55         mPipControlsView = findViewById(R.id.pip_controls);
     56         mFadeInAnimation = AnimatorInflater.loadAnimator(
     57                 this, R.anim.tv_pip_menu_fade_in_animation);
     58         mFadeInAnimation.setTarget(mPipControlsView);
     59         mFadeOutAnimation = AnimatorInflater.loadAnimator(
     60                 this, R.anim.tv_pip_menu_fade_out_animation);
     61         mFadeOutAnimation.setTarget(mPipControlsView);
     62 
     63         onPipMenuActionsChanged(getIntent().getParcelableExtra(EXTRA_CUSTOM_ACTIONS));
     64     }
     65 
     66     @Override
     67     protected void onNewIntent(Intent intent) {
     68         super.onNewIntent(intent);
     69 
     70         onPipMenuActionsChanged(getIntent().getParcelableExtra(EXTRA_CUSTOM_ACTIONS));
     71     }
     72 
     73     private void restorePipAndFinish() {
     74         if (mRestorePipSizeWhenClose) {
     75             // When PIP menu activity is closed, restore to the default position.
     76             mPipManager.resizePinnedStack(PipManager.STATE_PIP);
     77         }
     78         finish();
     79     }
     80 
     81     @Override
     82     public void onResume() {
     83         super.onResume();
     84         mFadeInAnimation.start();
     85     }
     86 
     87     @Override
     88     public void onPause() {
     89         super.onPause();
     90         mFadeOutAnimation.start();
     91         restorePipAndFinish();
     92     }
     93 
     94     @Override
     95     protected void onDestroy() {
     96         super.onDestroy();
     97         mPipManager.removeListener(this);
     98         mPipManager.resumePipResizing(
     99                 PipManager.SUSPEND_PIP_RESIZE_REASON_WAITING_FOR_MENU_ACTIVITY_FINISH);
    100     }
    101 
    102     @Override
    103     public void onBackPressed() {
    104         restorePipAndFinish();
    105     }
    106 
    107     @Override
    108     public void onPipEntered() { }
    109 
    110     @Override
    111     public void onPipActivityClosed() {
    112         finish();
    113     }
    114 
    115     @Override
    116     public void onPipMenuActionsChanged(ParceledListSlice actions) {
    117         boolean hasCustomActions = actions != null && !actions.getList().isEmpty();
    118         mPipControlsView.setActions(hasCustomActions ? actions.getList() : Collections.EMPTY_LIST);
    119     }
    120 
    121     @Override
    122     public void onShowPipMenu() { }
    123 
    124     @Override
    125     public void onMoveToFullscreen() {
    126         // Moving PIP to fullscreen is implemented by resizing PINNED_STACK with null bounds.
    127         // This conflicts with restoring PIP position, so disable it.
    128         mRestorePipSizeWhenClose = false;
    129         finish();
    130     }
    131 
    132     @Override
    133     public void onPipResizeAboutToStart() {
    134         finish();
    135         mPipManager.suspendPipResizing(
    136                 PipManager.SUSPEND_PIP_RESIZE_REASON_WAITING_FOR_MENU_ACTIVITY_FINISH);
    137     }
    138 }
    139