Merge "FTP: Handle null pointer exception for socket accept thread synchronize."
diff --git a/bttestapp/src/org/codeaurora/bluetooth/bttestapp/MapTestActivity.java b/bttestapp/src/org/codeaurora/bluetooth/bttestapp/MapTestActivity.java
index c35b0be..e6ff140 100644
--- a/bttestapp/src/org/codeaurora/bluetooth/bttestapp/MapTestActivity.java
+++ b/bttestapp/src/org/codeaurora/bluetooth/bttestapp/MapTestActivity.java
@@ -298,14 +298,14 @@
                     new MonkeyEvent("map-getfolderlisting-size", true)
                             .addReplyParam("size", size)
                             .send();
-                    shortToast("GetMessagesListing size OK: size=" + size);
+                    shortToast("GetFolderListing size OK: size=" + size);
                 }
 
                 @Override
                 public void onGetFolderListingSizeError() {
                     goToState(Job.IDLE);
                     new MonkeyEvent("map-getfolderlisting-size", false).send();
-                    shortToast("GetMessagesListing size FAILED");
+                    shortToast("GetFolderListing size FAILED");
                 }
 
                 @Override
diff --git a/src/org/codeaurora/bluetooth/ftp/BluetoothFtpObexServer.java b/src/org/codeaurora/bluetooth/ftp/BluetoothFtpObexServer.java
index 923c531..a769ff3 100644
--- a/src/org/codeaurora/bluetooth/ftp/BluetoothFtpObexServer.java
+++ b/src/org/codeaurora/bluetooth/ftp/BluetoothFtpObexServer.java
@@ -32,6 +32,7 @@
 import android.content.Context;
 import android.os.Message;
 import android.os.Handler;
+import android.os.PowerManager;
 import android.os.StatFs;
 import android.text.TextUtils;
 import android.util.Log;
@@ -98,6 +99,8 @@
 
     private Context mContext;
 
+    private PowerManager.WakeLock mWakeLock = null;
+
     public static boolean sIsAborted = false;
 
     public static final String ROOT_FOLDER_PATH = "/sdcard";
@@ -153,13 +156,22 @@
     @Override
     public int onConnect(final HeaderSet request, HeaderSet reply) {
         if (D) Log.d(TAG, "onConnect()+");
+        acquireFtpWakeLock();
+        int returnVal = onConnectInternal(request,  reply);
+        releaseFtpWakeLock();
+        if (D) Log.d(TAG, "onConnect()- returning " + returnVal);
+        return returnVal;
+    }
+
+    private int onConnectInternal(final HeaderSet request, HeaderSet reply) {
+        if (D) Log.d(TAG, "onConnectInternal()+");
         /* Extract the Target header */
         try {
             byte[] uuid = (byte[])request.getHeader(HeaderSet.TARGET);
             if (uuid == null) {
                 return ResponseCodes.OBEX_HTTP_NOT_ACCEPTABLE;
             }
-            if (D) Log.d(TAG, "onConnect(): uuid=" + Arrays.toString(uuid));
+            if (D) Log.d(TAG, "onConnectInternal(): uuid=" + Arrays.toString(uuid));
 
             if (uuid.length != UUID_LENGTH) {
                 Log.w(TAG, "Wrong UUID length");
@@ -175,23 +187,23 @@
             /* Add the uuid into the WHO header part of connect reply */
             reply.setHeader(HeaderSet.WHO, uuid);
         } catch (IOException e) {
-            Log.e(TAG,"onConnect "+ e.toString());
+            Log.e(TAG,"onConnectInternal "+ e.toString());
             return ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
         }
         /* Extract the remote WHO header and fill it in the Target header*/
         try {
             byte[] remote = (byte[])request.getHeader(HeaderSet.WHO);
             if (remote != null) {
-                if (D) Log.d(TAG, "onConnect(): remote=" +
+                if (D) Log.d(TAG, "onConnectInternal(): remote=" +
                                                  Arrays.toString(remote));
                 reply.setHeader(HeaderSet.TARGET, remote);
             }
         } catch (IOException e) {
-            Log.e(TAG,"onConnect "+ e.toString());
+            Log.e(TAG,"onConnectInternal "+ e.toString());
             return ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
         }
 
-        if (V) Log.v(TAG, "onConnect(): uuid is ok, will send out " +
+        if (V) Log.v(TAG, "onConnectInternal(): uuid is ok, will send out " +
                 "MSG_SESSION_ESTABLISHED msg.");
         /* Notify the FTP service about the session establishment */
         Message msg = Message.obtain(mCallback);
@@ -206,7 +218,7 @@
             "\n SEC: "+ rootSecondaryStoragePath +"\n");
 
 
-        if (D) Log.d(TAG, "onConnect() -");
+        if (D) Log.d(TAG, "onConnectInternal() -");
         return ResponseCodes.OBEX_HTTP_OK;
     }
     /**
@@ -223,6 +235,7 @@
     public void onDisconnect(final HeaderSet req, final HeaderSet resp) {
         if (D) Log.d(TAG, "onDisconnect() +");
 
+        acquireFtpWakeLock();
         resp.responseCode = ResponseCodes.OBEX_HTTP_OK;
         /* Send a message to the FTP service to close the Server session */
         if (mCallback != null) {
@@ -232,6 +245,7 @@
             if (V) Log.v(TAG,
                  "onDisconnect(): msg MSG_SESSION_DISCONNECTED sent out.");
         }
+        releaseFtpWakeLock();
         if (D) Log.d(TAG, "onDisconnect() -");
     }
     /**
@@ -240,8 +254,10 @@
     @Override
     public int onAbort(HeaderSet request, HeaderSet reply) {
         if (D) Log.d(TAG, "onAbort() +");
+        acquireFtpWakeLock();
         sIsAborted = true;
         if (D) Log.d(TAG, "onAbort() -");
+        releaseFtpWakeLock();
         return ResponseCodes.OBEX_HTTP_OK;
     }
 
@@ -261,7 +277,16 @@
     */
     @Override
     public int onDelete(HeaderSet request, HeaderSet reply) {
-        if (D) Log.d(TAG, "onDelete() +");
+        if (D) Log.d(TAG, "onDelete()+");
+        acquireFtpWakeLock();
+        int returnVal = onDeleteInternal(request,  reply);
+        releaseFtpWakeLock();
+        if (D) Log.d(TAG, "onDelete()- returning " + returnVal);
+        return returnVal;
+    }
+
+    private int onDeleteInternal(HeaderSet request, HeaderSet reply) {
+        if (D) Log.d(TAG, "onDeleteInternal() +");
         String name = "";
         /* Check if Card is mounted */
         if(FileUtils.checkMountedState() == false) {
@@ -282,11 +307,11 @@
               if(D)  Log.d(TAG, "cannot delete the directory " + name);
               return ResponseCodes.OBEX_HTTP_UNAUTHORIZED;
            }
-           if (D) Log.d(TAG,"OnDelete File = " + name +
+           if (D) Log.d(TAG,"onDeleteInternal File = " + name +
                                           "mCurrentPath = " + mCurrentPath );
            File deleteFile = new File(mCurrentPath + "/" + name);
            if(deleteFile.exists() == true){
-               if (D) Log.d(TAG, "onDelete(): Found File" + name + "in folder "
+               if (D) Log.d(TAG, "onDeleteInternal(): Found File" + name + "in folder "
                                                          + mCurrentPath);
                if(!deleteFile.canWrite()) {
                    return ResponseCodes.OBEX_HTTP_UNAUTHORIZED;
@@ -311,11 +336,11 @@
                return ResponseCodes.OBEX_HTTP_NOT_FOUND;
            }
         }catch (IOException e) {
-            Log.e(TAG,"onDelete "+ e.toString());
+            Log.e(TAG,"onDeleteInternal "+ e.toString());
             if (D) Log.d(TAG, "Delete operation failed");
             return ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
         }
-        if (D) Log.d(TAG, "onDelete() -");
+        if (D) Log.d(TAG, "onDeleteInternal() -");
         return ResponseCodes.OBEX_HTTP_OK;
     }
     /**
@@ -335,7 +360,16 @@
     */
     @Override
     public int onPut(final Operation op) {
-        if (D) Log.d(TAG, "onPut() +");
+        if (D) Log.d(TAG, "onPut()+");
+        acquireFtpWakeLock();
+        int returnVal = onPutInternal(op);
+        releaseFtpWakeLock();
+        if (D) Log.d(TAG, "onPut()- returning " + returnVal);
+        return returnVal;
+    }
+
+    private int onPutInternal(final Operation op) {
+        if (D) Log.d(TAG, "onPutInternal() +");
         HeaderSet request = null;
         long length;
         String name = "";
@@ -386,7 +420,7 @@
             try {
                 in_stream = op.openInputStream();
             } catch (IOException e1) {
-                Log.e(TAG,"onPut open input stream "+ e1.toString());
+                Log.e(TAG,"onPutInternal open input stream "+ e1.toString());
                 if (D) Log.d(TAG, "Error while openInputStream");
                 return ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
             }
@@ -452,7 +486,7 @@
                     }
                 }
             }catch (IOException e1) {
-                Log.e(TAG, "onPut File receive"+ e1.toString());
+                Log.e(TAG, "onPutInternal File receive"+ e1.toString());
                 if (D) Log.d(TAG, "Error when receiving file");
                 ((ServerOperation)op).isAborted = true;
                 sIsAborted = true;
@@ -465,7 +499,7 @@
                 try {
                     buff_op_stream.close();
                  } catch (IOException e) {
-                     Log.e(TAG,"onPut close stream "+ e.toString());
+                     Log.e(TAG,"onPutInternal close stream "+ e.toString());
                      if (D) Log.d(TAG, "Error when closing stream after send");
                      return ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
                 }
@@ -491,11 +525,11 @@
                                                        fileinfo.getAbsolutePath());
 
         }catch (IOException e) {
-            Log.e(TAG, "onPut headers error "+ e.toString());
+            Log.e(TAG, "onPutInternal headers error "+ e.toString());
             if (D) Log.d(TAG, "request headers error");
             return ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
         }
-        if (D) Log.d(TAG, "onPut() -");
+        if (D) Log.d(TAG, "onPutInternal() -");
         return ResponseCodes.OBEX_HTTP_OK;
     }
     /**
@@ -518,10 +552,20 @@
     *         will be used
     */
     @Override
-    public int onSetPath(final HeaderSet request, final HeaderSet reply, final boolean backup,
-            final boolean create) {
+    public int onSetPath(final HeaderSet request, final HeaderSet reply,
+            final boolean backup, final boolean create) {
+        if (D) Log.d(TAG, "onSetPath()+");
+        acquireFtpWakeLock();
+        int returnVal = onSetPathInternal(request, reply, backup, create);
+        releaseFtpWakeLock();
+        if (D) Log.d(TAG, "onSetPath()- returning " + returnVal);
+        return returnVal;
+    }
 
-        if (D) Log.d(TAG, "onSetPath() +");
+    private int onSetPathInternal(final HeaderSet request, final HeaderSet reply,
+            final boolean backup, final boolean create) {
+
+        if (D) Log.d(TAG, "onSetPathInternal() +");
 
         String current_path_tmp = mCurrentPath;
         String tmp_path = null;
@@ -534,12 +578,12 @@
         try {
             tmp_path = (String)request.getHeader(HeaderSet.NAME);
         } catch (IOException e) {
-            Log.e(TAG,"onSetPath  get header"+ e.toString());
+            Log.e(TAG,"onSetPathInternal  get header"+ e.toString());
             if (D) Log.d(TAG, "Get name header fail");
             return ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
         }
         if (D) Log.d(TAG, "backup=" + backup + " create=" + create +
-                   " name=" + tmp_path +"     mCurrentPath = " + mCurrentPath);
+                   " name=" + tmp_path +" mCurrentPath = " + mCurrentPath);
 
         /* If the name is "." or ".." do not allow to create or set the directory */
         if (TextUtils.equals(tmp_path, FOLDER_NAME_DOT) ||
@@ -557,17 +601,20 @@
          * set the current path to ROOT Folder path
          */
         if (backup) {
-            if (D) Log.d(TAG, "current_tmp_path: " + current_path_tmp);
-            if (current_path_tmp.length() != 0 && current_path_tmp.equals(rootPrimaryStoragePath) == false
-                      && current_path_tmp.equals(rootSecondaryStoragePath) == false ) {
-                current_path_tmp = current_path_tmp.substring(0,
-                        current_path_tmp.lastIndexOf("/"));
-            } else if (current_path_tmp.equals(rootPrimaryStoragePath ) ||
-               current_path_tmp.equals(rootSecondaryStoragePath)){
-             /* We have already reached the root folder but user tries to press the
-              * back button
-              */
-               current_path_tmp = null;
+            if (current_path_tmp != null) {
+                if (D) Log.d(TAG, "current_tmp_path: " + current_path_tmp);
+                if (current_path_tmp.length() != 0 &&
+                    current_path_tmp.equals(rootPrimaryStoragePath)
+                    == false && current_path_tmp.equals(rootSecondaryStoragePath) == false ) {
+                    current_path_tmp = current_path_tmp.substring(0,
+                            current_path_tmp.lastIndexOf("/"));
+                } else if (current_path_tmp.equals(rootPrimaryStoragePath ) ||
+                   current_path_tmp.equals(rootSecondaryStoragePath)){
+                 /* We have already reached the root folder but user tries to press the
+                  * back button
+                  */
+                   current_path_tmp = null;
+                }
             }
         } else {
             //SetPath here comes into picture only when tmp_path not null.
@@ -607,16 +654,16 @@
            // new folder requested at PRIMARY and SECONDARY FOlDEERs level,
            // so return  ResponseCodes.OBEX_HTTP_NOT_ACCEPTABLE
            return ResponseCodes.OBEX_HTTP_NOT_ACCEPTABLE;
-        } else if(current_path_tmp == null && !backup ){
-           // current_path_tmp cannot be null if not backup
-           return ResponseCodes.OBEX_HTTP_NOT_FOUND;
+        } else if(current_path_tmp == null && backup ){
+           // current_path_tmp cannot be null if backup
+           return ResponseCodes.OBEX_HTTP_NOT_ACCEPTABLE;
         }
 
 
         mCurrentPath = current_path_tmp;
         if (V) Log.v(TAG, "after setPath, mCurrentPath ==  " + mCurrentPath);
 
-        if (D) Log.d(TAG, "onSetPath() -");
+        if (D) Log.d(TAG, "onSetPathInternal() -");
 
         return ResponseCodes.OBEX_HTTP_OK;
     }
@@ -626,6 +673,7 @@
     @Override
     public void onClose() {
         if (D) Log.d(TAG, "onClose() +");
+        acquireFtpWakeLock();
         /* Send a message to the FTP service to close the Server session */
         if (mCallback != null) {
             Message msg = Message.obtain(mCallback);
@@ -634,12 +682,22 @@
             if (D) Log.d(TAG,
                        "onClose(): msg MSG_SERVERSESSION_CLOSE sent out.");
         }
+        releaseFtpWakeLock();
         if (D) Log.d(TAG, "onClose() -");
     }
 
     @Override
     public int onGet(Operation op) {
-        if (D) Log.d(TAG, "onGet() +");
+        if (D) Log.d(TAG, "onGet()+");
+        acquireFtpWakeLock();
+        int returnVal = onGetInternal(op);
+        releaseFtpWakeLock();
+        if (D) Log.d(TAG, "onGet()- returning " + returnVal);
+        return returnVal;
+    }
+
+    private int onGetInternal(Operation op) {
+        if (D) Log.d(TAG, "onGetInternal() +");
 
         sIsAborted = false;
         HeaderSet request = null;
@@ -662,7 +720,7 @@
                return ResponseCodes.OBEX_HTTP_NOT_ACCEPTABLE;
             }
         } catch (IOException e) {
-            Log.e(TAG,"onGet request headers "+ e.toString());
+            Log.e(TAG,"onGetInternal request headers "+ e.toString());
             if (D) Log.d(TAG, "request headers error");
             return ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
         }
@@ -749,7 +807,7 @@
             File fileinfo = new File (mCurrentPath + "/" + name);
             return sendFileContents(op,fileinfo);
         }
-        if (D) Log.d(TAG, "onGet() -");
+        if (D) Log.d(TAG, "onGetInternal() -");
         return ResponseCodes.OBEX_HTTP_BAD_REQUEST;
     }
 
@@ -1154,4 +1212,31 @@
 
         return returnvalue;
     }
+
+    private void acquireFtpWakeLock() {
+        if (mWakeLock == null) {
+            PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
+            mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
+                "StartingObexFtpTransaction");
+            mWakeLock.setReferenceCounted(false);
+            mWakeLock.acquire();
+            if (V) Log.v(TAG, "mWakeLock acquired");
+        }
+        else
+        {
+            Log.e(TAG, "mWakeLock already acquired");
+        }
+    }
+
+    private void releaseFtpWakeLock() {
+        if (mWakeLock != null) {
+            if (mWakeLock.isHeld()) {
+                mWakeLock.release();
+                if (V) Log.v(TAG, "mWakeLock released");
+            } else {
+                if (V) Log.v(TAG, "mWakeLock already released");
+            }
+            mWakeLock = null;
+        }
+    }
 };
diff --git a/src/org/codeaurora/bluetooth/ftp/BluetoothFtpService.java b/src/org/codeaurora/bluetooth/ftp/BluetoothFtpService.java
index 3c61ddf..5a55476 100644
--- a/src/org/codeaurora/bluetooth/ftp/BluetoothFtpService.java
+++ b/src/org/codeaurora/bluetooth/ftp/BluetoothFtpService.java
@@ -37,8 +37,6 @@
 import android.content.ContentProviderClient;
 import android.content.Intent;
 import android.os.Message;
-import android.os.PowerManager;
-import android.os.PowerManager.WakeLock;
 import android.util.Log;
 import android.os.Handler;
 import android.text.TextUtils;
@@ -206,8 +204,6 @@
 
     public static boolean isL2capSocket = false;
 
-    private WakeLock mWakeLock;
-
     private Notification mConnectedNotification = null;
 
     private BluetoothAdapter mAdapter;
@@ -369,10 +365,6 @@
         if (VERBOSE) Log.v(TAG, "Ftp Service onDestroy");
 
         super.onDestroy();
-        if (mWakeLock != null) {
-            mWakeLock.release();
-            mWakeLock = null;
-        }
         closeService();
     }
 
@@ -495,19 +487,6 @@
     private final void startObexServerSession() throws IOException {
         if (VERBOSE) Log.v(TAG, "Ftp Service startObexServerSession");
 
-        // acquire the wakeLock before start Obex transaction thread
-        if (mWakeLock == null) {
-            PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
-            mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
-                    "StartingObexFtpTransaction");
-            mWakeLock.setReferenceCounted(false);
-        }
-
-        if(!mWakeLock.isHeld()) {
-            Log.e(TAG,"Acquire partial wake lock");
-            mWakeLock.acquire();
-        }
-
         mFtpServer = new BluetoothFtpObexServer(mSessionStatusHandler, this);
         synchronized (this) {
             mAuth = new BluetoothFtpAuthenticator(mSessionStatusHandler);
@@ -531,16 +510,6 @@
     private void stopObexServerSession() {
         if (VERBOSE) Log.v(TAG, "Ftp Service stopObexServerSession");
 
-        // Release the wake lock if obex transaction is over
-        if(mWakeLock != null) {
-            if (mWakeLock.isHeld()) {
-                Log.e(TAG,"Release full wake lock");
-                mWakeLock.release();
-                mWakeLock = null;
-            } else {
-                mWakeLock = null;
-            }
-        }
         if (mServerSession != null) {
             mServerSession.close();
             mServerSession = null;
diff --git a/src/org/codeaurora/bluetooth/map/BluetoothMasAppEmail.java b/src/org/codeaurora/bluetooth/map/BluetoothMasAppEmail.java
index 7d1f99e..c218e40 100644
--- a/src/org/codeaurora/bluetooth/map/BluetoothMasAppEmail.java
+++ b/src/org/codeaurora/bluetooth/map/BluetoothMasAppEmail.java
@@ -735,64 +735,40 @@
 
     private int setMsgStatusEmail(long handle,
             BluetoothMasAppParams bluetoothMasAppParams){
-        //Query the mailbox table to get the id values for Inbox and Trash folder
-        Uri uri1 = Uri.parse("content://com.android.email.provider/mailbox");
-        Cursor cr1 = mContext.getContentResolver().query(uri1, null,
-                "(UPPER(displayName) = 'INBOX' OR UPPER(displayName) LIKE '%TRASH%')", null, null);
-        int inboxFolderId = 0;
-        int deletedFolderId = 0;
-        int msgFolderId = 0;
-        String folderName;
-        if (cr1 != null && cr1.moveToFirst()) {
-            do {
-                folderName = cr1.getString(cr1.getColumnIndex("displayName"));
-                if(folderName.equalsIgnoreCase("INBOX")){
-                    inboxFolderId = cr1.getInt(cr1.getColumnIndex("_id"));
-                } else {
-                    deletedFolderId = cr1.getInt(cr1.getColumnIndex("_id"));
-                }
-            } while (cr1.moveToNext());
-        }
-        if (cr1 != null) {
-            cr1.close();
-        }
 
-        //Query the message table for the given message id
         long emailMsgId = handle - 0;
         emailMsgId = handle - OFFSET_START;
-        Uri uri2 = Uri.parse("content://com.android.email.provider/message/"+emailMsgId);
-        Cursor crEmail = mContext.getContentResolver().query(uri2, null, null, null, null);
-        if (crEmail != null && crEmail.moveToFirst()) {
-            if (bluetoothMasAppParams.StatusIndicator == 0) {
-                /* Read Status */
-                ContentValues values = new ContentValues();
-                values.put("flagRead", bluetoothMasAppParams.StatusValue);
-                mContext.getContentResolver().update(uri2, values, null, null);
-            } else {
+        if (V) Log.v(TAG, " Msg id before Mail service:: " + emailMsgId);
+
+        long accountId = EmailUtils.getAccountId(mMasId);
+        if (V) Log.v(TAG, " Account id before Mail service:: " + accountId);
+
+        if(accountId <= -1 || emailMsgId <= -1){
+             return  ResponseCodes.OBEX_HTTP_BAD_REQUEST;
+        }
+
+        Intent emailIn = new Intent();
+        if (bluetoothMasAppParams.StatusIndicator == 0) {
+            // Read Status
+            emailIn.setAction("org.codeaurora.email.intent.action.MAIL_SERVICE_MESSAGE_READ");
+            emailIn.putExtra("org.codeaurora.email.intent.extra.MESSAGE_INFO",
+                (bluetoothMasAppParams.StatusValue ==1) ? 1:0);
+        } else {
                 if (bluetoothMasAppParams.StatusValue == 1) { //if the email is deleted
-                    msgFolderId = crEmail.getInt(crEmail.getColumnIndex("mailboxKey"));
-                    if(msgFolderId == deletedFolderId){
-                        // TODO: need to add notification for deleted email here
-                        mMnsClient.addMceInitiatedOperation(Long.toString(handle));
-                        mContext.getContentResolver().delete(
-                                Uri.parse("content://com.android.email.provider/message/"
-                                + emailMsgId), null, null);
-                    } else {
-                        ContentValues values = new ContentValues();
-                        values.put("mailboxKey", deletedFolderId);
-                        mContext.getContentResolver().update(uri2, values, null, null);
-                    }
+                    // TODO: need to add notification for deleted email here
+                    mMnsClient.addMceInitiatedOperation(Long.toString(handle));
+                    emailIn.setAction("org.codeaurora.email.intent.action.MAIL_SERVICE_DELETE_MESSAGE");
                 } else { // if the email is undeleted
+                    emailIn.setAction("org.codeaurora.email.intent.action.MAIL_SERVICE_MOVE_MESSAGE");
                     // TODO: restore it to original folder
-                    ContentValues values = new ContentValues();
-                    values.put("mailboxKey", inboxFolderId);
-                    mContext.getContentResolver().update(uri2, values, null, null);
+                    emailIn.putExtra("org.codeaurora.email.intent.extra.MESSAGE_INFO", TYPE_INBOX);
                 }
-            }
         }
-        if (crEmail != null) {
-            crEmail.close();
-        }
+
+        emailIn.putExtra("com.android.email.intent.extra.ACCOUNT", accountId);
+        emailIn.putExtra("org.codeaurora.email.intent.extra.MESSAGE_ID", emailMsgId);
+        mContext.startService(emailIn);
+
         return ResponseCodes.OBEX_HTTP_OK;
     }
 }