mirror of
https://github.com/Team4388/RidgeScout.git
synced 2026-06-09 08:38:03 -06:00
Fix #16
This commit is contained in:
@@ -2,6 +2,7 @@ package com.ridgebotics.ridgescout.utility;
|
||||
|
||||
import com.ridgebotics.ridgescout.scoutingData.Fields;
|
||||
import com.ridgebotics.ridgescout.scoutingData.transfer.TransferType;
|
||||
import com.ridgebotics.ridgescout.types.ColabArray;
|
||||
import com.ridgebotics.ridgescout.types.frcEvent;
|
||||
import com.ridgebotics.ridgescout.types.input.FieldType;
|
||||
|
||||
@@ -62,34 +63,27 @@ public class DataManager {
|
||||
}
|
||||
}
|
||||
|
||||
public static List<String> rescout_list = new ArrayList<>();
|
||||
public static ColabArray rescout_list = new ColabArray();
|
||||
public static void reload_rescout_list(){
|
||||
if(!FileEditor.fileExist(evcode + ".rescout")) {rescout_list = new ArrayList<>(); return;}
|
||||
byte[] file = FileEditor.readFile(evcode + ".rescout");
|
||||
if(file == null) {rescout_list = new ArrayList<>(); return;}
|
||||
String filename = evcode + ".rescout";
|
||||
if(!FileEditor.fileExist(filename)) {rescout_list = new ColabArray(); return;}
|
||||
byte[] file = FileEditor.readFile(filename);
|
||||
if(file == null) {rescout_list = new ColabArray(); return;}
|
||||
|
||||
try {
|
||||
BuiltByteParser bbp = new BuiltByteParser(file);
|
||||
rescout_list = new ArrayList<>(Arrays.asList((String[]) (bbp.parse().get(0).get())));
|
||||
|
||||
rescout_list = ColabArray.decode(file);
|
||||
} catch (Exception e){
|
||||
AlertManager.error("Error loading scout fields", e);
|
||||
rescout_list = new ArrayList<>();
|
||||
AlertManager.error("Error loading rescouting list", e);
|
||||
rescout_list = new ColabArray();
|
||||
}
|
||||
}
|
||||
|
||||
public static void save_rescout_list() {
|
||||
String filename = evcode + ".rescout";
|
||||
try {
|
||||
if(rescout_list.size() == 0){
|
||||
FileEditor.deleteFile(evcode + ".rescout");
|
||||
return;
|
||||
}
|
||||
|
||||
ByteBuilder bb = new ByteBuilder();
|
||||
bb.addStringArray(rescout_list.toArray(new String[0]));
|
||||
FileEditor.writeFile(evcode + ".rescout", bb.build());
|
||||
FileEditor.writeFile(filename, rescout_list.encode());
|
||||
} catch (Exception e){
|
||||
AlertManager.error("Error saving scout fields", e);
|
||||
AlertManager.error("Error saving rescouting list", e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,7 +100,7 @@ public class DataManager {
|
||||
|
||||
} catch (Exception e){
|
||||
AlertManager.error("Error loading scout notice", e);
|
||||
rescout_list = new ArrayList<>();
|
||||
scoutNotice = "";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ import static com.ridgebotics.ridgescout.utility.DataManager.pit_values;
|
||||
import android.content.Context;
|
||||
|
||||
import com.ridgebotics.ridgescout.scoutingData.ScoutingDataWriter;
|
||||
import com.ridgebotics.ridgescout.types.ColabArray;
|
||||
import com.ridgebotics.ridgescout.types.frcEvent;
|
||||
import com.ridgebotics.ridgescout.types.frcTeam;
|
||||
|
||||
@@ -239,16 +240,19 @@ public final class FileEditor {
|
||||
// }
|
||||
|
||||
|
||||
|
||||
public static boolean writeFile(String filepath, byte[] data) {
|
||||
return writeFile(new File(baseDir + filepath), data);
|
||||
}
|
||||
|
||||
public static boolean writeFile(File file, byte[] data) {
|
||||
try {
|
||||
FileOutputStream output = new FileOutputStream(baseDir + filepath);
|
||||
FileOutputStream output = new FileOutputStream(file.getPath());
|
||||
output.write(data);
|
||||
output.close();
|
||||
|
||||
// Date d = new Date();
|
||||
|
||||
new File(baseDir + filepath).setLastModified(new Date().getTime());
|
||||
file.setLastModified(new Date().getTime());
|
||||
return true;
|
||||
}
|
||||
catch (IOException e) {
|
||||
@@ -285,10 +289,15 @@ public final class FileEditor {
|
||||
}
|
||||
|
||||
public static byte[] readFile(String path){
|
||||
return readFileExact(baseDir + path);
|
||||
return readFileExact(new File(baseDir + path));
|
||||
}
|
||||
public static byte[] readFileExact(String path){
|
||||
File file = new File(path);
|
||||
|
||||
public static byte[] readFile(File path){
|
||||
return readFileExact(path);
|
||||
}
|
||||
|
||||
|
||||
public static byte[] readFileExact(File file){
|
||||
int size = (int) file.length();
|
||||
byte[] bytes = new byte[size];
|
||||
try {
|
||||
@@ -296,9 +305,6 @@ public final class FileEditor {
|
||||
buf.read(bytes, 0, bytes.length);
|
||||
buf.close();
|
||||
return bytes;
|
||||
} catch (FileNotFoundException e) {
|
||||
AlertManager.error(e);
|
||||
return null;
|
||||
} catch (IOException e) {
|
||||
AlertManager.error(e);
|
||||
return null;
|
||||
@@ -480,5 +486,37 @@ public final class FileEditor {
|
||||
}
|
||||
return removeFiles;
|
||||
}
|
||||
|
||||
public static boolean requiresSpecialInteraction(String name) {
|
||||
// String name = file.getName();
|
||||
|
||||
if(!fileExist(name)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if(name.endsWith(".rescout")) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void syncColabArray(String filename, byte[] currentBytes, byte[] newBytes) {
|
||||
if(!fileExist(filename)) {
|
||||
return;
|
||||
}
|
||||
|
||||
try{
|
||||
if(filename.endsWith(".rescout")) {
|
||||
ColabArray colabArrayCurrent = ColabArray.decode(currentBytes);
|
||||
ColabArray colabArrayNew = ColabArray.decode(newBytes);
|
||||
|
||||
colabArrayCurrent.append(colabArrayNew);
|
||||
writeFile(filename, colabArrayCurrent.encode());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
AlertManager.error("Failed to sync ColabArray!", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,11 +8,12 @@ import java.net.URL;
|
||||
public class HttpGetFile extends AsyncTask<Void, Integer, File> {
|
||||
|
||||
public interface DownloadCallback {
|
||||
void onResult(String error);
|
||||
void onResult(ByteArrayOutputStream bytes, String error);
|
||||
}
|
||||
|
||||
private String downloadUrl;
|
||||
private File destinationFile;
|
||||
private ByteArrayOutputStream outputStream;
|
||||
private DownloadCallback callback;
|
||||
private String errorMessage;
|
||||
public HttpGetFile(String downloadUrl, File destinationFile, DownloadCallback callback) {
|
||||
@@ -23,9 +24,12 @@ public class HttpGetFile extends AsyncTask<Void, Integer, File> {
|
||||
|
||||
@Override
|
||||
protected File doInBackground(Void... voids) {
|
||||
return run();
|
||||
}
|
||||
|
||||
public File run() {
|
||||
HttpURLConnection connection = null;
|
||||
InputStream inputStream = null;
|
||||
FileOutputStream outputStream = null;
|
||||
|
||||
try {
|
||||
URL url = new URL(downloadUrl);
|
||||
@@ -64,7 +68,7 @@ public class HttpGetFile extends AsyncTask<Void, Integer, File> {
|
||||
}
|
||||
}
|
||||
|
||||
outputStream = new FileOutputStream(destinationFile);
|
||||
outputStream = new ByteArrayOutputStream();
|
||||
|
||||
byte[] buffer = new byte[8192];
|
||||
long downloadedBytes = 0;
|
||||
@@ -87,6 +91,8 @@ public class HttpGetFile extends AsyncTask<Void, Integer, File> {
|
||||
}
|
||||
|
||||
outputStream.flush();
|
||||
|
||||
// FileEditor.writeFile(destinationFile, outputStream.toByteArray());
|
||||
// Log.d(TAG, "Download successful. File saved to: " + destinationFile.getAbsolutePath());
|
||||
return destinationFile;
|
||||
|
||||
@@ -104,7 +110,7 @@ public class HttpGetFile extends AsyncTask<Void, Integer, File> {
|
||||
@Override
|
||||
protected void onPostExecute(File result) {
|
||||
if (callback != null) {
|
||||
callback.onResult(errorMessage);
|
||||
callback.onResult(outputStream, errorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -112,7 +118,7 @@ public class HttpGetFile extends AsyncTask<Void, Integer, File> {
|
||||
protected void onCancelled() {
|
||||
deletePartialFile();
|
||||
if (callback != null) {
|
||||
callback.onResult("Download cancelled");
|
||||
callback.onResult(null, "Download cancelled");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
package com.ridgebotics.ridgescout.utility;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.os.AsyncTask;
|
||||
//import android.util.Log;
|
||||
import java.io.*;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
public class HttpPutFile extends AsyncTask<Void, Integer, Boolean> {
|
||||
|
||||
@@ -27,8 +29,13 @@ public class HttpPutFile extends AsyncTask<Void, Integer, Boolean> {
|
||||
this.headers = headers;
|
||||
}
|
||||
|
||||
@SuppressLint("WrongThread")
|
||||
@Override
|
||||
protected Boolean doInBackground(Void... voids) {
|
||||
return run();
|
||||
}
|
||||
|
||||
public boolean run() {
|
||||
HttpURLConnection connection = null;
|
||||
InputStream fileInputStream = null;
|
||||
OutputStream outputStream = null;
|
||||
@@ -49,8 +56,8 @@ public class HttpPutFile extends AsyncTask<Void, Integer, Boolean> {
|
||||
connection.setUseCaches(false);
|
||||
connection.setRequestProperty("Content-Type", "application/octet-stream");
|
||||
connection.setRequestProperty("Content-Length", String.valueOf(fileToUpload.length()));
|
||||
connection.setConnectTimeout(30000); // 30 seconds
|
||||
connection.setReadTimeout(60000); // 60 seconds
|
||||
connection.setConnectTimeout(5000); // 5 seconds
|
||||
connection.setReadTimeout(10000); // 10 seconds
|
||||
|
||||
for(int i = 0; i < headers.length; i++){
|
||||
String[] split = headers[i].split(": ");
|
||||
|
||||
Reference in New Issue
Block a user