diff --git a/android/build.gradle b/android/build.gradle index 09464e3303..e06be96d42 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -14,8 +14,8 @@ buildscript { dependencies { classpath 'com.android.tools.build:gradle:4.1.2' - classpath 'com.google.gms:google-services:4.3.5' if (googleServiceEnabled) { + classpath 'com.google.gms:google-services:4.3.5' classpath 'com.google.firebase:firebase-crashlytics-gradle:2.5.1' classpath 'com.google.firebase:firebase-appdistribution-gradle:2.1.0' } @@ -57,14 +57,10 @@ dependencies { implementation 'androidx.multidex:multidex:' + propMultiDexVersion - implementation 'com.google.android.gms:play-services-location:17.0.0' - implementation 'com.google.android.gms:play-services-analytics:17.0.0' - implementation 'com.google.android.gms:play-services-gcm:17.0.0' + // Google Services implementation 'com.google.android.gms:play-services-auth:17.0.0' - implementation 'com.google.android.gms:play-services-basement:17.1.1' - - // crash reporting if (googleServiceEnabled) { + implementation 'com.google.android.gms:play-services-location:17.0.0' implementation 'com.google.firebase:firebase-analytics:17.4.4' implementation 'com.google.firebase:firebase-crashlytics:17.1.1' implementation 'com.google.firebase:firebase-crashlytics-ndk:17.1.1' diff --git a/android/flavors/google-services-disabled/com/mapswithme/maps/location/LocationProviderFactory.java b/android/flavors/google-services-disabled/com/mapswithme/maps/location/LocationProviderFactory.java new file mode 100644 index 0000000000..4bdc46f9f0 --- /dev/null +++ b/android/flavors/google-services-disabled/com/mapswithme/maps/location/LocationProviderFactory.java @@ -0,0 +1,18 @@ +package com.mapswithme.maps.location; + +import android.content.Context; + +import androidx.annotation.NonNull; + +public class LocationProviderFactory +{ + public static boolean isGoogleLocationAvailable(@NonNull @SuppressWarnings("unused") Context context) + { + return false; + } + + public static BaseLocationProvider getProvider(@NonNull Context context) + { + return new AndroidNativeProvider(new DefaultLocationFixChecker(), context); + } +} diff --git a/android/src/com/mapswithme/maps/location/GoogleFusedLocationProvider.java b/android/flavors/google-services-enabled/com/mapswithme/maps/location/GoogleFusedLocationProvider.java similarity index 88% rename from android/src/com/mapswithme/maps/location/GoogleFusedLocationProvider.java rename to android/flavors/google-services-enabled/com/mapswithme/maps/location/GoogleFusedLocationProvider.java index 1c48ebc464..e66f9ccf46 100644 --- a/android/src/com/mapswithme/maps/location/GoogleFusedLocationProvider.java +++ b/android/flavors/google-services-enabled/com/mapswithme/maps/location/GoogleFusedLocationProvider.java @@ -2,9 +2,11 @@ package com.mapswithme.maps.location; import android.content.Context; import android.location.Location; +import android.location.LocationListener; import android.os.Bundle; import androidx.annotation.NonNull; + import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.api.GoogleApiClient; import com.google.android.gms.common.api.PendingResult; @@ -17,26 +19,32 @@ import com.google.android.gms.location.LocationSettingsResult; import com.google.android.gms.location.LocationSettingsStatusCodes; class GoogleFusedLocationProvider extends BaseLocationProvider - implements GoogleApiClient.ConnectionCallbacks, - GoogleApiClient.OnConnectionFailedListener + implements GoogleApiClient.ConnectionCallbacks, + GoogleApiClient.OnConnectionFailedListener { private final static String TAG = GoogleFusedLocationProvider.class.getSimpleName(); private final GoogleApiClient mGoogleApiClient; private LocationRequest mLocationRequest; private PendingResult mLocationSettingsResult; + + private class GoogleLocationListener extends BaseLocationListener implements com.google.android.gms.location.LocationListener + { + private GoogleLocationListener(@NonNull LocationFixChecker locationFixChecker) {super(locationFixChecker);} + } + @NonNull - private final BaseLocationListener mListener; + private final GoogleLocationListener mListener; GoogleFusedLocationProvider(@NonNull LocationFixChecker locationFixChecker, @NonNull Context context) { super(locationFixChecker); mGoogleApiClient = new GoogleApiClient.Builder(context) - .addApi(LocationServices.API) - .addConnectionCallbacks(this) - .addOnConnectionFailedListener(this) - .build(); - mListener = new BaseLocationListener(locationFixChecker); + .addApi(LocationServices.API) + .addConnectionCallbacks(this) + .addOnConnectionFailedListener(this) + .build(); + mListener = new GoogleLocationListener(locationFixChecker); } @Override @@ -54,7 +62,7 @@ class GoogleFusedLocationProvider extends BaseLocationProvider long interval = LocationHelper.INSTANCE.getInterval(); mLocationRequest.setInterval(interval); LOGGER.d(TAG, "Request Google fused provider to provide locations at this interval = " - + interval + " ms"); + + interval + " ms"); mLocationRequest.setFastestInterval(interval / 2); mGoogleApiClient.connect(); diff --git a/android/flavors/google-services-enabled/com/mapswithme/maps/location/LocationProviderFactory.java b/android/flavors/google-services-enabled/com/mapswithme/maps/location/LocationProviderFactory.java new file mode 100644 index 0000000000..fe93add298 --- /dev/null +++ b/android/flavors/google-services-enabled/com/mapswithme/maps/location/LocationProviderFactory.java @@ -0,0 +1,37 @@ +package com.mapswithme.maps.location; + +import android.content.Context; + +import com.google.android.gms.common.ConnectionResult; +import com.google.android.gms.common.GoogleApiAvailability; +import com.mapswithme.util.Config; +import com.mapswithme.util.log.Logger; +import com.mapswithme.util.log.LoggerFactory; + +import androidx.annotation.NonNull; + +public class LocationProviderFactory +{ + private final static String TAG = LocationProviderFactory.class.getSimpleName(); + private final static Logger mLogger = LoggerFactory.INSTANCE.getLogger(LoggerFactory.Type.LOCATION); + + public static boolean isGoogleLocationAvailable(@NonNull Context context) + { + return GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context) == ConnectionResult.SUCCESS; + } + + public static BaseLocationProvider getProvider(@NonNull Context context) + { + mLogger.d(TAG, "getProvider", new Throwable()); + if (isGoogleLocationAvailable(context) && Config.useGoogleServices()) + { + mLogger.d(TAG, "Use fused provider."); + return new GoogleFusedLocationProvider(new FusedLocationFixChecker(), context); + } + else + { + mLogger.d(TAG, "Use native provider"); + return new AndroidNativeProvider(new DefaultLocationFixChecker(), context); + } + } +} diff --git a/android/src/com/mapswithme/maps/location/BaseLocationListener.java b/android/src/com/mapswithme/maps/location/BaseLocationListener.java index e6c109f06e..f014ef447b 100644 --- a/android/src/com/mapswithme/maps/location/BaseLocationListener.java +++ b/android/src/com/mapswithme/maps/location/BaseLocationListener.java @@ -8,7 +8,7 @@ import androidx.annotation.NonNull; import com.mapswithme.util.log.Logger; import com.mapswithme.util.log.LoggerFactory; -class BaseLocationListener implements LocationListener, com.google.android.gms.location.LocationListener +class BaseLocationListener implements LocationListener { private static final String TAG = BaseLocationListener.class.getSimpleName(); private static final Logger LOGGER = LoggerFactory.INSTANCE.getLogger(LoggerFactory.Type.LOCATION); diff --git a/android/src/com/mapswithme/maps/location/LocationHelper.java b/android/src/com/mapswithme/maps/location/LocationHelper.java index c10d321752..8f2bf38cab 100644 --- a/android/src/com/mapswithme/maps/location/LocationHelper.java +++ b/android/src/com/mapswithme/maps/location/LocationHelper.java @@ -7,8 +7,6 @@ import android.location.Location; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.UiThread; -import com.google.android.gms.common.ConnectionResult; -import com.google.android.gms.common.GoogleApiAvailability; import com.mapswithme.maps.Framework; import com.mapswithme.maps.MwmApplication; import com.mapswithme.maps.base.Initializable; @@ -173,18 +171,7 @@ public enum LocationHelper implements Initializable private void initProvider() { - mLogger.d(TAG, "initProvider", new Throwable()); - final boolean containsGoogleServices = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(mContext) == ConnectionResult.SUCCESS; - final boolean googleServicesTurnedInSettings = Config.useGoogleServices(); - if (containsGoogleServices && googleServicesTurnedInSettings) - { - mLogger.d(TAG, "Use fused provider."); - mLocationProvider = new GoogleFusedLocationProvider(new FusedLocationFixChecker(), mContext); - } - else - { - initNativeProvider(); - } + mLocationProvider = LocationProviderFactory.getProvider(mContext); } void initNativeProvider() diff --git a/android/src/com/mapswithme/maps/settings/SettingsPrefsFragment.java b/android/src/com/mapswithme/maps/settings/SettingsPrefsFragment.java index 08048bf83c..34c25f091d 100644 --- a/android/src/com/mapswithme/maps/settings/SettingsPrefsFragment.java +++ b/android/src/com/mapswithme/maps/settings/SettingsPrefsFragment.java @@ -24,8 +24,6 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import com.google.android.gms.common.ConnectionResult; -import com.google.android.gms.common.GoogleApiAvailability; import com.mapswithme.maps.Framework; import com.mapswithme.maps.R; import com.mapswithme.maps.bookmarks.data.BookmarkManager; @@ -33,6 +31,7 @@ import com.mapswithme.maps.downloader.MapManager; import com.mapswithme.maps.downloader.OnmapDownloader; import com.mapswithme.maps.editor.ProfileActivity; import com.mapswithme.maps.location.LocationHelper; +import com.mapswithme.maps.location.LocationProviderFactory; import com.mapswithme.maps.location.TrackRecorder; import com.mapswithme.maps.purchase.AdsRemovalActivationCallback; import com.mapswithme.maps.purchase.AdsRemovalPurchaseControllerProvider; @@ -599,8 +598,7 @@ public class SettingsPrefsFragment extends BaseXmlSettingsFragment if (pref == null) return; - if (GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(getActivity().getApplicationContext()) - != ConnectionResult.SUCCESS) + if (!LocationProviderFactory.isGoogleLocationAvailable(getActivity().getApplicationContext())) { removePreference(getString(R.string.pref_subtittle_opt_out), pref); }