-
Notifications
You must be signed in to change notification settings - Fork 212
Supply permission callback #113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,12 +6,16 @@ | |
| import android.os.Build; | ||
| import android.support.v4.app.ActivityCompat; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
|
|
||
| import static android.support.v4.content.PermissionChecker.checkSelfPermission; | ||
|
|
||
| /** | ||
| * Utility class that handles runtime permissions | ||
| */ | ||
| final class RuntimePermissionUtils { | ||
| public final class RuntimePermissionUtils { | ||
|
|
||
| private RuntimePermissionUtils() { | ||
| } | ||
|
|
@@ -35,6 +39,27 @@ private static boolean verifyPermissions(int... grantResults) { | |
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Check the permissions are the requested permissions | ||
| * | ||
| * @param permissions | ||
| * @return true if the permissions are matched perfectly | ||
| */ | ||
| static boolean isRequestedPermission(String[] permissions) { | ||
| if (permissions.length != LOCATION_PERMISSIONS.length) { | ||
| return false; | ||
| } | ||
|
|
||
| final Set<String> requestPermissions = new HashSet<>(Arrays.asList(LOCATION_PERMISSIONS)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can use |
||
| for (String permission : permissions) { | ||
| if (!requestPermissions.contains(permission)) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should use 2-space indentation instead |
||
| } | ||
|
|
||
| /** | ||
| * Returns true if the context has access to any given permissions. | ||
| */ | ||
|
|
@@ -68,31 +93,41 @@ static boolean shouldShowRequestPermissionRationale(Activity activity, String... | |
| * | ||
| * @param airMapInterface the callback interface if permission is granted. | ||
| */ | ||
| static boolean checkLocationPermissions(Activity targetActivity, AirMapInterface airMapInterface) { | ||
| static void checkLocationPermissions(Activity targetActivity, AirMapInterface airMapInterface) { | ||
| if (hasSelfPermissions(targetActivity, LOCATION_PERMISSIONS)) { | ||
| airMapInterface.onLocationPermissionsGranted(); | ||
| return true; | ||
| } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | ||
| targetActivity.requestPermissions(LOCATION_PERMISSIONS, LOCATION_PERMISSION_REQUEST_CODE); | ||
| } else { | ||
| airMapInterface.onLocationPermissionsDenied(); | ||
| } | ||
| //else don't have location permissions in pre M, don't do anything. | ||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * Dispatch actions based off requested permission results.<br /> | ||
| * Dispatch actions based off requested permission results. | ||
| * The activity or fragment must call this function in onRequestPermissionsResult | ||
| * {@link android.support.v4.app.Fragment#onRequestPermissionsResult(int, String[], int[])} | ||
| * {@link android.app.Activity#onRequestPermissionsResult(int, String[], int[])}<br /> | ||
| * Further actions like | ||
| * 1> Rationale: showing a snack bar to explain why the permissions are needed and | ||
| * 2> Denied: adding airMapInterface.onLocationPermissionsDenied() | ||
| * should be added here if needed. | ||
| * | ||
| */ | ||
| static void onRequestPermissionsResult(AirMapInterface airMapInterface, int requestCode, | ||
| int[] grantResults) { | ||
| public static void onRequestPermissionsResult(Activity activity, AirMapInterface airMapInterface, int requestCode, | ||
| String[] permissions, int[] grantResults) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. revert formatting change? |
||
| switch (requestCode) { | ||
| case LOCATION_PERMISSION_REQUEST_CODE: | ||
| if (!isRequestedPermission(permissions)) { | ||
| break; | ||
| } | ||
|
|
||
| if (verifyPermissions(grantResults)) { | ||
| airMapInterface.onLocationPermissionsGranted(); | ||
| } else if (!shouldShowRequestPermissionRationale(activity, LOCATION_PERMISSIONS)) { | ||
| airMapInterface.onLocationPermissionsNeverAskAgain(); | ||
| } else { | ||
| airMapInterface.onLocationPermissionsDenied(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. indentation is still 4 spaces here |
||
| } | ||
| break; | ||
| default: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ | |
| import com.airbnb.android.airmapview.listeners.OnCameraChangeListener; | ||
| import com.airbnb.android.airmapview.listeners.OnInfoWindowClickListener; | ||
| import com.airbnb.android.airmapview.listeners.OnLatLngScreenLocationCallback; | ||
| import com.airbnb.android.airmapview.listeners.OnLocationPermissionListener; | ||
| import com.airbnb.android.airmapview.listeners.OnMapBoundsCallback; | ||
| import com.airbnb.android.airmapview.listeners.OnMapClickListener; | ||
| import com.airbnb.android.airmapview.listeners.OnMapLoadedListener; | ||
|
|
@@ -53,6 +54,7 @@ public abstract class WebViewMapFragment extends Fragment implements AirMapInter | |
| private InfoWindowCreator infoWindowCreator; | ||
| private OnMapBoundsCallback onMapBoundsCallback; | ||
| private OnLatLngScreenLocationCallback onLatLngScreenLocationCallback; | ||
| private OnLocationPermissionListener onLocationPermissionListener; | ||
| private LatLng center; | ||
| private int zoom; | ||
| private boolean loaded; | ||
|
|
@@ -218,23 +220,52 @@ public void setOnMarkerClickListener(OnMapMarkerClickListener listener) { | |
| // no-op | ||
| } | ||
|
|
||
| @Override public void setOnLocationPermissionListener(final OnLocationPermissionListener listener) { | ||
| this.onLocationPermissionListener = listener; | ||
| } | ||
|
|
||
| @Override public void setMyLocationEnabled(boolean trackUserLocationEnabled) { | ||
| trackUserLocation = trackUserLocationEnabled; | ||
| if (trackUserLocationEnabled) { | ||
| RuntimePermissionUtils.checkLocationPermissions(getActivity(), this); | ||
| } else { | ||
| webView.loadUrl("javascript:stopTrackingUserLocation();"); | ||
| } | ||
| } | ||
|
|
||
| @Override public void onLocationPermissionsGranted() { | ||
| trackUserLocation = true; | ||
| webView.loadUrl("javascript:startTrackingUserLocation();"); | ||
| if (onLocationPermissionListener != null) { | ||
| onLocationPermissionListener.onLocationPermissionGranted(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void onLocationPermissionsDenied() { | ||
| trackUserLocation = false; | ||
| webView.loadUrl("javascript:stopTrackingUserLocation();"); | ||
| if (onLocationPermissionListener != null) { | ||
| onLocationPermissionListener.onLocationPermissionDenied(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void onLocationPermissionsNeverAskAgain() { | ||
| trackUserLocation = false; | ||
| webView.loadUrl("javascript:stopTrackingUserLocation();"); | ||
| if (onLocationPermissionListener != null) { | ||
| onLocationPermissionListener.onLocationPermissionPermanentlyDenied(); | ||
| } | ||
| } | ||
|
|
||
| @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, | ||
| @NonNull int[] grantResults) { | ||
| @NonNull int[] grantResults) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. formatting |
||
| super.onRequestPermissionsResult(requestCode, permissions, grantResults); | ||
| RuntimePermissionUtils.onRequestPermissionsResult(this, requestCode, grantResults); | ||
| RuntimePermissionUtils.onRequestPermissionsResult(this.getActivity(), this, requestCode, permissions, grantResults); | ||
| } | ||
|
|
||
| @Override | ||
| public void onCheckLocationPermissionResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { | ||
| RuntimePermissionUtils.onRequestPermissionsResult(this.getActivity(), this, requestCode, permissions, grantResults); | ||
| } | ||
|
|
||
| @Override public boolean isMyLocationEnabled() { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package com.airbnb.android.airmapview.listeners; | ||
|
|
||
| public interface OnLocationPermissionListener { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you please add javadocs to this interface? |
||
| void onLocationPermissionGranted(); | ||
| void onLocationPermissionDenied(); | ||
| void onLocationPermissionPermanentlyDenied(); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does this need to be public?