Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Chucker #193

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ android {

kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8.toString()
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
kotlinOptions.jvmTarget = jvmTarget
}
}
lint {
abortOnError true
Expand Down Expand Up @@ -200,6 +203,9 @@ dependencies {
implementation 'org.jsoup:jsoup:1.16.1'

debugImplementation "com.squareup.leakcanary:leakcanary-android:2.12"

debugImplementation "com.github.chuckerteam.chucker:library:3.5.2"
releaseImplementation "com.github.chuckerteam.chucker:library-no-op:3.5.2"
}

kapt {
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/java/com/boardgamegeek/BggApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.work.*
import com.boardgamegeek.extensions.*
import com.boardgamegeek.pref.SyncPrefs
import com.boardgamegeek.util.CrashReportingTree
import com.boardgamegeek.util.DebugCrashHandler
import com.boardgamegeek.util.RemoteConfig
import com.boardgamegeek.work.PlayUploadWorker
import com.boardgamegeek.work.SyncCollectionWorker
Expand Down Expand Up @@ -50,6 +51,7 @@ class BggApplication : MultiDexApplication(), Configuration.Provider {
initializeStetho()
initializeFirebase()
initializeTimber()
initializeChucker()
initializePicasso()
migrateData()

Expand Down Expand Up @@ -112,6 +114,15 @@ class BggApplication : MultiDexApplication(), Configuration.Provider {
}
}

private fun initializeChucker() {
if (BuildConfig.DEBUG) {
// initialize the exception handler
// HTTP client call interceptor is added in the NetworkModule
val systemHandler = Thread.getDefaultUncaughtExceptionHandler()
Thread.setDefaultUncaughtExceptionHandler(DebugCrashHandler(systemHandler, this))
}
}

private fun initializeStetho() {
if (BuildConfig.DEBUG) {
Stetho.initialize(
Expand Down
25 changes: 17 additions & 8 deletions app/src/main/java/com/boardgamegeek/di/NetworkModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import android.content.Context
import com.boardgamegeek.BuildConfig
import com.boardgamegeek.io.*
import com.boardgamegeek.repository.*
import com.chuckerteam.chucker.api.ChuckerCollector
import com.chuckerteam.chucker.api.ChuckerInterceptor
import com.facebook.stetho.okhttp3.StethoInterceptor
import dagger.Module
import dagger.Provides
Expand All @@ -32,26 +34,26 @@ object NetworkModule {
@Provides
@Singleton
@Named("noAuth")
fun provideHttpClient(): OkHttpClient = OkHttpClient.Builder()
fun provideHttpClient(@ApplicationContext context: Context): OkHttpClient = OkHttpClient.Builder()
.connectTimeout(HTTP_REQUEST_TIMEOUT_SEC, TimeUnit.SECONDS)
.readTimeout(HTTP_REQUEST_TIMEOUT_SEC, TimeUnit.SECONDS)
.writeTimeout(HTTP_REQUEST_TIMEOUT_SEC, TimeUnit.SECONDS)
.addInterceptor(UserAgentInterceptor())
.addInterceptor(RetryInterceptor(true))
.addLoggingInterceptor()
.addLoggingInterceptor(context)
.build()

@Provides
@Singleton
@Named("withAuth")
fun provideHttpClientWithAuth(@ApplicationContext context: Context?) = OkHttpClient.Builder()
fun provideHttpClientWithAuth(@ApplicationContext context: Context) = OkHttpClient.Builder()
.connectTimeout(HTTP_REQUEST_TIMEOUT_SEC, TimeUnit.SECONDS)
.readTimeout(HTTP_REQUEST_TIMEOUT_SEC, TimeUnit.SECONDS)
.writeTimeout(HTTP_REQUEST_TIMEOUT_SEC, TimeUnit.SECONDS)
.addInterceptor(UserAgentInterceptor(context))
.addInterceptor(AuthInterceptor(context))
.addInterceptor(RetryInterceptor(true))
.addLoggingInterceptor()
.addLoggingInterceptor(context)
.build()

@Provides
Expand All @@ -62,30 +64,37 @@ object NetworkModule {
.readTimeout(HTTP_REQUEST_TIMEOUT_SEC, TimeUnit.SECONDS)
.writeTimeout(HTTP_REQUEST_TIMEOUT_SEC, TimeUnit.SECONDS)
.addInterceptor(UserAgentInterceptor(context))
.addLoggingInterceptor()
.addLoggingInterceptor(context)
.cache(Cache(File(context.cacheDir, "http"), 10 * 1024 * 1024))
.build()

@Provides
@Singleton
@Named("without202Retry")
fun provideHttpClientWithout202Retry() = OkHttpClient.Builder()
fun provideHttpClientWithout202Retry(@ApplicationContext context: Context) = OkHttpClient.Builder()
.connectTimeout(HTTP_REQUEST_TIMEOUT_SEC, TimeUnit.SECONDS)
.readTimeout(HTTP_REQUEST_TIMEOUT_SEC, TimeUnit.SECONDS)
.writeTimeout(HTTP_REQUEST_TIMEOUT_SEC, TimeUnit.SECONDS)
.addInterceptor(UserAgentInterceptor())
.addInterceptor(RetryInterceptor(false))
.addLoggingInterceptor()
.addLoggingInterceptor(context)
.build()

private fun OkHttpClient.Builder.addLoggingInterceptor() = apply {
private fun OkHttpClient.Builder.addLoggingInterceptor(context: Context) = apply {
if (BuildConfig.DEBUG) {
addInterceptor(HttpLoggingInterceptor { message ->
Timber.d(message)
}.apply {
level = HttpLoggingInterceptor.Level.BODY
})
addNetworkInterceptor(StethoInterceptor())
addInterceptor(
ChuckerInterceptor.Builder(context)
.collector(ChuckerCollector(context, true))
.maxContentLength(256 * 1024)
.alwaysReadResponseBody(true)
.build()
)
}
}

Expand Down
26 changes: 26 additions & 0 deletions app/src/main/java/com/boardgamegeek/util/DebugCrashHandler.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.boardgamegeek.util

import android.content.Context
import com.chuckerteam.chucker.api.ChuckerCollector
import com.chuckerteam.chucker.api.RetentionManager

class DebugCrashHandler(
private val defaultHandler: Thread.UncaughtExceptionHandler? = null,
applicationContext: Context
) : Thread.UncaughtExceptionHandler {

private val chuckerCollector: ChuckerCollector by lazy {
ChuckerCollector(
context = applicationContext,
// Toggles visibility of the push notification
showNotification = true,
// Allows to customize the retention period of collected data
retentionPeriod = RetentionManager.Period.FOREVER
)
}

override fun uncaughtException(t: Thread, e: Throwable) {
chuckerCollector.onError("error", e)
defaultHandler?.uncaughtException(t, e)
}
}