Jaewan Kim | a0d4d25 | 2016-03-31 13:37:10 +0900 | [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.animation.Animator; |
| 20 | import android.animation.AnimatorInflater; |
| 21 | import android.animation.AnimatorSet; |
| 22 | import android.content.Context; |
| 23 | import android.graphics.Rect; |
| 24 | import android.util.AttributeSet; |
| 25 | import android.util.Log; |
| 26 | import android.view.KeyEvent; |
| 27 | import android.view.View; |
| 28 | import android.view.View.OnFocusChangeListener; |
| 29 | |
| 30 | import com.android.systemui.R; |
| 31 | |
| 32 | import static com.android.systemui.tv.pip.PipManager.PLAYBACK_STATE_PLAYING; |
| 33 | import static com.android.systemui.tv.pip.PipManager.PLAYBACK_STATE_PAUSED; |
| 34 | import static com.android.systemui.tv.pip.PipManager.PLAYBACK_STATE_UNAVAILABLE; |
| 35 | |
| 36 | /** |
| 37 | * An extended version of {@link PipControlsView} that supports animation in Recents. |
| 38 | */ |
| 39 | public class PipRecentsControlsView extends PipControlsView { |
| 40 | /** |
| 41 | * An interface to listen user action. |
| 42 | */ |
| 43 | public interface Listener extends PipControlsView.Listener { |
| 44 | /** |
| 45 | * Called when an user presses BACK key and up. |
| 46 | */ |
| 47 | abstract void onBackPressed(); |
| 48 | } |
| 49 | |
| 50 | private AnimatorSet mFocusGainAnimatorSet; |
| 51 | private AnimatorSet mFocusLoseAnimatorSet; |
| 52 | |
| 53 | public PipRecentsControlsView(Context context) { |
| 54 | this(context, null, 0, 0); |
| 55 | } |
| 56 | |
| 57 | public PipRecentsControlsView(Context context, AttributeSet attrs) { |
| 58 | this(context, attrs, 0, 0); |
| 59 | } |
| 60 | |
| 61 | public PipRecentsControlsView(Context context, AttributeSet attrs, int defStyleAttr) { |
| 62 | this(context, attrs, defStyleAttr, 0); |
| 63 | } |
| 64 | |
| 65 | public PipRecentsControlsView( |
| 66 | Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 67 | super(context, attrs, defStyleAttr, defStyleRes); |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public void onFinishInflate() { |
| 72 | super.onFinishInflate(); |
| 73 | |
| 74 | int buttonsFocusGainAnim = R.anim.tv_pip_controls_buttons_in_recents_focus_gain_animation; |
| 75 | int textFocusGainAnim = R.anim.tv_pip_controls_text_in_recents_focus_gain_animation; |
| 76 | mFocusGainAnimatorSet = new AnimatorSet(); |
| 77 | mFocusGainAnimatorSet.playTogether( |
| 78 | loadAnimator(this, R.anim.tv_pip_controls_in_recents_focus_gain_animation), |
| 79 | loadAnimator(mFullButtonView,buttonsFocusGainAnim), |
| 80 | loadAnimator(mPlayPauseButtonImageView, buttonsFocusGainAnim), |
| 81 | loadAnimator(mCloseButtonView, buttonsFocusGainAnim), |
| 82 | loadAnimator(mFullDescriptionView, textFocusGainAnim), |
| 83 | loadAnimator(mPlayPauseDescriptionTextView, textFocusGainAnim), |
| 84 | loadAnimator(mCloseDescriptionView, textFocusGainAnim)); |
| 85 | |
| 86 | int buttonsFocusLoseAnim = R.anim.tv_pip_controls_buttons_in_recents_focus_lose_animation; |
| 87 | int textFocusLoseAnim = R.anim.tv_pip_controls_text_in_recents_focus_lose_animation; |
| 88 | mFocusLoseAnimatorSet = new AnimatorSet(); |
| 89 | mFocusLoseAnimatorSet.playTogether( |
| 90 | loadAnimator(this, R.anim.tv_pip_controls_in_recents_focus_lose_animation), |
| 91 | loadAnimator(mFullButtonView, buttonsFocusLoseAnim), |
| 92 | loadAnimator(mPlayPauseButtonImageView, buttonsFocusLoseAnim), |
| 93 | loadAnimator(mCloseButtonView, buttonsFocusLoseAnim), |
| 94 | loadAnimator(mFullDescriptionView, textFocusLoseAnim), |
| 95 | loadAnimator(mPlayPauseDescriptionTextView, textFocusLoseAnim), |
| 96 | loadAnimator(mCloseDescriptionView, textFocusLoseAnim)); |
| 97 | |
| 98 | Rect pipBounds = mPipManager.getRecentsFocusedPipBounds(); |
| 99 | int pipControlsMarginTop = getContext().getResources().getDimensionPixelSize( |
| 100 | R.dimen.recents_tv_pip_controls_margin_top); |
| 101 | setPadding(0, pipBounds.bottom + pipControlsMarginTop, 0, 0); |
| 102 | } |
| 103 | |
| 104 | private Animator loadAnimator(View view, int animatorResId) { |
| 105 | Animator animator = AnimatorInflater.loadAnimator(getContext(), animatorResId); |
| 106 | animator.setTarget(view); |
| 107 | return animator; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Starts focus gaining animation. |
| 112 | */ |
| 113 | public void startFocusGainAnimation() { |
| 114 | if (mFocusLoseAnimatorSet.isStarted()) { |
| 115 | mFocusLoseAnimatorSet.cancel(); |
| 116 | } |
| 117 | mFocusGainAnimatorSet.start(); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Starts focus losing animation. |
| 122 | */ |
| 123 | public void startFocusLoseAnimation() { |
| 124 | if (mFocusGainAnimatorSet.isStarted()) { |
| 125 | mFocusGainAnimatorSet.cancel(); |
| 126 | } |
| 127 | mFocusLoseAnimatorSet.start(); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Resets the view to the initial state. (i.e. end of the focus gain) |
| 132 | */ |
| 133 | public void reset() { |
| 134 | if (mFocusGainAnimatorSet.isStarted()) { |
| 135 | mFocusGainAnimatorSet.cancel(); |
| 136 | } |
| 137 | if (mFocusLoseAnimatorSet.isStarted()) { |
| 138 | mFocusLoseAnimatorSet.cancel(); |
| 139 | } |
| 140 | |
| 141 | // Reset to initial state (i.e. end of focused) |
| 142 | requestFocus(); |
| 143 | setTranslationY(0); |
| 144 | setScaleXY(mFullButtonView, 1); |
| 145 | setScaleXY(mPlayPauseButtonImageView, 1); |
| 146 | setScaleXY(mCloseButtonView, 1); |
| 147 | mFullDescriptionView.setAlpha(1); |
| 148 | mPlayPauseDescriptionTextView.setAlpha(1); |
| 149 | mCloseDescriptionView.setAlpha(1); |
| 150 | } |
| 151 | |
| 152 | private void setScaleXY(View view, float scale) { |
| 153 | view.setScaleX(scale); |
| 154 | view.setScaleY(scale); |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public boolean dispatchKeyEvent(KeyEvent event) { |
| 159 | if (!event.isCanceled() |
| 160 | && event.getKeyCode() == KeyEvent.KEYCODE_BACK |
| 161 | && event.getAction() == KeyEvent.ACTION_UP) { |
| 162 | if (mListener != null) { |
| 163 | ((PipRecentsControlsView.Listener) mListener).onBackPressed(); |
| 164 | } |
| 165 | return true; |
| 166 | } |
| 167 | return super.dispatchKeyEvent(event); |
| 168 | } |
| 169 | } |