Merge pull request #8332 from osmandapp/Fix_8079_ask_loc_permission

Fix #8079 - ask permission request for location after adding destination
This commit is contained in:
max-klaus 2020-01-31 15:49:30 +03:00 committed by GitHub
commit f7f670bcae
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 90 additions and 109 deletions

View file

@ -1,5 +1,6 @@
package net.osmand.plus;
import android.app.Activity;
import android.location.GnssNavigationMessage;
import android.location.GnssStatus;
import android.os.Build.VERSION;
@ -1020,4 +1021,12 @@ public class OsmAndLocationProvider implements SensorEventListener {
}
return true;
}
public static void requestFineLocationPermissionIfNeeded(Activity activity) {
if (!isLocationPermissionAvailable(activity)) {
ActivityCompat.requestPermissions(activity,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
OsmAndLocationProvider.REQUEST_LOCATION_PERMISSION);
}
}
}

View file

@ -2009,7 +2009,7 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven
}
if (requestCode == DataStorageFragment.PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE
&& grantResults.length > 0 && permissions.length > 0
&& permissions.length > 0
&& Manifest.permission.WRITE_EXTERNAL_STORAGE.equals(permissions[0])) {
if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this,
@ -2017,12 +2017,12 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven
Toast.LENGTH_LONG).show();
}
} else if (requestCode == DownloadActivity.PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE
&& grantResults.length > 0 && permissions.length > 0
&& permissions.length > 0
&& Manifest.permission.WRITE_EXTERNAL_STORAGE.equals(permissions[0])) {
permissionAsked = true;
permissionGranted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
} else if (requestCode == FirstUsageWizardFragment.FIRST_USAGE_REQUEST_WRITE_EXTERNAL_STORAGE_PERMISSION
&& grantResults.length > 0 && permissions.length > 0
&& permissions.length > 0
&& Manifest.permission.WRITE_EXTERNAL_STORAGE.equals(permissions[0])) {
new Timer().schedule(new TimerTask() {
@ -2045,7 +2045,7 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven
}
}, 1);
} else if (requestCode == MapActivityActions.REQUEST_LOCATION_FOR_DIRECTIONS_NAVIGATION_PERMISSION
&& grantResults.length > 0 && permissions.length > 0
&& permissions.length > 0
&& Manifest.permission.ACCESS_FINE_LOCATION.equals(permissions[0])) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
LatLon latLon = getContextMenu().getLatLon();

View file

@ -1,9 +1,11 @@
package net.osmand.plus.mapcontextmenu.other;
import android.Manifest;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
@ -156,7 +158,10 @@ public class FavouritesBottomSheetMenuFragment extends MenuBottomSheetDialogFrag
targetPointsHelper.setStartPoint(ll, true, point.getPointDescription(app));
break;
case TARGET:
if (getActivity() != null) {
targetPointsHelper.navigateToPoint(ll, true, -1, point.getPointDescription(app));
OsmAndLocationProvider.requestFineLocationPermissionIfNeeded(getActivity());
}
break;
case INTERMEDIATE:
targetPointsHelper.navigateToPoint(ll, true, targetPointsHelper.getIntermediatePoints().size(), point.getPointDescription(app));

View file

@ -428,7 +428,7 @@ public class AddPointBottomSheetDialog extends MenuBottomSheetDialogFragment {
menu.selectMapMarker((MapMarker) item, pointType);
dismiss();
} else {
TargetPointsHelper helper = mapActivity.getMyApplication().getTargetPointsHelper();
TargetPointsHelper targetPointsHelper = mapActivity.getMyApplication().getTargetPointsHelper();
Pair<LatLon, PointDescription> pair = getLocationAndDescrFromItem(item);
LatLon ll = pair.first;
PointDescription name = pair.second;
@ -442,13 +442,14 @@ public class AddPointBottomSheetDialog extends MenuBottomSheetDialogFragment {
FavouritesDbHelper favorites = requiredMyApplication().getFavorites();
switch (pointType) {
case START:
helper.setStartPoint(ll, true, name);
targetPointsHelper.setStartPoint(ll, true, name);
break;
case TARGET:
helper.navigateToPoint(ll, true, -1, name);
targetPointsHelper.navigateToPoint(ll, true, -1, name);
OsmAndLocationProvider.requestFineLocationPermissionIfNeeded(mapActivity);
break;
case INTERMEDIATE:
helper.navigateToPoint(ll, true, helper.getIntermediatePoints().size(), name);
targetPointsHelper.navigateToPoint(ll, true, targetPointsHelper.getIntermediatePoints().size(), name);
break;
case HOME:
favorites.setSpecialPoint(ll, FavouritePoint.SpecialPointType.HOME, null);

View file

@ -258,29 +258,10 @@ public class MapRouteInfoMenu implements IRouteInformationListener, CardListener
public boolean onSingleTap(PointF point, RotatedTileBox tileBox) {
MapActivity mapActivity = getMapActivity();
if (mapActivity != null) {
OsmandApplication app = mapActivity.getMyApplication();
if (selectFromMapTouch) {
LatLon latlon = tileBox.getLatLonFromPixel(point.x, point.y);
selectFromMapTouch = false;
TargetPointsHelper targets = app.getTargetPointsHelper();
FavouritesDbHelper favorites = app.getFavorites();
switch (selectFromMapPointType) {
case START:
targets.setStartPoint(latlon, true, null);
break;
case TARGET:
targets.navigateToPoint(latlon, true, -1);
break;
case INTERMEDIATE:
targets.navigateToPoint(latlon, true, targets.getIntermediatePoints().size());
break;
case HOME:
favorites.setSpecialPoint(latlon, FavouritePoint.SpecialPointType.HOME, null);
break;
case WORK:
favorites.setSpecialPoint(latlon, FavouritePoint.SpecialPointType.WORK, null);
break;
}
LatLon latLon = tileBox.getLatLonFromPixel(point.x, point.y);
choosePointTypeAction(mapActivity, latLon, selectFromMapPointType, null, null);
if (selectFromMapWaypoints) {
WaypointsFragment.showInstance(mapActivity.getSupportFragmentManager(), true);
} else {
@ -292,6 +273,30 @@ public class MapRouteInfoMenu implements IRouteInformationListener, CardListener
return false;
}
private void choosePointTypeAction(MapActivity mapActivity, LatLon latLon, PointType pointType, PointDescription pd, String address) {
OsmandApplication app = getApp();
FavouritesDbHelper favorites = app.getFavorites();
TargetPointsHelper targetPointsHelper = app.getTargetPointsHelper();
switch (pointType) {
case START:
targetPointsHelper.setStartPoint(latLon, true, pd);
break;
case TARGET:
targetPointsHelper.navigateToPoint(latLon, true, -1, pd);
OsmAndLocationProvider.requestFineLocationPermissionIfNeeded(mapActivity);
break;
case INTERMEDIATE:
targetPointsHelper.navigateToPoint(latLon, true, targetPointsHelper.getIntermediatePoints().size(), pd);
break;
case HOME:
favorites.setSpecialPoint(latLon, FavouritePoint.SpecialPointType.HOME, address);
break;
case WORK:
favorites.setSpecialPoint(latLon, FavouritePoint.SpecialPointType.WORK, address);
break;
}
}
public OnMarkerSelectListener getOnMarkerSelectListener() {
return onMarkerSelectListener;
}
@ -1785,29 +1790,11 @@ public class MapRouteInfoMenu implements IRouteInformationListener, CardListener
hide();
}
public void selectAddress(@Nullable String name, @NonNull LatLon l, PointType pointType) {
public void selectAddress(@Nullable String name, @NonNull LatLon latLon, PointType pointType) {
MapActivity mapActivity = getMapActivity();
if (mapActivity != null) {
PointDescription pd = new PointDescription(PointDescription.POINT_TYPE_ADDRESS, name);
FavouritesDbHelper favorites = mapActivity.getMyApplication().getFavorites();
TargetPointsHelper targets = mapActivity.getMyApplication().getTargetPointsHelper();
switch (pointType) {
case START:
targets.setStartPoint(l, true, pd);
break;
case TARGET:
targets.navigateToPoint(l, true, -1, pd);
break;
case INTERMEDIATE:
targets.navigateToPoint(l, true, targets.getIntermediatePoints().size(), pd);
break;
case HOME:
favorites.setSpecialPoint(l, FavouritePoint.SpecialPointType.HOME, name);
break;
case WORK:
favorites.setSpecialPoint(l, FavouritePoint.SpecialPointType.WORK, name);
break;
}
choosePointTypeAction(mapActivity, latLon, pointType, pd, name);
updateMenu();
}
}
@ -1844,30 +1831,13 @@ public class MapRouteInfoMenu implements IRouteInformationListener, CardListener
}
}
public void selectMapMarker(@Nullable final MapMarker m, @NonNull final PointType pointType) {
public void selectMapMarker(@Nullable final MapMarker marker, @NonNull final PointType pointType) {
MapActivity mapActivity = getMapActivity();
if (mapActivity != null) {
if (m != null) {
LatLon point = new LatLon(m.getLatitude(), m.getLongitude());
FavouritesDbHelper favorites = mapActivity.getMyApplication().getFavorites();
TargetPointsHelper targets = mapActivity.getMyApplication().getTargetPointsHelper();
switch (pointType) {
case START:
targets.setStartPoint(point, true, m.getPointDescription(mapActivity));
break;
case TARGET:
targets.navigateToPoint(point, true, -1, m.getPointDescription(mapActivity));
break;
case INTERMEDIATE:
targets.navigateToPoint(point, true, targets.getIntermediatePoints().size(), m.getPointDescription(mapActivity));
break;
case HOME:
favorites.setSpecialPoint(point, FavouritePoint.SpecialPointType.HOME, null);
break;
case WORK:
favorites.setSpecialPoint(point, FavouritePoint.SpecialPointType.WORK, null);
break;
}
if (marker != null) {
LatLon latLon = new LatLon(marker.getLatitude(), marker.getLongitude());
PointDescription pd = marker.getPointDescription(mapActivity);
choosePointTypeAction(mapActivity, latLon, pointType, pd, null);
updateMenu();
} else {
MapMarkerSelectionFragment selectionFragment = MapMarkerSelectionFragment.newInstance(pointType);

View file

@ -1,13 +1,15 @@
package net.osmand.plus.routepreparationmenu.cards;
import android.Manifest;
import android.support.v4.app.ActivityCompat;
import android.view.View;
import android.widget.TextView;
import net.osmand.data.FavouritePoint;
import net.osmand.data.LatLon;
import net.osmand.plus.FavouritesDbHelper;
import net.osmand.plus.OsmAndLocationProvider;
import net.osmand.plus.R;
import net.osmand.plus.TargetPointsHelper;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.routepreparationmenu.AddPointBottomSheetDialog;
import net.osmand.plus.routepreparationmenu.MapRouteInfoMenu.PointType;
@ -25,54 +27,48 @@ public class HomeWorkCard extends BaseCard {
@Override
protected void updateContent() {
final TargetPointsHelper targetPointsHelper = mapActivity.getMyApplication().getTargetPointsHelper();
final FavouritesDbHelper favorites = getMyApplication().getFavorites();
final FavouritePoint homePoint = favorites.getSpecialPoint(FavouritePoint.SpecialPointType.HOME);
final FavouritePoint workPoint = favorites.getSpecialPoint(FavouritePoint.SpecialPointType.WORK);
TextView homeDescr = view.findViewById(R.id.home_button_descr);
final TextView homeDescr = view.findViewById(R.id.home_button_descr);
final TextView workDescr = view.findViewById(R.id.work_button_descr);
homeDescr.setText(homePoint != null ? homePoint.getAddress() : mapActivity.getString(R.string.shared_string_add));
workDescr.setText(workPoint != null ? workPoint.getAddress() : mapActivity.getString(R.string.shared_string_add));
setSpecialButtonOnClickListeners(homePoint, R.id.home_button, PointType.HOME);
setSpecialButtonOnClickListeners(workPoint, R.id.work_button, PointType.WORK);
}
View homeButton = view.findViewById(R.id.home_button);
homeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (homePoint == null) {
AddPointBottomSheetDialog.showInstance(mapActivity, PointType.HOME);
} else {
targetPointsHelper.navigateToPoint(new LatLon(homePoint.getLatitude(), homePoint.getLongitude()),
true, -1, homePoint.getPointDescription(mapActivity));
}
}
});
private void setSpecialButtonOnClickListeners(FavouritePoint point, int buttonId, final PointType pointType) {
View homeButton = view.findViewById(buttonId);
homeButton.setOnClickListener(new SpecialButtonOnClickListener(point, pointType));
homeButton.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
AddPointBottomSheetDialog.showInstance(mapActivity, PointType.HOME);
AddPointBottomSheetDialog.showInstance(mapActivity, pointType);
return true;
}
});
}
class SpecialButtonOnClickListener implements View.OnClickListener {
FavouritePoint point;
PointType pointType;
SpecialButtonOnClickListener(FavouritePoint point, PointType pointType) {
this.point = point;
this.pointType = pointType;
}
View workButton = view.findViewById(R.id.work_button);
workButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (workPoint == null) {
AddPointBottomSheetDialog.showInstance(mapActivity, PointType.WORK);
if (point == null) {
AddPointBottomSheetDialog.showInstance(mapActivity, pointType);
} else {
targetPointsHelper.navigateToPoint(new LatLon(workPoint.getLatitude(), workPoint.getLongitude()),
true, -1, workPoint.getPointDescription(mapActivity));
mapActivity.getMyApplication().getTargetPointsHelper().navigateToPoint(
new LatLon(point.getLatitude(), point.getLongitude()),
true, -1, point.getPointDescription(app));
OsmAndLocationProvider.requestFineLocationPermissionIfNeeded(mapActivity);
}
}
});
workButton.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
AddPointBottomSheetDialog.showInstance(mapActivity, PointType.WORK);
return true;
}
});
}
}