Prevent change folder dialog from opening more than once.

Bug: 7247467
Change-Id: I0b5583830d7c8c2f63e01714b4a7fa2976d0eefb
diff --git a/src/com/android/mail/browse/SelectedConversationsActionMenu.java b/src/com/android/mail/browse/SelectedConversationsActionMenu.java
index 03e3275..56ecdd7 100644
--- a/src/com/android/mail/browse/SelectedConversationsActionMenu.java
+++ b/src/com/android/mail/browse/SelectedConversationsActionMenu.java
@@ -45,6 +45,7 @@
 import com.android.mail.ui.ConversationSetObserver;
 import com.android.mail.ui.ConversationUpdater;
 import com.android.mail.ui.DestructiveAction;
+import com.android.mail.ui.FolderSelectionDialog;
 import com.android.mail.ui.MultiFoldersSelectionDialog;
 import com.android.mail.ui.SingleFolderSelectionDialog;
 import com.android.mail.ui.SwipeableListView;
@@ -109,6 +110,7 @@
         mFolder = folder;
         mContext = mActivity.getActivityContext();
         mUpdater = activity.getConversationUpdater();
+        FolderSelectionDialog.setDialogDismissed();
     }
 
     @Override
@@ -190,14 +192,18 @@
                     }
                 }
                 if (!cantMove) {
+                    final FolderSelectionDialog dialog;
                     if (mAccount
                             .supportsCapability(UIProvider
                                     .AccountCapabilities.MULTIPLE_FOLDERS_PER_CONV)) {
-                        new MultiFoldersSelectionDialog(mContext, acct, mUpdater,
-                                mSelectionSet.values(), true, mFolder).show();
+                        dialog = MultiFoldersSelectionDialog.getInstance(
+                                mContext, acct, mUpdater, mSelectionSet.values(), true, mFolder);
                     } else {
-                        new SingleFolderSelectionDialog(mContext, acct, mUpdater,
-                                mSelectionSet.values(), true, mFolder).show();
+                        dialog = SingleFolderSelectionDialog.getInstance(
+                                mContext, acct, mUpdater, mSelectionSet.values(), true, mFolder);
+                    }
+                    if(dialog != null) {
+                        dialog.show();
                     }
                 }
                 break;
diff --git a/src/com/android/mail/ui/AbstractActivityController.java b/src/com/android/mail/ui/AbstractActivityController.java
index e13dc63..478ea72 100644
--- a/src/com/android/mail/ui/AbstractActivityController.java
+++ b/src/com/android/mail/ui/AbstractActivityController.java
@@ -754,6 +754,7 @@
         mPagerController = new ConversationPagerController(mActivity, this);
         mToastBar = (ActionableToastBar) mActivity.findViewById(R.id.toast_bar);
         attachActionBar();
+        FolderSelectionDialog.setDialogDismissed();
 
         final Intent intent = mActivity.getIntent();
         // Immediately handle a clean launch with intent, and any state restoration
@@ -910,13 +911,19 @@
                 Utils.showManageFolder(mActivity.getActivityContext(), mAccount);
                 break;
             case R.id.change_folder:
+                final FolderSelectionDialog dialog;
                 if (mAccount.supportsCapability(
                         UIProvider.AccountCapabilities.MULTIPLE_FOLDERS_PER_CONV)) {
-                    new MultiFoldersSelectionDialog(mActivity.getActivityContext(), mAccount, this,
-                            Conversation.listOf(mCurrentConversation), false, mFolder).show();
+                    dialog = MultiFoldersSelectionDialog.getInstance(mActivity.getActivityContext(),
+                            mAccount, this, Conversation.listOf(mCurrentConversation), false,
+                            mFolder);
                 } else {
-                    new SingleFolderSelectionDialog(mActivity.getActivityContext(), mAccount, this,
-                            Conversation.listOf(mCurrentConversation), false, mFolder).show();
+                    dialog = SingleFolderSelectionDialog.getInstance(mActivity.getActivityContext(),
+                            mAccount, this, Conversation.listOf(mCurrentConversation), false,
+                            mFolder);
+                }
+                if (dialog != null) {
+                    dialog.show();
                 }
                 break;
             default:
diff --git a/src/com/android/mail/ui/FolderSelectionDialog.java b/src/com/android/mail/ui/FolderSelectionDialog.java
new file mode 100644
index 0000000..ad83a3d
--- /dev/null
+++ b/src/com/android/mail/ui/FolderSelectionDialog.java
@@ -0,0 +1,45 @@
+/*
+ * Copyright (C) 2012 Google Inc.
+ * Licensed to The Android Open Source Project.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.mail.ui;
+
+import android.content.DialogInterface;
+import android.content.DialogInterface.OnDismissListener;
+
+import com.android.mail.utils.LogUtils;
+
+public abstract class FolderSelectionDialog implements OnDismissListener {
+    private static boolean sDialogShown;
+
+    protected static boolean isShown() {
+        return sDialogShown;
+    }
+
+    public static void setDialogDismissed() {
+        LogUtils.d("Gmail", "Folder Selection dialog dismissed");
+        sDialogShown = false;
+    }
+
+    public void show() {
+        FolderSelectionDialog.sDialogShown = true;
+    }
+
+    @Override
+    public void onDismiss(DialogInterface dialog) {
+        FolderSelectionDialog.setDialogDismissed();
+    }
+}
diff --git a/src/com/android/mail/ui/MultiFoldersSelectionDialog.java b/src/com/android/mail/ui/MultiFoldersSelectionDialog.java
index 9a98300..9b98562 100644
--- a/src/com/android/mail/ui/MultiFoldersSelectionDialog.java
+++ b/src/com/android/mail/ui/MultiFoldersSelectionDialog.java
@@ -42,10 +42,10 @@
 import java.util.HashSet;
 
 /**
- * Displays a folder selection dialog for the conversation provided. It allows the user to mark
- * folders to assign that conversation to.
+ * Displays a folder selection dialog for the conversation provided. It allows
+ * the user to mark folders to assign that conversation to.
  */
-public class MultiFoldersSelectionDialog implements OnClickListener {
+public class MultiFoldersSelectionDialog extends FolderSelectionDialog implements OnClickListener {
     private AlertDialog mDialog;
     private final ConversationUpdater mUpdater;
     private final boolean mSingle;
@@ -55,6 +55,16 @@
     private final HashMap<Uri, FolderOperation> mOperations;
     private final QueryRunner mRunner;
 
+    public static MultiFoldersSelectionDialog getInstance(final Context context, Account account,
+            final ConversationUpdater updater, Collection<Conversation> target, boolean isBatch,
+            Folder currentFolder) {
+        if (isShown()) {
+            return null;
+        }
+        return new MultiFoldersSelectionDialog(
+                context, account, updater, target, isBatch, currentFolder);
+    }
+
     /**
      * Create a new {@link MultiFoldersSelectionDialog}. It is displayed when
      * the {@link #show()} method is called.
@@ -67,7 +77,7 @@
      * @param currentFolder the current folder that the
      *            {@link FolderListFragment} is showing
      */
-    public MultiFoldersSelectionDialog(final Context context, Account account,
+    private MultiFoldersSelectionDialog(final Context context, Account account,
             final ConversationUpdater updater, Collection<Conversation> target, boolean isBatch,
             Folder currentFolder) {
         mUpdater = updater;
@@ -156,7 +166,9 @@
     /**
      * Shows the {@link MultiFoldersSelectionDialog} dialog.
      */
+    @Override
     public void show() {
+        super.show();
         mRunner.execute();
     }
 
@@ -174,6 +186,7 @@
                 }
             }
         });
+        mDialog.setOnDismissListener(this);
     }
 
     /**
diff --git a/src/com/android/mail/ui/SingleFolderSelectionDialog.java b/src/com/android/mail/ui/SingleFolderSelectionDialog.java
index e9a5c41..28bd52d 100644
--- a/src/com/android/mail/ui/SingleFolderSelectionDialog.java
+++ b/src/com/android/mail/ui/SingleFolderSelectionDialog.java
@@ -42,7 +42,7 @@
  * Displays a folder selection dialog for the conversation provided. It allows
  * the user to switch a conversation from one folder to another.
  */
-public class SingleFolderSelectionDialog implements OnClickListener {
+public class SingleFolderSelectionDialog extends FolderSelectionDialog implements OnClickListener {
     private AlertDialog mDialog;
     private ConversationUpdater mUpdater;
     private SeparatedFolderListAdapter mAdapter;
@@ -53,6 +53,16 @@
     private Builder mBuilder;
     private Account mAccount;
 
+    public static SingleFolderSelectionDialog getInstance(final Context context, Account account,
+            final ConversationUpdater updater, Collection<Conversation> target, boolean isBatch,
+            Folder currentFolder) {
+        if (isShown()) {
+            return null;
+        }
+        return new SingleFolderSelectionDialog(
+                context, account, updater, target, isBatch, currentFolder);
+    }
+
     /**
      * Create a new {@link SingleFolderSelectionDialog}. It is displayed when
      * the {@link #show()} method is called.
@@ -65,7 +75,7 @@
      * @param currentFolder the current folder that the
      *            {@link FolderListFragment} is showing
      */
-    public SingleFolderSelectionDialog(final Context context, Account account,
+    private SingleFolderSelectionDialog(final Context context, Account account,
             final ConversationUpdater updater, Collection<Conversation> target, boolean isBatch,
             Folder currentFolder) {
         mUpdater = updater;
@@ -132,7 +142,9 @@
     /**
      * Shows the {@link SingleFolderSelectionDialog} dialog.
      */
+    @Override
     public void show() {
+        super.show();
         mRunner.execute();
     }
 
@@ -156,6 +168,7 @@
                 }
             }
         });
+        mDialog.setOnDismissListener(this);
     }
 
     @Override