Skip to content

Commit

Permalink
Using AssetFileDescriptor instead of ParcelFileDescriptor.
Browse files Browse the repository at this point in the history
  • Loading branch information
MohitMaliDeveloper committed Nov 1, 2023
1 parent 8e7ce9e commit a4f0132
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import android.content.Intent
import android.content.ServiceConnection
import android.content.SharedPreferences
import android.content.pm.PackageManager
import android.content.res.AssetFileDescriptor
import android.content.res.Configuration
import android.graphics.Canvas
import android.graphics.Rect
Expand All @@ -38,7 +39,6 @@ import android.os.CountDownTimer
import android.os.Handler
import android.os.IBinder
import android.os.Looper
import android.os.ParcelFileDescriptor
import android.provider.Settings
import android.util.AttributeSet
import android.util.Log
Expand Down Expand Up @@ -1390,14 +1390,14 @@ abstract class CoreReaderFragment :
protected fun openZimFile(
file: File?,
isCustomApp: Boolean = false,
parcelFileDescriptor: ParcelFileDescriptor? = null
assetFileDescriptor: AssetFileDescriptor? = null
) {
if (hasPermission(Manifest.permission.READ_EXTERNAL_STORAGE) || isCustomApp) {
if (file?.isFileExist() == true) {
openAndSetInContainer(file = file)
updateTitle()
} else if (parcelFileDescriptor != null) {
openAndSetInContainer(parcelFileDescriptor = parcelFileDescriptor)
} else if (assetFileDescriptor != null) {
openAndSetInContainer(assetFileDescriptor = assetFileDescriptor)
updateTitle()
} else {
Log.w(TAG_KIWIX, "ZIM file doesn't exist at " + file?.absolutePath)
Expand Down Expand Up @@ -1429,7 +1429,7 @@ abstract class CoreReaderFragment :

private fun openAndSetInContainer(
file: File? = null,
parcelFileDescriptor: ParcelFileDescriptor? = null
assetFileDescriptor: AssetFileDescriptor? = null
) {
try {
if (isNotPreviouslyOpenZim(file?.canonicalPath)) {
Expand All @@ -1439,8 +1439,8 @@ abstract class CoreReaderFragment :
e.printStackTrace()
}
zimReaderContainer?.let { zimReaderContainer ->
if (parcelFileDescriptor != null) {
zimReaderContainer.setZimFileDescriptor(parcelFileDescriptor)
if (assetFileDescriptor != null) {
zimReaderContainer.setZimFileDescriptor(assetFileDescriptor)
} else {
zimReaderContainer.setZimFile(file)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ private const val TAG = "ZimFileReader"

class ZimFileReader constructor(
val zimFile: File?,
val parcelFileDescriptor: ParcelFileDescriptor? = null,
private val assetFileDescriptor: AssetFileDescriptor? = null,
private val jniKiwixReader: Archive,
private val nightModeConfig: NightModeConfig,
private val searcher: SuggestionSearcher = SuggestionSearcher(jniKiwixReader)
) {
interface Factory {
fun create(file: File): ZimFileReader?
fun create(parcelFileDescriptor: ParcelFileDescriptor): ZimFileReader?
fun create(assetFileDescriptor: AssetFileDescriptor): ZimFileReader?

class Impl @Inject constructor(private val nightModeConfig: NightModeConfig) :
Factory {
Expand All @@ -79,13 +79,17 @@ class ZimFileReader constructor(
null
}

override fun create(parcelFileDescriptor: ParcelFileDescriptor): ZimFileReader? =
override fun create(assetFileDescriptor: AssetFileDescriptor): ZimFileReader? =
try {
ZimFileReader(
null,
parcelFileDescriptor,
assetFileDescriptor,
nightModeConfig = nightModeConfig,
jniKiwixReader = Archive(parcelFileDescriptor.fileDescriptor)
jniKiwixReader = Archive(
assetFileDescriptor.parcelFileDescriptor.dup().fileDescriptor,
assetFileDescriptor.startOffset,
assetFileDescriptor.length
)
).also {
Log.e(TAG, "create: with fileDescriptor")
}
Expand Down Expand Up @@ -307,6 +311,7 @@ class ZimFileReader constructor(
fun dispose() {
jniKiwixReader.dispose()
searcher.dispose()
assetFileDescriptor?.parcelFileDescriptor?.detachFd()
}

@Suppress("TooGenericExceptionCaught")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
package org.kiwix.kiwixmobile.core.reader

import android.os.ParcelFileDescriptor
import android.content.res.AssetFileDescriptor
import android.webkit.WebResourceResponse
import org.kiwix.kiwixmobile.core.extensions.isFileExist
import org.kiwix.kiwixmobile.core.reader.ZimFileReader.Factory
Expand All @@ -43,10 +43,10 @@ class ZimReaderContainer @Inject constructor(private val zimFileReaderFactory: F
else null
}

fun setZimFileDescriptor(parcelFileDescriptor: ParcelFileDescriptor) {
fun setZimFileDescriptor(assetFileDescriptor: AssetFileDescriptor) {
zimFileReader =
if (parcelFileDescriptor.fileDescriptor.valid())
zimFileReaderFactory.create(parcelFileDescriptor)
if (assetFileDescriptor.parcelFileDescriptor.dup().fileDescriptor.valid())
zimFileReaderFactory.create(assetFileDescriptor)
else null
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package org.kiwix.kiwixmobile.custom.main

import android.content.Context
import android.content.pm.PackageManager
import android.os.ParcelFileDescriptor
import android.content.res.AssetFileDescriptor
import android.util.Log
import androidx.core.content.ContextCompat
import org.kiwix.kiwixmobile.custom.BuildConfig
Expand All @@ -43,24 +43,23 @@ class CustomFileValidator @Inject constructor(private val context: Context) {
private fun detectInstallationState(
obbFiles: List<File> = obbFiles(),
zimFiles: List<File> = zimFiles(),
assetFileDescriptor: ParcelFileDescriptor? = getParcelFileDescriptorFromPlayAssetDelivery()
assetFileDescriptor: AssetFileDescriptor? = getAssetFileDescriptorFromPlayAssetDelivery()
): ValidationState {
return when {
assetFileDescriptor != null -> HasFile(null, assetFileDescriptor)
// obbFiles.isNotEmpty() && zimFiles().isNotEmpty() -> HasBothFiles(obbFiles[0], zimFiles[0])
// obbFiles.isNotEmpty() -> HasFile(obbFiles[0])
// zimFiles.isNotEmpty() -> HasFile(zimFiles[0])
obbFiles.isNotEmpty() && zimFiles().isNotEmpty() -> HasBothFiles(obbFiles[0], zimFiles[0])
obbFiles.isNotEmpty() -> HasFile(obbFiles[0])
zimFiles.isNotEmpty() -> HasFile(zimFiles[0])
else -> HasNothing
}
}

@Suppress("NestedBlockDepth", "MagicNumber")
private fun getParcelFileDescriptorFromPlayAssetDelivery(): ParcelFileDescriptor? {
@Suppress("MagicNumber")
private fun getAssetFileDescriptorFromPlayAssetDelivery(): AssetFileDescriptor? {
try {
val context = context.createPackageContext(context.packageName, 0)
val assetManager = context.assets
val assetFileDescriptor = assetManager.openFd(BuildConfig.PLAY_ASSET_FILE)
return assetFileDescriptor.parcelFileDescriptor
return assetManager.openFd(BuildConfig.PLAY_ASSET_FILE)
} catch (packageNameNotFoundException: PackageManager.NameNotFoundException) {
Log.w(
"ASSET_PACKAGE_DELIVERY",
Expand Down Expand Up @@ -105,7 +104,7 @@ class CustomFileValidator @Inject constructor(private val context: Context) {

sealed class ValidationState {
data class HasBothFiles(val obbFile: File, val zimFile: File) : ValidationState()
data class HasFile(val file: File?, val parcelFileDescriptor: ParcelFileDescriptor? = null) :
data class HasFile(val file: File?, val assetFileDescriptor: AssetFileDescriptor? = null) :
ValidationState()

object HasNothing : ValidationState()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ class CustomReaderFragment : CoreReaderFragment() {
onFilesFound = {
when (it) {
is ValidationState.HasFile -> {
if (it.parcelFileDescriptor != null) {
openZimFile(null, true, it.parcelFileDescriptor)
if (it.assetFileDescriptor != null) {
openZimFile(null, true, it.assetFileDescriptor)
} else {
openZimFile(it.file, true)
}
Expand Down

0 comments on commit a4f0132

Please sign in to comment.