Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.simprints.core.tools.utils

import android.content.Context
import android.os.PowerManager
import com.simprints.core.ExcludedFromGeneratedTestCoverageReports

@ExcludedFromGeneratedTestCoverageReports("Platform glue code")
object BatteryOptimizationUtils {

fun isFollowingBatteryOptimizations(context: Context): Boolean =
(context.getSystemService(Context.POWER_SERVICE) as PowerManager)
.isIgnoringBatteryOptimizations(context.packageName)
.not()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This reminds me of very old Borat sketch :D


}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import androidx.work.Data
import androidx.work.ForegroundInfo
import androidx.work.WorkerParameters
import com.simprints.core.ExcludedFromGeneratedTestCoverageReports
import com.simprints.core.tools.utils.BatteryOptimizationUtils
import com.simprints.infra.logging.LoggingConstants.CrashReportTag
import com.simprints.infra.logging.Simber
import com.simprints.infra.network.exceptions.NetworkConnectionException
Expand Down Expand Up @@ -58,6 +59,9 @@ abstract class SimCoroutineWorker(

protected suspend fun showProgressNotification() {
try {
if (BatteryOptimizationUtils.isFollowingBatteryOptimizations(context)) {
return
}
setForeground(getForegroundInfo())
} catch (setForegroundException: Throwable) {
// Setting foreground (showing the notification) may be restricted by the system
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ internal class EventDownSyncDownloaderWorker @AssistedInject constructor(

override suspend fun doWork(): Result = withContext(dispatcher) {
try {
Simber.tag(SYNC_LOG_TAG).d("[DOWNLOADER] Started")
showProgressNotification()
Simber.tag(SYNC_LOG_TAG).d("[DOWNLOADER] Started")

val workerId = this@EventDownSyncDownloaderWorker.id.toString()
var count = syncCache.readProgress(workerId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ internal class EventEndSyncReporterWorker @AssistedInject constructor(
override suspend fun doWork(): Result =
withContext(dispatcher) {
try {
showProgressNotification()
val syncId = inputData.getString(SYNC_ID_TO_MARK_AS_COMPLETED)
crashlyticsLog("Start - Params: $syncId")
showProgressNotification()

inputData.getString(EVENT_DOWN_SYNC_SCOPE_TO_CLOSE)?.let { scopeId ->
eventRepository.closeEventScope(scopeId, EventScopeEndCause.WORKFLOW_ENDED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ internal class EventStartSyncReporterWorker @AssistedInject constructor(
override suspend fun doWork(): Result =
withContext(dispatcher) {
try {
showProgressNotification()
val syncId = inputData.getString(SYNC_ID_STARTED)
crashlyticsLog("Start - Params: $syncId")
showProgressNotification()
success(inputData)
} catch (t: Throwable) {
fail(t)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ class EventSyncMasterWorker @AssistedInject internal constructor(
override suspend fun doWork(): Result =
withContext(dispatcher) {
try {
showProgressNotification()
// check if device is rooted before starting the sync
securityManager.checkIfDeviceIsRooted()
crashlyticsLog("Start")
showProgressNotification()
val configuration = configRepository.getProjectConfiguration()

if (!configuration.canSyncDataToSimprints() && !isEventDownSyncAllowed(configuration)) return@withContext success(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ internal class EventUpSyncUploaderWorker @AssistedInject constructor(

override suspend fun doWork(): Result = withContext(dispatcher) {
try {
Simber.tag(SYNC_LOG_TAG).d("[UPLOADER] Started")
showProgressNotification()
Simber.tag(SYNC_LOG_TAG).d("[UPLOADER] Started")

val workerId = this@EventUpSyncUploaderWorker.id.toString()
var count = eventSyncCache.readProgress(workerId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ internal class DeviceConfigDownSyncWorker @AssistedInject constructor(

override suspend fun doWork(): Result =
withContext(dispatcher) {
crashlyticsLog("Fetching device config state")
showProgressNotification()
crashlyticsLog("Fetching device config state")

try {
val state = configRepository.getDeviceState()
Expand Down