| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 
 | public class SmsHandlerHook implements IHook {
 private static final String TELEPHONY_PACKAGE = "com.android.internal.telephony";
 private static final String SMS_HANDLER_CLASS = TELEPHONY_PACKAGE + ".InboundSmsHandler";
 private static final String SMSCODE_PACKAGE = BuildConfig.APPLICATION_ID;
 
 private Context mModContext;
 
 @Override
 public void onLoadPackage(XC_LoadPackage.LoadPackageParam lpparam) throws Throwable {
 if ("com.android.phone".equals(lpparam.packageName)) {
 XLog.i("SmsCode initializing");
 printDeviceInfo();
 try {
 hookSmsHandler(lpparam);
 } catch (Throwable e) {
 XLog.e("Failed to hook SmsHandler", e);
 throw e;
 }
 XLog.i("SmsCode initialize completely");
 }
 }
 
 @SuppressWarnings("deprecation")
 private static void printDeviceInfo() {
 XLog.i("Phone manufacturer: %s", Build.MANUFACTURER);
 XLog.i("Phone model: %s", Build.MODEL);
 XLog.i("Android version: %s", Build.VERSION.RELEASE);
 int xposedVersion;
 try {
 xposedVersion = XposedBridge.getXposedVersion();
 } catch (Throwable e) {
 xposedVersion = XposedBridge.XPOSED_BRIDGE_VERSION;
 }
 XLog.i("Xposed bridge version: %d", xposedVersion);
 XLog.i("SmsCode version: %s (%d)", BuildConfig.VERSION_NAME, BuildConfig.VERSION_CODE);
 }
 
 private void hookSmsHandler(XC_LoadPackage.LoadPackageParam lpparam) {
 hookConstructor(lpparam);
 hookDispatchIntent(lpparam);
 }
 
 private void hookConstructor(XC_LoadPackage.LoadPackageParam lpparam) {
 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
 hookConstructor24(lpparam);
 } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
 hookConstructor19(lpparam);
 }
 }
 
 private void hookConstructor24(XC_LoadPackage.LoadPackageParam lpparam) {
 XLog.i("Hooking InboundSmsHandler constructor for android v24+");
 XposedHelpers.findAndHookConstructor(SMS_HANDLER_CLASS, lpparam.classLoader,
 String.class,
 Context.class,
 TELEPHONY_PACKAGE + ".SmsStorageMonitor",
 TELEPHONY_PACKAGE + ".Phone",
 TELEPHONY_PACKAGE + ".CellBroadcastHandler",
 new ConstructorHook());
 }
 
 private void hookConstructor19(XC_LoadPackage.LoadPackageParam lpparam) {
 XLog.i("Hooking InboundSmsHandler constructor for Android v19+");
 XposedHelpers.findAndHookConstructor(SMS_HANDLER_CLASS, lpparam.classLoader,
 String.class,
 Context.class,
 TELEPHONY_PACKAGE + ".SmsStorageMonitor",
 TELEPHONY_PACKAGE + ".PhoneBase",
 TELEPHONY_PACKAGE + ".CellBroadcastHandler",
 new ConstructorHook());
 }
 
 private void hookDispatchIntent(XC_LoadPackage.LoadPackageParam lpparam) {
 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
 hookDispatchIntent23(lpparam);
 } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
 hookDispatchIntent21(lpparam);
 } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
 hookDispatchIntent19(lpparam);
 }
 }
 
 private void hookDispatchIntent19(XC_LoadPackage.LoadPackageParam lpparam) {
 XLog.i("Hooking dispatchIntent() for Android v19+");
 XposedHelpers.findAndHookMethod(SMS_HANDLER_CLASS, lpparam.classLoader, "dispatchIntent",
 Intent.class,
 String.class,
 int.class,
 BroadcastReceiver.class,
 new DispatchIntentHook(3));
 }
 
 private void hookDispatchIntent21(XC_LoadPackage.LoadPackageParam lpparam) {
 XLog.i("Hooking dispatchIntent() for Android v21+");
 XposedHelpers.findAndHookMethod(SMS_HANDLER_CLASS, lpparam.classLoader, "dispatchIntent",
 Intent.class,
 String.class,
 int.class,
 BroadcastReceiver.class,
 UserHandle.class,
 new DispatchIntentHook(3));
 }
 
 private void hookDispatchIntent23(XC_LoadPackage.LoadPackageParam lpparam) {
 XLog.i("Hooking dispatchIntent() for Android v23+");
 XposedHelpers.findAndHookMethod(SMS_HANDLER_CLASS, lpparam.classLoader, "dispatchIntent",
 Intent.class,
 String.class,
 int.class,
 Bundle.class,
 BroadcastReceiver.class,
 UserHandle.class,
 new DispatchIntentHook(4));
 }
 
 private class ConstructorHook extends XC_MethodHook {
 @Override
 protected void afterHookedMethod(MethodHookParam param) throws Throwable {
 try {
 afterConstructorHandler(param);
 } catch (Throwable e) {
 XLog.e("Error occurred in constructor hook", e);
 throw e;
 }
 }
 }
 
 private void afterConstructorHandler(XC_MethodHook.MethodHookParam param) {
 Context context = (Context) param.args[1];
 if (mModContext == null) {
 mModContext = context;
 }
 }
 
 private class DispatchIntentHook extends XC_MethodHook {
 private final int mReceiverIndex;
 
 DispatchIntentHook(int receiverIndex) {
 mReceiverIndex = receiverIndex;
 }
 
 @Override
 protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
 try {
 beforeDispatchIntentHandler(param, mReceiverIndex);
 } catch (Throwable e) {
 XLog.e("Error occurred in dispatchIntent() hook, ", e);
 throw e;
 }
 }
 }
 
 private void beforeDispatchIntentHandler(XC_MethodHook.MethodHookParam param, int receiverIndex) {
 
 Intent intent = (Intent) param.args[0];
 String action = intent.getAction();
 
 
 if (!Telephony.Sms.Intents.SMS_DELIVER_ACTION.equals(action)) {
 return;
 }
 
 
 Intent broadcastIntent = new Intent();
 broadcastIntent.setComponent(new ComponentName(SMSCODE_PACKAGE, SmsCodeReceiver.class.getName()));
 broadcastIntent.putExtra(SmsCodeService.EXTRA_KEY_SMS_INTENT, intent);
 mModContext.sendBroadcast(broadcastIntent);
 }
 }
 
 |