diff options
Diffstat (limited to 'src/com/android/tv/ui/TvViewUiManager.java')
-rw-r--r-- | src/com/android/tv/ui/TvViewUiManager.java | 318 |
1 files changed, 179 insertions, 139 deletions
diff --git a/src/com/android/tv/ui/TvViewUiManager.java b/src/com/android/tv/ui/TvViewUiManager.java index f042987a..7e354db3 100644 --- a/src/com/android/tv/ui/TvViewUiManager.java +++ b/src/com/android/tv/ui/TvViewUiManager.java @@ -42,16 +42,15 @@ import android.view.ViewGroup.LayoutParams; import android.view.ViewGroup.MarginLayoutParams; import android.view.animation.AnimationUtils; import android.widget.FrameLayout; - -import com.android.tv.Features; import com.android.tv.R; +import com.android.tv.TvFeatures; import com.android.tv.TvOptionsManager; import com.android.tv.data.DisplayMode; import com.android.tv.util.TvSettings; /** - * The TvViewUiManager is responsible for handling UI layouting and animation of main TvView. - * It also control the settings regarding TvView UI such as display mode. + * The TvViewUiManager is responsible for handling UI layouting and animation of main TvView. It + * also control the settings regarding TvView UI such as display mode. */ public class TvViewUiManager { private static final String TAG = "TvViewManager"; @@ -94,7 +93,7 @@ public class TvViewUiManager { mTvView.setLayoutParams(mTvViewFrame); // Smooth PIP size change, we don't change surface size when // isInPictureInPictureMode is true. - if (!Features.PICTURE_IN_PICTURE.isEnabled(mContext) + if (!TvFeatures.PICTURE_IN_PICTURE.isEnabled(mContext) || (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && !((Activity) mContext).isInPictureInPictureMode())) { mTvView.setFixedSurfaceSize( @@ -126,16 +125,19 @@ public class TvViewUiManager { private int mAppliedTvViewEndMargin; private float mAppliedVideoDisplayAspectRatio; - public TvViewUiManager(Context context, TunableTvView tvView, - FrameLayout contentView, TvOptionsManager tvOptionManager) { + public TvViewUiManager( + Context context, + TunableTvView tvView, + FrameLayout contentView, + TvOptionsManager tvOptionManager) { mContext = context; mResources = mContext.getResources(); mTvView = tvView; mContentView = contentView; mTvOptionsManager = tvOptionManager; - DisplayManager displayManager = (DisplayManager) mContext - .getSystemService(Context.DISPLAY_SERVICE); + DisplayManager displayManager = + (DisplayManager) mContext.getSystemService(Context.DISPLAY_SERVICE); Display display = displayManager.getDisplay(Display.DEFAULT_DISPLAY); Point size = new Point(); display.getSize(size); @@ -143,8 +145,8 @@ public class TvViewUiManager { mWindowHeight = size.y; // Have an assumption that TvView Shrinking happens only in full screen. - mTvViewShrunkenStartMargin = mResources - .getDimensionPixelOffset(R.dimen.shrunken_tvview_margin_start); + mTvViewShrunkenStartMargin = + mResources.getDimensionPixelOffset(R.dimen.shrunken_tvview_margin_start); mTvViewShrunkenEndMargin = mResources.getDimensionPixelOffset(R.dimen.shrunken_tvview_margin_end) + mResources.getDimensionPixelSize(R.dimen.side_panel_width); @@ -152,10 +154,12 @@ public class TvViewUiManager { mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(mContext); - mLinearOutSlowIn = AnimationUtils - .loadInterpolator(mContext, android.R.interpolator.linear_out_slow_in); - mFastOutLinearIn = AnimationUtils - .loadInterpolator(mContext, android.R.interpolator.fast_out_linear_in); + mLinearOutSlowIn = + AnimationUtils.loadInterpolator( + mContext, android.R.interpolator.linear_out_slow_in); + mFastOutLinearIn = + AnimationUtils.loadInterpolator( + mContext, android.R.interpolator.fast_out_linear_in); } public void onConfigurationChanged(final int windowWidth, final int windowHeight) { @@ -169,9 +173,9 @@ public class TvViewUiManager { } /** - * Initializes animator in advance of using the animator to improve animation performance. - * For fast first tune, it is not expected to be called in Activity.onCreate, but called - * a few seconds later after onCreate. + * Initializes animator in advance of using the animator to improve animation performance. For + * fast first tune, it is not expected to be called in Activity.onCreate, but called a few + * seconds later after onCreate. */ public void initAnimatorIfNeeded() { initTvAnimatorIfNeeded(); @@ -203,16 +207,14 @@ public class TvViewUiManager { setDisplayMode(mDisplayModeBeforeShrunken, false, true); } - /** - * Returns true, if TvView is shrunken. - */ + /** Returns true, if TvView is shrunken. */ public boolean isUnderShrunkenTvView() { return mIsUnderShrunkenTvView; } /** - * Returns true, if {@code displayMode} is available now. If screen ratio is matched to - * video ratio, other display modes than {@link DisplayMode#MODE_NORMAL} are not available. + * Returns true, if {@code displayMode} is available now. If screen ratio is matched to video + * ratio, other display modes than {@link DisplayMode#MODE_NORMAL} are not available. */ public boolean isDisplayModeAvailable(int displayMode) { if (displayMode == DisplayMode.MODE_NORMAL) { @@ -226,11 +228,15 @@ public class TvViewUiManager { if (viewWidth <= 0 || viewHeight <= 0 || videoDisplayAspectRatio <= 0f) { Log.w(TAG, "Video size is currently unavailable"); if (DEBUG) { - Log.d(TAG, "isDisplayModeAvailable: " - + "viewWidth=" + viewWidth - + ", viewHeight=" + viewHeight - + ", videoDisplayAspectRatio=" + videoDisplayAspectRatio - ); + Log.d( + TAG, + "isDisplayModeAvailable: " + + "viewWidth=" + + viewWidth + + ", viewHeight=" + + viewHeight + + ", videoDisplayAspectRatio=" + + videoDisplayAspectRatio); } return false; } @@ -239,9 +245,7 @@ public class TvViewUiManager { return Math.abs(viewRatio - videoDisplayAspectRatio) >= DISPLAY_MODE_EPSILON; } - /** - * Returns a constant defined in DisplayMode. - */ + /** Returns a constant defined in DisplayMode. */ public int getDisplayMode() { if (isDisplayModeAvailable(mDisplayMode)) { return mDisplayMode; @@ -264,49 +268,45 @@ public class TvViewUiManager { return prev; } - /** - * Restores the display mode to the display mode stored in preference. - */ + /** Restores the display mode to the display mode stored in preference. */ public void restoreDisplayMode(boolean animate) { - int displayMode = mSharedPreferences - .getInt(TvSettings.PREF_DISPLAY_MODE, DisplayMode.MODE_NORMAL); + int displayMode = + mSharedPreferences.getInt(TvSettings.PREF_DISPLAY_MODE, DisplayMode.MODE_NORMAL); setDisplayMode(displayMode, false, animate); } - /** - * Updates TvView's aspect ratio. It should be called when video resolution is changed. - */ + /** Updates TvView's aspect ratio. It should be called when video resolution is changed. */ public void updateTvAspectRatio() { applyDisplayMode(mTvView.getVideoDisplayAspectRatio(), false, false); if (mTvView.isVideoAvailable() && mTvView.isFadedOut()) { - mTvView.fadeIn(mResources.getInteger(R.integer.tvview_fade_in_duration), - mFastOutLinearIn, null); + mTvView.fadeIn( + mResources.getInteger(R.integer.tvview_fade_in_duration), + mFastOutLinearIn, + null); } } - /** - * Fades in TvView. - */ + /** Fades in TvView. */ public void fadeInTvView() { if (mTvView.isFadedOut()) { - mTvView.fadeIn(mResources.getInteger(R.integer.tvview_fade_in_duration), - mFastOutLinearIn, null); + mTvView.fadeIn( + mResources.getInteger(R.integer.tvview_fade_in_duration), + mFastOutLinearIn, + null); } } - /** - * Fades out TvView. - */ + /** Fades out TvView. */ public void fadeOutTvView(Runnable postAction) { if (!mTvView.isFadedOut()) { - mTvView.fadeOut(mResources.getInteger(R.integer.tvview_fade_out_duration), - mLinearOutSlowIn, postAction); + mTvView.fadeOut( + mResources.getInteger(R.integer.tvview_fade_out_duration), + mLinearOutSlowIn, + postAction); } } - /** - * This margins will be applied when applyDisplayMode is called. - */ + /** This margins will be applied when applyDisplayMode is called. */ private void setTvViewMargin(int tvViewStartMargin, int tvViewEndMargin) { mTvViewStartMargin = tvViewStartMargin; mTvViewEndMargin = tvViewEndMargin; @@ -316,8 +316,8 @@ public class TvViewUiManager { return mTvViewStartMargin == 0 && mTvViewEndMargin == 0; } - private void setBackgroundColor(int color, FrameLayout.LayoutParams targetLayoutParams, - boolean animate) { + private void setBackgroundColor( + int color, FrameLayout.LayoutParams targetLayoutParams, boolean animate) { if (animate) { initBackgroundAnimatorIfNeeded(); if (mBackgroundAnimator.isStarted()) { @@ -327,12 +327,13 @@ public class TvViewUiManager { int decorViewWidth = mContentView.getWidth(); int decorViewHeight = mContentView.getHeight(); - boolean hasPillarBox = mTvView.getWidth() != decorViewWidth - || mTvView.getHeight() != decorViewHeight; - boolean willHavePillarBox = ((targetLayoutParams.width != LayoutParams.MATCH_PARENT) - && targetLayoutParams.width != decorViewWidth) || ( - (targetLayoutParams.height != LayoutParams.MATCH_PARENT) - && targetLayoutParams.height != decorViewHeight); + boolean hasPillarBox = + mTvView.getWidth() != decorViewWidth || mTvView.getHeight() != decorViewHeight; + boolean willHavePillarBox = + ((targetLayoutParams.width != LayoutParams.MATCH_PARENT) + && targetLayoutParams.width != decorViewWidth) + || ((targetLayoutParams.height != LayoutParams.MATCH_PARENT) + && targetLayoutParams.height != decorViewHeight); if (!isTvViewFullScreen() && !hasPillarBox) { // If there is no pillar box, no animation is needed. @@ -351,13 +352,27 @@ public class TvViewUiManager { mBackgroundColor = color; } - private void setTvViewPosition(final FrameLayout.LayoutParams layoutParams, - FrameLayout.LayoutParams tvViewFrame, boolean animate) { + private void setTvViewPosition( + final FrameLayout.LayoutParams layoutParams, + FrameLayout.LayoutParams tvViewFrame, + boolean animate) { if (DEBUG) { - Log.d(TAG, "setTvViewPosition: w=" + layoutParams.width + " h=" + layoutParams.height - + " s=" + layoutParams.getMarginStart() + " t=" + layoutParams.topMargin - + " e=" + layoutParams.getMarginEnd() + " b=" + layoutParams.bottomMargin - + " animate=" + animate); + Log.d( + TAG, + "setTvViewPosition: w=" + + layoutParams.width + + " h=" + + layoutParams.height + + " s=" + + layoutParams.getMarginStart() + + " t=" + + layoutParams.topMargin + + " e=" + + layoutParams.getMarginEnd() + + " b=" + + layoutParams.bottomMargin + + " animate=" + + animate); } FrameLayout.LayoutParams oldTvViewFrame = mTvViewFrame; mTvViewLayoutParams = layoutParams; @@ -372,21 +387,25 @@ public class TvViewUiManager { mOldTvViewFrame = new FrameLayout.LayoutParams(oldTvViewFrame); } mTvViewAnimator.setObjectValues(mTvView.getTvViewLayoutParams(), layoutParams); - mTvViewAnimator.setEvaluator(new TypeEvaluator<FrameLayout.LayoutParams>() { - FrameLayout.LayoutParams lp; - @Override - public FrameLayout.LayoutParams evaluate(float fraction, - FrameLayout.LayoutParams startValue, FrameLayout.LayoutParams endValue) { - if (lp == null) { - lp = new FrameLayout.LayoutParams(0, 0); - lp.gravity = startValue.gravity; - } - interpolateMargins(lp, startValue, endValue, fraction); - return lp; - } - }); - mTvViewAnimator - .setInterpolator(isTvViewFullScreen() ? mFastOutLinearIn : mLinearOutSlowIn); + mTvViewAnimator.setEvaluator( + new TypeEvaluator<FrameLayout.LayoutParams>() { + FrameLayout.LayoutParams lp; + + @Override + public FrameLayout.LayoutParams evaluate( + float fraction, + FrameLayout.LayoutParams startValue, + FrameLayout.LayoutParams endValue) { + if (lp == null) { + lp = new FrameLayout.LayoutParams(0, 0); + lp.gravity = startValue.gravity; + } + interpolateMargins(lp, startValue, endValue, fraction); + return lp; + } + }); + mTvViewAnimator.setInterpolator( + isTvViewFullScreen() ? mFastOutLinearIn : mLinearOutSlowIn); mTvViewAnimator.start(); } else { if (mTvViewAnimator != null && mTvViewAnimator.isStarted()) { @@ -425,38 +444,42 @@ public class TvViewUiManager { mTvViewAnimator.setProperty( Property.of(FrameLayout.class, ViewGroup.LayoutParams.class, "layoutParams")); mTvViewAnimator.setDuration(mResources.getInteger(R.integer.tvview_anim_duration)); - mTvViewAnimator.addListener(new AnimatorListenerAdapter() { - private boolean mCanceled = false; + mTvViewAnimator.addListener( + new AnimatorListenerAdapter() { + private boolean mCanceled = false; - @Override - public void onAnimationCancel(Animator animation) { - mCanceled = true; - } + @Override + public void onAnimationCancel(Animator animation) { + mCanceled = true; + } - @Override - public void onAnimationEnd(Animator animation) { - if (mCanceled) { - mCanceled = false; - return; - } - mHandler.post(new Runnable() { @Override - public void run() { - setTvViewPosition(mTvViewLayoutParams, mTvViewFrame, false); + public void onAnimationEnd(Animator animation) { + if (mCanceled) { + mCanceled = false; + return; + } + mHandler.post( + new Runnable() { + @Override + public void run() { + setTvViewPosition(mTvViewLayoutParams, mTvViewFrame, false); + } + }); + } + }); + mTvViewAnimator.addUpdateListener( + new AnimatorUpdateListener() { + @Override + public void onAnimationUpdate(ValueAnimator animator) { + float fraction = animator.getAnimatedFraction(); + mLastAnimatedTvViewFrame = + (FrameLayout.LayoutParams) mTvView.getLayoutParams(); + interpolateMargins( + mLastAnimatedTvViewFrame, mOldTvViewFrame, mTvViewFrame, fraction); + mTvView.setLayoutParams(mLastAnimatedTvViewFrame); } }); - } - }); - mTvViewAnimator.addUpdateListener(new AnimatorUpdateListener() { - @Override - public void onAnimationUpdate(ValueAnimator animator) { - float fraction = animator.getAnimatedFraction(); - mLastAnimatedTvViewFrame = (FrameLayout.LayoutParams) mTvView.getLayoutParams(); - interpolateMargins(mLastAnimatedTvViewFrame, - mOldTvViewFrame, mTvViewFrame, fraction); - mTvView.setLayoutParams(mLastAnimatedTvViewFrame); - } - }); } private void initBackgroundAnimatorIfNeeded() { @@ -467,31 +490,33 @@ public class TvViewUiManager { mBackgroundAnimator = new ObjectAnimator(); mBackgroundAnimator.setTarget(mContentView); mBackgroundAnimator.setPropertyName("backgroundColor"); - mBackgroundAnimator - .setDuration(mResources.getInteger(R.integer.tvactivity_background_anim_duration)); - mBackgroundAnimator.addListener(new AnimatorListenerAdapter() { - @Override - public void onAnimationEnd(Animator animation) { - mHandler.post(new Runnable() { + mBackgroundAnimator.setDuration( + mResources.getInteger(R.integer.tvactivity_background_anim_duration)); + mBackgroundAnimator.addListener( + new AnimatorListenerAdapter() { @Override - public void run() { - mContentView.setBackgroundColor(mBackgroundColor); + public void onAnimationEnd(Animator animation) { + mHandler.post( + new Runnable() { + @Override + public void run() { + mContentView.setBackgroundColor(mBackgroundColor); + } + }); } }); - } - }); } - private void applyDisplayMode(float videoDisplayAspectRatio, boolean animate, - boolean forceUpdate) { + private void applyDisplayMode( + float videoDisplayAspectRatio, boolean animate, boolean forceUpdate) { if (videoDisplayAspectRatio <= 0f) { videoDisplayAspectRatio = (float) mWindowWidth / mWindowHeight; } if (mAppliedDisplayedMode == mDisplayMode && mAppliedTvViewStartMargin == mTvViewStartMargin && mAppliedTvViewEndMargin == mTvViewEndMargin - && Math.abs(mAppliedVideoDisplayAspectRatio - videoDisplayAspectRatio) < - DISPLAY_ASPECT_RATIO_EPSILON) { + && Math.abs(mAppliedVideoDisplayAspectRatio - videoDisplayAspectRatio) + < DISPLAY_ASPECT_RATIO_EPSILON) { if (!forceUpdate) { return; } @@ -507,14 +532,20 @@ public class TvViewUiManager { float availableAreaRatio = 0; if (availableAreaWidth <= 0 || availableAreaHeight <= 0) { displayMode = DisplayMode.MODE_FULL; - Log.w(TAG, "Some resolution info is missing during applyDisplayMode. (" - + "availableAreaWidth=" + availableAreaWidth + ", availableAreaHeight=" - + availableAreaHeight + ")"); + Log.w( + TAG, + "Some resolution info is missing during applyDisplayMode. (" + + "availableAreaWidth=" + + availableAreaWidth + + ", availableAreaHeight=" + + availableAreaHeight + + ")"); } else { availableAreaRatio = (float) availableAreaWidth / availableAreaHeight; } - FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(0, 0, - ((FrameLayout.LayoutParams) mTvView.getTvViewLayoutParams()).gravity); + FrameLayout.LayoutParams layoutParams = + new FrameLayout.LayoutParams( + 0, 0, ((FrameLayout.LayoutParams) mTvView.getTvViewLayoutParams()).gravity); switch (displayMode) { case DisplayMode.MODE_ZOOM: if (videoDisplayAspectRatio < availableAreaRatio) { @@ -549,12 +580,18 @@ public class TvViewUiManager { int marginStart = (availableAreaWidth - layoutParams.width) / 2; layoutParams.setMarginStart(marginStart); int tvViewFrameTop = (mWindowHeight - availableAreaHeight) / 2; - FrameLayout.LayoutParams tvViewFrame = createMarginLayoutParams( - mTvViewStartMargin, mTvViewEndMargin, tvViewFrameTop, tvViewFrameTop); + FrameLayout.LayoutParams tvViewFrame = + createMarginLayoutParams( + mTvViewStartMargin, mTvViewEndMargin, tvViewFrameTop, tvViewFrameTop); setTvViewPosition(layoutParams, tvViewFrame, animate); - setBackgroundColor(mResources.getColor(isTvViewFullScreen() - ? R.color.tvactivity_background : R.color.tvactivity_background_on_shrunken_tvview, - null), layoutParams, animate); + setBackgroundColor( + mResources.getColor( + isTvViewFullScreen() + ? R.color.tvactivity_background + : R.color.tvactivity_background_on_shrunken_tvview, + null), + layoutParams, + animate); // Update the current display mode. mTvOptionsManager.onDisplayModeChanged(displayMode); @@ -564,12 +601,15 @@ public class TvViewUiManager { return (int) (start + (end - start) * fraction); } - private static void interpolateMargins(MarginLayoutParams out, - MarginLayoutParams startValue, MarginLayoutParams endValue, float fraction) { + private static void interpolateMargins( + MarginLayoutParams out, + MarginLayoutParams startValue, + MarginLayoutParams endValue, + float fraction) { out.topMargin = interpolate(startValue.topMargin, endValue.topMargin, fraction); out.bottomMargin = interpolate(startValue.bottomMargin, endValue.bottomMargin, fraction); - out.setMarginStart(interpolate(startValue.getMarginStart(), endValue.getMarginStart(), - fraction)); + out.setMarginStart( + interpolate(startValue.getMarginStart(), endValue.getMarginStart(), fraction)); out.setMarginEnd(interpolate(startValue.getMarginEnd(), endValue.getMarginEnd(), fraction)); out.width = interpolate(startValue.width, endValue.width, fraction); out.height = interpolate(startValue.height, endValue.height, fraction); @@ -586,4 +626,4 @@ public class TvViewUiManager { lp.height = mWindowHeight - topMargin - bottomMargin; return lp; } -}
\ No newline at end of file +} |