3
3
import java .util .List ;
4
4
import java .util .ArrayList ;
5
5
import java .lang .Exception ;
6
- import java .io .File ;
7
- import java .io .FileInputStream ;
6
+ import java .io .RandomAccessFile ;
7
+ import java .io .IOException ;
8
8
import java .io .FileNotFoundException ;
9
9
import android .os .Bundle ;
10
10
import android .os .AsyncTask ;
27
27
import android .content .IntentFilter ;
28
28
import android .hardware .usb .UsbManager ;
29
29
import android .hardware .usb .UsbDevice ;
30
+ import android .util .Log ;
30
31
import ru .nikita .adb .FastbootVariablesListActivity ;
31
32
import ru .nikita .adb .PartitionListActivity ;
32
33
import ru .nikita .adb .FastbootDevice ;
@@ -239,47 +240,99 @@ protected void onPreExecute() {
239
240
protected void onProgressUpdate (Integer ... values ) {
240
241
pd .setProgress (values [0 ]);
241
242
pd .setMax (values [1 ]);
243
+ String message = getResources ().getString (R .string .sending );
242
244
if (values [0 ] == values [1 ])
243
- pd .setMessage (String .format ("%s '%s' %d/%d..." , getResources ().getString (R .string .writing ), partition , values [2 ], values [3 ]));
244
- else
245
- pd .setMessage (String .format ("%s '%s' %d/%d (%d KB)..." , getResources ().getString (R .string .sending ), partition , values [2 ], values [3 ], values [1 ] / 1024 ));
245
+ message = getResources ().getString (R .string .writing );
246
+ pd .setMessage (String .format ("%s '%s' %d/%d" , message , partition , values [2 ], values [3 ]));
246
247
}
247
248
@ Override
248
249
protected void onPostExecute (Exception result ) {
249
250
pd .dismiss ();
250
- if (result != null )
251
+ if (result != null ) {
251
252
showToast (result );
253
+ result .printStackTrace ();
254
+ }
252
255
}
253
256
254
- private void downloadData (byte [] data , int part , int maxParts ) {
255
- int length = data .length ;
256
- int offset = 0 ;
257
- device .downloadCommand (length );
258
- int n = 0 ;
259
- while (offset < length ) {
260
- offset = device .writeData (data , offset );
261
- if (n ++ % 300 == 0 ) // reduce lags
262
- publishProgress (offset , length , part , maxParts );
263
- }
257
+ //private class DownloadStream extends FastbootDevice.OutputStream {
258
+ // DownloadStream(int length, int part, int maxParts) {
259
+ // this.part = part;
260
+ // this.maxParts = maxParts;
261
+ // this.bytes = 0;
262
+ // }
263
+
264
+ // @Override
265
+ // public synchronized void write(byte[] buffer, int offset, int count) {
266
+ // Log.d("ADB", Integer.toString(bytes));
267
+ // while(offset < count) {
268
+ // int bytesWritten = device.writeData(buffer, offset);
269
+ // offset += bytesWritten;
270
+ // bytes += bytesWritten;
271
+ // //publishProgress(bytes, 0x8000000, part, maxParts);
272
+ // }
273
+ // }
274
+
275
+ // @Override
276
+ // public synchronized void write(int b) {
277
+ // Log.d("ADB", Integer.toString(bytes));
278
+ // device.writeByte(b);
279
+ // bytes++;
280
+ // //publishProgress(bytes, 0x80000000, part, maxParts);
281
+ // }
282
+
283
+ // private int length;
284
+ // private int part;
285
+ // private int maxParts;
286
+ //}
287
+
288
+ private void downloadData (SparseFile file , int part , int maxParts ) throws IOException {
289
+ byte [] data = file .getBytes ();
290
+ device .downloadCommand (file .countLen ());
291
+
292
+ RandomAccessFile f = new RandomAccessFile (String .format ("/sdcard/sparse%d.img" , part ), "rw" );
293
+ f .write (data );
294
+ f .close ();
295
+
296
+ FastbootDevice .OutputStream stream = device .new OutputStream ();
297
+ for (int bytes = 0 ; bytes < data .length ; bytes += stream .write (data , bytes ));
298
+ stream .close ();
299
+
264
300
device .checkOkay ();
265
301
publishProgress (1 , 1 , part , maxParts );
266
302
}
267
303
304
+ private void downloadData (RandomAccessFile file ) throws IOException {
305
+ device .downloadCommand (file .length ());
306
+
307
+ FastbootDevice .OutputStream stream = device .new OutputStream ();
308
+ for (int bytes = 0 ; bytes < file .length (); bytes += stream .write (file , (int ) file .length (), bytes ));
309
+ stream .close ();
310
+
311
+ device .checkOkay ();
312
+ publishProgress (1 , 1 , 1 , 1 );
313
+ }
314
+
268
315
@ Override
269
316
protected Exception doInBackground (Void ... args ) {
270
317
try {
271
- File file = new File (filePath );
272
- if (!file .exists () || !file .isFile ())
273
- throw new FileNotFoundException ("File not found" );
274
- long fileLength = file .length ();
275
- long sparseLimit = device .getSparseLimit ();
276
-
277
- byte [] data = new byte [(int ) file .length ()];
278
- FileInputStream stream = new FileInputStream (file );
279
- stream .read (data );
280
-
281
- downloadData (data , 1 , 1 );
282
- device .flash (partition );
318
+ RandomAccessFile file = new RandomAccessFile (filePath , "r" );
319
+ int fileLength = (int ) file .length ();
320
+ int sparseLimit = device .getSparseLimit ();
321
+
322
+ if (fileLength <= sparseLimit ) {
323
+ downloadData (file );
324
+ device .flash (partition );
325
+ } else {
326
+ SparseFile sparse = new SparseFile (file );
327
+ SparseFile [] files = sparse .resparse (sparseLimit );
328
+ device .erase (partition );
329
+ for (int i = 0 ; i < files .length ; i ++) {
330
+ Log .v ("ADB" , "Downloading" );
331
+ downloadData (files [i ], i + 1 , files .length );
332
+ Log .v ("ADB" , "Flashing" );
333
+ device .flash (partition );
334
+ }
335
+ }
283
336
} catch (Exception e ) {
284
337
return e ;
285
338
}
@@ -294,7 +347,6 @@ protected Exception doInBackground(Void ... args) {
294
347
private ProgressDialog pd ;
295
348
}
296
349
297
-
298
350
public void flash (View view ) {
299
351
if (getSelectedDevice ().openConnection (this )) {
300
352
String filePath = ((EditText ) findViewById (R .id .image_path )).getText ().toString ();
0 commit comments