[android] Add support for COARSE-only location

Signed-off-by: Roman Tsisyk <roman@tsisyk.com>
This commit is contained in:
Roman Tsisyk 2022-02-12 08:55:16 +03:00
parent eb5bf01fca
commit 2acaaa5223
7 changed files with 32 additions and 107 deletions

View file

@ -109,7 +109,6 @@ import com.mapswithme.util.UiUtils;
import com.mapswithme.util.Utils;
import com.mapswithme.util.log.Logger;
import com.mapswithme.util.log.LoggerFactory;
import com.mapswithme.util.permissions.PermissionsResult;
import java.util.Objects;
import java.util.Stack;
@ -154,6 +153,7 @@ public class MwmActivity extends BaseMwmFragmentActivity
private static final String EXTRA_LOCATION_DIALOG_IS_ANNOYING = "LOCATION_DIALOG_IS_ANNOYING";
private static final int REQ_CODE_LOCATION_PERMISSION = 1;
private static final int REQ_CODE_LOCATION_PERMISSION_ON_CLICK = 2;
public static final int REQ_CODE_ERROR_DRIVING_OPTIONS_DIALOG = 5;
public static final int REQ_CODE_DRIVING_OPTIONS = 6;
private static final int REQ_CODE_ISOLINES_ERROR = 8;
@ -781,11 +781,14 @@ public class MwmActivity extends BaseMwmFragmentActivity
closeSearchToolbar(clearSearchText, stopSearch);
}
public void startLocationToPoint(final @Nullable MapObject endPoint,
final boolean canUseMyPositionAsStart)
public void startLocationToPoint(final @Nullable MapObject endPoint)
{
closeMenu(() -> {
RoutingController.get().prepare(canUseMyPositionAsStart, endPoint);
if (!PermissionsUtils.isFineLocationGranted(MwmActivity.this))
PermissionsUtils.requestLocationPermission(MwmActivity.this, REQ_CODE_LOCATION_PERMISSION);
MapObject startPoint = LocationHelper.INSTANCE.getMyPosition();
RoutingController.get().prepare(startPoint, endPoint);
// TODO: check for tablet.
closePlacePage();
@ -923,19 +926,18 @@ public class MwmActivity extends BaseMwmFragmentActivity
@NonNull int[] grantResults)
{
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode != REQ_CODE_LOCATION_PERMISSION || grantResults.length == 0)
if (requestCode != REQ_CODE_LOCATION_PERMISSION && requestCode != REQ_CODE_LOCATION_PERMISSION_ON_CLICK)
return;
PermissionsResult result = PermissionsUtils.computePermissionsResult(permissions, grantResults);
if (result.isLocationGranted())
{
myPositionClick();
}
else
if (!PermissionsUtils.isLocationGranted(this))
{
Utils.showSnackbar(getActivity(), findViewById(R.id.coordinator), findViewById(R.id.menu_frame),
R.string.location_is_disabled_long_text);
return;
}
if (requestCode == REQ_CODE_LOCATION_PERMISSION_ON_CLICK)
myPositionClick();
}
@Override
@ -2117,9 +2119,9 @@ public class MwmActivity extends BaseMwmFragmentActivity
@Override
public void onClick(View v)
{
if (!PermissionsUtils.isLocationGranted(getApplicationContext()))
if (!PermissionsUtils.isFineLocationGranted(getApplicationContext()))
{
PermissionsUtils.requestLocationPermission(MwmActivity.this, REQ_CODE_LOCATION_PERMISSION);
PermissionsUtils.requestLocationPermission(MwmActivity.this, REQ_CODE_LOCATION_PERMISSION_ON_CLICK);
return;
}

View file

@ -2,6 +2,7 @@ package com.mapswithme.maps.intent;
import android.content.ContentResolver;
import android.content.Intent;
import android.location.Location;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
@ -26,6 +27,7 @@ import com.mapswithme.maps.api.RoutePoint;
import com.mapswithme.maps.bookmarks.data.BookmarkManager;
import com.mapswithme.maps.bookmarks.data.FeatureId;
import com.mapswithme.maps.bookmarks.data.MapObject;
import com.mapswithme.maps.location.LocationHelper;
import com.mapswithme.maps.routing.RoutingController;
import com.mapswithme.maps.search.SearchActivity;
import com.mapswithme.maps.search.SearchEngine;
@ -804,13 +806,15 @@ public class Factory
}
else if (routerType > 0)
{
RoutingController.get().prepare(true /* canUseMyPositionAsStart */,
MapObject startPoint = LocationHelper.INSTANCE.getMyPosition();
RoutingController.get().prepare(startPoint,
fromLatLon(mLatTo, mLonTo, mDaddr), routerType,
true /* fromApi */);
}
else
{
RoutingController.get().prepare(true /* canUseMyPositionAsStart */,
MapObject startPoint = LocationHelper.INSTANCE.getMyPosition();
RoutingController.get().prepare(startPoint,
fromLatLon(mLatTo, mLonTo, mDaddr), true /* fromApi */);
}
return true;

View file

@ -473,7 +473,7 @@ public enum LocationHelper implements Initializable<Context>, AppBackgroundTrack
calcLocationUpdatesInterval();
if (!PermissionsUtils.isLocationGranted(mContext))
{
mLogger.w(TAG, "Dynamic permission ACCESS_COARSE_LOCATION/ACCESS_FINE_LOCATION is not granted");
mLogger.w(TAG, "Dynamic permissions ACCESS_COARSE_LOCATION and/or ACCESS_FINE_LOCATION are granted");
onLocationError(ERROR_DENIED);
return;
}

View file

@ -389,24 +389,6 @@ public class RoutingController implements Initializable<Void>
prepare(getStartPoint(), getEndPoint(), false);
}
public void prepare(boolean canUseMyPositionAsStart, @Nullable MapObject endPoint)
{
prepare(canUseMyPositionAsStart, endPoint, false);
}
public void prepare(boolean canUseMyPositionAsStart, @Nullable MapObject endPoint, boolean fromApi)
{
MapObject startPoint = canUseMyPositionAsStart ? LocationHelper.INSTANCE.getMyPosition() : null;
prepare(startPoint, endPoint, fromApi);
}
public void prepare(boolean canUseMyPositionAsStart, @Nullable MapObject endPoint,
@Framework.RouterType int type, boolean fromApi)
{
MapObject startPoint = canUseMyPositionAsStart ? LocationHelper.INSTANCE.getMyPosition() : null;
prepare(startPoint, endPoint, type, fromApi);
}
public void prepare(@Nullable MapObject startPoint, @Nullable MapObject endPoint)
{
prepare(startPoint, endPoint, false);

View file

@ -3,7 +3,6 @@ package com.mapswithme.maps.widget.placepage;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.location.Location;
import android.text.Html;
import android.text.SpannableStringBuilder;
@ -564,7 +563,7 @@ public class PlacePageView extends NestedScrollViewClickFixed
}
else
{
getActivity().startLocationToPoint(getMapObject(), true);
getActivity().startLocationToPoint(getMapObject());
}
}

View file

@ -6,77 +6,31 @@ import static android.Manifest.permission.ACCESS_FINE_LOCATION;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import com.mapswithme.util.permissions.PermissionsResult;
import java.util.HashMap;
import java.util.Map;
import androidx.core.content.ContextCompat;
public final class PermissionsUtils
{
private static final String[] PERMISSIONS = new String[]
{
ACCESS_COARSE_LOCATION,
ACCESS_FINE_LOCATION
};
private static final String[] LOCATION_PERMISSIONS = new String[]
{
ACCESS_COARSE_LOCATION,
ACCESS_FINE_LOCATION
};
private PermissionsUtils() {}
@NonNull
public static PermissionsResult computePermissionsResult(@NonNull String[] permissions,
@NonNull int[] grantResults)
public static boolean isFineLocationGranted(@NonNull Context context)
{
Map<String, Boolean> result = new HashMap<>();
for (int i = 0; i < permissions.length; i++)
{
result.put(permissions[i], grantResults[i] == PackageManager.PERMISSION_GRANTED);
}
return getPermissionsResult(result);
return ContextCompat.checkSelfPermission(context, ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED;
}
public static boolean isLocationGranted(@NonNull Context context)
{
return checkPermissions(context).isLocationGranted();
}
@NonNull
private static PermissionsResult checkPermissions(@NonNull Context context)
{
Context appContext = context.getApplicationContext();
Map<String, Boolean> result = new HashMap<>();
for (String permission: PERMISSIONS)
{
boolean granted = Build.VERSION.SDK_INT < Build.VERSION_CODES.M
|| appContext.checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED;
result.put(permission, granted);
}
return getPermissionsResult(result);
}
@NonNull
private static PermissionsResult getPermissionsResult(@NonNull Map<String, Boolean> result)
{
boolean locationGranted = (result.containsKey(ACCESS_COARSE_LOCATION)
? result.get(ACCESS_COARSE_LOCATION) : false)
|| (result.containsKey(ACCESS_FINE_LOCATION)
? result.get(ACCESS_FINE_LOCATION) : false);
return new PermissionsResult(locationGranted);
return isFineLocationGranted(context) ||
ContextCompat.checkSelfPermission(context, ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED;
}
public static void requestLocationPermission(@NonNull Activity activity, int code)
{
ActivityCompat.requestPermissions(activity, LOCATION_PERMISSIONS, code);
ActivityCompat.requestPermissions(activity, new String[]{
ACCESS_COARSE_LOCATION,
ACCESS_FINE_LOCATION
}, code);
}
}

View file

@ -1,16 +0,0 @@
package com.mapswithme.util.permissions;
public final class PermissionsResult
{
private final boolean mLocationGranted;
public PermissionsResult(boolean locationGranted)
{
mLocationGranted = locationGranted;
}
public boolean isLocationGranted()
{
return mLocationGranted;
}
}