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; package net.osmand.plus;
import android.app.Activity;
import android.location.GnssNavigationMessage; import android.location.GnssNavigationMessage;
import android.location.GnssStatus; import android.location.GnssStatus;
import android.os.Build.VERSION; import android.os.Build.VERSION;
@ -1020,4 +1021,12 @@ public class OsmAndLocationProvider implements SensorEventListener {
} }
return true; 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 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])) { && Manifest.permission.WRITE_EXTERNAL_STORAGE.equals(permissions[0])) {
if (grantResults[0] != PackageManager.PERMISSION_GRANTED) { if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, Toast.makeText(this,
@ -2017,12 +2017,12 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven
Toast.LENGTH_LONG).show(); Toast.LENGTH_LONG).show();
} }
} else if (requestCode == DownloadActivity.PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE } 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])) { && Manifest.permission.WRITE_EXTERNAL_STORAGE.equals(permissions[0])) {
permissionAsked = true; permissionAsked = true;
permissionGranted = grantResults[0] == PackageManager.PERMISSION_GRANTED; permissionGranted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
} else if (requestCode == FirstUsageWizardFragment.FIRST_USAGE_REQUEST_WRITE_EXTERNAL_STORAGE_PERMISSION } 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])) { && Manifest.permission.WRITE_EXTERNAL_STORAGE.equals(permissions[0])) {
new Timer().schedule(new TimerTask() { new Timer().schedule(new TimerTask() {
@ -2045,7 +2045,7 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven
} }
}, 1); }, 1);
} else if (requestCode == MapActivityActions.REQUEST_LOCATION_FOR_DIRECTIONS_NAVIGATION_PERMISSION } 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])) { && Manifest.permission.ACCESS_FINE_LOCATION.equals(permissions[0])) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
LatLon latLon = getContextMenu().getLatLon(); LatLon latLon = getContextMenu().getLatLon();

View file

@ -1,9 +1,11 @@
package net.osmand.plus.mapcontextmenu.other; package net.osmand.plus.mapcontextmenu.other;
import android.Manifest;
import android.app.Activity; import android.app.Activity;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
@ -156,7 +158,10 @@ public class FavouritesBottomSheetMenuFragment extends MenuBottomSheetDialogFrag
targetPointsHelper.setStartPoint(ll, true, point.getPointDescription(app)); targetPointsHelper.setStartPoint(ll, true, point.getPointDescription(app));
break; break;
case TARGET: case TARGET:
targetPointsHelper.navigateToPoint(ll, true, -1, point.getPointDescription(app)); if (getActivity() != null) {
targetPointsHelper.navigateToPoint(ll, true, -1, point.getPointDescription(app));
OsmAndLocationProvider.requestFineLocationPermissionIfNeeded(getActivity());
}
break; break;
case INTERMEDIATE: case INTERMEDIATE:
targetPointsHelper.navigateToPoint(ll, true, targetPointsHelper.getIntermediatePoints().size(), point.getPointDescription(app)); 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); menu.selectMapMarker((MapMarker) item, pointType);
dismiss(); dismiss();
} else { } else {
TargetPointsHelper helper = mapActivity.getMyApplication().getTargetPointsHelper(); TargetPointsHelper targetPointsHelper = mapActivity.getMyApplication().getTargetPointsHelper();
Pair<LatLon, PointDescription> pair = getLocationAndDescrFromItem(item); Pair<LatLon, PointDescription> pair = getLocationAndDescrFromItem(item);
LatLon ll = pair.first; LatLon ll = pair.first;
PointDescription name = pair.second; PointDescription name = pair.second;
@ -442,13 +442,14 @@ public class AddPointBottomSheetDialog extends MenuBottomSheetDialogFragment {
FavouritesDbHelper favorites = requiredMyApplication().getFavorites(); FavouritesDbHelper favorites = requiredMyApplication().getFavorites();
switch (pointType) { switch (pointType) {
case START: case START:
helper.setStartPoint(ll, true, name); targetPointsHelper.setStartPoint(ll, true, name);
break; break;
case TARGET: case TARGET:
helper.navigateToPoint(ll, true, -1, name); targetPointsHelper.navigateToPoint(ll, true, -1, name);
OsmAndLocationProvider.requestFineLocationPermissionIfNeeded(mapActivity);
break; break;
case INTERMEDIATE: case INTERMEDIATE:
helper.navigateToPoint(ll, true, helper.getIntermediatePoints().size(), name); targetPointsHelper.navigateToPoint(ll, true, targetPointsHelper.getIntermediatePoints().size(), name);
break; break;
case HOME: case HOME:
favorites.setSpecialPoint(ll, FavouritePoint.SpecialPointType.HOME, null); 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) { public boolean onSingleTap(PointF point, RotatedTileBox tileBox) {
MapActivity mapActivity = getMapActivity(); MapActivity mapActivity = getMapActivity();
if (mapActivity != null) { if (mapActivity != null) {
OsmandApplication app = mapActivity.getMyApplication();
if (selectFromMapTouch) { if (selectFromMapTouch) {
LatLon latlon = tileBox.getLatLonFromPixel(point.x, point.y);
selectFromMapTouch = false; selectFromMapTouch = false;
TargetPointsHelper targets = app.getTargetPointsHelper(); LatLon latLon = tileBox.getLatLonFromPixel(point.x, point.y);
FavouritesDbHelper favorites = app.getFavorites(); choosePointTypeAction(mapActivity, latLon, selectFromMapPointType, null, null);
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;
}
if (selectFromMapWaypoints) { if (selectFromMapWaypoints) {
WaypointsFragment.showInstance(mapActivity.getSupportFragmentManager(), true); WaypointsFragment.showInstance(mapActivity.getSupportFragmentManager(), true);
} else { } else {
@ -292,6 +273,30 @@ public class MapRouteInfoMenu implements IRouteInformationListener, CardListener
return false; 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() { public OnMarkerSelectListener getOnMarkerSelectListener() {
return onMarkerSelectListener; return onMarkerSelectListener;
} }
@ -1785,29 +1790,11 @@ public class MapRouteInfoMenu implements IRouteInformationListener, CardListener
hide(); 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(); MapActivity mapActivity = getMapActivity();
if (mapActivity != null) { if (mapActivity != null) {
PointDescription pd = new PointDescription(PointDescription.POINT_TYPE_ADDRESS, name); PointDescription pd = new PointDescription(PointDescription.POINT_TYPE_ADDRESS, name);
FavouritesDbHelper favorites = mapActivity.getMyApplication().getFavorites(); choosePointTypeAction(mapActivity, latLon, pointType, pd, name);
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;
}
updateMenu(); 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(); MapActivity mapActivity = getMapActivity();
if (mapActivity != null) { if (mapActivity != null) {
if (m != null) { if (marker != null) {
LatLon point = new LatLon(m.getLatitude(), m.getLongitude()); LatLon latLon = new LatLon(marker.getLatitude(), marker.getLongitude());
FavouritesDbHelper favorites = mapActivity.getMyApplication().getFavorites(); PointDescription pd = marker.getPointDescription(mapActivity);
TargetPointsHelper targets = mapActivity.getMyApplication().getTargetPointsHelper(); choosePointTypeAction(mapActivity, latLon, pointType, pd, null);
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;
}
updateMenu(); updateMenu();
} else { } else {
MapMarkerSelectionFragment selectionFragment = MapMarkerSelectionFragment.newInstance(pointType); MapMarkerSelectionFragment selectionFragment = MapMarkerSelectionFragment.newInstance(pointType);

View file

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