[android] Refactor LocationHelper

No semantic changes intended.

Signed-off-by: Roman Tsisyk <roman@tsisyk.com>
This commit is contained in:
Roman Tsisyk 2022-10-05 09:14:18 +03:00 committed by Alexander Borsuk
parent 6b36cd2f6c
commit b1e852bd48
5 changed files with 35 additions and 27 deletions

View file

@ -6,6 +6,7 @@ import android.os.Looper;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationAvailability;
import com.google.android.gms.location.LocationCallback;
@ -16,8 +17,6 @@ import com.google.android.gms.location.LocationSettingsRequest;
import com.google.android.gms.location.SettingsClient;
import com.mapswithme.util.log.Logger;
import static com.mapswithme.maps.location.LocationHelper.ERROR_NOT_SUPPORTED;
class GoogleFusedLocationProvider extends BaseLocationProvider
{
private static final String TAG = GoogleFusedLocationProvider.class.getSimpleName();
@ -44,7 +43,6 @@ class GoogleFusedLocationProvider extends BaseLocationProvider
{
if (!availability.isLocationAvailable()) {
Logger.w(TAG, "isLocationAvailable returned false");
//mListener.onLocationError(ERROR_GPS_OFF);
}
}
}
@ -90,7 +88,7 @@ class GoogleFusedLocationProvider extends BaseLocationProvider
mFusedLocationClient.requestLocationUpdates(locationRequest, mCallback, Looper.myLooper());
}).addOnFailureListener(e -> {
Logger.e(TAG, "Service is not available: " + e);
mListener.onLocationError(ERROR_NOT_SUPPORTED);
mListener.onLocationDisabled();
});
// onLocationResult() may not always be called regularly, however the device location is known.

View file

@ -1672,14 +1672,14 @@ public class MwmActivity extends BaseMwmFragmentActivity
}
@Override
public void onLocationError(int errorCode)
public void onLocationDenied()
{
if (errorCode == LocationHelper.ERROR_DENIED)
{
PermissionsUtils.requestLocationPermission(MwmActivity.this, REQ_CODE_LOCATION_PERMISSION);
return;
}
PermissionsUtils.requestLocationPermission(this, REQ_CODE_LOCATION_PERMISSION);
}
@Override
public void onLocationDisabled()
{
if (mLocationErrorDialogAnnoying || (mLocationErrorDialog != null && mLocationErrorDialog.isShowing()))
return;

View file

@ -29,7 +29,7 @@ class AndroidNativeProvider extends BaseLocationProvider
Logger.d(TAG, "Disabled location provider: " + provider);
mProviderCount--;
if (mProviderCount < MIN_PROVIDER_COUNT)
mListener.onLocationError(LocationHelper.ERROR_GPS_OFF);
mListener.onLocationDisabled();
}
@Override
@ -78,7 +78,7 @@ class AndroidNativeProvider extends BaseLocationProvider
mProviderCount = providers.size();
if (mProviderCount < MIN_PROVIDER_COUNT)
{
mListener.onLocationError(LocationHelper.ERROR_GPS_OFF);
mListener.onLocationDisabled();
}
for (String provider : providers)

View file

@ -1,15 +1,18 @@
package com.mapswithme.maps.location;
import android.app.PendingIntent;
import android.location.Location;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
abstract class BaseLocationProvider
{
interface Listener
{
void onLocationChanged(@NonNull Location location);
void onLocationError(int errorCode);
void onLocationDenied();
void onLocationDisabled();
}
@NonNull

View file

@ -31,10 +31,10 @@ public enum LocationHelper implements Initializable<Context>, AppBackgroundTrack
// These constants should correspond to values defined in platform/location.hpp
// Leave 0-value as no any error.
public static final int ERROR_NOT_SUPPORTED = 1;
public static final int ERROR_DENIED = 2;
public static final int ERROR_GPS_OFF = 3;
public static final int ERROR_UNKNOWN = 0;
//private static final int ERROR_UNKNOWN = 0;
//private static final int ERROR_NOT_SUPPORTED = 1;
private static final int ERROR_DENIED = 2;
private static final int ERROR_GPS_OFF = 3;
private static final long INTERVAL_FOLLOW_AND_ROTATE_MS = 3000;
private static final long INTERVAL_FOLLOW_MS = 1000;
@ -286,12 +286,18 @@ public enum LocationHelper implements Initializable<Context>, AppBackgroundTrack
}
@Override
public void onLocationError(int errCode)
public void onLocationDenied()
{
Logger.d(TAG, "onLocationError errorCode = " + errCode +
", current state = " + LocationState.nameOf(getMyPositionMode()));
if (errCode == ERROR_NOT_SUPPORTED &&
LocationUtils.areLocationServicesTurnedOn(mContext) &&
mSavedLocation = null;
nativeOnLocationError(ERROR_DENIED);
if (mUiCallback != null)
mUiCallback.onLocationDenied();
}
@Override
public void onLocationDisabled()
{
if (LocationUtils.areLocationServicesTurnedOn(mContext) &&
!(mLocationProvider instanceof AndroidNativeProvider))
{
// If location service is enabled, try to downgrade to the native provider first
@ -303,9 +309,9 @@ public enum LocationHelper implements Initializable<Context>, AppBackgroundTrack
}
mSavedLocation = null;
nativeOnLocationError(errCode);
nativeOnLocationError(ERROR_GPS_OFF);
if (mUiCallback != null)
mUiCallback.onLocationError(errCode);
mUiCallback.onLocationDisabled();
}
private void notifyMyPositionModeChanged(int newMode)
@ -437,7 +443,7 @@ public enum LocationHelper implements Initializable<Context>, AppBackgroundTrack
{
Logger.d(TAG, "Location updates are stopped by the user manually, so skip provider start"
+ " until the user starts it manually.");
onLocationError(ERROR_GPS_OFF);
onLocationDisabled();
return;
}
@ -447,7 +453,7 @@ public enum LocationHelper implements Initializable<Context>, AppBackgroundTrack
if (!PermissionsUtils.isLocationGranted(mContext))
{
Logger.w(TAG, "Dynamic permissions ACCESS_COARSE_LOCATION and/or ACCESS_FINE_LOCATION are granted");
onLocationError(ERROR_DENIED);
onLocationDenied();
return;
}
Logger.i(TAG, "start(): interval = " + mInterval + " provider = '" + mLocationProvider + "' mInFirstRun = " + mInFirstRun);
@ -606,7 +612,8 @@ public enum LocationHelper implements Initializable<Context>, AppBackgroundTrack
void onMyPositionModeChanged(int newMode);
void onLocationUpdated(@NonNull Location location);
void onCompassUpdated(@NonNull CompassData compass);
void onLocationError(int errorCode);
void onLocationDenied();
void onLocationDisabled();
void onLocationNotFound();
void onRoutingFinish();
}