Refactor Tools.java

This commit is contained in:
SerpentSpirale 2022-02-26 17:35:51 +01:00 committed by ArtDev
parent 90d1bde4e6
commit ed68aca2a3

View File

@ -435,42 +435,37 @@ public final class Tools {
private static void showError(final Context ctx, final int titleId, final Throwable e, final boolean exitIfOk, final boolean showMore) { private static void showError(final Context ctx, final int titleId, final Throwable e, final boolean exitIfOk, final boolean showMore) {
e.printStackTrace(); e.printStackTrace();
Runnable runnable = new Runnable(){ Runnable runnable = () -> {
final String errMsg = showMore ? Log.getStackTraceString(e): e.getMessage();
@Override AlertDialog.Builder builder = new AlertDialog.Builder((Context) ctx)
public void run() .setTitle(titleId)
{ .setMessage(errMsg)
final String errMsg = showMore ? Log.getStackTraceString(e): e.getMessage(); .setPositiveButton(android.R.string.ok, (DialogInterface.OnClickListener) (p1, p2) -> {
AlertDialog.Builder builder = new AlertDialog.Builder((Context) ctx) if(exitIfOk) {
.setTitle(titleId) if (ctx instanceof BaseMainActivity) {
.setMessage(errMsg) BaseMainActivity.fullyExit();
.setPositiveButton(android.R.string.ok, (DialogInterface.OnClickListener) (p1, p2) -> { } else if (ctx instanceof Activity) {
if(exitIfOk) { ((Activity) ctx).finish();
if (ctx instanceof BaseMainActivity) {
BaseMainActivity.fullyExit();
} else if (ctx instanceof Activity) {
((Activity) ctx).finish();
}
} }
}) }
.setNegativeButton(showMore ? R.string.error_show_less : R.string.error_show_more, (DialogInterface.OnClickListener) (p1, p2) -> showError(ctx, titleId, e, exitIfOk, !showMore)) })
.setNeutralButton(android.R.string.copy, (DialogInterface.OnClickListener) (p1, p2) -> { .setNegativeButton(showMore ? R.string.error_show_less : R.string.error_show_more, (DialogInterface.OnClickListener) (p1, p2) -> showError(ctx, titleId, e, exitIfOk, !showMore))
ClipboardManager mgr = (ClipboardManager) ctx.getSystemService(Context.CLIPBOARD_SERVICE); .setNeutralButton(android.R.string.copy, (DialogInterface.OnClickListener) (p1, p2) -> {
mgr.setPrimaryClip(ClipData.newPlainText("error", Log.getStackTraceString(e))); ClipboardManager mgr = (ClipboardManager) ctx.getSystemService(Context.CLIPBOARD_SERVICE);
if(exitIfOk) { mgr.setPrimaryClip(ClipData.newPlainText("error", Log.getStackTraceString(e)));
if (ctx instanceof BaseMainActivity) { if(exitIfOk) {
BaseMainActivity.fullyExit(); if (ctx instanceof BaseMainActivity) {
} else { BaseMainActivity.fullyExit();
((Activity) ctx).finish(); } else {
} ((Activity) ctx).finish();
} }
}) }
.setCancelable(!exitIfOk); })
try { .setCancelable(!exitIfOk);
builder.show(); try {
} catch (Throwable th) { builder.show();
th.printStackTrace(); } catch (Throwable th) {
} th.printStackTrace();
} }
}; };
@ -481,8 +476,8 @@ public final class Tools {
} }
} }
public static void dialogOnUiThread(final Activity ctx, final CharSequence title, final CharSequence message) { public static void dialogOnUiThread(final Activity activity, final CharSequence title, final CharSequence message) {
ctx.runOnUiThread(() -> new AlertDialog.Builder(ctx) activity.runOnUiThread(() -> new AlertDialog.Builder(activity)
.setTitle(title) .setTitle(title)
.setMessage(message) .setMessage(message)
.setPositiveButton(android.R.string.ok, null) .setPositiveButton(android.R.string.ok, null)
@ -650,23 +645,19 @@ public final class Tools {
} }
public static String convertStream(InputStream inputStream, Charset charset) throws IOException { public static String convertStream(InputStream inputStream, Charset charset) throws IOException {
String out = ""; StringBuilder out = new StringBuilder();
int len; int len;
byte[] buf = new byte[512]; byte[] buf = new byte[512];
while((len = inputStream.read(buf))!=-1) { while((len = inputStream.read(buf))!=-1) {
out += new String(buf,0,len,charset); out.append(new String(buf, 0, len, charset));
} }
return out; return out.toString();
} }
public static File lastFileModified(String dir) { public static File lastFileModified(String dir) {
File fl = new File(dir); File fl = new File(dir);
File[] files = fl.listFiles(new FileFilter() { File[] files = fl.listFiles(File::isFile);
public boolean accept(File file) {
return file.isFile();
}
});
long lastMod = Long.MIN_VALUE; long lastMod = Long.MIN_VALUE;
File choice = null; File choice = null;
@ -682,13 +673,13 @@ public final class Tools {
public static String read(InputStream is) throws IOException { public static String read(InputStream is) throws IOException {
String out = ""; StringBuilder out = new StringBuilder();
int len; int len;
byte[] buf = new byte[512]; byte[] buf = new byte[512];
while((len = is.read(buf))!=-1) { while((len = is.read(buf))!=-1) {
out += new String(buf,0,len); out.append(new String(buf, 0, len));
} }
return out; return out.toString();
} }
public static String read(String path) throws IOException { public static String read(String path) throws IOException {
@ -734,6 +725,7 @@ public final class Tools {
public abstract static class DownloaderFeedback { public abstract static class DownloaderFeedback {
public abstract void updateProgress(int curr, int max); public abstract void updateProgress(int curr, int max);
} }
public static void downloadFileMonitored(String urlInput,String nameOutput, DownloaderFeedback monitor) throws IOException { public static void downloadFileMonitored(String urlInput,String nameOutput, DownloaderFeedback monitor) throws IOException {
File nameOutputFile = new File(nameOutput); File nameOutputFile = new File(nameOutput);
if (!nameOutputFile.exists()) { if (!nameOutputFile.exists()) {
@ -770,75 +762,6 @@ public final class Tools {
return true; return true;
} }
} }
public static class ZipTool
{
private ZipTool(){}
public static void zip(List<File> files, File zipFile) throws IOException {
final int BUFFER_SIZE = 2048;
BufferedInputStream origin = null;
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile)));
try {
byte data[] = new byte[BUFFER_SIZE];
for (File file : files) {
FileInputStream fileInputStream = new FileInputStream( file );
origin = new BufferedInputStream(fileInputStream, BUFFER_SIZE);
try {
ZipEntry entry = new ZipEntry(file.getName());
out.putNextEntry(entry);
int count;
while ((count = origin.read(data, 0, BUFFER_SIZE)) != -1) {
out.write(data, 0, count);
}
}
finally {
origin.close();
}
}
} finally {
out.close();
}
}
public static void unzip(File zipFile, File targetDirectory) throws IOException {
final int BUFFER_SIZE = 1024;
ZipInputStream zis = new ZipInputStream(
new BufferedInputStream(new FileInputStream(zipFile)));
try {
ZipEntry ze;
int count;
byte[] buffer = new byte[BUFFER_SIZE];
while ((ze = zis.getNextEntry()) != null) {
File file = new File(targetDirectory, ze.getName());
File dir = ze.isDirectory() ? file : file.getParentFile();
if (!dir.isDirectory() && !dir.mkdirs())
throw new FileNotFoundException("Failed to ensure directory: " +
dir.getAbsolutePath());
if (ze.isDirectory())
continue;
FileOutputStream fout = new FileOutputStream(file);
try {
while ((count = zis.read(buffer)) != -1)
fout.write(buffer, 0, count);
} finally {
fout.close();
}
/* if time should be restored as well
long time = ze.getTime();
if (time > 0)
file.setLastModified(time);
*/
}
} finally {
zis.close();
}
}
}
public static void ignoreNotch(boolean shouldIgnore, Activity ctx){ public static void ignoreNotch(boolean shouldIgnore, Activity ctx){
if (SDK_INT >= P) { if (SDK_INT >= P) {