Skip to content

Commit

Permalink
Move isAvailable implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
seadowg committed Aug 23, 2023
1 parent 0e93ab0 commit 4117899
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import static org.odk.collect.androidshared.ui.PrefUtils.createListPref;
import static org.odk.collect.androidshared.ui.PrefUtils.getInt;

import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;

import androidx.preference.Preference;
Expand Down Expand Up @@ -37,11 +40,29 @@ public GoogleMapConfigurator(String prefKey, int sourceLabelId, GoogleMapTypeOpt
}

@Override public boolean isAvailable(Context context) {
return GoogleMapFragment.isAvailable(context);
try {
ApplicationInfo applicationInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
String apiKey = applicationInfo.metaData.getString("com.google.android.geo.API_KEY");

return isGoogleMapsSdkAvailable(context) && isGooglePlayServicesAvailable(context) && !apiKey.equals("");
} catch (PackageManager.NameNotFoundException e) {
throw new RuntimeException(e);
}
}

private static boolean isGooglePlayServicesAvailable(Context context) {
return new PlayServicesChecker().isGooglePlayServicesAvailable(context);
}

private static boolean isGoogleMapsSdkAvailable(Context context) {
// The Google Maps SDK for Android requires OpenGL ES version 2.
// See https://developers.google.com/maps/documentation/android-sdk/config
return ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE))
.getDeviceConfigurationInfo().reqGlEsVersion >= 0x20000;
}

@Override public void showUnavailableMessage(Context context) {
if (!GoogleMapFragment.isGoogleMapsSdkAvailable(context)) {
if (!isGoogleMapsSdkAvailable(context)) {
ToastUtils.showLongToast(context, context.getString(
org.odk.collect.strings.R.string.basemap_source_unavailable, context.getString(sourceLabelId)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@
import static org.odk.collect.maps.MapConsts.POLYLINE_STROKE_WIDTH;

import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
Expand Down Expand Up @@ -51,7 +48,6 @@
import com.google.android.gms.maps.model.TileOverlayOptions;

import org.odk.collect.androidshared.system.ContextUtils;
import org.odk.collect.androidshared.system.PlayServicesChecker;
import org.odk.collect.androidshared.ui.ToastUtils;
import org.odk.collect.googlemaps.GoogleMapConfigurator.GoogleMapTypeOption;
import org.odk.collect.location.LocationClient;
Expand Down Expand Up @@ -87,28 +83,6 @@ public class GoogleMapFragment extends SupportMapFragment implements
// Bundle keys understood by applyConfig().
static final String KEY_MAP_TYPE = "MAP_TYPE";

public static boolean isAvailable(Context context) {
try {
ApplicationInfo applicationInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
String apiKey = applicationInfo.metaData.getString("com.google.android.geo.API_KEY");

return isGoogleMapsSdkAvailable(context) && isGooglePlayServicesAvailable(context) && !apiKey.equals("");
} catch (PackageManager.NameNotFoundException e) {
throw new RuntimeException(e);
}
}

public static boolean isGoogleMapsSdkAvailable(Context context) {
// The Google Maps SDK for Android requires OpenGL ES version 2.
// See https://developers.google.com/maps/documentation/android-sdk/config
return ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE))
.getDeviceConfigurationInfo().reqGlEsVersion >= 0x20000;
}

private static boolean isGooglePlayServicesAvailable(Context context) {
return new PlayServicesChecker().isGooglePlayServicesAvailable(context);
}

@Inject
ReferenceLayerRepository referenceLayerRepository;

Expand Down

0 comments on commit 4117899

Please sign in to comment.