Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ protected void onCreate(Bundle savedInstanceState) {
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
// 根据用户设置创建地图提供商
String mapProviderType = this.userSettings.getMapProvider();
this.mapProvider = MapProviderFactory.create(mapProviderType);
this.mapProvider = MapProviderFactory.create(MapProviderFactory.effectiveProvider(this, mapProviderType));

// 初始化地图
this.mapProvider.initialize(this, R.id.history_map, this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,9 @@ protected void onCreate(Bundle savedInstanceState) {

// 根据用户设置创建地图提供商
String mapProviderType = this.userSettings.getMapProvider();
// A build without a Google Maps key (secrets.properties) still has a map: OpenStreetMap
// needs no key, so it stands in for Google rather than the app refusing to start.
mapProviderType = MapProviderFactory.effectiveProvider(this, mapProviderType);

// **Before the new provider touches it.** Changing provider calls recreate(), and the
// FragmentManager restores Google's map fragment into this container while a plain view
Expand Down Expand Up @@ -1751,17 +1754,19 @@ public void onClickMoreForDevice(View view) {
deviceInfoActivityLauncher.launch(deviceInfoIntent);
}

private void checkApiKey() {
/** Whether this build carries a Google Maps key; without one the map falls back to OSM. */
private boolean checkApiKey() {
try {
ApplicationInfo appInfo = getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);
Bundle bundle = Objects.requireNonNull(appInfo.metaData);

String apiKey = bundle.getString("com.google.android.geo.API_KEY");

if (apiKey == null || apiKey.isBlank() || apiKey.equals("DEFAULT_API_KEY")) {
Toast.makeText(this, "API Key was not set in secrets.properties", LENGTH_SHORT).show();
throw new RuntimeException("API Key was not set in secrets.properties");
Log.w(TAG, "No Google Maps API key in secrets.properties; using OpenStreetMap");
return false;
}
return true;
} catch (PackageManager.NameNotFoundException e) {
Log.e(TAG, "Package name not found.", e);
throw new RuntimeException("Error getting package info", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,28 @@ public static void reset() {
replacement = null;
}

/**
* The provider to actually use: Google Maps needs the key from secrets.properties, and a
* build without one gets OpenStreetMap (which needs none) instead of a map that cannot load.
*/
public static String effectiveProvider(final android.content.Context context, final String providerType) {
if (providerType != null && !PROVIDER_GOOGLE.equals(providerType)) {
return providerType;
}
try {
final android.os.Bundle meta = context.getPackageManager().getApplicationInfo(
context.getPackageName(), android.content.pm.PackageManager.GET_META_DATA).metaData;
final String key = meta == null ? null : meta.getString("com.google.android.geo.API_KEY");
if (key == null || key.isBlank() || key.equals("DEFAULT_API_KEY")) {
return PROVIDER_OSM;
}
} catch (final Exception e) {
Log.w(TAG, "Could not read the Maps API key; using OpenStreetMap", e);
return PROVIDER_OSM;
}
return providerType;
}

public static IMapProvider create(String providerType) {
if (replacement != null) {
Log.d(TAG, "Creating a substituted map provider");
Expand Down