Skip to content

Commit e36f1e0

Browse files
committed
some changes
1 parent 377fe2b commit e36f1e0

File tree

7 files changed

+185
-242
lines changed

7 files changed

+185
-242
lines changed

app/src/main/java/ru/nikita/adb/BackedBlock.java

Lines changed: 0 additions & 32 deletions
This file was deleted.

app/src/main/java/ru/nikita/adb/BackedBlockFile.java

Lines changed: 0 additions & 26 deletions
This file was deleted.

app/src/main/java/ru/nikita/adb/BackedBlockFill.java

Lines changed: 0 additions & 23 deletions
This file was deleted.

app/src/main/java/ru/nikita/adb/FastbootActivity.java

Lines changed: 81 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import java.util.List;
44
import java.util.ArrayList;
55
import java.lang.Exception;
6-
import java.io.File;
7-
import java.io.FileInputStream;
6+
import java.io.RandomAccessFile;
7+
import java.io.IOException;
88
import java.io.FileNotFoundException;
99
import android.os.Bundle;
1010
import android.os.AsyncTask;
@@ -27,6 +27,7 @@
2727
import android.content.IntentFilter;
2828
import android.hardware.usb.UsbManager;
2929
import android.hardware.usb.UsbDevice;
30+
import android.util.Log;
3031
import ru.nikita.adb.FastbootVariablesListActivity;
3132
import ru.nikita.adb.PartitionListActivity;
3233
import ru.nikita.adb.FastbootDevice;
@@ -239,47 +240,99 @@ protected void onPreExecute() {
239240
protected void onProgressUpdate(Integer ... values) {
240241
pd.setProgress(values[0]);
241242
pd.setMax(values[1]);
243+
String message = getResources().getString(R.string.sending);
242244
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]));
246247
}
247248
@Override
248249
protected void onPostExecute(Exception result) {
249250
pd.dismiss();
250-
if(result != null)
251+
if(result != null) {
251252
showToast(result);
253+
result.printStackTrace();
254+
}
252255
}
253256

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+
264300
device.checkOkay();
265301
publishProgress(1, 1, part, maxParts);
266302
}
267303

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+
268315
@Override
269316
protected Exception doInBackground(Void ... args) {
270317
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+
}
283336
} catch(Exception e) {
284337
return e;
285338
}
@@ -294,7 +347,6 @@ protected Exception doInBackground(Void ... args) {
294347
private ProgressDialog pd;
295348
}
296349

297-
298350
public void flash(View view) {
299351
if(getSelectedDevice().openConnection(this)) {
300352
String filePath = ((EditText) findViewById(R.id.image_path)).getText().toString();

app/src/main/java/ru/nikita/adb/FastbootDevice.java

Lines changed: 56 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import java.util.Iterator;
55
import java.util.ArrayList;
66
import java.util.Arrays;
7+
import java.io.RandomAccessFile;
8+
import java.io.IOException;
9+
import java.nio.ByteBuffer;
710
import android.content.Context;
811
import android.content.Intent;
912
import android.content.IntentFilter;
@@ -15,6 +18,7 @@
1518
import android.hardware.usb.UsbInterface;
1619
import android.hardware.usb.UsbConstants;
1720
import android.hardware.usb.UsbDeviceConnection;
21+
import android.hardware.usb.UsbRequest;
1822
import android.util.Log;
1923
import ru.nikita.adb.FastbootVariable;
2024
import ru.nikita.adb.FastbootException;
@@ -61,23 +65,32 @@ public boolean openConnection(Context context) {
6165
}
6266

6367
private void rawCommand(String command) {
64-
byte[] bytes = command.getBytes();
65-
connection.bulkTransfer(out, bytes, bytes.length, 0);
68+
UsbRequest request = new UsbRequest();
69+
request.initialize(connection, out);
70+
request.queue(ByteBuffer.allocate(command.length()).put(command.getBytes()), command.length());
71+
connection.requestWait();
72+
request.close();
6673
}
6774

6875
private String readOnce() {
69-
byte[] bytes = new byte[1024];
70-
int length = connection.bulkTransfer(in, bytes, bytes.length, 0);
71-
String response = new String(bytes, 0, length);
72-
if(response.startsWith("FAIL"))
73-
throw new FastbootException(response);
74-
return response;
76+
int bufferMaxLength = in.getMaxPacketSize();
77+
ByteBuffer buffer = ByteBuffer.allocate(bufferMaxLength);
78+
UsbRequest request = new UsbRequest();
79+
request.initialize(connection, in);
80+
request.queue(buffer, bufferMaxLength);
81+
connection.requestWait();
82+
request.close();
83+
84+
Log.v("ADB", String.format("Buffer position: %d, size: %d", buffer.position(), buffer.limit()));
85+
86+
String string = new String(buffer.array(), 0, buffer.position());
87+
if(string.startsWith("FAIL"))
88+
throw new FastbootException(string);
89+
return string;
7590
}
7691

7792
private String readOkay() {
78-
byte[] bytes = new byte[1024];
79-
int length = connection.bulkTransfer(in, bytes, bytes.length, 0);
80-
String response = new String(bytes, 0, length);
93+
String response = readOnce();
8194
if(!response.startsWith("OKAY"))
8295
throw new FastbootException(response);
8396
return response.substring(4);
@@ -114,9 +127,9 @@ public String getVariable(String name) {
114127
return readOkay();
115128
}
116129

117-
public long getSparseLimit() {
130+
public int getSparseLimit() {
118131
String str = getVariable("max-download-size");
119-
return Long.decode(str);
132+
return Integer.decode(str);
120133
}
121134

122135
public boolean hasVbmetaPartiton() {
@@ -141,25 +154,47 @@ public FastbootVariable[] getAllVariables() {
141154
return list.toArray(new FastbootVariable[0]);
142155
}
143156

144-
public void downloadCommand(int length) {
157+
public void downloadCommand(long length) {
145158
rawCommand(String.format("download:%08x", length));
146159
String response = readOnce();
147160
if(!response.startsWith("DATA") || Integer.parseInt(response.substring(4), 16) != length)
148161
throw new FastbootException("Invalid response");
149162
}
150163

151-
public int writeData(byte[] data, int offset) {
152-
int size = Math.min(data.length - offset, out.getMaxPacketSize());
153-
int bytesWritten = connection.bulkTransfer(out, Arrays.copyOfRange(data, offset, offset + size), size, 0);
154-
offset += bytesWritten;
155-
return offset;
156-
}
157-
158164
public void flash(String partition) {
159165
rawCommand("flash:" + partition);
160166
readOnce();
161167
}
162168

169+
public class OutputStream {
170+
OutputStream() {
171+
request = new UsbRequest();
172+
request.initialize(connection, out);
173+
}
174+
public void close() {
175+
request.close();
176+
}
177+
public int write(byte[] data) {
178+
if(!request.queue(ByteBuffer.allocate(data.length).put(data), data.length))
179+
throw new FastbootException("Writing error");
180+
if(connection.requestWait() == null)
181+
throw new FastbootException("Writing error");
182+
return data.length;
183+
}
184+
public int write(byte[] array, int offset) throws IOException {
185+
int size = Math.min((int) array.length - offset, out.getMaxPacketSize());
186+
byte[] data = new byte[size];
187+
return write(data);
188+
}
189+
public int write(RandomAccessFile file, int count, int offset) throws IOException {
190+
int size = Math.min((int) file.length() - offset, out.getMaxPacketSize());
191+
byte[] data = new byte[size];
192+
file.read(data, 0, size);
193+
return write(data);
194+
}
195+
private UsbRequest request;
196+
}
197+
163198
private UsbManager manager;
164199
private UsbDevice device;
165200
private UsbInterface iface;

0 commit comments

Comments
 (0)