From 51888a6853cf6517de45dda090f11572f1eb0e8d Mon Sep 17 00:00:00 2001 From: Anuken Date: Mon, 18 Mar 2024 10:00:15 -0400 Subject: [PATCH] Don't crash with file chooser errors --- .../mindustry/android/AndroidLauncher.java | 98 ++++++++++--------- 1 file changed, 51 insertions(+), 47 deletions(-) diff --git a/android/src/mindustry/android/AndroidLauncher.java b/android/src/mindustry/android/AndroidLauncher.java index da3944fe06..7febad64a7 100644 --- a/android/src/mindustry/android/AndroidLauncher.java +++ b/android/src/mindustry/android/AndroidLauncher.java @@ -101,64 +101,68 @@ public class AndroidLauncher extends AndroidApplication{ } void showFileChooser(boolean open, String title, Cons cons, String... extensions){ - String extension = extensions[0]; + try{ + String extension = extensions[0]; - if(VERSION.SDK_INT >= VERSION_CODES.Q){ - Intent intent = new Intent(open ? Intent.ACTION_OPEN_DOCUMENT : Intent.ACTION_CREATE_DOCUMENT); - intent.addCategory(Intent.CATEGORY_OPENABLE); - intent.setType(extension.equals("zip") && !open && extensions.length == 1 ? "application/zip" : "*/*"); + if(VERSION.SDK_INT >= VERSION_CODES.Q){ + Intent intent = new Intent(open ? Intent.ACTION_OPEN_DOCUMENT : Intent.ACTION_CREATE_DOCUMENT); + intent.addCategory(Intent.CATEGORY_OPENABLE); + intent.setType(extension.equals("zip") && !open && extensions.length == 1 ? "application/zip" : "*/*"); - addResultListener(i -> startActivityForResult(intent, i), (code, in) -> { - if(code == Activity.RESULT_OK && in != null && in.getData() != null){ - Uri uri = in.getData(); + addResultListener(i -> startActivityForResult(intent, i), (code, in) -> { + if(code == Activity.RESULT_OK && in != null && in.getData() != null){ + Uri uri = in.getData(); - if(uri.getPath().contains("(invalid)")) return; + if(uri.getPath().contains("(invalid)")) return; - Core.app.post(() -> Core.app.post(() -> cons.get(new Fi(uri.getPath()){ - @Override - public InputStream read(){ - try{ - return getContentResolver().openInputStream(uri); - }catch(IOException e){ - throw new ArcRuntimeException(e); + Core.app.post(() -> Core.app.post(() -> cons.get(new Fi(uri.getPath()){ + @Override + public InputStream read(){ + try{ + return getContentResolver().openInputStream(uri); + }catch(IOException e){ + throw new ArcRuntimeException(e); + } } - } - @Override - public OutputStream write(boolean append){ - try{ - return getContentResolver().openOutputStream(uri); - }catch(IOException e){ - throw new ArcRuntimeException(e); + @Override + public OutputStream write(boolean append){ + try{ + return getContentResolver().openOutputStream(uri); + }catch(IOException e){ + throw new ArcRuntimeException(e); + } } - } - }))); - } - }); - }else if(VERSION.SDK_INT >= VERSION_CODES.M && !(checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED && + }))); + } + }); + }else if(VERSION.SDK_INT >= VERSION_CODES.M && !(checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)){ - chooser = new FileChooser(title, file -> Structs.contains(extensions, file.extension().toLowerCase()), open, file -> { - if(!open){ - cons.get(file.parent().child(file.nameWithoutExtension() + "." + extension)); - }else{ - cons.get(file); - } - }); + chooser = new FileChooser(title, file -> Structs.contains(extensions, file.extension().toLowerCase()), open, file -> { + if(!open){ + cons.get(file.parent().child(file.nameWithoutExtension() + "." + extension)); + }else{ + cons.get(file); + } + }); - ArrayList perms = new ArrayList<>(); - if(checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){ - perms.add(Manifest.permission.WRITE_EXTERNAL_STORAGE); - } - if(checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){ - perms.add(Manifest.permission.READ_EXTERNAL_STORAGE); - } - requestPermissions(perms.toArray(new String[0]), PERMISSION_REQUEST_CODE); - }else{ - if(open){ - new FileChooser(title, file -> Structs.contains(extensions, file.extension().toLowerCase()), true, cons).show(); + ArrayList perms = new ArrayList<>(); + if(checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){ + perms.add(Manifest.permission.WRITE_EXTERNAL_STORAGE); + } + if(checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){ + perms.add(Manifest.permission.READ_EXTERNAL_STORAGE); + } + requestPermissions(perms.toArray(new String[0]), PERMISSION_REQUEST_CODE); }else{ - super.showFileChooser(open, "@open", extension, cons); + if(open){ + new FileChooser(title, file -> Structs.contains(extensions, file.extension().toLowerCase()), true, cons).show(); + }else{ + super.showFileChooser(open, "@open", extension, cons); + } } + }catch(Throwable error){ + Core.app.post(() -> Vars.ui.showException(error)); } }