Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6710ad8d authored by Ashwini Munigala's avatar Ashwini Munigala Committed by Linux Build Service Account
Browse files

MAP: Modify GET Message email to read data under file in contentURI.

Email App Implementation moved email body parts storage to files
instead of direct db entries. Modify GET email Message implemenation
to fetch contentURI and data from corresponding files from Email db.

Change-Id: I7d3f945cfce43b004a64371f5d9d50101ddcc044
CRs-fixed: 719492
parent 247eb444
Loading
Loading
Loading
Loading
+112 −49
Original line number Diff line number Diff line
@@ -24,6 +24,9 @@ import android.text.format.Time;

import org.apache.http.util.ByteArrayBuffer;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import android.os.ParcelFileDescriptor;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
@@ -43,7 +46,9 @@ import android.text.TextUtils;
import android.text.format.Time;
import android.util.TimeFormatException;
import com.android.emailcommon.provider.EmailContent;
import com.android.emailcommon.provider.EmailContent.Body;
import com.android.emailcommon.provider.EmailContent.Message;
import com.android.emailcommon.provider.EmailContent.BodyColumns;
import com.android.emailcommon.provider.EmailContent.MessageColumns;
import com.android.emailcommon.provider.EmailContent.SyncColumns;

@@ -1956,6 +1961,32 @@ public class BluetoothMapContent {
        return retVal;
    }

    /*
     * Read  email body data under content URI.
     */
    private String readEmailBodyForMessageFd(ParcelFileDescriptor fd) {
        StringBuilder email = new StringBuilder("");
        FileInputStream is = null;
        try {
            int len=-1;
            is = new FileInputStream(fd.getFileDescriptor());
            byte[] buffer = new byte[1024];
            while((len = is.read(buffer)) != -1) {
                email.append(new String(buffer,0,len));
                if(V) Log.v(TAG, "Email part = " + new String(buffer,0,len) + " len=" + len);
            }
        } catch (NullPointerException e) {
            Log.w(TAG, e);
        } catch (IOException e) {
            Log.w(TAG, e);
        } finally {
            try {
                if(is != null)
                    is.close();
                } catch (IOException e) {}
        }
       return email.toString();
   }

    /**
     * Read out the mms parts and update the bMessage object provided i {@linkplain message}
@@ -1971,11 +2002,9 @@ public class BluetoothMapContent {
        BluetoothMapbMessageMmsEmail.MimePart part;
        Cursor c = null;
        try {
            c = mResolver.query(
            uriAddress,
            null,
            where,
            null, null);
            c = mResolver.query(uriAddress,
                    Body.CONTENT_PROJECTION, BodyColumns.MESSAGE_KEY + "=?",
                    new String[] {String.valueOf(id)}, null);
        } catch (Exception e){

           Log.w(TAG, " EMAIL BODY QUERY FAILDED " + e);
@@ -1983,9 +2012,32 @@ public class BluetoothMapContent {
        if(c != null) {
           if (V) Log.v(TAG, "cursor not null");
           if (c.moveToFirst()) {
               emailBody = c.getString(c.getColumnIndex("textContent"));
               if (emailBody == null || emailBody.length() == 0) {
                   String msgBody = c.getString(c.getColumnIndex("htmlContent"));
                String textContentURI = c.getString(c.getColumnIndex(BodyColumns.TEXT_CONTENT_URI));
                String htmlContentURI = c.getString(c.getColumnIndex(BodyColumns.HTML_CONTENT_URI));
                if(textContentURI != null || htmlContentURI != null ) {
                    if(V) {
                        Log.v(TAG, " EMAIL BODY textURI " + textContentURI);
                        Log.v(TAG, " EMAIL BODY htmlURI " + htmlContentURI);
                    }
                    // GET FD to parse text or HTML content
                    ParcelFileDescriptor fd = null;
                    try {
                       Log.v(TAG, " TRY EMAIL BODY textURI " + textContentURI);
                       fd = mResolver.openFileDescriptor(Uri.parse(textContentURI), "r");
                    } catch (FileNotFoundException e) {
                      Log.w(TAG, e);
                    }
                    if(fd == null ) {
                       //Try HTML content if  TEXT CONTENT NULL
                     try {
                       Log.v(TAG, " TRY EMAIL BODY htmlURI " + htmlContentURI);
                       fd = mResolver.openFileDescriptor(Uri.parse(htmlContentURI), "r");
                       } catch (FileNotFoundException e) {
                           Log.w(TAG, e);
                       } catch (NullPointerException e) {
                          Log.w(TAG, e);
                       }
                       String msgBody = readEmailBodyForMessageFd(fd);
                       if (msgBody != null) {
                          msgBody = msgBody.replaceAll("(?s)(<title>)(.*?)(</title>)", "");
                          msgBody = msgBody.replaceAll("(?s)(<style type=\"text/css\".*?>)(.*?)(</style>)", "");
@@ -1997,9 +2049,12 @@ public class BluetoothMapContent {
                          emailBody = emailBody.replaceAll("(?s)(\\r)", "");
                          emailBody = emailBody.replaceAll("(?s)(\\n)", "\r\n");
                       }
                   message.setEmailBody(emailBody);
                    } else {
                        emailBody = readEmailBodyForMessageFd(fd);
                    }

                    //Set BMessage emailBody
                    message.setEmailBody(emailBody);
                    //Parts
                    Long partId = c.getLong(c.getColumnIndex(BaseColumns._ID));
                    String contentType = "Content-Type: text/plain; charset=\"UTF-8\"";
                    String name = null;//c.getString(c.getColumnIndex("displayName"));
@@ -2028,7 +2083,15 @@ public class BluetoothMapContent {
                        part.charsetName = null;
                    } finally {
                    }
                    try {
                       if(fd != null)
                         fd.close();
                    } catch (IOException e) {}
                } else {
                    Log.w(TAG, " FETCH Email BODY File URI FAILED");
                }
           }
           c.close();
        }
        message.updateCharset();
        message.setEncoding("8BIT");