List view is shown completelly on the ShareFileFragment
[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.annotation.TargetApi;
23 import android.content.ContentUris;
24 import android.content.Context;
25 import android.database.Cursor;
26 import android.net.Uri;
27 import android.os.Build;
28 import android.os.Environment;
29 import android.provider.DocumentsContract;
30 import android.provider.MediaStore;
31 import android.provider.OpenableColumns;
32
33 import com.owncloud.android.MainApp;
34 import com.owncloud.android.lib.common.utils.Log_OC;
35
36 import java.io.File;
37
38
39 /**
40 * A helper class for some Uri operations.
41 */
42 public class UriUtils {
43
44 public static final String TAG = UriUtils.class.getSimpleName();
45
46 public static final String URI_CONTENT_SCHEME = "content://";
47
48
49 /**
50 * Get the value of the data column for this Uri. This is useful for
51 * MediaStore Uris, and other file-based ContentProviders.
52 *
53 * @param context The context.
54 * @param uri The Uri to query.
55 * @param selection (Optional) Filter used in the query.
56 * @param selectionArgs (Optional) Selection arguments used in the query.
57 * @return The value of the _data column, which is typically a file path.
58 */
59 public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
60
61 Cursor cursor = null;
62 final String column = "_data";
63 final String[] projection = { column };
64
65 try {
66 cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
67 if (cursor != null && cursor.moveToFirst()) {
68
69 final int column_index = cursor.getColumnIndexOrThrow(column);
70 return cursor.getString(column_index);
71 }
72 } finally {
73 if (cursor != null)
74 cursor.close();
75 }
76 return null;
77 }
78
79 /**
80 * @param uri The Uri to check.
81 * @return Whether the Uri authority is ExternalStorageProvider.
82 */
83 public static boolean isExternalStorageDocument(Uri uri) {
84 return "com.android.externalstorage.documents".equals(uri.getAuthority());
85 }
86
87 /**
88 * @param uri The Uri to check.
89 * @return Whether the Uri authority is DownloadsProvider.
90 */
91 public static boolean isDownloadsDocument(Uri uri) {
92 return "com.android.providers.downloads.documents".equals(uri.getAuthority());
93 }
94
95 /**
96 * @param uri The Uri to check.
97 * @return Whether the Uri authority is MediaProvider.
98 */
99 public static boolean isMediaDocument(Uri uri) {
100 return "com.android.providers.media.documents".equals(uri.getAuthority());
101 }
102
103 /**
104 * @param uri The Uri to check.
105 * @return Whether the Uri authority is Google Photos.
106 */
107 public static boolean isGooglePhotosUri(Uri uri) {
108 return "com.google.android.apps.photos.content".equals(uri.getAuthority());
109 }
110
111 /**
112 *
113 * @param uri The Uri to check.
114 * @return Whether the Uri is from a content provider as kind "content://..."
115 */
116 public static boolean isContentDocument(Uri uri) {
117 return uri.toString().startsWith(URI_CONTENT_SCHEME);
118 }
119
120
121 /**
122 * Translates a content:// URI referred to a local file file to a path on the local filesystem
123 *
124 * @param uri The URI to resolve
125 * @return The path in the file system to the content or null if it could not be found (not a file)
126 */
127 @TargetApi(Build.VERSION_CODES.KITKAT)
128 public static String getLocalPath(Uri uri, Context context) {
129 final boolean isKitKatOrLater = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
130
131 // DocumentProvider
132 if (isKitKatOrLater && DocumentsContract.isDocumentUri(context, uri)) {
133 // ExternalStorageProvider
134 if (UriUtils.isExternalStorageDocument(uri)) {
135 final String docId = DocumentsContract.getDocumentId(uri);
136 final String[] split = docId.split(":");
137 final String type = split[0];
138
139 if ("primary".equalsIgnoreCase(type)) {
140 return Environment.getExternalStorageDirectory() + "/" + split[1];
141 }
142 }
143 // DownloadsProvider
144 else if (UriUtils.isDownloadsDocument(uri)) {
145
146 final String id = DocumentsContract.getDocumentId(uri);
147 final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),
148 Long.valueOf(id));
149
150 return UriUtils.getDataColumn(context, contentUri, null, null);
151 }
152 // MediaProvider
153 else if (UriUtils.isMediaDocument(uri)) {
154 final String docId = DocumentsContract.getDocumentId(uri);
155 final String[] split = docId.split(":");
156 final String type = split[0];
157
158 Uri contentUri = null;
159 if ("image".equals(type)) {
160 contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
161 } else if ("video".equals(type)) {
162 contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
163 } else if ("audio".equals(type)) {
164 contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
165 }
166
167 final String selection = "_id=?";
168 final String[] selectionArgs = new String[] { split[1] };
169
170 return UriUtils.getDataColumn(context, contentUri, selection, selectionArgs);
171 }
172 // Documents providers returned as content://...
173 else if (UriUtils.isContentDocument(uri)) {
174 return uri.toString();
175 }
176 }
177 // MediaStore (and general)
178 else if ("content".equalsIgnoreCase(uri.getScheme())) {
179
180 // Return the remote address
181 if (UriUtils.isGooglePhotosUri(uri))
182 return uri.getLastPathSegment();
183
184 return UriUtils.getDataColumn(context, uri, null, null);
185 }
186 // File
187 else if ("file".equalsIgnoreCase(uri.getScheme())) {
188 return uri.getPath();
189 }
190 return null;
191 }
192
193 }