Merge "Fix b/5517002 by dismissing running progress dialog in onPause()." into ics-mr1
diff --git a/src/com/android/gallery3d/photoeditor/EffectsBar.java b/src/com/android/gallery3d/photoeditor/EffectsBar.java
index b4857e6..4075404 100644
--- a/src/com/android/gallery3d/photoeditor/EffectsBar.java
+++ b/src/com/android/gallery3d/photoeditor/EffectsBar.java
@@ -128,13 +128,12 @@
 
     private boolean exitActiveEffect(final Runnable runnableOnDone) {
         if (activeEffect != null) {
-            final SpinnerProgressDialog progressDialog = SpinnerProgressDialog.show(
-                    (ViewGroup) getRootView().findViewById(R.id.toolbar));
+            SpinnerProgressDialog.showDialog();
             activeEffect.end(new Runnable() {
 
                 @Override
                 public void run() {
-                    progressDialog.dismiss();
+                    SpinnerProgressDialog.dismissDialog();
                     View fullscreenTool = getRootView().findViewById(R.id.fullscreen_effect_tool);
                     if (fullscreenTool != null) {
                         ((ViewGroup) fullscreenTool.getParent()).removeView(fullscreenTool);
diff --git a/src/com/android/gallery3d/photoeditor/PhotoEditor.java b/src/com/android/gallery3d/photoeditor/PhotoEditor.java
index dba7e62..8f3990b 100644
--- a/src/com/android/gallery3d/photoeditor/PhotoEditor.java
+++ b/src/com/android/gallery3d/photoeditor/PhotoEditor.java
@@ -42,6 +42,7 @@
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.photoeditor_main);
+        SpinnerProgressDialog.initialize((ViewGroup) findViewById(R.id.toolbar));
 
         Intent intent = getIntent();
         if (Intent.ACTION_EDIT.equalsIgnoreCase(intent.getAction())) {
@@ -74,12 +75,8 @@
         actionBar.setClickRunnable(R.id.action_bar_back, createBackRunnable());
     }
 
-    private SpinnerProgressDialog createProgressDialog() {
-        return SpinnerProgressDialog.show((ViewGroup) findViewById(R.id.toolbar));
-    }
-
     private void openPhoto() {
-        final SpinnerProgressDialog progressDialog = createProgressDialog();
+        SpinnerProgressDialog.showDialog();
         LoadScreennailTask.Callback callback = new LoadScreennailTask.Callback() {
 
             @Override
@@ -88,7 +85,7 @@
 
                     @Override
                     public void onDone() {
-                        progressDialog.dismiss();
+                        SpinnerProgressDialog.dismissDialog();
                         effectsBar.setEnabled(result != null);
                     }
                 });
@@ -106,12 +103,12 @@
 
                     @Override
                     public void run() {
-                        final SpinnerProgressDialog progressDialog = createProgressDialog();
+                        SpinnerProgressDialog.showDialog();
                         OnDoneCallback callback = new OnDoneCallback() {
 
                             @Override
                             public void onDone() {
-                                progressDialog.dismiss();
+                                SpinnerProgressDialog.dismissDialog();
                             }
                         };
                         if (undo) {
@@ -134,7 +131,7 @@
 
                     @Override
                     public void run() {
-                        final SpinnerProgressDialog progressDialog = createProgressDialog();
+                        SpinnerProgressDialog.showDialog();
                         filterStack.getOutputBitmap(new OnDoneBitmapCallback() {
 
                             @Override
@@ -143,7 +140,7 @@
 
                                     @Override
                                     public void onComplete(Uri result) {
-                                        progressDialog.dismiss();
+                                        SpinnerProgressDialog.dismissDialog();
                                         saveUri = result;
                                         actionBar.updateSave(saveUri == null);
                                     }
@@ -223,9 +220,10 @@
 
     @Override
     protected void onPause() {
-        // TODO: Close running progress dialogs as all pending operations will be paused.
         super.onPause();
         filterStack.onPause();
+        // Dismiss any running progress dialog as all operations are paused.
+        SpinnerProgressDialog.dismissDialog();
     }
 
     @Override
diff --git a/src/com/android/gallery3d/photoeditor/SpinnerProgressDialog.java b/src/com/android/gallery3d/photoeditor/SpinnerProgressDialog.java
index 207c2d1..065075e 100644
--- a/src/com/android/gallery3d/photoeditor/SpinnerProgressDialog.java
+++ b/src/com/android/gallery3d/photoeditor/SpinnerProgressDialog.java
@@ -29,46 +29,53 @@
 
 /**
  * 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);