Skip to content

Commit

Permalink
opt: app add CMD with root & Delete all DexKit cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Sevtinge committed Jul 17, 2024
1 parent ea23b47 commit 53a7a40
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@
*/
package com.sevtinge.hyperceiler.module.base.dexkit;

import static com.sevtinge.hyperceiler.utils.shell.ShellUtils.safeExecCommandWithRoot;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.sevtinge.hyperceiler.R;
import com.sevtinge.hyperceiler.utils.FileUtils;
import com.sevtinge.hyperceiler.utils.Helpers;
import com.sevtinge.hyperceiler.utils.log.XposedLogUtils;
Expand Down Expand Up @@ -566,4 +571,14 @@ public void close() {
hostDir = null;
isInit = false;
}

public static void deleteAllCache(Context context) {
String[] folderNames = context.getResources().getStringArray(R.array.xposed_scope);
for (String folderName : folderNames) {
String folderPath = "/data/data/" + folderName + "/cache";
if (safeExecCommandWithRoot("ls " + folderPath).contains("dexkit")) {
safeExecCommandWithRoot("rm -rf " + folderPath + "/dexkit");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,101 @@
*/
package com.sevtinge.hyperceiler.ui.fragment.base.settings.development;

import static com.sevtinge.hyperceiler.utils.shell.ShellUtils.safeExecCommandWithRoot;

import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.sevtinge.hyperceiler.R;
import com.sevtinge.hyperceiler.data.AppData;
import com.sevtinge.hyperceiler.module.base.dexkit.DexKit;
import com.sevtinge.hyperceiler.ui.fragment.base.SettingsPreferenceFragment;
import com.sevtinge.hyperceiler.utils.ContextUtils;
import com.sevtinge.hyperceiler.utils.DialogHelper;
import com.sevtinge.hyperceiler.utils.ThreadPoolManager;
import com.sevtinge.hyperceiler.utils.ToastHelper;
import com.sevtinge.hyperceiler.utils.shell.ShellInit;

import java.util.concurrent.ExecutorService;

import moralnorm.appcompat.app.AlertDialog;
import moralnorm.preference.Preference;

public class DevelopmentFragment extends SettingsPreferenceFragment implements Preference.OnPreferenceClickListener {

Preference mCmdR;
Preference mDeleteAllDexKitCache;

public interface EditDialogCallback {
void onInputReceived(String command);
}

public class DevelopmentFragment extends SettingsPreferenceFragment {
@Override
public int getContentResId() {
return R.xml.prefs_development;
}

@Override
public void initPrefs() {
mCmdR = findPreference("prefs_key_development_cmd_r");
mDeleteAllDexKitCache = findPreference("prefs_key_development_delete_all_dexkit_cache");
mCmdR.setOnPreferenceClickListener(this);
mDeleteAllDexKitCache.setOnPreferenceClickListener(this);
}

@Override
public boolean onPreferenceClick(@NonNull Preference preference) {
switch (preference.getKey()) {
case "prefs_key_development_cmd_r" -> {
showInDialog(new DevelopmentKillFragment.EditDialogCallback() {
@Override
public void onInputReceived(String command) {
showOutDialog(safeExecCommandWithRoot(command));
}
});
}
case "prefs_key_development_delete_all_dexkit_cache" -> {
DialogHelper.showDialog(getActivity(), R.string.warn, R.string.delete_all_dexkit_cache_desc, (dialog, which) -> {
DexKit.deleteAllCache(getActivity());
Toast.makeText(getActivity(), R.string.delete_all_dexkit_cache_success, Toast.LENGTH_LONG).show();
});
}
}
return true;
}

private void showInDialog(DevelopmentKillFragment.EditDialogCallback callback) {
View view = LayoutInflater.from(getActivity()).inflate(R.layout.edit_dialog, null);
EditText input = view.findViewById(R.id.title);
new AlertDialog.Builder(getActivity())
.setTitle("# root@HyperCeiler > Input")
.setView(view)
.setCancelable(false)
.setPositiveButton(android.R.string.ok, (dialog, which) -> {
String userInput = input.getText().toString();
if (userInput.isEmpty()) {
dialog.dismiss();
showInDialog(callback);
return;
}
callback.onInputReceived(userInput);
dialog.dismiss();
})
.setNegativeButton(android.R.string.cancel, (dialog, which) -> dialog.dismiss())
.show();
}

private void showOutDialog(String show) {
new AlertDialog.Builder(getActivity())
.setCancelable(false)
.setTitle("# root@HyperCeiler > Output")
.setMessage(show)
.setPositiveButton(android.R.string.ok, null)
.show();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@
*/
package com.sevtinge.hyperceiler.utils.shell;

import android.util.Log;

import com.sevtinge.hyperceiler.utils.log.AndroidLogUtils;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
Expand Down Expand Up @@ -255,20 +258,51 @@ public CommandResult(int result, String successMsg, String errorMsg) {
}
}

public static String safeExecCommandWithRoot(String command) {
public static String safeExecCommandWithRoot(String cmd) {
String result = "";
DataOutputStream dos = null;
DataInputStream dis = null;

try {
Process suProcess = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());
os.writeBytes(command + "\n");
os.flush();
BufferedReader reader = new BufferedReader(new InputStreamReader(suProcess.getInputStream()));
String result = reader.readLine();
os.writeBytes("exit\n");
os.flush();
suProcess.waitFor();
return result;
} catch (IOException | InterruptedException e) {
return e.toString();
Process p = Runtime.getRuntime().exec("su");
dos = new DataOutputStream(p.getOutputStream());
dis = new DataInputStream(p.getInputStream());

//Log.i("ihasddfihasifha", cmd);
dos.writeBytes("nsenter --mount=/proc/1/ns/mnt -- " + cmd + "\n"); // 沟槽的命名空间
dos.flush();
dos.writeBytes("exit\n");
dos.flush();
String line = null;
while ((line = dis.readLine()) != null) {
//Log.d("ihasddfihasifha", line);
result += line + "\n";
}
p.waitFor();
} catch (Exception e) {
//Log.d("ihasddfihasifha", String.valueOf(e));
return String.valueOf(e);
} finally {
if (dos != null) {
try {
dos.close();
} catch (IOException e) {
//Log.d("ihasddfihasifha", String.valueOf(e));
return String.valueOf(e);
}
}
if (dis != null) {
try {
dis.close();
} catch (IOException e) {
//Log.d("ihasddfihasifha", String.valueOf(e));
return String.valueOf(e);
}
}
}
if (!result.isEmpty()) {
result = result.substring(0, result.length() - 1);
}
return result;
}
}
5 changes: 5 additions & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@

<string name="preference_recommend_tip">在查找其他内容吗?</string>

<string name="delete_all_dexkit_cache_desc">确定要删除全部 DexKit 缓存吗?</string>
<string name="delete_all_dexkit_cache_success">已删除全部 DexKit 缓存</string>

<string name="set_homepage_entrance">主页入口设置</string>
<string name="set_homepage_entrance_about">仅主页入口可见性设置,已经开启的功能不会被关闭。\n关闭入口实时生效,重新显示需重启应用。</string>
<string name="help_cant_see_apps_guide">列表中没有您想要找的应用?</string>
Expand Down Expand Up @@ -79,6 +82,8 @@
<string name="development_ui">界面元素测试</string>
<string name="development_debug_info">调试信息</string>
<string name="development_prefs">共享首选项</string>
<string name="development_cmd_r">使用 root 的模拟终端</string>
<string name="development_delete_all_dexkit_cache">删除全部 DexKit 缓存</string>

<string name="settings_show_title">显示</string>
<string name="settings_data_title">备份与恢复</string>
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@
<string name="development_ui">UI</string>
<string name="development_debug_info">Debug info</string>
<string name="development_prefs">Share preference</string>
<string name="development_cmd_r">CMD with root</string>
<string name="development_delete_all_dexkit_cache">Delete all DexKit cache</string>
<string name="about_privacy">Privacy Policy</string>
<string name="about_protocol">User Agreement</string>
<string name="settings_show_title">Show</string>
Expand Down Expand Up @@ -163,6 +165,8 @@
<string name="about_crwd">Crowdin</string>
<string name="about_crwd_desc">Help us translate HyperCeiler into you language</string>
<string name="debug_info" translatable="false">Cannot get debug info.</string>
<string name="delete_all_dexkit_cache_desc">Are you sure you want to clear all the DexKit cache?</string>
<string name="delete_all_dexkit_cache_success">Successfully cleared all the DexKit cache</string>
<!--Pop-up window/miuix part-->
<string name="soft_reboot">Reboot </string>
<string name="restart_app_desc">Are you sure you want to restart now %1$s?</string>
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/res/xml/prefs_development.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,12 @@
android:key="prefs_key_development_ui"
android:title="@string/development_ui" />

<Preference
android:key="prefs_key_development_cmd_r"
android:title="@string/development_cmd_r" />

<Preference
android:key="prefs_key_development_delete_all_dexkit_cache"
android:title="@string/development_delete_all_dexkit_cache" />

</PreferenceScreen>

0 comments on commit 53a7a40

Please sign in to comment.