Make bluetooth work, Make alert() better, and more formal errors

This commit is contained in:
astatin3
2024-07-24 15:27:13 -06:00
parent d063319c84
commit 588a71763a
24 changed files with 342 additions and 195 deletions
@@ -0,0 +1,72 @@
package com.astatin3.scoutingapp2025.utility;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Looper;
import android.widget.Toast;
import java.io.PrintWriter;
import java.io.StringWriter;
public class AlertManager {
private static Context context;
public static void init(Context c){
context = c;
}
public static void alert(String title, String content) {
((Activity) context).runOnUiThread(new Runnable() {
public void run() {
AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setMessage(content);
alert.setTitle(title);
alert.setPositiveButton("OK", null);
alert.setCancelable(true);
alert.create().show();
}
});
}
public static void toast(String content) {
((Activity) context).runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(context, content, Toast.LENGTH_LONG).show();
}
});
}
public static void error(String content) {
((Activity) context).runOnUiThread(new Runnable() {
public void run() {
AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setMessage(content);
alert.setTitle("Error!");
alert.setPositiveButton("OK", null);
alert.setCancelable(true);
alert.create().show();
}
});
}
public static void error(Exception e) {
((Activity) context).runOnUiThread(new Runnable() {
public void run() {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setMessage(sw.toString());
alert.setTitle(e.getMessage());
alert.setPositiveButton("OK", null);
alert.setCancelable(true);
alert.create().show();
}
});
}
}
@@ -37,7 +37,7 @@ public class RequestTask extends AsyncTask<String, String, String> {
return null; // See documentation for more info on response handling
}
} catch (IOException e) {
e.printStackTrace();
AlertManager.error(e);
}
return null;
}
@@ -51,13 +51,13 @@ public class RequestTask extends AsyncTask<String, String, String> {
response.append(line);
}
} catch (IOException e) {
e.printStackTrace();
AlertManager.error(e);
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
AlertManager.error(e);
}
}
}
@@ -82,6 +82,8 @@ public final class fileEditor {
public static byte[] getByteBlock(byte[] bytes, int start, int end){
end = Math.min(end, bytes.length);
byte[] dataBlock = new byte[end-start];
for(int a=start;a<end;a++){
@@ -108,6 +110,44 @@ public final class fileEditor {
return outputStream.toByteArray();
}
public static byte[] blockCompress(byte[] inputData) {
List<byte[]> compiledData = new ArrayList<>();
for(int i=0;i<Math.ceil((double) inputData.length / fileEditor.maxCompressedBlockSize);i++){
final int start = i*fileEditor.maxCompressedBlockSize;
int end = ((i+1)*fileEditor.maxCompressedBlockSize);
if(end > inputData.length) {
end = inputData.length;
}
byte[] dataBlock = fileEditor.getByteBlock(inputData, start, end);
final byte[] compressedBlock = fileEditor.compress(dataBlock);
compiledData.add(fileEditor.toBytes(compressedBlock.length, 2));
compiledData.add(compressedBlock);
}
return combineByteArrays(compiledData);
}
public static byte[] blockUncompress(byte[] data) throws DataFormatException {
List<byte[]> uncompressedData = new ArrayList<>();
int curIndex = 0;
while (curIndex < data.length) {
final int blockLength = fileEditor.fromBytes(fileEditor.getByteBlock(data, curIndex, curIndex + 2), 2);
uncompressedData.add(
decompress(
fileEditor.getByteBlock(data, curIndex + 2, curIndex + blockLength + 2)
)
);
curIndex += blockLength + 2;
}
return combineByteArrays(uncompressedData);
}
public static byte[] decompress(byte[] input) throws DataFormatException {
Inflater inflater = new Inflater();
@@ -163,7 +203,7 @@ public final class fileEditor {
return true;
}
catch (IOException e) {
e.printStackTrace();
AlertManager.error(e);
return false;
}
}
@@ -177,7 +217,7 @@ public final class fileEditor {
return file.createNewFile();
}
catch (IOException e) {
e.printStackTrace();
AlertManager.error(e);
return false;
}
}
@@ -197,10 +237,10 @@ public final class fileEditor {
buf.close();
return bytes;
} catch (FileNotFoundException e) {
e.printStackTrace();
AlertManager.error(e);
return null;
} catch (IOException e) {
e.printStackTrace();
AlertManager.error(e);
return null;
}
}