The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 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 | */ |
| 16 | |
| 17 | package android.app; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.graphics.drawable.Drawable; |
| 21 | import android.os.Bundle; |
| 22 | import android.os.Handler; |
| 23 | import android.os.Message; |
| 24 | import android.view.LayoutInflater; |
| 25 | import android.view.View; |
| 26 | import android.widget.ProgressBar; |
| 27 | import android.widget.TextView; |
| 28 | |
| 29 | import com.android.internal.R; |
| 30 | |
| 31 | import java.text.NumberFormat; |
| 32 | |
| 33 | /** |
| 34 | * <p>A dialog showing a progress indicator and an optional text message or view. |
| 35 | * Only a text message or a view can be used at the same time.</p> |
| 36 | * <p>The dialog can be made cancelable on back key press.</p> |
| 37 | * <p>The progress range is 0..10000.</p> |
| 38 | */ |
| 39 | public class ProgressDialog extends AlertDialog { |
| 40 | |
| 41 | /** Creates a ProgressDialog with a ciruclar, spinning progress |
| 42 | * bar. This is the default. |
| 43 | */ |
| 44 | public static final int STYLE_SPINNER = 0; |
| 45 | |
| 46 | /** Creates a ProgressDialog with a horizontal progress bar. |
| 47 | */ |
| 48 | public static final int STYLE_HORIZONTAL = 1; |
| 49 | |
| 50 | private ProgressBar mProgress; |
| 51 | private TextView mMessageView; |
| 52 | |
| 53 | private int mProgressStyle = STYLE_SPINNER; |
| 54 | private TextView mProgressNumber; |
| 55 | private TextView mProgressPercent; |
| 56 | private NumberFormat mProgressPercentFormat; |
| 57 | |
| 58 | private int mMax; |
| 59 | private int mProgressVal; |
| 60 | private int mSecondaryProgressVal; |
| 61 | private int mIncrementBy; |
| 62 | private int mIncrementSecondaryBy; |
| 63 | private Drawable mProgressDrawable; |
| 64 | private Drawable mIndeterminateDrawable; |
| 65 | private CharSequence mMessage; |
| 66 | private boolean mIndeterminate; |
| 67 | |
| 68 | private boolean mHasStarted; |
| 69 | private Handler mViewUpdateHandler; |
| 70 | |
| 71 | public ProgressDialog(Context context) { |
| 72 | this(context, com.android.internal.R.style.Theme_Dialog_Alert); |
| 73 | } |
| 74 | |
| 75 | public ProgressDialog(Context context, int theme) { |
| 76 | super(context, theme); |
| 77 | } |
| 78 | |
| 79 | public static ProgressDialog show(Context context, CharSequence title, |
| 80 | CharSequence message) { |
| 81 | return show(context, title, message, false); |
| 82 | } |
| 83 | |
| 84 | public static ProgressDialog show(Context context, CharSequence title, |
| 85 | CharSequence message, boolean indeterminate) { |
| 86 | return show(context, title, message, indeterminate, false, null); |
| 87 | } |
| 88 | |
| 89 | public static ProgressDialog show(Context context, CharSequence title, |
| 90 | CharSequence message, boolean indeterminate, boolean cancelable) { |
| 91 | return show(context, title, message, indeterminate, cancelable, null); |
| 92 | } |
| 93 | |
| 94 | public static ProgressDialog show(Context context, CharSequence title, |
| 95 | CharSequence message, boolean indeterminate, |
| 96 | boolean cancelable, OnCancelListener cancelListener) { |
| 97 | ProgressDialog dialog = new ProgressDialog(context); |
| 98 | dialog.setTitle(title); |
| 99 | dialog.setMessage(message); |
| 100 | dialog.setIndeterminate(indeterminate); |
| 101 | dialog.setCancelable(cancelable); |
| 102 | dialog.setOnCancelListener(cancelListener); |
| 103 | dialog.show(); |
| 104 | return dialog; |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | protected void onCreate(Bundle savedInstanceState) { |
| 109 | LayoutInflater inflater = LayoutInflater.from(mContext); |
| 110 | if (mProgressStyle == STYLE_HORIZONTAL) { |
| 111 | |
| 112 | /* Use a separate handler to update the text views as they |
| 113 | * must be updated on the same thread that created them. |
| 114 | */ |
| 115 | mViewUpdateHandler = new Handler() { |
| 116 | @Override |
| 117 | public void handleMessage(Message msg) { |
| 118 | super.handleMessage(msg); |
| 119 | |
| 120 | /* Update the number and percent */ |
| 121 | int progress = mProgress.getProgress(); |
| 122 | int max = mProgress.getMax(); |
| 123 | double percent = (double) progress / (double) max; |
| 124 | mProgressNumber.setText(progress + "/" + max); |
| 125 | mProgressPercent.setText(mProgressPercentFormat.format(percent)); |
| 126 | } |
| 127 | }; |
| 128 | View view = inflater.inflate(R.layout.alert_dialog_progress, null); |
| 129 | mProgress = (ProgressBar) view.findViewById(R.id.progress); |
| 130 | mProgressNumber = (TextView) view.findViewById(R.id.progress_number); |
| 131 | mProgressPercent = (TextView) view.findViewById(R.id.progress_percent); |
| 132 | mProgressPercentFormat = NumberFormat.getPercentInstance(); |
| 133 | mProgressPercentFormat.setMaximumFractionDigits(0); |
| 134 | setView(view); |
| 135 | } else { |
| 136 | View view = inflater.inflate(R.layout.progress_dialog, null); |
| 137 | mProgress = (ProgressBar) view.findViewById(R.id.progress); |
| 138 | mMessageView = (TextView) view.findViewById(R.id.message); |
| 139 | setView(view); |
| 140 | } |
| 141 | if (mMax > 0) { |
| 142 | setMax(mMax); |
| 143 | } |
| 144 | if (mProgressVal > 0) { |
| 145 | setProgress(mProgressVal); |
| 146 | } |
| 147 | if (mSecondaryProgressVal > 0) { |
| 148 | setSecondaryProgress(mSecondaryProgressVal); |
| 149 | } |
| 150 | if (mIncrementBy > 0) { |
| 151 | incrementProgressBy(mIncrementBy); |
| 152 | } |
| 153 | if (mIncrementSecondaryBy > 0) { |
| 154 | incrementSecondaryProgressBy(mIncrementSecondaryBy); |
| 155 | } |
| 156 | if (mProgressDrawable != null) { |
| 157 | setProgressDrawable(mProgressDrawable); |
| 158 | } |
| 159 | if (mIndeterminateDrawable != null) { |
| 160 | setIndeterminateDrawable(mIndeterminateDrawable); |
| 161 | } |
| 162 | if (mMessage != null) { |
| 163 | setMessage(mMessage); |
| 164 | } |
| 165 | setIndeterminate(mIndeterminate); |
| 166 | onProgressChanged(); |
| 167 | super.onCreate(savedInstanceState); |
| 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public void onStart() { |
| 172 | super.onStart(); |
| 173 | mHasStarted = true; |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | protected void onStop() { |
| 178 | super.onStop(); |
| 179 | mHasStarted = false; |
| 180 | } |
| 181 | |
| 182 | public void setProgress(int value) { |
| 183 | if (mHasStarted) { |
| 184 | mProgress.setProgress(value); |
| 185 | onProgressChanged(); |
| 186 | } else { |
| 187 | mProgressVal = value; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | public void setSecondaryProgress(int secondaryProgress) { |
| 192 | if (mProgress != null) { |
| 193 | mProgress.setSecondaryProgress(secondaryProgress); |
| 194 | onProgressChanged(); |
| 195 | } else { |
| 196 | mSecondaryProgressVal = secondaryProgress; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | public int getProgress() { |
| 201 | if (mProgress != null) { |
| 202 | return mProgress.getProgress(); |
| 203 | } |
| 204 | return mProgressVal; |
| 205 | } |
| 206 | |
| 207 | public int getSecondaryProgress() { |
| 208 | if (mProgress != null) { |
| 209 | return mProgress.getSecondaryProgress(); |
| 210 | } |
| 211 | return mSecondaryProgressVal; |
| 212 | } |
| 213 | |
| 214 | public int getMax() { |
| 215 | if (mProgress != null) { |
| 216 | return mProgress.getMax(); |
| 217 | } |
| 218 | return mMax; |
| 219 | } |
| 220 | |
| 221 | public void setMax(int max) { |
| 222 | if (mProgress != null) { |
| 223 | mProgress.setMax(max); |
| 224 | onProgressChanged(); |
| 225 | } else { |
| 226 | mMax = max; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | public void incrementProgressBy(int diff) { |
| 231 | if (mProgress != null) { |
| 232 | mProgress.incrementProgressBy(diff); |
| 233 | onProgressChanged(); |
| 234 | } else { |
| 235 | mIncrementBy += diff; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | public void incrementSecondaryProgressBy(int diff) { |
| 240 | if (mProgress != null) { |
| 241 | mProgress.incrementSecondaryProgressBy(diff); |
| 242 | onProgressChanged(); |
| 243 | } else { |
| 244 | mIncrementSecondaryBy += diff; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | public void setProgressDrawable(Drawable d) { |
| 249 | if (mProgress != null) { |
| 250 | mProgress.setProgressDrawable(d); |
| 251 | } else { |
| 252 | mProgressDrawable = d; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | public void setIndeterminateDrawable(Drawable d) { |
| 257 | if (mProgress != null) { |
| 258 | mProgress.setIndeterminateDrawable(d); |
| 259 | } else { |
| 260 | mIndeterminateDrawable = d; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | public void setIndeterminate(boolean indeterminate) { |
| 265 | if (mProgress != null) { |
| 266 | mProgress.setIndeterminate(indeterminate); |
| 267 | } else { |
| 268 | mIndeterminate = indeterminate; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | public boolean isIndeterminate() { |
| 273 | if (mProgress != null) { |
| 274 | return mProgress.isIndeterminate(); |
| 275 | } |
| 276 | return mIndeterminate; |
| 277 | } |
| 278 | |
| 279 | @Override |
| 280 | public void setMessage(CharSequence message) { |
| 281 | if (mProgress != null) { |
| 282 | if (mProgressStyle == STYLE_HORIZONTAL) { |
| 283 | super.setMessage(message); |
| 284 | } else { |
| 285 | mMessageView.setText(message); |
| 286 | } |
| 287 | } else { |
| 288 | mMessage = message; |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | public void setProgressStyle(int style) { |
| 293 | mProgressStyle = style; |
| 294 | } |
| 295 | |
| 296 | private void onProgressChanged() { |
| 297 | if (mProgressStyle == STYLE_HORIZONTAL) { |
| 298 | mViewUpdateHandler.sendEmptyMessage(0); |
| 299 | } |
| 300 | } |
| 301 | } |