Skip to content

Commit

Permalink
Merge pull request #145 from frknkrc44/feat-21-xx-support
Browse files Browse the repository at this point in the history
feat: Add initial 21.xx support for WA + WAB and fix tablet UI crashes
  • Loading branch information
Dev4Mod authored Oct 19, 2024
2 parents 2d46a3e + d3c3b8b commit 3d5f3af
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,11 @@ public static Activity getCurrentConversation() {
if (mCurrentActivity == null) return null;
Class<?> conversation = XposedHelpers.findClass("com.whatsapp.Conversation", mCurrentActivity.getClassLoader());
if (conversation.isInstance(mCurrentActivity)) return mCurrentActivity;

// for tablet UI, they're using HomeActivity instead of Conversation
// TODO: Add more checks for ConversationFragment
Class<?> home = XposedHelpers.findClass("com.whatsapp.HomeActivity", mCurrentActivity.getClassLoader());
if (mCurrentActivity.getResources().getConfiguration().smallestScreenWidthDp >= 600 && home.isInstance(mCurrentActivity)) return mCurrentActivity;
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -537,10 +537,13 @@ public synchronized static Method loadMenuStatusMethod(ClassLoader loader) throw
public synchronized static Field loadStatusDownloadFileField(ClassLoader classLoader) throws Exception {
return UnobfuscatorCache.getInstance().getField(classLoader, () -> {
var clazz = loadStatusDownloadMediaClass(classLoader);
var clazz2 = clazz.getField("A01").getType();
var field = ReflectionUtils.getFieldByType(clazz2, File.class);
if (field == null) throw new Exception("StatusDownloadFile field not found");
return field;
for (Field clazzField : clazz.getFields()) {
var clazz2 = clazzField.getType();
var field = ReflectionUtils.getFieldByType(clazz2, File.class);
if (field != null) return field;
}

throw new Exception("StatusDownloadFile field not found");
});
}

Expand Down Expand Up @@ -1073,9 +1076,15 @@ public synchronized static Method loadGetEditMessageMethod(ClassLoader loader) t
if (methodData == null) throw new RuntimeException("GetEditMessage method not found");
var invokes = methodData.getInvokes();
for (var invoke : invokes) {
// pre 21.xx method
if (invoke.getParamTypes().isEmpty() && Objects.equals(invoke.getDeclaredClass(), methodData.getParamTypes().get(0))) {
return invoke.getMethodInstance(loader);
}

// 21.xx+ method (static)
if (Modifier.isStatic(invoke.getMethodInstance(loader).getModifiers()) && Objects.equals(invoke.getParamTypes().get(0), methodData.getParamTypes().get(0))) {
return invoke.getMethodInstance(loader);
}
}
throw new RuntimeException("GetEditMessage method not found");
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.wmods.wppenhacer.xposed.utils.ResId;
import com.wmods.wppenhacer.xposed.utils.Utils;

import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Objects;

Expand Down Expand Up @@ -65,7 +66,10 @@ public void doHook() throws Throwable {
XposedBridge.hookMethod(onMessageEdit, new XC_MethodHook() {
@Override
protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
var editMessage = getEditMessage.invoke(param.args[0]);
// for 21.xx, getEditMessage is static
var editMessage = Modifier.isStatic(getEditMessage.getModifiers())
? getEditMessage.invoke(null, param.args[0])
: getEditMessage.invoke(param.args[0]);
if (editMessage == null) return;
long timestamp = XposedHelpers.getLongField(editMessage, "A00");
var fMessage = new FMessageWpp(param.args[0]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,13 @@ private void sharedStatus(FMessageWpp fMessageWpp) {

private void downloadFile(FMessageWpp fMessage) {
try {
var fileData = XposedHelpers.getObjectField(fMessage.getObject(), "A01");
if (!fieldFile.getDeclaringClass().isInstance(fileData)) {
Field fieldFileInMessage = ReflectionUtils.getFieldByType(fMessage.getObject().getClass(), fieldFile.getDeclaringClass());
if (fieldFileInMessage == null) {
Utils.showToast(Utils.getApplication().getString(ResId.string.msg_text_status_not_downloadable), Toast.LENGTH_SHORT);
return;
}

var fileData = XposedHelpers.getObjectField(fMessage.getObject(), fieldFileInMessage.getName());
var file = (File) ReflectionUtils.getField(fieldFile, fileData);
var userJid = fMessage.getUserJid();
var fileType = file.getName().substring(file.getName().lastIndexOf(".") + 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.CompletableFuture;

import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XC_MethodReplacement;
import de.robv.android.xposed.XSharedPreferences;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;
Expand Down Expand Up @@ -46,18 +49,43 @@ protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
}
});

XposedHelpers.findAndHookMethod("com.whatsapp.messagetranslation.UnityMessageTranslation", classLoader, "translate", classLoader.loadClass("java.lang.String"),
new XC_MethodHook() {
@Override
protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
var texto = (String) param.args[0];
var currentMethod = (Method) param.method;
var unityTranslationResultClass = currentMethod.getReturnType();
var translation = translateGoogle(texto, Locale.getDefault().getLanguage()).get();
var unityTranslationResult = unityTranslationResultClass.getConstructor(String.class, float.class, int.class).newInstance(translation, 1, 0);
param.setResult(unityTranslationResult);
Class<?> translatorClazz = XposedHelpers.findClass("com.whatsapp.messagetranslation.UnityMessageTranslation", classLoader);

var pre21Method = XposedHelpers.findMethodExactIfExists(translatorClazz, "translate", String.class);
if (pre21Method != null) {
XposedHelpers.findAndHookMethod(translatorClazz, pre21Method.getName(), String.class, new XC_MethodReplacement() {
@Override
protected Object replaceHookedMethod(MethodHookParam param) throws Throwable {
var texto = (String) param.args[0];
var currentMethod = (Method) param.method;
var unityTranslationResultClass = currentMethod.getReturnType();
var translation = translateGoogle(texto, Locale.getDefault().getLanguage()).get();
return unityTranslationResultClass.getConstructor(String.class, float.class, int.class).newInstance(translation, 1, 0);
}
});
}

var newMethod = XposedHelpers.findMethodExactIfExists(translatorClazz, "translate", List.class);
if (newMethod != null) {
XposedHelpers.findAndHookMethod(translatorClazz, "translate", List.class, new XC_MethodReplacement() {
@Override
protected Object replaceHookedMethod(MethodHookParam param) throws Throwable {
var list = (List) param.args[0];
var translated = new ArrayList<String>();

for (var texto : list) {
var translation = translateGoogle((String) texto, Locale.getDefault().getLanguage()).get();
translated.add(translation);
}
});

var currentMethod = (Method) param.method;
var unityTranslationResultClass = currentMethod.getReturnType();
return unityTranslationResultClass.getConstructor(String[].class, float.class, int.class).newInstance(translated.toArray(new String[0]), 1, 0);
}
});
}

if (pre21Method == null && newMethod == null) throw new Exception("GoogleTranslate method not found");
}

public CompletableFuture<String> translateGoogle(String text, String languageDest) {
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,15 @@
<item>2.24.18.xx</item>
<item>2.24.19.xx</item>
<item>2.24.20.xx</item>
<item>2.24.21.xx</item>
</string-array>
<string-array name="supported_versions_business">
<item>2.24.16.xx</item>
<item>2.24.17.xx</item>
<item>2.24.18.xx</item>
<item>2.24.19.xx</item>
<item>2.24.20.xx</item>
<item>2.24.21.xx</item>
</string-array>
<string-array name="image_picker">
<item>image/*</item>
Expand Down

0 comments on commit 3d5f3af

Please sign in to comment.