-
Notifications
You must be signed in to change notification settings - Fork 228
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start implementing alarm scheduling with WorkManager
- Loading branch information
Showing
6 changed files
with
93 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.apkupdater.receiver | ||
|
||
import android.content.BroadcastReceiver | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.util.Log | ||
import com.apkupdater.worker.UpdatesWorker | ||
|
||
class BootReceiver : BroadcastReceiver() { | ||
|
||
override fun onReceive(context: Context, intent: Intent) { | ||
if (intent.action == "android.intent.action.BOOT_COMPLETED") { | ||
Log.e("BootReceiver", "Test") | ||
UpdatesWorker.launch(context) | ||
} | ||
} | ||
|
||
} |
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,47 @@ | ||
package com.apkupdater.worker | ||
|
||
import android.content.Context | ||
import android.util.Log | ||
import androidx.work.CoroutineWorker | ||
import androidx.work.ExistingPeriodicWorkPolicy | ||
import androidx.work.PeriodicWorkRequestBuilder | ||
import androidx.work.WorkManager | ||
import androidx.work.WorkerParameters | ||
import com.apkupdater.repository.UpdatesRepository | ||
import com.apkupdater.util.millisUntilHour | ||
import org.koin.core.component.KoinComponent | ||
import org.koin.core.component.inject | ||
import java.util.concurrent.TimeUnit | ||
|
||
class UpdatesWorker( | ||
context: Context, | ||
workerParams: WorkerParameters | ||
): CoroutineWorker(context, workerParams), KoinComponent { | ||
|
||
companion object { | ||
|
||
private const val TAG = "UpdatesWorker" | ||
|
||
fun launch(context: Context) { | ||
val request = PeriodicWorkRequestBuilder<UpdatesWorker>(1L, TimeUnit.DAYS) | ||
.setInitialDelay(millisUntilHour(12), TimeUnit.MILLISECONDS) | ||
.build() | ||
WorkManager.getInstance(context) | ||
.enqueueUniquePeriodicWork(TAG, ExistingPeriodicWorkPolicy.UPDATE, request) | ||
} | ||
|
||
} | ||
|
||
private val updatesRepository: UpdatesRepository by inject() | ||
|
||
override suspend fun doWork(): Result { | ||
Log.e("UpdatesWorker", "Start") | ||
updatesRepository.updates().collect { | ||
// TODO: Send notification | ||
Log.e("UpdatesWorker", "Got ${it.size} updates.") | ||
} | ||
Log.e("UpdatesWorker", "Stop") | ||
return Result.success() | ||
} | ||
|
||
} |