Skip to content

Commit

Permalink
Default to not include MMS part sub_ids in import
Browse files Browse the repository at this point in the history
Some MMS part metadata apparently contain a sub_id field (despite the
absence of any mention of this in the API documentation at
https://developer.android.com/reference/android/provider/Telephony.Mms.Part),
and attempting to import these sub_ids can cause the app to crash with a
FileNotFoundException: No entry for content.

This is probably related to #128.
In any event, this commit fixes the problem by extending the solution to
that issue (c56fa0e) to MMS part
metadata sub_ids.

Addresses: #142
Related: #128
  • Loading branch information
tmo1 committed Dec 20, 2023
1 parent 7170506 commit 9dcff76
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ Note that upon import or wipe, message apps present on the system may not immedi

SMS Import / Export tries to preserve as much data and metadata as possible upon import. Android includes a `sub_id` ([Subscription ID](https://developer.android.com/training/articles/user-data-ids#accounts)) field in both [SMS](https://developer.android.com/reference/android/provider/Telephony.TextBasedSmsColumns) and [MMS](https://developer.android.com/reference/android/provider/Telephony.BaseMmsColumns#SUBSCRIPTION_ID) message metadata. Earlier versions of the app included these `sub_id`s when importing, but this can cause messages to disappear on Android 14 ([issue #128](https://github.com/tmo1/sms-ie/issues/128), [Reddit](https://old.reddit.com/r/android_beta/comments/15mzaij/sms_backup_and_restore_issues/)), so the current default is to set all `sub_id`s to `-1` upon import ([negative values indicate that "the sub id cannot be determined"](https://developer.android.com/reference/android/provider/Telephony.TextBasedSmsColumns#SUBSCRIPTION_ID)). The old behavior is still available via a settings toggle.

Additionally, some MMS part metadata apparently contain a `sub_id` field as well (despite the absence of any mention of this in [the API documentation](https://developer.android.com/reference/android/provider/Telephony.Mms.Part)), and [attempting to import these `sub_id`s can cause the app to crash](https://github.com/tmo1/sms-ie/issues/142). These `sub_id`s are currently handled the same way as the ones in the SMS and MMS metadata.

### Deduplication

SMS Import / Export can attempt to deduplicate messages upon import. If this feature is enabled in the app's settings, the app will check all new messages against the existing message database (including those messages already inserted earlier in the import process) and ignore those it considers to be duplicates of ones already present. This feature is currently considered experimental.
Expand Down
21 changes: 13 additions & 8 deletions app/src/main/java/com/github/tmo1/sms_ie/ImportExportMessages.kt
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,7 @@ suspend fun importMessages(
if (key in smsColumns) messageMetadata.put(
key, messageJSON.getString(key)
)
}
/* If we don't yet have a 'thread_id' (i.e., the message has a new
}/* If we don't yet have a 'thread_id' (i.e., the message has a new
'thread_id' that we haven't yet encountered and so isn't yet in
'threadIdMap'), then we need to get a new 'thread_id' and record the mapping
between the old and new ones in 'threadIdMap'
Expand Down Expand Up @@ -526,8 +525,7 @@ suspend fun importMessages(
addresses.add(address)
}
}
}
/* If we don't yet have a thread_id (i.e., the message has a new
}/* If we don't yet have a thread_id (i.e., the message has a new
thread_id that we haven't yet encountered and so isn't yet in
threadIdMap), then we need to get a new thread_id and record the mapping
between the old and new ones in threadIdMap
Expand Down Expand Up @@ -562,8 +560,7 @@ suspend fun importMessages(
addresses.forEach { address ->
address.put(
Telephony.Mms.Addr.MSG_ID, messageId
)
/*Log.v(
)/*Log.v(
LOG_TAG,
"Trying to insert MMS address - metadata:" + address.toString()
)*/
Expand All @@ -572,8 +569,7 @@ suspend fun importMessages(
addressUri, address
)
if (insertAddressUri == null) Log.e(
LOG_TAG,
"MMS address insert failed!"
LOG_TAG, "MMS address insert failed!"
)
else Log.d(LOG_TAG, "MMS address insert succeeded")
}
Expand All @@ -589,6 +585,15 @@ suspend fun importMessages(
partKey, messagePart.getString(partKey)
)
}
// Some MMS part metadata contain sub_ids, and attempting to import them can cause a FileNotFoundException: No entry for content
// when subsequently trying to write the part's binary data.
// See: https://github.com/tmo1/sms-ie/issues/142
if (!prefs.getBoolean(
"import_sub_ids", false
) && part.containsKey("sub_id")
) {
part.put("sub_id", "-1")
}
val insertPartUri =
appContext.contentResolver.insert(
partUri, part
Expand Down

0 comments on commit 9dcff76

Please # to comment.