From 7485cb2a39f3f2f24f6d25e44ebee47cf8d023c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Borcz?= Date: Thu, 6 Jun 2019 22:32:43 +0200 Subject: [PATCH] Fix change of worker specs after app update (#402) --- .../wulkanowy/utils/CrashlyticsUtils.kt | 8 +++--- app/src/main/AndroidManifest.xml | 1 - .../java/io/github/wulkanowy/WulkanowyApp.kt | 21 +++++++++++--- .../java/io/github/wulkanowy/di/AppModule.kt | 6 ++-- .../wulkanowy/services/sync/SyncManager.kt | 24 +++++++++++++--- .../ui/modules/about/AboutFragment.kt | 11 +++++--- .../modules/login/form/LoginFormFragment.kt | 7 +++-- .../modules/login/form/LoginFormPresenter.kt | 6 ++-- .../ui/modules/settings/SettingsFragment.kt | 7 +++-- .../ui/modules/settings/SettingsPresenter.kt | 2 +- .../java/io/github/wulkanowy/utils/AppInfo.kt | 28 +++++++++++++++++++ .../wulkanowy/utils/CrashlyticsUtils.kt | 20 ++++++++----- .../login/form/LoginFormPresenterTest.kt | 6 +++- 13 files changed, 110 insertions(+), 37 deletions(-) create mode 100644 app/src/main/java/io/github/wulkanowy/utils/AppInfo.kt diff --git a/app/src/fdroid/java/io/github/wulkanowy/utils/CrashlyticsUtils.kt b/app/src/fdroid/java/io/github/wulkanowy/utils/CrashlyticsUtils.kt index 33ef5a58..be4c0ebe 100644 --- a/app/src/fdroid/java/io/github/wulkanowy/utils/CrashlyticsUtils.kt +++ b/app/src/fdroid/java/io/github/wulkanowy/utils/CrashlyticsUtils.kt @@ -5,13 +5,13 @@ package io.github.wulkanowy.utils import android.content.Context import timber.log.Timber +fun initCrashlytics(context: Context, appInfo: AppInfo) { + // do nothing +} + class CrashlyticsTree : Timber.Tree() { override fun log(priority: Int, tag: String?, message: String, t: Throwable?) { // do nothing } } - -fun initCrashlytics(context: Context) { - // do nothing -} diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 2a214b8b..65a140f2 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -80,7 +80,6 @@ android:name="android.appwidget.provider" android:resource="@xml/provider_widget_timetable" /> - diff --git a/app/src/main/java/io/github/wulkanowy/WulkanowyApp.kt b/app/src/main/java/io/github/wulkanowy/WulkanowyApp.kt index 35a0e8da..8e972c80 100644 --- a/app/src/main/java/io/github/wulkanowy/WulkanowyApp.kt +++ b/app/src/main/java/io/github/wulkanowy/WulkanowyApp.kt @@ -1,6 +1,8 @@ package io.github.wulkanowy import android.content.Context +import android.util.Log.INFO +import android.util.Log.VERBOSE import androidx.multidex.MultiDex import androidx.work.Configuration import androidx.work.WorkManager @@ -9,10 +11,10 @@ import dagger.android.AndroidInjector import dagger.android.support.DaggerApplication import eu.davidea.flexibleadapter.FlexibleAdapter import eu.davidea.flexibleadapter.utils.Log -import io.github.wulkanowy.BuildConfig.DEBUG import io.github.wulkanowy.di.DaggerAppComponent import io.github.wulkanowy.services.sync.SyncWorkerFactory import io.github.wulkanowy.utils.ActivityLifecycleLogger +import io.github.wulkanowy.utils.AppInfo import io.github.wulkanowy.utils.CrashlyticsTree import io.github.wulkanowy.utils.DebugLogTree import io.github.wulkanowy.utils.initCrashlytics @@ -27,6 +29,9 @@ class WulkanowyApp : DaggerApplication() { @Inject lateinit var workerFactory: SyncWorkerFactory + @Inject + lateinit var appInfo: AppInfo + override fun attachBaseContext(base: Context?) { super.attachBaseContext(base) MultiDex.install(this) @@ -35,15 +40,23 @@ class WulkanowyApp : DaggerApplication() { override fun onCreate() { super.onCreate() AndroidThreeTen.init(this) - WorkManager.initialize(this, Configuration.Builder().setWorkerFactory(workerFactory).build()) RxJavaPlugins.setErrorHandler(::onError) - initCrashlytics(applicationContext) + initWorkManager() initLogging() + initCrashlytics(this, appInfo) + } + + private fun initWorkManager() { + WorkManager.initialize(this, + Configuration.Builder() + .setWorkerFactory(workerFactory) + .setMinimumLoggingLevel(if (appInfo.isDebug) VERBOSE else INFO) + .build()) } private fun initLogging() { - if (DEBUG) { + if (appInfo.isDebug) { Timber.plant(DebugLogTree()) FlexibleAdapter.enableLogs(Log.Level.DEBUG) } else { diff --git a/app/src/main/java/io/github/wulkanowy/di/AppModule.kt b/app/src/main/java/io/github/wulkanowy/di/AppModule.kt index 9ea2bf10..42e50fa4 100644 --- a/app/src/main/java/io/github/wulkanowy/di/AppModule.kt +++ b/app/src/main/java/io/github/wulkanowy/di/AppModule.kt @@ -6,10 +6,9 @@ import dagger.Module import dagger.Provides import eu.davidea.flexibleadapter.FlexibleAdapter import eu.davidea.flexibleadapter.items.AbstractFlexibleItem -import io.github.wulkanowy.BuildConfig.DEBUG import io.github.wulkanowy.WulkanowyApp +import io.github.wulkanowy.utils.AppInfo import io.github.wulkanowy.utils.SchedulersProvider -import javax.inject.Named import javax.inject.Singleton @Module @@ -31,7 +30,6 @@ internal class AppModule { fun provideAppWidgetManager(context: Context): AppWidgetManager = AppWidgetManager.getInstance(context) @Singleton - @Named("isDebug") @Provides - fun provideIsDebug() = DEBUG + fun provideAppInfo() = AppInfo() } diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/SyncManager.kt b/app/src/main/java/io/github/wulkanowy/services/sync/SyncManager.kt index 7f700bac..d85c75c3 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/SyncManager.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/SyncManager.kt @@ -10,30 +10,46 @@ import androidx.work.NetworkType.CONNECTED import androidx.work.NetworkType.UNMETERED import androidx.work.PeriodicWorkRequest import androidx.work.WorkManager +import io.github.wulkanowy.data.db.SharedPrefHelper import io.github.wulkanowy.data.repositories.preferences.PreferencesRepository import io.github.wulkanowy.services.sync.channels.DebugChannel import io.github.wulkanowy.services.sync.channels.NewEntriesChannel +import io.github.wulkanowy.utils.AppInfo import io.github.wulkanowy.utils.isHolidays import org.threeten.bp.LocalDate.now import timber.log.Timber import java.util.concurrent.TimeUnit.MINUTES import javax.inject.Inject -import javax.inject.Named import javax.inject.Singleton @Singleton class SyncManager @Inject constructor( private val workManager: WorkManager, private val preferencesRepository: PreferencesRepository, + sharedPrefHelper: SharedPrefHelper, newEntriesChannel: NewEntriesChannel, debugChannel: DebugChannel, - @Named("isDebug") isDebug: Boolean + appInfo: AppInfo ) { + companion object { + + private const val APP_VERSION_CODE_KEY = "app_version_code" + } + init { - if (SDK_INT >= O) newEntriesChannel.create() - if (SDK_INT >= O && isDebug) debugChannel.create() if (now().isHolidays) stopSyncWorker() + + if (SDK_INT > O) { + newEntriesChannel.create() + if (appInfo.isDebug) debugChannel.create() + } + + if (sharedPrefHelper.getLong(APP_VERSION_CODE_KEY, -1L) != appInfo.versionCode.toLong()) { + startSyncWorker(true) + sharedPrefHelper.putLong(APP_VERSION_CODE_KEY, appInfo.versionCode.toLong(), true) + } + Timber.i("SyncManager was initialized") } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/about/AboutFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/about/AboutFragment.kt index 8d07566f..79373be9 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/about/AboutFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/about/AboutFragment.kt @@ -8,10 +8,10 @@ import android.view.View import android.view.ViewGroup import com.mikepenz.aboutlibraries.LibsBuilder import com.mikepenz.aboutlibraries.LibsFragmentCompat -import io.github.wulkanowy.BuildConfig import io.github.wulkanowy.R import io.github.wulkanowy.ui.base.BaseFragment import io.github.wulkanowy.ui.modules.main.MainView +import io.github.wulkanowy.utils.AppInfo import io.github.wulkanowy.utils.openInternetBrowser import io.github.wulkanowy.utils.withOnExtraListener import javax.inject.Inject @@ -24,6 +24,9 @@ class AboutFragment : BaseFragment(), AboutView, MainView.TitledView { @Inject lateinit var fragmentCompat: LibsFragmentCompat + @Inject + lateinit var appInfo: AppInfo + companion object { fun newInstance() = AboutFragment() } @@ -71,9 +74,9 @@ class AboutFragment : BaseFragment(), AboutView, MainView.TitledView { putExtra(Intent.EXTRA_EMAIL, Array(1) { "wulkanowyinc@gmail.com" }) putExtra(Intent.EXTRA_SUBJECT, "Zgłoszenie błędu") putExtra(Intent.EXTRA_TEXT, "Tu umieść treść zgłoszenia\n\n" + "-".repeat(40) + "\n" + """ - Build: ${BuildConfig.VERSION_CODE} - SDK: ${android.os.Build.VERSION.SDK_INT} - Device: ${android.os.Build.MANUFACTURER} ${android.os.Build.MODEL} + Build: ${appInfo.versionCode} + SDK: ${appInfo.systemVersion} + Device: ${appInfo.systemManufacturer} ${appInfo.systemModel} """.trimIndent()) } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/login/form/LoginFormFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/login/form/LoginFormFragment.kt index b17a1205..93db3679 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/login/form/LoginFormFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/login/form/LoginFormFragment.kt @@ -10,11 +10,11 @@ import android.view.ViewGroup import android.view.inputmethod.EditorInfo.IME_ACTION_DONE import android.view.inputmethod.EditorInfo.IME_NULL import android.widget.ArrayAdapter -import io.github.wulkanowy.BuildConfig.VERSION_NAME import io.github.wulkanowy.R import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.ui.base.BaseFragment import io.github.wulkanowy.ui.modules.login.LoginActivity +import io.github.wulkanowy.utils.AppInfo import io.github.wulkanowy.utils.hideSoftInput import io.github.wulkanowy.utils.openInternetBrowser import io.github.wulkanowy.utils.setOnItemSelectedListener @@ -28,6 +28,9 @@ class LoginFormFragment : BaseFragment(), LoginFormView { @Inject lateinit var presenter: LoginFormPresenter + @Inject + lateinit var appInfo: AppInfo + companion object { fun newInstance() = LoginFormFragment() } @@ -128,7 +131,7 @@ class LoginFormFragment : BaseFragment(), LoginFormView { override fun showVersion() { loginFormVersion.apply { visibility = VISIBLE - text = "${getString(R.string.app_name)} $VERSION_NAME" + text = "${getString(R.string.app_name)} ${appInfo.versionName}" } } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/login/form/LoginFormPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/login/form/LoginFormPresenter.kt index 0fbfb329..2b6fa04f 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/login/form/LoginFormPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/login/form/LoginFormPresenter.kt @@ -3,25 +3,25 @@ package io.github.wulkanowy.ui.modules.login.form import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.modules.login.LoginErrorHandler +import io.github.wulkanowy.utils.AppInfo import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.SchedulersProvider import timber.log.Timber import javax.inject.Inject -import javax.inject.Named class LoginFormPresenter @Inject constructor( schedulers: SchedulersProvider, studentRepository: StudentRepository, private val loginErrorHandler: LoginErrorHandler, private val analytics: FirebaseAnalyticsHelper, - @param:Named("isDebug") private val isDebug: Boolean + private val appInfo: AppInfo ) : BasePresenter(loginErrorHandler, studentRepository, schedulers) { override fun onAttachView(view: LoginFormView) { super.onAttachView(view) view.run { initView() - if (isDebug) showVersion() else showPrivacyPolicy() + if (appInfo.isDebug) showVersion() else showPrivacyPolicy() loginErrorHandler.onBadCredentials = { setErrorPassIncorrect() diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/settings/SettingsFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/settings/SettingsFragment.kt index d0550bd4..60de9920 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/settings/SettingsFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/settings/SettingsFragment.kt @@ -5,10 +5,10 @@ import android.content.SharedPreferences import android.os.Bundle import com.takisoft.preferencex.PreferenceFragmentCompat import dagger.android.support.AndroidSupportInjection -import io.github.wulkanowy.BuildConfig.DEBUG import io.github.wulkanowy.R import io.github.wulkanowy.ui.base.BaseActivity import io.github.wulkanowy.ui.modules.main.MainView +import io.github.wulkanowy.utils.AppInfo import javax.inject.Inject class SettingsFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedPreferenceChangeListener, @@ -17,6 +17,9 @@ class SettingsFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedP @Inject lateinit var presenter: SettingsPresenter + @Inject + lateinit var appInfo: AppInfo + companion object { fun newInstance() = SettingsFragment() } @@ -36,7 +39,7 @@ class SettingsFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedP override fun onCreatePreferencesFix(savedInstanceState: Bundle?, rootKey: String?) { addPreferencesFromResource(R.xml.scheme_preferences) - findPreference(getString(R.string.pref_key_notification_debug)).isVisible = DEBUG + findPreference(getString(R.string.pref_key_notification_debug)).isVisible = appInfo.isDebug } override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: String) { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/settings/SettingsPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/settings/SettingsPresenter.kt index a2edc414..3c3d4990 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/settings/SettingsPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/settings/SettingsPresenter.kt @@ -33,7 +33,7 @@ class SettingsPresenter @Inject constructor( Timber.i("Change settings $key") preferencesRepository.apply { when (key) { - serviceEnableKey -> syncManager.run { if (isServiceEnabled) startSyncWorker() else stopSyncWorker() } + serviceEnableKey -> with(syncManager) { if (isServiceEnabled) startSyncWorker() else stopSyncWorker() } servicesIntervalKey, servicesOnlyWifiKey -> syncManager.startSyncWorker(true) isDebugNotificationEnableKey -> chuckCollector.showNotification(isDebugNotificationEnable) appThemeKey -> view?.recreateView() diff --git a/app/src/main/java/io/github/wulkanowy/utils/AppInfo.kt b/app/src/main/java/io/github/wulkanowy/utils/AppInfo.kt new file mode 100644 index 00000000..3fa83a04 --- /dev/null +++ b/app/src/main/java/io/github/wulkanowy/utils/AppInfo.kt @@ -0,0 +1,28 @@ +package io.github.wulkanowy.utils + +import android.os.Build.MANUFACTURER +import android.os.Build.MODEL +import android.os.Build.VERSION.SDK_INT +import io.github.wulkanowy.BuildConfig.CRASHLYTICS_ENABLED +import io.github.wulkanowy.BuildConfig.DEBUG +import io.github.wulkanowy.BuildConfig.VERSION_CODE +import io.github.wulkanowy.BuildConfig.VERSION_NAME +import javax.inject.Singleton + +@Singleton +open class AppInfo { + + open val isCrashlyticsEnabled get() = CRASHLYTICS_ENABLED + + open val isDebug get() = DEBUG + + open val versionCode get() = VERSION_CODE + + open val versionName get() = VERSION_NAME + + open val systemVersion get() = SDK_INT + + open val systemManufacturer: String get() = MANUFACTURER + + open val systemModel: String get() = MODEL +} diff --git a/app/src/play/java/io/github/wulkanowy/utils/CrashlyticsUtils.kt b/app/src/play/java/io/github/wulkanowy/utils/CrashlyticsUtils.kt index 77c2f4a0..4f76ef83 100644 --- a/app/src/play/java/io/github/wulkanowy/utils/CrashlyticsUtils.kt +++ b/app/src/play/java/io/github/wulkanowy/utils/CrashlyticsUtils.kt @@ -4,9 +4,21 @@ import android.content.Context import com.crashlytics.android.Crashlytics import com.crashlytics.android.core.CrashlyticsCore import io.fabric.sdk.android.Fabric -import io.github.wulkanowy.BuildConfig import timber.log.Timber +fun initCrashlytics(context: Context, appInfo: AppInfo) { + Fabric.with(Fabric.Builder(context) + .kits( + Crashlytics.Builder() + .core(CrashlyticsCore.Builder() + .disabled(!appInfo.isCrashlyticsEnabled) + .build()) + .build() + ) + .debuggable(appInfo.isDebug) + .build()) +} + class CrashlyticsTree : Timber.Tree() { override fun log(priority: Int, tag: String?, message: String, t: Throwable?) { @@ -17,9 +29,3 @@ class CrashlyticsTree : Timber.Tree() { else Crashlytics.logException(t) } } - -fun initCrashlytics(context: Context) { - Fabric.with(Fabric.Builder(context).kits( - Crashlytics.Builder().core(CrashlyticsCore.Builder().disabled(!BuildConfig.CRASHLYTICS_ENABLED).build()).build() - ).debuggable(BuildConfig.DEBUG).build()) -} diff --git a/app/src/test/java/io/github/wulkanowy/ui/modules/login/form/LoginFormPresenterTest.kt b/app/src/test/java/io/github/wulkanowy/ui/modules/login/form/LoginFormPresenterTest.kt index 644d74f0..453adfba 100644 --- a/app/src/test/java/io/github/wulkanowy/ui/modules/login/form/LoginFormPresenterTest.kt +++ b/app/src/test/java/io/github/wulkanowy/ui/modules/login/form/LoginFormPresenterTest.kt @@ -4,6 +4,7 @@ import io.github.wulkanowy.TestSchedulersProvider import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.ui.modules.login.LoginErrorHandler +import io.github.wulkanowy.utils.AppInfo import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.reactivex.Single import org.junit.Before @@ -33,13 +34,16 @@ class LoginFormPresenterTest { @Mock lateinit var analytics: FirebaseAnalyticsHelper + @Mock + lateinit var appInfo: AppInfo + private lateinit var presenter: LoginFormPresenter @Before fun initPresenter() { MockitoAnnotations.initMocks(this) clearInvocations(repository, loginFormView) - presenter = LoginFormPresenter(TestSchedulersProvider(), repository, errorHandler, analytics, false) + presenter = LoginFormPresenter(TestSchedulersProvider(), repository, errorHandler, analytics, appInfo) presenter.onAttachView(loginFormView) }