Skip to content

Commit 4ab85c2

Browse files
committed
feat(demo): Enable edge-to-edge for StreetView demo activities
This commit refactors the `StreetViewDemoActivity` in both its Kotlin and Java versions to support edge-to-edge display. The key changes include: - Changing the base class from `Activity` to `AppCompatActivity`. - Using `WindowCompat.setDecorFitsSystemWindows` to allow the app to draw behind the system bars. - Adding an `OnApplyWindowInsetsListener` to the root view to apply padding for the system bars, preventing content from being obscured. - Updating the `street_view_demo.xml` layout with an ID for the root view to facilitate the insets listener. - Updating the copyright year.
1 parent e0dd0a9 commit 4ab85c2

3 files changed

Lines changed: 102 additions & 53 deletions

File tree

demo/src/main/java/com/google/maps/android/utils/demo/StreetViewDemoActivity.kt

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2024 Google LLC
2+
* Copyright 2025 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,35 +17,61 @@
1717
package com.google.maps.android.utils.demo
1818

1919
import android.annotation.SuppressLint
20-
import android.app.Activity
2120
import android.os.Bundle
2221
import android.widget.TextView
2322
import android.widget.Toast
23+
import androidx.appcompat.app.AppCompatActivity
24+
import androidx.core.view.ViewCompat
25+
import androidx.core.view.WindowCompat
26+
import androidx.core.view.WindowInsetsCompat
27+
import androidx.lifecycle.lifecycleScope
2428
import com.google.android.gms.maps.model.LatLng
2529
import com.google.maps.android.StreetViewUtils
26-
import kotlinx.coroutines.DelicateCoroutinesApi
2730
import kotlinx.coroutines.Dispatchers
28-
import kotlinx.coroutines.GlobalScope
2931
import kotlinx.coroutines.launch
3032

31-
class StreetViewDemoActivity : Activity() {
33+
/**
34+
* An activity that demonstrates how to use the [StreetViewUtils] to check for Street View
35+
* availability at different locations.
36+
*
37+
* This activity performs the following actions:
38+
* 1. Sets up the layout to fit system windows.
39+
* 2. Checks if a valid Google Maps API key is present.
40+
* 3. Launches a coroutine to fetch Street View data for two predefined locations.
41+
* 4. Displays the results of the Street View data fetch on the screen.
42+
*/
43+
class StreetViewDemoActivity : AppCompatActivity() {
3244

3345
@SuppressLint("SetTextI18n")
34-
@OptIn(DelicateCoroutinesApi::class)
3546
override fun onCreate(savedInstanceState: Bundle?) {
3647
super.onCreate(savedInstanceState)
48+
// Make the activity content fit behind the system bars.
49+
WindowCompat.setDecorFitsSystemWindows(window, false)
3750
setContentView(R.layout.street_view_demo)
3851

52+
// Apply window insets to the main view to avoid content overlapping with system bars.
53+
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
54+
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
55+
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
56+
insets
57+
}
58+
59+
// Check for a valid Maps API key before proceeding.
3960
if (!hasMapsApiKey(this)) {
4061
Toast.makeText(this, R.string.bad_maps_api_key, Toast.LENGTH_LONG).show()
4162
finish()
63+
return // Return early to prevent further execution
4264
}
4365

44-
GlobalScope.launch(Dispatchers.Main) {
66+
// Launch a coroutine in the Main dispatcher to fetch Street View data and update the UI.
67+
lifecycleScope.launch(Dispatchers.Main) {
68+
// Fetch Street View data for the first location (which is expected to be supported).
4569
val response1 =
4670
StreetViewUtils.fetchStreetViewData(LatLng(48.1425918, 11.5386121), BuildConfig.MAPS_API_KEY)
71+
// Fetch Street View data for the second location (which is expected to be unsupported).
4772
val response2 = StreetViewUtils.fetchStreetViewData(LatLng(8.1425918, 11.5386121), BuildConfig.MAPS_API_KEY)
4873

74+
// Update the UI with the results.
4975
findViewById<TextView>(R.id.textViewFirstLocation).text = "Location 1 is supported in StreetView: $response1"
5076
findViewById<TextView>(R.id.textViewSecondLocation).text = "Location 2 is supported in StreetView: $response2"
5177
}

demo/src/main/java/com/google/maps/android/utils/demo/StreetViewDemoActivityJava.java

Lines changed: 68 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -17,75 +17,97 @@
1717
package com.google.maps.android.utils.demo;
1818

1919
import android.annotation.SuppressLint;
20-
import android.app.Activity;
2120
import android.os.Bundle;
2221
import android.widget.TextView;
2322
import android.widget.Toast;
2423

24+
import androidx.annotation.NonNull;
25+
import androidx.appcompat.app.AppCompatActivity;
26+
import androidx.core.graphics.Insets;
27+
import androidx.core.view.ViewCompat;
28+
import androidx.core.view.WindowCompat;
29+
import androidx.core.view.WindowInsetsCompat;
30+
2531
import com.google.android.gms.maps.model.LatLng;
2632
import com.google.maps.android.Status;
27-
import com.google.maps.android.utils.demo.BuildConfig;
28-
import com.google.maps.android.utils.demo.R;
2933

3034
/**
31-
* This activity demonstrates how to use the Street View utility in Java.
35+
* An activity that demonstrates how to use the Street View utility in Java to check for Street View
36+
* availability at different locations.
37+
*
38+
* This activity performs the following actions:
39+
* 1. Sets up the layout to fit system windows.
40+
* 2. Checks if a valid Google Maps API key is present.
41+
* 3. Fetches Street View data for two predefined locations using asynchronous callbacks.
42+
* 4. Displays the results of the Street View data fetch on the screen.
3243
*/
33-
public class StreetViewDemoActivityJava extends Activity {
44+
public class StreetViewDemoActivityJava extends AppCompatActivity {
3445

46+
/**
47+
* Called when the activity is first created.
48+
*
49+
* @param savedInstanceState If the activity is being re-initialized after
50+
* previously being shut down then this Bundle contains the data it most
51+
* recently supplied in {@link #onSaveInstanceState}. Otherwise it is null.
52+
*/
3553
@SuppressLint("SetTextI18n")
3654
@Override
3755
protected void onCreate(Bundle savedInstanceState) {
3856
super.onCreate(savedInstanceState);
57+
// Make the activity content fit behind the system bars.
58+
WindowCompat.setDecorFitsSystemWindows(getWindow(), false);
3959
setContentView(R.layout.street_view_demo);
4060

61+
// Apply window insets to the main view to avoid content overlapping with system bars.
62+
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insetsCompat) -> {
63+
Insets insets = insetsCompat.getInsets(WindowInsetsCompat.Type.systemBars());
64+
v.setPadding(insets.left, insets.top, insets.right, insets.bottom);
65+
return insetsCompat;
66+
});
67+
68+
// Check for a valid Maps API key before proceeding.
4169
if (!ApiKeyValidator.hasMapsApiKey(this)) {
4270
Toast.makeText(this, R.string.bad_maps_api_key, Toast.LENGTH_LONG).show();
4371
finish();
44-
return;
72+
return; // Return early to prevent further execution
4573
}
4674

47-
// Fetches Street View data for the first location.
75+
// Fetches Street View data for the first location (expected to be supported).
4876
StreetViewHelper.fetchStreetViewData(
49-
new LatLng(48.1425918, 11.5386121),
50-
BuildConfig.MAPS_API_KEY,
51-
new StreetViewHelper.StreetViewCallback() {
52-
@Override
53-
public void onStreetViewResult(Status status) {
54-
// Updates the UI with the result.
55-
runOnUiThread(() -> {
56-
((TextView) findViewById(R.id.textViewFirstLocation)).setText("Location 1 is supported in StreetView: " + status);
57-
});
58-
}
77+
new LatLng(48.1425918, 11.5386121),
78+
BuildConfig.MAPS_API_KEY, new StreetViewHelper.StreetViewCallback() {
79+
@Override
80+
public void onStreetViewResult(@NonNull Status status) {
81+
// Updates the UI with the result on the UI thread.
82+
runOnUiThread(() -> {
83+
((TextView) findViewById(R.id.textViewFirstLocation)).setText("Location 1 is supported in StreetView: " + status);
84+
});
85+
}
5986

60-
@Override
61-
public void onStreetViewError(Exception e) {
62-
// Handles the error.
63-
e.printStackTrace();
64-
Toast.makeText(StreetViewDemoActivityJava.this, "Error fetching Street View data: " + e.getMessage(), Toast.LENGTH_SHORT).show();
65-
}
66-
}
67-
);
87+
@Override
88+
public void onStreetViewError(@NonNull Exception e) {
89+
// Handles the error by printing stack trace and showing a toast.
90+
e.printStackTrace();
91+
Toast.makeText(StreetViewDemoActivityJava.this, "Error fetching Street View data: " + e.getMessage(), Toast.LENGTH_SHORT).show();
92+
}
93+
});
6894

69-
// Fetches Street View data for the second location.
70-
StreetViewHelper.fetchStreetViewData(
71-
new LatLng(8.1425918, 11.5386121),
72-
BuildConfig.MAPS_API_KEY,
73-
new StreetViewHelper.StreetViewCallback() {
74-
@Override
75-
public void onStreetViewResult(Status status) {
76-
// Updates the UI with the result.
77-
runOnUiThread(() -> {
78-
((TextView) findViewById(R.id.textViewSecondLocation)).setText("Location 2 is supported in StreetView: " + status);
79-
});
80-
}
95+
// Fetches Street View data for the second location (expected to be unsupported).
96+
StreetViewHelper.fetchStreetViewData(new LatLng(8.1425918, 11.5386121), BuildConfig.MAPS_API_KEY, new StreetViewHelper.StreetViewCallback() {
97+
@Override
98+
public void onStreetViewResult(@NonNull Status status) {
99+
// Updates the UI with the result on the UI thread.
100+
runOnUiThread(() -> {
101+
((TextView) findViewById(R.id.textViewSecondLocation)).setText("Location 2 is supported in StreetView: " + status);
102+
});
103+
}
81104

82-
@Override
83-
public void onStreetViewError(Exception e) {
84-
// Handles the error.
85-
e.printStackTrace();
86-
Toast.makeText(StreetViewDemoActivityJava.this, "Error fetching Street View data: " + e.getMessage(), Toast.LENGTH_SHORT).show();
87-
}
105+
@Override
106+
public void onStreetViewError(@NonNull Exception e) {
107+
// Handles the error by printing stack trace and showing a toast.
108+
e.printStackTrace();
109+
Toast.makeText(StreetViewDemoActivityJava.this, "Error fetching Street View data: " + e.getMessage(), Toast.LENGTH_SHORT).show();
88110
}
89-
);
111+
});
90112
}
91-
}
113+
}

demo/src/main/res/layout/street_view_demo.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
-->
1717

1818
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
19+
android:id="@+id/main"
1920
android:orientation="vertical"
2021
android:layout_width="match_parent"
2122
android:layout_height="match_parent">

0 commit comments

Comments
 (0)