OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.browser.widget; |
| 6 |
| 7 import android.content.Context; |
| 8 import android.graphics.Canvas; |
| 9 import android.util.AttributeSet; |
| 10 import android.widget.ProgressBar; |
| 11 |
| 12 import org.chromium.base.annotations.SuppressFBWarnings; |
| 13 |
| 14 /** |
| 15 * A throttled version of {@link ProgressBar}. This steals calls to |
| 16 * {@link android.view.View#postInvalidateOnAnimation} and delays them if necess
ary to reach a |
| 17 * specific FPS. |
| 18 */ |
| 19 public class SlowedProgressBar extends ProgressBar { |
| 20 private long mLastDrawTimeMs = 0; |
| 21 private boolean mPendingInvalidation = false; |
| 22 private static final int MIN_MS_PER_FRAME = 66; |
| 23 private int mTargetProgress; |
| 24 |
| 25 private final Runnable mInvalidationRunnable = new Runnable() { |
| 26 @Override |
| 27 public void run() { |
| 28 mPendingInvalidation = false; |
| 29 invalidate(); |
| 30 } |
| 31 }; |
| 32 |
| 33 private final Runnable mUpdateProgressRunnable = new Runnable() { |
| 34 @Override |
| 35 public void run() { |
| 36 setProgressInternal(mTargetProgress); |
| 37 } |
| 38 }; |
| 39 |
| 40 /** |
| 41 * Create a new progress bar with range 0...100 and initial progress of 0. |
| 42 * @param context the application environment. |
| 43 */ |
| 44 public SlowedProgressBar(Context context) { |
| 45 super(context, null); |
| 46 } |
| 47 |
| 48 /** |
| 49 * Create a new progress bar with range 0...100 and initial progress of 0. |
| 50 * @param context the application environment. |
| 51 * @param attrs the xml attributes that should be used to initialize this vi
ew. |
| 52 */ |
| 53 public SlowedProgressBar(Context context, AttributeSet attrs) { |
| 54 super(context, attrs); |
| 55 } |
| 56 |
| 57 /** |
| 58 * Create a new progress bar with range 0...100 and initial progress of 0. |
| 59 * @param context the application environment. |
| 60 * @param attrs the xml attributes that should be used to initialize this vi
ew. |
| 61 * @param defStyle the default style to apply to this view. |
| 62 */ |
| 63 public SlowedProgressBar(Context context, AttributeSet attrs, int defStyle)
{ |
| 64 super(context, attrs, defStyle); |
| 65 } |
| 66 |
| 67 /** |
| 68 * Keep track of the last time we drew. |
| 69 */ |
| 70 @Override |
| 71 public void onDraw(Canvas canvas) { |
| 72 mLastDrawTimeMs = System.currentTimeMillis(); |
| 73 super.onDraw(canvas); |
| 74 } |
| 75 |
| 76 /** |
| 77 * Delay invalidations to be no more than 1 per MIN_MS_PER_FRAME; |
| 78 * never allow more than one invalidation outstanding. |
| 79 */ |
| 80 @Override |
| 81 public void postInvalidateOnAnimation() { |
| 82 if (mPendingInvalidation) return; |
| 83 long nextDrawTime = mLastDrawTimeMs + MIN_MS_PER_FRAME; |
| 84 long delay = Math.max(0, nextDrawTime - System.currentTimeMillis()); |
| 85 mPendingInvalidation = true; |
| 86 postOnAnimationDelayed(mInvalidationRunnable, delay); |
| 87 } |
| 88 |
| 89 /** |
| 90 * Ensure invalidations always occurs in the scope of animation. |
| 91 * @param progress The progress value to set the visuals to. |
| 92 */ |
| 93 @SuppressFBWarnings("CHROMIUM_SYNCHRONIZED_METHOD") |
| 94 @Override |
| 95 public synchronized void setProgress(int progress) { |
| 96 if (mTargetProgress == progress) return; |
| 97 mTargetProgress = progress; |
| 98 removeCallbacks(mUpdateProgressRunnable); |
| 99 postOnAnimation(mUpdateProgressRunnable); |
| 100 } |
| 101 |
| 102 /** |
| 103 * Called to update the progress visuals. |
| 104 * @param progress The progress value to set the visuals to. |
| 105 */ |
| 106 protected void setProgressInternal(int progress) { |
| 107 super.setProgress(progress); |
| 108 } |
| 109 } |
OLD | NEW |