Skip to content

Commit 398a37b

Browse files
committed
Add a background task
1 parent ca0a105 commit 398a37b

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed

app/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ dependencies {
7575
implementation(project(":OpacityCore"))
7676
implementation(libs.androidx.espresso.intents)
7777
implementation(libs.kotlinx.serialization.json)
78+
implementation(libs.androidx.work.runtime.ktx)
79+
implementation(libs.work.runtime.ktx)
7880
testImplementation(libs.junit)
7981
androidTestImplementation(libs.androidx.junit)
8082
androidTestImplementation(libs.androidx.espresso.core)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.opacity.example
2+
3+
import android.content.Context
4+
import android.util.Log
5+
import androidx.work.CoroutineWorker
6+
import androidx.work.WorkerParameters
7+
import com.opacitylabs.opacitycore.OpacityCore
8+
9+
class GitHubProfileWorker(
10+
context: Context,
11+
params: WorkerParameters
12+
) : CoroutineWorker(context, params) {
13+
14+
override suspend fun doWork(): Result {
15+
val startTime = System.currentTimeMillis()
16+
Log.d(TAG, "🚀 Background task started at $startTime")
17+
18+
return try {
19+
OpacityCore.get("github:profile", null)
20+
21+
val endTime = System.currentTimeMillis()
22+
val duration = (endTime - startTime) / 1000.0
23+
Log.d(TAG, "✅ SUCCESS at $endTime: GitHub profile fetched")
24+
Log.d(TAG, "⏱️ Duration: $duration seconds")
25+
26+
Result.success()
27+
} catch (e: Exception) {
28+
Log.e(TAG, "❌ FAILURE at ${System.currentTimeMillis()}: Failed to fetch GitHub profile", e)
29+
Result.retry()
30+
}
31+
}
32+
33+
companion object {
34+
private const val TAG = "GitHubProfileWorker"
35+
}
36+
}

app/src/main/java/com/opacitylabs/opacitycoreexample/MainActivity.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ import androidx.compose.runtime.remember
2121
import androidx.compose.runtime.setValue
2222
import androidx.compose.ui.Modifier
2323
import androidx.lifecycle.lifecycleScope
24+
import androidx.work.Constraints
25+
import androidx.work.ExistingPeriodicWorkPolicy
26+
import androidx.work.NetworkType
27+
import androidx.work.PeriodicWorkRequestBuilder
28+
import androidx.work.WorkManager
2429
import com.opacitylabs.opacitycore.JsonConverter
2530
import com.opacitylabs.opacitycore.OpacityCore
2631
import com.opacitylabs.opacitycore.OpacityError
@@ -29,6 +34,8 @@ import kotlinx.coroutines.launch
2934
import kotlinx.serialization.json.Json
3035
import java.io.BufferedReader
3136
import java.io.InputStreamReader
37+
import java.util.concurrent.TimeUnit
38+
import com.opacity.example.GitHubProfileWorker
3239

3340
class MainActivity : ComponentActivity() {
3441

@@ -235,5 +242,24 @@ class MainActivity : ComponentActivity() {
235242
}
236243
}
237244

245+
scheduleGitHubProfileFetch()
246+
}
247+
248+
private fun scheduleGitHubProfileFetch() {
249+
val constraints = Constraints.Builder()
250+
.setRequiredNetworkType(NetworkType.CONNECTED)
251+
.build()
252+
253+
val workRequest = PeriodicWorkRequestBuilder<GitHubProfileWorker>(
254+
15, TimeUnit.MINUTES
255+
)
256+
.setConstraints(constraints)
257+
.build()
258+
259+
WorkManager.getInstance(applicationContext).enqueueUniquePeriodicWork(
260+
"GitHubProfileFetch",
261+
ExistingPeriodicWorkPolicy.KEEP,
262+
workRequest
263+
)
238264
}
239265
}

gradle/libs.versions.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@ appcompat = "1.7.0"
1414
material = "1.12.0"
1515
securityCrypto = "1.1.0-alpha06"
1616
espressoIntents = "3.6.1"
17+
workManager = "2.9.1"
18+
workRuntimeKtx = "2.10.5"
1719

1820
[libraries]
1921
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
2022
androidx-security-crypto = { module = "androidx.security:security-crypto", version.ref = "securityCrypto" }
23+
androidx-work-runtime-ktx = { group = "androidx.work", name = "work-runtime-ktx", version.ref = "workManager" }
2124
geckoview = { module = "org.mozilla.geckoview:geckoview", version.ref = "geckoview" }
2225
junit = { group = "junit", name = "junit", version.ref = "junit" }
2326
androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" }
@@ -37,6 +40,7 @@ material = { group = "com.google.android.material", name = "material", version.r
3740
kotlinx-serialization-json = { group = "org.jetbrains.kotlinx", name = "kotlinx-serialization-json", version = "1.6.0" }
3841
androidx-espresso-intents = { group = "androidx.test.espresso", name = "espresso-intents", version.ref = "espressoIntents" }
3942
compose-compiler = { module = "androidx.compose.compiler:compiler", version.ref = "compose-compiler" }
43+
work-runtime-ktx = { group = "androidx.work", name = "work-runtime-ktx", version.ref = "workRuntimeKtx" }
4044

4145
[plugins]
4246
android-application = { id = "com.android.application", version.ref = "agp" }

0 commit comments

Comments
 (0)