-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bfd0449
commit 0b50844
Showing
4 changed files
with
215 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
197 changes: 197 additions & 0 deletions
197
app/src/main/java/space/celestia/mobilecelestia/utils/RealPathUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
/* | ||
* RealPathUtils.kt | ||
* | ||
* Copyright (C) 2001-2020, the Celestia Development Team | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU General Public License | ||
* as published by the Free Software Foundation; either version 2 | ||
* of the License, or (at your option) any later version. | ||
*/ | ||
|
||
package space.celestia.mobilecelestia.utils | ||
|
||
import android.annotation.SuppressLint | ||
import android.content.ContentUris | ||
import android.content.Context | ||
import android.database.Cursor | ||
import android.net.Uri | ||
import android.os.Environment | ||
import android.provider.DocumentsContract | ||
import android.provider.MediaStore | ||
import space.celestia.mobilecelestia.BuildConfig | ||
import java.io.File | ||
|
||
object RealPathUtils { | ||
fun getRealPath( | ||
context: Context, | ||
fileUri: Uri | ||
): String? { | ||
return getRealPathFromURIAboveAPI19(context, fileUri) | ||
} | ||
|
||
@SuppressLint("NewApi") | ||
private fun getRealPathFromURIAboveAPI19( | ||
context: Context, | ||
uri: Uri | ||
): String? { | ||
// DocumentProvider | ||
when { | ||
DocumentsContract.isDocumentUri(context, uri) -> { | ||
// ExternalStorageProvider | ||
when { | ||
isExternalStorageDocument(uri) -> { | ||
val docId = DocumentsContract.getDocumentId(uri) | ||
val split = docId.split(":").toTypedArray() | ||
val type = split[0] | ||
|
||
// This is for checking Main Memory | ||
return if ("primary".equals(type, ignoreCase = true)) { | ||
if (split.size > 1) { | ||
getExternalStorageDirectory(context) + "/" + split[1] | ||
} else { | ||
getExternalStorageDirectory(context) + "/" | ||
} | ||
// This is for checking SD Card | ||
} else { | ||
"storage" + "/" + docId.replace(":", "/") | ||
} | ||
} | ||
isDownloadsDocument(uri) -> { | ||
val fileName = getFilePath(context, uri) | ||
if (fileName != null) { | ||
return getExternalStorageDirectory(context) + "/Download/" + fileName | ||
} | ||
val id = DocumentsContract.getDocumentId(uri) | ||
val contentUri = ContentUris.withAppendedId( | ||
Uri.parse("content://downloads/public_downloads"), | ||
java.lang.Long.valueOf(id) | ||
) | ||
return getDataColumn(context, contentUri, null, null) | ||
} | ||
isMediaDocument(uri) -> { | ||
val docId = DocumentsContract.getDocumentId(uri) | ||
val split = docId.split(":").toTypedArray() | ||
val type = split[0] | ||
var contentUri: Uri? = null | ||
when (type) { | ||
"image" -> { | ||
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI | ||
} | ||
"video" -> { | ||
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI | ||
} | ||
"audio" -> { | ||
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI | ||
} | ||
} | ||
val selection = "_id=?" | ||
val selectionArgs = arrayOf( | ||
split[1] | ||
) | ||
return getDataColumn(context, contentUri, selection, selectionArgs) | ||
} | ||
} | ||
} | ||
"content".equals(uri.scheme, ignoreCase = true) -> { | ||
// Return the remote address | ||
return if (isGooglePhotosUri(uri)) uri.lastPathSegment else getDataColumn( | ||
context, | ||
uri, | ||
null, | ||
null | ||
) | ||
} | ||
"file".equals(uri.scheme, ignoreCase = true) -> { | ||
return uri.path | ||
} | ||
} | ||
return null | ||
} | ||
|
||
private fun getExternalStorageDirectory(context: Context): String { | ||
val rootPath = context.getExternalFilesDir(null)?.absolutePath ?: return "" | ||
val extra = "/Android/data/" + BuildConfig.APPLICATION_ID + File.separator + "files" | ||
return rootPath.replace(extra, "") | ||
} | ||
|
||
private fun getDataColumn( | ||
context: Context, uri: Uri?, selection: String?, | ||
selectionArgs: Array<String>? | ||
): String? { | ||
var cursor: Cursor? = null | ||
val column = "_data" | ||
val projection = arrayOf( | ||
column | ||
) | ||
try { | ||
cursor = context.contentResolver.query( | ||
uri!!, projection, selection, selectionArgs, | ||
null | ||
) | ||
if (cursor != null && cursor.moveToFirst()) { | ||
val index: Int = cursor.getColumnIndexOrThrow(column) | ||
return cursor.getString(index) | ||
} | ||
} finally { | ||
cursor?.close() | ||
} | ||
return null | ||
} | ||
|
||
|
||
private fun getFilePath( | ||
context: Context, | ||
uri: Uri | ||
): String? { | ||
var cursor: Cursor? = null | ||
val projection = arrayOf( | ||
MediaStore.MediaColumns.DISPLAY_NAME | ||
) | ||
try { | ||
cursor = context.contentResolver.query( | ||
uri, projection, null, null, | ||
null | ||
) | ||
if (cursor != null && cursor.moveToFirst()) { | ||
val index: Int = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DISPLAY_NAME) | ||
return cursor.getString(index) | ||
} | ||
} finally { | ||
cursor?.close() | ||
} | ||
return null | ||
} | ||
|
||
/** | ||
* @param uri The Uri to check. | ||
* @return Whether the Uri authority is ExternalStorageProvider. | ||
*/ | ||
private fun isExternalStorageDocument(uri: Uri): Boolean { | ||
return "com.android.externalstorage.documents" == uri.authority | ||
} | ||
|
||
/** | ||
* @param uri The Uri to check. | ||
* @return Whether the Uri authority is DownloadsProvider. | ||
*/ | ||
private fun isDownloadsDocument(uri: Uri): Boolean { | ||
return "com.android.providers.downloads.documents" == uri.authority | ||
} | ||
|
||
/** | ||
* @param uri The Uri to check. | ||
* @return Whether the Uri authority is MediaProvider. | ||
*/ | ||
private fun isMediaDocument(uri: Uri): Boolean { | ||
return "com.android.providers.media.documents" == uri.authority | ||
} | ||
|
||
/** | ||
* @param uri The Uri to check. | ||
* @return Whether the Uri authority is Google Photos. | ||
*/ | ||
private fun isGooglePhotosUri(uri: Uri): Boolean { | ||
return "com.google.android.apps.photos.content" == uri.authority | ||
} | ||
} |