|
@@ -13,116 +13,78 @@
|
|
* GNU General Public License for more details.
|
|
* GNU General Public License for more details.
|
|
*
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* You should have received a copy of the GNU General Public License
|
|
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
+ * along with this program. If not, see <http:></http:>//www.gnu.org/licenses/>.
|
|
*/
|
|
*/
|
|
|
|
+package com.owncloud.android.utils
|
|
|
|
|
|
-package com.owncloud.android.utils;
|
|
|
|
-
|
|
|
|
-import android.content.ContentResolver;
|
|
|
|
-import android.content.Context;
|
|
|
|
-import android.database.Cursor;
|
|
|
|
-import android.net.Uri;
|
|
|
|
-import android.provider.MediaStore;
|
|
|
|
-import android.webkit.MimeTypeMap;
|
|
|
|
-
|
|
|
|
-import com.owncloud.android.lib.common.utils.Log_OC;
|
|
|
|
-
|
|
|
|
-import java.util.Locale;
|
|
|
|
-
|
|
|
|
|
|
+import android.content.ContentResolver
|
|
|
|
+import android.content.Context
|
|
|
|
+import android.net.Uri
|
|
|
|
+import android.provider.MediaStore
|
|
|
|
+import com.owncloud.android.lib.common.utils.Log_OC
|
|
|
|
|
|
/**
|
|
/**
|
|
* A helper class for some Uri operations.
|
|
* A helper class for some Uri operations.
|
|
*/
|
|
*/
|
|
-public final class UriUtils {
|
|
|
|
-
|
|
|
|
- private static final String TAG = UriUtils.class.getSimpleName();
|
|
|
|
-
|
|
|
|
- public static final String URI_CONTENT_SCHEME = "content://";
|
|
|
|
-
|
|
|
|
- private UriUtils() {
|
|
|
|
- // utility class -> private constructor
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public static String getDisplayNameForUri(Uri uri, Context context) {
|
|
|
|
|
|
+object UriUtils {
|
|
|
|
+ private val TAG = UriUtils::class.java.simpleName
|
|
|
|
+ const val URI_CONTENT_SCHEME = "content://"
|
|
|
|
|
|
- if (uri == null || context == null) {
|
|
|
|
- throw new IllegalArgumentException("Received NULL!");
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- String displayName;
|
|
|
|
-
|
|
|
|
- if (!ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) {
|
|
|
|
- displayName = uri.getLastPathSegment(); // ready to return
|
|
|
|
|
|
+ @JvmStatic
|
|
|
|
+ fun getDisplayNameForUri(uri: Uri?, context: Context?): String? {
|
|
|
|
+ require(!(uri == null || context == null)) { "Received NULL!" }
|
|
|
|
+ var displayName: String?
|
|
|
|
|
|
- } else {
|
|
|
|
|
|
+ if (ContentResolver.SCHEME_CONTENT == uri.scheme) {
|
|
// content: URI
|
|
// content: URI
|
|
-
|
|
|
|
- displayName = getDisplayNameFromContentResolver(uri, context);
|
|
|
|
-
|
|
|
|
- try {
|
|
|
|
- if (displayName == null) {
|
|
|
|
- // last chance to have a name
|
|
|
|
- displayName = uri.getLastPathSegment().replaceAll("\\s", "");
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // Add best possible extension
|
|
|
|
- int index = displayName.lastIndexOf('.');
|
|
|
|
- if (index == -1 || MimeTypeMap.getSingleton().
|
|
|
|
- getMimeTypeFromExtension(displayName.substring(index + 1).toLowerCase(Locale.ROOT)) == null) {
|
|
|
|
- String mimeType = context.getContentResolver().getType(uri);
|
|
|
|
- String extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType);
|
|
|
|
- if (extension != null) {
|
|
|
|
- displayName += "." + extension;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- Log_OC.e(TAG, "No way to get a display name for " + uri.toString());
|
|
|
|
|
|
+ displayName = getDisplayNameFromContentResolver(uri, context)
|
|
|
|
+ if (displayName == null) {
|
|
|
|
+ // last chance to have a name
|
|
|
|
+ displayName = uri.lastPathSegment?.replace("\\s".toRegex(), "")
|
|
}
|
|
}
|
|
|
|
+ } else {
|
|
|
|
+ displayName = uri.lastPathSegment // ready to return
|
|
}
|
|
}
|
|
|
|
|
|
// Replace path separator characters to avoid inconsistent paths
|
|
// Replace path separator characters to avoid inconsistent paths
|
|
- return displayName.replaceAll("/", "-");
|
|
|
|
|
|
+ return displayName?.replace("/".toRegex(), "-")
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
- private static String getDisplayNameFromContentResolver(Uri uri, Context context) {
|
|
|
|
- String displayName = null;
|
|
|
|
- String mimeType = context.getContentResolver().getType(uri);
|
|
|
|
|
|
+ private fun getDisplayNameFromContentResolver(uri: Uri, context: Context): String? {
|
|
|
|
+ var displayName: String? = null
|
|
|
|
+ val mimeType = context.contentResolver.getType(uri)
|
|
if (mimeType != null) {
|
|
if (mimeType != null) {
|
|
- String displayNameColumn;
|
|
|
|
- if (MimeTypeUtil.isImage(mimeType)) {
|
|
|
|
- displayNameColumn = MediaStore.Images.ImageColumns.DISPLAY_NAME;
|
|
|
|
-
|
|
|
|
- } else if (MimeTypeUtil.isVideo(mimeType)) {
|
|
|
|
- displayNameColumn = MediaStore.Video.VideoColumns.DISPLAY_NAME;
|
|
|
|
-
|
|
|
|
- } else if (MimeTypeUtil.isAudio(mimeType)) {
|
|
|
|
- displayNameColumn = MediaStore.Audio.AudioColumns.DISPLAY_NAME;
|
|
|
|
- } else {
|
|
|
|
- displayNameColumn = MediaStore.Files.FileColumns.DISPLAY_NAME;
|
|
|
|
|
|
+ val displayNameColumn: String = when {
|
|
|
|
+ MimeTypeUtil.isImage(mimeType) -> {
|
|
|
|
+ MediaStore.Images.ImageColumns.DISPLAY_NAME
|
|
|
|
+ }
|
|
|
|
+ MimeTypeUtil.isVideo(mimeType) -> {
|
|
|
|
+ MediaStore.Video.VideoColumns.DISPLAY_NAME
|
|
|
|
+ }
|
|
|
|
+ MimeTypeUtil.isAudio(mimeType) -> {
|
|
|
|
+ MediaStore.Audio.AudioColumns.DISPLAY_NAME
|
|
|
|
+ }
|
|
|
|
+ else -> {
|
|
|
|
+ MediaStore.Files.FileColumns.DISPLAY_NAME
|
|
|
|
+ }
|
|
}
|
|
}
|
|
-
|
|
|
|
- try (Cursor cursor = context.getContentResolver().query(
|
|
|
|
- uri,
|
|
|
|
- new String[]{displayNameColumn},
|
|
|
|
- null,
|
|
|
|
- null,
|
|
|
|
- null
|
|
|
|
- )) {
|
|
|
|
- if (cursor != null) {
|
|
|
|
- cursor.moveToFirst();
|
|
|
|
- displayName = cursor.getString(cursor.getColumnIndex(displayNameColumn));
|
|
|
|
|
|
+ try {
|
|
|
|
+ context.contentResolver.query(
|
|
|
|
+ uri, arrayOf(displayNameColumn),
|
|
|
|
+ null,
|
|
|
|
+ null,
|
|
|
|
+ null
|
|
|
|
+ ).use { cursor ->
|
|
|
|
+ if (cursor != null) {
|
|
|
|
+ cursor.moveToFirst()
|
|
|
|
+ displayName = cursor.getString(cursor.getColumnIndex(displayNameColumn))
|
|
|
|
+ }
|
|
}
|
|
}
|
|
-
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- Log_OC.e(TAG, "Could not retrieve display name for " + uri.toString());
|
|
|
|
|
|
+ } catch (e: Exception) {
|
|
|
|
+ Log_OC.e(TAG, "Could not retrieve display name for $uri")
|
|
// nothing else, displayName keeps null
|
|
// nothing else, displayName keeps null
|
|
-
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- return displayName;
|
|
|
|
|
|
+ return displayName
|
|
}
|
|
}
|
|
-
|
|
|
|
-
|
|
|
|
}
|
|
}
|