Merge remote-tracking branch 'upstream/develop' into uploadNewFolder
[pub/Android/ownCloud.git] / src / com / owncloud / android / utils / UriUtils.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012-2014 ownCloud Inc.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 */
17
18 package com.owncloud.android.utils;
19
20 import android.content.Context;
21 import android.database.Cursor;
22 import android.net.Uri;
23
24
25 /**
26 * A helper class for some Uri operations.
27 */
28 public class UriUtils {
29
30 public static final String URI_CONTENT_SCHEME = "content://";
31
32
33 /**
34 * Get the value of the data column for this Uri. This is useful for
35 * MediaStore Uris, and other file-based ContentProviders.
36 *
37 * @param context The context.
38 * @param uri The Uri to query.
39 * @param selection (Optional) Filter used in the query.
40 * @param selectionArgs (Optional) Selection arguments used in the query.
41 * @return The value of the _data column, which is typically a file path.
42 */
43 public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
44
45 Cursor cursor = null;
46 final String column = "_data";
47 final String[] projection = { column };
48
49 try {
50 cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
51 if (cursor != null && cursor.moveToFirst()) {
52
53 final int column_index = cursor.getColumnIndexOrThrow(column);
54 return cursor.getString(column_index);
55 }
56 } finally {
57 if (cursor != null)
58 cursor.close();
59 }
60 return null;
61 }
62
63 /**
64 * @param uri The Uri to check.
65 * @return Whether the Uri authority is ExternalStorageProvider.
66 */
67 public static boolean isExternalStorageDocument(Uri uri) {
68 return "com.android.externalstorage.documents".equals(uri.getAuthority());
69 }
70
71 /**
72 * @param uri The Uri to check.
73 * @return Whether the Uri authority is DownloadsProvider.
74 */
75 public static boolean isDownloadsDocument(Uri uri) {
76 return "com.android.providers.downloads.documents".equals(uri.getAuthority());
77 }
78
79 /**
80 * @param uri The Uri to check.
81 * @return Whether the Uri authority is MediaProvider.
82 */
83 public static boolean isMediaDocument(Uri uri) {
84 return "com.android.providers.media.documents".equals(uri.getAuthority());
85 }
86
87 /**
88 * @param uri The Uri to check.
89 * @return Whether the Uri authority is Google Photos.
90 */
91 public static boolean isGooglePhotosUri(Uri uri) {
92 return "com.google.android.apps.photos.content".equals(uri.getAuthority());
93 }
94
95 /**
96 *
97 * @param uri The Uri to check.
98 * @return Whether the Uri is from a content provider as kind "content://..."
99 */
100 public static boolean isContentDocument(Uri uri) {
101 return uri.toString().startsWith(URI_CONTENT_SCHEME);
102 }
103 }