Skip to content
This repository has been archived by the owner on Sep 15, 2024. It is now read-only.

Commit

Permalink
feat: migrate TbDialog to native
Browse files Browse the repository at this point in the history
  • Loading branch information
mkx173 committed Apr 8, 2024
1 parent 2648403 commit abd5040
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 255 deletions.
4 changes: 2 additions & 2 deletions app/src/main/java/gm/tieba/tabswitch/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ object Constants {
" ②本模块不会主动发起网络请求,不会上传任何用户数据,隐私泄露或者账号异常行为与本模块无关。\n" +
" ③本模块主要用于学习和交流技术,任何人不得将本模块用于商业或非法用途。",
"dev_tip" to "提示:您当前安装的是非正式版本,可能含有较多错误,如果您希望得到更稳定的使用体验,建议您安装正式版本。",
"exception_rules_incomplete" to "规则异常,请点击确定并重启应用以重新执行反混淆。若执行反混淆后仍出现此对话框则应尝试更新模块或向作者反馈。",
"version_mismatch" to "规则异常,当前贴吧版本不受支持。请使用受支持的贴吧版本(%s-%s)或尝试更新模块。",
"exception_rules_incomplete" to "请点击确定并重启应用以重新执行反混淆。若执行反混淆后仍出现此对话框则应尝试更新模块或向作者反馈。",
"version_mismatch" to "当前贴吧版本不受支持。请使用受支持的贴吧版本(%s-%s)或尝试更新模块。",
"exception_init_preference" to "初始化设置失败,请尝试更换贴吧版本。",
"regex_hint" to "请输入正则表达式,如.*",
)
Expand Down
10 changes: 2 additions & 8 deletions app/src/main/java/gm/tieba/tabswitch/XposedInit.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.WindowManager;

import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -60,7 +59,6 @@
import gm.tieba.tabswitch.hooker.extra.Hide;
import gm.tieba.tabswitch.hooker.extra.StackTrace;
import gm.tieba.tabswitch.util.DisplayUtils;
import gm.tieba.tabswitch.widget.TbDialog;
import gm.tieba.tabswitch.widget.TbToast;

public class XposedInit extends XposedContext implements IXposedHookZygoteInit, IXposedHookLoadPackage {
Expand Down Expand Up @@ -160,7 +158,6 @@ protected void afterHookedMethod(MethodHookParam param) throws Throwable {
new SelectClipboard()
);
final var matchers = new ArrayList<Obfuscated>(hookers.size() + 2);
matchers.add(new TbDialog());
matchers.add(new TbToast());
for (final var hooker : hookers) {
if (hooker instanceof Obfuscated) {
Expand Down Expand Up @@ -241,7 +238,7 @@ protected void beforeHookedMethod(final MethodHookParam param) throws Throwable
XposedBridge.log(message);
AlertDialog alert = new AlertDialog.Builder(activity, DisplayUtils.isLightMode(getContext()) ?
android.R.style.Theme_DeviceDefault_Light_Dialog_Alert : android.R.style.Theme_DeviceDefault_Dialog_Alert)
.setTitle("警告").setMessage(message).setCancelable(false)
.setTitle("规则异常").setMessage(message).setCancelable(false)
.setNeutralButton("更新模块", (dialogInterface, i) -> {
final Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
Expand All @@ -254,10 +251,7 @@ protected void beforeHookedMethod(final MethodHookParam param) throws Throwable
DeobfuscationHelper.saveAndRestart(activity, "unknown", null);
}).create();
alert.show();
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
layoutParams.copyFrom(alert.getWindow().getAttributes());
layoutParams.width = DisplayUtils.getDisplayWidth(getContext());
alert.getWindow().setAttributes(layoutParams);
DisplayUtils.fixAlertDialogWidth(alert);
}
});
return;
Expand Down
59 changes: 29 additions & 30 deletions app/src/main/java/gm/tieba/tabswitch/hooker/TSPreference.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
Expand All @@ -40,7 +39,6 @@
import gm.tieba.tabswitch.hooker.extra.TraceChecker;
import gm.tieba.tabswitch.util.DisplayUtils;
import gm.tieba.tabswitch.widget.NavigationBar;
import gm.tieba.tabswitch.widget.TbDialog;
import gm.tieba.tabswitch.widget.TbToast;

public class TSPreference extends XposedContext implements IHooker, Obfuscated {
Expand Down Expand Up @@ -115,15 +113,18 @@ protected void afterHookedMethod(final MethodHookParam param) throws Throwable {
} catch (final Throwable tr) {
final var messages = new ArrayList<String>();
messages.add(Constants.getStrings().get("exception_init_preference"));
messages.add(String.format(Locale.CHINA, "tbversion: %s, module version: %d",
messages.add(String.format(Locale.CHINA, "贴吧版本:%s, 模块版本:%d",
DeobfuscationHelper.getTbVersion(getContext()), BuildConfig.VERSION_CODE));
messages.add(Log.getStackTraceString(tr));
final var message = TextUtils.join("\n", messages);
XposedBridge.log(message);
new AlertDialog.Builder(activity, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT)
.setTitle("警告").setMessage(message).setCancelable(false)
AlertDialog alert = new AlertDialog.Builder(activity, DisplayUtils.isLightMode(getContext()) ?
android.R.style.Theme_DeviceDefault_Light_Dialog_Alert : android.R.style.Theme_DeviceDefault_Dialog_Alert)
.setTitle("规则异常").setMessage(message).setCancelable(false)
.setPositiveButton(activity.getString(android.R.string.ok), (dialogInterface, i) -> activity.finish())
.show();
.create();
alert.show();
DisplayUtils.fixAlertDialogWidth(alert);
}
}
});
Expand Down Expand Up @@ -154,21 +155,17 @@ private void startRootPreferenceActivity(final Activity activity) {
if (BuildConfig.VERSION_NAME.contains("alpha") || BuildConfig.VERSION_NAME.contains("beta")) {
stringBuilder.append("\n\n").append(Constants.getStrings().get("dev_tip"));
}
final TbDialog bdAlert = new TbDialog(activity, "使用协议", stringBuilder.toString(), true, null);
bdAlert.setOnNoButtonClickListener(v -> {
final Intent intent = new Intent();
intent.setAction(Intent.ACTION_DELETE);
intent.setData(Uri.parse("package:" + (sPath.contains(BuildConfig.APPLICATION_ID)
&& new File(sPath).exists() ?
BuildConfig.APPLICATION_ID : activity.getPackageName())));
activity.startActivity(intent);
});
bdAlert.setOnYesButtonClickListener(v -> {
Preferences.putEULAAccepted();
startRootPreferenceActivity(activity);
bdAlert.dismiss();
});
bdAlert.show();
AlertDialog alert = new AlertDialog.Builder(activity, DisplayUtils.isLightMode(getContext()) ?
android.R.style.Theme_DeviceDefault_Light_Dialog_Alert : android.R.style.Theme_DeviceDefault_Dialog_Alert)
.setTitle("使用协议").setMessage(stringBuilder.toString())
.setNegativeButton(activity.getString(android.R.string.cancel), null)
.setPositiveButton(activity.getString(android.R.string.ok), (dialogInterface, i) -> {
Preferences.putEULAAccepted();
startRootPreferenceActivity(activity);
})
.create();
alert.show();
DisplayUtils.fixAlertDialogWidth(alert);
} else {
final Intent intent = new Intent().setClassName(activity, PROXY_ACTIVITY);
intent.putExtra("proxyPage", MAIN);
Expand Down Expand Up @@ -209,15 +206,17 @@ private LinearLayout createRootPreference(final Activity activity) {
final SwitchButtonHolder autoSign = new SwitchButtonHolder(activity, "自动签到", "auto_sign", SwitchButtonHolder.TYPE_SWITCH);
autoSign.setOnButtonClickListener(v -> {
if (!Preferences.getIsAutoSignEnabled()) {
final TbDialog bdalert = new TbDialog(activity, "提示",
"这是一个需要网络请求并且有封号风险的功能,您需要自行承担使用此功能的风险,请谨慎使用!", true, null);
bdalert.setOnNoButtonClickListener(v2 -> bdalert.dismiss());
bdalert.setOnYesButtonClickListener(v2 -> {
Preferences.putAutoSignEnabled();
autoSign.bdSwitch.turnOn();
bdalert.dismiss();
});
bdalert.show();
AlertDialog alert = new AlertDialog.Builder(activity, DisplayUtils.isLightMode(getContext()) ?
android.R.style.Theme_DeviceDefault_Light_Dialog_Alert : android.R.style.Theme_DeviceDefault_Dialog_Alert)
.setTitle("提示").setMessage("这是一个需要网络请求并且有封号风险的功能,您需要自行承担使用此功能的风险,请谨慎使用!")
.setNegativeButton(activity.getString(android.R.string.cancel), null)
.setPositiveButton(activity.getString(android.R.string.ok), (dialogInterface, i) -> {
Preferences.putAutoSignEnabled();
autoSign.bdSwitch.turnOn();
})
.create();
alert.show();
DisplayUtils.fixAlertDialogWidth(alert);
} else {
autoSign.bdSwitch.changeState();
}
Expand Down
80 changes: 57 additions & 23 deletions app/src/main/java/gm/tieba/tabswitch/hooker/TSPreferenceHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Color;
import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
Expand All @@ -32,10 +36,9 @@
import gm.tieba.tabswitch.Constants;
import gm.tieba.tabswitch.XposedContext;
import gm.tieba.tabswitch.dao.Preferences;
import gm.tieba.tabswitch.util.DisplayUtils;
import gm.tieba.tabswitch.util.ReflectUtils;
import gm.tieba.tabswitch.widget.Switch;
import gm.tieba.tabswitch.widget.TbDialog;
import gm.tieba.tabswitch.widget.TbEditText;
import gm.tieba.tabswitch.widget.TbToast;

public class TSPreferenceHelper extends XposedContext {
Expand Down Expand Up @@ -190,7 +193,7 @@ static class SwitchButtonHolder {
else bdSwitch.turnOff();
break;
case TYPE_DIALOG:
switchButton = createButton(text, null, false, v -> showRegexDialog(activity));
switchButton = createButton(text, null, false, v -> showRegexDialog(activity, text));
bdSwitchView.setOnTouchListener((v, event) -> false);
if (Preferences.getString(key) != null) bdSwitch.turnOn();
else bdSwitch.turnOff();
Expand All @@ -207,34 +210,65 @@ void setOnButtonClickListener(final View.OnClickListener l) {
});
}

private void showRegexDialog(final Activity activity) {
final EditText editText = new TbEditText(getContext());
private void showRegexDialog(final Activity activity, final String title) {
Activity currentActivity = ReflectUtils.getCurrentActivity();
boolean isLightMode = DisplayUtils.isLightMode(getContext());

final EditText editText = new EditText(currentActivity);
editText.setHint(Constants.getStrings().get("regex_hint"));
editText.setText(Preferences.getString(mKey));
final TbDialog bdAlert = new TbDialog(activity, null, null, true, editText);
bdAlert.setOnNoButtonClickListener(v -> bdAlert.dismiss());
bdAlert.setOnYesButtonClickListener(v -> {
try {
if (TextUtils.isEmpty(editText.getText())) {
Preferences.remove(mKey);
bdSwitch.turnOff();
} else {
Pattern.compile(editText.getText().toString());
Preferences.putString(mKey, editText.getText().toString());
bdSwitch.turnOn();
if (!isLightMode) {
editText.setTextColor(Color.WHITE);
editText.setHintTextColor(Color.GRAY);
}
editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE);

final LinearLayout linearLayout = new LinearLayout(currentActivity);
linearLayout.setGravity(Gravity.CENTER_HORIZONTAL);

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT
);
layoutParams.leftMargin = DisplayUtils.dipToPx(currentActivity, 20F);
layoutParams.rightMargin = DisplayUtils.dipToPx(currentActivity, 20F);
editText.setLayoutParams(layoutParams);

linearLayout.addView(editText);

AlertDialog alert = new AlertDialog.Builder(currentActivity, isLightMode ?
android.R.style.Theme_DeviceDefault_Light_Dialog_Alert : android.R.style.Theme_DeviceDefault_Dialog_Alert)
.setTitle(title).setView(linearLayout)
.setNegativeButton(activity.getString(android.R.string.cancel), null)
.setPositiveButton(activity.getString(android.R.string.ok), null).create();

alert.setOnShowListener(dialogInterface -> {
Button button = alert.getButton(AlertDialog.BUTTON_POSITIVE);
button.setOnClickListener(view -> {
try {
if (TextUtils.isEmpty(editText.getText())) {
Preferences.remove(mKey);
bdSwitch.turnOff();
} else {
Pattern.compile(editText.getText().toString());
Preferences.putString(mKey, editText.getText().toString());
bdSwitch.turnOn();
}
alert.dismiss();
} catch (final PatternSyntaxException e) {
TbToast.showTbToast(e.getMessage(), TbToast.LENGTH_SHORT);
}
bdAlert.dismiss();
} catch (final PatternSyntaxException e) {
TbToast.showTbToast(e.getMessage(), TbToast.LENGTH_SHORT);
}
});
});
bdAlert.show();
bdAlert.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

alert.show();
DisplayUtils.fixAlertDialogWidth(alert);

alert.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
editText.setOnEditorActionListener((v, actionId, event) -> {
if (actionId == EditorInfo.IME_ACTION_DONE || event != null
&& event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
bdAlert.findYesButton().performClick();
alert.getButton(AlertDialog.BUTTON_POSITIVE).performClick();
return true;
}
return false;
Expand Down
Loading

0 comments on commit abd5040

Please sign in to comment.