Request location permissions on demand

pull/795/head
Alex Baker 6 years ago
parent 8cdbe66a19
commit a4796d9952

@ -73,10 +73,14 @@ public class GoogleMapFragment implements MapFragment, OnMapReadyCallback, OnMar
}
@SuppressLint("MissingPermission")
@Override
public void showMyLocation() {
map.setMyLocationEnabled(true);
}
@Override
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
map.setMyLocationEnabled(true);
if (dark) {
map.setMapStyle(MapStyleOptions.loadRawResourceStyle(context, R.raw.mapstyle_night));
}

@ -4,6 +4,7 @@ import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Lists.transform;
import static com.todoroo.andlib.utility.AndroidUtilities.atLeastLollipop;
import static com.todoroo.andlib.utility.AndroidUtilities.hideKeyboard;
import static org.tasks.PermissionUtil.verifyPermissions;
import static org.tasks.data.Place.newPlace;
import android.annotation.SuppressLint;
@ -57,6 +58,7 @@ import org.tasks.R;
import org.tasks.billing.Inventory;
import org.tasks.data.LocationDao;
import org.tasks.data.PlaceUsage;
import org.tasks.dialogs.DialogBuilder;
import org.tasks.gtasks.PlayServices;
import org.tasks.injection.ActivityComponent;
import org.tasks.injection.ForApplication;
@ -64,6 +66,9 @@ import org.tasks.injection.InjectingAppCompatActivity;
import org.tasks.location.LocationPickerAdapter.OnLocationPicked;
import org.tasks.location.LocationSearchAdapter.OnPredictionPicked;
import org.tasks.location.MapFragment.MapFragmentCallback;
import org.tasks.preferences.ActivityPermissionRequestor;
import org.tasks.preferences.PermissionChecker;
import org.tasks.preferences.PermissionRequestor;
import org.tasks.preferences.Preferences;
import org.tasks.themes.Theme;
import org.tasks.themes.ThemeColor;
@ -114,6 +119,9 @@ public class LocationPickerActivity extends InjectingAppCompatActivity
@Inject Preferences preferences;
@Inject LocationDao locationDao;
@Inject PlaceSearchProvider searchProvider;
@Inject PermissionChecker permissionChecker;
@Inject ActivityPermissionRequestor permissionRequestor;
@Inject DialogBuilder dialogBuilder;
private MapFragment map;
private FusedLocationProviderClient fusedLocationProviderClient;
@ -269,12 +277,15 @@ public class LocationPickerActivity extends InjectingAppCompatActivity
@Override
public void onMapReady(MapFragment mapFragment) {
map = mapFragment;
updateMarkers();
if (permissionChecker.canAccessLocation()) {
mapFragment.showMyLocation();
}
if (mapPosition != null) {
map.movePosition(mapPosition, false);
} else {
} else if (permissionRequestor.requestFineLocation()) {
moveToCurrentLocation(false);
}
updateMarkers();
}
@Override
@ -302,7 +313,28 @@ public class LocationPickerActivity extends InjectingAppCompatActivity
@OnClick(R.id.current_location)
void onClick() {
moveToCurrentLocation(true);
if (permissionRequestor.requestFineLocation()) {
moveToCurrentLocation(true);
}
}
@Override
public void onRequestPermissionsResult(
int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == PermissionRequestor.REQUEST_LOCATION) {
if (verifyPermissions(grantResults)) {
map.showMyLocation();
moveToCurrentLocation(true);
} else {
dialogBuilder
.newMessageDialog(R.string.location_permission_required_location)
.setTitle(R.string.missing_permissions)
.setPositiveButton(android.R.string.ok, null)
.show();
}
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
@OnClick(R.id.select_this_location)

@ -77,21 +77,21 @@ public class MapboxMapFragment implements MapFragment, OnMapReadyCallback, OnMar
}
@SuppressLint("MissingPermission")
@Override
public void showMyLocation() {
LocationComponent locationComponent = map.getLocationComponent();
locationComponent.activateLocationComponent(context, map.getStyle());
locationComponent.setLocationComponentEnabled(true);
locationComponent.setCameraMode(CameraMode.NONE);
locationComponent.setRenderMode(RenderMode.NORMAL);
}
@Override
public void onMapReady(@NonNull MapboxMap mapboxMap) {
map = mapboxMap;
map.getUiSettings().setRotateGesturesEnabled(false);
map.setStyle(
dark ? Style.DARK : Style.MAPBOX_STREETS,
style -> {
LocationComponent locationComponent = map.getLocationComponent();
locationComponent.activateLocationComponent(context, style);
locationComponent.setLocationComponentEnabled(true);
locationComponent.setCameraMode(CameraMode.NONE);
locationComponent.setRenderMode(RenderMode.NORMAL);
});
map.setOnMarkerClickListener(this);
callbacks.onMapReady(this);
map.setStyle(dark ? Style.DARK : Style.MAPBOX_STREETS, style -> callbacks.onMapReady(this));
}
@Override

@ -1,6 +1,7 @@
package org.tasks.dialogs;
import static android.app.Activity.RESULT_OK;
import static org.tasks.PermissionUtil.verifyPermissions;
import static org.tasks.dialogs.SeekBarDialog.newSeekBarDialog;
import android.app.Dialog;
@ -18,6 +19,7 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;
import com.google.common.base.Strings;
import javax.inject.Inject;
@ -27,6 +29,10 @@ import org.tasks.injection.DialogFragmentComponent;
import org.tasks.injection.ForActivity;
import org.tasks.injection.InjectingDialogFragment;
import org.tasks.locale.Locale;
import org.tasks.preferences.FragmentPermissionRequestor;
import org.tasks.preferences.PermissionChecker;
import org.tasks.preferences.PermissionRequestor;
import org.tasks.ui.Toaster;
public class LocationDialog extends InjectingDialogFragment {
@ -39,6 +45,9 @@ public class LocationDialog extends InjectingDialogFragment {
@Inject DialogBuilder dialogBuilder;
@Inject @ForActivity Context context;
@Inject Locale locale;
@Inject PermissionChecker permissionChecker;
@Inject FragmentPermissionRequestor permissionRequestor;
@Inject Toaster toaster;
@BindView(R.id.location_arrival)
Switch arrivalView;
@ -63,6 +72,13 @@ public class LocationDialog extends InjectingDialogFragment {
return dialog;
}
@OnCheckedChanged({R.id.location_arrival, R.id.location_departure})
void geofenceCheckedChanged(boolean enabled) {
if (enabled) {
permissionRequestor.requestFineLocation();
}
}
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
@ -74,8 +90,9 @@ public class LocationDialog extends InjectingDialogFragment {
LayoutInflater layoutInflater = LayoutInflater.from(context);
View view = layoutInflater.inflate(R.layout.location_details, null);
ButterKnife.bind(this, view);
arrivalView.setChecked(location.isArrival());
departureView.setChecked(location.isDeparture());
boolean hasLocationPermission = permissionChecker.canAccessLocation();
arrivalView.setChecked(hasLocationPermission && location.isArrival());
departureView.setChecked(hasLocationPermission && location.isDeparture());
updateRadius(location.getRadius());
String phone = location.getPhone();
if (!Strings.isNullOrEmpty(phone)) {
@ -138,6 +155,32 @@ public class LocationDialog extends InjectingDialogFragment {
outState.putParcelable(EXTRA_LOCATION, toLocation());
}
@Override
public void onResume() {
super.onResume();
if (!permissionChecker.canAccessLocation()) {
arrivalView.setChecked(false);
departureView.setChecked(false);
}
}
@Override
public void onRequestPermissionsResult(
int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == PermissionRequestor.REQUEST_LOCATION) {
if (!verifyPermissions(grantResults)) {
dialogBuilder
.newMessageDialog(R.string.location_permission_required_geofence)
.setTitle(R.string.missing_permissions)
.setPositiveButton(android.R.string.ok, null)
.show();
}
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_RADIUS) {

@ -11,6 +11,8 @@ public interface MapFragment {
void setMarkers(List<Place> places);
void showMyLocation();
interface MapFragmentCallback {
void onMapReady(MapFragment mapFragment);

@ -2,7 +2,6 @@ package org.tasks.ui;
import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Sets.newHashSet;
import static org.tasks.PermissionUtil.verifyPermissions;
import static org.tasks.dialogs.LocationDialog.newLocationDialog;
import android.app.Activity;
@ -38,8 +37,6 @@ import org.tasks.dialogs.LocationDialog;
import org.tasks.injection.FragmentComponent;
import org.tasks.location.GeofenceService;
import org.tasks.location.PlacePicker;
import org.tasks.preferences.FragmentPermissionRequestor;
import org.tasks.preferences.PermissionRequestor;
import org.tasks.preferences.Preferences;
import timber.log.Timber;
@ -55,7 +52,6 @@ public class LocationControlSet extends TaskEditControlFragment {
private static final String EXTRA_GEOFENCES = "extra_geofences";
private final Set<Location> locations = new LinkedHashSet<>();
@Inject GeofenceService geofenceService;
@Inject FragmentPermissionRequestor permissionRequestor;
@Inject Preferences preferences;
@Inject DialogBuilder dialogBuilder;
@ -89,9 +85,7 @@ public class LocationControlSet extends TaskEditControlFragment {
@OnClick(R.id.alarms_add)
void addAlarm(View view) {
if (permissionRequestor.requestFineLocation()) {
pickLocation();
}
pickLocation();
}
@Override
@ -166,23 +160,8 @@ public class LocationControlSet extends TaskEditControlFragment {
}
}
@Override
public void onRequestPermissionsResult(
int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == PermissionRequestor.REQUEST_LOCATION) {
if (verifyPermissions(grantResults)) {
pickLocation();
}
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
private void pickLocation() {
Intent intent = PlacePicker.getIntent(getActivity());
if (intent != null) {
startActivityForResult(intent, REQUEST_LOCATION_REMINDER);
}
startActivityForResult(PlacePicker.getIntent(getActivity()), REQUEST_LOCATION_REMINDER);
}
private void addGeolocationReminder(final Location location) {

@ -880,4 +880,7 @@ File %1$s contained %2$s.\n\n
<string name="map_search_provider">Search provider</string>
<string name="requires_android_version">Requires Android %s</string>
<string name="requires_google_play_services">Requires Google Play Services</string>
<string name="missing_permissions">Missing permissions</string>
<string name="location_permission_required_geofence">Location permissions are needed for location reminders</string>
<string name="location_permission_required_location">Location permissions are needed to find your current location</string>
</resources>

Loading…
Cancel
Save