Skip to content

Commit d43d304

Browse files
committed
修复应用状态控制 & 修复重复的阿巴阿巴
1 parent 4899d91 commit d43d304

2 files changed

Lines changed: 107 additions & 94 deletions

File tree

app/src/main/java/com/sevtinge/hyperceiler/module/hook/securitycenter/app/AppDisable.java

Lines changed: 97 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
/*
2-
* This file is part of HyperCeiler.
2+
* This file is part of HyperCeiler.
33
4-
* HyperCeiler is free software: you can redistribute it and/or modify
5-
* it under the terms of the GNU Affero General Public License as
6-
* published by the Free Software Foundation, either version 3 of the
7-
* License.
4+
* HyperCeiler is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Affero General Public License as
6+
* published by the Free Software Foundation, either version 3 of the
7+
* License.
88
9-
* This program is distributed in the hope that it will be useful,
10-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12-
* GNU Affero General Public License for more details.
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Affero General Public License for more details.
1313
14-
* You should have received a copy of the GNU Affero General Public License
15-
* along with this program. If not, see <https://www.gnu.org/licenses/>.
14+
* You should have received a copy of the GNU Affero General Public License
15+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
1616
17-
* Copyright (C) 2023-2024 HyperCeiler Contributions
18-
*/
17+
* Copyright (C) 2023-2024 HyperCeiler Contributions
18+
*/
1919
package com.sevtinge.hyperceiler.module.hook.securitycenter.app;
2020

2121
import android.annotation.SuppressLint;
2222
import android.app.Activity;
23+
import android.content.DialogInterface;
2324
import android.content.pm.ApplicationInfo;
2425
import android.content.pm.PackageInfo;
2526
import android.content.pm.PackageManager;
@@ -41,92 +42,101 @@
4142
@SuppressLint("DiscouragedApi")
4243
public class AppDisable extends BaseHook {
4344

44-
public ArrayList<String> mMiuiCoreApps;
45+
public ArrayList<String> mMiuiCoreApps = new ArrayList<>();
4546

4647
@Override
4748
public void init() {
48-
49-
findAndHookMethod("com.miui.appmanager.ApplicationsDetailsActivity", "onCreateOptionsMenu", Menu.class, new MethodHook() {
50-
@Override
51-
protected void after(MethodHookParam param) throws Throwable {
52-
Activity act = (Activity) param.thisObject;
53-
Menu menu = (Menu) param.args[0];
54-
MenuItem dis = menu.add(0, 666, 1, act.getResources().getIdentifier("app_manager_disable_text", "string", lpparam.packageName));
55-
dis.setIcon(act.getResources().getIdentifier("action_button_stop_svg", "drawable", lpparam.packageName));
56-
dis.setEnabled(true);
57-
dis.setShowAsAction(1);
58-
// XposedHelpers.setAdditionalInstanceField(param.thisObject, "mDisableButton", dis);
59-
60-
PackageManager pm = act.getPackageManager();
61-
Field piField = XposedHelpers.findFirstFieldByExactType(act.getClass(), PackageInfo.class);
62-
PackageInfo mPackageInfo = (PackageInfo) piField.get(act);
63-
ApplicationInfo appInfo = pm.getApplicationInfo(mPackageInfo.packageName, PackageManager.GET_META_DATA);
64-
boolean isSystem = (appInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
65-
boolean isUpdatedSystem = (appInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0;
66-
67-
dis.setTitle(act.getResources().getIdentifier(appInfo.enabled ? "app_manager_disable_text" : "app_manager_enable_text", "string", lpparam.packageName));
68-
69-
mMiuiCoreApps = new ArrayList<>(Arrays.asList(act.getResources().getStringArray(R.array.miui_core_app_package_name)));
70-
71-
if (mMiuiCoreApps.contains(mPackageInfo.packageName)) {
72-
dis.setEnabled(false);
73-
}
74-
75-
if (!appInfo.enabled || (isSystem && !isUpdatedSystem)) {
76-
MenuItem item = menu.findItem(2);
77-
if (item != null) item.setVisible(false);
78-
}
79-
}
80-
});
81-
82-
findAndHookMethod("com.miui.appmanager.ApplicationsDetailsActivity", "onOptionsItemSelected", MenuItem.class, new MethodHook() {
83-
@Override
84-
protected void after(MethodHookParam param) throws Throwable {
85-
MenuItem item = (MenuItem) param.args[0];
86-
87-
if (item != null && item.getItemId() == 666) {
88-
Activity act = (Activity) param.thisObject;
89-
Resources modRes = getModuleRes(act);
90-
Field piField = XposedHelpers.findFirstFieldByExactType(act.getClass(), PackageInfo.class);
91-
PackageInfo mPackageInfo = (PackageInfo) piField.get(act);
92-
93-
if (mMiuiCoreApps.contains(mPackageInfo.packageName)) {
94-
Toast.makeText(act, modRes.getString(R.string.disable_app_settings), Toast.LENGTH_SHORT).show();
95-
return;
49+
findAndHookMethod("com.miui.appmanager.ApplicationsDetailsActivity",
50+
"onCreateOptionsMenu", Menu.class,
51+
new MethodHook() {
52+
@Override
53+
protected void after(MethodHookParam param) throws Throwable {
54+
Activity act = (Activity) param.thisObject;
55+
Menu menu = (Menu) param.args[0];
56+
MenuItem dis = menu.add(0, 666, 1,
57+
act.getResources().getIdentifier("app_manager_disable_text", "string", lpparam.packageName));
58+
dis.setIcon(act.getResources().getIdentifier("action_button_stop_svg", "drawable", lpparam.packageName));
59+
dis.setEnabled(true);
60+
dis.setShowAsAction(1);
61+
// XposedHelpers.setAdditionalInstanceField(param.thisObject, "mDisableButton", dis);
62+
PackageManager pm = act.getPackageManager();
63+
Field piField = XposedHelpers.findFirstFieldByExactType(act.getClass(), PackageInfo.class);
64+
PackageInfo mPackageInfo = (PackageInfo) piField.get(act);
65+
ApplicationInfo appInfo = pm.getApplicationInfo(mPackageInfo.packageName, PackageManager.GET_META_DATA);
66+
boolean isSystem = (appInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
67+
boolean isUpdatedSystem = (appInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0;
68+
dis.setTitle(act.getResources().getIdentifier(appInfo.enabled ? "app_manager_disable_text" : "app_manager_enable_text", "string", lpparam.packageName));
69+
getModuleRes(act);
70+
mMiuiCoreApps = new ArrayList<>(Arrays.asList(act.getResources().getStringArray(R.array.miui_core_app_package_name)));
71+
if (mMiuiCoreApps.contains(mPackageInfo.packageName)) {
72+
dis.setEnabled(false);
73+
}
74+
if (!appInfo.enabled || (isSystem && !isUpdatedSystem)) {
75+
MenuItem item = menu.findItem(2);
76+
if (item != null) item.setVisible(false);
77+
}
9678
}
97-
98-
PackageManager pm = act.getPackageManager();
99-
ApplicationInfo appInfo = pm.getApplicationInfo(mPackageInfo.packageName, PackageManager.GET_META_DATA);
100-
boolean isSystem = (appInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
101-
int state = pm.getApplicationEnabledSetting(mPackageInfo.packageName);
102-
boolean isEnabledOrDefault = (state == PackageManager.COMPONENT_ENABLED_STATE_ENABLED || state == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
103-
if (isEnabledOrDefault) {
104-
if (isSystem) {
105-
/* String title = modRes.getString(R.string.disable_app_title);
106-
String text = modRes.getString(R.string.disable_app_text);
107-
new AlertDialog.Builder(act)
108-
.setTitle(title)
109-
.setMessage(text)
110-
.setPositiveButton(android.R.string.ok, (dialog, which) -> setAppState(act, mPackageInfo.packageName, item, false))
111-
.setNegativeButton(android.R.string.cancel, null)
112-
.show();*/
113-
setAppState(act, mPackageInfo.packageName, item, false);
114-
} else {
115-
setAppState(act, mPackageInfo.packageName, item, false);
79+
}
80+
);
81+
82+
findAndHookMethod("com.miui.appmanager.ApplicationsDetailsActivity",
83+
"onOptionsItemSelected", MenuItem.class,
84+
new MethodHook() {
85+
@Override
86+
protected void after(MethodHookParam param) throws Throwable {
87+
MenuItem item = (MenuItem) param.args[0];
88+
if (item != null && item.getItemId() == 666) {
89+
Activity act = (Activity) param.thisObject;
90+
Resources modRes = getModuleRes(act);
91+
Field piField = XposedHelpers.findFirstFieldByExactType(act.getClass(), PackageInfo.class);
92+
93+
PackageInfo mPackageInfo = (PackageInfo) piField.get(act);
94+
if (mMiuiCoreApps.contains(mPackageInfo.packageName)) {
95+
Toast.makeText(act, modRes.getString(R.string.disable_app_settings), Toast.LENGTH_SHORT).show();
96+
return;
97+
}
98+
99+
PackageManager pm = act.getPackageManager();
100+
ApplicationInfo appInfo = pm.getApplicationInfo(mPackageInfo.packageName, PackageManager.GET_META_DATA);
101+
boolean isSystem = (appInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
102+
int state = pm.getApplicationEnabledSetting(mPackageInfo.packageName);
103+
boolean isEnabledOrDefault = (state == PackageManager.COMPONENT_ENABLED_STATE_ENABLED || state == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
104+
if (isEnabledOrDefault) {
105+
if (isSystem) {
106+
String title = modRes.getString(R.string.disable_app_title);
107+
String text = modRes.getString(R.string.disable_app_text);
108+
DialogInterface.OnClickListener onClickListener = new DialogInterface.OnClickListener() {
109+
@Override
110+
public void onClick(DialogInterface dialog, int which) {
111+
setAppState(act, mPackageInfo.packageName, item, false);
112+
}
113+
};
114+
Class<?> miuiDialog = findClassIfExists("miuix.appcompat.app.AlertDialog");
115+
Class<?> builder = findClassIfExists("miuix.appcompat.app.AlertDialog$Builder");
116+
Object newBuilder = XposedHelpers.newInstance(builder, act);
117+
XposedHelpers.callMethod(XposedHelpers.callMethod(XposedHelpers.callMethod(XposedHelpers.callMethod(
118+
XposedHelpers.callMethod(XposedHelpers.callMethod(newBuilder, "setTitle", title),
119+
"setMessage", text), "setPositiveButton", android.R.string.ok, onClickListener),
120+
"setNegativeButton", android.R.string.cancel, null), "create"), "show");
121+
// setAppState(act, mPackageInfo.packageName, item, false);
122+
} else {
123+
setAppState(act, mPackageInfo.packageName, item, false);
124+
}
125+
} else {
126+
setAppState(act, mPackageInfo.packageName, item, true);
127+
}
128+
param.setResult(true);
116129
}
117-
} else {
118-
setAppState(act, mPackageInfo.packageName, item, true);
119130
}
120-
param.setResult(true);
121131
}
122-
}
123-
});
132+
);
124133
}
125134

126135
private void setAppState(final Activity act, String pkgName, MenuItem item, boolean enable) {
127136
try {
128137
PackageManager pm = act.getPackageManager();
129-
pm.setApplicationEnabledSetting(pkgName, enable ? PackageManager.COMPONENT_ENABLED_STATE_DEFAULT : PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 0);
138+
pm.setApplicationEnabledSetting(pkgName, enable ? PackageManager.COMPONENT_ENABLED_STATE_DEFAULT :
139+
PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 0);
130140
int state = pm.getApplicationEnabledSetting(pkgName);
131141
boolean isEnabledOrDefault = (state == PackageManager.COMPONENT_ENABLED_STATE_ENABLED || state == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
132142
if ((enable && isEnabledOrDefault) || (!enable && !isEnabledOrDefault)) {

app/src/main/java/com/sevtinge/hyperceiler/ui/MainActivity.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import com.sevtinge.hyperceiler.R;
2929
import com.sevtinge.hyperceiler.callback.IResult;
30+
import com.sevtinge.hyperceiler.prefs.PreferenceHeader;
3031
import com.sevtinge.hyperceiler.ui.base.NavigationActivity;
3132
import com.sevtinge.hyperceiler.utils.BackupUtils;
3233
import com.sevtinge.hyperceiler.utils.Helpers;
@@ -53,7 +54,7 @@ public void onCreate(Bundle savedInstanceState) {
5354
Helpers.checkXposedActivateState(this);
5455
ShellInit.init(this);
5556
PropUtils.setProp("persist.hyperceiler.log.level",
56-
(ProjectApi.isRelease() ? def : ProjectApi.isCanary() ? (def == 0 ? 3 : 4) : def));
57+
(ProjectApi.isRelease() ? def : ProjectApi.isCanary() ? (def == 0 ? 3 : 4) : def));
5758
// test();
5859
}
5960

@@ -63,12 +64,12 @@ public void error(String reason) {
6364
@Override
6465
public void run() {
6566
new AlertDialog.Builder(context)
66-
.setCancelable(false)
67-
.setTitle(getResources().getString(R.string.tip))
68-
.setMessage(getResources().getString(R.string.root))
69-
.setHapticFeedbackEnabled(true)
70-
.setPositiveButton(android.R.string.ok, null)
71-
.show();
67+
.setCancelable(false)
68+
.setTitle(getResources().getString(R.string.tip))
69+
.setMessage(getResources().getString(R.string.root))
70+
.setHapticFeedbackEnabled(true)
71+
.setPositiveButton(android.R.string.ok, null)
72+
.show();
7273
}
7374
});
7475
}
@@ -77,6 +78,8 @@ public void run() {
7778
protected void onDestroy() {
7879
ShellInit.destroy();
7980
ThreadPoolManager.shutdown();
81+
PreferenceHeader.mUninstallApp.clear();
82+
PreferenceHeader.mDisableOrHiddenApp.clear();
8083
super.onDestroy();
8184
}
8285

0 commit comments

Comments
 (0)