Merge "32394618, 36899165, 37245921 Fix unread and text_only flags"
diff --git a/src/com/android/providers/telephony/TelephonyBackupAgent.java b/src/com/android/providers/telephony/TelephonyBackupAgent.java
index 85ccc76..0a0ebb6 100644
--- a/src/com/android/providers/telephony/TelephonyBackupAgent.java
+++ b/src/com/android/providers/telephony/TelephonyBackupAgent.java
@@ -278,12 +278,15 @@
 
 
     static {
-        // Consider restored messages seen, but use the read value from the data.
+        // Consider restored messages read and seen by default. The actual data can override
+        // these values.
+        sDefaultValuesSms.put(Telephony.Sms.READ, 1);
         sDefaultValuesSms.put(Telephony.Sms.SEEN, 1);
         sDefaultValuesSms.put(Telephony.Sms.ADDRESS, UNKNOWN_SENDER);
         // If there is no sub_id with self phone number on restore set it to -1.
         sDefaultValuesSms.put(Telephony.Sms.SUBSCRIPTION_ID, -1);
 
+        sDefaultValuesMms.put(Telephony.Mms.READ, 1);
         sDefaultValuesMms.put(Telephony.Mms.SEEN, 1);
         sDefaultValuesMms.put(Telephony.Mms.SUBSCRIPTION_ID, -1);
         sDefaultValuesMms.put(Telephony.Mms.MESSAGE_BOX, Telephony.Mms.MESSAGE_BOX_ALL);
@@ -363,9 +366,8 @@
         try (
                 Cursor smsCursor = mContentResolver.query(Telephony.Sms.CONTENT_URI, SMS_PROJECTION,
                         null, null, ORDER_BY_DATE);
-                // Do not backup non text-only MMS's.
                 Cursor mmsCursor = mContentResolver.query(Telephony.Mms.CONTENT_URI, MMS_PROJECTION,
-                        Telephony.Mms.TEXT_ONLY+"=1", null, ORDER_BY_DATE)) {
+                        null, null, ORDER_BY_DATE)) {
 
             if (smsCursor != null) {
                 smsCursor.moveToFirst();
@@ -832,6 +834,7 @@
     private int writeMmsToWriter(JsonWriter jsonWriter, Cursor cursor) throws IOException {
         final int mmsId = cursor.getInt(ID_IDX);
         final MmsBody body = getMmsBody(mmsId);
+        // We backup any message that contains text, but only backup the text part.
         if (body == null || body.text == null) {
             return 0;
         }
@@ -952,6 +955,9 @@
         if (bodyText != null) {
             mms.body = new MmsBody(bodyText, bodyCharset);
         }
+        // Set the text_only flag
+        mms.values.put(Telephony.Mms.TEXT_ONLY, (mms.attachments == null
+                || mms.attachments.size() == 0) && bodyText != null ? 1 : 0);
 
         // Set default charset for subject.
         if (mms.values.get(Telephony.Mms.SUBJECT) != null &&
@@ -1186,7 +1192,7 @@
             mContentResolver.update(partUri, values, null, null);
         }
 
-        { // Insert adderesses into "addr".
+        { // Insert addresses into "addr".
             final Uri addrUri = Uri.withAppendedPath(mmsUri, "addr");
             for (ContentValues mmsAddress : mms.addresses) {
                 ContentValues values = new ContentValues(mmsAddress);
diff --git a/tests/src/com/android/providers/telephony/TelephonyBackupAgentTest.java b/tests/src/com/android/providers/telephony/TelephonyBackupAgentTest.java
index 812cc17..7e53cf3 100644
--- a/tests/src/com/android/providers/telephony/TelephonyBackupAgentTest.java
+++ b/tests/src/com/android/providers/telephony/TelephonyBackupAgentTest.java
@@ -59,6 +59,8 @@
  * Tests for testing backup/restore of SMS and text MMS messages.
  * For backup it creates fake provider and checks resulting json array.
  * For restore provides json array and checks inserts of the messages into provider.
+ *
+ * To run this test from the android root: runtest --path packages/providers/TelephonyProvider/
  */
 @TargetApi(Build.VERSION_CODES.O)
 public class TelephonyBackupAgentTest extends AndroidTestCase {
@@ -227,7 +229,7 @@
         mMmsAttachmentJson = new String[1];
         mMmsAttachmentRows[0] = createMmsRow(1 /*id*/, 1 /*subid*/, "Subject 1" /*subject*/,
                 100 /*subcharset*/, 111111 /*date*/, 111112 /*datesent*/, 3 /*type*/,
-                17 /*version*/, 1 /*textonly*/,
+                17 /*version*/, 0 /*textonly*/,
                 11 /*msgBox*/, "location 1" /*contentLocation*/, "MMs body 1" /*body*/,
                 111 /*body charset*/,
                 new String[]{"+111 (111) 11111111", "+11121212", "example@example.com",
@@ -250,6 +252,7 @@
                 "\"example@example.com\",\"charset\":102},{\"type\":13,\"address\":\"+999999999\"" +
                 ",\"charset\":103}],\"mms_body\":\"MMs body 1\",\"mms_charset\":111,\"" +
                 "sub_cs\":\"100\"}";
+
         mMmsAllAttachmentJson = makeJsonArray(mMmsAttachmentJson);
 
 
@@ -515,6 +518,17 @@
     }
 
     /**
+     * Test with attachment mms.
+     * @throws Exception
+     */
+    public void testBackupMmsWithAttachmentMms() throws Exception {
+        mTelephonyBackupAgent.mMaxMsgPerFile = 4;
+        mMmsTable.addAll(Arrays.asList(mMmsAttachmentRows));
+        mTelephonyBackupAgent.putMmsMessagesToJson(mMmsCursor, new JsonWriter(mStringWriter));
+        assertEquals(mMmsAllAttachmentJson, mStringWriter.toString());
+    }
+
+    /**
      * Test with 3 mms in the provider with the limit per file 1.
      * @throws Exception
      */