10
10
import com .ninetripods .aopermission .permissionlib .bean .CancelBean ;
11
11
import com .ninetripods .aopermission .permissionlib .bean .DenyBean ;
12
12
import com .ninetripods .aopermission .permissionlib .interf .IPermission ;
13
+ import com .ninetripods .aopermission .permissionlib .util .Utils ;
13
14
14
15
import org .aspectj .lang .ProceedingJoinPoint ;
15
16
import org .aspectj .lang .annotation .Around ;
21
22
import java .util .List ;
22
23
23
24
/**
25
+ * 权限切面Aspect类
24
26
* Created by mq on 2018/3/6 上午11:33
25
27
26
28
*/
27
29
@ Aspect
28
30
public class PermissionAspect {
29
31
32
+ Context context ;
33
+
30
34
private static final String PERMISSION_REQUEST_POINTCUT =
31
35
"execution(@com.ninetripods.aopermission.permissionlib.annotation.NeedPermission * *(..))" ;
32
36
@@ -37,15 +41,32 @@ public void requestPermissionMethod(NeedPermission needPermission) {
37
41
@ Around ("requestPermissionMethod(needPermission)" )
38
42
public void AroundJoinPoint (final ProceedingJoinPoint joinPoint , NeedPermission needPermission ) {
39
43
40
- Context context = null ;
41
44
final Object object = joinPoint .getThis ();
45
+ if (object == null ) return ;
46
+
42
47
if (object instanceof Context ) {
43
48
context = (Context ) object ;
44
49
} else if (object instanceof Fragment ) {
45
50
context = ((Fragment ) object ).getActivity ();
46
51
} else if (object instanceof android .support .v4 .app .Fragment ) {
47
52
context = ((android .support .v4 .app .Fragment ) object ).getActivity ();
53
+ } else {
54
+ //获取切入点方法上的参数列表
55
+ Object [] objects = joinPoint .getArgs ();
56
+ if (objects .length > 0 ) {
57
+ //非静态方法且第一个参数为context
58
+ if (objects [0 ] instanceof Context ) {
59
+ context = (Context ) objects [0 ];
60
+ } else {
61
+ //没有传入context 默认使用application
62
+ context = Utils .getApp ();
63
+ }
64
+ } else {
65
+ context = Utils .getApp ();
66
+ }
67
+
48
68
}
69
+
49
70
if (context == null || needPermission == null ) return ;
50
71
51
72
PermissionRequestActivity .PermissionRequest (context , needPermission .value (),
@@ -63,21 +84,22 @@ public void PermissionGranted() {
63
84
public void PermissionDenied (int requestCode , List <String > denyList ) {
64
85
Class <?> cls = object .getClass ();
65
86
Method [] methods = cls .getDeclaredMethods ();
66
- if (methods == null || methods .length == 0 ) return ;
87
+ if (methods .length == 0 ) return ;
67
88
for (Method method : methods ) {
68
89
//过滤不含自定义注解PermissionDenied的方法
69
90
boolean isHasAnnotation = method .isAnnotationPresent (PermissionDenied .class );
70
91
if (isHasAnnotation ) {
71
92
method .setAccessible (true );
72
93
//获取方法类型
73
94
Class <?>[] types = method .getParameterTypes ();
74
- if (types == null || types .length != 1 ) return ;
95
+ if (types .length != 1 ) return ;
75
96
//获取方法上的注解
76
97
PermissionDenied aInfo = method .getAnnotation (PermissionDenied .class );
77
98
if (aInfo == null ) return ;
78
99
//解析注解上对应的信息
79
100
DenyBean bean = new DenyBean ();
80
101
bean .setRequestCode (requestCode );
102
+ bean .setContext (context );
81
103
bean .setDenyList (denyList );
82
104
try {
83
105
method .invoke (object , bean );
@@ -94,20 +116,21 @@ public void PermissionDenied(int requestCode, List<String> denyList) {
94
116
public void PermissionCanceled (int requestCode ) {
95
117
Class <?> cls = object .getClass ();
96
118
Method [] methods = cls .getDeclaredMethods ();
97
- if (methods == null || methods .length == 0 ) return ;
119
+ if (methods .length == 0 ) return ;
98
120
for (Method method : methods ) {
99
121
//过滤不含自定义注解PermissionCanceled的方法
100
122
boolean isHasAnnotation = method .isAnnotationPresent (PermissionCanceled .class );
101
123
if (isHasAnnotation ) {
102
124
method .setAccessible (true );
103
125
//获取方法类型
104
126
Class <?>[] types = method .getParameterTypes ();
105
- if (types == null || types .length != 1 ) return ;
127
+ if (types .length != 1 ) return ;
106
128
//获取方法上的注解
107
129
PermissionCanceled aInfo = method .getAnnotation (PermissionCanceled .class );
108
130
if (aInfo == null ) return ;
109
131
//解析注解上对应的信息
110
132
CancelBean bean = new CancelBean ();
133
+ bean .setContext (context );
111
134
bean .setRequestCode (requestCode );
112
135
try {
113
136
method .invoke (object , bean );
0 commit comments