Merge "Let ComposeActivity use settings."
diff --git a/src/com/android/mail/providers/Account.java b/src/com/android/mail/providers/Account.java
index 50170bb..4eb59ed 100644
--- a/src/com/android/mail/providers/Account.java
+++ b/src/com/android/mail/providers/Account.java
@@ -296,12 +296,6 @@
         return null;
     }
 
-    public Folder getAccountInbox() {
-        // TODO: (mindyp) fill in with call to settings or reading of account settings
-        // to get the default inbox for this account.
-        return Folder.getInbox();
-    }
-
     @SuppressWarnings("hiding")
     public static final Creator<Account> CREATOR = new Creator<Account>() {
         @Override
diff --git a/src/com/android/mail/providers/AccountCacheProvider.java b/src/com/android/mail/providers/AccountCacheProvider.java
index 84754d5..986bd42 100644
--- a/src/com/android/mail/providers/AccountCacheProvider.java
+++ b/src/com/android/mail/providers/AccountCacheProvider.java
@@ -29,8 +29,10 @@
 
 import com.google.common.base.Objects;
 import com.google.common.collect.Maps;
+import com.google.common.collect.Sets;
 
 import java.util.Map;
+import java.util.Set;
 
 
 /**
@@ -48,6 +50,9 @@
 
     private final static Map<Uri, CachedAccount> ACCOUNT_CACHE = Maps.newHashMap();
 
+    // Map from content provider query uri to the set of account uri that resulted from that query
+    private final static Map<Uri, Set<Uri>> QUERY_URI_ACCOUNT_URIS_MAP = Maps.newHashMap();
+
     private ContentResolver mResolver;
     private static String sAuthority;
     private static AccountCacheProvider sInstance;
@@ -168,7 +173,8 @@
      * @param resolver
      * @param accountsQueryUri
      */
-    public static void addAccountsForUri(ContentResolver resolver, Uri accountsQueryUri) {
+    public static synchronized void addAccountsForUri(
+            ContentResolver resolver, Uri accountsQueryUri) {
         final Cursor c = resolver.query(accountsQueryUri, UIProvider.ACCOUNTS_PROJECTION,
                 null, null, null);
         if (c == null) {
@@ -177,17 +183,38 @@
         }
 
         // TODO(pwestbro):
-        // 1) Keep a map of queryUri -> Accounts to make it easy to remove deleted accounts
-        // 2) Keep a cache of Cursors which would allow changes to be observered.
+        // 1) Keep a cache of Cursors which would allow changes to be observered.
+        final Set<Uri> previousQueryUriMap = QUERY_URI_ACCOUNT_URIS_MAP.get(accountsQueryUri);
 
+        final Set<Uri> newQueryUriMap = Sets.newHashSet();
         try {
             while (c.moveToNext()) {
                 final Account account = new Account(c);
+                final Uri accountUri = account.uri;
+                newQueryUriMap.add(accountUri);
                 addAccount(new CachedAccount(account));
             }
         } finally {
             c.close();
         }
+
+        // Save the new set, or remove the previous entry if it is empty
+        if (newQueryUriMap.size() > 0) {
+            QUERY_URI_ACCOUNT_URIS_MAP.put(accountsQueryUri, newQueryUriMap);
+        } else {
+            QUERY_URI_ACCOUNT_URIS_MAP.remove(accountsQueryUri);
+        }
+
+        if (previousQueryUriMap != null) {
+            // Remove all of the accounts that are in the new result set
+            previousQueryUriMap.removeAll(newQueryUriMap);
+
+            // For all of the entries that had been in the previous result set, and are not
+            // in the new result set, remove them from the cache
+            if (previousQueryUriMap.size() > 0) {
+                removeAccounts(previousQueryUriMap);
+            }
+        }
     }
 
     public static void addAccount(CachedAccount account) {
@@ -201,12 +228,20 @@
         broadcastAccountChange();
     }
 
-    public static void removeAccount(String accountUri) {
+    public static void removeAccount(Uri accountUri) {
         synchronized (ACCOUNT_CACHE) {
-            final CachedAccount account = ACCOUNT_CACHE.get(accountUri);
+            ACCOUNT_CACHE.remove(accountUri);
+        }
 
-            if (account != null) {
-                ACCOUNT_CACHE.remove(account);
+        // Explicitly calling this out of the synchronized block in case any of the observers get
+        // called synchronously.
+        broadcastAccountChange();
+    }
+
+    private static void removeAccounts(Set<Uri> uris) {
+        synchronized (ACCOUNT_CACHE) {
+            for (Uri accountUri : uris) {
+                ACCOUNT_CACHE.remove(accountUri);
             }
         }
 
diff --git a/src/com/android/mail/providers/Folder.java b/src/com/android/mail/providers/Folder.java
index 8a1674c..4a86187 100644
--- a/src/com/android/mail/providers/Folder.java
+++ b/src/com/android/mail/providers/Folder.java
@@ -323,13 +323,4 @@
     public boolean supportsCapability(int capability) {
         return (capabilities & capability) != 0;
     }
-
-    /**
-     * Get a fake inbox folder.
-     */
-    public static Folder getInbox() {
-        Folder inbox = new Folder();
-        inbox.name = "inbox";
-        return inbox;
-    }
 }
diff --git a/src/com/android/mail/ui/AbstractActivityController.java b/src/com/android/mail/ui/AbstractActivityController.java
index 66f6be9..67f066f 100644
--- a/src/com/android/mail/ui/AbstractActivityController.java
+++ b/src/com/android/mail/ui/AbstractActivityController.java
@@ -124,7 +124,7 @@
         }
     };
     private final Set<Uri> mCurrentAccountUris = Sets.newHashSet();
-    private Settings mCachedSettings;
+    protected Settings mCachedSettings;
 
     protected static final String LOG_TAG = new LogUtils().getLogTag();
     private static final int ACCOUNT_CURSOR_LOADER = 0;
@@ -272,6 +272,7 @@
 
     public void onSettingsChanged(Settings settings) {
         mCachedSettings = settings;
+        resetActionBarIcon();
     }
 
     private void fetchAccountFolderInfo() {
diff --git a/src/com/android/mail/ui/FolderSelectionActivity.java b/src/com/android/mail/ui/FolderSelectionActivity.java
index d57d8cb..36f7d7d 100644
--- a/src/com/android/mail/ui/FolderSelectionActivity.java
+++ b/src/com/android/mail/ui/FolderSelectionActivity.java
@@ -165,8 +165,7 @@
                  * account, calculate the human readable name of the label and
                  * use it as the shortcut name, etc...
                  */
-                final Intent clickIntent = Utils.createViewConversationIntent(this, mAccount,
-                        mSelectedFolder, UIProvider.INVALID_CONVERSATION_ID);
+                final Intent clickIntent = Utils.createViewFolderIntent(mSelectedFolder, mAccount);
                 resultIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, clickIntent);
                 resultIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
                         Intent.ShortcutIconResource.fromContext(this,
diff --git a/src/com/android/mail/ui/OnePaneController.java b/src/com/android/mail/ui/OnePaneController.java
index 0249f04..f2bcd3a 100644
--- a/src/com/android/mail/ui/OnePaneController.java
+++ b/src/com/android/mail/ui/OnePaneController.java
@@ -20,6 +20,7 @@
 import android.app.Fragment;
 import android.app.FragmentManager;
 import android.app.FragmentTransaction;
+import android.net.Uri;
 import android.os.Bundle;
 
 import com.android.mail.ConversationListContext;
@@ -76,7 +77,10 @@
     @Override
     public void resetActionBarIcon() {
         final int mode = mViewMode.getMode();
-        if (!inInbox() || (mode == ViewMode.CONVERSATION_LIST && mConvListContext.isSearchResult())
+        // If the settings aren't loaded yet, we may not know what the default
+        // inbox is, so err toward this being the account inbox.
+        if ((mCachedSettings != null && mConvListContext != null && !inInbox())
+                || (mode == ViewMode.CONVERSATION_LIST && mConvListContext.isSearchResult())
                 || mode == ViewMode.CONVERSATION || mode == ViewMode.FOLDER_LIST) {
             mActionBarView.setBackButton();
         } else {
@@ -85,9 +89,9 @@
     }
 
     private boolean inInbox() {
-        return mConvListContext != null && mConvListContext.folder != null ?
-                (!mConvListContext.isSearchResult() && mConvListContext.folder.name
-                .toLowerCase().equals(mAccount.getAccountInbox().name.toLowerCase())) : false;
+        Uri inboxUri = mCachedSettings != null ? mCachedSettings.defaultInbox : null;
+        return mConvListContext != null && mConvListContext.folder != null ? (!mConvListContext
+                .isSearchResult() && mConvListContext.folder.uri.equals(inboxUri)) : false;
     }
 
     @Override
diff --git a/src/com/android/mail/utils/Utils.java b/src/com/android/mail/utils/Utils.java
index b674b4a..d311242 100644
--- a/src/com/android/mail/utils/Utils.java
+++ b/src/com/android/mail/utils/Utils.java
@@ -596,30 +596,6 @@
     }
 
     /**
-     * @return an intent which, if launched, will display the corresponding conversation
-     */
-    public static Intent createViewConversationIntent(Context context,
-            Account account, Folder folder, long conversationId) {
-        final Intent intent = new Intent(Intent.ACTION_VIEW);
-        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
-        Uri.Builder builder = new Uri.Builder();
-        builder.scheme("content");
-        builder.authority(UIProvider.AUTHORITY);
-        builder.appendEncodedPath("account/" + account);
-        final String intentLabel = folder.name != null ? folder.name
-                : account.getAccountInbox().name;
-        builder.appendPath("label");
-        builder.appendPath(intentLabel);
-
-        if (conversationId != UIProvider.INVALID_CONVERSATION_ID) {
-            builder.appendEncodedPath("conversationId/" + conversationId);
-        }
-        intent.setDataAndType(builder.build(), "application/" + UIProvider.AUTHORITY);
-
-        return intent;
-    }
-
-    /**
      * Helper method to show context-aware Gmail help.
      *
      * @param context Context to be used to open the help.