Last active
May 11, 2017 19:43
-
-
Save dmi3coder/0fefd00b0ed91ac3c37f80b01a66595b to your computer and use it in GitHub Desktop.
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
@Aspect | |
public class PermissionAspect { | |
@Around("execution(@DangerousPermission void *(..))") | |
public void beforeDangerousMethod(ProceedingJoinPoint point) | |
throws Throwable { | |
Activity activity = ((ActivityHolder) point.getThis()).getActivity();//getting Activity | |
MethodSignature signature = (MethodSignature) point.getSignature(); | |
Method method = signature.getMethod(); | |
//Taking our required permission to check | |
DangerousPermission dangerousPermission = method.getAnnotation(DangerousPermission.class); | |
String requiredPermission = dangerousPermission.value(); | |
//And.. checking | |
if (VERSION.SDK_INT >= VERSION_CODES.M) { | |
if (activity.checkSelfPermission(requiredPermission) != PackageManager.PERMISSION_GRANTED) { | |
activity.requestPermissions(new String[]{requiredPermission}, 1); | |
return; | |
} | |
} | |
point.proceed(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment