Add support for the flag (favorite) property in EAS 12; cleanup

* Support flag property in Exchange 2007 (EAS 12)
* Support startSync/stopSync/reloadFolderList in EAS service
* Cleanup files a little bit
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index a49bfba..48224b2 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -173,6 +173,8 @@
             android:enabled="false"
             >
         </service>
+        
+        <!-- Add android:process=":remote" below to enable SyncManager as a separate process -->
         <service
             android:name="com.android.exchange.SyncManager"
             android:enabled="true"
diff --git a/src/com/android/exchange/SyncManager.java b/src/com/android/exchange/SyncManager.java
index cf71cf7..b66f92c 100644
--- a/src/com/android/exchange/SyncManager.java
+++ b/src/com/android/exchange/SyncManager.java
@@ -22,6 +22,7 @@
 import com.android.exchange.EmailContent.Attachment;
 import com.android.exchange.EmailContent.HostAuth;
 import com.android.exchange.EmailContent.Mailbox;
+import com.android.exchange.EmailContent.MailboxColumns;
 import com.android.exchange.EmailContent.Message;
 import com.android.exchange.EmailContent.MessageColumns;
 import com.android.exchange.EmailContent.SyncColumns;
@@ -100,6 +101,7 @@
      * spins its wheels counting up to 100%.
      */
     private final IEmailService.Stub mBinder = new IEmailService.Stub() {
+
         public int validate(String protocol, String host, String userName, String password,
                 int port, boolean ssl) throws RemoteException {
             try {
@@ -112,35 +114,11 @@
         }
 
         public void startSync(long mailboxId) throws RemoteException {
-            // TODO Auto-generated method stub
+            startManualSync(mailboxId, null);
         }
 
         public void stopSync(long mailboxId) throws RemoteException {
-            // TODO Auto-generated method stub
-        }
-
-        public void updateFolderList(long accountId) throws RemoteException {
-            // TODO Auto-generated method stub
-        }
-
-        public void loadMore(long messageId, IEmailServiceCallback cb) throws RemoteException {
-            // TODO Auto-generated method stub
-        }
-
-        public boolean createFolder(long accountId, String name) throws RemoteException {
-            // TODO Auto-generated method stub
-            return false;
-        }
-
-        public boolean deleteFolder(long accountId, String name) throws RemoteException {
-            // TODO Auto-generated method stub
-            return false;
-        }
-
-        public boolean renameFolder(long accountId, String oldName, String newName)
-                throws RemoteException {
-            // TODO Auto-generated method stub
-            return false;
+            stopManualSync(mailboxId);
         }
 
         public void loadAttachment(long attachmentId, IEmailServiceCallback cb)
@@ -148,6 +126,45 @@
             Attachment att = Attachment.restoreAttachmentWithId(SyncManager.this, attachmentId);
             partRequest(new PartRequest(att, cb));
         }
+
+        public void updateFolderList(long accountId) throws RemoteException {
+            Cursor c = getContentResolver().query(Mailbox.CONTENT_URI,
+                    Mailbox.CONTENT_PROJECTION, MailboxColumns.ACCOUNT_KEY + "=? AND " +
+                    MailboxColumns.SERVER_ID + "=?",
+                    new String[] {Long.toString(accountId), Eas.ACCOUNT_MAILBOX}, null);
+            try {
+                if (c.moveToFirst()) {
+                    synchronized(mSyncToken) {
+                        AbstractSyncService svc =
+                            INSTANCE.mServiceMap.get(c.getLong(Mailbox.CONTENT_ID_COLUMN));
+                        // TODO See if this is sufficient.  Low priority since there is no
+                        // user-driven "update folder list" in the UI
+                        svc.mThread.interrupt();
+                    }
+                }
+            } finally {
+                c.close();
+            }
+        }
+
+        public void loadMore(long messageId, IEmailServiceCallback cb) throws RemoteException {
+            // TODO Auto-generated method stub
+        }
+
+        // The following three methods are not implemented in this version
+        public boolean createFolder(long accountId, String name) throws RemoteException {
+            return false;
+        }
+
+        public boolean deleteFolder(long accountId, String name) throws RemoteException {
+            return false;
+        }
+
+        public boolean renameFolder(long accountId, String oldName, String newName)
+                throws RemoteException {
+            return false;
+        }
+
     };
 
     class AccountObserver extends ContentObserver {
diff --git a/src/com/android/exchange/adapter/EasEmailSyncAdapter.java b/src/com/android/exchange/adapter/EasEmailSyncAdapter.java
index 4b25c11..e1f2bd5 100644
--- a/src/com/android/exchange/adapter/EasEmailSyncAdapter.java
+++ b/src/com/android/exchange/adapter/EasEmailSyncAdapter.java
@@ -152,6 +152,9 @@
                     case EasTags.BASE_BODY:
                         bodyParser(msg);
                         break;
+                    case EasTags.EMAIL_FLAG:
+                        msg.mFlagFavorite = flagParser();
+                        break;
                     case EasTags.EMAIL_BODY:
                         msg.mTextInfo = "X;X;8;" + size; // location;encoding;charset;size
                         msg.mText = getValue();
@@ -197,6 +200,21 @@
             emails.add(msg);
         }
 
+        // For now, we only care about the "active" state
+        private Boolean flagParser() throws IOException {
+            Boolean state = false;
+            while (nextTag(EasTags.EMAIL_FLAG) != END) {
+                switch (tag) {
+                    case EasTags.EMAIL_FLAG_STATUS:
+                        state = true;
+                        break;
+                    default:
+                        skipTag();
+                }
+            }
+            return state;
+        }
+
         private void bodyParser(Message msg) throws IOException {
             String bodyType = Eas.BODY_PREFERENCE_TEXT;
             String body = "";
@@ -327,18 +345,22 @@
 
         class ServerChange {
             long id;
-            boolean read;
+            Boolean read;
+            Boolean flag;
 
-            ServerChange(long _id, boolean _read) {
+            ServerChange(long _id, Boolean _read, Boolean _flag) {
                 id = _id;
                 read = _read;
+                flag = _flag;
             }
         }
 
         private void changeParser(ArrayList<ServerChange> changes) throws IOException {
             String serverId = null;
-            boolean oldRead = false;
-            boolean read = true;
+            Boolean oldRead = false;
+            Boolean read = null;
+            Boolean oldFlag = false;
+            Boolean flag = null;
             long id = 0;
             while (nextTag(EasTags.SYNC_CHANGE) != END) {
                 switch (tag) {
@@ -349,6 +371,7 @@
                             if (c.moveToFirst()) {
                                 mService.userLog("Changing " + serverId);
                                 oldRead = c.getInt(Message.LIST_READ_COLUMN) == Message.READ;
+                                oldFlag = c.getInt(Message.LIST_FAVORITE_COLUMN) == 1;
                                 id = c.getLong(Message.LIST_ID_COLUMN);
                             }
                         } finally {
@@ -358,14 +381,18 @@
                     case EasTags.EMAIL_READ:
                         read = getValueInt() == 1;
                         break;
+                    case EasTags.EMAIL_FLAG:
+                        flag = flagParser();
+                        break;
                     case EasTags.SYNC_APPLICATION_DATA:
                         break;
                     default:
                         skipTag();
                 }
             }
-            if (oldRead != read) {
-                changes.add(new ServerChange(id, read));
+            if ((read != null && !oldRead.equals(read)) ||
+                    (flag != null && !oldFlag.equals(flag))) {
+                changes.add(new ServerChange(id, read, flag));
             }
         }
 
@@ -401,9 +428,13 @@
             if (!changedEmails.isEmpty()) {
                 // Server wins in a conflict...
                 for (ServerChange change : changedEmails) {
-                    // For now, don't handle read->unread
-                    ContentValues cv = new ContentValues();
-                    cv.put(MessageColumns.FLAG_READ, change.read);
+                     ContentValues cv = new ContentValues();
+                    if (change.read != null) {
+                        cv.put(MessageColumns.FLAG_READ, change.read);
+                    }
+                    if (change.flag != null) {
+                        cv.put(MessageColumns.FLAG_FAVORITE, change.flag);
+                    }
                     ops.add(ContentProviderOperation.newUpdate(
                             ContentUris.withAppendedId(Message.CONTENT_URI, change.id))
                                 .withValues(cv)
@@ -541,6 +572,7 @@
                     }
                     // Send the change to "read".  We'll do "flagged" here eventually as well
                     // TODO Add support for flags here (EAS 12.0 and above)
+                    // Or is this not safe??
                     s.start("Change")
                         .data("ServerId", c.getString(Message.LIST_SERVER_ID_COLUMN))
                         .start("ApplicationData")