1 package org.coolreader.crengine; 2 3 import org.coolreader.R; 4 5 import android.app.AlertDialog; 6 import android.content.Context; 7 import android.graphics.drawable.Drawable; 8 import android.os.Bundle; 9 import android.os.Handler; 10 import android.os.Message; 11 import android.view.LayoutInflater; 12 import android.view.View; 13 import android.widget.ProgressBar; 14 import android.widget.TextView; 15 16 /** 17 * Progress dialog, based on source code of android.app.ProgressDialog 18 */ 19 public class ProgressDialog extends AlertDialog { 20 21 /** Creates a ProgressDialog with a ciruclar, spinning progress 22 * bar. This is the default. 23 */ 24 public static final int STYLE_SPINNER = 0; 25 26 /** Creates a ProgressDialog with a horizontal progress bar. 27 */ 28 public static final int STYLE_HORIZONTAL = 1; 29 30 private ProgressBar mProgress; 31 private TextView mMessageView; 32 33 private int mProgressStyle = STYLE_SPINNER; 34 35 private int mMax; 36 private int mProgressVal; 37 private int mSecondaryProgressVal; 38 private int mIncrementBy; 39 private int mIncrementSecondaryBy; 40 private Drawable mProgressDrawable; 41 private Drawable mIndeterminateDrawable; 42 private CharSequence mMessage; 43 private boolean mIndeterminate; 44 45 private boolean mHasStarted; 46 private Handler mViewUpdateHandler; 47 private Context mContext; 48 ProgressDialog(Context context)49 public ProgressDialog(Context context) { 50 super(context); 51 //super(context, android.R.style.Theme_Light); 52 mContext = context; 53 } 54 show(Context context, CharSequence title, CharSequence message)55 public static ProgressDialog show(Context context, CharSequence title, 56 CharSequence message) { 57 return show(context, title, message, false); 58 } 59 show(Context context, CharSequence title, CharSequence message, boolean indeterminate)60 public static ProgressDialog show(Context context, CharSequence title, 61 CharSequence message, boolean indeterminate) { 62 return show(context, title, message, indeterminate, false, null); 63 } 64 show(Context context, CharSequence title, CharSequence message, boolean indeterminate, boolean cancelable)65 public static ProgressDialog show(Context context, CharSequence title, 66 CharSequence message, boolean indeterminate, boolean cancelable) { 67 return show(context, title, message, indeterminate, cancelable, null); 68 } 69 show(Context context, CharSequence title, CharSequence message, boolean indeterminate, boolean cancelable, OnCancelListener cancelListener)70 public static ProgressDialog show(Context context, CharSequence title, 71 CharSequence message, boolean indeterminate, 72 boolean cancelable, OnCancelListener cancelListener) { 73 ProgressDialog dialog = new ProgressDialog(context); 74 dialog.setTitle(title); 75 dialog.setMessage(message); 76 dialog.setIndeterminate(indeterminate); 77 dialog.setCancelable(cancelable); 78 dialog.setOnCancelListener(cancelListener); 79 dialog.show(); 80 return dialog; 81 } 82 83 @Override onCreate(Bundle savedInstanceState)84 protected void onCreate(Bundle savedInstanceState) { 85 LayoutInflater inflater = LayoutInflater.from(mContext); 86 if (mProgressStyle == STYLE_HORIZONTAL) { 87 88 /* Use a separate handler to update the text views as they 89 * must be updated on the same thread that created them. 90 */ 91 mViewUpdateHandler = new Handler() { 92 @Override 93 public void handleMessage(Message msg) { 94 super.handleMessage(msg); 95 } 96 }; 97 View view = inflater.inflate(R.layout.alert_dialog_progress, null); 98 mProgress = (ProgressBar) view.findViewById(R.id.progress); 99 //view.setBackgroundColor(Color.WHITE); 100 setView(view); 101 // if ( DeviceInfo.FORCE_LIGHT_THEME ) { 102 // //getWindow() setTheme(android.R.style.Theme_Light); 103 // getWindow().setBackgroundDrawableResource(drawable.editbox_background); 104 // } 105 } else { 106 View view = inflater.inflate(R.layout.progress_dialog, null); 107 mProgress = (ProgressBar) view.findViewById(R.id.progress); 108 mMessageView = (TextView) view.findViewById(R.id.message); 109 setView(view); 110 } 111 if (mMax > 0) { 112 setMax(mMax); 113 } 114 if (mProgressVal > 0) { 115 setProgress(mProgressVal); 116 } 117 if (mSecondaryProgressVal > 0) { 118 setSecondaryProgress(mSecondaryProgressVal); 119 } 120 if (mIncrementBy > 0) { 121 incrementProgressBy(mIncrementBy); 122 } 123 if (mIncrementSecondaryBy > 0) { 124 incrementSecondaryProgressBy(mIncrementSecondaryBy); 125 } 126 if (mProgressDrawable != null) { 127 setProgressDrawable(mProgressDrawable); 128 } 129 if (mIndeterminateDrawable != null) { 130 setIndeterminateDrawable(mIndeterminateDrawable); 131 } 132 if (mMessage != null) { 133 setMessage(mMessage); 134 } 135 setIndeterminate(mIndeterminate); 136 onProgressChanged(); 137 super.onCreate(savedInstanceState); 138 } 139 140 @Override onStart()141 public void onStart() { 142 super.onStart(); 143 mHasStarted = true; 144 } 145 146 @Override onStop()147 protected void onStop() { 148 super.onStop(); 149 mHasStarted = false; 150 } 151 setProgress(int value)152 public void setProgress(int value) { 153 if (mHasStarted && mProgress!=null) { 154 mProgress.setProgress(value); 155 onProgressChanged(); 156 } else { 157 mProgressVal = value; 158 } 159 } 160 setSecondaryProgress(int secondaryProgress)161 public void setSecondaryProgress(int secondaryProgress) { 162 if (mProgress != null) { 163 mProgress.setSecondaryProgress(secondaryProgress); 164 onProgressChanged(); 165 } else { 166 mSecondaryProgressVal = secondaryProgress; 167 } 168 } 169 getProgress()170 public int getProgress() { 171 if (mProgress != null) { 172 return mProgress.getProgress(); 173 } 174 return mProgressVal; 175 } 176 getSecondaryProgress()177 public int getSecondaryProgress() { 178 if (mProgress != null) { 179 return mProgress.getSecondaryProgress(); 180 } 181 return mSecondaryProgressVal; 182 } 183 getMax()184 public int getMax() { 185 if (mProgress != null) { 186 return mProgress.getMax(); 187 } 188 return mMax; 189 } 190 setMax(int max)191 public void setMax(int max) { 192 if (mProgress != null) { 193 mProgress.setMax(max); 194 onProgressChanged(); 195 } else { 196 mMax = max; 197 } 198 } 199 incrementProgressBy(int diff)200 public void incrementProgressBy(int diff) { 201 if (mProgress != null) { 202 mProgress.incrementProgressBy(diff); 203 onProgressChanged(); 204 } else { 205 mIncrementBy += diff; 206 } 207 } 208 incrementSecondaryProgressBy(int diff)209 public void incrementSecondaryProgressBy(int diff) { 210 if (mProgress != null) { 211 mProgress.incrementSecondaryProgressBy(diff); 212 onProgressChanged(); 213 } else { 214 mIncrementSecondaryBy += diff; 215 } 216 } 217 setProgressDrawable(Drawable d)218 public void setProgressDrawable(Drawable d) { 219 if (mProgress != null) { 220 mProgress.setProgressDrawable(d); 221 } else { 222 mProgressDrawable = d; 223 } 224 } 225 setIndeterminateDrawable(Drawable d)226 public void setIndeterminateDrawable(Drawable d) { 227 if (mProgress != null) { 228 mProgress.setIndeterminateDrawable(d); 229 } else { 230 mIndeterminateDrawable = d; 231 } 232 } 233 setIndeterminate(boolean indeterminate)234 public void setIndeterminate(boolean indeterminate) { 235 if (mProgress != null) { 236 mProgress.setIndeterminate(indeterminate); 237 } else { 238 mIndeterminate = indeterminate; 239 } 240 } 241 isIndeterminate()242 public boolean isIndeterminate() { 243 if (mProgress != null) { 244 return mProgress.isIndeterminate(); 245 } 246 return mIndeterminate; 247 } 248 249 @Override setMessage(CharSequence message)250 public void setMessage(CharSequence message) { 251 if (mProgress != null) { 252 if (mProgressStyle == STYLE_HORIZONTAL) { 253 super.setMessage(message); 254 } else { 255 mMessageView.setText(message); 256 } 257 } else { 258 mMessage = message; 259 } 260 } 261 setProgressStyle(int style)262 public void setProgressStyle(int style) { 263 mProgressStyle = style; 264 } 265 onProgressChanged()266 private void onProgressChanged() { 267 if (mProgressStyle == STYLE_HORIZONTAL) { 268 mViewUpdateHandler.sendEmptyMessage(0); 269 } 270 } 271 } 272