将车聊聊工程代码加入到项目中,保证能跑起来了,后续做架构升级
Signed-off-by: 董宏宇 <martindhy@gmail.com>
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
package com.mogo.chat.aspect
|
||||
|
||||
import android.os.Looper
|
||||
import android.os.Trace
|
||||
import android.util.Log
|
||||
import com.mogo.commons.debug.DebugConfig
|
||||
import org.aspectj.lang.ProceedingJoinPoint
|
||||
import org.aspectj.lang.reflect.CodeSignature
|
||||
import org.aspectj.lang.reflect.MethodSignature
|
||||
|
||||
open class BaseAspectj {
|
||||
|
||||
companion object {
|
||||
@Volatile
|
||||
private var enable: Boolean = DebugConfig.isDebug()
|
||||
}
|
||||
|
||||
fun enterMethod(joinPoint: ProceedingJoinPoint) {
|
||||
if (!enable) return
|
||||
|
||||
val signature = joinPoint.signature as CodeSignature
|
||||
val cls = signature.declaringType
|
||||
val methodName = signature.name
|
||||
val parameterNames = signature.parameterNames
|
||||
val parameterValues = joinPoint.args
|
||||
|
||||
val builder = StringBuilder("\u21E2 ")
|
||||
builder.append(methodName).append('(')
|
||||
parameterValues.forEachIndexed { index, _ ->
|
||||
if (index > 0) {
|
||||
builder.append(",")
|
||||
}
|
||||
builder.append(parameterNames[index]).append('=')
|
||||
builder.append(parameterValues[index])
|
||||
}
|
||||
builder.append(')')
|
||||
|
||||
if (Looper.myLooper() != Looper.getMainLooper()) {
|
||||
builder.append("[Thread:\"").append(Thread.currentThread().name).append("\"]")
|
||||
}
|
||||
Log.i(asTag(cls), builder.toString())
|
||||
}
|
||||
|
||||
fun exitMethod(joinPoint: ProceedingJoinPoint, result: Any?, lengthMill: Long) {
|
||||
if (!enable) return
|
||||
|
||||
val signature = joinPoint.signature
|
||||
val cls = signature.declaringType
|
||||
val methodName = signature.name
|
||||
val hasReturnType = signature is MethodSignature
|
||||
&& signature.returnType != Void.TYPE
|
||||
|
||||
val builder = StringBuilder("\u21E0 ")
|
||||
.append(methodName)
|
||||
.append("[")
|
||||
.append(lengthMill)
|
||||
.append("ms]")
|
||||
|
||||
if (hasReturnType) {
|
||||
builder.append(" = ")
|
||||
builder.append(result.let {
|
||||
result.toString()
|
||||
})
|
||||
}
|
||||
Log.i(asTag(cls), builder.toString())
|
||||
}
|
||||
|
||||
private fun asTag(cls: Class<*>): String {
|
||||
if (cls.isAnonymousClass) {
|
||||
return asTag(cls.enclosingClass!!)
|
||||
}
|
||||
return cls.simpleName
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.mogo.chat.aspect;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import static java.lang.annotation.ElementType.CONSTRUCTOR;
|
||||
import static java.lang.annotation.ElementType.METHOD;
|
||||
import static java.lang.annotation.ElementType.TYPE;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
@Target({TYPE, METHOD, CONSTRUCTOR})
|
||||
@Retention(RUNTIME)
|
||||
public @interface DebugLog {
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.mogo.chat.aspect
|
||||
|
||||
import org.aspectj.lang.ProceedingJoinPoint
|
||||
import org.aspectj.lang.annotation.Around
|
||||
import org.aspectj.lang.annotation.Aspect
|
||||
import org.aspectj.lang.annotation.Pointcut
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
@Aspect
|
||||
class ExceptionAspectj : BaseAspectj() {
|
||||
|
||||
@Pointcut("execution(* com.tencent.sharp.jni.TraeAudioManager\$2.run(..))")
|
||||
fun gmeTrack() {
|
||||
}
|
||||
|
||||
@Around("gmeTrack()")
|
||||
fun logExecute(joinPoint: ProceedingJoinPoint) {
|
||||
try {
|
||||
enterMethod(joinPoint)
|
||||
val startNanos = System.nanoTime()
|
||||
val result = joinPoint.proceed()
|
||||
val stopNanos = System.nanoTime()
|
||||
val lengthMill = TimeUnit.NANOSECONDS.toMillis(stopNanos - startNanos)
|
||||
exitMethod(joinPoint, result, lengthMill)
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.mogo.chat.aspect
|
||||
|
||||
import org.aspectj.lang.ProceedingJoinPoint
|
||||
import org.aspectj.lang.annotation.Around
|
||||
import org.aspectj.lang.annotation.Aspect
|
||||
import org.aspectj.lang.annotation.Pointcut
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
@Aspect
|
||||
class LogAspectj : BaseAspectj() {
|
||||
|
||||
|
||||
@Pointcut("within(@com.mogo.chat.aspect.DebugLog *)")
|
||||
fun withinAnnotatedClass() {
|
||||
}
|
||||
|
||||
@Pointcut("execution(!synthetic * *(..))&& withinAnnotatedClass()")
|
||||
fun methodInsideAnnotatedType() {
|
||||
}
|
||||
|
||||
@Pointcut("execution(!synthetic *.new(..))&& withinAnnotatedClass()")
|
||||
fun constructorInsideAnnotatedType() {
|
||||
}
|
||||
|
||||
@Pointcut("execution(@com.mogo.chat.aspect.DebugLog * *(..))|| methodInsideAnnotatedType()")
|
||||
fun method() {
|
||||
}
|
||||
|
||||
@Pointcut("execution(@com.mogo.chat.aspect.DebugLog *.new(..))|| constructorInsideAnnotatedType()")
|
||||
fun constructor() {
|
||||
}
|
||||
|
||||
@Around("method() || constructor()")
|
||||
fun logExecute(joinPoint: ProceedingJoinPoint) {
|
||||
|
||||
enterMethod(joinPoint)
|
||||
|
||||
val startNanos = System.nanoTime()
|
||||
val result = joinPoint.proceed()
|
||||
val stopNanos = System.nanoTime()
|
||||
val lengthMill = TimeUnit.NANOSECONDS.toMillis(stopNanos - startNanos)
|
||||
|
||||
exitMethod(joinPoint, result, lengthMill)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.mogo.chat.aspect;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import static java.lang.annotation.ElementType.CONSTRUCTOR;
|
||||
import static java.lang.annotation.ElementType.METHOD;
|
||||
import static java.lang.annotation.ElementType.TYPE;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
@Target({TYPE, METHOD, CONSTRUCTOR})
|
||||
@Retention(RUNTIME)
|
||||
public @interface PushMsg {
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.mogo.chat.aspect
|
||||
|
||||
import android.util.Log
|
||||
import android.view.View
|
||||
import com.mogo.chat.constant.PUSH_MSG_AGREE_ENTER
|
||||
import com.mogo.chat.constant.PUSH_MSG_HANG_UP
|
||||
import com.mogo.chat.model.bean.Message
|
||||
import com.mogo.chat.util.isDoubleClick
|
||||
import com.mogo.chat.util.sp.recordCallTime
|
||||
import com.mogo.chat.util.trackHangUp
|
||||
import org.aspectj.lang.JoinPoint
|
||||
import org.aspectj.lang.annotation.Aspect
|
||||
import org.aspectj.lang.annotation.Before
|
||||
import org.aspectj.lang.annotation.Pointcut
|
||||
|
||||
@Aspect
|
||||
class TrackAspectj {
|
||||
|
||||
companion object {
|
||||
const val TAG = "TrackAspectj"
|
||||
}
|
||||
|
||||
@Pointcut("execution(* android.view.View.OnClickListener.onClick(..))")
|
||||
fun trackOnClick() {
|
||||
|
||||
}
|
||||
|
||||
@Before("trackOnClick()")
|
||||
fun trackClick(joinPoint: JoinPoint) {
|
||||
val view = joinPoint.args[0] as View
|
||||
if (isDoubleClick(view.id)) {
|
||||
Log.i("trackClick", "重复点击,已过滤")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Pointcut("within(@com.mogo.chat.aspect.PushMsg *)")
|
||||
fun withinPushClass() {
|
||||
}
|
||||
|
||||
@Pointcut("execution(!synthetic * *(..))&& withinPushClass()")
|
||||
fun methodInsidePushType() {
|
||||
}
|
||||
|
||||
@Pointcut("execution(@com.mogo.chat.aspect.PushMsg * *(..))|| methodInsidePushType()")
|
||||
fun pushMethod() {
|
||||
}
|
||||
|
||||
@Before("pushMethod()")
|
||||
fun trackPushMsg(joinPoint: JoinPoint) {
|
||||
val msg = joinPoint.args[0] as Message
|
||||
when (msg.status) {
|
||||
PUSH_MSG_AGREE_ENTER -> {
|
||||
recordCallTime()
|
||||
}
|
||||
PUSH_MSG_HANG_UP -> {
|
||||
trackHangUp(msg.type)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user