|
| 1 | +/* |
| 2 | + * Copyright 2025 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.wear.snippets.datalayer |
| 18 | + |
| 19 | +import android.content.Context |
| 20 | +import android.graphics.Bitmap |
| 21 | +import android.graphics.BitmapFactory |
| 22 | +import androidx.activity.ComponentActivity |
| 23 | +import com.example.wear.R |
| 24 | +import com.google.android.gms.tasks.Task |
| 25 | +import com.google.android.gms.tasks.Tasks |
| 26 | +import com.google.android.gms.wearable.Asset |
| 27 | +import com.google.android.gms.wearable.DataClient |
| 28 | +import com.google.android.gms.wearable.DataEvent |
| 29 | +import com.google.android.gms.wearable.DataEventBuffer |
| 30 | +import com.google.android.gms.wearable.DataItem |
| 31 | +import com.google.android.gms.wearable.DataMapItem |
| 32 | +import com.google.android.gms.wearable.PutDataMapRequest |
| 33 | +import com.google.android.gms.wearable.PutDataRequest |
| 34 | +import com.google.android.gms.wearable.Wearable |
| 35 | +import java.io.ByteArrayOutputStream |
| 36 | +import java.io.InputStream |
| 37 | + |
| 38 | +class DataLayerActivity : ComponentActivity(), DataClient.OnDataChangedListener { |
| 39 | + private val dataClient by lazy { Wearable.getDataClient(this) } |
| 40 | + private val messageClient by lazy { Wearable.getMessageClient(this) } |
| 41 | + private val capabilityClient by lazy { Wearable.getCapabilityClient(this) } |
| 42 | + |
| 43 | + private var count = 0 |
| 44 | + |
| 45 | + override fun onResume() { |
| 46 | + super.onResume() |
| 47 | + Wearable.getDataClient(this).addListener(this) |
| 48 | + } |
| 49 | + |
| 50 | + override fun onPause() { |
| 51 | + super.onPause() |
| 52 | + Wearable.getDataClient(this).removeListener(this) |
| 53 | + } |
| 54 | + |
| 55 | + // [START android_wear_datalayer_increasecounter] |
| 56 | + private fun increaseCounter() { |
| 57 | + val putDataReq: PutDataRequest = PutDataMapRequest.create("/count").run { |
| 58 | + dataMap.putInt(COUNT_KEY, count++) |
| 59 | + asPutDataRequest() |
| 60 | + } |
| 61 | + val putDataTask: Task<DataItem> = dataClient.putDataItem(putDataReq) |
| 62 | + } |
| 63 | + // [END android_wear_datalayer_increasecounter] |
| 64 | + |
| 65 | + // [START android_wear_datalayer_ondatachangedlistener] |
| 66 | + override fun onDataChanged(dataEvents: DataEventBuffer) { |
| 67 | + |
| 68 | + dataEvents.forEach { event -> |
| 69 | + // DataItem changed |
| 70 | + if (event.type == DataEvent.TYPE_CHANGED) { |
| 71 | + event.dataItem.also { item -> |
| 72 | + if (item.uri.path?.compareTo("/count") == 0) { |
| 73 | + DataMapItem.fromDataItem(item).dataMap.apply { |
| 74 | + updateCount(getInt(COUNT_KEY)) |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } else if (event.type == DataEvent.TYPE_DELETED) { |
| 79 | + // DataItem deleted |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + // [END android_wear_datalayer_ondatachangedlistener] |
| 84 | + |
| 85 | + private fun updateCount(int: Int) { |
| 86 | + } |
| 87 | + companion object { |
| 88 | + private const val COUNT_KEY = "com.example.key.count" |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +// [START android_wear_sync_createasset] |
| 93 | +private fun createAssetFromBitmap(bitmap: Bitmap): Asset = |
| 94 | + ByteArrayOutputStream().let { byteStream -> |
| 95 | + bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteStream) |
| 96 | + Asset.createFromBytes(byteStream.toByteArray()) |
| 97 | + } |
| 98 | +// [END android_wear_sync_createasset] |
| 99 | + |
| 100 | +// [START android_wear_datalayer_imageputdata] |
| 101 | +private fun Context.sendImagePutDataRequest(): Task<DataItem> { |
| 102 | + |
| 103 | + val asset: Asset = createAssetFromBitmap(BitmapFactory.decodeResource(resources, R.drawable.ic_walk)) |
| 104 | + val request: PutDataRequest = PutDataRequest.create("/image").apply { |
| 105 | + putAsset("profileImage", asset) |
| 106 | + } |
| 107 | + val putTask: Task<DataItem> = Wearable.getDataClient(this).putDataItem(request) |
| 108 | + |
| 109 | + return putTask |
| 110 | +} |
| 111 | +// [END android_wear_datalayer_imageputdata] |
| 112 | + |
| 113 | +// [START android_wear_datalayer_imageputdatamap] |
| 114 | +private fun Context.sendImagePutDataMapRequest(): Task<DataItem> { |
| 115 | + |
| 116 | + val asset: Asset = createAssetFromBitmap(BitmapFactory.decodeResource(resources, R.drawable.ic_walk)) |
| 117 | + val request: PutDataRequest = PutDataMapRequest.create("/image").run { |
| 118 | + dataMap.putAsset("profileImage", asset) |
| 119 | + asPutDataRequest() |
| 120 | + } |
| 121 | + val putTask: Task<DataItem> = Wearable.getDataClient(this).putDataItem(request) |
| 122 | + |
| 123 | + return putTask |
| 124 | +} |
| 125 | +// [END android_wear_datalayer_imageputdatamap] |
| 126 | + |
| 127 | +class DataLayerActivity2 : ComponentActivity(), DataClient.OnDataChangedListener { |
| 128 | + // [START android_wear_datalayer_ondatachanged_assetextract] |
| 129 | + override fun onDataChanged(dataEvents: DataEventBuffer) { |
| 130 | + dataEvents |
| 131 | + .filter { it.type == DataEvent.TYPE_CHANGED && it.dataItem.uri.path == "/image" } |
| 132 | + .forEach { event -> |
| 133 | + val bitmap: Bitmap? = DataMapItem.fromDataItem(event.dataItem) |
| 134 | + .dataMap.getAsset("profileImage") |
| 135 | + ?.let { asset -> loadBitmapFromAsset(asset) } |
| 136 | + // Do something with the bitmap |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + fun loadBitmapFromAsset(asset: Asset): Bitmap? { |
| 141 | + // Convert asset into a file descriptor and block until it's ready |
| 142 | + val assetInputStream: InputStream? = |
| 143 | + Tasks.await(Wearable.getDataClient(this).getFdForAsset(asset)) |
| 144 | + ?.inputStream |
| 145 | + |
| 146 | + return assetInputStream?.let { inputStream -> |
| 147 | + // Decode the stream into a bitmap |
| 148 | + BitmapFactory.decodeStream(inputStream) |
| 149 | + } ?: run { |
| 150 | + // Requested an unknown asset |
| 151 | + null |
| 152 | + } |
| 153 | + } |
| 154 | + // [END android_wear_datalayer_ondatachanged_assetextract] |
| 155 | +} |
0 commit comments