OSDN Git Service

Fix b/5517002 by dismissing running progress dialog in onPause().
authorYuli Huang <yuli@google.com>
Mon, 14 Nov 2011 14:38:16 +0000 (22:38 +0800)
committerYuli Huang <yuli@google.com>
Tue, 15 Nov 2011 04:02:25 +0000 (12:02 +0800)
Change-Id: I524f876e53776c38bc850120a2d7b7e6381ca33a

src/com/android/gallery3d/photoeditor/SpinnerProgressDialog.java

index 207c2d1..065075e 100644 (file)
@@ -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<View> enabledTools = new ArrayList<View>();
 
-    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);