-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
218 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package app | ||
|
||
import android.content.Context | ||
import android.content.pm.ApplicationInfo | ||
|
||
fun Context.isDebuggable(): Boolean { | ||
return applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE != 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package sync | ||
|
||
import android.content.Context | ||
import androidx.work.Constraints | ||
import androidx.work.ExistingPeriodicWorkPolicy | ||
import androidx.work.NetworkType | ||
import androidx.work.PeriodicWorkRequestBuilder | ||
import androidx.work.WorkManager | ||
import app.isDebuggable | ||
import java.util.concurrent.TimeUnit | ||
|
||
class BackgroundSyncScheduler(private val context: Context) { | ||
fun schedule() { | ||
val workManager = WorkManager.getInstance(context) | ||
|
||
val constraints = Constraints.Builder() | ||
.setRequiredNetworkType(NetworkType.UNMETERED) | ||
.build() | ||
|
||
val periodicSyncRequest = if (context.isDebuggable()) { | ||
PeriodicWorkRequestBuilder<SyncWorker>( | ||
repeatInterval = 1, | ||
repeatIntervalTimeUnit = TimeUnit.HOURS, | ||
) | ||
.setConstraints(constraints) | ||
.setInitialDelay(1, TimeUnit.HOURS) | ||
} else { | ||
PeriodicWorkRequestBuilder<SyncWorker>( | ||
repeatInterval = 1, | ||
repeatIntervalTimeUnit = TimeUnit.DAYS, | ||
) | ||
.setConstraints(constraints) | ||
.setInitialDelay(1, TimeUnit.DAYS) | ||
}.build() | ||
|
||
workManager.enqueueUniquePeriodicWork( | ||
WORK_NAME, | ||
ExistingPeriodicWorkPolicy.UPDATE, | ||
periodicSyncRequest, | ||
) | ||
} | ||
|
||
companion object { | ||
private const val WORK_NAME = "sync" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package sync | ||
|
||
import android.Manifest | ||
import android.app.Activity | ||
import android.app.NotificationChannel | ||
import android.app.NotificationManager | ||
import android.app.PendingIntent | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.content.pm.PackageManager | ||
import androidx.core.app.ActivityCompat | ||
import androidx.core.app.NotificationCompat | ||
import androidx.core.app.NotificationManagerCompat | ||
import androidx.core.content.getSystemService | ||
import app.isDebuggable | ||
import element.ElementsRepo | ||
import org.btcmap.R | ||
|
||
class SyncNotificationController(private val context: Context) { | ||
|
||
fun showPostSyncNotifications(elementsSyncReport: ElementsRepo.SyncReport?) { | ||
if (elementsSyncReport == null) { | ||
return | ||
} | ||
|
||
if (context.isDebuggable()) { | ||
createSyncSummaryNotificationChannel(context) | ||
} | ||
|
||
createNewMerchantsNotificationChannel(context) | ||
|
||
val intent = Intent(context, Activity::class.java).apply { | ||
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK | ||
} | ||
|
||
val pendingIntent = | ||
PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE) | ||
|
||
val builder = NotificationCompat.Builder(context, SYNC_SUMMARY_CHANNEL_ID) | ||
.setSmallIcon(R.drawable.area_placeholder_icon) | ||
.setContentTitle(context.getString(R.string.app_name)) | ||
.setContentText(elementsSyncReport.toString()) | ||
.setPriority(NotificationCompat.PRIORITY_DEFAULT) | ||
.setContentIntent(pendingIntent) | ||
.setAutoCancel(true) | ||
|
||
if (ActivityCompat.checkSelfPermission( | ||
context, | ||
Manifest.permission.POST_NOTIFICATIONS, | ||
) == PackageManager.PERMISSION_GRANTED | ||
) { | ||
with(NotificationManagerCompat.from(context)) { | ||
notify(SYNC_SUMMARY_NOTIFICATION_ID, builder.build()) | ||
} | ||
} | ||
} | ||
|
||
private fun createSyncSummaryNotificationChannel(context: Context) { | ||
val name = "Sync summary" | ||
val descriptionText = "Sync summary" | ||
val importance = NotificationManager.IMPORTANCE_DEFAULT | ||
val channel = NotificationChannel(SYNC_SUMMARY_CHANNEL_ID, name, importance).apply { | ||
description = descriptionText | ||
} | ||
val notificationManager = context.getSystemService<NotificationManager>()!! | ||
notificationManager.createNotificationChannel(channel) | ||
} | ||
|
||
private fun createNewMerchantsNotificationChannel(context: Context) { | ||
val name = "New merchants" | ||
val descriptionText = "New merchants" | ||
val importance = NotificationManager.IMPORTANCE_DEFAULT | ||
val channel = NotificationChannel(NEW_MERCHANTS_CHANNEL_ID, name, importance).apply { | ||
description = descriptionText | ||
} | ||
val notificationManager = context.getSystemService<NotificationManager>()!! | ||
notificationManager.createNotificationChannel(channel) | ||
} | ||
|
||
companion object { | ||
private const val SYNC_SUMMARY_CHANNEL_ID = "sync_summary" | ||
private const val SYNC_SUMMARY_NOTIFICATION_ID = 1 | ||
private const val NEW_MERCHANTS_CHANNEL_ID = "new_merchants" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package sync | ||
|
||
import android.content.Context | ||
import androidx.work.Worker | ||
import androidx.work.WorkerParameters | ||
import app.App | ||
import conf.ConfRepo | ||
import kotlinx.coroutines.runBlocking | ||
import org.koin.android.ext.android.get | ||
|
||
class SyncWorker(context: Context, workerParams: WorkerParameters) : Worker(context, workerParams) { | ||
override fun doWork() = runBlocking { doWorkAsync() } | ||
|
||
private suspend fun doWorkAsync(): Result { | ||
val app = applicationContext as App | ||
val conf = app.get<ConfRepo>().conf.value | ||
val sync = app.get<Sync>() | ||
|
||
if (conf.lastSyncDate == null) { | ||
return Result.retry() | ||
} | ||
|
||
sync.sync() | ||
|
||
return Result.success() | ||
} | ||
} |