diff --git a/app/src/main/java/io/github/wulkanowy/data/RepositoryModule.kt b/app/src/main/java/io/github/wulkanowy/data/RepositoryModule.kt index 14d64abef..8fdfde306 100644 --- a/app/src/main/java/io/github/wulkanowy/data/RepositoryModule.kt +++ b/app/src/main/java/io/github/wulkanowy/data/RepositoryModule.kt @@ -50,7 +50,7 @@ internal class RepositoryModule { @Provides fun provideChuckCollector(context: Context, prefRepository: PreferencesRepository): ChuckCollector { return ChuckCollector(context) - .showNotification(prefRepository.isShowChuckerNotification) + .showNotification(prefRepository.isDebugNotificationEnable) .retentionManager(RetentionManager(context, ChuckCollector.Period.ONE_HOUR)) } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/preferences/PreferencesRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/preferences/PreferencesRepository.kt index 6f3016644..9dad2278c 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/preferences/PreferencesRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/preferences/PreferencesRepository.kt @@ -48,7 +48,7 @@ class PreferencesRepository @Inject constructor( val isNotificationsEnable: Boolean 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) + val isDebugNotificationEnableKey: String = context.getString(R.string.pref_key_notification_debug) + val isDebugNotificationEnable: Boolean + get() = sharedPref.getBoolean(isDebugNotificationEnableKey, false) } 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 9a730cab7..7a9a99568 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 @@ -11,23 +11,28 @@ import androidx.work.NetworkType.UNMETERED import androidx.work.PeriodicWorkRequest import androidx.work.WorkManager 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.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, - newEntriesChannel: NewEntriesChannel + newEntriesChannel: NewEntriesChannel, + debugChannel: DebugChannel, + @Named("isDebug") isDebug: Boolean ) { init { if (SDK_INT >= O) newEntriesChannel.create() + if (SDK_INT >= O && isDebug) debugChannel.create() if (now().isHolidays) stopSyncWorker() Timber.i("SyncManager was initialized") } @@ -35,7 +40,7 @@ class SyncManager @Inject constructor( fun startSyncWorker(restart: Boolean = false) { if (preferencesRepository.isServiceEnabled && !now().isHolidays) { workManager.enqueueUniquePeriodicWork(SyncWorker::class.java.simpleName, if (restart) REPLACE else KEEP, - PeriodicWorkRequest.Builder(SyncWorker::class.java, preferencesRepository.servicesInterval, MINUTES, 10, MINUTES) + PeriodicWorkRequest.Builder(SyncWorker::class.java, preferencesRepository.servicesInterval, MINUTES) .setBackoffCriteria(EXPONENTIAL, 30, MINUTES) .setConstraints(Constraints.Builder() .setRequiredNetworkType(if (preferencesRepository.isServicesOnlyWifi) METERED else UNMETERED) diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/SyncWorker.kt b/app/src/main/java/io/github/wulkanowy/services/sync/SyncWorker.kt index 53460a0b0..ec753b7e2 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/SyncWorker.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/SyncWorker.kt @@ -1,24 +1,35 @@ package io.github.wulkanowy.services.sync import android.content.Context +import androidx.core.app.NotificationCompat +import androidx.core.app.NotificationCompat.BigTextStyle +import androidx.core.app.NotificationCompat.PRIORITY_DEFAULT +import androidx.core.app.NotificationManagerCompat import androidx.work.ListenableWorker import androidx.work.RxWorker import androidx.work.WorkerParameters import com.squareup.inject.assisted.Assisted import com.squareup.inject.assisted.AssistedInject +import io.github.wulkanowy.R +import io.github.wulkanowy.data.repositories.preferences.PreferencesRepository import io.github.wulkanowy.data.repositories.semester.SemesterRepository import io.github.wulkanowy.data.repositories.student.StudentRepository +import io.github.wulkanowy.services.sync.channels.DebugChannel import io.github.wulkanowy.services.sync.works.Work +import io.github.wulkanowy.utils.getCompatColor import io.reactivex.Completable import io.reactivex.Single import timber.log.Timber +import kotlin.random.Random class SyncWorker @AssistedInject constructor( @Assisted appContext: Context, @Assisted workerParameters: WorkerParameters, private val studentRepository: StudentRepository, private val semesterRepository: SemesterRepository, - private val works: Set<@JvmSuppressWildcards Work> + private val works: Set<@JvmSuppressWildcards Work>, + private val preferencesRepository: PreferencesRepository, + private val notificationManager: NotificationManagerCompat ) : RxWorker(appContext, workerParameters) { override fun createWork(): Single { @@ -34,6 +45,18 @@ class SyncWorker @AssistedInject constructor( Timber.e(it, "There was an error during synchronization") Result.retry() } + .doOnSuccess { if (preferencesRepository.isDebugNotificationEnable) notify(it) } + } + + private fun notify(result: Result) { + notificationManager.notify(Random.nextInt(Int.MAX_VALUE), NotificationCompat.Builder(applicationContext, DebugChannel.CHANNEL_ID) + .setContentTitle("Debug notification") + .setSmallIcon(R.drawable.ic_more_settings_24dp) + .setAutoCancel(true) + .setColor(applicationContext.getCompatColor(R.color.colorPrimary)) + .setStyle(BigTextStyle().bigText("${SyncWorker::class.java.simpleName} result: $result")) + .setPriority(PRIORITY_DEFAULT) + .build()) } @AssistedInject.Factory diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/channels/DebugChannel.kt b/app/src/main/java/io/github/wulkanowy/services/sync/channels/DebugChannel.kt new file mode 100644 index 000000000..02fff5690 --- /dev/null +++ b/app/src/main/java/io/github/wulkanowy/services/sync/channels/DebugChannel.kt @@ -0,0 +1,28 @@ +package io.github.wulkanowy.services.sync.channels + +import android.annotation.TargetApi +import android.app.Notification.VISIBILITY_PUBLIC +import android.app.NotificationChannel +import android.app.NotificationManager +import android.app.NotificationManager.IMPORTANCE_DEFAULT +import android.content.Context +import io.github.wulkanowy.R +import javax.inject.Inject + +@TargetApi(26) +class DebugChannel @Inject constructor( + private val notificationManager: NotificationManager, + private val context: Context +) { + + companion object { + const val CHANNEL_ID = "debug_channel" + } + + fun create() { + notificationManager.createNotificationChannel( + NotificationChannel(CHANNEL_ID, context.getString(R.string.channel_debug), IMPORTANCE_DEFAULT).apply { + lockscreenVisibility = VISIBILITY_PUBLIC + }) + } +} 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 57fcea099..d82eaba7e 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 @@ -6,7 +6,7 @@ import android.os.Bundle import androidx.appcompat.app.AppCompatDelegate import com.takisoft.preferencex.PreferenceFragmentCompat import dagger.android.support.AndroidSupportInjection -import io.github.wulkanowy.BuildConfig +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 @@ -37,7 +37,7 @@ class SettingsFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedP override fun onCreatePreferencesFix(savedInstanceState: Bundle?, rootKey: String?) { addPreferencesFromResource(R.xml.scheme_preferences) - findPreference(getString(R.string.pref_key_debug_chucker_notification)).isVisible = BuildConfig.DEBUG + findPreference(getString(R.string.pref_key_notification_debug)).isVisible = DEBUG } 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 8e3cbf5f3..65e871ea3 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 @@ -32,7 +32,7 @@ class SettingsPresenter @Inject constructor( serviceEnableKey -> syncManager.run { if (isServiceEnabled) startSyncWorker() else stopSyncWorker() } servicesIntervalKey, servicesOnlyWifiKey -> syncManager.startSyncWorker(true) currentThemeKey -> view?.setTheme(currentTheme) - isShowChuckerNotificationKey -> chuckCollector.showNotification(isShowChuckerNotification) + isDebugNotificationEnableKey -> chuckCollector.showNotification(isDebugNotificationEnable) } } analytics.logEvent("setting_changed", "name" to key) diff --git a/app/src/main/res/values-pl/strings.xml b/app/src/main/res/values-pl/strings.xml index 4c4e8f260..75dbca80a 100644 --- a/app/src/main/res/values-pl/strings.xml +++ b/app/src/main/res/values-pl/strings.xml @@ -249,7 +249,7 @@ Powiadomienia Pokazuj powiadomienia - Pokazuj powiadomienia debugowania + Pokazuj powiadomienia debugowania Synchronizacja Automatyczna aktualizacja @@ -260,6 +260,7 @@ Nowe wpisy w dzienniku + Debugowanie diff --git a/app/src/main/res/values/preferences_keys.xml b/app/src/main/res/values/preferences_keys.xml index 9007b5df5..0a2f8ea8e 100644 --- a/app/src/main/res/values/preferences_keys.xml +++ b/app/src/main/res/values/preferences_keys.xml @@ -11,5 +11,5 @@ services_interval services_disable_wifi_only notifications_enable - chucker_notification + notification_debug diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index d07ecaea0..3203f8b1f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -234,7 +234,7 @@ Notifications Show notifications - Show debug notifications + Show debug notifications Synchronization Automatic update @@ -245,6 +245,7 @@ New entries in register + Debug diff --git a/app/src/main/res/xml/scheme_preferences.xml b/app/src/main/res/xml/scheme_preferences.xml index a6e6da58d..f3b9ae2d8 100644 --- a/app/src/main/res/xml/scheme_preferences.xml +++ b/app/src/main/res/xml/scheme_preferences.xml @@ -90,8 +90,8 @@ app:iconSpaceReserved="false" />