Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.chrome.browser.widget; | 5 package org.chromium.chrome.browser.widget; |
| 6 | 6 |
| 7 import android.animation.TimeAnimator; | |
| 8 import android.animation.TimeAnimator.TimeListener; | |
| 7 import android.content.Context; | 9 import android.content.Context; |
| 10 import android.text.TextUtils; | |
| 8 import android.util.AttributeSet; | 11 import android.util.AttributeSet; |
| 9 | 12 |
| 13 import org.chromium.base.CommandLine; | |
| 10 import org.chromium.base.VisibleForTesting; | 14 import org.chromium.base.VisibleForTesting; |
| 15 import org.chromium.chrome.browser.ChromeSwitches; | |
| 11 import org.chromium.ui.interpolators.BakedBezierInterpolator; | 16 import org.chromium.ui.interpolators.BakedBezierInterpolator; |
| 12 | 17 |
| 13 /** | 18 /** |
| 14 * Progress bar for use in the Toolbar view. | 19 * Progress bar for use in the Toolbar view. |
| 15 */ | 20 */ |
| 16 public class ToolbarProgressBar extends ClipDrawableProgressBar { | 21 public class ToolbarProgressBar extends ClipDrawableProgressBar { |
| 17 private long mAlphaAnimationDurationMs = 130; | 22 |
| 23 /** | |
| 24 * Interface for progress bar animation interpolation logics. | |
| 25 */ | |
| 26 interface AnimationLogic { | |
| 27 /** | |
| 28 * Resets internal data. It must be called on every loading start. | |
| 29 */ | |
| 30 void reset(); | |
| 31 | |
| 32 /** | |
| 33 * Returns interpolated progress for animation. | |
| 34 * | |
| 35 * @param targetProgress Actual page loading progress. | |
| 36 * @param frameTimeSec Duration since the last call. | |
| 37 * @param resolution Resolution of the displayed progress bar. Mainl y for rounding. | |
| 38 */ | |
| 39 float updateProgress(float targetProgress, float frameTimeSec, int resol ution); | |
| 40 } | |
| 41 | |
| 42 private static final long PROGRESS_FRAME_TIME_CAP_MS = 50; | |
| 43 private long mAlphaAnimationDurationMs = 140; | |
| 18 private long mHidingDelayMs = 100; | 44 private long mHidingDelayMs = 100; |
| 19 | 45 |
| 20 private boolean mIsStarted; | 46 private boolean mIsStarted; |
| 47 private float mTargetProgress; | |
| 21 private float mTargetAlpha = 0.0f; | 48 private float mTargetAlpha = 0.0f; |
| 49 AnimationLogic mAnimationLogic; | |
| 50 | |
| 22 private final Runnable mHideRunnable = new Runnable() { | 51 private final Runnable mHideRunnable = new Runnable() { |
| 23 @Override | 52 @Override |
| 24 public void run() { | 53 public void run() { |
| 25 animateAlphaTo(0.0f); | 54 animateAlphaTo(0.0f); |
| 26 } | 55 } |
| 27 }; | 56 }; |
| 28 | 57 |
| 58 private final TimeAnimator mProgressAnimator = new TimeAnimator(); | |
| 59 { | |
| 60 mProgressAnimator.setTimeListener(new TimeListener() { | |
| 61 @Override | |
| 62 public void onTimeUpdate(TimeAnimator animation, long totalTimeMs, l ong deltaTimeMs) { | |
| 63 // Cap progress bar animation frame time so that it doesn't jump too much even when | |
| 64 // the animation is janky. | |
| 65 ToolbarProgressBar.super.setProgress(mAnimationLogic.updateProgr ess( | |
| 66 mTargetProgress, | |
| 67 Math.max(deltaTimeMs, PROGRESS_FRAME_TIME_CAP_MS) * 0.00 1f, | |
| 68 getWidth())); | |
| 69 | |
| 70 if (getProgress() == mTargetProgress) { | |
| 71 if (mTargetProgress == 1.0f && !mIsStarted) { | |
| 72 postDelayed(mHideRunnable, mHidingDelayMs); | |
| 73 } | |
| 74 mProgressAnimator.end(); | |
| 75 return; | |
| 76 } | |
| 77 } | |
| 78 }); | |
| 79 } | |
| 80 | |
| 29 /** | 81 /** |
| 30 * Creates a toolbar progress bar. | 82 * Creates a toolbar progress bar. |
| 31 * | 83 * |
| 32 * @param context the application environment. | 84 * @param context the application environment. |
| 33 * @param attrs the xml attributes that should be used to initialize this vi ew. | 85 * @param attrs the xml attributes that should be used to initialize this vi ew. |
| 34 */ | 86 */ |
| 35 public ToolbarProgressBar(Context context, AttributeSet attrs) { | 87 public ToolbarProgressBar(Context context, AttributeSet attrs) { |
| 36 super(context, attrs); | 88 super(context, attrs); |
| 37 setAlpha(mTargetAlpha); | 89 setAlpha(mTargetAlpha); |
| 38 } | 90 } |
| 39 | 91 |
| 40 /** | 92 /** |
| 93 * Initializes animation based on command line configuration. This must be c alled when native | |
| 94 * library is ready. | |
| 95 */ | |
| 96 public void initializeAnimation() { | |
| 97 assert mAnimationLogic == null; | |
| 98 | |
| 99 String animation = CommandLine.getInstance().getSwitchValue( | |
| 100 ChromeSwitches.PROGRESS_BAR_ANIMATION); | |
| 101 if (TextUtils.equals(animation, "smooth")) { | |
| 102 mAnimationLogic = new ProgressAnimationSmooth(); | |
| 103 } else if (TextUtils.equals(animation, "fast-start")) { | |
| 104 mAnimationLogic = new ProgressAnimationFastStart(); | |
| 105 } | |
| 106 } | |
| 107 | |
| 108 /** | |
| 41 * Start showing progress bar animation. | 109 * Start showing progress bar animation. |
| 42 */ | 110 */ |
| 43 public void start() { | 111 public void start() { |
| 44 mIsStarted = true; | 112 mIsStarted = true; |
| 113 super.setProgress(0.0f); | |
| 114 if (mAnimationLogic != null) mAnimationLogic.reset(); | |
| 45 removeCallbacks(mHideRunnable); | 115 removeCallbacks(mHideRunnable); |
| 46 animateAlphaTo(1.0f); | 116 animateAlphaTo(1.0f); |
| 47 } | 117 } |
| 48 | 118 |
| 49 /** | 119 /** |
| 50 * Start hiding progress bar animation. | 120 * Start hiding progress bar animation. |
| 51 * @param delayed Whether a delayed fading out animation should be posted. | 121 * @param delayed Whether a delayed fading out animation should be posted. |
| 52 */ | 122 */ |
| 53 public void finish(boolean delayed) { | 123 public void finish(boolean delayed) { |
| 54 mIsStarted = false; | 124 mIsStarted = false; |
| 55 | 125 |
| 56 removeCallbacks(mHideRunnable); | |
| 57 | |
| 58 if (delayed) { | 126 if (delayed) { |
| 59 postDelayed(mHideRunnable, mHidingDelayMs); | 127 updateVisibleProgress(); |
|
jdduke (slow)
2015/08/03 23:35:35
Silly ignorant question: Is finish() allowed to be
Kibeom Kim (inactive)
2015/08/04 02:08:44
Yeah currently finish() can be called multiple tim
| |
| 60 } else { | 128 } else { |
| 61 mTargetAlpha = 0.0f; | 129 mTargetAlpha = 0.0f; |
| 62 setAlpha(0.0f); | 130 setAlpha(0.0f); |
| 63 } | 131 } |
| 64 } | 132 } |
| 65 | 133 |
| 66 /** | 134 /** |
| 67 * Set alpha show&hide animation duration. This is for faster testing. | 135 * Set alpha show&hide animation duration. This is for faster testing. |
| 68 * @param alphaAnimationDurationMs Alpha animation duration in milliseconds. | 136 * @param alphaAnimationDurationMs Alpha animation duration in milliseconds. |
| 69 */ | 137 */ |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 86 float alphaDiff = targetAlpha - getAlpha(); | 154 float alphaDiff = targetAlpha - getAlpha(); |
| 87 if (alphaDiff != 0.0f) { | 155 if (alphaDiff != 0.0f) { |
| 88 animate().alpha(targetAlpha) | 156 animate().alpha(targetAlpha) |
| 89 .setDuration((long) Math.abs(alphaDiff * mAlphaAnimationDura tionMs)) | 157 .setDuration((long) Math.abs(alphaDiff * mAlphaAnimationDura tionMs)) |
| 90 .setInterpolator(alphaDiff > 0 | 158 .setInterpolator(alphaDiff > 0 |
| 91 ? BakedBezierInterpolator.FADE_IN_CURVE | 159 ? BakedBezierInterpolator.FADE_IN_CURVE |
| 92 : BakedBezierInterpolator.FADE_OUT_CURVE); | 160 : BakedBezierInterpolator.FADE_OUT_CURVE); |
| 93 } | 161 } |
| 94 } | 162 } |
| 95 | 163 |
| 164 private void updateVisibleProgress() { | |
| 165 if (mAnimationLogic == null) { | |
| 166 super.setProgress(mTargetProgress); | |
| 167 if (!mIsStarted) { | |
| 168 postDelayed(mHideRunnable, mHidingDelayMs); | |
| 169 } | |
| 170 } else { | |
| 171 if (!mProgressAnimator.isStarted()) mProgressAnimator.start(); | |
| 172 } | |
| 173 } | |
| 174 | |
| 96 // ClipDrawableProgressBar implementation. | 175 // ClipDrawableProgressBar implementation. |
| 97 | 176 |
| 98 @Override | 177 @Override |
| 99 public void setProgress(float progress) { | 178 public void setProgress(float progress) { |
| 100 assert mIsStarted; | 179 assert mIsStarted; |
| 101 super.setProgress(progress); | 180 assert getProgress() <= progress; |
| 181 | |
| 182 mTargetProgress = progress; | |
| 183 updateVisibleProgress(); | |
| 102 } | 184 } |
| 103 } | 185 } |
| OLD | NEW |