1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.messaging.datamodel; 18 19 import android.content.ContentResolver; 20 import android.content.Context; 21 import android.database.Cursor; 22 import android.database.MatrixCursor; 23 import android.database.MatrixCursor.RowBuilder; 24 import android.net.Uri; 25 import android.provider.OpenableColumns; 26 import android.support.v4.util.SimpleArrayMap; 27 import android.text.TextUtils; 28 29 import com.android.messaging.Factory; 30 import com.android.messaging.util.Assert; 31 import com.android.messaging.util.LogUtil; 32 import com.google.common.annotations.VisibleForTesting; 33 34 import java.io.File; 35 import java.util.List; 36 37 /** 38 * A very simple content provider that can serve media files from our cache directory. 39 */ 40 public class MediaScratchFileProvider extends FileProvider { 41 private static final String TAG = LogUtil.BUGLE_TAG; 42 43 private static final SimpleArrayMap<Uri, String> sUriToDisplayNameMap = 44 new SimpleArrayMap<Uri, String>(); 45 46 @VisibleForTesting 47 public static final String AUTHORITY = 48 "com.android.messaging.datamodel.MediaScratchFileProvider"; 49 private static final String MEDIA_SCRATCH_SPACE_DIR = "mediascratchspace"; 50 51 public static boolean isMediaScratchSpaceUri(final Uri uri) { 52 if (uri == null) { 53 return false; 54 } 55 56 final List<String> segments = uri.getPathSegments(); 57 return (TextUtils.equals(uri.getScheme(), ContentResolver.SCHEME_CONTENT) && 58 TextUtils.equals(uri.getAuthority(), AUTHORITY) && 59 segments.size() == 1 && FileProvider.isValidFileId(segments.get(0))); 60 } 61 62 /** 63 * Returns a uri that can be used to access a raw mms file. 64 * 65 * @return the URI for an raw mms file 66 */ 67 public static Uri buildMediaScratchSpaceUri(final String extension) { 68 final Uri uri = FileProvider.buildFileUri(AUTHORITY, extension); 69 final File file = getFileWithExtension(uri.getPath(), extension); 70 if (!ensureFileExists(file)) { 71 LogUtil.e(TAG, "Failed to create temp file " + file.getAbsolutePath()); 72 } 73 return uri; 74 } 75 76 public static File getFileFromUri(final Uri uri) { 77 Assert.equals(AUTHORITY, uri.getAuthority()); 78 return getFileWithExtension(uri.getPath(), getExtensionFromUri(uri)); 79 } 80 81 public static Uri.Builder getUriBuilder() { 82 return (new Uri.Builder()).authority(AUTHORITY).scheme(ContentResolver.SCHEME_CONTENT); 83 } 84 85 @Override 86 File getFile(final String path, final String extension) { 87 return getFileWithExtension(path, extension); 88 } 89 90 private static File getFileWithExtension(final String path, final String extension) { 91 final Context context = Factory.get().getApplicationContext(); 92 return new File(getDirectory(context), 93 TextUtils.isEmpty(extension) ? path : path + "." + extension); 94 } 95 96 private static File getDirectory(final Context context) { 97 return new File(context.getCacheDir(), MEDIA_SCRATCH_SPACE_DIR); 98 } 99 100 @Override 101 public Cursor query(final Uri uri, final String[] projection, final String selection, 102 final String[] selectionArgs, final String sortOrder) { 103 if (projection != null && projection.length > 0 && 104 TextUtils.equals(projection[0], OpenableColumns.DISPLAY_NAME) && 105 isMediaScratchSpaceUri(uri)) { 106 // Retrieve the display name associated with a temp file. This is used by the Contacts 107 // ImportVCardActivity to retrieve the name of the contact(s) being imported. 108 String displayName; 109 synchronized (sUriToDisplayNameMap) { 110 displayName = sUriToDisplayNameMap.get(uri); 111 } 112 if (!TextUtils.isEmpty(displayName)) { 113 MatrixCursor cursor = 114 new MatrixCursor(new String[] { OpenableColumns.DISPLAY_NAME }); 115 RowBuilder row = cursor.newRow(); 116 row.add(displayName); 117 return cursor; 118 } 119 } 120 return null; 121 } 122 123 public static void addUriToDisplayNameEntry(final Uri scratchFileUri, 124 final String displayName) { 125 if (TextUtils.isEmpty(displayName)) { 126 return; 127 } 128 synchronized (sUriToDisplayNameMap) { 129 sUriToDisplayNameMap.put(scratchFileUri, displayName); 130 } 131 } 132 } 133