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,
1717package com .google .maps .android .utils .demo ;
1818
1919import android .annotation .SuppressLint ;
20- import android .app .Activity ;
2120import android .os .Bundle ;
2221import android .widget .TextView ;
2322import 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+
2531import com .google .android .gms .maps .model .LatLng ;
2632import 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+ }
0 commit comments