/base/android/java/src/org/chromium/base/ContentUriUtils.java

https://gitlab.com/0072016/Facebook-SDK- · Java · 160 lines · 86 code · 17 blank · 57 comment · 17 complexity · 53ff73361b7e1500e26e188df26da7d1 MD5 · raw file

  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
  3. // found in the LICENSE file.
  4. package org.chromium.base;
  5. import android.content.ContentResolver;
  6. import android.content.Context;
  7. import android.database.Cursor;
  8. import android.net.Uri;
  9. import android.os.ParcelFileDescriptor;
  10. import android.util.Log;
  11. import org.chromium.base.annotations.CalledByNative;
  12. import java.io.File;
  13. import java.io.FileNotFoundException;
  14. /**
  15. * This class provides methods to access content URI schemes.
  16. */
  17. public abstract class ContentUriUtils {
  18. private static final String TAG = "ContentUriUtils";
  19. private static FileProviderUtil sFileProviderUtil;
  20. // Guards access to sFileProviderUtil.
  21. private static final Object sLock = new Object();
  22. /**
  23. * Provides functionality to translate a file into a content URI for use
  24. * with a content provider.
  25. */
  26. public interface FileProviderUtil {
  27. /**
  28. * Generate a content URI from the given file.
  29. * @param context Application context.
  30. * @param file The file to be translated.
  31. */
  32. Uri getContentUriFromFile(Context context, File file);
  33. }
  34. // Prevent instantiation.
  35. private ContentUriUtils() {}
  36. public static void setFileProviderUtil(FileProviderUtil util) {
  37. synchronized (sLock) {
  38. sFileProviderUtil = util;
  39. }
  40. }
  41. public static Uri getContentUriFromFile(Context context, File file) {
  42. synchronized (sLock) {
  43. if (sFileProviderUtil != null) {
  44. return sFileProviderUtil.getContentUriFromFile(context, file);
  45. }
  46. }
  47. return null;
  48. }
  49. /**
  50. * Opens the content URI for reading, and returns the file descriptor to
  51. * the caller. The caller is responsible for closing the file desciptor.
  52. *
  53. * @param context {@link Context} in interest
  54. * @param uriString the content URI to open
  55. * @return file desciptor upon success, or -1 otherwise.
  56. */
  57. @CalledByNative
  58. public static int openContentUriForRead(Context context, String uriString) {
  59. ParcelFileDescriptor pfd = getParcelFileDescriptor(context, uriString);
  60. if (pfd != null) {
  61. return pfd.detachFd();
  62. }
  63. return -1;
  64. }
  65. /**
  66. * Check whether a content URI exists.
  67. *
  68. * @param context {@link Context} in interest.
  69. * @param uriString the content URI to query.
  70. * @return true if the URI exists, or false otherwise.
  71. */
  72. @CalledByNative
  73. public static boolean contentUriExists(Context context, String uriString) {
  74. return getParcelFileDescriptor(context, uriString) != null;
  75. }
  76. /**
  77. * Retrieve the MIME type for the content URI.
  78. *
  79. * @param context {@link Context} in interest.
  80. * @param uriString the content URI to look up.
  81. * @return MIME type or null if the input params are empty or invalid.
  82. */
  83. @CalledByNative
  84. public static String getMimeType(Context context, String uriString) {
  85. ContentResolver resolver = context.getContentResolver();
  86. if (resolver == null) return null;
  87. Uri uri = Uri.parse(uriString);
  88. return resolver.getType(uri);
  89. }
  90. /**
  91. * Helper method to open a content URI and returns the ParcelFileDescriptor.
  92. *
  93. * @param context {@link Context} in interest.
  94. * @param uriString the content URI to open.
  95. * @return ParcelFileDescriptor of the content URI, or NULL if the file does not exist.
  96. */
  97. private static ParcelFileDescriptor getParcelFileDescriptor(Context context, String uriString) {
  98. ContentResolver resolver = context.getContentResolver();
  99. Uri uri = Uri.parse(uriString);
  100. ParcelFileDescriptor pfd = null;
  101. try {
  102. pfd = resolver.openFileDescriptor(uri, "r");
  103. } catch (FileNotFoundException e) {
  104. Log.w(TAG, "Cannot find content uri: " + uriString, e);
  105. } catch (SecurityException e) {
  106. Log.w(TAG, "Cannot open content uri: " + uriString, e);
  107. } catch (IllegalArgumentException e) {
  108. Log.w(TAG, "Unknown content uri: " + uriString, e);
  109. } catch (IllegalStateException e) {
  110. Log.w(TAG, "Unknown content uri: " + uriString, e);
  111. }
  112. return pfd;
  113. }
  114. /**
  115. * Method to resolve the display name of a content URI.
  116. *
  117. * @param uri the content URI to be resolved.
  118. * @param contentResolver the content resolver to query.
  119. * @param columnField the column field to query.
  120. * @return the display name of the @code uri if present in the database
  121. * or an empty string otherwise.
  122. */
  123. public static String getDisplayName(
  124. Uri uri, ContentResolver contentResolver, String columnField) {
  125. if (contentResolver == null || uri == null) return "";
  126. Cursor cursor = null;
  127. try {
  128. cursor = contentResolver.query(uri, null, null, null, null);
  129. if (cursor != null && cursor.getCount() >= 1) {
  130. cursor.moveToFirst();
  131. int index = cursor.getColumnIndex(columnField);
  132. if (index > -1) return cursor.getString(index);
  133. }
  134. } catch (NullPointerException e) {
  135. // Some android models don't handle the provider call correctly.
  136. // see crbug.com/345393
  137. return "";
  138. } finally {
  139. if (cursor != null) cursor.close();
  140. }
  141. return "";
  142. }
  143. }