-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Try to catch invalid map api keys in the demo
feat: Export the demo activities so they can be run directly chore: Update gradle version, Google maps to 19.0.0
- Loading branch information
Showing
11 changed files
with
146 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
demo/src/main/java/com/google/maps/android/utils/demo/ApiKeyValidator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.google.maps.android.utils.demo | ||
|
||
import android.content.Context | ||
import android.content.pm.PackageManager | ||
import java.util.regex.Pattern | ||
|
||
/** | ||
* Checks if the provided context has a valid Google Maps API key in its metadata. | ||
* | ||
* | ||
* This method retrieves the API key from the application's metadata and returns whether or | ||
* not it has a valid format. | ||
* | ||
* @param context The context to check for the API key. | ||
* @return `true` if the context has a valid API key, `false` otherwise. | ||
*/ | ||
fun hasMapsApiKey(context: Context): Boolean { | ||
val mapsApiKey = getMapsApiKey(context) | ||
|
||
return mapsApiKey != null && keyHasValidFormat(mapsApiKey) | ||
} | ||
|
||
/** | ||
* Checks if the provided API key has a valid format. | ||
* | ||
* | ||
* The valid format is defined by the regular expression "^AIza[0-9A-Za-z\\-_]{35}$". | ||
* | ||
* @param apiKey The API key to validate. | ||
* @return `true` if the API key has a valid format, `false` otherwise. | ||
*/ | ||
private fun keyHasValidFormat(apiKey: String): Boolean { | ||
val regex = "^AIza[0-9A-Za-z\\-_]{35}$" | ||
val pattern = Pattern.compile(regex) | ||
val matcher = pattern.matcher(apiKey) | ||
return matcher.matches() | ||
} | ||
|
||
/** | ||
* Retrieves the Google Maps API key from the application metadata. | ||
* | ||
* @param context The context to retrieve the API key from. | ||
* @return The API key if found, `null` otherwise. | ||
*/ | ||
private fun getMapsApiKey(context: Context): String? { | ||
try { | ||
val bundle = context.packageManager | ||
.getApplicationInfo(context.packageName, PackageManager.GET_META_DATA) | ||
.metaData | ||
return bundle.getString("com.google.android.geo.API_KEY") | ||
} catch (e: PackageManager.NameNotFoundException) { | ||
e.printStackTrace() | ||
return null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters