2014-12-12 09:03:24 +00:00
|
|
|
package org.thoughtcrime.securesms.mms;
|
|
|
|
|
|
|
|
import android.content.ContentUris;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.UriMatcher;
|
|
|
|
import android.net.Uri;
|
2015-10-13 01:25:05 +00:00
|
|
|
import android.support.annotation.NonNull;
|
2014-12-12 09:03:24 +00:00
|
|
|
|
2015-10-13 01:25:05 +00:00
|
|
|
import org.thoughtcrime.securesms.attachments.AttachmentId;
|
2014-12-12 09:03:24 +00:00
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
2015-06-08 18:07:46 +00:00
|
|
|
import org.thoughtcrime.securesms.providers.CaptureProvider;
|
2014-12-12 09:03:24 +00:00
|
|
|
import org.thoughtcrime.securesms.providers.PartProvider;
|
2015-10-13 01:25:05 +00:00
|
|
|
import org.thoughtcrime.securesms.providers.SingleUseBlobProvider;
|
2014-12-12 09:03:24 +00:00
|
|
|
|
2014-12-30 09:36:51 +00:00
|
|
|
import java.io.IOException;
|
2014-12-12 09:03:24 +00:00
|
|
|
import java.io.InputStream;
|
|
|
|
|
|
|
|
public class PartAuthority {
|
|
|
|
|
2015-03-31 22:44:41 +00:00
|
|
|
private static final String PART_URI_STRING = "content://org.thoughtcrime.securesms/part";
|
|
|
|
private static final String THUMB_URI_STRING = "content://org.thoughtcrime.securesms/thumb";
|
2015-05-18 15:16:06 +00:00
|
|
|
private static final Uri PART_CONTENT_URI = Uri.parse(PART_URI_STRING);
|
|
|
|
private static final Uri THUMB_CONTENT_URI = Uri.parse(THUMB_URI_STRING);
|
2014-12-12 09:03:24 +00:00
|
|
|
|
2015-10-13 01:25:05 +00:00
|
|
|
private static final int PART_ROW = 1;
|
|
|
|
private static final int THUMB_ROW = 2;
|
|
|
|
private static final int CAPTURE_ROW = 3;
|
|
|
|
private static final int SINGLE_USE_ROW = 4;
|
2014-12-12 09:03:24 +00:00
|
|
|
|
|
|
|
private static final UriMatcher uriMatcher;
|
|
|
|
|
|
|
|
static {
|
|
|
|
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
|
2015-05-18 15:16:06 +00:00
|
|
|
uriMatcher.addURI("org.thoughtcrime.securesms", "part/*/#", PART_ROW);
|
|
|
|
uriMatcher.addURI("org.thoughtcrime.securesms", "thumb/*/#", THUMB_ROW);
|
2015-06-08 18:07:46 +00:00
|
|
|
uriMatcher.addURI(CaptureProvider.AUTHORITY, CaptureProvider.EXPECTED_PATH, CAPTURE_ROW);
|
2015-10-13 01:25:05 +00:00
|
|
|
uriMatcher.addURI(SingleUseBlobProvider.AUTHORITY, SingleUseBlobProvider.PATH, SINGLE_USE_ROW);
|
2014-12-12 09:03:24 +00:00
|
|
|
}
|
|
|
|
|
2015-10-13 01:25:05 +00:00
|
|
|
public static InputStream getAttachmentStream(@NonNull Context context, @NonNull MasterSecret masterSecret, @NonNull Uri uri)
|
2014-12-30 09:36:51 +00:00
|
|
|
throws IOException
|
2014-12-12 09:03:24 +00:00
|
|
|
{
|
2015-06-08 18:07:46 +00:00
|
|
|
int match = uriMatcher.match(uri);
|
2015-01-13 19:47:32 +00:00
|
|
|
try {
|
|
|
|
switch (match) {
|
2015-05-18 15:16:06 +00:00
|
|
|
case PART_ROW:
|
2015-05-21 18:55:03 +00:00
|
|
|
PartUriParser partUri = new PartUriParser(uri);
|
2015-10-13 01:25:05 +00:00
|
|
|
return DatabaseFactory.getAttachmentDatabase(context).getAttachmentStream(masterSecret, partUri.getPartId());
|
2015-05-18 15:16:06 +00:00
|
|
|
case THUMB_ROW:
|
2015-05-21 18:55:03 +00:00
|
|
|
partUri = new PartUriParser(uri);
|
2015-10-13 01:25:05 +00:00
|
|
|
return DatabaseFactory.getAttachmentDatabase(context).getThumbnailStream(masterSecret, partUri.getPartId());
|
2015-06-08 18:07:46 +00:00
|
|
|
case CAPTURE_ROW:
|
|
|
|
return CaptureProvider.getInstance(context).getStream(masterSecret, ContentUris.parseId(uri));
|
2015-10-13 01:25:05 +00:00
|
|
|
case SINGLE_USE_ROW:
|
|
|
|
return SingleUseBlobProvider.getInstance().getStream(ContentUris.parseId(uri));
|
2015-05-18 15:16:06 +00:00
|
|
|
default:
|
2015-06-08 18:07:46 +00:00
|
|
|
return context.getContentResolver().openInputStream(uri);
|
2015-01-13 19:47:32 +00:00
|
|
|
}
|
|
|
|
} catch (SecurityException se) {
|
|
|
|
throw new IOException(se);
|
2014-12-12 09:03:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-13 01:25:05 +00:00
|
|
|
public static Uri getAttachmentPublicUri(Uri uri) {
|
2015-05-21 18:55:03 +00:00
|
|
|
PartUriParser partUri = new PartUriParser(uri);
|
|
|
|
return PartProvider.getContentUri(partUri.getPartId());
|
2014-12-12 09:03:24 +00:00
|
|
|
}
|
2015-03-31 22:44:41 +00:00
|
|
|
|
2015-10-13 01:25:05 +00:00
|
|
|
public static Uri getAttachmentDataUri(AttachmentId attachmentId) {
|
|
|
|
Uri uri = Uri.withAppendedPath(PART_CONTENT_URI, String.valueOf(attachmentId.getUniqueId()));
|
|
|
|
return ContentUris.withAppendedId(uri, attachmentId.getRowId());
|
2015-05-18 15:16:06 +00:00
|
|
|
}
|
|
|
|
|
2015-10-13 01:25:05 +00:00
|
|
|
public static Uri getAttachmentThumbnailUri(AttachmentId attachmentId) {
|
|
|
|
Uri uri = Uri.withAppendedPath(THUMB_CONTENT_URI, String.valueOf(attachmentId.getUniqueId()));
|
|
|
|
return ContentUris.withAppendedId(uri, attachmentId.getRowId());
|
2015-03-31 22:44:41 +00:00
|
|
|
}
|
2014-12-12 09:03:24 +00:00
|
|
|
}
|