Upstream version 9.37.197.0
[platform/framework/web/crosswalk.git] / src / base / android / java / src / org / chromium / base / ContentUriUtils.java
index 74c74f4..b806b47 100644 (file)
@@ -6,6 +6,7 @@ package org.chromium.base;
 
 import android.content.ContentResolver;
 import android.content.Context;
+import android.database.Cursor;
 import android.net.Uri;
 import android.os.ParcelFileDescriptor;
 import android.util.Log;
@@ -13,7 +14,7 @@ import android.util.Log;
 /**
  * This class provides methods to access content URI schemes.
  */
-abstract class ContentUriUtils {
+public abstract class ContentUriUtils {
     private static final String TAG = "ContentUriUtils";
 
     // Prevent instantiation.
@@ -71,4 +72,35 @@ abstract class ContentUriUtils {
         }
         return pfd;
     }
+
+    /**
+     * Method to resolve the display name of a content URI.
+     *
+     * @param uri the content URI to be resolved.
+     * @param contentResolver the content resolver to query.
+     * @param columnField the column field to query.
+     * @returns the display name of the @code uri if present in the database
+     *  or an empty string otherwise.
+     */
+    public static String getDisplayName(
+            Uri uri, ContentResolver contentResolver, String columnField) {
+        if (contentResolver == null || uri == null) return "";
+        Cursor cursor = null;
+        try {
+            cursor = contentResolver.query(uri, null, null, null, null);
+
+            if (cursor != null && cursor.getCount() >= 1) {
+                cursor.moveToFirst();
+                int index = cursor.getColumnIndex(columnField);
+                if (index > -1) return cursor.getString(index);
+            }
+        } catch (NullPointerException e) {
+            // Some android models don't handle the provider call correctly.
+            // see crbug.com/345393
+            return "";
+        } finally {
+            if (cursor != null) cursor.close();
+        }
+        return "";
+    }
 }