Add account/folder status columns

Change-Id: I0dd68839e8065d3897ed5f8d017e4ea7d41819d8
diff --git a/src/com/android/mail/providers/Account.java b/src/com/android/mail/providers/Account.java
index ad45336..71c95e4 100644
--- a/src/com/android/mail/providers/Account.java
+++ b/src/com/android/mail/providers/Account.java
@@ -83,17 +83,16 @@
     public final String undoUri;
 
     /**
-     * The content provider uri that can be used to get a status cursor for this
-     * account.
-     */
-    public final String statusUri;
-
-    /**
      * Uri for VIEW intent that will cause the settings screens for this account type to be
      * shown.
      */
     public final String settingIntentUri;
 
+    /**
+     * The sync status of the account
+     */
+    public final int syncStatus;
+
     public Account(Parcel in) {
         super(in);
         providerVersion = in.readInt();
@@ -106,8 +105,8 @@
         sendMessageUri = in.readString();
         expungeMessageUri = in.readString();
         undoUri = in.readString();
-        statusUri = in.readString();
         settingIntentUri = in.readString();
+        syncStatus = in.readInt();
     }
 
     // TODO(pwestbro): remove this constructor.
@@ -126,8 +125,8 @@
         sendMessageUri = null;
         expungeMessageUri = null;
         undoUri = null;
-        statusUri = null;
         settingIntentUri = null;
+        syncStatus = 0;
     }
 
     public Account(Cursor cursor) {
@@ -142,8 +141,8 @@
         sendMessageUri = cursor.getString(UIProvider.ACCOUNT_SEND_MESSAGE_URI_COLUMN);
         expungeMessageUri = cursor.getString(UIProvider.ACCOUNT_EXPUNGE_MESSAGE_URI_COLUMN);
         undoUri = cursor.getString(UIProvider.ACCOUNT_UNDO_URI_COLUMN);
-        statusUri = cursor.getString(UIProvider.ACCOUNT_STATUS_URI_COLUMN);
-        settingIntentUri = cursor.getString(UIProvider.SETTINGS_INTENT_URI_COLUMN);
+        settingIntentUri = cursor.getString(UIProvider.ACCOUNT_SETTINGS_INTENT_URI_COLUMN);
+        syncStatus = cursor.getInt(UIProvider.ACCOUNT_SYNC_STATUS_COLUMN);
     }
 
     public boolean supportsCapability(int capability) {
@@ -163,8 +162,8 @@
         dest.writeString(sendMessageUri);
         dest.writeString(expungeMessageUri);
         dest.writeString(undoUri);
-        dest.writeString(statusUri);
         dest.writeString(settingIntentUri);
+        dest.writeInt(syncStatus);
     }
 
     @SuppressWarnings("hiding")
diff --git a/src/com/android/mail/providers/AccountCacheProvider.java b/src/com/android/mail/providers/AccountCacheProvider.java
index 6181988..84d8d38 100644
--- a/src/com/android/mail/providers/AccountCacheProvider.java
+++ b/src/com/android/mail/providers/AccountCacheProvider.java
@@ -25,11 +25,10 @@
 import android.provider.BaseColumns;
 import android.text.TextUtils;
 
-import com.google.common.collect.Maps;
+import com.android.mail.providers.AccountCacheProvider.CachedAccount;
 import com.google.common.base.Objects;
+import com.google.common.collect.Maps;
 
-import java.lang.Integer;
-import java.lang.String;
 import java.util.Map;
 
 
@@ -114,11 +113,11 @@
                     builder.add(account.mExpungeMessageUri);
                 } else if (TextUtils.equals(column, UIProvider.AccountColumns.UNDO_URI)) {
                     builder.add(account.mUndoUri);
-                } else if (TextUtils.equals(column, UIProvider.AccountColumns.STATUS_URI)) {
-                    builder.add(account.mStatusUri);
                 } else if (TextUtils.equals(column,
                         UIProvider.AccountColumns.SETTINGS_INTENT_URI)) {
                     builder.add(account.mSettingsIntentUri);
+                } else if (TextUtils.equals(column, UIProvider.AccountColumns.SYNC_STATUS)) {
+                    builder.add(Integer.valueOf((int)account.mSyncStatus));
                 } else {
                     throw new IllegalStateException("Column not found: " + column);
                 }
@@ -198,13 +197,13 @@
         private final String mSendMailUri;
         private final String mExpungeMessageUri;
         private final String mUndoUri;
-        private final String mStatusUri;
         private final String mSettingsIntentUri;
+        private final int mSyncStatus;
 
         public CachedAccount(long id, String name, String uri, long capabilities,
                 String folderListUri, String searchUri, String fromAddressesUri,
                 String saveDraftUri, String sendMailUri, String expungeMessageUri,
-                String undoUri, String statusUri, String settingsIntentUri) {
+                String undoUri, String settingsIntentUri, int syncStatus) {
             mId = id;
             mName = name;
             mUri = uri;
@@ -216,8 +215,8 @@
             mSendMailUri = sendMailUri;
             mExpungeMessageUri = expungeMessageUri;
             mUndoUri = undoUri;
-            mStatusUri = statusUri;
             mSettingsIntentUri = settingsIntentUri;
+            mSyncStatus = syncStatus;
         }
 
         @Override
@@ -240,15 +239,15 @@
                     TextUtils.equals(mSendMailUri, other.mSendMailUri) &&
                     TextUtils.equals(mExpungeMessageUri, other.mExpungeMessageUri) &&
                     TextUtils.equals(mUndoUri, other.mUndoUri) &&
-                    TextUtils.equals(mStatusUri, other.mStatusUri) &&
-                    TextUtils.equals(mSettingsIntentUri, other.mSettingsIntentUri);
+                    TextUtils.equals(mSettingsIntentUri, other.mSettingsIntentUri) &&
+                    (mSyncStatus == other.mSyncStatus);
         }
 
         @Override
         public int hashCode() {
             return Objects.hashCode(mId, mName, mUri, mCapabilities, mFolderListUri, mSearchUri,
                     mAccountFromAddressesUri, mSaveDraftUri, mSendMailUri, mExpungeMessageUri,
-                    mUndoUri, mStatusUri, mSettingsIntentUri);
+                    mUndoUri, mSettingsIntentUri, mSyncStatus);
         }
     }
 }
\ No newline at end of file
diff --git a/src/com/android/mail/providers/Folder.java b/src/com/android/mail/providers/Folder.java
index 3ca859a..ab6f20b 100644
--- a/src/com/android/mail/providers/Folder.java
+++ b/src/com/android/mail/providers/Folder.java
@@ -17,14 +17,14 @@
 
 package com.android.mail.providers;
 
-import com.google.common.collect.Maps;
-
 import android.database.Cursor;
 import android.os.Parcel;
 import android.os.Parcelable;
+import android.os.Parcelable.Creator;
 import android.text.TextUtils;
 
 import com.android.mail.utils.LogUtils;
+import com.google.common.collect.Maps;
 
 import java.util.Collection;
 import java.util.Map;
@@ -104,15 +104,20 @@
     public String refreshUri;
 
     /**
-     * The content provider URI to get a status cursor for this folder.
+     * The current sync status of the folder
      */
-    public String statusUri;
+    public int syncStatus;
+
+    /**
+     * The result of the last sync for this folder
+     */
+    public int lastSyncResult;
 
     /**
      * Total number of members that comprise an instance of a folder. Count up the members above.
      * This is the number of members that need to be serialized or parceled.
      */
-    private static final int NUMBER_MEMBERS = 12;
+    private static final int NUMBER_MEMBERS = 14;
 
     /**
      * Used only for debugging.
@@ -151,8 +156,9 @@
         unreadCount = in.readInt();
         totalCount = in.readInt();
         refreshUri = in.readString();
-        statusUri = in.readString();
-    }
+        syncStatus = in.readInt();
+        lastSyncResult = in.readInt();
+     }
 
     public Folder(Cursor cursor) {
         id = cursor.getString(UIProvider.FOLDER_ID_COLUMN);
@@ -168,7 +174,8 @@
         unreadCount = cursor.getInt(UIProvider.FOLDER_UNREAD_COUNT_COLUMN);
         totalCount = cursor.getInt(UIProvider.FOLDER_TOTAL_COUNT_COLUMN);
         refreshUri = cursor.getString(UIProvider.FOLDER_REFRESH_URI_COLUMN);
-        statusUri = cursor.getString(UIProvider.FOLDER_STATUS_URI_COLUMN);
+        syncStatus = cursor.getInt(UIProvider.FOLDER_SYNC_STATUS_COLUMN);
+        lastSyncResult = cursor.getInt(UIProvider.FOLDER_LAST_SYNC_RESULT_COLUMN);
     }
 
     @Override
@@ -186,7 +193,8 @@
         dest.writeInt(unreadCount);
         dest.writeInt(totalCount);
         dest.writeString(refreshUri);
-        dest.writeString(statusUri);
+        dest.writeInt(syncStatus);
+        dest.writeInt(lastSyncResult);
     }
 
     /**
@@ -206,7 +214,8 @@
         out.append(unreadCount).append(LABEL_COMPONENT_SEPARATOR);
         out.append(totalCount).append(LABEL_COMPONENT_SEPARATOR);
         out.append(refreshUri).append(LABEL_COMPONENT_SEPARATOR);
-        out.append(statusUri).append(LABEL_COMPONENT_SEPARATOR);
+        out.append(syncStatus).append(LABEL_COMPONENT_SEPARATOR);
+        out.append(lastSyncResult).append(LABEL_COMPONENT_SEPARATOR);
         return out.toString();
     }
 
@@ -233,7 +242,8 @@
         unreadCount = Integer.valueOf(folderMembers[8]);
         totalCount = Integer.valueOf(folderMembers[9]);
         refreshUri = folderMembers[10];
-        refreshUri = folderMembers[11];
+        syncStatus = Integer.valueOf(folderMembers[11]);
+        lastSyncResult = Integer.valueOf(folderMembers[12]);
     }
 
     /**
diff --git a/src/com/android/mail/providers/UIProvider.java b/src/com/android/mail/providers/UIProvider.java
index 52c3382..e97528f 100644
--- a/src/com/android/mail/providers/UIProvider.java
+++ b/src/com/android/mail/providers/UIProvider.java
@@ -23,7 +23,6 @@
 
 import com.android.common.contacts.DataUsageStatUpdater;
 
-import java.lang.String;
 import java.util.ArrayList;
 
 
@@ -32,6 +31,41 @@
     public static final long INVALID_CONVERSATION_ID = -1;
     public static final long INVALID_MESSAGE_ID = -1;
 
+    /**
+     * Values for the current state of a Folder/Account; note that it's possible that more than one
+     * sync is in progress
+     */
+    public static final class SyncStatus {
+        // No sync in progress
+        public static final int NO_SYNC = 1<<0;
+        // A user-requested sync/refresh is in progress
+        public static final int USER_REFRESH = 1<<1;
+        // A user-requested query is in progress
+        public static final int USER_QUERY = 1<<2;
+        // A user request for additional results is in progress
+        public static final int USER_MORE_RESULTS = 1<<3;
+        // A background sync is in progress
+        public static final int BACKGROUND_SYNC = 1<<4;
+    }
+
+    /**
+     * Values for the result of the last attempted sync of a Folder/Account
+     */
+    public static final class LastSyncResult {
+        // The sync completed successfully
+        public static final int SUCCESS = 0;
+        // The sync wasn't completed due to a connection error
+        public static final int CONNECTION_ERROR = 1;
+        // The sync wasn't completed due to an authentication error
+        public static final int AUTH_ERROR = 2;
+        // The sync wasn't completed due to a security error
+        public static final int SECURITY_ERROR = 3;
+        // The sync wasn't completed due to a low memory condition
+        public static final int STORAGE_ERROR = 4;
+        // The sync wasn't completed due to an internal error/exception
+        public static final int INTERNAL_ERROR = 5;
+    }
+
     // The actual content provider should define its own authority
     public static final String AUTHORITY = "com.android.mail.providers";
 
@@ -53,8 +87,8 @@
             AccountColumns.SEND_MAIL_URI,
             AccountColumns.EXPUNGE_MESSAGE_URI,
             AccountColumns.UNDO_URI,
-            AccountColumns.STATUS_URI,
-            AccountColumns.SETTINGS_INTENT_URI
+            AccountColumns.SETTINGS_INTENT_URI,
+            AccountColumns.SYNC_STATUS
     };
 
     public static final int ACCOUNT_ID_COLUMN = 0;
@@ -69,8 +103,8 @@
     public static final int ACCOUNT_SEND_MESSAGE_URI_COLUMN = 9;
     public static final int ACCOUNT_EXPUNGE_MESSAGE_URI_COLUMN = 10;
     public static final int ACCOUNT_UNDO_URI_COLUMN = 11;
-    public static final int ACCOUNT_STATUS_URI_COLUMN = 12;
-    public static final int SETTINGS_INTENT_URI_COLUMN = 13;
+    public static final int ACCOUNT_SETTINGS_INTENT_URI_COLUMN = 12;
+    public static final int ACCOUNT_SYNC_STATUS_COLUMN = 13;
 
     public static final class AccountCapabilities {
         /**
@@ -211,11 +245,6 @@
          * to undo the last committed action.
          */
         public static final String UNDO_URI = "undoUri";
-        /**
-         * This string column contains the content provider uri for getting a
-         * cursor that reflects the sync status of this account.
-         */
-        public static final String STATUS_URI = "statusUri";
 
         /**
          * Uri for VIEW intent that will cause the settings screens for this account type to be
@@ -224,6 +253,12 @@
          * to be moved to a global content provider.
          */
         public static String SETTINGS_INTENT_URI = "accountSettingsIntentUri";
+
+        /**
+         * This int column contains the current sync status of the account (the logical AND of the
+         * sync status of folders in this account)
+         */
+        public static final String SYNC_STATUS = "syncStatus";
     }
 
     // We define a "folder" as anything that contains a list of conversations.
@@ -245,7 +280,8 @@
         FolderColumns.UNREAD_COUNT,
         FolderColumns.TOTAL_COUNT,
         FolderColumns.REFRESH_URI,
-        FolderColumns.STATUS_URI
+        FolderColumns.SYNC_STATUS,
+        FolderColumns.LAST_SYNC_RESULT
     };
 
     public static final int FOLDER_ID_COLUMN = 0;
@@ -260,7 +296,8 @@
     public static final int FOLDER_UNREAD_COUNT_COLUMN = 9;
     public static final int FOLDER_TOTAL_COUNT_COLUMN = 10;
     public static final int FOLDER_REFRESH_URI_COLUMN = 11;
-    public static final int FOLDER_STATUS_URI_COLUMN = 12;
+    public static final int FOLDER_SYNC_STATUS_COLUMN = 12;
+    public static final int FOLDER_LAST_SYNC_RESULT_COLUMN = 13;
 
     public static final class FolderCapabilities {
         public static final int SYNCABLE = 0x0001;
@@ -313,10 +350,15 @@
          */
         public static final  String REFRESH_URI = "refreshUri";
         /**
-         * This string column contains the content provider uri for getting a
-         * cursor that reflects the sync status of this folder.
+         * This int column contains current sync status of the folder; some combination of the
+         * SyncStatus bits defined above
          */
-        public static final String STATUS_URI  = "statusUri";
+        public static final String SYNC_STATUS  = "syncStatus";
+        /**
+         * This int column contains the sync status of the last sync attempt; one of the
+         * LastSyncStatus values defined above
+         */
+        public static final String LAST_SYNC_RESULT  = "lastSyncResult";
 
         public FolderColumns() {}
     }
diff --git a/src/com/android/mail/providers/protos/mock/MockUiProvider.java b/src/com/android/mail/providers/protos/mock/MockUiProvider.java
index 6ceceac..3166b39 100644
--- a/src/com/android/mail/providers/protos/mock/MockUiProvider.java
+++ b/src/com/android/mail/providers/protos/mock/MockUiProvider.java
@@ -16,15 +16,6 @@
 
 package com.android.mail.providers.protos.mock;
 
-import com.android.mail.providers.AccountCacheProvider;
-import com.android.mail.providers.UIProvider.AccountCapabilities;
-import com.android.mail.providers.UIProvider.AccountColumns;
-import com.android.mail.providers.UIProvider.AttachmentColumns;
-import com.android.mail.providers.UIProvider.ConversationColumns;
-import com.android.mail.providers.UIProvider.FolderCapabilities;
-import com.android.mail.providers.UIProvider.FolderColumns;
-import com.android.mail.providers.UIProvider.MessageColumns;
-
 import android.content.ContentProvider;
 import android.content.ContentValues;
 import android.database.Cursor;
@@ -33,16 +24,20 @@
 import android.provider.BaseColumns;
 import android.text.Html;
 
+import com.android.mail.providers.AccountCacheProvider;
+import com.android.mail.providers.UIProvider.AccountCapabilities;
+import com.android.mail.providers.UIProvider.AccountColumns;
+import com.android.mail.providers.UIProvider.AttachmentColumns;
+import com.android.mail.providers.UIProvider.ConversationColumns;
+import com.android.mail.providers.UIProvider.FolderCapabilities;
+import com.android.mail.providers.UIProvider.FolderColumns;
+import com.android.mail.providers.UIProvider.MessageColumns;
 import com.google.common.annotations.VisibleForTesting;
-import com.google.common.collect.Lists;
-import com.google.common.collect.Maps;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
 
-import java.lang.Long;
-import java.lang.Object;
-import java.lang.Override;
-import java.lang.String;
 import java.util.Date;
 import java.util.List;
 import java.util.Map;
@@ -238,6 +233,8 @@
                         FolderCapabilities.CAN_ACCEPT_MOVED_MESSAGES));
         folderMap.put(FolderColumns.UNREAD_COUNT, unread);
         folderMap.put(FolderColumns.TOTAL_COUNT, total);
+        folderMap.put(FolderColumns.SYNC_STATUS, 0);
+        folderMap.put(FolderColumns.LAST_SYNC_RESULT, 0);
         return folderMap;
     }
 
@@ -271,8 +268,8 @@
         accountMap.put(AccountColumns.SEND_MAIL_URI, accountUri + "/sendMail");
         accountMap.put(AccountColumns.EXPUNGE_MESSAGE_URI, accountUri + "/expungeMessage");
         accountMap.put(AccountColumns.UNDO_URI, accountUri + "/undo");
-        accountMap.put(AccountColumns.STATUS_URI, accountUri + "/status");
         accountMap.put(AccountColumns.SETTINGS_INTENT_URI, "http://www.google.com");
+        accountMap.put(AccountColumns.SYNC_STATUS, 0);
 
         if (cacheMap) {
             addAccountInfoToAccountCache(accountMap);
@@ -361,8 +358,8 @@
                         (String)accountInfo.get(AccountColumns.SEND_MAIL_URI),
                         (String)accountInfo.get(AccountColumns.EXPUNGE_MESSAGE_URI),
                         (String)accountInfo.get(AccountColumns.UNDO_URI),
-                        (String)accountInfo.get(AccountColumns.STATUS_URI),
-                        (String)accountInfo.get(AccountColumns.SETTINGS_INTENT_URI));
+                        (String)accountInfo.get(AccountColumns.SETTINGS_INTENT_URI),
+                        (Integer)accountInfo.get(AccountColumns.SYNC_STATUS));
 
 
         AccountCacheProvider.addAccount(account);