Start using the fields in scouting

This commit is contained in:
Astatin3
2024-06-27 22:12:36 -06:00
parent d183e3c913
commit 46219e0a6a
16 changed files with 200 additions and 76 deletions
@@ -0,0 +1,75 @@
package com.astatin3.scoutingapp2025.utility;
import android.os.Handler;
import android.os.Looper;
public class AutoSaveManager {
private static final long AUTO_SAVE_DELAY = 5000; // 5 seconds
private final Handler handler;
private final Runnable autoSaveRunnable;
private boolean isAutoSaveScheduled = false;
private final AutoSaveFunction autoSaveFunction;
private boolean isRunning = false;
// Functional interface for the auto-save function
@FunctionalInterface
public interface AutoSaveFunction {
void save();
}
public AutoSaveManager(AutoSaveFunction autoSaveFunction) {
this.autoSaveFunction = autoSaveFunction;
handler = new Handler(Looper.getMainLooper());
autoSaveRunnable = new Runnable() {
@Override
public void run() {
performAutoSave();
isAutoSaveScheduled = false;
}
};
}
public void start() {
isRunning = true;
}
public void stop() {
isRunning = false;
handler.removeCallbacks(autoSaveRunnable);
isAutoSaveScheduled = false;
}
public void update() {
if (!isRunning) {
return; // Don't schedule auto-saves when not running
}
// Cancel any previously scheduled auto-save
handler.removeCallbacks(autoSaveRunnable);
// Schedule a new auto-save
handler.postDelayed(autoSaveRunnable, AUTO_SAVE_DELAY);
isAutoSaveScheduled = true;
}
private void performAutoSave() {
if (isRunning) {
// Call the provided auto-save function
autoSaveFunction.save();
}
}
public void onDestroy() {
// Remove any pending auto-save tasks when the activity or fragment is destroyed
stop();
}
public boolean isAutoSaveScheduled() {
return isAutoSaveScheduled;
}
public boolean isRunning() {
return isRunning;
}
}
@@ -0,0 +1,144 @@
package com.astatin3.scoutingapp2025.utility;
import com.astatin3.scoutingapp2025.fileEditor;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
public class BuiltByteParser {
public static final Integer intType = 0;
public static final Integer stringType = 1;
public static final Integer intArrayType = 2;
public static final Integer stringArrayType = 3;
public class byteParsingExeption extends Exception {
public byteParsingExeption() {}
public byteParsingExeption(String message) {
super(message);
}
}
public abstract class parsedObject {
public abstract Integer getType();
public abstract Object get();
}
public class intObject extends parsedObject{
int num;
public Integer getType(){return intType;}
public Object get(){return num;}
}
public class stringObject extends parsedObject{
String str;
public Integer getType(){return stringType;}
public Object get(){return str;}
}
public class intArrayObject extends parsedObject{
int[] arr;
public Integer getType(){return intArrayType;}
public Object get(){return arr;}
}
public class stringArrayObject extends parsedObject{
String[] arr;
public Integer getType(){return stringArrayType;}
public Object get(){return arr;}
}
public class rawObject extends parsedObject {
private int type;
public rawObject(int type){this.type = type;}
byte[] bytes;
public Integer getType(){return type;}
public Object get(){return bytes;}
}
byte[] bytes;
ArrayList<parsedObject> objects = new ArrayList<>();
public BuiltByteParser(byte[] bytes){
this.bytes = bytes;
}
public ArrayList<parsedObject> parse() throws byteParsingExeption {
if(bytes.length < 3){throw new byteParsingExeption("Invalid length");}
int curIndex = 0;
while(true){
// Log.i("t", String.valueOf(curIndex));
final int length = fileEditor.fromBytes(fileEditor.getByteBlock(bytes, curIndex, curIndex+2), 2);
final int type = bytes[curIndex+2] & 0xFF;
if(length == 0){
curIndex += 3;
continue;
}
final byte[] block;
try {
block = fileEditor.getByteBlock(bytes, curIndex + 3, curIndex + length + 3);
} catch(Exception e){
throw new byteParsingExeption("Array out of bounds");
}
switch(type){
case 0:
intObject io = new intObject();
io.num = fileEditor.fromBytes(block, length);
objects.add(io);
break;
case 1:
stringObject so = new stringObject();
so.str = new String(block, StandardCharsets.UTF_8);
objects.add(so);
break;
case 2:
BuiltByteParser int_bbp = new BuiltByteParser(block);
ArrayList<parsedObject> intArrayObjects = int_bbp.parse();
int[] intArr = new int[intArrayObjects.size()];
for(int i = 0; i < intArrayObjects.size(); i ++){
intArr[i] = (int) intArrayObjects.get(i).get();
}
intArrayObject ia = new intArrayObject();
ia.arr = intArr;
objects.add(ia);
break;
case 3:
BuiltByteParser str_bbp = new BuiltByteParser(block);
ArrayList<parsedObject> strArrayObjects = str_bbp.parse();
String[] StringArr = new String[strArrayObjects.size()];
for(int i = 0; i < strArrayObjects.size(); i ++){
StringArr[i] = (String) strArrayObjects.get(i).get();
}
stringArrayObject sa = new stringArrayObject();
sa.arr = StringArr;
objects.add(sa);
break;
default:
rawObject ro = new rawObject(type);
ro.bytes = block;
objects.add(ro);
break;
}
curIndex += length + 3;
if(curIndex == bytes.length){
break;
}else if(curIndex > bytes.length){
throw new byteParsingExeption("Block length problem");
}
}
return objects;
}
}
@@ -0,0 +1,190 @@
package com.astatin3.scoutingapp2025.utility;
import com.astatin3.scoutingapp2025.fileEditor;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
public class ByteBuilder {
public static final int int_id = 0;
public static final int string_id = 1;
public static final int int_arr_id = 2;
public static final int string_arr_id = 3;
ArrayList<byteType> bytesToBuild = new ArrayList<>();
public class buildingException extends Exception {
public buildingException() {}
public buildingException(String message) {
super(message);
}
}
private abstract static class byteType {
public abstract byte getType();
public abstract int length();
public abstract byte[] build();
}
private class intType extends byteType {
public int precision;
public int num;
public byte getType(){return int_id;}
public int length(){return precision;}
public byte[] build(){
return fileEditor.toBytes(num, precision);
}
}
private int getLeastBytePrecision(int num){
if(num <= 1){return 1;}
return (int) Math.ceil(Math.log(Math.abs(num))/Math.log(8));
}
public ByteBuilder addInt(int num) throws buildingException {
// Get closest number of bytes
int precision = getLeastBytePrecision(num);
return addInt(num, precision);
}
public ByteBuilder addInt(int num, int precision) throws buildingException {
if(precision <= 0){throw new buildingException("Invalid precision: " + precision);}
if(precision > 65536){throw new buildingException("Precision too large (greter than 65536)");}
if(precision < getLeastBytePrecision(num)){throw new buildingException("Precision too small");}
if(num > Integer.MAX_VALUE){throw new buildingException("Integer overflow");}
if(num < Integer.MIN_VALUE){throw new buildingException("Integer overflow");}
intType intType = new intType();
intType.num = num;
intType.precision = precision;
bytesToBuild.add(intType);
return this;
}
private class stringType extends byteType {
public byte[] bytes;
public byte getType(){return string_id;}
public int length(){return bytes.length;}
public byte[] build(){
return bytes;
// return str.getBytes(charset);
}
}
public ByteBuilder addString(String str) throws buildingException {
if(str.length() > 65536){throw new buildingException("String too long (greater than 65536)");}
stringType stringType = new stringType();
// To get the length correctly, the string bytes need to be precalculated
stringType.bytes = str.getBytes(StandardCharsets.UTF_8);
bytesToBuild.add(stringType);
return this;
}
private class intArrayType extends byteType {
public byte[] bytes;
public byte getType(){return int_arr_id;}
public int length(){return bytes.length;}
public byte[] build(){
return bytes;
// return str.getBytes(charset);
}
}
public ByteBuilder addIntArray(int[] arr) throws buildingException {
intArrayType intArrayType = new intArrayType();
ByteBuilder bb = new ByteBuilder();
for(int i = 0; i < arr.length; i++){
bb.addInt(arr[i]);
}
intArrayType.bytes = bb.build();
bytesToBuild.add(intArrayType);
return this;
}
private class stringArrayType extends byteType {
public byte[] bytes;
public byte getType(){return string_arr_id;}
public int length(){return bytes.length;}
public byte[] build(){
return bytes;
// return str.getBytes(charset);
}
}
public ByteBuilder addStringArray(String[] arr) throws buildingException {
stringArrayType stringArrayType = new stringArrayType();
ByteBuilder bb = new ByteBuilder();
for(int i = 0; i < arr.length; i++){
bb.addString(arr[i]);
}
stringArrayType.bytes = bb.build();
bytesToBuild.add(stringArrayType);
return this;
}
private class rawType extends byteType {
public int type;
public byte[] bytes;
public byte getType(){return (byte) type;}
public int length(){return bytes.length;}
public byte[] build(){
return bytes;
}
}
public ByteBuilder addRaw(int type, byte[] bytes) throws buildingException {
if(bytes.length > 65536){throw new buildingException("Byte array length to long (greater than 65536)");}
rawType rawType = new rawType();
rawType.type = type;
rawType.bytes = bytes;
bytesToBuild.add(rawType);
return this;
}
public byte[] build() throws buildingException {
if(bytesToBuild.size() == 0){throw new buildingException("Cannot build null data");}
int length = bytesToBuild.size() * 3;
for(byteType bt : bytesToBuild){
length += bt.length();
}
byte[] bytes = new byte[length];
int bytesFilled = 0;
for(byteType bt : bytesToBuild){
byte[] blockLength = fileEditor.toBytes(bt.length(), 2);
bytes[bytesFilled] = blockLength[0];
bytesFilled += 1;
bytes[bytesFilled] = blockLength[1];
bytesFilled += 1;
bytes[bytesFilled] = bt.getType();
bytesFilled += 1;
// if(bt.length == 0)
// continue;
int i = 1;
byte[] newBytes = bt.build();
for(byte b : newBytes){
// Log.i("i", (bytesFilled+1) + "/" + length + " (" + i + "/" + bt.length() + ")");
bytes[bytesFilled] = b;
bytesFilled += 1;
i++;
}
}
return bytes;
}
}
@@ -0,0 +1,24 @@
package com.astatin3.scoutingapp2025.utility;
import org.json.JSONArray;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class JSONUtil {
public static JSONArray sort(JSONArray array, Comparator c){
List asList = new ArrayList(array.length());
for (int i=0; i<array.length(); i++){
asList.add(array.opt(i));
}
asList.sort(c);
JSONArray res = new JSONArray();
for (Object o : asList){
res.put(o);
}
return res;
}
}
@@ -0,0 +1,78 @@
package com.astatin3.scoutingapp2025.utility;
import android.os.AsyncTask;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.function.Function;
import javax.net.ssl.HttpsURLConnection;
public class RequestTask extends AsyncTask<String, String, String> {
private Function<String, String> resultFunction = null;
@Override
protected String doInBackground(String... uri) {
try {
URL url = new URL(uri[0]);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
String[] headers = uri[1].split(", ");
for(String header : headers){
String[] split = header.split(": ");
conn.setRequestProperty(split[0], split[1]);
}
if(conn.getResponseCode() == HttpsURLConnection.HTTP_OK){
// ByteArrayOutputStream out = new ByteArrayOutputStream();
return readStream(conn.getInputStream());
// Do normal input or output stream reading
}
else {
return null; // See documentation for more info on response handling
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private static String readStream(InputStream in) {
BufferedReader reader = null;
StringBuilder response = new StringBuilder();
try {
reader = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return response.toString();
}
public void onResult(Function<String, String> func) {
this.resultFunction = func;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if(resultFunction != null){
resultFunction.apply(result);
}
}
}