From adf860cd9edec2bddb5ad0330c08a3a04c2b4bcc Mon Sep 17 00:00:00 2001 From: Yuli Huang Date: Mon, 14 Nov 2011 22:38:16 +0800 Subject: [PATCH] Fix b/5517002 by dismissing running progress dialog in onPause(). Change-Id: I524f876e53776c38bc850120a2d7b7e6381ca33a --- .../photoeditor/SpinnerProgressDialog.java | 59 ++++++++++++---------- 1 file changed, 33 insertions(+), 26 deletions(-) diff --git a/src/com/android/gallery3d/photoeditor/SpinnerProgressDialog.java b/src/com/android/gallery3d/photoeditor/SpinnerProgressDialog.java index 207c2d142..065075e52 100644 --- a/src/com/android/gallery3d/photoeditor/SpinnerProgressDialog.java +++ b/src/com/android/gallery3d/photoeditor/SpinnerProgressDialog.java @@ -29,46 +29,53 @@ import java.util.ArrayList; /** * Spinner model progress dialog that disables all tools for user interaction after it shows up and - * and re-enables them after it dismisses. + * and re-enables them after it dismisses; this class along with all its methods should be accessed + * in only UI thread and allows only one instance at a time. */ public class SpinnerProgressDialog extends Dialog { - private final ViewGroup toolbar; + private static ViewGroup toolbar; + private static SpinnerProgressDialog dialog; private final ArrayList enabledTools = new ArrayList(); - public static SpinnerProgressDialog show(ViewGroup toolbar) { - SpinnerProgressDialog dialog = new SpinnerProgressDialog(toolbar); - dialog.setCancelable(false); - dialog.show(); - return dialog; + public static void initialize(ViewGroup toolbar) { + SpinnerProgressDialog.toolbar = toolbar; } - private SpinnerProgressDialog(ViewGroup toolbar) { - super(toolbar.getContext(), R.style.SpinnerProgressDialog); - - addContentView(new ProgressBar(toolbar.getContext()), new LayoutParams( - LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); - - // Disable enabled tools when showing spinner progress dialog. - for (int i = 0; i < toolbar.getChildCount(); i++) { - View view = toolbar.getChildAt(i); - if (view.isEnabled()) { - enabledTools.add(view); - view.setEnabled(false); + public static void showDialog() { + // There should be only one progress dialog running at a time. + if (dialog == null) { + dialog = new SpinnerProgressDialog(); + dialog.setCancelable(false); + dialog.show(); + // Disable enabled tools when showing spinner progress dialog. + for (int i = 0; i < toolbar.getChildCount(); i++) { + View view = toolbar.getChildAt(i); + if (view.isEnabled()) { + dialog.enabledTools.add(view); + view.setEnabled(false); + } } } - this.toolbar = toolbar; } - @Override - public void dismiss() { - super.dismiss(); - // Enable tools that were disabled by this spinner progress dialog. - for (View view : enabledTools) { - view.setEnabled(true); + public static void dismissDialog() { + if (dialog != null) { + dialog.dismiss(); + // Enable tools that were disabled by this spinner progress dialog. + for (View view : dialog.enabledTools) { + view.setEnabled(true); + } + dialog = null; } } + private SpinnerProgressDialog() { + super(toolbar.getContext(), R.style.SpinnerProgressDialog); + addContentView(new ProgressBar(toolbar.getContext()), new LayoutParams( + LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); + } + @Override public boolean onTouchEvent(MotionEvent event) { super.onTouchEvent(event); -- 2.11.0