|
1 | 1 | /* |
2 | | - * This file is part of HyperCeiler. |
| 2 | + * This file is part of HyperCeiler. |
3 | 3 |
|
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. |
8 | 8 |
|
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. |
13 | 13 |
|
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/>. |
16 | 16 |
|
17 | | - * Copyright (C) 2023-2024 HyperCeiler Contributions |
18 | | -*/ |
| 17 | + * Copyright (C) 2023-2024 HyperCeiler Contributions |
| 18 | + */ |
19 | 19 | package com.sevtinge.hyperceiler.module.hook.securitycenter.app; |
20 | 20 |
|
21 | 21 | import android.annotation.SuppressLint; |
22 | 22 | import android.app.Activity; |
| 23 | +import android.content.DialogInterface; |
23 | 24 | import android.content.pm.ApplicationInfo; |
24 | 25 | import android.content.pm.PackageInfo; |
25 | 26 | import android.content.pm.PackageManager; |
|
41 | 42 | @SuppressLint("DiscouragedApi") |
42 | 43 | public class AppDisable extends BaseHook { |
43 | 44 |
|
44 | | - public ArrayList<String> mMiuiCoreApps; |
| 45 | + public ArrayList<String> mMiuiCoreApps = new ArrayList<>(); |
45 | 46 |
|
46 | 47 | @Override |
47 | 48 | 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 | + } |
96 | 78 | } |
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); |
116 | 129 | } |
117 | | - } else { |
118 | | - setAppState(act, mPackageInfo.packageName, item, true); |
119 | 130 | } |
120 | | - param.setResult(true); |
121 | 131 | } |
122 | | - } |
123 | | - }); |
| 132 | + ); |
124 | 133 | } |
125 | 134 |
|
126 | 135 | private void setAppState(final Activity act, String pkgName, MenuItem item, boolean enable) { |
127 | 136 | try { |
128 | 137 | 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); |
130 | 140 | int state = pm.getApplicationEnabledSetting(pkgName); |
131 | 141 | boolean isEnabledOrDefault = (state == PackageManager.COMPONENT_ENABLED_STATE_ENABLED || state == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT); |
132 | 142 | if ((enable && isEnabledOrDefault) || (!enable && !isEnabledOrDefault)) { |
|
0 commit comments