diff --git a/android/src/com/mapswithme/maps/MwmActivity.java b/android/src/com/mapswithme/maps/MwmActivity.java index 1f0d9e0184..45b01199a1 100644 --- a/android/src/com/mapswithme/maps/MwmActivity.java +++ b/android/src/com/mapswithme/maps/MwmActivity.java @@ -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; } diff --git a/android/src/com/mapswithme/maps/intent/Factory.java b/android/src/com/mapswithme/maps/intent/Factory.java index 0f52eb57c5..473534c2b7 100644 --- a/android/src/com/mapswithme/maps/intent/Factory.java +++ b/android/src/com/mapswithme/maps/intent/Factory.java @@ -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; diff --git a/android/src/com/mapswithme/maps/location/LocationHelper.java b/android/src/com/mapswithme/maps/location/LocationHelper.java index b93136ead8..f8882f2f8d 100644 --- a/android/src/com/mapswithme/maps/location/LocationHelper.java +++ b/android/src/com/mapswithme/maps/location/LocationHelper.java @@ -473,7 +473,7 @@ public enum LocationHelper implements Initializable, 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; } diff --git a/android/src/com/mapswithme/maps/routing/RoutingController.java b/android/src/com/mapswithme/maps/routing/RoutingController.java index 056aedfd0d..fd238aa3c5 100644 --- a/android/src/com/mapswithme/maps/routing/RoutingController.java +++ b/android/src/com/mapswithme/maps/routing/RoutingController.java @@ -389,24 +389,6 @@ public class RoutingController implements Initializable 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); diff --git a/android/src/com/mapswithme/maps/widget/placepage/PlacePageView.java b/android/src/com/mapswithme/maps/widget/placepage/PlacePageView.java index 86b97498a0..597c25897c 100644 --- a/android/src/com/mapswithme/maps/widget/placepage/PlacePageView.java +++ b/android/src/com/mapswithme/maps/widget/placepage/PlacePageView.java @@ -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()); } } diff --git a/android/src/com/mapswithme/util/PermissionsUtils.java b/android/src/com/mapswithme/util/PermissionsUtils.java index 612f99678c..0a1ad4cee4 100644 --- a/android/src/com/mapswithme/util/PermissionsUtils.java +++ b/android/src/com/mapswithme/util/PermissionsUtils.java @@ -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 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 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 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); } } diff --git a/android/src/com/mapswithme/util/permissions/PermissionsResult.java b/android/src/com/mapswithme/util/permissions/PermissionsResult.java deleted file mode 100644 index f40e3f57ba..0000000000 --- a/android/src/com/mapswithme/util/permissions/PermissionsResult.java +++ /dev/null @@ -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; - } -}