Enhanced ZIM file support for Android 10 and above devices, including USB-OTG integration.

* Previously we can only open zim files from `USB-OTG` in android 10 and below devices, because `context.getExternalFilesDirs("")` does not provide the USB path in android 10 and above devices for security reasons. There is no direct way to access the USB-OTG on these devices, but USB-OTG are mounted under the `/mnt/media_rw` directory, so now we are directly using this to open zim files from USB drive for android 10 and above devices.
This commit is contained in:
MohitMali 2023-08-25 12:33:31 +05:30 committed by Kelson
parent 6ec78d0ed9
commit c1cbebd64f
2 changed files with 19 additions and 4 deletions

View File

@ -36,6 +36,7 @@
<ID>MaxLineLength:MetaLinkNetworkEntityTest.kt$MetaLinkNetworkEntityTest$"http://www.mirrorservice.org/sites/download.kiwix.org/zim/wikipedia/wikipedia_af_all_nopic_2016-05.zim"</ID>
<ID>MaxLineLength:NetworkUtilsTest.kt$NetworkUtilsTest$// Here the Method should return the substring between the first '?' character and the nearest '/' character preceeding it</ID>
<ID>NestedBlockDepth:FileUtils.kt$FileUtils$@JvmStatic @Synchronized fun deleteZimFile(path: String)</ID>
<ID>NestedBlockDepth:FileUtils.kt$FileUtils$@JvmStatic fun getLocalFilePathByUri( context: Context, uri: Uri ): String?</ID>
<ID>NestedBlockDepth:ImageUtils.kt$ImageUtils$private fun getBitmapFromView(width: Int, height: Int, viewToDrawFrom: View): Bitmap?</ID>
<ID>NestedBlockDepth:JNIInitialiser.kt$JNIInitialiser$private fun loadICUData(context: Context): String?</ID>
<ID>NestedBlockDepth:OnSwipeTouchListener.kt$OnSwipeTouchListener.GestureListener$override fun onFling( e1: MotionEvent, e2: MotionEvent, velocityX: Float, velocityY: Float ): Boolean</ID>

View File

@ -125,7 +125,12 @@ object FileUtils {
return "${Environment.getExternalStorageDirectory()}/${documentId[1]}"
}
return try {
"${getSdCardMainPath(context, documentId[0])}/${documentId[1]}"
var sdCardOrUsbMainPath = getSdCardOrUSBMainPath(context, documentId[0])
if (sdCardOrUsbMainPath == null) {
// USB sticks are mounted under the `/mnt/media_rw` directory.
sdCardOrUsbMainPath = "/mnt/media_rw/${documentId[0]}"
}
"$sdCardOrUsbMainPath/${documentId[1]}"
} catch (ignore: Exception) {
null
}
@ -270,11 +275,20 @@ object FileUtils {
fun isValidZimFile(filePath: String): Boolean =
filePath.endsWith(".zim") || filePath.endsWith(".zimaa")
/**
* Get the main storage path for a given storage name (SD card or USB stick).
*
* @param context The application context.
* @param storageName The name of the storage (e.g., "sdcard" or "usbstick").
* @return The main storage path for the given storage name,
* or null if the path is a USB path on Android 10 and above
* (due to limitations in `context.getExternalFilesDirs("")` behavior).
*/
@JvmStatic
fun getSdCardMainPath(context: Context, storageName: String) =
fun getSdCardOrUSBMainPath(context: Context, storageName: String) =
context.getExternalFilesDirs("")
.first { it.path.contains(storageName) }
.path.substringBefore(context.getString(R.string.android_directory_seperator))
.firstOrNull { it.path.contains(storageName) }
?.path?.substringBefore(context.getString(R.string.android_directory_seperator))
@SuppressLint("WrongConstant")
@JvmStatic