Skip to content

Commit c3472d3

Browse files
Snippets for /training/wearables/data/data-items (#696)
1 parent 07fc354 commit c3472d3

File tree

1 file changed

+82
-0
lines changed
  • wear/src/main/java/com/example/wear/snippets/com/example/wear/snippets/datalayer

1 file changed

+82
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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.com.example.wear.snippets.datalayer
18+
19+
import androidx.activity.ComponentActivity
20+
import com.google.android.gms.tasks.Task
21+
import com.google.android.gms.wearable.DataClient
22+
import com.google.android.gms.wearable.DataEvent
23+
import com.google.android.gms.wearable.DataEventBuffer
24+
import com.google.android.gms.wearable.DataItem
25+
import com.google.android.gms.wearable.DataMapItem
26+
import com.google.android.gms.wearable.PutDataMapRequest
27+
import com.google.android.gms.wearable.PutDataRequest
28+
import com.google.android.gms.wearable.Wearable
29+
30+
class DataLayerActivity : ComponentActivity(), DataClient.OnDataChangedListener {
31+
private val dataClient by lazy { Wearable.getDataClient(this) }
32+
private val messageClient by lazy { Wearable.getMessageClient(this) }
33+
private val capabilityClient by lazy { Wearable.getCapabilityClient(this) }
34+
35+
private var count = 0
36+
37+
override fun onResume() {
38+
super.onResume()
39+
Wearable.getDataClient(this).addListener(this)
40+
}
41+
42+
override fun onPause() {
43+
super.onPause()
44+
Wearable.getDataClient(this).removeListener(this)
45+
}
46+
47+
// [START android_wear_datalayer_increasecounter]
48+
private fun increaseCounter() {
49+
val putDataReq: PutDataRequest = PutDataMapRequest.create("/count").run {
50+
dataMap.putInt(COUNT_KEY, count++)
51+
asPutDataRequest()
52+
}
53+
val putDataTask: Task<DataItem> = dataClient.putDataItem(putDataReq)
54+
}
55+
// [END android_wear_datalayer_increasecounter]
56+
57+
// [START android_wear_datalayer_ondatachangedlistener]
58+
override fun onDataChanged(dataEvents: DataEventBuffer) {
59+
60+
dataEvents.forEach { event ->
61+
// DataItem changed
62+
if (event.type == DataEvent.TYPE_CHANGED) {
63+
event.dataItem.also { item ->
64+
if (item.uri.path?.compareTo("/count") == 0) {
65+
DataMapItem.fromDataItem(item).dataMap.apply {
66+
updateCount(getInt(COUNT_KEY))
67+
}
68+
}
69+
}
70+
} else if (event.type == DataEvent.TYPE_DELETED) {
71+
// DataItem deleted
72+
}
73+
}
74+
}
75+
// [END android_wear_datalayer_ondatachangedlistener]
76+
77+
private fun updateCount(int: Int) {
78+
}
79+
companion object {
80+
private const val COUNT_KEY = "com.example.key.count"
81+
}
82+
}

0 commit comments

Comments
 (0)