Skip to content

Commit 76af8f1

Browse files
committed
Check root permission ahead of flashing
* This splits permission error from flashing error
1 parent 840db0b commit 76af8f1

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

app/src/main/java/xzr/hkf/Worker.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ public void run() {
4545
return;
4646
}
4747

48+
if (!rootAvailable()) {
49+
MainActivity._appendLog(activity.getResources().getString(R.string.unable_flash_root), activity);
50+
MainActivity.cur_status = MainActivity.status.error;
51+
((MainActivity) activity).update_title();
52+
return;
53+
}
54+
4855
try {
4956
copy();
5057
} catch (IOException ioException) {
@@ -82,7 +89,7 @@ public void run() {
8289
is_error = true;
8390
}
8491
if (is_error) {
85-
MainActivity._appendLog(activity.getResources().getString(R.string.unable_flash_root), activity);
92+
MainActivity._appendLog(activity.getResources().getString(R.string.unable_flash_error), activity);
8693
MainActivity.cur_status = MainActivity.status.error;
8794
((MainActivity) activity).update_title();
8895
return;
@@ -106,6 +113,15 @@ public void run() {
106113
((MainActivity) activity).update_title();
107114
}
108115

116+
boolean rootAvailable() {
117+
try {
118+
String ret = runWithNewProcessReturn(true, "id");
119+
return ret != null && ret.contains("root");
120+
} catch (IOException e) {
121+
return false;
122+
}
123+
}
124+
109125
void copy() throws IOException {
110126
InputStream inputStream = activity.getContentResolver().openInputStream(uri);
111127
FileOutputStream fileOutputStream = new FileOutputStream(new File(file_path));
@@ -160,6 +176,10 @@ void reboot() throws IOException {
160176
}
161177

162178
void runWithNewProcessNoReturn(boolean su, String cmd) throws IOException {
179+
runWithNewProcessReturn(su, cmd);
180+
}
181+
182+
String runWithNewProcessReturn(boolean su, String cmd) throws IOException {
163183
Process process = null;
164184
if (su) {
165185
process = new ProcessBuilder("su").redirectErrorStream(true).start();
@@ -171,11 +191,16 @@ void runWithNewProcessNoReturn(boolean su, String cmd) throws IOException {
171191
outputStreamWriter.write(cmd + "\n");
172192
outputStreamWriter.write("exit\n");
173193
outputStreamWriter.flush();
174-
while (bufferedReader.readLine() != null) {
194+
String tmp;
195+
StringBuilder ret = new StringBuilder();
196+
while ((tmp = bufferedReader.readLine()) != null) {
197+
ret.append(tmp);
198+
ret.append("\n");
175199
}
176200
outputStreamWriter.close();
177201
bufferedReader.close();
178202
process.destroy();
203+
return ret.toString();
179204
}
180205

181206
}

app/src/main/res/values-zh-rCN/strings.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@
1818
<string name="flashing_done">刷入完成</string>
1919
<string name="failed">刷入错误</string>
2020
<string name="task_running">一个任务尚未结束</string>
21-
<string name="unable_flash_root">刷入时发生错误,请检查root权限</string>
21+
<string name="unable_flash_root">没有root权限</string>
22+
<string name="unable_flash_error">刷入时发生了错误</string>
2223
</resources>

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@
1818
<string name="flashing_done">Done</string>
1919
<string name="failed">Failed</string>
2020
<string name="task_running">A task is still running</string>
21-
<string name="unable_flash_root">Failed when flashing. Please check root permission.</string>
21+
<string name="unable_flash_root">Unable to get root permission</string>
22+
<string name="unable_flash_error">An error occurred while flashing</string>
2223
</resources>

0 commit comments

Comments
 (0)