Add chucker okhttp inspector (#205)

This commit is contained in:
Mikołaj Pich 2019-01-10 17:10:10 +01:00 committed by Rafał Borcz
parent ed49eb4c9c
commit c3d354cd5b
14 changed files with 54 additions and 6 deletions

View File

@ -11,6 +11,10 @@ cache:
- $HOME/.gradle/caches/ - $HOME/.gradle/caches/
- $HOME/.gradle/wrapper/ - $HOME/.gradle/wrapper/
branches:
only:
- master
android: android:
licenses: licenses:
- android-sdk-preview-license-.+ - android-sdk-preview-license-.+

View File

@ -80,7 +80,7 @@ play {
dependencies { dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
implementation('io.github.wulkanowy:api:0.6.2') { exclude module: "threetenbp" } implementation('io.github.wulkanowy:api:0.6.3') { exclude module: "threetenbp" }
implementation "androidx.legacy:legacy-support-v4:1.0.0" implementation "androidx.legacy:legacy-support-v4:1.0.0"
implementation "androidx.appcompat:appcompat:1.0.2" implementation "androidx.appcompat:appcompat:1.0.2"
@ -118,6 +118,9 @@ dependencies {
implementation 'com.google.firebase:firebase-core:16.0.6' implementation 'com.google.firebase:firebase-core:16.0.6'
implementation 'com.crashlytics.sdk.android:crashlytics:2.9.8' implementation 'com.crashlytics.sdk.android:crashlytics:2.9.8'
releaseImplementation 'fr.o80.chucker:library-no-op:2.0.3'
debugImplementation 'fr.o80.chucker:library:2.0.3'
debugImplementation "com.amitshekhar.android:debug-db:1.0.4" debugImplementation "com.amitshekhar.android:debug-db:1.0.4"
testImplementation "junit:junit:4.12" testImplementation "junit:junit:4.12"

View File

@ -4,6 +4,8 @@
package="io.github.wulkanowy" package="io.github.wulkanowy"
android:installLocation="internalOnly"> android:installLocation="internalOnly">
<uses-sdk tools:overrideLibrary="com.readystatesoftware.chuck"/>
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

View File

@ -6,10 +6,14 @@ import android.content.res.Resources
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.InternetObservingSettings import com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.InternetObservingSettings
import com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.strategy.SocketInternetObservingStrategy import com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.strategy.SocketInternetObservingStrategy
import com.readystatesoftware.chuck.api.ChuckCollector
import com.readystatesoftware.chuck.api.ChuckInterceptor
import com.readystatesoftware.chuck.api.RetentionManager
import dagger.Module import dagger.Module
import dagger.Provides import dagger.Provides
import io.github.wulkanowy.api.Api import io.github.wulkanowy.api.Api
import io.github.wulkanowy.data.db.AppDatabase import io.github.wulkanowy.data.db.AppDatabase
import io.github.wulkanowy.data.repositories.PreferencesRepository
import okhttp3.logging.HttpLoggingInterceptor import okhttp3.logging.HttpLoggingInterceptor
import okhttp3.logging.HttpLoggingInterceptor.Level.BASIC import okhttp3.logging.HttpLoggingInterceptor.Level.BASIC
import okhttp3.logging.HttpLoggingInterceptor.Level.NONE import okhttp3.logging.HttpLoggingInterceptor.Level.NONE
@ -30,15 +34,26 @@ internal class RepositoryModule {
@Singleton @Singleton
@Provides @Provides
fun provideApi(): Api { fun provideApi(chuckCollector: ChuckCollector, context: Context): Api {
return Api().apply { return Api().apply {
logLevel = NONE logLevel = NONE
androidVersion = android.os.Build.VERSION.RELEASE androidVersion = android.os.Build.VERSION.RELEASE
buildTag = android.os.Build.MODEL buildTag = android.os.Build.MODEL
setInterceptor(HttpLoggingInterceptor(HttpLoggingInterceptor.Logger { Timber.d(it) }).setLevel(BASIC)) setInterceptor(HttpLoggingInterceptor(HttpLoggingInterceptor.Logger { Timber.d(it) }).setLevel(BASIC))
// for debug only
setInterceptor(ChuckInterceptor(context, chuckCollector).maxContentLength(250000L), true, 0)
} }
} }
@Singleton
@Provides
fun provideChuckCollector(context: Context, prefRepository: PreferencesRepository): ChuckCollector {
return ChuckCollector(context)
.showNotification(prefRepository.isShowChuckerNotification)
.retentionManager(RetentionManager(context, ChuckCollector.Period.ONE_HOUR))
}
@Singleton @Singleton
@Provides @Provides
fun provideDatabase(context: Context) = AppDatabase.newInstance(context) fun provideDatabase(context: Context) = AppDatabase.newInstance(context)

View File

@ -44,4 +44,8 @@ class PreferencesRepository @Inject constructor(
val isNotificationsEnable: Boolean val isNotificationsEnable: Boolean
get() = sharedPref.getBoolean(context.getString(R.string.pref_key_notifications_enable), true) get() = sharedPref.getBoolean(context.getString(R.string.pref_key_notifications_enable), true)
val isShowChuckerNotificationKey: String = context.getString(R.string.pref_key_debug_chucker_notification)
val isShowChuckerNotification: Boolean
get() = sharedPref.getBoolean(isShowChuckerNotificationKey, false)
} }

View File

@ -1,6 +1,7 @@
package io.github.wulkanowy.ui.base package io.github.wulkanowy.ui.base
import android.content.res.Resources import android.content.res.Resources
import com.readystatesoftware.chuck.api.ChuckCollector
import io.github.wulkanowy.R import io.github.wulkanowy.R
import io.github.wulkanowy.api.interceptor.ServiceUnavailableException import io.github.wulkanowy.api.interceptor.ServiceUnavailableException
import io.github.wulkanowy.api.login.NotLoggedInException import io.github.wulkanowy.api.login.NotLoggedInException
@ -9,11 +10,12 @@ import java.net.SocketTimeoutException
import java.net.UnknownHostException import java.net.UnknownHostException
import javax.inject.Inject import javax.inject.Inject
open class ErrorHandler @Inject constructor(protected val resources: Resources) { open class ErrorHandler @Inject constructor(protected val resources: Resources, private val chuckCollector: ChuckCollector) {
var showErrorMessage: (String, Throwable) -> Unit = { _, _ -> } var showErrorMessage: (String, Throwable) -> Unit = { _, _ -> }
fun dispatch(error: Throwable) { fun dispatch(error: Throwable) {
chuckCollector.onError(error.javaClass.simpleName, error)
Timber.e(error, "An exception occurred while the Wulkanowy was running") Timber.e(error, "An exception occurred while the Wulkanowy was running")
proceed(error) proceed(error)
} }

View File

@ -1,11 +1,12 @@
package io.github.wulkanowy.ui.base.session package io.github.wulkanowy.ui.base.session
import android.content.res.Resources import android.content.res.Resources
import com.readystatesoftware.chuck.api.ChuckCollector
import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.ui.base.ErrorHandler
import io.github.wulkanowy.utils.security.ScramblerException import io.github.wulkanowy.utils.security.ScramblerException
import javax.inject.Inject import javax.inject.Inject
class SessionErrorHandler @Inject constructor(resources: Resources) : ErrorHandler(resources) { class SessionErrorHandler @Inject constructor(resources: Resources, chuckCollector: ChuckCollector) : ErrorHandler(resources, chuckCollector) {
var onDecryptionFail: () -> Unit = {} var onDecryptionFail: () -> Unit = {}

View File

@ -2,12 +2,13 @@ package io.github.wulkanowy.ui.modules.login
import android.content.res.Resources import android.content.res.Resources
import android.database.sqlite.SQLiteConstraintException import android.database.sqlite.SQLiteConstraintException
import com.readystatesoftware.chuck.api.ChuckCollector
import io.github.wulkanowy.R import io.github.wulkanowy.R
import io.github.wulkanowy.api.login.BadCredentialsException import io.github.wulkanowy.api.login.BadCredentialsException
import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.ui.base.ErrorHandler
import javax.inject.Inject import javax.inject.Inject
class LoginErrorHandler @Inject constructor(resources: Resources) : ErrorHandler(resources) { class LoginErrorHandler @Inject constructor(resources: Resources, chuckCollector: ChuckCollector) : ErrorHandler(resources, chuckCollector) {
var onBadCredentials: () -> Unit = {} var onBadCredentials: () -> Unit = {}

View File

@ -6,6 +6,7 @@ import android.os.Bundle
import androidx.appcompat.app.AppCompatDelegate import androidx.appcompat.app.AppCompatDelegate
import com.takisoft.preferencex.PreferenceFragmentCompat import com.takisoft.preferencex.PreferenceFragmentCompat
import dagger.android.support.AndroidSupportInjection import dagger.android.support.AndroidSupportInjection
import io.github.wulkanowy.BuildConfig
import io.github.wulkanowy.R import io.github.wulkanowy.R
import io.github.wulkanowy.ui.base.BaseActivity import io.github.wulkanowy.ui.base.BaseActivity
import io.github.wulkanowy.ui.modules.main.MainView import io.github.wulkanowy.ui.modules.main.MainView
@ -36,6 +37,7 @@ class SettingsFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedP
override fun onCreatePreferencesFix(savedInstanceState: Bundle?, rootKey: String?) { override fun onCreatePreferencesFix(savedInstanceState: Bundle?, rootKey: String?) {
addPreferencesFromResource(R.xml.scheme_preferences) addPreferencesFromResource(R.xml.scheme_preferences)
findPreference(getString(R.string.pref_key_debug_chucker_notification)).isVisible = BuildConfig.DEBUG
} }
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: String) { override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: String) {

View File

@ -1,5 +1,7 @@
package io.github.wulkanowy.ui.modules.settings package io.github.wulkanowy.ui.modules.settings
import com.readystatesoftware.chuck.api.ChuckCollector
import io.github.wulkanowy.data.RepositoryModule_ProvideChuckCollectorFactory
import io.github.wulkanowy.data.repositories.PreferencesRepository import io.github.wulkanowy.data.repositories.PreferencesRepository
import io.github.wulkanowy.services.job.ServiceHelper import io.github.wulkanowy.services.job.ServiceHelper
import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.BasePresenter
@ -13,7 +15,8 @@ class SettingsPresenter @Inject constructor(
errorHandler: ErrorHandler, errorHandler: ErrorHandler,
private val preferencesRepository: PreferencesRepository, private val preferencesRepository: PreferencesRepository,
private val serviceHelper: ServiceHelper, private val serviceHelper: ServiceHelper,
private val analytics: FirebaseAnalyticsHelper private val analytics: FirebaseAnalyticsHelper,
private val chuckCollector: ChuckCollector
) : BasePresenter<SettingsView>(errorHandler) { ) : BasePresenter<SettingsView>(errorHandler) {
override fun onAttachView(view: SettingsView) { override fun onAttachView(view: SettingsView) {
@ -37,6 +40,9 @@ class SettingsPresenter @Inject constructor(
preferencesRepository.currentThemeKey -> { preferencesRepository.currentThemeKey -> {
view?.setTheme(preferencesRepository.currentTheme) view?.setTheme(preferencesRepository.currentTheme)
} }
preferencesRepository.isShowChuckerNotificationKey -> {
chuckCollector.showNotification(preferencesRepository.isShowChuckerNotification)
}
} }
analytics.logEvent("setting_changed", mapOf("name" to key)) analytics.logEvent("setting_changed", mapOf("name" to key))

View File

@ -227,6 +227,7 @@
<string name="pref_notify_header">Powiadomienia</string> <string name="pref_notify_header">Powiadomienia</string>
<string name="pref_notify_switch">Pokazuj powiadomienia</string> <string name="pref_notify_switch">Pokazuj powiadomienia</string>
<string name="pref_debug_notify_switch">Pokazuj powiadomienia debugowania</string>
<string name="pref_services_header">Synchronizacja</string> <string name="pref_services_header">Synchronizacja</string>
<string name="pref_services_switch">Automatyczna aktualizacja</string> <string name="pref_services_switch">Automatyczna aktualizacja</string>

View File

@ -10,4 +10,5 @@
<string name="pref_key_services_interval">services_interval</string> <string name="pref_key_services_interval">services_interval</string>
<string name="pref_key_services_wifi_only">services_disable_wifi_only</string> <string name="pref_key_services_wifi_only">services_disable_wifi_only</string>
<string name="pref_key_notifications_enable">notifications_enable</string> <string name="pref_key_notifications_enable">notifications_enable</string>
<string name="pref_key_debug_chucker_notification">chucker_notification</string>
</resources> </resources>

View File

@ -210,6 +210,7 @@
<string name="pref_notify_header">Notifications</string> <string name="pref_notify_header">Notifications</string>
<string name="pref_notify_switch">Show notifications</string> <string name="pref_notify_switch">Show notifications</string>
<string name="pref_debug_notify_switch">Show debug notifications</string>
<string name="pref_services_header">Synchronization</string> <string name="pref_services_header">Synchronization</string>
<string name="pref_services_switch">Automatic update</string> <string name="pref_services_switch">Automatic update</string>

View File

@ -80,5 +80,10 @@
android:key="@string/pref_key_notifications_enable" android:key="@string/pref_key_notifications_enable"
android:title="@string/pref_notify_switch" android:title="@string/pref_notify_switch"
app:iconSpaceReserved="false" /> app:iconSpaceReserved="false" />
<SwitchPreference
android:defaultValue="false"
android:key="@string/pref_key_debug_chucker_notification"
android:title="@string/pref_debug_notify_switch"
app:iconSpaceReserved="false" />
</PreferenceCategory> </PreferenceCategory>
</PreferenceScreen> </PreferenceScreen>