Skip to content

Commit 3031c28

Browse files
committed
fix(ApiDemos,WearOS): fix toolbar title in LocationSourceDemo and add safe ambient mode initialization
1 parent f17d90a commit 3031c28

6 files changed

Lines changed: 30 additions & 8 deletions

File tree

ApiDemos/project/java-app/src/main/java/com/example/mapdemo/LocationSourceDemoActivity.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ protected void onCreate(Bundle savedInstanceState) {
9090
super.onCreate(savedInstanceState);
9191
setContentView(com.example.common_ui.R.layout.basic_demo);
9292

93+
androidx.appcompat.widget.Toolbar toolbar = findViewById(com.example.common_ui.R.id.top_bar);
94+
if (toolbar != null) {
95+
toolbar.setTitle(com.example.common_ui.R.string.location_source_demo_label);
96+
}
97+
9398
mLocationSource = new LongPressLocationSource();
9499

95100
SupportMapFragment mapFragment =

ApiDemos/project/kotlin-app/src/main/java/com/example/kotlindemos/LocationSourceDemoActivity.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class LocationSourceDemoActivity : SamplesBaseActivity() {
4545
override fun onCreate(savedInstanceState: Bundle?) {
4646
super.onCreate(savedInstanceState)
4747
setContentView(R.layout.basic_demo)
48+
findViewById<com.google.android.material.appbar.MaterialToolbar>(R.id.top_bar)?.setTitle(R.string.location_source_demo_label)
4849
val mapFragment = supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment
4950
lifecycleScope.launch {
5051
val map = mapFragment.awaitMap()

WearOS/Wearable/src/main/java/com/example/wearosmap/AmbientActivity.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,12 @@ public void onCreate(Bundle savedState) {
4848
// Enable ambient support, so the map remains visible in simplified, low-color display
4949
// when the user is no longer actively using the app but the app is still visible on the
5050
// watch face.
51-
AmbientModeSupport.AmbientController controller = AmbientModeSupport.attach(this);
52-
Log.d(AmbientActivity.class.getSimpleName(), "Is ambient enabled: " + controller.isAmbient());
51+
try {
52+
AmbientModeSupport.AmbientController controller = AmbientModeSupport.attach(this);
53+
Log.d(AmbientActivity.class.getSimpleName(), "Is ambient enabled: " + controller.isAmbient());
54+
} catch (Exception e) {
55+
Log.w(AmbientActivity.class.getSimpleName(), "Ambient mode unavailable on this device: " + e.getMessage());
56+
}
5357

5458
// Obtain the MapFragment and set the async listener to be notified when the map is ready.
5559
mapFragment = (SupportMapFragment) getSupportFragmentManager()

WearOS/Wearable/src/main/java/com/example/wearosmap/MainActivity.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,13 @@ public void onCreate(Bundle savedState) {
5454
// when the user is no longer actively using the app but the app is still visible on the
5555
// watch face.
5656
// [START maps_wear_os_ambient_mode_support]
57-
AmbientModeSupport.AmbientController controller = AmbientModeSupport.attach(this);
57+
try {
58+
AmbientModeSupport.AmbientController controller = AmbientModeSupport.attach(this);
59+
Log.d(MainActivity.class.getSimpleName(), "Is ambient enabled: " + controller.isAmbient());
60+
} catch (Exception e) {
61+
Log.w(MainActivity.class.getSimpleName(), "Ambient mode unavailable on this device: " + e.getMessage());
62+
}
5863
// [END maps_wear_os_ambient_mode_support]
59-
Log.d(MainActivity.class.getSimpleName(), "Is ambient enabled: " + controller.isAmbient());
6064

6165
// Retrieve the containers for the root of the layout and the map. Margins will need to be
6266
// set on them to account for the system window insets.

WearOS/Wearable/src/main/kotlin/com/example/wearosmap/kt/AmbientActivity.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,12 @@ class AmbientActivity : AppCompatActivity(), AmbientModeSupport.AmbientCallbackP
3939
// Enable ambient support, so the map remains visible in simplified, low-color display
4040
// when the user is no longer actively using the app but the app is still visible on the
4141
// watch face.
42-
val controller = AmbientModeSupport.attach(this)
43-
Log.d(AmbientActivity::class.java.simpleName, "Is ambient enabled: " + controller.isAmbient)
42+
try {
43+
val controller = AmbientModeSupport.attach(this)
44+
Log.d(AmbientActivity::class.java.simpleName, "Is ambient enabled: " + controller.isAmbient)
45+
} catch (e: Exception) {
46+
Log.w(AmbientActivity::class.java.simpleName, "Ambient mode unavailable on this device: ${e.message}")
47+
}
4448

4549
// Obtain the MapFragment and set the async listener to be notified when the map is ready.
4650
mapFragment = supportFragmentManager

WearOS/Wearable/src/main/kotlin/com/example/wearosmap/kt/MainActivity.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,13 @@ class MainActivity : AppCompatActivity(), OnMapReadyCallback,
4949
// when the user is no longer actively using the app but the app is still visible on the
5050
// watch face.
5151
// [START maps_wear_os_ambient_mode_support]
52-
val controller = AmbientModeSupport.attach(this)
52+
try {
53+
val controller = AmbientModeSupport.attach(this)
54+
Log.d(MainActivity::class.java.simpleName, "Is ambient enabled: " + controller.isAmbient)
55+
} catch (e: Exception) {
56+
Log.w(MainActivity::class.java.simpleName, "Ambient mode unavailable on this device: ${e.message}")
57+
}
5358
// [END maps_wear_os_ambient_mode_support]
54-
Log.d(MainActivity::class.java.simpleName, "Is ambient enabled: " + controller.isAmbient)
5559

5660
// Retrieve the containers for the root of the layout and the map. Margins will need to be
5761
// set on them to account for the system window insets.

0 commit comments

Comments
 (0)