2018-01-25 03:17:44 +00:00
|
|
|
/*
|
2015-10-13 01:25:05 +00:00
|
|
|
* Copyright (C) 2011 Whisper Systems
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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;
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
import android.annotation.SuppressLint;
|
2015-10-13 01:25:05 +00:00
|
|
|
import android.content.ContentValues;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.database.Cursor;
|
2016-12-11 21:37:27 +00:00
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.media.MediaMetadataRetriever;
|
2015-10-13 01:25:05 +00:00
|
|
|
import android.net.Uri;
|
2016-12-11 21:37:27 +00:00
|
|
|
import android.os.Build;
|
2015-10-13 01:25:05 +00:00
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.support.annotation.Nullable;
|
2016-11-03 16:36:45 +00:00
|
|
|
import android.support.annotation.VisibleForTesting;
|
2015-10-13 01:25:05 +00:00
|
|
|
import android.text.TextUtils;
|
2018-08-01 15:09:24 +00:00
|
|
|
import org.thoughtcrime.securesms.logging.Log;
|
2018-02-01 21:30:33 +00:00
|
|
|
import android.util.Pair;
|
2015-10-13 01:25:05 +00:00
|
|
|
|
2018-09-17 22:08:08 +00:00
|
|
|
import com.bumptech.glide.Glide;
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
import net.sqlcipher.database.SQLiteDatabase;
|
|
|
|
|
2018-02-07 22:01:37 +00:00
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONException;
|
2015-10-13 01:25:05 +00:00
|
|
|
import org.thoughtcrime.securesms.attachments.Attachment;
|
|
|
|
import org.thoughtcrime.securesms.attachments.AttachmentId;
|
|
|
|
import org.thoughtcrime.securesms.attachments.DatabaseAttachment;
|
2018-01-25 03:17:44 +00:00
|
|
|
import org.thoughtcrime.securesms.crypto.AttachmentSecret;
|
|
|
|
import org.thoughtcrime.securesms.crypto.ClassicDecryptingPartInputStream;
|
|
|
|
import org.thoughtcrime.securesms.crypto.ModernDecryptingPartInputStream;
|
|
|
|
import org.thoughtcrime.securesms.crypto.ModernEncryptingPartOutputStream;
|
|
|
|
import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper;
|
2015-11-23 04:18:54 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.MediaStream;
|
2017-05-08 22:32:59 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.MmsException;
|
2015-10-13 01:25:05 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.PartAuthority;
|
2018-04-02 23:17:32 +00:00
|
|
|
import org.thoughtcrime.securesms.util.BitmapDecodingException;
|
|
|
|
import org.thoughtcrime.securesms.util.BitmapUtil;
|
2018-02-07 22:01:37 +00:00
|
|
|
import org.thoughtcrime.securesms.util.JsonUtils;
|
2015-10-13 01:25:05 +00:00
|
|
|
import org.thoughtcrime.securesms.util.MediaUtil;
|
|
|
|
import org.thoughtcrime.securesms.util.MediaUtil.ThumbnailData;
|
2018-02-16 19:13:20 +00:00
|
|
|
import org.thoughtcrime.securesms.util.StorageUtil;
|
2015-10-13 01:25:05 +00:00
|
|
|
import org.thoughtcrime.securesms.util.Util;
|
2016-12-11 21:37:27 +00:00
|
|
|
import org.thoughtcrime.securesms.video.EncryptedMediaDataSource;
|
2015-10-13 01:25:05 +00:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
2018-02-07 22:01:37 +00:00
|
|
|
import java.util.Collections;
|
2018-04-27 00:03:54 +00:00
|
|
|
import java.util.HashMap;
|
2015-10-13 01:25:05 +00:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
2018-04-27 00:03:54 +00:00
|
|
|
import java.util.Map;
|
2015-10-13 01:25:05 +00:00
|
|
|
import java.util.concurrent.Callable;
|
|
|
|
import java.util.concurrent.ExecutionException;
|
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
|
|
|
|
public class AttachmentDatabase extends Database {
|
|
|
|
|
|
|
|
private static final String TAG = AttachmentDatabase.class.getSimpleName();
|
|
|
|
|
2018-02-26 17:58:18 +00:00
|
|
|
public static final String TABLE_NAME = "part";
|
|
|
|
public static final String ROW_ID = "_id";
|
2018-02-07 22:01:37 +00:00
|
|
|
static final String ATTACHMENT_JSON_ALIAS = "attachment_json";
|
2018-06-21 23:48:46 +00:00
|
|
|
public static final String MMS_ID = "mid";
|
2015-10-13 01:25:05 +00:00
|
|
|
static final String CONTENT_TYPE = "ct";
|
2015-10-21 22:32:19 +00:00
|
|
|
static final String NAME = "name";
|
|
|
|
static final String CONTENT_DISPOSITION = "cd";
|
|
|
|
static final String CONTENT_LOCATION = "cl";
|
2018-02-26 17:58:18 +00:00
|
|
|
public static final String DATA = "_data";
|
2015-10-13 01:25:05 +00:00
|
|
|
static final String TRANSFER_STATE = "pending_push";
|
2018-02-26 17:58:18 +00:00
|
|
|
public static final String SIZE = "data_size";
|
2017-03-28 19:05:30 +00:00
|
|
|
static final String FILE_NAME = "file_name";
|
2018-02-26 17:58:18 +00:00
|
|
|
public static final String THUMBNAIL = "thumbnail";
|
2015-10-13 01:25:05 +00:00
|
|
|
static final String THUMBNAIL_ASPECT_RATIO = "aspect_ratio";
|
2018-01-04 19:11:49 +00:00
|
|
|
public static final String UNIQUE_ID = "unique_id";
|
2017-02-26 18:06:27 +00:00
|
|
|
static final String DIGEST = "digest";
|
2017-05-12 05:46:35 +00:00
|
|
|
static final String VOICE_NOTE = "voice_note";
|
2018-02-07 22:01:37 +00:00
|
|
|
static final String QUOTE = "quote";
|
|
|
|
static final String FAST_PREFLIGHT_ID = "fast_preflight_id";
|
2018-02-26 17:58:18 +00:00
|
|
|
public static final String DATA_RANDOM = "data_random";
|
2018-02-01 21:30:33 +00:00
|
|
|
private static final String THUMBNAIL_RANDOM = "thumbnail_random";
|
2018-03-18 23:04:33 +00:00
|
|
|
static final String WIDTH = "width";
|
|
|
|
static final String HEIGHT = "height";
|
2015-10-13 01:25:05 +00:00
|
|
|
|
2018-02-26 17:58:18 +00:00
|
|
|
public static final String DIRECTORY = "parts";
|
|
|
|
|
2017-07-21 22:59:27 +00:00
|
|
|
public static final int TRANSFER_PROGRESS_DONE = 0;
|
|
|
|
public static final int TRANSFER_PROGRESS_STARTED = 1;
|
|
|
|
public static final int TRANSFER_PROGRESS_PENDING = 2;
|
|
|
|
public static final int TRANSFER_PROGRESS_FAILED = 3;
|
2015-10-13 01:25:05 +00:00
|
|
|
|
|
|
|
private static final String PART_ID_WHERE = ROW_ID + " = ? AND " + UNIQUE_ID + " = ?";
|
|
|
|
|
2018-02-07 22:01:37 +00:00
|
|
|
private static final String[] PROJECTION = new String[] {ROW_ID,
|
2015-10-21 22:32:19 +00:00
|
|
|
MMS_ID, CONTENT_TYPE, NAME, CONTENT_DISPOSITION,
|
2016-12-11 21:37:27 +00:00
|
|
|
CONTENT_LOCATION, DATA, THUMBNAIL, TRANSFER_STATE,
|
2017-03-28 19:05:30 +00:00
|
|
|
SIZE, FILE_NAME, THUMBNAIL, THUMBNAIL_ASPECT_RATIO,
|
2018-01-25 03:17:44 +00:00
|
|
|
UNIQUE_ID, DIGEST, FAST_PREFLIGHT_ID, VOICE_NOTE,
|
2018-02-07 22:01:37 +00:00
|
|
|
QUOTE, DATA_RANDOM, THUMBNAIL_RANDOM, WIDTH, HEIGHT};
|
2015-10-21 22:32:19 +00:00
|
|
|
|
2015-10-13 01:25:05 +00:00
|
|
|
public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" + ROW_ID + " INTEGER PRIMARY KEY, " +
|
|
|
|
MMS_ID + " INTEGER, " + "seq" + " INTEGER DEFAULT 0, " +
|
|
|
|
CONTENT_TYPE + " TEXT, " + NAME + " TEXT, " + "chset" + " INTEGER, " +
|
|
|
|
CONTENT_DISPOSITION + " TEXT, " + "fn" + " TEXT, " + "cid" + " TEXT, " +
|
|
|
|
CONTENT_LOCATION + " TEXT, " + "ctt_s" + " INTEGER, " +
|
|
|
|
"ctt_t" + " TEXT, " + "encrypted" + " INTEGER, " +
|
|
|
|
TRANSFER_STATE + " INTEGER, "+ DATA + " TEXT, " + SIZE + " INTEGER, " +
|
2017-03-28 19:05:30 +00:00
|
|
|
FILE_NAME + " TEXT, " + THUMBNAIL + " TEXT, " + THUMBNAIL_ASPECT_RATIO + " REAL, " +
|
2017-05-12 05:46:35 +00:00
|
|
|
UNIQUE_ID + " INTEGER NOT NULL, " + DIGEST + " BLOB, " + FAST_PREFLIGHT_ID + " TEXT, " +
|
2018-03-18 23:04:33 +00:00
|
|
|
VOICE_NOTE + " INTEGER DEFAULT 0, " + DATA_RANDOM + " BLOB, " + THUMBNAIL_RANDOM + " BLOB, " +
|
2018-02-07 22:01:37 +00:00
|
|
|
QUOTE + " INTEGER DEFAULT 0, " + WIDTH + " INTEGER DEFAULT 0, " + HEIGHT + " INTEGER DEFAULT 0);";
|
2015-10-13 01:25:05 +00:00
|
|
|
|
|
|
|
public static final String[] CREATE_INDEXS = {
|
|
|
|
"CREATE INDEX IF NOT EXISTS part_mms_id_index ON " + TABLE_NAME + " (" + MMS_ID + ");",
|
|
|
|
"CREATE INDEX IF NOT EXISTS pending_push_index ON " + TABLE_NAME + " (" + TRANSFER_STATE + ");",
|
|
|
|
};
|
|
|
|
|
|
|
|
private final ExecutorService thumbnailExecutor = Util.newSingleThreadedLifoExecutor();
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
private final AttachmentSecret attachmentSecret;
|
|
|
|
|
|
|
|
public AttachmentDatabase(Context context, SQLCipherOpenHelper databaseHelper, AttachmentSecret attachmentSecret) {
|
2015-10-13 01:25:05 +00:00
|
|
|
super(context, databaseHelper);
|
2018-01-25 03:17:44 +00:00
|
|
|
this.attachmentSecret = attachmentSecret;
|
2015-10-13 01:25:05 +00:00
|
|
|
}
|
|
|
|
|
2018-02-24 19:09:26 +00:00
|
|
|
public @NonNull InputStream getAttachmentStream(AttachmentId attachmentId, long offset)
|
2015-10-13 01:25:05 +00:00
|
|
|
throws IOException
|
|
|
|
{
|
2018-02-24 19:09:26 +00:00
|
|
|
InputStream dataStream = getDataStream(attachmentId, DATA, offset);
|
2015-10-13 01:25:05 +00:00
|
|
|
|
|
|
|
if (dataStream == null) throw new IOException("No stream for: " + attachmentId);
|
|
|
|
else return dataStream;
|
|
|
|
}
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
public @NonNull InputStream getThumbnailStream(@NonNull AttachmentId attachmentId)
|
2015-10-13 01:25:05 +00:00
|
|
|
throws IOException
|
|
|
|
{
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.d(TAG, "getThumbnailStream(" + attachmentId + ")");
|
2018-02-24 19:09:26 +00:00
|
|
|
InputStream dataStream = getDataStream(attachmentId, THUMBNAIL, 0);
|
2015-10-13 01:25:05 +00:00
|
|
|
|
|
|
|
if (dataStream != null) {
|
|
|
|
return dataStream;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2018-01-25 03:17:44 +00:00
|
|
|
InputStream generatedStream = thumbnailExecutor.submit(new ThumbnailFetchCallable(attachmentId)).get();
|
2015-10-13 01:25:05 +00:00
|
|
|
|
2015-11-27 22:45:07 +00:00
|
|
|
if (generatedStream == null) throw new FileNotFoundException("No thumbnail stream available: " + attachmentId);
|
2015-10-13 01:25:05 +00:00
|
|
|
else return generatedStream;
|
|
|
|
} catch (InterruptedException ie) {
|
|
|
|
throw new AssertionError("interrupted");
|
|
|
|
} catch (ExecutionException ee) {
|
|
|
|
Log.w(TAG, ee);
|
|
|
|
throw new IOException(ee);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setTransferProgressFailed(AttachmentId attachmentId, long mmsId)
|
|
|
|
throws MmsException
|
|
|
|
{
|
|
|
|
SQLiteDatabase database = databaseHelper.getWritableDatabase();
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
values.put(TRANSFER_STATE, TRANSFER_PROGRESS_FAILED);
|
|
|
|
|
|
|
|
database.update(TABLE_NAME, values, PART_ID_WHERE, attachmentId.toStrings());
|
|
|
|
notifyConversationListeners(DatabaseFactory.getMmsDatabase(context).getThreadIdForMessage(mmsId));
|
|
|
|
}
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
public @Nullable DatabaseAttachment getAttachment(@NonNull AttachmentId attachmentId)
|
2017-03-28 19:05:30 +00:00
|
|
|
{
|
2015-10-13 01:25:05 +00:00
|
|
|
SQLiteDatabase database = databaseHelper.getReadableDatabase();
|
|
|
|
Cursor cursor = null;
|
|
|
|
|
|
|
|
try {
|
2015-10-21 22:32:19 +00:00
|
|
|
cursor = database.query(TABLE_NAME, PROJECTION, PART_ID_WHERE, attachmentId.toStrings(), null, null, null);
|
2015-10-13 01:25:05 +00:00
|
|
|
|
2018-02-07 22:01:37 +00:00
|
|
|
if (cursor != null && cursor.moveToFirst()) {
|
|
|
|
List<DatabaseAttachment> list = getAttachment(cursor);
|
|
|
|
|
|
|
|
if (list != null && list.size() > 0) {
|
|
|
|
return list.get(0);
|
|
|
|
}
|
|
|
|
}
|
2015-10-13 01:25:05 +00:00
|
|
|
|
2018-02-07 22:01:37 +00:00
|
|
|
return null;
|
2015-10-13 01:25:05 +00:00
|
|
|
} finally {
|
|
|
|
if (cursor != null)
|
|
|
|
cursor.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
public @NonNull List<DatabaseAttachment> getAttachmentsForMessage(long mmsId) {
|
2015-10-13 01:25:05 +00:00
|
|
|
SQLiteDatabase database = databaseHelper.getReadableDatabase();
|
|
|
|
List<DatabaseAttachment> results = new LinkedList<>();
|
|
|
|
Cursor cursor = null;
|
|
|
|
|
|
|
|
try {
|
2015-10-21 22:32:19 +00:00
|
|
|
cursor = database.query(TABLE_NAME, PROJECTION, MMS_ID + " = ?", new String[] {mmsId+""},
|
2015-10-13 01:25:05 +00:00
|
|
|
null, null, null);
|
|
|
|
|
|
|
|
while (cursor != null && cursor.moveToNext()) {
|
2018-02-07 22:01:37 +00:00
|
|
|
results.addAll(getAttachment(cursor));
|
2015-10-13 01:25:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
} finally {
|
|
|
|
if (cursor != null)
|
|
|
|
cursor.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
public @NonNull List<DatabaseAttachment> getPendingAttachments() {
|
2015-10-13 01:25:05 +00:00
|
|
|
final SQLiteDatabase database = databaseHelper.getReadableDatabase();
|
|
|
|
final List<DatabaseAttachment> attachments = new LinkedList<>();
|
|
|
|
|
|
|
|
Cursor cursor = null;
|
|
|
|
try {
|
2015-10-21 22:32:19 +00:00
|
|
|
cursor = database.query(TABLE_NAME, PROJECTION, TRANSFER_STATE + " = ?", new String[] {String.valueOf(TRANSFER_PROGRESS_STARTED)}, null, null, null);
|
2015-10-13 01:25:05 +00:00
|
|
|
while (cursor != null && cursor.moveToNext()) {
|
2018-02-07 22:01:37 +00:00
|
|
|
attachments.addAll(getAttachment(cursor));
|
2015-10-13 01:25:05 +00:00
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (cursor != null) cursor.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
return attachments;
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("ResultOfMethodCallIgnored")
|
2018-01-25 03:17:44 +00:00
|
|
|
void deleteAttachmentsForMessage(long mmsId) {
|
2015-10-13 01:25:05 +00:00
|
|
|
SQLiteDatabase database = databaseHelper.getWritableDatabase();
|
|
|
|
Cursor cursor = null;
|
|
|
|
|
|
|
|
try {
|
2018-09-17 22:08:08 +00:00
|
|
|
cursor = database.query(TABLE_NAME, new String[] {DATA, THUMBNAIL, CONTENT_TYPE}, MMS_ID + " = ?",
|
2015-10-13 01:25:05 +00:00
|
|
|
new String[] {mmsId+""}, null, null, null);
|
|
|
|
|
|
|
|
while (cursor != null && cursor.moveToNext()) {
|
2018-09-17 22:08:08 +00:00
|
|
|
deleteAttachmentOnDisk(cursor.getString(0), cursor.getString(1), cursor.getString(2));
|
2015-10-13 01:25:05 +00:00
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (cursor != null)
|
|
|
|
cursor.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
database.delete(TABLE_NAME, MMS_ID + " = ?", new String[] {mmsId + ""});
|
2018-03-14 01:24:42 +00:00
|
|
|
notifyAttachmentListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void deleteAttachment(@NonNull AttachmentId id) {
|
|
|
|
SQLiteDatabase database = databaseHelper.getWritableDatabase();
|
|
|
|
|
|
|
|
try (Cursor cursor = database.query(TABLE_NAME,
|
2018-09-17 22:08:08 +00:00
|
|
|
new String[]{DATA, THUMBNAIL, CONTENT_TYPE},
|
2018-03-14 01:24:42 +00:00
|
|
|
PART_ID_WHERE,
|
|
|
|
id.toStrings(),
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null))
|
|
|
|
{
|
|
|
|
if (cursor == null || !cursor.moveToNext()) {
|
|
|
|
Log.w(TAG, "Tried to delete an attachment, but it didn't exist.");
|
|
|
|
return;
|
|
|
|
}
|
2018-09-17 22:08:08 +00:00
|
|
|
String data = cursor.getString(0);
|
|
|
|
String thumbnail = cursor.getString(1);
|
|
|
|
String contentType = cursor.getString(2);
|
2018-03-14 01:24:42 +00:00
|
|
|
|
|
|
|
database.delete(TABLE_NAME, PART_ID_WHERE, id.toStrings());
|
2018-09-17 22:08:08 +00:00
|
|
|
deleteAttachmentOnDisk(data, thumbnail, contentType);
|
2018-03-14 01:24:42 +00:00
|
|
|
notifyAttachmentListeners();
|
|
|
|
}
|
2015-10-13 01:25:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("ResultOfMethodCallIgnored")
|
2018-01-25 03:17:44 +00:00
|
|
|
void deleteAllAttachments() {
|
2015-10-13 01:25:05 +00:00
|
|
|
SQLiteDatabase database = databaseHelper.getWritableDatabase();
|
|
|
|
database.delete(TABLE_NAME, null, null);
|
|
|
|
|
2018-02-26 17:58:18 +00:00
|
|
|
File attachmentsDirectory = context.getDir(DIRECTORY, Context.MODE_PRIVATE);
|
2015-10-13 01:25:05 +00:00
|
|
|
File[] attachments = attachmentsDirectory.listFiles();
|
|
|
|
|
|
|
|
for (File attachment : attachments) {
|
|
|
|
attachment.delete();
|
|
|
|
}
|
2018-03-14 01:24:42 +00:00
|
|
|
|
|
|
|
notifyAttachmentListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("ResultOfMethodCallIgnored")
|
2018-09-17 22:08:08 +00:00
|
|
|
private void deleteAttachmentOnDisk(@Nullable String data, @Nullable String thumbnail, @Nullable String contentType) {
|
2018-03-14 01:24:42 +00:00
|
|
|
if (!TextUtils.isEmpty(data)) {
|
|
|
|
new File(data).delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!TextUtils.isEmpty(thumbnail)) {
|
|
|
|
new File(thumbnail).delete();
|
|
|
|
}
|
2018-09-17 22:08:08 +00:00
|
|
|
|
|
|
|
if (MediaUtil.isImageType(contentType) || thumbnail != null) {
|
|
|
|
Glide.get(context).clearDiskCache();
|
|
|
|
}
|
2015-10-13 01:25:05 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
public void insertAttachmentsForPlaceholder(long mmsId, @NonNull AttachmentId attachmentId, @NonNull InputStream inputStream)
|
2015-10-13 01:25:05 +00:00
|
|
|
throws MmsException
|
|
|
|
{
|
2018-04-02 23:17:32 +00:00
|
|
|
DatabaseAttachment placeholder = getAttachment(attachmentId);
|
|
|
|
SQLiteDatabase database = databaseHelper.getWritableDatabase();
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
DataInfo dataInfo = setAttachmentData(inputStream);
|
|
|
|
|
|
|
|
if (placeholder != null && placeholder.isQuote() && !placeholder.getContentType().startsWith("image")) {
|
|
|
|
values.put(THUMBNAIL, dataInfo.file.getAbsolutePath());
|
|
|
|
values.put(THUMBNAIL_RANDOM, dataInfo.random);
|
|
|
|
} else {
|
|
|
|
values.put(DATA, dataInfo.file.getAbsolutePath());
|
|
|
|
values.put(SIZE, dataInfo.length);
|
|
|
|
values.put(DATA_RANDOM, dataInfo.random);
|
|
|
|
}
|
2015-10-13 01:25:05 +00:00
|
|
|
|
|
|
|
values.put(TRANSFER_STATE, TRANSFER_PROGRESS_DONE);
|
|
|
|
values.put(CONTENT_LOCATION, (String)null);
|
|
|
|
values.put(CONTENT_DISPOSITION, (String)null);
|
2017-02-26 18:06:27 +00:00
|
|
|
values.put(DIGEST, (byte[])null);
|
2015-10-13 01:25:05 +00:00
|
|
|
values.put(NAME, (String) null);
|
2017-04-22 23:29:26 +00:00
|
|
|
values.put(FAST_PREFLIGHT_ID, (String)null);
|
2015-10-13 01:25:05 +00:00
|
|
|
|
|
|
|
if (database.update(TABLE_NAME, values, PART_ID_WHERE, attachmentId.toStrings()) == 0) {
|
|
|
|
//noinspection ResultOfMethodCallIgnored
|
2018-01-25 03:17:44 +00:00
|
|
|
dataInfo.file.delete();
|
2015-10-13 01:25:05 +00:00
|
|
|
} else {
|
|
|
|
notifyConversationListeners(DatabaseFactory.getMmsDatabase(context).getThreadIdForMessage(mmsId));
|
2015-10-16 20:59:40 +00:00
|
|
|
notifyConversationListListeners();
|
2015-10-13 01:25:05 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
thumbnailExecutor.submit(new ThumbnailFetchCallable(attachmentId));
|
2015-10-13 01:25:05 +00:00
|
|
|
}
|
|
|
|
|
2018-04-27 00:03:54 +00:00
|
|
|
@NonNull Map<Attachment, AttachmentId> insertAttachmentsForMessage(long mmsId, @NonNull List<Attachment> attachments, @NonNull List<Attachment> quoteAttachment)
|
2015-10-13 01:25:05 +00:00
|
|
|
throws MmsException
|
|
|
|
{
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.d(TAG, "insertParts(" + attachments.size() + ")");
|
2015-10-13 01:25:05 +00:00
|
|
|
|
2018-04-27 00:03:54 +00:00
|
|
|
Map<Attachment, AttachmentId> insertedAttachments = new HashMap<>();
|
|
|
|
|
2015-10-13 01:25:05 +00:00
|
|
|
for (Attachment attachment : attachments) {
|
2018-02-07 22:01:37 +00:00
|
|
|
AttachmentId attachmentId = insertAttachment(mmsId, attachment, attachment.isQuote());
|
2018-04-27 00:03:54 +00:00
|
|
|
insertedAttachments.put(attachment, attachmentId);
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.i(TAG, "Inserted attachment at ID: " + attachmentId);
|
2015-10-13 01:25:05 +00:00
|
|
|
}
|
2018-02-07 22:01:37 +00:00
|
|
|
|
|
|
|
for (Attachment attachment : quoteAttachment) {
|
2018-04-27 00:03:54 +00:00
|
|
|
AttachmentId attachmentId = insertAttachment(mmsId, attachment, true);
|
|
|
|
insertedAttachments.put(attachment, attachmentId);
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.i(TAG, "Inserted quoted attachment at ID: " + attachmentId);
|
2018-02-07 22:01:37 +00:00
|
|
|
}
|
2018-04-27 00:03:54 +00:00
|
|
|
|
|
|
|
return insertedAttachments;
|
2015-10-13 01:25:05 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
public @NonNull Attachment updateAttachmentData(@NonNull Attachment attachment,
|
2015-11-23 04:18:54 +00:00
|
|
|
@NonNull MediaStream mediaStream)
|
2015-10-13 01:25:05 +00:00
|
|
|
throws MmsException
|
|
|
|
{
|
|
|
|
SQLiteDatabase database = databaseHelper.getWritableDatabase();
|
|
|
|
DatabaseAttachment databaseAttachment = (DatabaseAttachment) attachment;
|
2018-01-25 03:17:44 +00:00
|
|
|
DataInfo dataInfo = getAttachmentDataFileInfo(databaseAttachment.getAttachmentId(), DATA);
|
2015-10-13 01:25:05 +00:00
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
if (dataInfo == null) {
|
2015-10-13 01:25:05 +00:00
|
|
|
throw new MmsException("No attachment data found!");
|
|
|
|
}
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
dataInfo = setAttachmentData(dataInfo.file, mediaStream.getStream());
|
2015-10-13 01:25:05 +00:00
|
|
|
|
|
|
|
ContentValues contentValues = new ContentValues();
|
2018-01-25 03:17:44 +00:00
|
|
|
contentValues.put(SIZE, dataInfo.length);
|
2015-11-23 04:18:54 +00:00
|
|
|
contentValues.put(CONTENT_TYPE, mediaStream.getMimeType());
|
2018-03-18 23:04:33 +00:00
|
|
|
contentValues.put(WIDTH, mediaStream.getWidth());
|
|
|
|
contentValues.put(HEIGHT, mediaStream.getHeight());
|
2018-01-25 03:17:44 +00:00
|
|
|
contentValues.put(DATA_RANDOM, dataInfo.random);
|
2015-10-13 01:25:05 +00:00
|
|
|
|
|
|
|
database.update(TABLE_NAME, contentValues, PART_ID_WHERE, databaseAttachment.getAttachmentId().toStrings());
|
|
|
|
|
|
|
|
return new DatabaseAttachment(databaseAttachment.getAttachmentId(),
|
|
|
|
databaseAttachment.getMmsId(),
|
|
|
|
databaseAttachment.hasData(),
|
2016-12-11 21:37:27 +00:00
|
|
|
databaseAttachment.hasThumbnail(),
|
2015-11-23 04:18:54 +00:00
|
|
|
mediaStream.getMimeType(),
|
2015-10-13 01:25:05 +00:00
|
|
|
databaseAttachment.getTransferState(),
|
2018-01-25 03:17:44 +00:00
|
|
|
dataInfo.length,
|
2017-03-28 19:05:30 +00:00
|
|
|
databaseAttachment.getFileName(),
|
2015-11-23 04:18:54 +00:00
|
|
|
databaseAttachment.getLocation(),
|
2015-10-13 01:25:05 +00:00
|
|
|
databaseAttachment.getKey(),
|
2017-02-26 18:06:27 +00:00
|
|
|
databaseAttachment.getRelay(),
|
2017-04-22 23:29:26 +00:00
|
|
|
databaseAttachment.getDigest(),
|
2017-05-12 05:46:35 +00:00
|
|
|
databaseAttachment.getFastPreflightId(),
|
2018-03-18 23:04:33 +00:00
|
|
|
databaseAttachment.isVoiceNote(),
|
|
|
|
mediaStream.getWidth(),
|
2018-02-07 22:01:37 +00:00
|
|
|
mediaStream.getHeight(),
|
|
|
|
databaseAttachment.isQuote());
|
2015-10-13 01:25:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
public void updateAttachmentFileName(@NonNull AttachmentId attachmentId,
|
2017-03-28 19:05:30 +00:00
|
|
|
@Nullable String fileName)
|
|
|
|
{
|
|
|
|
SQLiteDatabase database = databaseHelper.getWritableDatabase();
|
|
|
|
|
|
|
|
ContentValues contentValues = new ContentValues(1);
|
2018-02-16 19:13:20 +00:00
|
|
|
contentValues.put(FILE_NAME, StorageUtil.getCleanFileName(fileName));
|
2017-03-28 19:05:30 +00:00
|
|
|
|
|
|
|
database.update(TABLE_NAME, contentValues, PART_ID_WHERE, attachmentId.toStrings());
|
|
|
|
}
|
|
|
|
|
2015-10-13 01:25:05 +00:00
|
|
|
public void markAttachmentUploaded(long messageId, Attachment attachment) {
|
|
|
|
ContentValues values = new ContentValues(1);
|
|
|
|
SQLiteDatabase database = databaseHelper.getWritableDatabase();
|
|
|
|
|
|
|
|
values.put(TRANSFER_STATE, TRANSFER_PROGRESS_DONE);
|
|
|
|
database.update(TABLE_NAME, values, PART_ID_WHERE, ((DatabaseAttachment)attachment).getAttachmentId().toStrings());
|
|
|
|
|
|
|
|
notifyConversationListeners(DatabaseFactory.getMmsDatabase(context).getThreadIdForMessage(messageId));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setTransferState(long messageId, @NonNull Attachment attachment, int transferState) {
|
|
|
|
if (!(attachment instanceof DatabaseAttachment)) {
|
|
|
|
throw new AssertionError("Attempt to update attachment that doesn't belong to DB!");
|
|
|
|
}
|
|
|
|
|
|
|
|
setTransferState(messageId, ((DatabaseAttachment) attachment).getAttachmentId(), transferState);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setTransferState(long messageId, @NonNull AttachmentId attachmentId, int transferState) {
|
|
|
|
final ContentValues values = new ContentValues(1);
|
|
|
|
final SQLiteDatabase database = databaseHelper.getWritableDatabase();
|
|
|
|
|
|
|
|
values.put(TRANSFER_STATE, transferState);
|
|
|
|
database.update(TABLE_NAME, values, PART_ID_WHERE, attachmentId.toStrings());
|
|
|
|
notifyConversationListeners(DatabaseFactory.getMmsDatabase(context).getThreadIdForMessage(messageId));
|
|
|
|
}
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
@SuppressWarnings("WeakerAccess")
|
2015-10-13 01:25:05 +00:00
|
|
|
@VisibleForTesting
|
2018-02-24 19:09:26 +00:00
|
|
|
protected @Nullable InputStream getDataStream(AttachmentId attachmentId, String dataType, long offset)
|
2015-10-13 01:25:05 +00:00
|
|
|
{
|
2018-01-25 03:17:44 +00:00
|
|
|
DataInfo dataInfo = getAttachmentDataFileInfo(attachmentId, dataType);
|
|
|
|
|
|
|
|
if (dataInfo == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2015-10-13 01:25:05 +00:00
|
|
|
|
|
|
|
try {
|
2018-01-25 03:17:44 +00:00
|
|
|
if (dataInfo.random != null && dataInfo.random.length == 32) {
|
2018-02-24 19:09:26 +00:00
|
|
|
return ModernDecryptingPartInputStream.createFor(attachmentSecret, dataInfo.random, dataInfo.file, offset);
|
2018-01-25 03:17:44 +00:00
|
|
|
} else {
|
2018-02-24 19:09:26 +00:00
|
|
|
InputStream stream = ClassicDecryptingPartInputStream.createFor(attachmentSecret, dataInfo.file);
|
|
|
|
long skipped = stream.skip(offset);
|
|
|
|
|
|
|
|
if (skipped != offset) {
|
|
|
|
Log.w(TAG, "Skip failed: " + skipped + " vs " + offset);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return stream;
|
2018-01-25 03:17:44 +00:00
|
|
|
}
|
2017-03-28 19:05:30 +00:00
|
|
|
} catch (IOException e) {
|
2015-10-13 01:25:05 +00:00
|
|
|
Log.w(TAG, e);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
private @Nullable DataInfo getAttachmentDataFileInfo(@NonNull AttachmentId attachmentId, @NonNull String dataType)
|
2015-10-13 01:25:05 +00:00
|
|
|
{
|
|
|
|
SQLiteDatabase database = databaseHelper.getReadableDatabase();
|
|
|
|
Cursor cursor = null;
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
String randomColumn;
|
|
|
|
|
|
|
|
switch (dataType) {
|
|
|
|
case DATA: randomColumn = DATA_RANDOM; break;
|
|
|
|
case THUMBNAIL: randomColumn = THUMBNAIL_RANDOM; break;
|
|
|
|
default:throw new AssertionError("Unknown data type: " + dataType);
|
|
|
|
}
|
|
|
|
|
2015-10-13 01:25:05 +00:00
|
|
|
try {
|
2018-01-25 03:17:44 +00:00
|
|
|
cursor = database.query(TABLE_NAME, new String[]{dataType, SIZE, randomColumn}, PART_ID_WHERE, attachmentId.toStrings(),
|
2015-10-13 01:25:05 +00:00
|
|
|
null, null, null);
|
|
|
|
|
|
|
|
if (cursor != null && cursor.moveToFirst()) {
|
|
|
|
if (cursor.isNull(0)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
return new DataInfo(new File(cursor.getString(0)),
|
|
|
|
cursor.getLong(1),
|
|
|
|
cursor.getBlob(2));
|
2015-10-13 01:25:05 +00:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (cursor != null)
|
|
|
|
cursor.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
private @NonNull DataInfo setAttachmentData(@NonNull Uri uri)
|
2015-10-13 01:25:05 +00:00
|
|
|
throws MmsException
|
|
|
|
{
|
|
|
|
try {
|
2018-01-25 03:17:44 +00:00
|
|
|
InputStream inputStream = PartAuthority.getAttachmentStream(context, uri);
|
|
|
|
return setAttachmentData(inputStream);
|
2015-10-13 01:25:05 +00:00
|
|
|
} catch (IOException e) {
|
|
|
|
throw new MmsException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
private @NonNull DataInfo setAttachmentData(@NonNull InputStream in)
|
2015-10-13 01:25:05 +00:00
|
|
|
throws MmsException
|
|
|
|
{
|
|
|
|
try {
|
2018-02-26 17:58:18 +00:00
|
|
|
File partsDirectory = context.getDir(DIRECTORY, Context.MODE_PRIVATE);
|
2015-10-13 01:25:05 +00:00
|
|
|
File dataFile = File.createTempFile("part", ".mms", partsDirectory);
|
2018-01-25 03:17:44 +00:00
|
|
|
return setAttachmentData(dataFile, in);
|
2015-10-13 01:25:05 +00:00
|
|
|
} catch (IOException e) {
|
|
|
|
throw new MmsException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
private @NonNull DataInfo setAttachmentData(@NonNull File destination, @NonNull InputStream in)
|
2015-10-13 01:25:05 +00:00
|
|
|
throws MmsException
|
|
|
|
{
|
|
|
|
try {
|
2018-02-01 21:30:33 +00:00
|
|
|
Pair<byte[], OutputStream> out = ModernEncryptingPartOutputStream.createFor(attachmentSecret, destination, false);
|
|
|
|
long length = Util.copy(in, out.second);
|
2018-01-25 03:17:44 +00:00
|
|
|
|
2018-02-01 21:30:33 +00:00
|
|
|
return new DataInfo(destination, length, out.first);
|
2015-10-13 01:25:05 +00:00
|
|
|
} catch (IOException e) {
|
|
|
|
throw new MmsException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-07 22:01:37 +00:00
|
|
|
public List<DatabaseAttachment> getAttachment(@NonNull Cursor cursor) {
|
|
|
|
try {
|
|
|
|
if (cursor.getColumnIndex(AttachmentDatabase.ATTACHMENT_JSON_ALIAS) != -1) {
|
|
|
|
if (cursor.isNull(cursor.getColumnIndexOrThrow(ATTACHMENT_JSON_ALIAS))) {
|
|
|
|
return new LinkedList<>();
|
|
|
|
}
|
|
|
|
|
|
|
|
List<DatabaseAttachment> result = new LinkedList<>();
|
|
|
|
JSONArray array = new JSONArray(cursor.getString(cursor.getColumnIndexOrThrow(ATTACHMENT_JSON_ALIAS)));
|
|
|
|
|
|
|
|
for (int i=0;i<array.length();i++) {
|
|
|
|
JsonUtils.SaneJSONObject object = new JsonUtils.SaneJSONObject(array.getJSONObject(i));
|
|
|
|
|
|
|
|
if (!object.isNull(ROW_ID)) {
|
|
|
|
result.add(new DatabaseAttachment(new AttachmentId(object.getLong(ROW_ID), object.getLong(UNIQUE_ID)),
|
|
|
|
object.getLong(MMS_ID),
|
|
|
|
!TextUtils.isEmpty(object.getString(DATA)),
|
|
|
|
!TextUtils.isEmpty(object.getString(THUMBNAIL)),
|
|
|
|
object.getString(CONTENT_TYPE),
|
|
|
|
object.getInt(TRANSFER_STATE),
|
|
|
|
object.getLong(SIZE),
|
|
|
|
object.getString(FILE_NAME),
|
|
|
|
object.getString(CONTENT_LOCATION),
|
|
|
|
object.getString(CONTENT_DISPOSITION),
|
|
|
|
object.getString(NAME),
|
|
|
|
null,
|
|
|
|
object.getString(FAST_PREFLIGHT_ID),
|
|
|
|
object.getInt(VOICE_NOTE) == 1,
|
|
|
|
object.getInt(WIDTH),
|
|
|
|
object.getInt(HEIGHT),
|
|
|
|
object.getInt(QUOTE) == 1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
return Collections.singletonList(new DatabaseAttachment(new AttachmentId(cursor.getLong(cursor.getColumnIndexOrThrow(ROW_ID)),
|
|
|
|
cursor.getLong(cursor.getColumnIndexOrThrow(UNIQUE_ID))),
|
|
|
|
cursor.getLong(cursor.getColumnIndexOrThrow(MMS_ID)),
|
|
|
|
!cursor.isNull(cursor.getColumnIndexOrThrow(DATA)),
|
|
|
|
!cursor.isNull(cursor.getColumnIndexOrThrow(THUMBNAIL)),
|
|
|
|
cursor.getString(cursor.getColumnIndexOrThrow(CONTENT_TYPE)),
|
|
|
|
cursor.getInt(cursor.getColumnIndexOrThrow(TRANSFER_STATE)),
|
|
|
|
cursor.getLong(cursor.getColumnIndexOrThrow(SIZE)),
|
|
|
|
cursor.getString(cursor.getColumnIndexOrThrow(FILE_NAME)),
|
|
|
|
cursor.getString(cursor.getColumnIndexOrThrow(CONTENT_LOCATION)),
|
|
|
|
cursor.getString(cursor.getColumnIndexOrThrow(CONTENT_DISPOSITION)),
|
|
|
|
cursor.getString(cursor.getColumnIndexOrThrow(NAME)),
|
|
|
|
cursor.getBlob(cursor.getColumnIndexOrThrow(DIGEST)),
|
|
|
|
cursor.getString(cursor.getColumnIndexOrThrow(FAST_PREFLIGHT_ID)),
|
|
|
|
cursor.getInt(cursor.getColumnIndexOrThrow(VOICE_NOTE)) == 1,
|
|
|
|
cursor.getInt(cursor.getColumnIndexOrThrow(WIDTH)),
|
|
|
|
cursor.getInt(cursor.getColumnIndexOrThrow(HEIGHT)),
|
|
|
|
cursor.getInt(cursor.getColumnIndexOrThrow(QUOTE)) == 1));
|
|
|
|
}
|
|
|
|
} catch (JSONException e) {
|
|
|
|
throw new AssertionError(e);
|
|
|
|
}
|
2015-10-13 01:25:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-02-07 22:01:37 +00:00
|
|
|
private AttachmentId insertAttachment(long mmsId, Attachment attachment, boolean quote)
|
2015-10-13 01:25:05 +00:00
|
|
|
throws MmsException
|
|
|
|
{
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.d(TAG, "Inserting attachment for mms id: " + mmsId);
|
2015-10-13 01:25:05 +00:00
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
SQLiteDatabase database = databaseHelper.getWritableDatabase();
|
|
|
|
DataInfo dataInfo = null;
|
|
|
|
long uniqueId = System.currentTimeMillis();
|
2015-10-13 01:25:05 +00:00
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
if (attachment.getDataUri() != null) {
|
|
|
|
dataInfo = setAttachmentData(attachment.getDataUri());
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.d(TAG, "Wrote part to file: " + dataInfo.file.getAbsolutePath());
|
2017-03-28 19:05:30 +00:00
|
|
|
}
|
|
|
|
|
2015-10-13 01:25:05 +00:00
|
|
|
ContentValues contentValues = new ContentValues();
|
|
|
|
contentValues.put(MMS_ID, mmsId);
|
|
|
|
contentValues.put(CONTENT_TYPE, attachment.getContentType());
|
|
|
|
contentValues.put(TRANSFER_STATE, attachment.getTransferState());
|
|
|
|
contentValues.put(UNIQUE_ID, uniqueId);
|
|
|
|
contentValues.put(CONTENT_LOCATION, attachment.getLocation());
|
2017-02-26 18:06:27 +00:00
|
|
|
contentValues.put(DIGEST, attachment.getDigest());
|
2015-10-13 01:25:05 +00:00
|
|
|
contentValues.put(CONTENT_DISPOSITION, attachment.getKey());
|
|
|
|
contentValues.put(NAME, attachment.getRelay());
|
2018-02-16 19:13:20 +00:00
|
|
|
contentValues.put(FILE_NAME, StorageUtil.getCleanFileName(attachment.getFileName()));
|
2017-03-28 19:05:30 +00:00
|
|
|
contentValues.put(SIZE, attachment.getSize());
|
2017-04-22 23:29:26 +00:00
|
|
|
contentValues.put(FAST_PREFLIGHT_ID, attachment.getFastPreflightId());
|
2017-05-12 05:46:35 +00:00
|
|
|
contentValues.put(VOICE_NOTE, attachment.isVoiceNote() ? 1 : 0);
|
2018-03-18 23:04:33 +00:00
|
|
|
contentValues.put(WIDTH, attachment.getWidth());
|
|
|
|
contentValues.put(HEIGHT, attachment.getHeight());
|
2018-02-07 22:01:37 +00:00
|
|
|
contentValues.put(QUOTE, quote);
|
2015-10-13 01:25:05 +00:00
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
if (dataInfo != null) {
|
|
|
|
contentValues.put(DATA, dataInfo.file.getAbsolutePath());
|
|
|
|
contentValues.put(SIZE, dataInfo.length);
|
|
|
|
contentValues.put(DATA_RANDOM, dataInfo.random);
|
2015-10-13 01:25:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
long rowId = database.insert(TABLE_NAME, null, contentValues);
|
|
|
|
AttachmentId attachmentId = new AttachmentId(rowId, uniqueId);
|
2018-04-02 23:17:32 +00:00
|
|
|
Uri thumbnailUri = attachment.getThumbnailUri();
|
|
|
|
boolean hasThumbnail = false;
|
|
|
|
|
|
|
|
if (thumbnailUri != null) {
|
|
|
|
try (InputStream attachmentStream = PartAuthority.getAttachmentStream(context, thumbnailUri)) {
|
|
|
|
Pair<Integer, Integer> dimens = BitmapUtil.getDimensions(attachmentStream);
|
|
|
|
updateAttachmentThumbnail(attachmentId,
|
|
|
|
PartAuthority.getAttachmentStream(context, thumbnailUri),
|
|
|
|
(float) dimens.first / (float) dimens.second);
|
|
|
|
hasThumbnail = true;
|
|
|
|
} catch (IOException | BitmapDecodingException e) {
|
|
|
|
Log.w(TAG, "Failed to save existing thumbnail.", e);
|
|
|
|
}
|
|
|
|
}
|
2015-10-13 01:25:05 +00:00
|
|
|
|
2018-04-02 23:17:32 +00:00
|
|
|
if (!hasThumbnail && dataInfo != null) {
|
2017-04-20 04:23:57 +00:00
|
|
|
if (MediaUtil.hasVideoThumbnail(attachment.getDataUri())) {
|
|
|
|
Bitmap bitmap = MediaUtil.getVideoThumbnail(context, attachment.getDataUri());
|
|
|
|
|
|
|
|
if (bitmap != null) {
|
|
|
|
ThumbnailData thumbnailData = new ThumbnailData(bitmap);
|
2018-01-25 03:17:44 +00:00
|
|
|
updateAttachmentThumbnail(attachmentId, thumbnailData.toDataStream(), thumbnailData.getAspectRatio());
|
2017-04-20 04:23:57 +00:00
|
|
|
} else {
|
|
|
|
Log.w(TAG, "Retrieving video thumbnail failed, submitting thumbnail generation job...");
|
2018-01-25 03:17:44 +00:00
|
|
|
thumbnailExecutor.submit(new ThumbnailFetchCallable(attachmentId));
|
2017-04-20 04:23:57 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.i(TAG, "Submitting thumbnail generation job...");
|
2018-01-25 03:17:44 +00:00
|
|
|
thumbnailExecutor.submit(new ThumbnailFetchCallable(attachmentId));
|
2017-04-20 04:23:57 +00:00
|
|
|
}
|
2015-10-13 01:25:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return attachmentId;
|
|
|
|
}
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
@SuppressWarnings("WeakerAccess")
|
2015-10-13 01:25:05 +00:00
|
|
|
@VisibleForTesting
|
2018-01-25 03:17:44 +00:00
|
|
|
protected void updateAttachmentThumbnail(AttachmentId attachmentId, InputStream in, float aspectRatio)
|
2015-10-13 01:25:05 +00:00
|
|
|
throws MmsException
|
|
|
|
{
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.i(TAG, "updating part thumbnail for #" + attachmentId);
|
2015-10-13 01:25:05 +00:00
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
DataInfo thumbnailFile = setAttachmentData(in);
|
2015-10-13 01:25:05 +00:00
|
|
|
|
|
|
|
SQLiteDatabase database = databaseHelper.getWritableDatabase();
|
|
|
|
ContentValues values = new ContentValues(2);
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
values.put(THUMBNAIL, thumbnailFile.file.getAbsolutePath());
|
2015-10-13 01:25:05 +00:00
|
|
|
values.put(THUMBNAIL_ASPECT_RATIO, aspectRatio);
|
2018-01-25 03:17:44 +00:00
|
|
|
values.put(THUMBNAIL_RANDOM, thumbnailFile.random);
|
2015-10-13 01:25:05 +00:00
|
|
|
|
|
|
|
database.update(TABLE_NAME, values, PART_ID_WHERE, attachmentId.toStrings());
|
2016-12-11 21:37:27 +00:00
|
|
|
|
|
|
|
Cursor cursor = database.query(TABLE_NAME, new String[] {MMS_ID}, PART_ID_WHERE, attachmentId.toStrings(), null, null, null);
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (cursor != null && cursor.moveToFirst()) {
|
|
|
|
notifyConversationListeners(DatabaseFactory.getMmsDatabase(context).getThreadIdForMessage(cursor.getLong(cursor.getColumnIndexOrThrow(MMS_ID))));
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (cursor != null) cursor.close();
|
|
|
|
}
|
2015-10-13 01:25:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@VisibleForTesting
|
|
|
|
class ThumbnailFetchCallable implements Callable<InputStream> {
|
2016-12-11 21:37:27 +00:00
|
|
|
|
2015-10-13 01:25:05 +00:00
|
|
|
private final AttachmentId attachmentId;
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
ThumbnailFetchCallable(AttachmentId attachmentId) {
|
2015-10-13 01:25:05 +00:00
|
|
|
this.attachmentId = attachmentId;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public @Nullable InputStream call() throws Exception {
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.d(TAG, "Executing thumbnail job...");
|
2018-02-24 19:09:26 +00:00
|
|
|
final InputStream stream = getDataStream(attachmentId, THUMBNAIL, 0);
|
2015-10-13 01:25:05 +00:00
|
|
|
|
|
|
|
if (stream != null) {
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
DatabaseAttachment attachment = getAttachment(attachmentId);
|
2015-10-13 01:25:05 +00:00
|
|
|
|
2015-10-26 18:39:00 +00:00
|
|
|
if (attachment == null || !attachment.hasData()) {
|
2015-10-13 01:25:05 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-03-18 22:06:51 +00:00
|
|
|
ThumbnailData data = null;
|
2016-12-11 21:37:27 +00:00
|
|
|
|
2017-05-08 22:32:59 +00:00
|
|
|
if (MediaUtil.isVideoType(attachment.getContentType())) {
|
2018-01-25 03:17:44 +00:00
|
|
|
data = generateVideoThumbnail(attachmentId);
|
2016-12-11 21:37:27 +00:00
|
|
|
}
|
2015-10-13 01:25:05 +00:00
|
|
|
|
|
|
|
if (data == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
updateAttachmentThumbnail(attachmentId, data.toDataStream(), data.getAspectRatio());
|
2015-10-13 01:25:05 +00:00
|
|
|
|
2018-02-24 19:09:26 +00:00
|
|
|
return getDataStream(attachmentId, THUMBNAIL, 0);
|
2015-10-13 01:25:05 +00:00
|
|
|
}
|
2016-12-11 21:37:27 +00:00
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
@SuppressLint("NewApi")
|
|
|
|
private ThumbnailData generateVideoThumbnail(AttachmentId attachmentId) {
|
2016-12-11 21:37:27 +00:00
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
|
|
|
|
Log.w(TAG, "Video thumbnails not supported...");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
DataInfo dataInfo = getAttachmentDataFileInfo(attachmentId, DATA);
|
2016-12-11 21:37:27 +00:00
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
if (dataInfo == null) {
|
2016-12-11 21:37:27 +00:00
|
|
|
Log.w(TAG, "No data file found for video thumbnail...");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
EncryptedMediaDataSource dataSource = new EncryptedMediaDataSource(attachmentSecret, dataInfo.file, dataInfo.random, dataInfo.length);
|
2016-12-11 21:37:27 +00:00
|
|
|
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
|
|
|
|
retriever.setDataSource(dataSource);
|
|
|
|
|
|
|
|
Bitmap bitmap = retriever.getFrameAtTime(1000);
|
|
|
|
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.i(TAG, "Generated video thumbnail...");
|
2016-12-11 21:37:27 +00:00
|
|
|
return new ThumbnailData(bitmap);
|
|
|
|
}
|
2015-10-13 01:25:05 +00:00
|
|
|
}
|
2018-01-25 03:17:44 +00:00
|
|
|
|
|
|
|
private static class DataInfo {
|
|
|
|
private final File file;
|
|
|
|
private final long length;
|
|
|
|
private final byte[] random;
|
|
|
|
|
|
|
|
private DataInfo(File file, long length, byte[] random) {
|
|
|
|
this.file = file;
|
|
|
|
this.length = length;
|
|
|
|
this.random = random;
|
|
|
|
}
|
|
|
|
}
|
2015-10-13 01:25:05 +00:00
|
|
|
}
|