Skip to content

Commit 6c67e34

Browse files
authored
Adds snippets for hardware buttons (#655)
1 parent fcf8a03 commit 6c67e34

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed

gradle/libs.versions.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ wearComposeMaterial3 = "1.5.3"
8080
wearOngoing = "1.1.0"
8181
wearToolingPreview = "1.0.0"
8282
webkit = "1.14.0"
83+
wearInput = "1.2.0"
8384

8485
[libraries]
8586
accompanist-adaptive = "com.google.accompanist:accompanist-adaptive:0.37.3"
@@ -198,6 +199,7 @@ play-services-wearable = { module = "com.google.android.gms:play-services-wearab
198199
validator-push = { module = "com.google.android.wearable.watchface.validator:validator-push", version.ref = "validatorPush" }
199200
wear-compose-material = { module = "androidx.wear.compose:compose-material", version.ref = "wearComposeMaterial" }
200201
wear-compose-material3 = { module = "androidx.wear.compose:compose-material3", version.ref = "wearComposeMaterial3" }
202+
androidx-wear-input = { group = "androidx.wear", name = "wear-input", version.ref = "wearInput" }
201203

202204
[plugins]
203205
android-application = { id = "com.android.application", version.ref = "androidGradlePlugin" }

wear/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ dependencies {
5555
implementation(libs.androidx.core.ktx)
5656
implementation(libs.androidx.media3.exoplayer)
5757
implementation(libs.androidx.media3.ui)
58+
implementation(libs.androidx.wear.input)
5859
val composeBom = platform(libs.androidx.compose.bom)
5960
implementation(composeBom)
6061
androidTestImplementation(composeBom)

wear/src/main/AndroidManifest.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@
4141
android:name="com.google.android.wearable.standalone"
4242
android:value="true" />
4343

44+
<activity
45+
android:name=".snippets.hardwarebuttons.HardwareButtonsActivity"
46+
android:exported="true"
47+
android:taskAffinity=""
48+
android:theme="@android:style/Theme.DeviceDefault">
49+
<intent-filter>
50+
<action android:name="android.intent.action.MAIN" />
51+
52+
<category android:name="android.intent.category.LAUNCHER" />
53+
</intent-filter>
54+
</activity>
4455
<activity
4556
android:name=".snippets.m3.MainActivity"
4657
android:exported="true"
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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.hardwarebuttons
18+
19+
import android.app.Activity
20+
import android.content.Context
21+
import android.util.Log
22+
import android.view.KeyEvent
23+
import androidx.activity.ComponentActivity
24+
import androidx.wear.input.WearableButtons
25+
26+
class HardwareButtonsActivity : ComponentActivity() {
27+
// [START android_wear_hardware_buttons_events]
28+
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
29+
return if (event?.repeatCount == 0) {
30+
when (keyCode) {
31+
KeyEvent.KEYCODE_STEM_1 -> {
32+
Log.d(TAG, "KEYCODE_STEM_1 pressed")
33+
true
34+
}
35+
KeyEvent.KEYCODE_STEM_2 -> {
36+
Log.d(TAG, "KEYCODE_STEM_2 pressed")
37+
true
38+
}
39+
else -> {
40+
super.onKeyDown(keyCode, event)
41+
}
42+
}
43+
} else {
44+
super.onKeyDown(keyCode, event)
45+
}
46+
}
47+
// [END android_wear_hardware_buttons_events]
48+
49+
fun hardwareButtonsCount(context: Context, activity: Activity) {
50+
// [START android_wear_hardware_buttons_count]
51+
val count = WearableButtons.getButtonCount(context)
52+
53+
if (count > 1) {
54+
Log.d(TAG, "More than one button available")
55+
}
56+
57+
val buttonInfo = WearableButtons.getButtonInfo(
58+
activity,
59+
KeyEvent.KEYCODE_STEM_1
60+
)
61+
62+
if (buttonInfo == null) {
63+
// KEYCODE_STEM_1 is unavailable
64+
Log.d(TAG, "KEYCODE_STEM_1 not available")
65+
} else {
66+
// KEYCODE_STEM_1 is present on the device
67+
Log.d(TAG, "KEYCODE_STEM_1 is present on the device")
68+
}
69+
// [END android_wear_hardware_buttons_count]
70+
}
71+
}
72+
private const val TAG = "HardwareButtons"

0 commit comments

Comments
 (0)