@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface RequirePermission {
String[] value();
}
- aspectj
@Aspect
public class RequirePermissionAspect {
private final String TAG = “RequirePermissionAspect”;
private final String POINTCUT = “execution(@com.androidwind.quickaop.annotation.RequirePermission * *(…))”;
@Pointcut(POINTCUT)
public void onRequirePermissionMethod() {
}
@Around(“onRequirePermissionMethod() && @annotation(requirePermission)”)
public void doRequirePermissionMethod(ProceedingJoinPoint joinPoint, RequirePermission requirePermission) throws Throwable {
FragmentActivity activity = null;
final Object object = joinPoint.getThis();
if (object instanceof FragmentActivity) {
activity = (FragmentActivity) object;
} else if (object instanceof Fragment) {
activity = ((Fragment) object).getActivity();
}
if (activity == null) {
joinPoint.proceed();
} else {
new RxPermissions(activity)
.request(requirePermission.value())
.subscribe(new Consumer() {
@Override
public void accept(Boolean granted) throws Exception {
if (granted) { // Always true pre-M
try {
joinPoint.proceed();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
} else {
Toast.makeText(MyApplication.getApplication(), “授权失败!”, Toast.LENGTH_SHORT).show();
}
}
});
}
}
}
- 使用方法
@RequirePermission(value = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE})
public void requirePermission(View view) {
Toast.makeText(MyApplication.getApplication(), “授权成功,继续进行”, Toast.LENGTH_SHORT).show();
}
- 功能
添加埋点信息,用于数据统计功能。
- annotation
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface EventTracking {
String key();
String value();
}
- aspectj
@Aspect
public class EventTrackingAspect {
private final String TAG = “EventTrackingAspect”;
private final String POINTCUT = “execution(@com.androidwind.quickaop.library.annotation.EventTracking * *(…))”;
@Pointcut(POINTCUT)
public void onEventTrackingMethod() {
}
@Around(“onEventTrackingMethod() && @annotation(eventTracking)”)
public void doEventTrackingMethod(ProceedingJoinPoint joinPoint, EventTracking eventTracking) throws Throwable {
String key = eventTracking.key();
String value = eventTracking.value();
SPUtils.getInstance().put(key, value);
joinPoint.proceed();
}
}
- 使用方法
@EventTracking(key = “1000”, value = “埋点值1”)
public void eventTracking(View view) {
Log.i(TAG, "this is an event tracking log, the eventTracking is " + SPUtils.getInstance().getString(“1000”));
}
- 功能
保证方法是通过异步方式执行,这里使用RxJava实现异步。
- annotation
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface Asynchronize {
}
- aspectj
@Aspect
public class AsynchronizeAspect {
private final String TAG = “AsynchronizeAspect”;
private final String POINTCUT = “execution(@com.androidwind.quickaop.annotation.Asynchronize * *(…))”;
@Pointcut(POINTCUT)
public void onAsynchronizeMethod() {
}
@Around(“onAsynchronizeMethod()”)
public void doAsynchronizeMethod(ProceedingJoinPoint joinPoint) throws Throwable {
Observable.create(new ObservableOnSubscribe() {
@Override
public void subscribe(ObservableEmitter emitter) throws Exception {
System.out.println(“[Thread Name-AsynchronizeAspect: ]” + Thread.currentThread().getName());
try {
joinPoint.proceed();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe();
}
}
- 使用方法
@Asynchronize
public void asynchronize(View view) {
System.out.println(“[Thread Name-asynchronize: ]” + Thread.currentThread().getName());
}
- 功能
捕获此方法所可能产生的异常情况,保证执行此方法不会导致app崩溃。
- annotation
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface CatchException {
}
- aspectj
@Aspect
public class CatchExceptionAspect {
private final String TAG = “CatchExceptionAspect”;
private final String POINTCUT = “execution(@com.androidwind.quickaop.library.annotation.CatchException * *(…))”;
@Pointcut(POINTCUT)
public void onCatchExceptionMethod() {
}
@Around(“onCatchExceptionMethod()”)
public void doCatchExceptionMethod(ProceedingJoinPoint joinPoint) throws Throwable {
try {
joinPoint.proceed();
} catch (Exception e) {
LogUtils.e(TAG, getException(e));
}
}
private String getException(Throwable ex) {
StringWriter errors = new StringWriter();
ex.printStackTrace(new PrintWriter(errors));
return errors.toString();
}
}
- 使用方法
@CatchException
public void catchException(View view) {
String s = null;
s.toString();
}
- 功能
可以在执行某个方法的前和后,执行另外指定的方法。
- annotation
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface HookMethod {
String beforeMethod();
String afterMethod();
}
- aspectj
@Aspect
public class HookMethodAspect {
private final String TAG = “HookMethodAspect”;
private final String POINTCUT = “execution(@com.androidwind.quickaop.library.annotation.HookMethod * *(…))”;
@Pointcut(POINTCUT)
public void onHookMethodMethod() {
}
@Around(“onHookMethodMethod()”)
public void doHookMethodMethod(ProceedingJoinPoint joinPoint) throws Throwable {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
HookMethod hookMethod = method.getAnnotation(HookMethod.class);
if (hookMethod == null) {
return;
}
String beforeMethod = hookMethod.beforeMethod();
String afterMethod = hookMethod.afterMethod();
if (!StringUtils.isEmpty(beforeMethod)) {
try {
ReflectUtils.reflect(joinPoint.getTarget()).method(beforeMethod);
} catch (ReflectUtils.ReflectException e) {
e.printStackTrace();
Log.e(TAG, "no method " + beforeMethod);
}
}
joinPoint.proceed();
if (!StringUtils.isEmpty(afterMethod)) {
try {
ReflectUtils.reflect(joinPoint.getTarget()).method(afterMethod);
} catch (ReflectUtils.ReflectException e) {
e.printStackTrace();
Log.e(TAG, "no method " + afterMethod);
}
}
}
}
- 使用方法
@HookMethod(beforeMethod = “beforeMethod”, afterMethod = “afterMethod”)
public void hookMethod(View view) {
Log.i(TAG, “this is a hookMethod”);
}
private void beforeMethod() {
Log.i(TAG, “this is a before method”);
}
private void afterMethod() {
Log.i(TAG, “this is an after method”);
}
- 功能
将方法执行结果保存到缓存中,这里使用sharedpreferences。
- annotation
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Cache {
String key();
}
- aspectj
@Aspect
public class CacheAspect {
private static final String POINTCUT_METHOD = “execution(@com.androidwind.quickaop.library.annotation.Cache * *(…))”;
@Pointcut(POINTCUT_METHOD)
public void onCacheMethod() {
}
@Around(“onCacheMethod() && @annotation(cache)”)
public Object doCacheMethod(ProceedingJoinPoint joinPoint, Cache cache) throws Throwable {
String key = cache.key();
Object result = joinPoint.proceed();
if (result instanceof String) {
SPUtils.getInstance().put(key, (String)result);
}
return result;
}
}
- 使用方法
@Cache(key = “name”)
public String cache(View view) {
return “Jerry”;
}
- 功能
对入参进行null检测。
- annotation
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface NullCheck {
int position() default 0;//the input params position
}
- aspectj
@Aspect
public class NullCheckAspect {
private final String TAG = “NullCheckAspect”;
private final String POINTCUT = “execution(@com.androidwind.quickaop.library.annotation.NullCheck * *(…))”;
@Pointcut(POINTCUT)
public void onNullCheckMethod() {
}
@Around(“onNullCheckMethod() && @annotation(nullCheck)”)
public void doNullCheckMethod(ProceedingJoinPoint joinPoint, NullCheck nullCheck) throws Throwable {
int position = nullCheck.position();
Object[] objects = joinPoint.getArgs();
if (objects.length > 0 && position < objects.length) {
if (!ObjectUtils.isEmpty(objects[position])) {
joinPoint.proceed();
}
}
}
}
- 使用方法
public void nullCheck(View view) {
getString(“Tommy”, null);
}
@NullCheck(position = 1)
private void getString(String name, String country) {
Log.i(TAG, “this is after nullcheck input”);
}
- 功能
Android UI初始化完毕后,对View的操作,这里以显示一个View布局为例。
- annotation
这里需要注意ElementType选择的是TYPE,因为作用的是类。
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface AddView {
}
- aspectj
@Aspect
public class AddViewAspect {
private final String TAG = “AddViewAspect”;
private final String POINTCUT = “execution(* android.app.Activity.onCreate(…)) && within(@com.androidwind.quickaop.annotation.AddView *)”;
@After(POINTCUT)
public void addView(JoinPoint joinPoint) throws Throwable {
FragmentActivity activity = null;
String signatureStr = joinPoint.getSignature().toString();
Log.d(TAG, “[addView]” + signatureStr);
final Object object = joinPoint.getThis();
if (object instanceof FragmentActivity) {
activity = (FragmentActivity) object;
TextView tv = activity.findViewById(R.id.view);
tv.setVisibility(View.VISIBLE);
}
}
}
- 使用方法
@AddView
public class MainActivity extends AppCompatActivity {
private final String TAG = “MainActivity”;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, “[addView]” + “MainActivity->onCreate”);
System.out.println(“[Thread Name-MainActivity: ]” + Thread.currentThread().getName());
}
}
- 功能