From 24e10caf19b8a40b80caca985abdd5995bfb1bb9 Mon Sep 17 00:00:00 2001 From: MohitMaliFtechiz Date: Fri, 29 Dec 2023 15:39:18 +0530 Subject: [PATCH] Fixed: Application crashing below API level 29 when we select the external storage. * The issue was with the `FileObserver` class constructor we used, which is introduced in SDK 29. As a result, it is not available for older versions, leading to a `NoSuchMethodError` when attempting to access it. This caused the application to crash below API level 29 when selecting external storage. --- .../kiwixmobile/zimManager/Fat32Checker.kt | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/zimManager/Fat32Checker.kt b/app/src/main/java/org/kiwix/kiwixmobile/zimManager/Fat32Checker.kt index db2e66e14..7b6f3ef41 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/zimManager/Fat32Checker.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/zimManager/Fat32Checker.kt @@ -18,6 +18,7 @@ package org.kiwix.kiwixmobile.zimManager import android.annotation.SuppressLint +import android.os.Build import android.os.FileObserver import io.reactivex.Flowable import io.reactivex.functions.BiFunction @@ -63,11 +64,20 @@ class Fat32Checker constructor( } private fun fileObserver(it: String): FileObserver = - object : FileObserver(File(it), MOVED_FROM or DELETE) { - override fun onEvent(event: Int, path: String?) { - requestCheckSystemFileType.onNext(Unit) - } - }.apply { startWatching() } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + object : FileObserver(File(it), MOVED_FROM or DELETE) { + override fun onEvent(event: Int, path: String?) { + requestCheckSystemFileType.onNext(Unit) + } + }.apply { startWatching() } + } else { + @Suppress("DEPRECATION") + object : FileObserver(it, MOVED_FROM or DELETE) { + override fun onEvent(event: Int, path: String?) { + requestCheckSystemFileType.onNext(Unit) + } + }.apply { startWatching() } + } private fun toFileSystemState(it: String) = when {