Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.base; | 5 package org.chromium.base; |
| 6 | 6 |
| 7 import android.content.ContentResolver; | 7 import android.content.ContentResolver; |
| 8 import android.content.Context; | 8 import android.content.Context; |
| 9 import android.database.Cursor; | 9 import android.database.Cursor; |
| 10 import android.net.Uri; | 10 import android.net.Uri; |
| 11 import android.os.ParcelFileDescriptor; | 11 import android.os.ParcelFileDescriptor; |
| 12 import android.util.Log; | 12 import android.util.Log; |
| 13 | 13 |
| 14 import java.io.File; | 14 import java.io.File; |
| 15 | 15 |
| 16 /** | 16 /** |
| 17 * This class provides methods to access content URI schemes. | 17 * This class provides methods to access content URI schemes. |
| 18 */ | 18 */ |
| 19 public abstract class ContentUriUtils { | 19 public abstract class ContentUriUtils { |
| 20 private static final String TAG = "ContentUriUtils"; | 20 private static final String TAG = "ContentUriUtils"; |
| 21 private static FileProviderUtil sFileProviderUtil; | 21 private static FileProviderUtil sFileProviderUtil; |
| 22 | 22 |
| 23 /** | 23 /** |
| 24 * Provides functionality to translate a file into a content URI for use | 24 * Provides functionality to translate a file into a content URI for use |
| 25 * with a content provider. | 25 * with a content provider. |
| 26 */ | 26 */ |
| 27 public interface FileProviderUtil { | 27 public interface FileProviderUtil { |
| 28 /** | 28 /** |
| 29 * Generate a content uri from the given file. | 29 * Generate a content URI from the given file. |
| 30 * @param context Application context. | 30 * @param context Application context. |
| 31 * @param file The file to be translated. | 31 * @param file The file to be translated. |
| 32 */ | 32 */ |
| 33 public Uri getContentUriFromFile(Context context, File file); | 33 public Uri getContentUriFromFile(Context context, File file); |
|
nyquist
2014/11/20 20:01:11
This is implicitly public.
qinmin
2014/11/20 23:07:19
ok, removed the public keyword.
| |
| 34 } | 34 } |
| 35 | 35 |
| 36 // Prevent instantiation. | 36 // Prevent instantiation. |
| 37 private ContentUriUtils() {} | 37 private ContentUriUtils() {} |
| 38 | 38 |
| 39 public static void setFileProviderUtil(FileProviderUtil util) { | 39 public static void setFileProviderUtil(FileProviderUtil util) { |
| 40 sFileProviderUtil = util; | 40 sFileProviderUtil = util; |
| 41 } | 41 } |
| 42 | 42 |
| 43 public static Uri getContentUriFromFile(Context context, File file) { | 43 public static Uri getContentUriFromFile(Context context, File file) { |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 63 return pfd.detachFd(); | 63 return pfd.detachFd(); |
| 64 } | 64 } |
| 65 return -1; | 65 return -1; |
| 66 } | 66 } |
| 67 | 67 |
| 68 /** | 68 /** |
| 69 * Check whether a content URI exists. | 69 * Check whether a content URI exists. |
| 70 * | 70 * |
| 71 * @param context {@link Context} in interest. | 71 * @param context {@link Context} in interest. |
| 72 * @param uriString the content URI to query. | 72 * @param uriString the content URI to query. |
| 73 * @returns true if the uri exists, or false otherwise. | 73 * @returns true if the URI exists, or false otherwise. |
| 74 */ | 74 */ |
| 75 @CalledByNative | 75 @CalledByNative |
| 76 public static boolean contentUriExists(Context context, String uriString) { | 76 public static boolean contentUriExists(Context context, String uriString) { |
| 77 ParcelFileDescriptor pfd = getParcelFileDescriptor(context, uriString); | 77 ParcelFileDescriptor pfd = getParcelFileDescriptor(context, uriString); |
| 78 if (pfd == null) { | 78 if (pfd == null) { |
|
nyquist
2014/11/20 20:01:11
Optional: This bugs me; feel free to change this t
qinmin
2014/11/20 23:07:19
Done.
| |
| 79 return false; | 79 return false; |
| 80 } | 80 } |
| 81 return true; | 81 return true; |
| 82 } | 82 } |
| 83 | 83 |
| 84 /** | 84 /** |
| 85 * Retrieve the MIME type for the content URI. | |
| 86 * | |
| 87 * @param context {@link Context} in interest. | |
| 88 * @param uriString the content URI to look up. | |
| 89 * @returns MIME type or null if the input params are empty or invalid. | |
|
nyquist
2014/11/20 20:01:11
This should be @return (throughout this file)
qinmin
2014/11/20 23:07:18
Done.
| |
| 90 */ | |
| 91 @CalledByNative | |
| 92 public static String getMimeType(Context context, String uriString) { | |
| 93 ContentResolver resolver = context.getContentResolver(); | |
| 94 Uri uri = Uri.parse(uriString); | |
| 95 if (resolver == null || uri == null) return null; | |
|
nyquist
2014/11/20 20:01:11
Uri.parse(...) never returns null (specifically me
qinmin
2014/11/20 23:07:19
Done.
| |
| 96 return resolver.getType(uri); | |
| 97 } | |
| 98 | |
| 99 /** | |
| 85 * Helper method to open a content URI and return the ParcelFileDescriptor. | 100 * Helper method to open a content URI and return the ParcelFileDescriptor. |
| 86 * | 101 * |
| 87 * @param context {@link Context} in interest. | 102 * @param context {@link Context} in interest. |
| 88 * @param uriString the content URI to open. | 103 * @param uriString the content URI to open. |
| 89 * @returns ParcelFileDescriptor of the content URI, or NULL if the file doe s not exist. | 104 * @returns ParcelFileDescriptor of the content URI, or NULL if the file doe s not exist. |
| 90 */ | 105 */ |
| 91 private static ParcelFileDescriptor getParcelFileDescriptor(Context context, String uriString) { | 106 private static ParcelFileDescriptor getParcelFileDescriptor(Context context, String uriString) { |
| 92 ContentResolver resolver = context.getContentResolver(); | 107 ContentResolver resolver = context.getContentResolver(); |
| 93 Uri uri = Uri.parse(uriString); | 108 Uri uri = Uri.parse(uriString); |
| 94 | 109 |
| 95 ParcelFileDescriptor pfd = null; | 110 ParcelFileDescriptor pfd = null; |
| 96 try { | 111 try { |
| 97 pfd = resolver.openFileDescriptor(uri, "r"); | 112 pfd = resolver.openFileDescriptor(uri, "r"); |
| 98 } catch (java.io.FileNotFoundException e) { | 113 } catch (java.io.FileNotFoundException e) { |
|
nyquist
2014/11/20 20:01:11
could you change this one to import FileNotFoundEx
qinmin
2014/11/20 23:07:18
Done.
| |
| 99 Log.w(TAG, "Cannot find content uri: " + uriString, e); | 114 Log.w(TAG, "Cannot find content uri: " + uriString, e); |
| 115 } catch (SecurityException e) { | |
| 116 Log.w(TAG, "Cannot open content uri: " + uriString, e); | |
| 100 } | 117 } |
| 101 return pfd; | 118 return pfd; |
| 102 } | 119 } |
| 103 | 120 |
| 104 /** | 121 /** |
| 105 * Method to resolve the display name of a content URI. | 122 * Method to resolve the display name of a content URI. |
| 106 * | 123 * |
| 107 * @param uri the content URI to be resolved. | 124 * @param uri the content URI to be resolved. |
| 108 * @param contentResolver the content resolver to query. | 125 * @param contentResolver the content resolver to query. |
| 109 * @param columnField the column field to query. | 126 * @param columnField the column field to query. |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 125 } catch (NullPointerException e) { | 142 } catch (NullPointerException e) { |
| 126 // Some android models don't handle the provider call correctly. | 143 // Some android models don't handle the provider call correctly. |
| 127 // see crbug.com/345393 | 144 // see crbug.com/345393 |
| 128 return ""; | 145 return ""; |
| 129 } finally { | 146 } finally { |
| 130 if (cursor != null) cursor.close(); | 147 if (cursor != null) cursor.close(); |
| 131 } | 148 } |
| 132 return ""; | 149 return ""; |
| 133 } | 150 } |
| 134 } | 151 } |
| OLD | NEW |