package moe.yuuta.workmode.suspend import android.content.Context import android.os.Bundle import moe.yuuta.workmode.access.AccessorStarter import moe.yuuta.workmode.access.DumpResult import moe.yuuta.workmode.suspend.data.TransferableSuspendedApp import java.util.concurrent.CompletableFuture import java.util.function.Function import java.util.function.Supplier /** * An async suspender which wraps suspend tasks and run them in the background */ class AsyncSuspender(private val mContext: Context) { fun isSuspended(packages: List): CompletableFuture = generalCall(Function { return@Function it.isPackageSuspended(packages) }) fun suspend(packages: List, suspended: Boolean): CompletableFuture> = generalCall( Function { return@Function it.suspend(packages, suspended) }) fun getInstalledApplicationsAcrossUser(flags: Int): CompletableFuture> = generalCall( Function { return@Function it.getInstalledApplicationsAcrossUser(flags) }) private fun generalCall(thenApply: java.util.function.Function): CompletableFuture { return CompletableFuture.supplyAsync(Supplier { return@Supplier AccessorStarter.start(mContext, true, thenApply) }) } fun applyFromSettings(): CompletableFuture = generalCall(Function { t -> t.apply() }) fun getSuspendedPackageAppExtras(packageInfo: TransferableSuspendedApp): CompletableFuture = generalCall(Function { return@Function it.getSuspendedPackageAppExtras(packageInfo) }) fun getSuspendedPackageLauncherExtras(packageInfo: TransferableSuspendedApp): CompletableFuture = generalCall(Function { return@Function it.getSuspendedPackageLauncherExtras(packageInfo) }) fun dump(packageInfo: TransferableSuspendedApp): CompletableFuture = generalCall(Function { return@Function it.dump(packageInfo) }) private data class DataWrapper(val accessorStarter: AccessorStarter?, val data: T?) }