diff --git a/src/com/androidquery/callback/AbstractAjaxCallback.java b/src/com/androidquery/callback/AbstractAjaxCallback.java index 4474ac74..ec549623 100644 --- a/src/com/androidquery/callback/AbstractAjaxCallback.java +++ b/src/com/androidquery/callback/AbstractAjaxCallback.java @@ -83,21 +83,21 @@ import android.app.Activity; import android.app.Dialog; +import android.app.ProgressDialog; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.util.Xml; import android.view.View; +import android.widget.ProgressBar; import com.androidquery.AQuery; import com.androidquery.auth.AccountHandle; import com.androidquery.auth.GoogleHandle; import com.androidquery.util.AQUtility; -import com.androidquery.util.Common; import com.androidquery.util.Constants; import com.androidquery.util.PredefinedBAOS; -import com.androidquery.util.Progress; import com.androidquery.util.XmlDom; /** @@ -117,6 +117,7 @@ public abstract class AbstractAjaxCallback implements Runnable{ private Object handler; private String callback; private WeakReference progress; + private ProgressStrategy pStrategy; private String url; private Map params; @@ -451,7 +452,7 @@ public K params(Map params){ * @return self */ public K progress(View view){ - return progress((Object) view); + return progress((Object) view, null); } /** @@ -461,12 +462,28 @@ public K progress(View view){ * @return self */ public K progress(Dialog dialog){ - return progress((Object) dialog); + return progress((Object) dialog, null); } public K progress(Object progress){ + return progress(progress, null); + } + + public K progress(Object progress, ProgressStrategy customStrategy){ if(progress != null){ this.progress = new WeakReference(progress); + this.pStrategy = customStrategy; + } + if(progress != null && pStrategy == null) { + if(progress instanceof ProgressBar){ + pStrategy = new ProgressBarStrategy(); + }else if(progress instanceof ProgressDialog){ + pStrategy = new ProgressDialogStrategy(); + }else if(progress instanceof Activity){ + pStrategy = new ActivityStrategy(); + }else if(progress instanceof View){ + pStrategy = new ViewStrategy(); + } } return self(); } @@ -590,13 +607,13 @@ protected void showProgress(final boolean show){ if(p != null){ if(AQUtility.isUIThread()){ - Common.showProgress(p, url, show); + pStrategy.showProgress(p, url, show); }else{ AQUtility.post(new Runnable() { @Override public void run() { - Common.showProgress(p, url, show); + pStrategy.showProgress(p, url, show); } }); } @@ -1567,13 +1584,7 @@ private void copy(InputStream is, OutputStream os, String encoding, int max) thr o = progress.get(); } - Progress p = null; - - if(o != null){ - p = new Progress(o); - } - - AQUtility.copy(is, os, max, p); + AQUtility.copy(is, os, max, o, pStrategy); } diff --git a/src/com/androidquery/callback/ProgressStrategy.java b/src/com/androidquery/callback/ProgressStrategy.java new file mode 100644 index 00000000..ae698a7b --- /dev/null +++ b/src/com/androidquery/callback/ProgressStrategy.java @@ -0,0 +1,156 @@ +package com.androidquery.callback; + +import com.androidquery.util.Common; + +import android.app.Activity; +import android.app.ProgressDialog; +import android.widget.ProgressBar; + +public interface ProgressStrategy { + + public void reset(Object progress); + public void setBytes(Object progress, int bytes); + public void increment(Object progress, int delta); + public void done(Object progress); + public void showProgress(Object progress, String url, boolean show); +} + +/* package */ abstract class StrategyBase implements ProgressStrategy { + + protected boolean unknown; + protected int bytes; + protected int current; + protected String url; + + @Override + public void reset(Object progress) { + unknown = false; + current = 0; + bytes = 10000; + } + + @Override + public void setBytes(Object progress, int bytes) { + if(bytes <= 0){ + unknown = true; + bytes = 10000; + } + + this.bytes = bytes; + } + + @Override + public void increment(Object progress, int delta) { + } + + @Override + public void done(Object progress) { + } + + @Override + public void showProgress(Object progress, String url, boolean show) { + Common.showProgress(progress, url, show); + } + +} + +/* package */ class ProgressBarStrategy extends StrategyBase { + + @Override + public void reset(Object progress) { + ProgressBar pb = (ProgressBar) progress; + pb.setProgress(0); + pb.setMax(10000); + super.reset(progress); + } + + @Override + public void setBytes(Object progress, int bytes) { + super.setBytes(progress, bytes); + ProgressBar pb = (ProgressBar) progress; + pb.setProgress(0); + pb.setMax(bytes); + } + + @Override + public void increment(Object progress, int delta) { + ProgressBar pb = (ProgressBar) progress; + pb.incrementProgressBy(unknown ? 1 : delta); + } + + @Override + public void done(Object progress) { + ProgressBar pb = (ProgressBar) progress; + pb.setProgress(pb.getMax()); + } + +} + +/* package */ class ProgressDialogStrategy extends StrategyBase { + + @Override + public void reset(Object progress) { + ProgressDialog pd = (ProgressDialog) progress; + pd.setProgress(0); + pd.setMax(10000); + super.reset(progress); + } + + @Override + public void setBytes(Object progress, int bytes) { + super.setBytes(progress, bytes); + ProgressDialog pd = (ProgressDialog) progress; + pd.setProgress(0); + pd.setMax(bytes); + } + + @Override + public void increment(Object progress, int delta) { + ProgressDialog pd = (ProgressDialog) progress; + pd.incrementProgressBy(unknown ? 1 : delta); + } + + @Override + public void done(Object progress) { + ProgressDialog pd = (ProgressDialog) progress; + pd.setProgress(pd.getMax()); + } + +} + +/* package */ class ActivityStrategy extends StrategyBase { + + @Override + public void reset(Object progress) { + Activity act = (Activity) progress; + act.setProgress(0); + super.reset(progress); + } + + @Override + public void increment(Object progress, int delta) { + Activity act = (Activity) progress; + int p; + if(unknown){ + p = current++; + }else{ + current+= delta; + p = (10000 * current) / bytes; + } + if(p > 9999){ + p = 9999; + } + act.setProgress(p); + } + + @Override + public void done(Object progress) { + Activity act = (Activity) progress; + act.setProgress(9999); + } + +} + +/* package */ class ViewStrategy extends StrategyBase { + +} diff --git a/src/com/androidquery/util/AQUtility.java b/src/com/androidquery/util/AQUtility.java index bbd5ea27..b26bc71b 100644 --- a/src/com/androidquery/util/AQUtility.java +++ b/src/com/androidquery/util/AQUtility.java @@ -47,6 +47,7 @@ import android.view.animation.AlphaAnimation; import com.androidquery.AQuery; +import com.androidquery.callback.ProgressStrategy; /** * Utility methods. Warning: Methods might changed in future versions. @@ -322,16 +323,16 @@ private static byte[] getMD5(byte[] data){ private static final int IO_BUFFER_SIZE = 1024; public static void copy(InputStream in, OutputStream out) throws IOException { //copy(in, out, 0, null, null); - copy(in, out, 0, null); + copy(in, out, 0, null, null); } - public static void copy(InputStream in, OutputStream out, int max, Progress progress) throws IOException { + public static void copy(InputStream in, OutputStream out, int max, Object progress, ProgressStrategy pStrategy) throws IOException { AQUtility.debug("content header", max); if(progress != null){ - progress.reset(); - progress.setBytes(max); + pStrategy.reset(progress); + pStrategy.setBytes(progress, max); } byte[] b = new byte[IO_BUFFER_SIZE]; @@ -339,12 +340,12 @@ public static void copy(InputStream in, OutputStream out, int max, Progress prog while((read = in.read(b)) != -1){ out.write(b, 0, read); if(progress != null){ - progress.increment(read); + pStrategy.increment(progress, read); } } if(progress != null){ - progress.done(); + pStrategy.done(progress); } } diff --git a/src/com/androidquery/util/Progress.java b/src/com/androidquery/util/Progress.java deleted file mode 100644 index 1365384b..00000000 --- a/src/com/androidquery/util/Progress.java +++ /dev/null @@ -1,258 +0,0 @@ -package com.androidquery.util; - -import android.app.Activity; -import android.app.Dialog; -import android.app.ProgressDialog; -import android.view.View; -import android.widget.ProgressBar; - -import com.androidquery.AQuery; - -public class Progress implements Runnable{ - - private ProgressBar pb; - private ProgressDialog pd; - private Activity act; - private View view; - private boolean unknown; - private int bytes; - private int current; - private String url; - - public Progress(Object p){ - - if(p instanceof ProgressBar){ - pb = (ProgressBar) p; - }else if(p instanceof ProgressDialog){ - pd = (ProgressDialog) p; - }else if(p instanceof Activity){ - act = (Activity) p; - }else if(p instanceof View){ - view = (View) p; - } - - } - - public void reset(){ - - if(pb != null){ - pb.setProgress(0); - pb.setMax(10000); - } - if(pd != null){ - pd.setProgress(0); - pd.setMax(10000); - } - - if(act != null){ - act.setProgress(0); - } - - unknown = false; - current = 0; - bytes = 10000; - - } - - public void setBytes(int bytes){ - - if(bytes <= 0){ - unknown = true; - bytes = 10000; - } - - this.bytes = bytes; - - if(pb != null){ - pb.setProgress(0); - pb.setMax(bytes); - } - if(pd != null){ - pd.setProgress(0); - pd.setMax(bytes); - } - - - } - - public void increment(int delta){ - - if(pb != null){ - pb.incrementProgressBy(unknown ? 1 : delta); - } - - if(pd != null){ - pd.incrementProgressBy(unknown ? 1 : delta); - } - - if(act != null){ - int p; - if(unknown){ - p = current++; - }else{ - current+= delta; - p = (10000 * current) / bytes; - } - if(p > 9999){ - p = 9999; - } - act.setProgress(p); - } - - } - - - public void done(){ - - if(pb != null){ - pb.setProgress(pb.getMax()); - } - if(pd != null){ - pd.setProgress(pd.getMax()); - } - - if(act != null){ - act.setProgress(9999); - } - - } - - @Override - public void run() { - dismiss(url); - } - - public void show(String url){ - - reset(); - - if(pd != null){ - AQuery aq = new AQuery(pd.getContext()); - aq.show(pd); - } - - if(act != null){ - act.setProgressBarIndeterminateVisibility(true); - act.setProgressBarVisibility(true); - } - - if(pb != null){ - pb.setTag(AQuery.TAG_URL, url); - pb.setVisibility(View.VISIBLE); - } - - if(view != null){ - view.setTag(AQuery.TAG_URL, url); - view.setVisibility(View.VISIBLE); - } - - } - - public void hide(String url){ - - if(AQUtility.isUIThread()){ - dismiss(url); - }else{ - this.url = url; - AQUtility.post(this); - } - - } - - private void dismiss(String url){ - - if(pd != null){ - AQuery aq = new AQuery(pd.getContext()); - aq.dismiss(pd); - } - - if(act != null){ - act.setProgressBarIndeterminateVisibility(false); - act.setProgressBarVisibility(false); - } - - if(pb != null){ - pb.setTag(AQuery.TAG_URL, url); - pb.setVisibility(View.VISIBLE); - } - - View pv = pb; - if(pv == null){ - pv = view; - } - - if(pv != null){ - - Object tag = pv.getTag(AQuery.TAG_URL); - if(tag == null || tag.equals(url)){ - pv.setTag(AQuery.TAG_URL, null); - - if(pb != null && pb.isIndeterminate()){ - pv.setVisibility(View.GONE); - } - } - } - - } - - - private void showProgress(Object p, String url, boolean show){ - - if(p != null){ - - if(p instanceof View){ - - View pv = (View) p; - - ProgressBar pbar = null; - - if(p instanceof ProgressBar){ - pbar = (ProgressBar) p; - } - - if(show){ - pv.setTag(AQuery.TAG_URL, url); - pv.setVisibility(View.VISIBLE); - if(pbar != null){ - pbar.setProgress(0); - pbar.setMax(100); - } - - }else{ - Object tag = pv.getTag(AQuery.TAG_URL); - if(tag == null || tag.equals(url)){ - pv.setTag(AQuery.TAG_URL, null); - - if(pbar != null && pbar.isIndeterminate()){ - pv.setVisibility(View.GONE); - } - } - } - }else if(p instanceof Dialog){ - - Dialog pd = (Dialog) p; - - AQuery aq = new AQuery(pd.getContext()); - - if(show){ - aq.show(pd); - }else{ - aq.dismiss(pd); - } - - }else if(p instanceof Activity){ - - Activity act = (Activity) p;; - act.setProgressBarIndeterminateVisibility(show); - act.setProgressBarVisibility(show); - - if(show){ - act.setProgress(0); - } - } - } - - } - - -}