huangcm
2025-04-09 02d4ce54b909bd733f12e9f3fa4c1b03cf2d6f45
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package com.DeviceTest.helper;
 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
 
import com.rockchip.newton.UserModeManager;
 
import android.content.pm.PackageManager;
import android.graphics.Picture;
import android.util.Log;
 
public class SystemUtil {
   private static final String TAG = "SystemUtil";
   public static int execShellCmdForStatue(String command) {
       int status = -1;
       try {
           Process p = Runtime.getRuntime().exec(command);
           BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(p.getInputStream()));
           String s = "";
           while((s = bufferedReader.readLine()) != null){
               Log.d(TAG, " >>>> " + s);
           }
           status = p.waitFor();
           Log.d(TAG, " ________________----------- command: " + command + "    status = " + status);
       } catch (Exception e) {
           e.printStackTrace();
       }
       return status;
 
   }
 
   public static String execShellCmd(String command) {
       String result = "";
       Log.i("execShellCmd", command);
       try {
           Process process = Runtime.getRuntime().exec(command + "\n");
           DataOutputStream stdin = new DataOutputStream(
                   process.getOutputStream());
           DataInputStream stdout = new DataInputStream(
                   process.getInputStream());
           DataInputStream stderr = new DataInputStream(
                   process.getErrorStream());
           String line;
           while ((line = stdout.readLine()) != null) {
               result += line + "\n";
           }
           if (result.length() > 0) {
               result = result.substring(0, result.length() - 1);
           }
           while ((line = stderr.readLine()) != null) {
               Log.e("EXEC", line);
           }
           process.waitFor();
       } catch (Exception e) {
           e.getMessage();
       }
       return result;
   }
 
   public static String execRootCmd(String command) {
       int userMode = UserModeManager.getCurrentUserMode();
       UserModeManager.switchToUserMode(UserModeManager.SUPER_USER_MODE);
 
       String result = execShellCmd("su root " + command + "\n");
 
       UserModeManager.switchToUserMode(userMode);
 
       return result;
   }
 
   public static String execScriptCmd(String command, String path, boolean root) {
       int userMode = UserModeManager.getCurrentUserMode();
       UserModeManager.switchToUserMode(UserModeManager.SUPER_USER_MODE);
       File tempFile = null;
       String result = "";
       Log.i("execScriptCmd", command);
       try {
           tempFile = new File(path);
           tempFile.deleteOnExit();
           BufferedWriter br = new BufferedWriter(new OutputStreamWriter(
                   new FileOutputStream(tempFile)));
           br.write("#!/system/bin/sh\n");
           br.write(command);
           br.close();
           SystemUtil.execShellCmd("su root chmod 777 "
                   + tempFile.getAbsolutePath());
           result = SystemUtil.execShellCmd((root ? "su root " : "")
                   + tempFile.getAbsolutePath());
       } catch (FileNotFoundException e) {
           e.printStackTrace();
       } catch (IOException e) {
           e.printStackTrace();
       } finally {
           if (tempFile != null && tempFile.exists()) {
               tempFile.delete();
           }
       }
       UserModeManager.switchToUserMode(userMode);
       return result;
   }
 
   public static boolean killProcessByPath(String exePath) {
       File dir = new File("/proc/");
       String[] files = dir.list();
       int pid = -1;
       for (String path : files) {
           File file = new File("/proc/" + path + "/cmdline");
           if (file.exists()) {
               String cmdline = execShellCmd("cat " + file.getAbsolutePath());
               if (cmdline.startsWith(exePath)) {
                   try {
                       pid = Integer.parseInt(path);
                       break;
                   } catch (Exception e) {
                       break;
                   }
               }
           }
       }
 
       if (pid >= 0) {
           int userMode = UserModeManager.getCurrentUserMode();
           UserModeManager.switchToUserMode(UserModeManager.SUPER_USER_MODE);
           execShellCmd("su root kill " + pid);
           UserModeManager.switchToUserMode(userMode);
           return true;
       }
       return false;
   }
 
}