|
| 1 | +package fr.castorflex.android.smoothprogressbar; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.util.AttributeSet; |
| 5 | +import android.view.View; |
| 6 | + |
| 7 | +/** |
| 8 | + * This is a copy of the ContentLoadingProgressBar from the support library, but extends |
| 9 | + * SmoothProgressBar. |
| 10 | + */ |
| 11 | +public class ContentLoadingSmoothProgressBar extends SmoothProgressBar { |
| 12 | + |
| 13 | + private static final int MIN_SHOW_TIME = 500; // ms |
| 14 | + private static final int MIN_DELAY = 500; // ms |
| 15 | + |
| 16 | + private long mStartTime = -1; |
| 17 | + |
| 18 | + private boolean mPostedHide = false; |
| 19 | + |
| 20 | + private boolean mPostedShow = false; |
| 21 | + |
| 22 | + private boolean mDismissed = false; |
| 23 | + |
| 24 | + private final Runnable mDelayedHide = new Runnable() { |
| 25 | + |
| 26 | + @Override |
| 27 | + public void run() { |
| 28 | + mPostedHide = false; |
| 29 | + mStartTime = -1; |
| 30 | + setVisibility(View.GONE); |
| 31 | + } |
| 32 | + }; |
| 33 | + |
| 34 | + private final Runnable mDelayedShow = new Runnable() { |
| 35 | + |
| 36 | + @Override |
| 37 | + public void run() { |
| 38 | + mPostedShow = false; |
| 39 | + if (!mDismissed) { |
| 40 | + mStartTime = System.currentTimeMillis(); |
| 41 | + setVisibility(View.VISIBLE); |
| 42 | + } |
| 43 | + } |
| 44 | + }; |
| 45 | + |
| 46 | + public ContentLoadingSmoothProgressBar(Context context) { |
| 47 | + this(context, null); |
| 48 | + } |
| 49 | + |
| 50 | + public ContentLoadingSmoothProgressBar(Context context, AttributeSet attrs) { |
| 51 | + super(context, attrs, 0); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void onAttachedToWindow() { |
| 56 | + super.onAttachedToWindow(); |
| 57 | + removeCallbacks(); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public void onDetachedFromWindow() { |
| 62 | + super.onDetachedFromWindow(); |
| 63 | + removeCallbacks(); |
| 64 | + } |
| 65 | + |
| 66 | + private void removeCallbacks() { |
| 67 | + removeCallbacks(mDelayedHide); |
| 68 | + removeCallbacks(mDelayedShow); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Hide the progress view if it is visible. The progress view will not be |
| 73 | + * hidden until it has been shown for at least a minimum show time. If the |
| 74 | + * progress view was not yet visible, cancels showing the progress view. |
| 75 | + */ |
| 76 | + public void hide() { |
| 77 | + mDismissed = true; |
| 78 | + removeCallbacks(mDelayedShow); |
| 79 | + long diff = System.currentTimeMillis() - mStartTime; |
| 80 | + if (diff >= MIN_SHOW_TIME || mStartTime == -1) { |
| 81 | + // The progress spinner has been shown long enough |
| 82 | + // OR was not shown yet. If it wasn't shown yet, |
| 83 | + // it will just never be shown. |
| 84 | + setVisibility(View.GONE); |
| 85 | + } else { |
| 86 | + // The progress spinner is shown, but not long enough, |
| 87 | + // so put a delayed message in to hide it when its been |
| 88 | + // shown long enough. |
| 89 | + if (!mPostedHide) { |
| 90 | + postDelayed(mDelayedHide, MIN_SHOW_TIME - diff); |
| 91 | + mPostedHide = true; |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Show the progress view after waiting for a minimum delay. If |
| 98 | + * during that time, hide() is called, the view is never made visible. |
| 99 | + */ |
| 100 | + public void show() { |
| 101 | + // Reset the start time. |
| 102 | + mStartTime = -1; |
| 103 | + mDismissed = false; |
| 104 | + removeCallbacks(mDelayedHide); |
| 105 | + if (!mPostedShow) { |
| 106 | + postDelayed(mDelayedShow, MIN_DELAY); |
| 107 | + mPostedShow = true; |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments