Skip to content

Commit

Permalink
Merge pull request #3953 from CalebKL/task/download-fragment-crash-fix
Browse files Browse the repository at this point in the history
Task/download fragment crash fix
  • Loading branch information
kelson42 authored Jul 26, 2024
2 parents 7dcf042 + f29634e commit 71d8a18
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
1 change: 1 addition & 0 deletions app/multidex-instrumentation-config.pro
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
-keep class org.hamcrest.Matchers
-keep class org.kiwix.kiwixmobile.** { *; }
-keep class org.mockito.** { *; }
-keep class androidx.core.app.CoreComponentFactory { *; }
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ import androidx.lifecycle.ViewModel
import io.reactivex.Flowable
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.disposables.Disposable
import io.reactivex.exceptions.UndeliverableException
import io.reactivex.functions.BiFunction
import io.reactivex.functions.Function6
import io.reactivex.plugins.RxJavaPlugins
import io.reactivex.processors.BehaviorProcessor
import io.reactivex.processors.PublishProcessor
import io.reactivex.schedulers.Schedulers
Expand All @@ -47,6 +49,7 @@ import org.kiwix.kiwixmobile.core.extensions.calculateSearchMatches
import org.kiwix.kiwixmobile.core.extensions.registerReceiver
import org.kiwix.kiwixmobile.core.utils.BookUtils
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
import org.kiwix.kiwixmobile.core.utils.files.Log
import org.kiwix.kiwixmobile.core.utils.files.ScanningProgressListener
import org.kiwix.kiwixmobile.core.zim_manager.Language
import org.kiwix.kiwixmobile.core.zim_manager.fileselect_view.SelectionMode.MULTI
Expand Down Expand Up @@ -74,13 +77,16 @@ import org.kiwix.kiwixmobile.zimManager.libraryView.adapter.LibraryListItem
import org.kiwix.kiwixmobile.zimManager.libraryView.adapter.LibraryListItem.BookItem
import org.kiwix.kiwixmobile.zimManager.libraryView.adapter.LibraryListItem.DividerItem
import org.kiwix.kiwixmobile.zimManager.libraryView.adapter.LibraryListItem.LibraryDownloadItem
import java.io.IOException
import java.util.LinkedList
import java.util.Locale
import java.util.concurrent.TimeUnit.MILLISECONDS
import javax.inject.Inject

const val DEFAULT_PROGRESS = 0
const val MAX_PROGRESS = 100
private const val TAG_RX_JAVA_DEFAULT_ERROR_HANDLER = "RxJavaDefaultErrorHandler"

class ZimManageViewModel @Inject constructor(
private val downloadDao: FetchDownloadDao,
private val bookDao: NewBookDao,
Expand Down Expand Up @@ -156,7 +162,9 @@ class ZimManageViewModel @Inject constructor(
updateNetworkStates(),
requestsAndConnectivtyChangesToLibraryRequests(networkLibrary),
fileSelectActions()
)
).also {
setUpUncaughtErrorHandlerForOnlineLibrary(networkLibrary)
}
}

private fun fileSelectActions() = fileSelectActions.subscribe({
Expand Down Expand Up @@ -288,6 +296,11 @@ class ZimManageViewModel @Inject constructor(
Function6(::combineLibrarySources)
)
.doOnNext { libraryListIsRefreshing.postValue(false) }
.doOnError { throwable ->
if (throwable is OutOfMemoryError) {
Log.e("ZimManageViewModel", "Error----${throwable.printStackTrace()}")
}
}
.subscribeOn(Schedulers.io())
.subscribe(
libraryItems::postValue,
Expand Down Expand Up @@ -512,4 +525,35 @@ class ZimManageViewModel @Inject constructor(
}
)
}

private fun setUpUncaughtErrorHandlerForOnlineLibrary(
library: PublishProcessor<LibraryNetworkEntity>
) {
RxJavaPlugins.setErrorHandler { exception ->
if (exception is RuntimeException && exception.cause == IOException()) {
Log.i(
TAG_RX_JAVA_DEFAULT_ERROR_HANDLER,
"Caught undeliverable exception: ${exception.cause}"
)
}
when (exception) {
is UndeliverableException -> {
library.onNext(
LibraryNetworkEntity().apply { book = LinkedList() }
).also {
Log.i(
TAG_RX_JAVA_DEFAULT_ERROR_HANDLER,
"Caught undeliverable exception: ${exception.cause}"
)
}
}

else -> {
Thread.currentThread().also { thread ->
thread.uncaughtExceptionHandler?.uncaughtException(thread, exception)
}
}
}
}
}
}
1 change: 1 addition & 0 deletions core/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
-keep class androidx.core.app.CoreComponentFactory { *; }
1 change: 1 addition & 0 deletions core/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
android:fullBackupContent="@xml/backup_rules"
android:dataExtractionRules = "@xml/data_extraction_rules"
android:hardwareAccelerated="true"
android:largeHeap="true"
android:requestLegacyExternalStorage="true"
android:resizeableActivity="true"
android:supportsRtl="true"
Expand Down

0 comments on commit 71d8a18

Please sign in to comment.