Permalink
Browse files

Use kotlinx-coroutines-play-services to await() Tasks

  • Loading branch information...
ianhanniballake committed Jan 28, 2019
1 parent 35a424e commit ee293208acd6e2ae2fa579cf253b0681dc8e115d
@@ -99,6 +99,7 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlinVersion"
api "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion"
api "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutinesVersion"
api "org.jetbrains.kotlinx:kotlinx-coroutines-play-services:$coroutinesVersion"
implementation "androidx.multidex:multidex:$multidexVersion"
implementation "com.squareup.okhttp3:okhttp:$okhttpVersion"
implementation "com.github.bumptech.glide:glide:$glideVersion"
@@ -28,16 +28,14 @@ import com.google.android.apps.muzei.util.observeNonNull
import com.google.android.gms.common.ConnectionResult
import com.google.android.gms.common.GoogleApiAvailability
import com.google.android.gms.common.api.AvailabilityException
import com.google.android.gms.tasks.Tasks
import com.google.android.gms.wearable.Asset
import com.google.android.gms.wearable.DataItem
import com.google.android.gms.wearable.PutDataMapRequest
import com.google.android.gms.wearable.Wearable
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.tasks.await
import java.io.ByteArrayOutputStream
import java.util.concurrent.ExecutionException
import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeoutException

/**
@@ -65,8 +63,7 @@ class WearableController(private val context: Context) : DefaultLifecycleObserve
}
val dataClient = Wearable.getDataClient(context)
try {
Tasks.await(GoogleApiAvailability.getInstance()
.checkApiAvailability(dataClient), 5, TimeUnit.SECONDS)
GoogleApiAvailability.getInstance().checkApiAvailability(dataClient).await()
} catch (e: ExecutionException) {
if (e.cause is AvailabilityException) {
val connectionResult = (e.cause as AvailabilityException)
@@ -98,7 +95,7 @@ class WearableController(private val context: Context) : DefaultLifecycleObserve
dataMap.putAsset("image", asset)
}
try {
Tasks.await<DataItem>(dataClient.putDataItem(dataMapRequest.asPutDataRequest().setUrgent()))
dataClient.putDataItem(dataMapRequest.asPutDataRequest().setUrgent()).await()
} catch (e: ExecutionException) {
Log.w(TAG, "Error uploading artwork to Wear", e)
} catch (e: InterruptedException) {
@@ -86,6 +86,7 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlinVersion"
api "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion"
api "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutinesVersion"
api "org.jetbrains.kotlinx:kotlinx-coroutines-play-services:$coroutinesVersion"
implementation "android.arch.work:work-runtime-ktx:$workManagerVersion"
implementation "androidx.core:core-ktx:$coreVersion"
implementation "androidx.lifecycle:lifecycle-livedata-core-ktx:$lifecycleVersion"
@@ -40,18 +40,16 @@ import androidx.core.content.ContextCompat
import androidx.core.content.edit
import com.google.android.apps.muzei.ChooseProviderActivity
import com.google.android.apps.muzei.util.toast
import com.google.android.gms.tasks.Tasks
import com.google.android.gms.wearable.CapabilityClient
import com.google.android.gms.wearable.CapabilityInfo
import com.google.android.gms.wearable.Node
import com.google.android.gms.wearable.Wearable
import com.google.android.wearable.intent.RemoteIntent
import com.google.android.wearable.playstore.PlayStoreAvailability
import com.google.firebase.analytics.FirebaseAnalytics
import kotlinx.coroutines.tasks.await
import net.nurik.roman.muzei.R
import java.util.TreeSet
import java.util.concurrent.ExecutionException
import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeoutException

class ActivateMuzeiIntentService : IntentService(TAG) {
@@ -75,7 +73,7 @@ class ActivateMuzeiIntentService : IntentService(TAG) {
}
}

fun checkForPhoneApp(context: Context) {
suspend fun checkForPhoneApp(context: Context) {
val node: Node?
try {
node = getNode(context, CapabilityClient.FILTER_ALL)
@@ -176,11 +174,10 @@ class ActivateMuzeiIntentService : IntentService(TAG) {
}

@Throws(TimeoutException::class)
private fun getNode(context: Context, capability: Int): Node? {
private suspend fun getNode(context: Context, capability: Int): Node? {
val capabilityClient = Wearable.getCapabilityClient(context)
val nodes: Set<Node> = try {
Tasks.await<CapabilityInfo>(capabilityClient.getCapability(
"activate_muzei", capability), 1, TimeUnit.SECONDS).nodes
capabilityClient.getCapability("activate_muzei", capability).await().nodes
} catch (e: ExecutionException) {
Log.e(TAG, "Error getting all capability info", e)
TreeSet()
@@ -23,12 +23,12 @@ import android.util.Log
import com.google.android.apps.muzei.api.provider.Artwork
import com.google.android.apps.muzei.api.provider.MuzeiArtProvider
import com.google.android.apps.muzei.util.toastFromBackground
import com.google.android.gms.tasks.Tasks
import com.google.android.gms.wearable.CapabilityClient
import com.google.android.gms.wearable.CapabilityInfo
import com.google.android.gms.wearable.Node
import com.google.android.gms.wearable.Wearable
import com.google.firebase.analytics.FirebaseAnalytics
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.tasks.await
import net.nurik.roman.muzei.R
import java.io.File
import java.io.FileInputStream
@@ -59,14 +59,14 @@ class DataLayerArtProvider : MuzeiArtProvider() {
override fun onCommand(artwork: Artwork, id: Int) {
val context = context ?: return
when(id) {
OPEN_ON_PHONE_ACTION -> {
OPEN_ON_PHONE_ACTION -> runBlocking {
// Open on Phone action
val capabilityClient = Wearable.getCapabilityClient(context)
val nodes: Set<Node> = try {
// We use activate_muzei for compatibility with
// older versions of Muzei's phone app
Tasks.await<CapabilityInfo>(capabilityClient.getCapability(
"activate_muzei", CapabilityClient.FILTER_REACHABLE)).nodes
capabilityClient.getCapability("activate_muzei",
CapabilityClient.FILTER_REACHABLE).await().nodes
} catch (e: ExecutionException) {
Log.e(TAG, "Error getting reachable capability info", e)
TreeSet()
@@ -92,8 +92,8 @@ class DataLayerArtProvider : MuzeiArtProvider() {
try {
// We use notification/open for compatibility with
// older versions of Muzei's phone app
Tasks.await(messageClient.sendMessage(node.id,
"notification/open", null))
messageClient.sendMessage(node.id,
"notification/open", null).await()
} catch (e: ExecutionException) {
Log.w(TAG, "Unable to send Open on phone message to ${node.id}", e)
} catch (e: InterruptedException) {
@@ -21,16 +21,15 @@ import android.content.Context
import android.content.pm.PackageManager
import android.net.Uri
import android.util.Log
import androidx.work.CoroutineWorker
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.WorkManager
import androidx.work.Worker
import androidx.work.WorkerParameters
import com.google.android.apps.muzei.api.provider.ProviderContract
import com.google.android.apps.muzei.wearable.toArtwork
import com.google.android.gms.tasks.Tasks
import com.google.android.gms.wearable.DataClient
import com.google.android.gms.wearable.DataMapItem
import com.google.android.gms.wearable.Wearable
import kotlinx.coroutines.tasks.await
import net.nurik.roman.muzei.BuildConfig
import net.nurik.roman.muzei.BuildConfig.DATA_LAYER_AUTHORITY
import java.io.FileOutputStream
@@ -43,7 +42,7 @@ import java.util.concurrent.ExecutionException
class DataLayerLoadWorker(
context: Context,
workerParams: WorkerParameters
) : Worker(context, workerParams) {
) : CoroutineWorker(context, workerParams) {

companion object {
private const val TAG = "DataLayerLoadJobService"
@@ -57,14 +56,14 @@ class DataLayerLoadWorker(
}
}

override fun doWork(): Result {
override suspend fun doWork(): Result {
if (BuildConfig.DEBUG) {
Log.d(TAG, "Loading artwork from the DataLayer")
}
val dataClient = Wearable.getDataClient(applicationContext)
try {
val dataItemBuffer = Tasks.await(dataClient.getDataItems(
Uri.parse("wear://*/artwork")))
val dataItemBuffer = dataClient.getDataItems(
Uri.parse("wear://*/artwork")).await()
if (!dataItemBuffer.status.isSuccess) {
if (BuildConfig.DEBUG) {
Log.i(TAG, "Error getting artwork DataItem")
@@ -81,8 +80,8 @@ class DataLayerLoadWorker(
}
return Result.failure()
}
val result = Tasks.await<DataClient.GetFdForAssetResponse>(
dataClient.getFdForAsset(dataMap.getAsset("image")))
val result = dataClient.getFdForAsset(
dataMap.getAsset("image")).await()
try {
result.inputStream.use { input ->
FileOutputStream(DataLayerArtProvider.getAssetFile(applicationContext)).use { out ->

0 comments on commit ee29320

Please sign in to comment.