-
Notifications
You must be signed in to change notification settings - Fork 213
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
Open
skywalkerDavid
wants to merge
3
commits into
master
Choose a base branch
from
dawei-location-permission
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,10 @@ | |
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; | ||
|
||
/** | ||
|
@@ -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)); | ||
for (String permission : permissions) { | ||
if (!requestPermissions.contains(permission)) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* 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) { | ||
static void onRequestPermissionsResult(Activity activity, AirMapInterface airMapInterface, int requestCode, | ||
String[] permissions, int[] grantResults) { | ||
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(); | ||
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: | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
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() { | ||
|
23 changes: 23 additions & 0 deletions
23
...y/src/main/java/com/airbnb/android/airmapview/listeners/OnLocationPermissionListener.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.airbnb.android.airmapview.listeners; | ||
|
||
/** | ||
* Interface which supplies callbacks when showing dynamic permission request dialogs | ||
* Location permission is requested when enable 'my location'. | ||
* {@link com.airbnb.android.airmapview.AirMapInterface#setMyLocationEnabled(boolean)} | ||
*/ | ||
public interface OnLocationPermissionListener { | ||
/** | ||
* Called when location permission is granted | ||
*/ | ||
void onLocationPermissionGranted(); | ||
|
||
/** | ||
* Called when location permission is denied | ||
*/ | ||
void onLocationPermissionDenied(); | ||
|
||
/** | ||
* Called when location permission is denied, and the checkbox of 'Never Ask Again' is checked | ||
*/ | ||
void onLocationPermissionPermanentlyDenied(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
it's not clear to me why you're creating this set with a single item inside?
can you just check if
permission.equals(LOCATION_PERMISSIONS)
on line55
?