2018-01-25 03:17:44 +00:00
|
|
|
/*
|
2011-12-20 18:20:44 +00:00
|
|
|
* Copyright (C) 2011 Whisper Systems
|
2012-08-03 03:23:41 +00:00
|
|
|
*
|
2011-12-20 18:20:44 +00:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2012-08-03 03:23:41 +00:00
|
|
|
*
|
2011-12-20 18:20:44 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
package org.thoughtcrime.securesms.database;
|
|
|
|
|
2012-08-03 03:23:41 +00:00
|
|
|
import android.content.Context;
|
|
|
|
import android.database.Cursor;
|
2015-10-26 19:33:45 +00:00
|
|
|
import android.database.sqlite.SQLiteException;
|
2012-08-03 03:23:41 +00:00
|
|
|
import android.net.Uri;
|
2017-08-01 15:56:00 +00:00
|
|
|
import android.support.annotation.Nullable;
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
import net.sqlcipher.database.SQLiteDatabase;
|
|
|
|
import net.sqlcipher.database.SQLiteStatement;
|
|
|
|
|
2018-08-01 15:09:24 +00:00
|
|
|
import org.thoughtcrime.securesms.logging.Log;
|
2017-08-01 15:56:00 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
import java.util.HashSet;
|
2017-07-26 16:59:15 +00:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
2017-08-01 15:56:00 +00:00
|
|
|
import java.util.Set;
|
2012-08-03 03:23:41 +00:00
|
|
|
import java.util.StringTokenizer;
|
2011-12-20 18:20:44 +00:00
|
|
|
|
|
|
|
public class SmsMigrator {
|
|
|
|
|
2015-10-26 19:33:45 +00:00
|
|
|
private static final String TAG = SmsMigrator.class.getSimpleName();
|
|
|
|
|
2012-08-03 03:23:41 +00:00
|
|
|
private static void addStringToStatement(SQLiteStatement statement, Cursor cursor,
|
|
|
|
int index, String key)
|
|
|
|
{
|
2011-12-20 18:20:44 +00:00
|
|
|
int columnIndex = cursor.getColumnIndexOrThrow(key);
|
2012-08-03 03:23:41 +00:00
|
|
|
|
|
|
|
if (cursor.isNull(columnIndex)) {
|
2011-12-20 18:20:44 +00:00
|
|
|
statement.bindNull(index);
|
2012-08-03 03:23:41 +00:00
|
|
|
} else {
|
2011-12-20 18:20:44 +00:00
|
|
|
statement.bindString(index, cursor.getString(columnIndex));
|
2012-08-03 03:23:41 +00:00
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
|
2012-08-03 03:23:41 +00:00
|
|
|
private static void addIntToStatement(SQLiteStatement statement, Cursor cursor,
|
|
|
|
int index, String key)
|
|
|
|
{
|
2011-12-20 18:20:44 +00:00
|
|
|
int columnIndex = cursor.getColumnIndexOrThrow(key);
|
2012-08-03 03:23:41 +00:00
|
|
|
|
|
|
|
if (cursor.isNull(columnIndex)) {
|
2011-12-20 18:20:44 +00:00
|
|
|
statement.bindNull(index);
|
2012-08-03 03:23:41 +00:00
|
|
|
} else {
|
|
|
|
statement.bindLong(index, cursor.getLong(columnIndex));
|
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-08-03 03:23:41 +00:00
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
@SuppressWarnings("SameParameterValue")
|
|
|
|
private static void addTranslatedTypeToStatement(SQLiteStatement statement, Cursor cursor, int index, String key)
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
{
|
|
|
|
int columnIndex = cursor.getColumnIndexOrThrow(key);
|
|
|
|
|
|
|
|
if (cursor.isNull(columnIndex)) {
|
2018-01-31 01:45:12 +00:00
|
|
|
statement.bindLong(index, SmsDatabase.Types.BASE_INBOX_TYPE);
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
} else {
|
|
|
|
long theirType = cursor.getLong(columnIndex);
|
2018-01-31 01:45:12 +00:00
|
|
|
statement.bindLong(index, SmsDatabase.Types.translateFromSystemBaseType(theirType));
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-13 03:35:52 +00:00
|
|
|
private static boolean isAppropriateTypeForMigration(Cursor cursor, int columnIndex) {
|
|
|
|
long systemType = cursor.getLong(columnIndex);
|
|
|
|
long ourType = SmsDatabase.Types.translateFromSystemBaseType(systemType);
|
|
|
|
|
|
|
|
return ourType == MmsSmsColumns.Types.BASE_INBOX_TYPE ||
|
|
|
|
ourType == MmsSmsColumns.Types.BASE_SENT_TYPE ||
|
|
|
|
ourType == MmsSmsColumns.Types.BASE_SENT_FAILED_TYPE;
|
|
|
|
}
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
private static void getContentValuesForRow(Context context, Cursor cursor,
|
|
|
|
long threadId, SQLiteStatement statement)
|
2012-08-03 03:23:41 +00:00
|
|
|
{
|
2017-08-10 19:30:13 +00:00
|
|
|
String theirAddress = cursor.getString(cursor.getColumnIndexOrThrow(SmsDatabase.ADDRESS));
|
|
|
|
statement.bindString(1, Address.fromExternal(context, theirAddress).serialize());
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
addIntToStatement(statement, cursor, 2, SmsDatabase.PERSON);
|
2013-01-07 02:47:20 +00:00
|
|
|
addIntToStatement(statement, cursor, 3, SmsDatabase.DATE_RECEIVED);
|
2013-01-06 21:13:14 +00:00
|
|
|
addIntToStatement(statement, cursor, 4, SmsDatabase.DATE_RECEIVED);
|
|
|
|
addIntToStatement(statement, cursor, 5, SmsDatabase.PROTOCOL);
|
|
|
|
addIntToStatement(statement, cursor, 6, SmsDatabase.READ);
|
|
|
|
addIntToStatement(statement, cursor, 7, SmsDatabase.STATUS);
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
addTranslatedTypeToStatement(statement, cursor, 8, SmsDatabase.TYPE);
|
2013-01-06 21:13:14 +00:00
|
|
|
addIntToStatement(statement, cursor, 9, SmsDatabase.REPLY_PATH_PRESENT);
|
|
|
|
addStringToStatement(statement, cursor, 10, SmsDatabase.SUBJECT);
|
2018-01-25 03:17:44 +00:00
|
|
|
addStringToStatement(statement, cursor, 11, SmsDatabase.BODY);
|
2013-01-06 21:13:14 +00:00
|
|
|
addStringToStatement(statement, cursor, 12, SmsDatabase.SERVICE_CENTER);
|
2012-08-03 03:23:41 +00:00
|
|
|
|
2013-01-06 21:13:14 +00:00
|
|
|
statement.bindLong(13, threadId);
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-08-03 03:23:41 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private static String getTheirCanonicalAddress(Context context, String theirRecipientId) {
|
2012-08-03 03:23:41 +00:00
|
|
|
Uri uri = Uri.parse("content://mms-sms/canonical-address/" + theirRecipientId);
|
2011-12-20 18:20:44 +00:00
|
|
|
Cursor cursor = null;
|
2012-08-03 03:23:41 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
try {
|
|
|
|
cursor = context.getContentResolver().query(uri, null, null, null, null);
|
2012-08-03 03:23:41 +00:00
|
|
|
|
|
|
|
if (cursor != null && cursor.moveToFirst()) {
|
|
|
|
return cursor.getString(0);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2013-02-24 19:59:43 +00:00
|
|
|
} catch (IllegalStateException iae) {
|
|
|
|
Log.w("SmsMigrator", iae);
|
|
|
|
return null;
|
2011-12-20 18:20:44 +00:00
|
|
|
} finally {
|
|
|
|
if (cursor != null)
|
2012-08-03 03:23:41 +00:00
|
|
|
cursor.close();
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
}
|
2012-08-03 03:23:41 +00:00
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
private static @Nullable Set<Recipient> getOurRecipients(Context context, String theirRecipients) {
|
|
|
|
StringTokenizer tokenizer = new StringTokenizer(theirRecipients.trim(), " ");
|
|
|
|
Set<Recipient> recipientList = new HashSet<>();
|
2012-08-03 03:23:41 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
while (tokenizer.hasMoreTokens()) {
|
|
|
|
String theirRecipientId = tokenizer.nextToken();
|
|
|
|
String address = getTheirCanonicalAddress(context, theirRecipientId);
|
2012-08-03 03:23:41 +00:00
|
|
|
|
2017-07-26 16:59:15 +00:00
|
|
|
if (address != null) {
|
2017-08-22 01:32:38 +00:00
|
|
|
recipientList.add(Recipient.from(context, Address.fromExternal(context, address), true));
|
2017-07-26 16:59:15 +00:00
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-08-03 03:23:41 +00:00
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
if (recipientList.isEmpty()) return null;
|
|
|
|
else return recipientList;
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-08-03 03:23:41 +00:00
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
private static void migrateConversation(Context context, SmsMigrationProgressListener listener,
|
2013-02-17 19:42:30 +00:00
|
|
|
ProgressDescription progress,
|
2012-08-03 03:23:41 +00:00
|
|
|
long theirThreadId, long ourThreadId)
|
|
|
|
{
|
2011-12-20 18:20:44 +00:00
|
|
|
SmsDatabase ourSmsDatabase = DatabaseFactory.getSmsDatabase(context);
|
|
|
|
Cursor cursor = null;
|
Close SQL statement, preventing finalizer crashes
This will stop instances of the following from occuring in the logs
on SMS migration:
W/SQLiteCompiledSql: Releasing statement in a finalizer. Please ensure
that you explicitly call close() on your cursor: INSERT INTO sms
(address, person, date_sent, date, protocol, read, status, type,
reply_path_present,
net.sqlcipher.database.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
at net.sqlcipher.database.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:62)
at net.sqlcipher.database.SQLiteProgram.<init>(SQLiteProgram.java:109)
at net.sqlcipher.database.SQLiteStatement.<init>(SQLiteStatement.java:39)
at net.sqlcipher.database.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1647)
at org.thoughtcrime.securesms.database.SmsDatabase.createInsertStatement(SmsDatabase.java:767)
at org.thoughtcrime.securesms.database.SmsMigrator.migrateConversation(SmsMigrator.java:166)
at org.thoughtcrime.securesms.database.SmsMigrator.migrateDatabase(SmsMigrator.java:210)
at org.thoughtcrime.securesms.service.ApplicationMigrationService$ImportRunnable.run(ApplicationMigrationService.java:159)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
We aren't closing Statement objects before the finalizer on those
objects runs. When the GC runs, we'll get warnings like the above
which alert us to the fact that these objects are being automatically
closed for us in the finalizer, but that this is suboptimal behavior.
If we leave too many Statement (or Cursor) objects to be closed in
their finalizers, when the GC runs, it'll take longer than 10 seconds
to close them all and Android will kill the app. This 10 second limit
is hardcoded and we can only try to avoid it. A crash will look like:
java.util.concurrent.TimeoutException: net.sqlcipher.database.SQLiteCompiledSql.finalize() timed out after 10 seconds
at java.lang.Object.wait(Native Method)
at java.lang.Thread.parkFor$(Thread.java:1220)
at sun.misc.Unsafe.park(Unsafe.java:299)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:158)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:810)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireQueued(AbstractQueuedSynchronizer.java:844)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquire(AbstractQueuedSynchronizer.java:1173)
at java.util.concurrent.locks.ReentrantLock$FairSync.lock(ReentrantLock.java:196)
at java.util.concurrent.locks.ReentrantLock.lock(ReentrantLock.java:257)
at net.sqlcipher.database.SQLiteDatabase.lock(SQLiteDatabase.java:553)
at net.sqlcipher.database.SQLiteCompiledSql.releaseSqlStatement(SQLiteCompiledSql.java:106)
at net.sqlcipher.database.SQLiteCompiledSql.finalize(SQLiteCompiledSql.java:152)
at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:202)
at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:185)
at java.lang.Thread.run(Thread.java:818)
I was able to replicate the above crash consistently on a
Samsung Galaxy S7 edge when importing well over 100k SMS messages.
But as soon as I attached a debugger the crash did not persist. I
assume this is because of some VM-level interactions between the two
and did not investigate further after fixing it.
I do not have access to the stack trace for issue #7953 but this
could potentially resolve it. The crash is identical to that in #7477
but this patch is for SMS migration not restoring from a backup. I
was not able to replicate the crash on restoring a >100k message
backup.
2018-10-07 11:11:15 +00:00
|
|
|
SQLiteStatement statement = null;
|
2012-08-03 03:23:41 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
try {
|
2015-10-26 19:33:45 +00:00
|
|
|
Uri uri = Uri.parse("content://sms/conversations/" + theirThreadId);
|
|
|
|
|
|
|
|
try {
|
|
|
|
cursor = context.getContentResolver().query(uri, null, null, null, null);
|
|
|
|
} catch (SQLiteException e) {
|
|
|
|
/// Work around for weird sony-specific (?) bug: #4309
|
|
|
|
Log.w(TAG, e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
SQLiteDatabase transaction = ourSmsDatabase.beginTransaction();
|
Close SQL statement, preventing finalizer crashes
This will stop instances of the following from occuring in the logs
on SMS migration:
W/SQLiteCompiledSql: Releasing statement in a finalizer. Please ensure
that you explicitly call close() on your cursor: INSERT INTO sms
(address, person, date_sent, date, protocol, read, status, type,
reply_path_present,
net.sqlcipher.database.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
at net.sqlcipher.database.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:62)
at net.sqlcipher.database.SQLiteProgram.<init>(SQLiteProgram.java:109)
at net.sqlcipher.database.SQLiteStatement.<init>(SQLiteStatement.java:39)
at net.sqlcipher.database.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1647)
at org.thoughtcrime.securesms.database.SmsDatabase.createInsertStatement(SmsDatabase.java:767)
at org.thoughtcrime.securesms.database.SmsMigrator.migrateConversation(SmsMigrator.java:166)
at org.thoughtcrime.securesms.database.SmsMigrator.migrateDatabase(SmsMigrator.java:210)
at org.thoughtcrime.securesms.service.ApplicationMigrationService$ImportRunnable.run(ApplicationMigrationService.java:159)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
We aren't closing Statement objects before the finalizer on those
objects runs. When the GC runs, we'll get warnings like the above
which alert us to the fact that these objects are being automatically
closed for us in the finalizer, but that this is suboptimal behavior.
If we leave too many Statement (or Cursor) objects to be closed in
their finalizers, when the GC runs, it'll take longer than 10 seconds
to close them all and Android will kill the app. This 10 second limit
is hardcoded and we can only try to avoid it. A crash will look like:
java.util.concurrent.TimeoutException: net.sqlcipher.database.SQLiteCompiledSql.finalize() timed out after 10 seconds
at java.lang.Object.wait(Native Method)
at java.lang.Thread.parkFor$(Thread.java:1220)
at sun.misc.Unsafe.park(Unsafe.java:299)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:158)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:810)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireQueued(AbstractQueuedSynchronizer.java:844)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquire(AbstractQueuedSynchronizer.java:1173)
at java.util.concurrent.locks.ReentrantLock$FairSync.lock(ReentrantLock.java:196)
at java.util.concurrent.locks.ReentrantLock.lock(ReentrantLock.java:257)
at net.sqlcipher.database.SQLiteDatabase.lock(SQLiteDatabase.java:553)
at net.sqlcipher.database.SQLiteCompiledSql.releaseSqlStatement(SQLiteCompiledSql.java:106)
at net.sqlcipher.database.SQLiteCompiledSql.finalize(SQLiteCompiledSql.java:152)
at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:202)
at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:185)
at java.lang.Thread.run(Thread.java:818)
I was able to replicate the above crash consistently on a
Samsung Galaxy S7 edge when importing well over 100k SMS messages.
But as soon as I attached a debugger the crash did not persist. I
assume this is because of some VM-level interactions between the two
and did not investigate further after fixing it.
I do not have access to the stack trace for issue #7953 but this
could potentially resolve it. The crash is identical to that in #7477
but this patch is for SMS migration not restoring from a backup. I
was not able to replicate the crash on restoring a >100k message
backup.
2018-10-07 11:11:15 +00:00
|
|
|
statement = ourSmsDatabase.createInsertStatement(transaction);
|
2012-08-03 03:23:41 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
while (cursor != null && cursor.moveToNext()) {
|
2014-12-13 03:35:52 +00:00
|
|
|
int typeColumn = cursor.getColumnIndex(SmsDatabase.TYPE);
|
|
|
|
|
|
|
|
if (cursor.isNull(typeColumn) || isAppropriateTypeForMigration(cursor, typeColumn)) {
|
2018-01-25 03:17:44 +00:00
|
|
|
getContentValuesForRow(context, cursor, ourThreadId, statement);
|
2014-12-13 03:35:52 +00:00
|
|
|
statement.execute();
|
|
|
|
}
|
2012-08-03 03:23:41 +00:00
|
|
|
|
2013-02-17 19:42:30 +00:00
|
|
|
listener.progressUpdate(new ProgressDescription(progress, cursor.getCount(), cursor.getPosition()));
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-08-03 03:23:41 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
ourSmsDatabase.endTransaction(transaction);
|
2015-11-23 23:07:41 +00:00
|
|
|
DatabaseFactory.getThreadDatabase(context).update(ourThreadId, true);
|
2011-12-20 18:20:44 +00:00
|
|
|
DatabaseFactory.getThreadDatabase(context).notifyConversationListeners(ourThreadId);
|
|
|
|
|
|
|
|
} finally {
|
Close SQL statement, preventing finalizer crashes
This will stop instances of the following from occuring in the logs
on SMS migration:
W/SQLiteCompiledSql: Releasing statement in a finalizer. Please ensure
that you explicitly call close() on your cursor: INSERT INTO sms
(address, person, date_sent, date, protocol, read, status, type,
reply_path_present,
net.sqlcipher.database.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
at net.sqlcipher.database.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:62)
at net.sqlcipher.database.SQLiteProgram.<init>(SQLiteProgram.java:109)
at net.sqlcipher.database.SQLiteStatement.<init>(SQLiteStatement.java:39)
at net.sqlcipher.database.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1647)
at org.thoughtcrime.securesms.database.SmsDatabase.createInsertStatement(SmsDatabase.java:767)
at org.thoughtcrime.securesms.database.SmsMigrator.migrateConversation(SmsMigrator.java:166)
at org.thoughtcrime.securesms.database.SmsMigrator.migrateDatabase(SmsMigrator.java:210)
at org.thoughtcrime.securesms.service.ApplicationMigrationService$ImportRunnable.run(ApplicationMigrationService.java:159)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
We aren't closing Statement objects before the finalizer on those
objects runs. When the GC runs, we'll get warnings like the above
which alert us to the fact that these objects are being automatically
closed for us in the finalizer, but that this is suboptimal behavior.
If we leave too many Statement (or Cursor) objects to be closed in
their finalizers, when the GC runs, it'll take longer than 10 seconds
to close them all and Android will kill the app. This 10 second limit
is hardcoded and we can only try to avoid it. A crash will look like:
java.util.concurrent.TimeoutException: net.sqlcipher.database.SQLiteCompiledSql.finalize() timed out after 10 seconds
at java.lang.Object.wait(Native Method)
at java.lang.Thread.parkFor$(Thread.java:1220)
at sun.misc.Unsafe.park(Unsafe.java:299)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:158)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:810)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireQueued(AbstractQueuedSynchronizer.java:844)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquire(AbstractQueuedSynchronizer.java:1173)
at java.util.concurrent.locks.ReentrantLock$FairSync.lock(ReentrantLock.java:196)
at java.util.concurrent.locks.ReentrantLock.lock(ReentrantLock.java:257)
at net.sqlcipher.database.SQLiteDatabase.lock(SQLiteDatabase.java:553)
at net.sqlcipher.database.SQLiteCompiledSql.releaseSqlStatement(SQLiteCompiledSql.java:106)
at net.sqlcipher.database.SQLiteCompiledSql.finalize(SQLiteCompiledSql.java:152)
at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:202)
at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:185)
at java.lang.Thread.run(Thread.java:818)
I was able to replicate the above crash consistently on a
Samsung Galaxy S7 edge when importing well over 100k SMS messages.
But as soon as I attached a debugger the crash did not persist. I
assume this is because of some VM-level interactions between the two
and did not investigate further after fixing it.
I do not have access to the stack trace for issue #7953 but this
could potentially resolve it. The crash is identical to that in #7477
but this patch is for SMS migration not restoring from a backup. I
was not able to replicate the crash on restoring a >100k message
backup.
2018-10-07 11:11:15 +00:00
|
|
|
if (statement != null)
|
|
|
|
statement.close();
|
2011-12-20 18:20:44 +00:00
|
|
|
if (cursor != null)
|
2012-08-03 03:23:41 +00:00
|
|
|
cursor.close();
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
}
|
2012-08-03 03:23:41 +00:00
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
public static void migrateDatabase(Context context, SmsMigrationProgressListener listener)
|
2012-08-03 03:23:41 +00:00
|
|
|
{
|
2013-06-25 04:02:30 +00:00
|
|
|
// if (context.getSharedPreferences("SecureSMS", Context.MODE_PRIVATE).getBoolean("migrated", false))
|
|
|
|
// return;
|
2012-08-03 03:23:41 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
ThreadDatabase threadDatabase = DatabaseFactory.getThreadDatabase(context);
|
2012-08-03 03:23:41 +00:00
|
|
|
Cursor cursor = null;
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
try {
|
|
|
|
Uri threadListUri = Uri.parse("content://mms-sms/conversations?simple=true");
|
|
|
|
cursor = context.getContentResolver().query(threadListUri, null, null, null, "date ASC");
|
|
|
|
|
|
|
|
while (cursor != null && cursor.moveToNext()) {
|
2017-08-01 15:56:00 +00:00
|
|
|
long theirThreadId = cursor.getLong(cursor.getColumnIndexOrThrow("_id"));
|
|
|
|
String theirRecipients = cursor.getString(cursor.getColumnIndexOrThrow("recipient_ids"));
|
|
|
|
Set<Recipient> ourRecipients = getOurRecipients(context, theirRecipients);
|
|
|
|
ProgressDescription progress = new ProgressDescription(cursor.getCount(), cursor.getPosition(), 100, 0);
|
2012-08-03 03:23:41 +00:00
|
|
|
|
|
|
|
if (ourRecipients != null) {
|
2017-08-01 15:56:00 +00:00
|
|
|
if (ourRecipients.size() == 1) {
|
|
|
|
long ourThreadId = threadDatabase.getThreadIdFor(ourRecipients.iterator().next());
|
2018-01-25 03:17:44 +00:00
|
|
|
migrateConversation(context, listener, progress, theirThreadId, ourThreadId);
|
2017-08-01 15:56:00 +00:00
|
|
|
} else if (ourRecipients.size() > 1) {
|
2017-08-22 01:32:38 +00:00
|
|
|
ourRecipients.add(Recipient.from(context, Address.fromSerialized(TextSecurePreferences.getLocalNumber(context)), true));
|
2017-08-01 15:56:00 +00:00
|
|
|
|
|
|
|
List<Address> memberAddresses = new LinkedList<>();
|
|
|
|
|
|
|
|
for (Recipient recipient : ourRecipients) {
|
|
|
|
memberAddresses.add(recipient.getAddress());
|
|
|
|
}
|
|
|
|
|
|
|
|
String ourGroupId = DatabaseFactory.getGroupDatabase(context).getOrCreateGroupForMembers(memberAddresses, true);
|
2017-08-22 01:32:38 +00:00
|
|
|
Recipient ourGroupRecipient = Recipient.from(context, Address.fromSerialized(ourGroupId), true);
|
2017-08-01 15:56:00 +00:00
|
|
|
long ourThreadId = threadDatabase.getThreadIdFor(ourGroupRecipient, ThreadDatabase.DistributionTypes.CONVERSATION);
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
migrateConversation(context, listener, progress, theirThreadId, ourThreadId);
|
2017-08-01 15:56:00 +00:00
|
|
|
}
|
2012-08-03 03:23:41 +00:00
|
|
|
}
|
|
|
|
|
2013-02-17 19:42:30 +00:00
|
|
|
progress.incrementPrimaryComplete();
|
|
|
|
listener.progressUpdate(progress);
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (cursor != null)
|
2012-08-03 03:23:41 +00:00
|
|
|
cursor.close();
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-08-03 03:23:41 +00:00
|
|
|
|
|
|
|
context.getSharedPreferences("SecureSMS", Context.MODE_PRIVATE).edit()
|
2014-05-05 18:48:26 +00:00
|
|
|
.putBoolean("migrated", true).apply();
|
2012-08-03 03:23:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public interface SmsMigrationProgressListener {
|
2018-01-25 03:17:44 +00:00
|
|
|
void progressUpdate(ProgressDescription description);
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2013-02-17 19:42:30 +00:00
|
|
|
|
|
|
|
public static class ProgressDescription {
|
|
|
|
public final int primaryTotal;
|
|
|
|
public int primaryComplete;
|
|
|
|
public final int secondaryTotal;
|
|
|
|
public final int secondaryComplete;
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
ProgressDescription(int primaryTotal, int primaryComplete,
|
|
|
|
int secondaryTotal, int secondaryComplete)
|
2013-02-17 19:42:30 +00:00
|
|
|
{
|
|
|
|
this.primaryTotal = primaryTotal;
|
|
|
|
this.primaryComplete = primaryComplete;
|
|
|
|
this.secondaryTotal = secondaryTotal;
|
|
|
|
this.secondaryComplete = secondaryComplete;
|
|
|
|
}
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
ProgressDescription(ProgressDescription that, int secondaryTotal, int secondaryComplete) {
|
2013-02-17 19:42:30 +00:00
|
|
|
this.primaryComplete = that.primaryComplete;
|
|
|
|
this.primaryTotal = that.primaryTotal;
|
|
|
|
this.secondaryComplete = secondaryComplete;
|
|
|
|
this.secondaryTotal = secondaryTotal;
|
|
|
|
}
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
void incrementPrimaryComplete() {
|
2013-02-17 19:42:30 +00:00
|
|
|
primaryComplete += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|