| 
									
										
										
										
											2014-12-12 01:03:24 -08:00
										 |  |  | package org.thoughtcrime.securesms.mms;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import android.content.ContentUris;
 | 
					
						
							|  |  |  | import android.content.Context;
 | 
					
						
							|  |  |  | import android.content.UriMatcher;
 | 
					
						
							|  |  |  | import android.net.Uri;
 | 
					
						
							| 
									
										
										
										
											2015-10-12 18:25:05 -07:00
										 |  |  | import android.support.annotation.NonNull;
 | 
					
						
							| 
									
										
										
										
											2017-05-10 15:21:52 -07:00
										 |  |  | import android.support.annotation.Nullable;
 | 
					
						
							| 
									
										
										
										
											2014-12-12 01:03:24 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-10 15:21:52 -07:00
										 |  |  | import org.thoughtcrime.securesms.attachments.Attachment;
 | 
					
						
							| 
									
										
										
										
											2015-10-12 18:25:05 -07:00
										 |  |  | import org.thoughtcrime.securesms.attachments.AttachmentId;
 | 
					
						
							| 
									
										
										
										
											2014-12-12 01:03:24 -08:00
										 |  |  | import org.thoughtcrime.securesms.database.DatabaseFactory;
 | 
					
						
							| 
									
										
										
										
											2019-02-25 17:47:30 -08:00
										 |  |  | import org.thoughtcrime.securesms.providers.BlobProvider;
 | 
					
						
							|  |  |  | import org.thoughtcrime.securesms.providers.DeprecatedPersistentBlobProvider;
 | 
					
						
							| 
									
										
										
										
											2014-12-12 01:03:24 -08:00
										 |  |  | import org.thoughtcrime.securesms.providers.PartProvider;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-30 01:36:51 -08:00
										 |  |  | import java.io.IOException;
 | 
					
						
							| 
									
										
										
										
											2014-12-12 01:03:24 -08:00
										 |  |  | import java.io.InputStream;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | public class PartAuthority {
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-31 15:44:41 -07: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 08:16:06 -07: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 01:03:24 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-12 18:25:05 -07:00
										 |  |  |   private static final int PART_ROW       = 1;
 | 
					
						
							|  |  |  |   private static final int THUMB_ROW      = 2;
 | 
					
						
							| 
									
										
										
										
											2015-10-15 14:40:45 -07:00
										 |  |  |   private static final int PERSISTENT_ROW = 3;
 | 
					
						
							| 
									
										
										
										
											2019-02-25 17:47:30 -08:00
										 |  |  |   private static final int BLOB_ROW       = 4;
 | 
					
						
							| 
									
										
										
										
											2014-12-12 01:03:24 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   private static final UriMatcher uriMatcher;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   static {
 | 
					
						
							|  |  |  |     uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
 | 
					
						
							| 
									
										
										
										
											2015-05-18 08:16:06 -07:00
										 |  |  |     uriMatcher.addURI("org.thoughtcrime.securesms", "part/*/#", PART_ROW);
 | 
					
						
							|  |  |  |     uriMatcher.addURI("org.thoughtcrime.securesms", "thumb/*/#", THUMB_ROW);
 | 
					
						
							| 
									
										
										
										
											2019-02-25 17:47:30 -08:00
										 |  |  |     uriMatcher.addURI(DeprecatedPersistentBlobProvider.AUTHORITY, DeprecatedPersistentBlobProvider.EXPECTED_PATH_OLD, PERSISTENT_ROW);
 | 
					
						
							|  |  |  |     uriMatcher.addURI(DeprecatedPersistentBlobProvider.AUTHORITY, DeprecatedPersistentBlobProvider.EXPECTED_PATH_NEW, PERSISTENT_ROW);
 | 
					
						
							|  |  |  |     uriMatcher.addURI(BlobProvider.AUTHORITY, BlobProvider.PATH, BLOB_ROW);
 | 
					
						
							| 
									
										
										
										
											2014-12-12 01:03:24 -08:00
										 |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-24 19:17:44 -08:00
										 |  |  |   public static InputStream getAttachmentStream(@NonNull Context context, @NonNull Uri uri)
 | 
					
						
							| 
									
										
										
										
											2014-12-30 01:36:51 -08:00
										 |  |  |       throws IOException
 | 
					
						
							| 
									
										
										
										
											2014-12-12 01:03:24 -08:00
										 |  |  |   {
 | 
					
						
							| 
									
										
										
										
											2015-06-08 11:07:46 -07:00
										 |  |  |     int match = uriMatcher.match(uri);
 | 
					
						
							| 
									
										
										
										
											2015-01-13 09:47:32 -10:00
										 |  |  |     try {
 | 
					
						
							|  |  |  |       switch (match) {
 | 
					
						
							| 
									
										
										
										
											2018-02-24 11:09:26 -08:00
										 |  |  |       case PART_ROW:       return DatabaseFactory.getAttachmentDatabase(context).getAttachmentStream(new PartUriParser(uri).getPartId(), 0);
 | 
					
						
							| 
									
										
										
										
											2018-01-24 19:17:44 -08:00
										 |  |  |       case THUMB_ROW:      return DatabaseFactory.getAttachmentDatabase(context).getThumbnailStream(new PartUriParser(uri).getPartId());
 | 
					
						
							| 
									
										
										
										
											2019-02-25 17:47:30 -08:00
										 |  |  |       case PERSISTENT_ROW: return DeprecatedPersistentBlobProvider.getInstance(context).getStream(context, ContentUris.parseId(uri));
 | 
					
						
							|  |  |  |       case BLOB_ROW:       return BlobProvider.getInstance().getStream(context, uri);
 | 
					
						
							| 
									
										
										
										
											2017-05-10 15:21:52 -07:00
										 |  |  |       default:             return context.getContentResolver().openInputStream(uri);
 | 
					
						
							|  |  |  |       }
 | 
					
						
							|  |  |  |     } catch (SecurityException se) {
 | 
					
						
							|  |  |  |       throw new IOException(se);
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-24 19:17:44 -08:00
										 |  |  |   public static @Nullable String getAttachmentFileName(@NonNull Context context, @NonNull Uri uri) {
 | 
					
						
							| 
									
										
										
										
											2017-05-10 15:21:52 -07:00
										 |  |  |     int match = uriMatcher.match(uri);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     switch (match) {
 | 
					
						
							|  |  |  |     case THUMB_ROW:
 | 
					
						
							|  |  |  |     case PART_ROW:
 | 
					
						
							| 
									
										
										
										
											2018-01-24 19:17:44 -08:00
										 |  |  |       Attachment attachment = DatabaseFactory.getAttachmentDatabase(context).getAttachment(new PartUriParser(uri).getPartId());
 | 
					
						
							| 
									
										
										
										
											2017-05-10 15:21:52 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |       if (attachment != null) return attachment.getFileName();
 | 
					
						
							|  |  |  |       else                    return null;
 | 
					
						
							|  |  |  |     case PERSISTENT_ROW:
 | 
					
						
							| 
									
										
										
										
											2019-02-25 17:47:30 -08:00
										 |  |  |       return DeprecatedPersistentBlobProvider.getFileName(context, uri);
 | 
					
						
							|  |  |  |     case BLOB_ROW:
 | 
					
						
							|  |  |  |       return BlobProvider.getFileName(uri);
 | 
					
						
							| 
									
										
										
										
											2017-05-10 15:21:52 -07:00
										 |  |  |     default:
 | 
					
						
							|  |  |  |       return null;
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-24 19:17:44 -08:00
										 |  |  |   public static @Nullable Long getAttachmentSize(@NonNull Context context, @NonNull Uri uri) {
 | 
					
						
							| 
									
										
										
										
											2017-05-10 15:21:52 -07:00
										 |  |  |     int match = uriMatcher.match(uri);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     switch (match) {
 | 
					
						
							|  |  |  |       case THUMB_ROW:
 | 
					
						
							| 
									
										
										
										
											2015-05-18 08:16:06 -07:00
										 |  |  |       case PART_ROW:
 | 
					
						
							| 
									
										
										
										
											2018-01-24 19:17:44 -08:00
										 |  |  |         Attachment attachment = DatabaseFactory.getAttachmentDatabase(context).getAttachment(new PartUriParser(uri).getPartId());
 | 
					
						
							| 
									
										
										
										
											2017-05-10 15:21:52 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (attachment != null) return attachment.getSize();
 | 
					
						
							|  |  |  |         else                    return null;
 | 
					
						
							|  |  |  |       case PERSISTENT_ROW:
 | 
					
						
							| 
									
										
										
										
											2019-02-25 17:47:30 -08:00
										 |  |  |         return DeprecatedPersistentBlobProvider.getFileSize(context, uri);
 | 
					
						
							|  |  |  |       case BLOB_ROW:
 | 
					
						
							|  |  |  |         return BlobProvider.getFileSize(uri);
 | 
					
						
							| 
									
										
										
										
											2017-05-10 15:21:52 -07:00
										 |  |  |       default:
 | 
					
						
							|  |  |  |         return null;
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-24 19:17:44 -08:00
										 |  |  |   public static @Nullable String getAttachmentContentType(@NonNull Context context, @NonNull Uri uri) {
 | 
					
						
							| 
									
										
										
										
											2017-05-10 15:21:52 -07:00
										 |  |  |     int match = uriMatcher.match(uri);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     switch (match) {
 | 
					
						
							| 
									
										
										
										
											2015-05-18 08:16:06 -07:00
										 |  |  |       case THUMB_ROW:
 | 
					
						
							| 
									
										
										
										
											2017-05-10 15:21:52 -07:00
										 |  |  |       case PART_ROW:
 | 
					
						
							| 
									
										
										
										
											2018-01-24 19:17:44 -08:00
										 |  |  |         Attachment attachment = DatabaseFactory.getAttachmentDatabase(context).getAttachment(new PartUriParser(uri).getPartId());
 | 
					
						
							| 
									
										
										
										
											2017-05-10 15:21:52 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (attachment != null) return attachment.getContentType();
 | 
					
						
							|  |  |  |         else                    return null;
 | 
					
						
							| 
									
										
										
										
											2015-10-15 14:40:45 -07:00
										 |  |  |       case PERSISTENT_ROW:
 | 
					
						
							| 
									
										
										
										
											2019-02-25 17:47:30 -08:00
										 |  |  |         return DeprecatedPersistentBlobProvider.getMimeType(context, uri);
 | 
					
						
							|  |  |  |       case BLOB_ROW:
 | 
					
						
							|  |  |  |         return BlobProvider.getMimeType(uri);
 | 
					
						
							| 
									
										
										
										
											2015-05-18 08:16:06 -07:00
										 |  |  |       default:
 | 
					
						
							| 
									
										
										
										
											2017-05-10 15:21:52 -07:00
										 |  |  |         return null;
 | 
					
						
							| 
									
										
										
										
											2014-12-12 01:03:24 -08:00
										 |  |  |     }
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-12 18:25:05 -07:00
										 |  |  |   public static Uri getAttachmentPublicUri(Uri uri) {
 | 
					
						
							| 
									
										
										
										
											2015-05-21 11:55:03 -07:00
										 |  |  |     PartUriParser partUri = new PartUriParser(uri);
 | 
					
						
							|  |  |  |     return PartProvider.getContentUri(partUri.getPartId());
 | 
					
						
							| 
									
										
										
										
											2014-12-12 01:03:24 -08:00
										 |  |  |   }
 | 
					
						
							| 
									
										
										
										
											2015-03-31 15:44:41 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-12 18:25:05 -07: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 08:16:06 -07:00
										 |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-12 18:25:05 -07: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 15:44:41 -07:00
										 |  |  |   }
 | 
					
						
							| 
									
										
										
										
											2015-11-18 12:54:40 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   public static boolean isLocalUri(final @NonNull Uri uri) {
 | 
					
						
							|  |  |  |     int match = uriMatcher.match(uri);
 | 
					
						
							|  |  |  |     switch (match) {
 | 
					
						
							|  |  |  |     case PART_ROW:
 | 
					
						
							|  |  |  |     case THUMB_ROW:
 | 
					
						
							|  |  |  |     case PERSISTENT_ROW:
 | 
					
						
							| 
									
										
										
										
											2019-02-25 17:47:30 -08:00
										 |  |  |     case BLOB_ROW:
 | 
					
						
							| 
									
										
										
										
											2015-11-18 12:54:40 -08:00
										 |  |  |       return true;
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  |     return false;
 | 
					
						
							|  |  |  |   }
 | 
					
						
							| 
									
										
										
										
											2014-12-12 01:03:24 -08:00
										 |  |  | }
 |