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; |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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) { |
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. | |
nyquist
2014/11/19 00:06:25
Nit: MIME here and below?
qinmin
2014/11/19 02:13:07
Done.
| |
86 * | |
87 * @param context {@link Context} in interest. | |
88 * @param uriString the content URI to open. | |
nyquist
2014/11/19 00:06:25
It feels a bit off that we say 'open' here. Maybe
qinmin
2014/11/19 02:13:07
Done.
| |
89 * @returns mime type or an empty string if the input params are invalid. | |
nyquist
2014/11/19 00:06:25
This leads to believe that the method can never re
palmer
2014/11/19 00:23:54
FWIW, The Java Way would be to throw, not return,
qinmin
2014/11/19 02:13:07
Ok, changed the description to just return NULL on
qinmin
2014/11/19 02:13:07
This method is called by native, so either the jav
| |
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 ""; | |
96 return resolver.getType(uri); | |
nyquist
2014/11/19 00:06:25
Do we want to ensure that this never returns null?
qinmin
2014/11/19 02:13:07
returned null for the empty inputs case
| |
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) { |
99 Log.w(TAG, "Cannot find content uri: " + uriString, e); | 114 Log.w(TAG, "Cannot find content uri: " + uriString, e); |
115 } catch (java.lang.SecurityException e) { | |
nyquist
2014/11/19 00:06:25
Do you really need the 'java.lang.' prefix here?
palmer
2014/11/19 00:23:54
Nope.
qinmin
2014/11/19 02:13:07
removed
| |
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 |