Skip to content

Instantly share code, notes, and snippets.

@dmi3coder
Last active May 11, 2017 19:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmi3coder/0fefd00b0ed91ac3c37f80b01a66595b to your computer and use it in GitHub Desktop.
Save dmi3coder/0fefd00b0ed91ac3c37f80b01a66595b to your computer and use it in GitHub Desktop.
@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