Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Adds lifecycle-aware datalayer snippets #812

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
garanj merged 1 commit into main from datalayer-lifecycle-2
Feb 24, 2026
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import androidx.activity.ComponentActivity
import androidx.lifecycle.lifecycleScope
import com.example.wear.R
import com.google.android.gms.common.GoogleApiAvailability
import com.google.android.gms.common.api.AvailabilityException
import com.google.android.gms.common.api.GoogleApi
import com.google.android.gms.tasks.Task
import com.google.android.gms.tasks.Tasks
import com.google.android.gms.wearable.Asset
import com.google.android.gms.wearable.DataClient
import com.google.android.gms.wearable.DataEvent
Expand All @@ -36,9 +36,11 @@ import com.google.android.gms.wearable.PutDataMapRequest
import com.google.android.gms.wearable.PutDataRequest
import com.google.android.gms.wearable.Wearable
import java.io.ByteArrayOutputStream
import java.io.InputStream
import java.util.concurrent.ExecutionException
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.tasks.await
import kotlinx.coroutines.withContext

private const val TAG = "DataLayer"

Expand Down Expand Up @@ -169,24 +171,29 @@ class DataLayerActivity2 : ComponentActivity(), DataClient.OnDataChangedListener
dataEvents
.filter { it.type == DataEvent.TYPE_CHANGED && it.dataItem.uri.path == "/image" }
.forEach { event ->
val bitmap: Bitmap? = DataMapItem.fromDataItem(event.dataItem)
val asset = DataMapItem.fromDataItem(event.dataItem)
.dataMap.getAsset("profileImage")
?.let { asset -> loadBitmapFromAsset(asset) }
// Do something with the bitmap

asset?.let { safeAsset ->
lifecycleScope.launch {
val bitmap = loadBitmapFromAsset(safeAsset)
// Do something with the bitmap
}
}
}
}

fun loadBitmapFromAsset(asset: Asset): Bitmap? {
// Convert asset into a file descriptor and block until it's ready
val assetInputStream: InputStream? =
Tasks.await(Wearable.getDataClient(this).getFdForAsset(asset))
?.inputStream
private suspend fun loadBitmapFromAsset(asset: Asset): Bitmap? = withContext(Dispatchers.IO) {
try {
val assetResult = Wearable.getDataClient(this@DataLayerActivity2)
.getFdForAsset(asset)
.await()

return assetInputStream?.let { inputStream ->
// Decode the stream into a bitmap
BitmapFactory.decodeStream(inputStream)
} ?: run {
// Requested an unknown asset
assetResult?.inputStream?.use { inputStream ->
BitmapFactory.decodeStream(inputStream)
}
} catch (e: Exception) {
e.printStackTrace()
null
}
}
Expand Down Expand Up @@ -215,23 +222,19 @@ private fun handleTaskComplete() { }
// [END android_wear_datalayer_async_call]

// [START android_wear_datalayer_sync_call]
private fun Context.sendDataSync(count: Int) {
// Create a data item with the path and data to be sent
val putDataReq: PutDataRequest = PutDataMapRequest.create("/count").run {
private fun Context.sendDataSync(count: Int) = runBlocking {
val putDataReq = PutDataMapRequest.create("/count").run {
dataMap.putInt("count_key", count)
asPutDataRequest()
}
// Create a task to send the data to the data layer
val task: Task<DataItem> = Wearable.getDataClient(this).putDataItem(putDataReq)

try {
Tasks.await(task).apply {
// Add your logic here
}
} catch (e: ExecutionException) {
// TODO: Handle exception
} catch (e: InterruptedException) {
// TODO: Handle exception
Thread.currentThread().interrupt()
val result = Wearable.getDataClient(this@sendDataSync)
.putDataItem(putDataReq)
.await()
// Logic for success
} catch (e: Exception) {
// Handle failure
}
}
// [END android_wear_datalayer_sync_call]
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2026 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.wear.snippets.datalayer

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import com.google.android.gms.wearable.DataClient
import com.google.android.gms.wearable.DataEventBuffer
import com.google.android.gms.wearable.Wearable

// [START android_wear_datalayer_lifecycle_observer]
class WearDataLayerObserver(
private val dataClient: DataClient,
private val onDataReceived: (DataEventBuffer) -> Unit
) : DefaultLifecycleObserver, DataClient.OnDataChangedListener {

// Implementation of the DataClient listener
override fun onDataChanged(dataEvents: DataEventBuffer) {
onDataReceived(dataEvents)
}

// Automatically register when the Activity starts
override fun onResume(owner: LifecycleOwner) {
dataClient.addListener(this)
}

// Automatically unregister when the Activity pauses
override fun onPause(owner: LifecycleOwner) {
dataClient.removeListener(this)
}
}
// [END android_wear_datalayer_lifecycle_observer]

// [START android_wear_datalayer_lifecycle_activity]
class DataLayerLifecycleActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val dataClient = Wearable.getDataClient(this)

// Create the observer and link it to the activity's lifecycle
val wearObserver = WearDataLayerObserver(dataClient) { dataEvents ->
handleDataEvents(dataEvents)
}

lifecycle.addObserver(wearObserver)
}

private fun handleDataEvents(dataEvents: DataEventBuffer) {
// ... filter and process events ...
}
}
// [END android_wear_datalayer_lifecycle_activity]
Loading

AltStyle によって変換されたページ (->オリジナル) /