Skip to content

Commit

Permalink
Start implementing alarm scheduling with WorkManager
Browse files Browse the repository at this point in the history
  • Loading branch information
rumboalla committed Jul 19, 2023
1 parent 2b5bc29 commit d66ac8d
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 2 deletions.
5 changes: 3 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ dependencies {
implementation 'androidx.activity:activity-compose:1.7.2'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.1'
implementation 'androidx.lifecycle:lifecycle-runtime-compose:2.6.1'
implementation "androidx.compose.ui:ui:1.4.3"
implementation "androidx.compose.ui:ui-tooling-preview:1.4.3"
implementation 'androidx.compose.ui:ui:1.4.3'
implementation 'androidx.compose.ui:ui-tooling-preview:1.4.3'
implementation 'androidx.compose.material3:material3:1.1.1'
implementation 'androidx.work:work-runtime-ktx:2.8.1'
implementation 'io.insert-koin:koin-android:3.4.2'
implementation 'io.insert-koin:koin-androidx-compose:3.4.2'
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
xmlns:tools="http://schemas.android.com/tools"
>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission
android:name="android.permission.QUERY_ALL_PACKAGES"
tools:ignore="QueryAllPackagesPermission"
Expand All @@ -30,5 +31,10 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".receiver.BootReceiver" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
18 changes: 18 additions & 0 deletions app/src/main/java/com/apkupdater/receiver/BootReceiver.kt
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)
}
}

}
10 changes: 10 additions & 0 deletions app/src/main/java/com/apkupdater/ui/screen/SettingsScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@ package com.apkupdater.ui.screen

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import com.apkupdater.R
import com.apkupdater.ui.component.SliderSetting
import com.apkupdater.ui.component.SwitchSetting
import com.apkupdater.ui.component.TitleText
import com.apkupdater.viewmodel.SettingsViewModel
import com.apkupdater.worker.UpdatesWorker
import org.koin.androidx.compose.koinViewModel


Expand Down Expand Up @@ -60,6 +63,13 @@ fun SettingsScreen(viewModel: SettingsViewModel = koinViewModel()) = Column {
{ viewModel.setIgnoreBeta(it) },
stringResource(R.string.ignore_beta)
)

val context = LocalContext.current
Button({
UpdatesWorker.launch(context)
}) {

}
}

@OptIn(ExperimentalMaterial3Api::class)
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/java/com/apkupdater/util/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import java.security.MessageDigest
import java.util.Calendar
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext

Expand Down Expand Up @@ -62,3 +63,11 @@ fun PackageInfo.getSignatureHash(): String = runCatching {
signatures[0].toByteArray().toSha1()
}
}.getOrDefault("")

fun millisUntilHour(hour: Int): Long {
val calendar = Calendar.getInstance()
if (calendar.get(Calendar.HOUR_OF_DAY) >= hour) calendar.add(Calendar.HOUR, 24)
calendar.set(Calendar.HOUR_OF_DAY, hour)
calendar.set(Calendar.MINUTE, 0)
return calendar.timeInMillis - System.currentTimeMillis()
}
47 changes: 47 additions & 0 deletions app/src/main/java/com/apkupdater/worker/UpdatesWorker.kt
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()
}

}

0 comments on commit d66ac8d

Please sign in to comment.