Skip to content

Commit

Permalink
Stop using WorkManager internals
Browse files Browse the repository at this point in the history
Updating the WorkManager version to `2.8.0` which added `Worker.getForegroundInfo` ([release notes](https://developer.android.com/jetpack/androidx/releases/work#2.8.0)) which removes the need to mess with `ListenableFuture`.

Fixes #2650
  • Loading branch information
pyricau committed Apr 17, 2024
1 parent b324c96 commit e47e474
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ kotlin = "1.8.21"
coroutines = "1.7.3"
androidXTest = "1.1.0"
androidXJunit = "1.1.3"
workManager = "2.7.0"
workManager = "2.8.0"

[libraries]
gradlePlugin-android = { module = "com.android.tools.build:gradle", version = "8.0.0" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import androidx.work.Data
import androidx.work.ForegroundInfo
import androidx.work.Worker
import androidx.work.WorkerParameters
import androidx.work.impl.utils.futures.SettableFuture
import com.google.common.util.concurrent.ListenableFuture
import com.squareup.leakcanary.core.R
import leakcanary.EventListener.Event

internal class HeapAnalyzerWorker(appContext: Context, workerParams: WorkerParameters) :
internal class HeapAnalyzerWorker(
appContext: Context,
workerParams: WorkerParameters
) :
Worker(appContext, workerParams) {
override fun doWork(): Result {
val doneEvent =
Expand All @@ -22,8 +23,8 @@ internal class HeapAnalyzerWorker(appContext: Context, workerParams: WorkerParam
return Result.success()
}

override fun getForegroundInfoAsync(): ListenableFuture<ForegroundInfo> {
return applicationContext.heapAnalysisForegroundInfoAsync()
override fun getForegroundInfo(): ForegroundInfo {
return applicationContext.heapAnalysisForegroundInfo()
}

companion object {
Expand All @@ -36,21 +37,17 @@ internal class HeapAnalyzerWorker(appContext: Context, workerParams: WorkerParam
inline fun <reified T> Data.asEvent(): T =
Serializables.fromByteArray<T>(getByteArray(EVENT_BYTES)!!)!!

fun Context.heapAnalysisForegroundInfoAsync(): ListenableFuture<ForegroundInfo> {
val infoFuture = SettableFuture.create<ForegroundInfo>()
fun Context.heapAnalysisForegroundInfo(): ForegroundInfo {
val builder = Notification.Builder(this)
.setContentTitle(getString(R.string.leak_canary_notification_analysing))
.setContentText("LeakCanary is working.")
.setProgress(100, 0, true)
val notification =
Notifications.buildNotification(this, builder, NotificationType.LEAKCANARY_LOW)
infoFuture.set(
ForegroundInfo(
R.id.leak_canary_notification_analyzing_heap,
notification
)
return ForegroundInfo(
R.id.leak_canary_notification_analyzing_heap,
notification
)
return infoFuture
}
}
}

0 comments on commit e47e474

Please sign in to comment.