MinTimeProgressView.java revision 30e2c24b056542f3b1b438aeb798305d1226d0c8
1/** 2 * Copyright (c) 2011, Google Inc. 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 */ 16package com.android.mail; 17 18import android.content.Context; 19import android.os.Handler; 20import android.util.AttributeSet; 21import android.view.View; 22import android.widget.ProgressBar; 23 24/** 25 * MinTimeProgressView implements a ProgressBar that waits MIN_DELAY ms to be 26 * dismissed before showing. Once visible, the progress bar will be visible for 27 * at least MIN_SHOW_TIME to avoid "flashes" in the UI when an event could take 28 * a largely variable time to complete (from none, to a user perceivable amount) 29 * 30 * @author mindyp 31 */ 32public class MinTimeProgressView extends ProgressBar { 33 private static int sMinShowTime; 34 35 private static int sMinDelay; 36 37 private long mStartTime = -1; 38 39 private final Handler mHandler = new Handler(); 40 41 private boolean mDismissed = false; 42 43 public MinTimeProgressView(Context context) { 44 this(context, null); 45 } 46 47 public MinTimeProgressView(Context context, AttributeSet attrs) { 48 super(context, attrs, R.style.MinTimeProgressViewStyle); 49 sMinShowTime = context.getResources() 50 .getInteger(R.integer.html_conv_progress_display_time); 51 sMinDelay = context.getResources() 52 .getInteger(R.integer.html_conv_progress_wait_time); 53 } 54 55 private final Runnable mDelayedHide = new Runnable() { 56 public void run() { 57 MinTimeProgressView.super.setVisibility(View.GONE); 58 } 59 }; 60 61 private final Runnable mDelayedShow = new Runnable() { 62 public void run() { 63 if (!mDismissed) { 64 mStartTime = System.currentTimeMillis(); 65 MinTimeProgressView.super.setVisibility(View.VISIBLE); 66 } 67 } 68 }; 69 70 private void hide() { 71 mDismissed = true; 72 long diff = System.currentTimeMillis() - mStartTime; 73 if (diff >= sMinShowTime || mStartTime == -1) { 74 // The progress spinner has been shown long enough 75 // OR was not shown yet. If it wasn't shown yet, 76 // it will just never be shown. 77 MinTimeProgressView.super.setVisibility(View.GONE); 78 } else { 79 // The progress spinner is shown, but not long enough, 80 // so put a delayed message in to hide it when its been 81 // shown long enough. 82 mHandler.postDelayed(mDelayedHide, sMinShowTime - diff); 83 } 84 } 85 86 private void show() { 87 // Reset the start time. 88 mStartTime = -1; 89 mHandler.postDelayed(mDelayedShow, sMinDelay); 90 } 91 92 @Override 93 public void setVisibility(int visibility) { 94 // Whenever the visibility gets changed, clear dismissed 95 // state. 96 mDismissed = false; 97 switch (visibility) { 98 case View.VISIBLE: 99 show(); 100 break; 101 case View.GONE: 102 hide(); 103 break; 104 default: 105 super.setVisibility(visibility); 106 } 107 } 108} 109