12
12
import java .io .InputStreamReader ;
13
13
import java .io .OutputStreamWriter ;
14
14
15
- public class Worker extends MainActivity .fileWorker {
15
+ public class Worker extends MainActivity .fileWorker {
16
16
Activity activity ;
17
17
String file_path ;
18
18
String binary_path ;
19
19
boolean is_error ;
20
- public Worker (Activity activity ){
21
- this .activity =activity ;
20
+
21
+ public Worker (Activity activity ) {
22
+ this .activity = activity ;
22
23
}
23
- public void run (){
24
- MainActivity .cur_status = MainActivity .status .flashing ;
25
- ((MainActivity )activity ).update_title ();
26
- is_error =false ;
27
- file_path =activity .getFilesDir ().getAbsolutePath ()+"/anykernel.zip" ;
28
- binary_path =activity .getFilesDir ()+"/META-INF/com/google/android/update-binary" ;
24
+
25
+ public void run () {
26
+ MainActivity .cur_status = MainActivity .status .flashing ;
27
+ ((MainActivity ) activity ).update_title ();
28
+ is_error = false ;
29
+ file_path = activity .getFilesDir ().getAbsolutePath () + "/anykernel.zip" ;
30
+ binary_path = activity .getFilesDir ().getAbsolutePath () + "/META-INF/com/google/android/update-binary" ;
29
31
30
32
try {
31
33
cleanup ();
32
34
} catch (IOException ioException ) {
33
- is_error = true ;
35
+ is_error = true ;
34
36
}
35
- if (is_error ){
36
- MainActivity ._appendLog (activity .getResources ().getString (R .string .unable_cleanup ),activity );
37
- MainActivity .cur_status = MainActivity .status .error ;
38
- ((MainActivity )activity ).update_title ();
37
+ if (is_error ) {
38
+ MainActivity ._appendLog (activity .getResources ().getString (R .string .unable_cleanup ), activity );
39
+ MainActivity .cur_status = MainActivity .status .error ;
40
+ ((MainActivity ) activity ).update_title ();
39
41
return ;
40
42
}
41
43
42
- try {
44
+ try {
43
45
copy ();
44
- }catch (IOException ioException ){
45
- is_error = true ;
46
+ } catch (IOException ioException ) {
47
+ is_error = true ;
46
48
}
47
- if (!is_error )
48
- is_error = !new File (file_path ).exists ();
49
- if (is_error ){
50
- MainActivity ._appendLog (activity .getResources ().getString (R .string .unable_copy ),activity );
51
- MainActivity .cur_status = MainActivity .status .error ;
52
- ((MainActivity )activity ).update_title ();
49
+ if (!is_error )
50
+ is_error = !new File (file_path ).exists ();
51
+ if (is_error ) {
52
+ MainActivity ._appendLog (activity .getResources ().getString (R .string .unable_copy ), activity );
53
+ MainActivity .cur_status = MainActivity .status .error ;
54
+ ((MainActivity ) activity ).update_title ();
53
55
return ;
54
56
}
55
57
56
- try {
58
+ try {
57
59
getBinary ();
58
- }catch (IOException ioException ){
59
- is_error = true ;
60
+ } catch (IOException ioException ) {
61
+ is_error = true ;
60
62
}
61
- if (is_error ){
63
+ if (is_error ) {
62
64
MainActivity ._appendLog (activity .getResources ().getString (R .string .unable_get_exe ), activity );
63
- MainActivity .cur_status = MainActivity .status .error ;
64
- ((MainActivity )activity ).update_title ();
65
+ MainActivity .cur_status = MainActivity .status .error ;
66
+ ((MainActivity ) activity ).update_title ();
65
67
return ;
66
68
}
67
69
68
- try {
70
+ try {
69
71
flash (activity );
70
- }catch (IOException ioException ){
71
- is_error = true ;
72
+ } catch (IOException ioException ) {
73
+ is_error = true ;
72
74
}
73
- if (is_error ){
75
+ if (is_error ) {
74
76
MainActivity ._appendLog (activity .getResources ().getString (R .string .unable_flash_root ), activity );
75
- MainActivity .cur_status = MainActivity .status .error ;
76
- ((MainActivity )activity ).update_title ();
77
+ MainActivity .cur_status = MainActivity .status .error ;
78
+ ((MainActivity ) activity ).update_title ();
77
79
return ;
78
80
}
79
81
activity .runOnUiThread (() -> {
@@ -85,22 +87,22 @@ public void run(){
85
87
try {
86
88
reboot ();
87
89
} catch (IOException e ) {
88
- Toast .makeText (activity ,R .string .failed_reboot ,Toast .LENGTH_SHORT ).show ();
90
+ Toast .makeText (activity , R .string .failed_reboot , Toast .LENGTH_SHORT ).show ();
89
91
}
90
92
})
91
- .setNegativeButton (R .string .no ,null )
93
+ .setNegativeButton (R .string .no , null )
92
94
.create ().show ();
93
95
});
94
- MainActivity .cur_status = MainActivity .status .flashing_done ;
95
- ((MainActivity )activity ).update_title ();
96
+ MainActivity .cur_status = MainActivity .status .flashing_done ;
97
+ ((MainActivity ) activity ).update_title ();
96
98
}
97
99
98
100
void copy () throws IOException {
99
101
InputStream inputStream = activity .getContentResolver ().openInputStream (uri );
100
102
FileOutputStream fileOutputStream = new FileOutputStream (new File (file_path ));
101
103
byte [] buffer = new byte [1024 ];
102
- int count = 0 ;
103
- while ((count = inputStream .read (buffer ))!= -1 ) {
104
+ int count = 0 ;
105
+ while ((count = inputStream .read (buffer )) != -1 ) {
104
106
fileOutputStream .write (buffer , 0 , count );
105
107
}
106
108
fileOutputStream .flush ();
@@ -109,58 +111,52 @@ void copy() throws IOException {
109
111
}
110
112
111
113
void getBinary () throws IOException {
112
- Process process =new ProcessBuilder ("sh" ).start ();
113
- BufferedReader bufferedReader =new BufferedReader (new InputStreamReader (process .getInputStream ()));
114
- OutputStreamWriter outputStreamWriter =new OutputStreamWriter (process .getOutputStream ());
115
- outputStreamWriter .write ("unzip " +file_path +" \" */update-binary\" -d " +activity .getFilesDir ()+"\n exit\n " );
116
- outputStreamWriter .flush ();
117
- while (bufferedReader .readLine ()!=null ){}
118
- bufferedReader .close ();
119
- outputStreamWriter .close ();
120
- process .destroy ();
121
- if (!new File (binary_path ).exists ())
114
+ runWithNewProcessNoReturn (false , "unzip " + file_path + " \" */update-binary\" -d " + activity .getFilesDir ().getAbsolutePath ());
115
+ if (!new File (binary_path ).exists ())
122
116
throw new IOException ();
123
117
}
124
118
125
119
void flash (Activity activity ) throws IOException {
126
- Process process = new ProcessBuilder ("su" ).redirectErrorStream (true ).start ();
127
- OutputStreamWriter outputStreamWriter = new OutputStreamWriter (process .getOutputStream ());
128
- BufferedReader bufferedReader = new BufferedReader (new InputStreamReader (process .getInputStream ()));
129
- outputStreamWriter .write ("export POSTINSTALL=" + activity .getFilesDir ()+ "\n " );
130
- outputStreamWriter .write ("sh " + (MainActivity .DEBUG ? "-x " : "" )+ binary_path + " 3 1 " + file_path + "&& touch " + activity .getFilesDir ()+ "/done\n exit\n " );
120
+ Process process = new ProcessBuilder ("su" ).redirectErrorStream (true ).start ();
121
+ OutputStreamWriter outputStreamWriter = new OutputStreamWriter (process .getOutputStream ());
122
+ BufferedReader bufferedReader = new BufferedReader (new InputStreamReader (process .getInputStream ()));
123
+ outputStreamWriter .write ("export POSTINSTALL=" + activity .getFilesDir (). getAbsolutePath () + "\n " );
124
+ outputStreamWriter .write ("sh " + (MainActivity .DEBUG ? "-x " : "" ) + binary_path + " 3 1 " + file_path + "&& touch " + activity .getFilesDir (). getAbsolutePath () + "/done\n exit\n " );
131
125
outputStreamWriter .flush ();
132
126
String line ;
133
- while ((line = bufferedReader .readLine ())!= null )
134
- MainActivity .appendLog (line ,activity );
127
+ while ((line = bufferedReader .readLine ()) != null )
128
+ MainActivity .appendLog (line , activity );
135
129
136
130
bufferedReader .close ();
137
131
outputStreamWriter .close ();
138
132
process .destroy ();
139
133
140
- if (!new File (activity .getFilesDir ()+ "/done" ).exists ())
134
+ if (!new File (activity .getFilesDir (). getAbsolutePath () + "/done" ).exists ())
141
135
throw new IOException ();
142
136
}
143
137
144
138
void cleanup () throws IOException {
145
- Process process =new ProcessBuilder ("sh" ).start ();
146
- BufferedReader bufferedReader =new BufferedReader (new InputStreamReader (process .getInputStream ()));
147
- OutputStreamWriter outputStreamWriter =new OutputStreamWriter (process .getOutputStream ());
148
- outputStreamWriter .write ("rm -rf " +activity .getFilesDir ()+"/*\n exit\n " );
149
- outputStreamWriter .flush ();
150
- while (bufferedReader .readLine ()!=null ){}
151
- bufferedReader .close ();
152
- outputStreamWriter .close ();
153
- process .destroy ();
139
+ runWithNewProcessNoReturn (false , "rm -rf " + activity .getFilesDir ().getAbsolutePath () + "/*" );
140
+ }
141
+
142
+ void reboot () throws IOException {
143
+ runWithNewProcessNoReturn (true , "svc power reboot" );
154
144
}
155
145
156
- void reboot () throws IOException {
157
- Process process =new ProcessBuilder ("su" ).redirectErrorStream (true ).start ();
158
- OutputStreamWriter outputStreamWriter =new OutputStreamWriter ((process .getOutputStream ()));
159
- BufferedReader bufferedReader =new BufferedReader (new InputStreamReader (process .getInputStream ()));
160
- outputStreamWriter .write ("svc power reboot\n " );
146
+ void runWithNewProcessNoReturn (boolean su , String cmd ) throws IOException {
147
+ Process process = null ;
148
+ if (su ) {
149
+ process = new ProcessBuilder ("su" ).redirectErrorStream (true ).start ();
150
+ } else {
151
+ process = new ProcessBuilder ("sh" ).redirectErrorStream (true ).start ();
152
+ }
153
+ OutputStreamWriter outputStreamWriter = new OutputStreamWriter ((process .getOutputStream ()));
154
+ BufferedReader bufferedReader = new BufferedReader (new InputStreamReader (process .getInputStream ()));
155
+ outputStreamWriter .write (cmd + "\n " );
161
156
outputStreamWriter .write ("exit\n " );
162
157
outputStreamWriter .flush ();
163
- while (bufferedReader .readLine ()!=null ){}
158
+ while (bufferedReader .readLine () != null ) {
159
+ }
164
160
outputStreamWriter .close ();
165
161
bufferedReader .close ();
166
162
process .destroy ();
0 commit comments