refactoring
This commit is contained in:
parent
e77e7bd7d2
commit
a579fa75a5
|
@ -0,0 +1,103 @@
|
||||||
|
package com.rusefi.app;
|
||||||
|
|
||||||
|
import android.content.ContextWrapper;
|
||||||
|
import android.widget.TextView;
|
||||||
|
|
||||||
|
import com.rusefi.Listener;
|
||||||
|
import com.rusefi.dfu.DfuImage;
|
||||||
|
import com.rusefi.shared.ConnectionAndMeta;
|
||||||
|
import com.rusefi.shared.FileUtil;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.security.KeyManagementException;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
|
||||||
|
public class DfuUpload {
|
||||||
|
private static final String BUNDLE_FILE = "rusefi_bundle_autoupdate.zip";
|
||||||
|
//public static final String FILE = "rusefi_bundle_mre_f4_autoupdate.zip";
|
||||||
|
//private static final String DFU_FILE_NAME = "rusefi_mre_f4.dfu";
|
||||||
|
private static final String DFU_FILE_NAME = "rusefi_.dfu";
|
||||||
|
|
||||||
|
public final String localDfuImageFileName;
|
||||||
|
public final String localFullFile;
|
||||||
|
public final File localFolder;
|
||||||
|
|
||||||
|
public DfuUpload(ContextWrapper context) {
|
||||||
|
localFolder = context.getExternalFilesDir(null);
|
||||||
|
localFullFile = localFolder + File.separator + BUNDLE_FILE;
|
||||||
|
localDfuImageFileName = localFolder + File.separator + DFU_FILE_NAME;
|
||||||
|
}
|
||||||
|
|
||||||
|
void fileOperation(final TextView mResultView) {
|
||||||
|
if (new File(this.localFullFile).exists()) {
|
||||||
|
mResultView.append(this.BUNDLE_FILE + " found!\n");
|
||||||
|
uncompressFile(this.localFullFile, this.localFolder, this.localDfuImageFileName, mResultView);
|
||||||
|
} else {
|
||||||
|
mResultView.append(this.BUNDLE_FILE + " not found!\n");
|
||||||
|
|
||||||
|
new Thread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
ConnectionAndMeta c = new ConnectionAndMeta(BUNDLE_FILE).invoke();
|
||||||
|
ConnectionAndMeta.downloadFile(localFullFile, c, new ConnectionAndMeta.DownloadProgressListener() {
|
||||||
|
@Override
|
||||||
|
public void onPercentage(final int currentProgress) {
|
||||||
|
mResultView.post(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
mResultView.append("Downloading " + currentProgress + "\n");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
mResultView.post(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
mResultView.append("Downloaded! " + "\n");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
uncompressFile(localFullFile, localFolder, localDfuImageFileName, mResultView);
|
||||||
|
|
||||||
|
} catch (IOException | KeyManagementException | NoSuchAlgorithmException e) {
|
||||||
|
mResultView.post(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
mResultView.append("Error downloading " + e + "\n");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}).start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void uncompressFile(final String localFullFile, final File localFolder, final String localDfuImageFileName, final TextView mResultView) {
|
||||||
|
final Listener<Integer> onSuccess = new Listener<Integer>() {
|
||||||
|
@Override
|
||||||
|
public void onResult(final Integer size) {
|
||||||
|
mResultView.post(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
mResultView.append(localDfuImageFileName + " File size: " + size + "\n");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
DfuImage dfuImage = new DfuImage();
|
||||||
|
dfuImage.read(localDfuImageFileName);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
new Thread(() -> {
|
||||||
|
try {
|
||||||
|
FileUtil.unzip(localFullFile, localFolder);
|
||||||
|
final int size = (int) new File(localDfuImageFileName).length();
|
||||||
|
onSuccess.onResult(size);
|
||||||
|
|
||||||
|
} catch (final IOException e) {
|
||||||
|
mResultView.post(() -> mResultView.append("Error uncompressing " + e + "\n"));
|
||||||
|
}
|
||||||
|
|
||||||
|
}).start();
|
||||||
|
}
|
||||||
|
}
|
|
@ -34,29 +34,20 @@ import android.widget.TextView;
|
||||||
|
|
||||||
import com.hoho.android.usbserial.driver.UsbSerialDriver;
|
import com.hoho.android.usbserial.driver.UsbSerialDriver;
|
||||||
import com.hoho.android.usbserial.driver.UsbSerialPort;
|
import com.hoho.android.usbserial.driver.UsbSerialPort;
|
||||||
import com.rusefi.Listener;
|
import com.rusefi.app.serial.AndroidSerial;
|
||||||
import com.rusefi.dfu.DfuConnection;
|
import com.rusefi.dfu.DfuConnection;
|
||||||
import com.rusefi.dfu.DfuImage;
|
import com.rusefi.dfu.DfuImage;
|
||||||
import com.rusefi.dfu.DfuLogic;
|
import com.rusefi.dfu.DfuLogic;
|
||||||
import com.rusefi.dfu.android.AndroidDfuConnection;
|
import com.rusefi.dfu.android.AndroidDfuConnection;
|
||||||
import com.rusefi.dfu.android.DfuDeviceLocator;
|
import com.rusefi.dfu.android.DfuDeviceLocator;
|
||||||
import com.rusefi.io.DfuHelper;
|
import com.rusefi.io.DfuHelper;
|
||||||
import com.rusefi.shared.ConnectionAndMeta;
|
|
||||||
import com.rusefi.shared.FileUtil;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.security.KeyManagementException;
|
|
||||||
import java.security.NoSuchAlgorithmException;
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class rusEFI extends Activity {
|
public class rusEFI extends Activity {
|
||||||
private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
|
private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
|
||||||
//public static final String FILE = "rusefi_bundle_mre_f4_autoupdate.zip";
|
|
||||||
//private static final String DFU_FILE_NAME = "rusefi_mre_f4.dfu";
|
|
||||||
private static final String BUNDLE_FILE = "rusefi_bundle_autoupdate.zip";
|
|
||||||
private static final String DFU_FILE_NAME = "rusefi_.dfu";
|
|
||||||
|
|
||||||
private static final byte REQUEST_TYPE_CLASS = 32;
|
private static final byte REQUEST_TYPE_CLASS = 32;
|
||||||
private static final byte RECIPIENT_INTERFACE = 0x01;
|
private static final byte RECIPIENT_INTERFACE = 0x01;
|
||||||
|
@ -69,7 +60,7 @@ public class rusEFI extends Activity {
|
||||||
private TextView mResultView;
|
private TextView mResultView;
|
||||||
|
|
||||||
private UsbManager usbManager;
|
private UsbManager usbManager;
|
||||||
private String localDfuImageFileName;
|
private DfuUpload dfuUpload;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
|
@ -89,53 +80,9 @@ public class rusEFI extends Activity {
|
||||||
|
|
||||||
mStatusView.setText("Hello");
|
mStatusView.setText("Hello");
|
||||||
|
|
||||||
|
dfuUpload = new DfuUpload(this);
|
||||||
|
|
||||||
final File localFolder = getExternalFilesDir(null);
|
dfuUpload.fileOperation(mResultView);
|
||||||
final String localFullFile = localFolder + File.separator + BUNDLE_FILE;
|
|
||||||
localDfuImageFileName = localFolder + File.separator + DFU_FILE_NAME;
|
|
||||||
|
|
||||||
if (new File(localFullFile).exists()) {
|
|
||||||
mResultView.append(BUNDLE_FILE + " found!\n");
|
|
||||||
uncompressFile(localFullFile, localFolder, localDfuImageFileName);
|
|
||||||
} else {
|
|
||||||
mResultView.append(BUNDLE_FILE + " not found!\n");
|
|
||||||
|
|
||||||
new Thread(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
try {
|
|
||||||
ConnectionAndMeta c = new ConnectionAndMeta(BUNDLE_FILE).invoke();
|
|
||||||
ConnectionAndMeta.downloadFile(localFullFile, c, new ConnectionAndMeta.DownloadProgressListener() {
|
|
||||||
@Override
|
|
||||||
public void onPercentage(final int currentProgress) {
|
|
||||||
mResultView.post(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
mResultView.append("Downloading " + currentProgress + "\n");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
mResultView.post(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
mResultView.append("Downloaded! " + "\n");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
uncompressFile(localFullFile, localFolder, localDfuImageFileName);
|
|
||||||
|
|
||||||
} catch (IOException | KeyManagementException | NoSuchAlgorithmException e) {
|
|
||||||
mResultView.post(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
mResultView.append("Error downloading " + e + "\n");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}).start();
|
|
||||||
}
|
|
||||||
|
|
||||||
handleButton();
|
handleButton();
|
||||||
}
|
}
|
||||||
|
@ -149,34 +96,6 @@ public class rusEFI extends Activity {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void uncompressFile(final String localFullFile, final File localFolder, final String localDfuImageFileName) {
|
|
||||||
final Listener<Integer> onSuccess = new Listener<Integer>() {
|
|
||||||
@Override
|
|
||||||
public void onResult(final Integer size) {
|
|
||||||
mResultView.post(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
mResultView.append(localDfuImageFileName + " File size: " + size + "\n");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
DfuImage dfuImage = new DfuImage();
|
|
||||||
dfuImage.read(localDfuImageFileName);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
new Thread(() -> {
|
|
||||||
try {
|
|
||||||
FileUtil.unzip(localFullFile, localFolder);
|
|
||||||
final int size = (int) new File(localDfuImageFileName).length();
|
|
||||||
onSuccess.onResult(size);
|
|
||||||
|
|
||||||
} catch (final IOException e) {
|
|
||||||
mResultView.post(() -> mResultView.append("Error uncompressing " + e + "\n"));
|
|
||||||
}
|
|
||||||
|
|
||||||
}).start();
|
|
||||||
}
|
|
||||||
|
|
||||||
private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
|
private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
|
||||||
public void onReceive(Context context, Intent intent) {
|
public void onReceive(Context context, Intent intent) {
|
||||||
String action = intent.getAction();
|
String action = intent.getAction();
|
||||||
|
@ -184,7 +103,7 @@ public class rusEFI extends Activity {
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
|
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
|
||||||
UsbDevice dfuDevice = DfuDeviceLocator.findDevice(usbManager);
|
UsbDevice dfuDevice = DfuDeviceLocator.findDevice(usbManager);
|
||||||
dfuUpdate(dfuDevice);
|
dfuUpdate(dfuDevice, rusEFI.this.mResultView);
|
||||||
// if (device != null) {
|
// if (device != null) {
|
||||||
// //call method to set up device communication
|
// //call method to set up device communication
|
||||||
// }
|
// }
|
||||||
|
@ -243,19 +162,19 @@ public class rusEFI extends Activity {
|
||||||
|
|
||||||
private void handleDfuDevice(UsbDevice dfuDevice) {
|
private void handleDfuDevice(UsbDevice dfuDevice) {
|
||||||
if (usbManager.hasPermission(dfuDevice)) {
|
if (usbManager.hasPermission(dfuDevice)) {
|
||||||
dfuUpdate(dfuDevice);
|
dfuUpdate(dfuDevice, mResultView);
|
||||||
} else {
|
} else {
|
||||||
PendingIntent mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
|
PendingIntent mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
|
||||||
usbManager.requestPermission(dfuDevice, mPermissionIntent);
|
usbManager.requestPermission(dfuDevice, mPermissionIntent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void dfuUpdate(UsbDevice dfuDevice) {
|
private void dfuUpdate(UsbDevice dfuDevice, TextView mResultView) {
|
||||||
mStatusView.setText("rusEFI: DFU detected");
|
mStatusView.setText("rusEFI: DFU detected");
|
||||||
DfuDeviceLocator.Result dfu = new DfuDeviceLocator().openDfu(usbManager, dfuDevice);
|
DfuDeviceLocator.Result dfu = new DfuDeviceLocator().openDfu(usbManager, dfuDevice);
|
||||||
|
|
||||||
DfuImage dfuImage = new DfuImage();
|
DfuImage dfuImage = new DfuImage();
|
||||||
dfuImage.read(localDfuImageFileName);
|
dfuImage.read(dfuUpload.localDfuImageFileName);
|
||||||
mResultView.append("Image size " + dfuImage.getImageSize() + "\n");
|
mResultView.append("Image size " + dfuImage.getImageSize() + "\n");
|
||||||
|
|
||||||
DfuConnection connection = new AndroidDfuConnection(dfu.getConnection(), dfu.getInterfaceIndex(), dfu.getTransferSize(), dfu.getFlashRange());
|
DfuConnection connection = new AndroidDfuConnection(dfu.getConnection(), dfu.getInterfaceIndex(), dfu.getTransferSize(), dfu.getFlashRange());
|
||||||
|
@ -266,9 +185,8 @@ public class rusEFI extends Activity {
|
||||||
// mResultView.append("State " + state + "\n");
|
// mResultView.append("State " + state + "\n");
|
||||||
|
|
||||||
DfuLogic.uploadImage(logger, connection, dfuImage, dfu.getFlashRange());
|
DfuLogic.uploadImage(logger, connection, dfuImage, dfu.getFlashRange());
|
||||||
|
|
||||||
} catch (IllegalStateException e) {
|
} catch (IllegalStateException e) {
|
||||||
mResultView.append("Error " + e + "\n");
|
this.mResultView.append("Error " + e + "\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package com.rusefi.app;
|
package com.rusefi.app.serial;
|
||||||
|
|
||||||
import android.hardware.usb.UsbManager;
|
import android.hardware.usb.UsbManager;
|
||||||
|
|
||||||
|
@ -22,7 +22,7 @@ public class AndroidSerial extends AbstractIoStream {
|
||||||
|
|
||||||
private UsbSerialPort usbSerialPort;
|
private UsbSerialPort usbSerialPort;
|
||||||
|
|
||||||
static List<UsbSerialDriver> findUsbSerial(UsbManager usbManager) {
|
public static List<UsbSerialDriver> findUsbSerial(UsbManager usbManager) {
|
||||||
ProbeTable customTable = UsbSerialProber.getDefaultProbeTable();
|
ProbeTable customTable = UsbSerialProber.getDefaultProbeTable();
|
||||||
customTable.addProduct(DfuLogic.ST_VENDOR, ST_CDC, CdcAcmSerialDriver.class);
|
customTable.addProduct(DfuLogic.ST_VENDOR, ST_CDC, CdcAcmSerialDriver.class);
|
||||||
UsbSerialProber prober = new UsbSerialProber(customTable);
|
UsbSerialProber prober = new UsbSerialProber(customTable);
|
Loading…
Reference in New Issue