Multiple accounts -> Show account name

Change-Id: I3ced41b403a6f216c316a9476cbb444e886a71ee
diff --git a/src/com/android/mail/providers/AllAccountObserver.java b/src/com/android/mail/providers/AllAccountObserver.java
new file mode 100644
index 0000000..50ce259
--- /dev/null
+++ b/src/com/android/mail/providers/AllAccountObserver.java
@@ -0,0 +1,99 @@
+/*
+ * Copyright (C) 2013 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.providers;
+
+import com.android.mail.ui.AccountController;
+import com.android.mail.utils.LogTag;
+import com.android.mail.utils.LogUtils;
+
+import android.database.DataSetObserver;
+
+/**
+ * A simple extension of {@link android.database.DataSetObserver} to provide all Accounts in
+ * {@link #onChanged(Account[])} when the list of Accounts changes. Initializing the object
+ * registers with the observer with the {@link com.android.mail.ui.AccountController} provided.
+ * The object will then begin to receive {@link #onChanged(Account[])} till {@link
+ * #unregisterAndDestroy()} is called. <p> To implement an {@link com.android.mail.providers
+ * .AllAccountObserver}, you need to implement the {@link #onChanged(Account[])} method.
+ */
+public abstract class AllAccountObserver extends DataSetObserver {
+    /**
+     * The AccountController that the observer is registered with.
+     */
+    private AccountController mController;
+
+    private static final String LOG_TAG = LogTag.getLogTag();
+
+    /**
+     * The no-argument constructor leaves the object unusable till
+     * {@link #initialize(com.android.mail.ui.AccountController)} is called.
+     */
+    public AllAccountObserver() {
+    }
+
+    /**
+     * Initializes an {@link com.android.mail.providers.AllAccountObserver} object that receives
+     * a call to {@link #onChanged(Account[])} when the controller changes the list of accounts.
+     *
+     * @param controller
+     */
+    public Account[] initialize(AccountController controller) {
+        if (controller == null) {
+            LogUtils.wtf(LOG_TAG, "AllAccountObserver initialized with null controller!");
+        }
+        mController = controller;
+        mController.registerAllAccountObserver(this);
+        return mController.getAllAccounts();
+    }
+
+    @Override
+    public final void onChanged() {
+        if (mController == null) {
+            return;
+        }
+        onChanged(mController.getAllAccounts());
+    }
+
+    /**
+     * Callback invoked when the list of Accounts changes.
+     * The updated list is passed as the argument.
+     * @param allAccounts the array of all accounts in the current application.
+     */
+    public abstract void onChanged(Account[] allAccounts);
+
+    /**
+     * Return the array of existing accounts.
+     * @return the array of existing accounts.
+     */
+    public final Account[] getAllAccounts() {
+        if (mController == null) {
+            return null;
+        }
+        return mController.getAllAccounts();
+    }
+
+    /**
+     * Unregisters for list of Account changes and makes the object unusable.
+     */
+    public void unregisterAndDestroy() {
+        if (mController == null) {
+            return;
+        }
+        mController.unregisterAllAccountObserver(this);
+    }
+}
diff --git a/src/com/android/mail/ui/FolderListFragment.java b/src/com/android/mail/ui/FolderListFragment.java
index d30b3f0..eab850d 100644
--- a/src/com/android/mail/ui/FolderListFragment.java
+++ b/src/com/android/mail/ui/FolderListFragment.java
@@ -120,19 +120,6 @@
     /** List of all accounts currently known */
     private Account[] mAllAccounts;
 
-    private final class AllAccountObserver extends DataSetObserver {
-        @Override
-        public void onChanged() {
-            if (mActivity == null) {
-                return;
-            }
-            final AccountController controller = mActivity.getAccountController();
-            if (controller == null) {
-                return;
-            }
-            mAllAccounts = controller.getAllAccounts();
-        }
-    }
     /**
      * Constructor needs to be public to handle orientation changes and activity lifecycle events.
      */
@@ -214,10 +201,13 @@
             // Current account and its observer.
             mCurrentAccount = mAccountObserver.initialize(accountController);
             // List of all accounts and its observer.
-            mAllAccountObserver = new AllAccountObserver();
-            accountController.registerAllAccountObserver(mAllAccountObserver);
-            mAllAccounts = accountController.getAllAccounts();
-
+            mAllAccountObserver = new AllAccountObserver(){
+                @Override
+                public void onChanged(Account[] allAccounts) {
+                    mAllAccounts = allAccounts;
+                }
+            };
+            mAllAccounts = mAllAccountObserver.initialize(accountController);
             mAccountChanger = accountController;
         }
 
@@ -320,12 +310,9 @@
             mAccountObserver.unregisterAndDestroy();
             mAccountObserver = null;
         }
-        final AccountController controller = mActivity.getAccountController();
-        if (controller != null) {
-            if (mAllAccountObserver != null) {
-                controller.unregisterAllAccountObserver(mAllAccountObserver);
-                mAllAccountObserver = null;
-            }
+        if (mAllAccountObserver != null) {
+            mAllAccountObserver.unregisterAndDestroy();
+            mAllAccountObserver = null;
         }
         super.onDestroyView();
     }
diff --git a/src/com/android/mail/ui/MailActionBarView.java b/src/com/android/mail/ui/MailActionBarView.java
index 1248017..090649b 100644
--- a/src/com/android/mail/ui/MailActionBarView.java
+++ b/src/com/android/mail/ui/MailActionBarView.java
@@ -22,6 +22,7 @@
 import com.android.mail.browse.SnippetTextView;
 import com.android.mail.providers.Account;
 import com.android.mail.providers.AccountObserver;
+import com.android.mail.providers.AllAccountObserver;
 import com.android.mail.providers.Conversation;
 import com.android.mail.providers.Folder;
 import com.android.mail.providers.FolderObserver;
@@ -40,7 +41,6 @@
 import android.content.Context;
 import android.content.res.Resources;
 import android.database.Cursor;
-import android.database.DataSetObserver;
 import android.os.Bundle;
 import android.os.Handler;
 import android.text.SpannableString;
@@ -97,6 +97,8 @@
     private View mRefreshActionView;
     private boolean mRefreshInProgress;
     private Conversation mCurrentConversation;
+    private AllAccountObserver mAllAccountObserver;
+    private boolean mHaveMultipleAccounts = false;
 
     public static final String LOG_TAG = LogTag.getLogTag();
 
@@ -269,19 +271,25 @@
             }
         };
         mFolderObserver.initialize(mController);
+        mAllAccountObserver = new AllAccountObserver() {
+            @Override
+            public void onChanged(Account[] allAccounts) {
+                mHaveMultipleAccounts = (allAccounts.length > 1);
+            }
+        };
+        mAllAccountObserver.initialize(mController);
         updateAccount(mAccountObserver.initialize(activity.getAccountController()));
     }
 
     private void updateAccount(Account account) {
         mAccount = account;
-        // Always show the name: TODO(viki) Only show if multiple accounts.
         if (mAccount != null) {
             final ContentResolver resolver = mActivity.getActivityContext().getContentResolver();
             final Bundle bundle = new Bundle(1);
             bundle.putParcelable(UIProvider.SetCurrentAccountColumns.ACCOUNT, account);
             resolver.call(mAccount.uri, UIProvider.AccountCallMethods.SET_CURRENT_ACCOUNT,
                     mAccount.uri.toString(), bundle);
-            if (mActionBar != null) {
+            if (mHaveMultipleAccounts && mActionBar != null) {
                 mActionBar.setSubtitle(account.name);
             }
         }
@@ -310,6 +318,10 @@
             mFolderObserver.unregisterAndDestroy();
             mFolderObserver = null;
         }
+        if (mAllAccountObserver != null) {
+            mAllAccountObserver.unregisterAndDestroy();
+            mAllAccountObserver = null;
+        }
         mAccountObserver.unregisterAndDestroy();
     }
 
@@ -440,7 +452,9 @@
     private void setFoldersMode() {
         setTitleModeFlags(ActionBar.DISPLAY_SHOW_TITLE);
         mActionBar.setTitle(R.string.folders);
-        mActionBar.setSubtitle(mAccount.name);
+        if (mHaveMultipleAccounts) {
+            mActionBar.setSubtitle(mAccount.name);
+        }
     }
 
     /**
diff --git a/src/com/android/mail/utils/Observable.java b/src/com/android/mail/utils/Observable.java
index fc2be5f..81e8d83 100644
--- a/src/com/android/mail/utils/Observable.java
+++ b/src/com/android/mail/utils/Observable.java
@@ -19,9 +19,13 @@
 import android.database.DataSetObservable;
 import android.database.DataSetObserver;
 
+/**
+ * A Utility class to register observers and return logging and counts for the number of registered
+ * observers.
+ */
 public class Observable extends DataSetObservable {
     protected static final String LOG_TAG = LogTag.getLogTag();
-    final String mName;
+    private final String mName;
 
     public Observable(String name) {
         mName = name;