1+ // Copyright 2025 Google LLC
2+ //
3+ // Licensed under the Apache License, Version 2.0 (the "License");
4+ // you may not use this file except in compliance with the License.
5+ // You may obtain a copy of the License at
6+ //
7+ // http://www.apache.org/licenses/LICENSE-2.0
8+ //
9+ // Unless required by applicable law or agreed to in writing, software
10+ // distributed under the License is distributed on an "AS IS" BASIS,
11+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+ // See the License for the specific language governing permissions and
13+ // limitations under the License.
14+
15+ package com .example .maps3djava ;
16+
17+ import android .app .Application ;
18+ import android .content .pm .ApplicationInfo ;
19+ import android .content .pm .PackageManager ;
20+ import android .os .Bundle ;
21+ import android .util .Log ;
22+ import android .widget .Toast ;
23+
24+ import java .util .Objects ;
25+
26+ /**
27+ * `Maps3DKotlinApplication` is a custom Application class for the API demo.
28+ *
29+ * This class is responsible for application-wide initialization and setup,
30+ * such as checking for the presence and validity of the API key during the
31+ * application's startup.
32+ *
33+ * It extends the [Application] class and overrides the [.onCreate]
34+ * method to perform these initialization tasks.
35+ */
36+ public class Maps3DJavaApplication extends Application {
37+ private final String TAG = this .getClass ().getSimpleName ();
38+
39+ /**
40+ * Called when the application is starting, before any activity, service,
41+ * or receiver objects (excluding content providers) have been created.
42+ * This is where application-wide initialization should be performed.
43+ */
44+ @ Override
45+ public void onCreate () {
46+ super .onCreate ();
47+ checkApiKey ();
48+ }
49+
50+ /**
51+ * Checks if the API key for Google Maps is properly configured in the application's metadata.
52+ *
53+ * This method retrieves the API key from the application's metadata, specifically looking for
54+ * a string value associated with the key "com.google.android.geo.maps3d.API_KEY".
55+ * The key must be present, not blank, and not set to the placeholder value "DEFAULT_API_KEY".
56+ *
57+ * If any of these checks fail, a Toast message is displayed indicating that the API key is missing or
58+ * incorrectly configured, and a RuntimeException is thrown.
59+ */
60+ private void checkApiKey () {
61+ try {
62+ // Get the application's info, including its meta-data.
63+ ApplicationInfo appInfo =
64+ getPackageManager ().getApplicationInfo (getPackageName (), PackageManager .GET_META_DATA );
65+ // Ensure the meta-data bundle is not null.
66+ Bundle bundle = Objects .requireNonNull (appInfo .metaData );
67+
68+ // Retrieve the API key string from the bundle. The key name is crucial.
69+ String apiKey =
70+ bundle .getString ("com.google.android.geo.maps3d.API_KEY" ); // Key name is important!
71+
72+ // Check if the API key is null, blank, or still the default placeholder value.
73+ if (apiKey == null || apiKey .isBlank () || apiKey .equals ("DEFAULT_API_KEY" )) {
74+ // Display a Toast message to the user indicating the API key issue.
75+ Toast .makeText (
76+ this ,
77+ "API Key was not set in secrets.properties" ,
78+ Toast .LENGTH_LONG
79+ ).show ();
80+ // Throw a RuntimeException to halt the application if the API key is misconfigured.
81+ throw new RuntimeException ("API Key was not set in secrets.properties" );
82+ }
83+ } catch (PackageManager .NameNotFoundException e ) {
84+ // Log an error if the package name is not found (should not happen in a running app).
85+ Log .e (TAG , "Package name not found." , e );
86+ // Re-throw as a RuntimeException as this is a critical application setup issue.
87+ throw new RuntimeException ("Error getting package info." , e );
88+ } catch (NullPointerException e ) {
89+ // Log an error if meta-data is completely missing from the application info.
90+ Log .e (TAG , "Error accessing meta-data." , e ); // Handle the case where meta-data is completely missing.
91+ // Re-throw as a RuntimeException.
92+ throw new RuntimeException ("Error accessing meta-data in manifest" , e );
93+ }
94+ }
95+ }
0 commit comments