|
| 1 | +/* |
| 2 | + * Created by zhangwei on 2021/08/09. |
| 3 | + * Copyright 2015-2021 Sensors Data Inc. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package com.sensorsdata.analytics.android.plugin.push; |
| 19 | + |
| 20 | +import com.sensorsdata.analytics.android.plugin.SensorsAnalyticsUtil; |
| 21 | + |
| 22 | +import org.objectweb.asm.MethodVisitor; |
| 23 | +import org.objectweb.asm.Opcodes; |
| 24 | +import org.objectweb.asm.Type; |
| 25 | +import org.objectweb.asm.commons.AdviceAdapter; |
| 26 | + |
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.List; |
| 29 | + |
| 30 | +public class SensorsAnalyticsPushMethodVisitor extends AdviceAdapter { |
| 31 | + private static final String PENDING_INTENT_OWNER = "android/app/PendingIntent"; |
| 32 | + |
| 33 | + public SensorsAnalyticsPushMethodVisitor(MethodVisitor methodVisitor, int access, String name, String descriptor) { |
| 34 | + super(SensorsAnalyticsUtil.ASM_VERSION, methodVisitor, access, name, descriptor); |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public void visitCode() { |
| 39 | + super.visitCode(); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public void visitMethodInsn(int opcodeAndSource, String owner, String name, String descriptor, boolean isInterface) { |
| 44 | + //PendingIntent.getActivity() before and after |
| 45 | + if (opcodeAndSource == Opcodes.INVOKESTATIC && PENDING_INTENT_OWNER.equals(owner) && checkPendingIntentName(name)) { |
| 46 | + Type[] argTypes = Type.getArgumentTypes(descriptor); |
| 47 | + List<Integer> positionList = new ArrayList<>(); |
| 48 | + for (int index = argTypes.length - 1; index >= 0; index--) { |
| 49 | + int position = newLocal(argTypes[index]); |
| 50 | + storeLocal(position, argTypes[index]); |
| 51 | + positionList.add(0, position); |
| 52 | + } |
| 53 | + positionList.forEach(this::loadLocal); |
| 54 | + mv.visitMethodInsn(Opcodes.INVOKESTATIC, SensorsPushInjected.PUSH_TRACK_OWNER, getIntentHookMethodName(argTypes.length, name), |
| 55 | + refactorHookBeforeMethodDescriptor(descriptor), false); |
| 56 | + positionList.forEach(this::loadLocal); |
| 57 | + super.visitMethodInsn(opcodeAndSource, owner, name, descriptor, isInterface); |
| 58 | + mv.visitInsn(Opcodes.DUP); |
| 59 | + positionList.forEach(this::loadLocal); |
| 60 | + mv.visitMethodInsn(Opcodes.INVOKESTATIC, SensorsPushInjected.PUSH_TRACK_OWNER, getPendingIntentHookMethodName(argTypes.length, name), |
| 61 | + refactorHookAfterMethodDescriptor(descriptor), false); |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + //NotificationManager.notify() |
| 66 | + if (opcodeAndSource == Opcodes.INVOKEVIRTUAL && "android/app/NotificationManager".equals(owner) && "notify".equals(name)) { |
| 67 | + Type[] argTypes = Type.getArgumentTypes(descriptor); |
| 68 | + List<Integer> positionList = new ArrayList<>(); |
| 69 | + for (int index = argTypes.length - 1; index >= 0; index--) { |
| 70 | + int position = newLocal(argTypes[index]); |
| 71 | + storeLocal(position, argTypes[index]); |
| 72 | + positionList.add(0, position); |
| 73 | + } |
| 74 | + mv.visitInsn(Opcodes.DUP); |
| 75 | + positionList.forEach(this::loadLocal); |
| 76 | + super.visitMethodInsn(opcodeAndSource, owner, name, descriptor, isInterface); |
| 77 | + positionList.forEach(this::loadLocal); |
| 78 | + mv.visitMethodInsn(Opcodes.INVOKESTATIC, SensorsPushInjected.PUSH_TRACK_OWNER, "onNotify", |
| 79 | + SensorsAnalyticsUtil.appendDescBeforeGiven(descriptor, "Landroid/app/NotificationManager;"), false); |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + super.visitMethodInsn(opcodeAndSource, owner, name, descriptor, isInterface); |
| 84 | + } |
| 85 | + |
| 86 | + private boolean checkPendingIntentName(String methodName) { |
| 87 | + return "getActivity".equals(methodName) || "getService".equals(methodName) || "getBroadcast".equals(methodName) || "getForegroundService".equals(methodName); |
| 88 | + } |
| 89 | + |
| 90 | + private String refactorHookBeforeMethodDescriptor(String desc) { |
| 91 | + //change return type to void |
| 92 | + return desc.substring(0, desc.lastIndexOf(")") + 1) + "V"; |
| 93 | + } |
| 94 | + |
| 95 | + private String refactorHookAfterMethodDescriptor(String desc) { |
| 96 | + //change return type to void and add PendingIntent prefix |
| 97 | + return "(Landroid/app/PendingIntent;" + desc.substring(1, desc.lastIndexOf(")") + 1) + "V"; |
| 98 | + } |
| 99 | + |
| 100 | + private String getIntentHookMethodName(int argsLength, String name) { |
| 101 | + if ("getActivity".equals(name)) { |
| 102 | + return argsLength == 4 ? "hookIntentGetActivity" : "hookIntentGetActivityBundle"; |
| 103 | + } |
| 104 | + return String.format("hookIntent%s", firstLetterUpper(name)); |
| 105 | + } |
| 106 | + |
| 107 | + private String getPendingIntentHookMethodName(int argsLength, String name) { |
| 108 | + if ("getActivity".equals(name)) { |
| 109 | + return argsLength == 4 ? "hookPendingIntentGetActivity" : "hookPendingIntentGetActivityBundle"; |
| 110 | + } |
| 111 | + return String.format("hookPendingIntent%s", firstLetterUpper(name)); |
| 112 | + } |
| 113 | + |
| 114 | + private static String firstLetterUpper(String name) { |
| 115 | + char[] cs = name.toCharArray(); |
| 116 | + cs[0] -= 32; |
| 117 | + return String.valueOf(cs); |
| 118 | + } |
| 119 | +} |
0 commit comments