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