package com.xxx.base.aspectj;
import android.util.Log;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import java.util.HashMap;
import java.util.Map;
/**
* author: liweipeng
* date: 2020-04-09
*/
@Aspect
public class ActivityTimeAspectJ {
private Map<String, Long> timeMap = new HashMap<>();
@Pointcut("execution(* android.app.Activity+.onCreate(..))")
public void activityOnCreate() {
}
@Pointcut("execution(* android.app.Activity+.onResume(..))")
public void activityOnResume() {
}
@Pointcut("execution(* android.support.v4.app.Fragment+.onCreate(..))")
public void fragmentOnCreate() {
}
@Pointcut("execution(* android.support.v4.app.Fragment+.onResume(..))")
public void fragmentOnResume() {
}
@Before("activityOnCreate()||fragmentOnCreate()")
public void activityOnCreateBefore(JoinPoint joinPoint) {
String name = joinPoint.getThis().getClass().getName();
Log.e("aspectj", "aspectj---onCreate,name=" + name);
long startTime = System.currentTimeMillis();
timeMap.put(name, startTime);
}
@After("activityOnResume()||fragmentOnResume()")
public void activityOnOnResumeAfter(JoinPoint joinPoint) {
String name = joinPoint.getThis().getClass().getName();
Log.e("aspectj", "aspectj---onResume,name=" + name);
long endTime = System.currentTimeMillis();
if (timeMap.containsKey(name)) {
Long startTime = timeMap.remove(name);
Log.e("aspectj", "aspectj---onResume---" + (endTime - startTime)+"ms--"+name);
}
}
// @Around("onCreateOrOnResume()")
// public Object onCreateOrOnResumeAdvice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
// Object result = null;
// try {
// AppCompatActivity activity = (AppCompatActivity) proceedingJoinPoint.getTarget();
// // Log.d("AJAOP", "Aop Info" + activity.getClass().getCanonicalName() +
// // "\r\nkind : " + thisJoinPoint.getKind() +
// // "\r\nargs : " + thisJoinPoint.getArgs() +
// // "\r\nClass : " + thisJoinPoint.getClass() +
// // "\r\nsign : " + thisJoinPoint.getSignature() +
// // "\r\nsource : " + thisJoinPoint.getSourceLocation() +
// // "\r\nthis : " + thisJoinPoint.getThis()
// // );
// long startTime = System.currentTimeMillis();
// result = proceedingJoinPoint.proceed();
// String activityName = activity.getClass().getCanonicalName();
//
// Signature signature = proceedingJoinPoint.getSignature();
// String sign = "";
// String methodName = "";
// if (signature != null) {
// sign = signature.toString();
// methodName = signature.getName();
// }
// if (!TextUtils.isEmpty(activityName) && !TextUtils.isEmpty(sign) && sign.contains(activityName)) {
// Log.d("", "aspectj---activityName=" + activityName + ",methodName=" + methodName + ",sign=" + sign);
// }
// } catch (Exception e) {
// e.printStackTrace();
// } catch (Throwable throwable) {
// throwable.printStackTrace();
// }
// return result;
// }
}