Skip to content

Commit

Permalink
feat(dokit): 展示所有properties
Browse files Browse the repository at this point in the history
  • Loading branch information
hss01248 committed Jun 11, 2024
1 parent 94b6666 commit c1795ce
Show file tree
Hide file tree
Showing 2 changed files with 225 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import android.content.DialogInterface;
import android.os.Build;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.WindowManager;

import androidx.appcompat.app.AlertDialog;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.ThreadUtils;
import com.blankj.utilcode.util.Utils;
import com.google.gson.GsonBuilder;
import com.hss01248.dokit.R;
Expand Down Expand Up @@ -44,7 +46,7 @@ public void onClick() {

int[] choosen = new int[]{0};
CharSequence[] items = new CharSequence[]{"BuildConfig","Build","Settings.System",
"Settings.Secure","Settings.Global","Settings All"};
"Settings.Secure","Settings.Global","Settings All","adb getprop"};
AlertDialog dialog = new AlertDialog.Builder(ActivityUtils.getTopActivity())
.setTitle("选择你要显示的信息")
.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
Expand All @@ -63,6 +65,8 @@ public void onClick(DialogInterface dialog, int which) {
showSettingsGlobal();
}else if(choosen[0] ==5){
showSettingsAll();
}else if(choosen[0] ==6){
showPropertiesAll();
}
}
})
Expand All @@ -75,6 +79,54 @@ public void onClick(DialogInterface dialog, int which) {
dialog.show();
}

private void showPropertiesAll() {

ThreadUtils.executeByIo(new ThreadUtils.SimpleTask<String>() {
@Override
public String doInBackground() throws Throwable {
String[] arr = getProps();
Map map = new TreeMap();
if(arr.length >0){
int i = 0;
for (String s : arr) {
if(s.contains(":")){
String[] split = s.split(":");
String key = split[0].replace("[","").replace("]","");
String value = split[1].replace("[","").replace("]","");
map.put(key,value);
}else {
map.put(i+"",s);
}
i++;
}

}else {
map.put("reason","arr length is 0");
}
return new GsonBuilder().setPrettyPrinting().create().toJson(map);
}

@Override
public void onSuccess(String result) {
showMsg("getprop All",result);
}

@Override
public void onFail(Throwable t) {
super.onFail(t);
showMsg("getprop All",t.getMessage());
}
});



}

public static String[] getProps() {
MyShellUtils.CommandResult commandResult = MyShellUtils.execCmd("getprop", false);
return commandResult != null && !TextUtils.isEmpty(commandResult.successMsg) ? commandResult.successMsg.split("\n") : new String[0];
}

private void showSettingsAll() {
Map map = new TreeMap();

Expand Down
172 changes: 172 additions & 0 deletions dokit/src/main/java/com/hss01248/dokit/btns/MyShellUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
package com.hss01248.dokit.btns;

import android.os.Build.VERSION;

import com.blankj.utilcode.util.LogUtils;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.concurrent.TimeUnit;

public final class MyShellUtils {
private static final String LINE_SEP = System.getProperty("line.separator");

private MyShellUtils() {
throw new UnsupportedOperationException("u can't instantiate me...");
}

public static CommandResult execCmd(String command, boolean isRooted) {
return execCmd(new String[]{command}, isRooted, true);
}

public static CommandResult execCmd(String command, List<String> envp, boolean isRooted) {
return execCmd(new String[]{command}, envp == null ? null : (String[])envp.toArray(new String[0]), isRooted, true);
}

public static CommandResult execCmd(List<String> commands, boolean isRooted) {
return execCmd(commands == null ? null : (String[])commands.toArray(new String[0]), isRooted, true);
}

public static CommandResult execCmd(List<String> commands, List<String> envp, boolean isRooted) {
return execCmd(commands == null ? null : (String[])commands.toArray(new String[0]), envp == null ? null : (String[])envp.toArray(new String[0]), isRooted, true);
}

public static CommandResult execCmd(String[] commands, boolean isRooted) {
return execCmd(commands, isRooted, true);
}

public static CommandResult execCmd(String command, boolean isRooted, boolean isNeedResultMsg) {
return execCmd(new String[]{command}, isRooted, isNeedResultMsg);
}

public static CommandResult execCmd(String command, List<String> envp, boolean isRooted, boolean isNeedResultMsg) {
return execCmd(new String[]{command}, envp == null ? null : (String[])envp.toArray(new String[0]), isRooted, isNeedResultMsg);
}

public static CommandResult execCmd(String command, String[] envp, boolean isRooted, boolean isNeedResultMsg) {
return execCmd(new String[]{command}, envp, isRooted, isNeedResultMsg);
}

public static CommandResult execCmd(List<String> commands, boolean isRooted, boolean isNeedResultMsg) {
return execCmd(commands == null ? null : (String[])commands.toArray(new String[0]), isRooted, isNeedResultMsg);
}

public static CommandResult execCmd(String[] commands, boolean isRooted, boolean isNeedResultMsg) {
return execCmd((String[])commands, (String[])null, isRooted, isNeedResultMsg);
}

public static CommandResult execCmd(String[] commands, String[] envp, boolean isRooted, boolean isNeedResultMsg) {
int result = -1;
if (commands != null && commands.length != 0) {
Process process = null;
BufferedReader successResult = null;
BufferedReader errorResult = null;
StringBuilder successMsg = null;
StringBuilder errorMsg = null;
DataOutputStream os = null;

try {
process = Runtime.getRuntime().exec(isRooted ? "su" : "sh", envp, (File)null);
os = new DataOutputStream(process.getOutputStream());
String[] var11 = commands;
int var12 = commands.length;

for(int var13 = 0; var13 < var12; ++var13) {
String command = var11[var13];
if (command != null) {
os.write(command.getBytes());
os.writeBytes(LINE_SEP);
os.flush();
}
}

os.writeBytes("exit" + LINE_SEP);
os.flush();
if (VERSION.SDK_INT >= 26) {
result = process.waitFor(1L, TimeUnit.SECONDS) ? 1 : 0;
} else {
result = process.waitFor();
}

if (isNeedResultMsg) {
successMsg = new StringBuilder();
errorMsg = new StringBuilder();
successResult = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8"));
errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream(), "UTF-8"));
String line;
if ((line = successResult.readLine()) != null) {
successMsg.append(line);

while((line = successResult.readLine()) != null) {
successMsg.append(LINE_SEP).append(line);
}
}

if ((line = errorResult.readLine()) != null) {
errorMsg.append(line);

while((line = errorResult.readLine()) != null) {
errorMsg.append(LINE_SEP).append(line);
}
}
}
} catch (Exception var31) {
LogUtils.w(var31);
} finally {
try {
if (os != null) {
os.close();
}
} catch (IOException var30) {
var30.printStackTrace();
}

try {
if (successResult != null) {
successResult.close();
}
} catch (IOException var29) {
var29.printStackTrace();
}

try {
if (errorResult != null) {
errorResult.close();
}
} catch (IOException var28) {
var28.printStackTrace();
}

if (process != null) {
process.destroy();
}

}

return new CommandResult(result, successMsg == null ? "" : successMsg.toString(), errorMsg == null ? "" : errorMsg.toString());
} else {
return new CommandResult(result, "", "");
}
}

public static class CommandResult {
public int result;
public String successMsg;
public String errorMsg;

public CommandResult(int result, String successMsg, String errorMsg) {
this.result = result;
this.successMsg = successMsg;
this.errorMsg = errorMsg;
}

public String toString() {
return "result: " + this.result + "\nsuccessMsg: " + this.successMsg + "\nerrorMsg: " + this.errorMsg;
}
}
}

0 comments on commit c1795ce

Please sign in to comment.