Skip to content

Commit fcf8a03

Browse files
authored
Add location snippet (#652)
1 parent 3507dfb commit fcf8a03

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

wear/src/main/AndroidManifest.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@
7777
android:taskAffinity=""
7878
android:theme="@android:style/Theme.DeviceDefault"/>
7979

80+
<activity android:name=".snippets.location.LocationActivity"
81+
android:exported="true"
82+
android:taskAffinity=""
83+
android:theme="@android:style/Theme.DeviceDefault"/>
84+
8085
<!-- [START android_wear_tile_manifest] -->
8186
<service
8287
android:name=".snippets.tile.MyTileService"
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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.location
18+
19+
import android.content.pm.PackageManager
20+
import android.os.Bundle
21+
import androidx.activity.ComponentActivity
22+
import androidx.activity.compose.setContent
23+
import androidx.compose.foundation.layout.fillMaxWidth
24+
import androidx.compose.runtime.Composable
25+
import androidx.compose.ui.Modifier
26+
import androidx.wear.compose.foundation.lazy.TransformingLazyColumn
27+
import androidx.wear.compose.foundation.lazy.rememberTransformingLazyColumnState
28+
import androidx.wear.compose.material3.ListHeader
29+
import androidx.wear.compose.material3.ScreenScaffold
30+
import androidx.wear.compose.material3.Text
31+
import com.google.android.horologist.compose.layout.AppScaffold
32+
import com.google.android.horologist.compose.layout.ColumnItemType
33+
import com.google.android.horologist.compose.layout.rememberResponsiveColumnPadding
34+
35+
36+
// [START android_wear_location]
37+
class LocationActivity : ComponentActivity() {
38+
override fun onCreate(savedInstanceState: Bundle?) {
39+
super.onCreate(savedInstanceState)
40+
41+
// [START_EXCLUDE]
42+
setContent {
43+
WearApp(hasGps = { hasGps() })
44+
}
45+
// [END_EXCLUDE]
46+
}
47+
fun hasGps(): Boolean =
48+
packageManager.hasSystemFeature(PackageManager.FEATURE_LOCATION_GPS)
49+
}
50+
// [END android_wear_location]
51+
52+
@Composable
53+
fun WearApp(hasGps: () -> Boolean) {
54+
55+
val columnState = rememberTransformingLazyColumnState()
56+
val contentPadding = rememberResponsiveColumnPadding(
57+
first = ColumnItemType.ListHeader,
58+
last = ColumnItemType.Button,
59+
)
60+
AppScaffold {
61+
ScreenScaffold(
62+
scrollState = columnState,
63+
contentPadding = contentPadding
64+
) { contentPadding ->
65+
TransformingLazyColumn(
66+
state = columnState,
67+
contentPadding = contentPadding
68+
) {
69+
item {
70+
ListHeader(
71+
modifier = Modifier.fillMaxWidth()
72+
) {
73+
if (!hasGps()) {
74+
Text(text = "This hardware doesn't have GPS")
75+
// Fall back to functionality that doesn't use location or
76+
// warn the user that location function isn't available.
77+
}
78+
else {
79+
Text(text = "This hardware has GPS")
80+
}
81+
}
82+
}
83+
}
84+
}
85+
}
86+
}

0 commit comments

Comments
 (0)