diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/JavaGUILauncherActivity.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/JavaGUILauncherActivity.java index 3a87663fef..6d559e30c7 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/JavaGUILauncherActivity.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/JavaGUILauncherActivity.java @@ -1,7 +1,9 @@ package net.kdt.pojavlaunch; import android.annotation.SuppressLint; +import android.app.ProgressDialog; import android.content.ClipboardManager; +import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.view.GestureDetector; @@ -24,9 +26,11 @@ import net.kdt.pojavlaunch.utils.JREUtils; import net.kdt.pojavlaunch.utils.MathUtils; +import org.apache.commons.io.IOUtils; import org.lwjgl.glfw.CallbackBridge; import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.nio.ByteBuffer; @@ -47,7 +51,7 @@ public class JavaGUILauncherActivity extends BaseActivity implements View.OnTouc private ImageView mMousePointerImageView; private GestureDetector mGestureDetector; - private boolean mSkipDetectMod, mIsVirtualMouseEnabled; + private boolean mIsVirtualMouseEnabled; @SuppressLint("ClickableViewAccessibility") @Override @@ -147,40 +151,23 @@ public boolean onTouch(View v, MotionEvent event) { try { placeMouseAt(CallbackBridge.physicalWidth / 2f, CallbackBridge.physicalHeight / 2f); - - final File modFile = (File) getIntent().getExtras().getSerializable("modFile"); - final String javaArgs = getIntent().getExtras().getString("javaArgs"); - String jreName = LauncherPreferences.PREF_DEFAULT_RUNTIME; - if(modFile != null) { - int javaVersion = getJavaVersion(modFile); - if(javaVersion != -1) { - String autoselectRuntime = MultiRTUtils.getNearestJreName(javaVersion); - if (autoselectRuntime != null) jreName = autoselectRuntime; - } - } - final Runtime runtime = MultiRTUtils.forceReread(jreName); - - mSkipDetectMod = getIntent().getExtras().getBoolean("skipDetectMod", false); - if(getIntent().getExtras().getBoolean("openLogOutput", false)) openLogOutput(null); - if (mSkipDetectMod) { - new Thread(() -> launchJavaRuntime(runtime, modFile, javaArgs), "JREMainThread").start(); + Bundle extras = getIntent().getExtras(); + if(extras == null) { + finish(); return; } - - // No skip detection - openLogOutput(null); - new Thread(() -> { - try { - // Due to time, the code here became, like, actually useless - // So it was removed - // Tbh this whole class needs a refactor... - doCustomInstall(runtime, modFile, javaArgs); - } catch (Throwable e) { - Logger.appendToLog("Install failed:"); - Logger.appendToLog(Log.getStackTraceString(e)); - Tools.showError(JavaGUILauncherActivity.this, e); - } - }, "Installer").start(); + final String javaArgs = extras.getString("javaArgs"); + final Uri resourceUri = (Uri) extras.getParcelable("modUri"); + if(extras.getBoolean("openLogOutput", false)) openLogOutput(null); + if (javaArgs != null) { + startModInstaller(null, javaArgs); + }else if(resourceUri != null) { + ProgressDialog barrierDialog = Tools.getWaitingDialog(this, R.string.multirt_progress_caching); + PojavApplication.sExecutorService.execute(()->{ + startModInstallerWithUri(resourceUri); + runOnUiThread(barrierDialog::dismiss); + }); + } } catch (Throwable th) { Tools.showError(this, th, true); } @@ -194,6 +181,39 @@ public void handleOnBackPressed() { }); } + private void startModInstallerWithUri(Uri uri) { + try { + File cacheFile = new File(getCacheDir(), "mod-installer-temp"); + InputStream contentStream = getContentResolver().openInputStream(uri); + try (FileOutputStream fileOutputStream = new FileOutputStream(cacheFile)) { + IOUtils.copy(contentStream, fileOutputStream); + } + contentStream.close(); + startModInstaller(cacheFile, null); + }catch (IOException e) { + Tools.showError(this, e, true); + } + } + + private void startModInstaller(File modFile, String javaArgs) { + new Thread(() -> { + Runtime runtime = pickJreForMod(modFile); + launchJavaRuntime(runtime, modFile, javaArgs); + }, "JREMainThread").start(); + } + + private Runtime pickJreForMod(File modFile) { + String jreName = LauncherPreferences.PREF_DEFAULT_RUNTIME; + if(modFile != null) { + int javaVersion = getJavaVersion(modFile); + if(javaVersion != -1) { + String autoselectRuntime = MultiRTUtils.getNearestJreName(javaVersion); + if (autoselectRuntime != null) jreName = autoselectRuntime; + } + } + return MultiRTUtils.forceReread(jreName); + } + @Override public void onResume() { super.onResume(); @@ -313,13 +333,6 @@ public void launchJavaRuntime(Runtime runtime, File modFile, String javaArgs) { } } - - - private void doCustomInstall(Runtime runtime, File modFile, String javaArgs) { - mSkipDetectMod = true; - launchJavaRuntime(runtime, modFile, javaArgs); - } - public void toggleKeyboard(View view) { mTouchCharInput.switchKeyboardState(); } diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/Tools.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/Tools.java index 1dd336aca6..de5aeb6899 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/Tools.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/Tools.java @@ -980,7 +980,6 @@ public static void installMod(Activity activity, boolean customJavaArgs) { .setView(editText) .setPositiveButton(android.R.string.ok, (di, i) -> { Intent intent = new Intent(activity, JavaGUILauncherActivity.class); - intent.putExtra("skipDetectMod", true); intent.putExtra("javaArgs", editText.getText().toString()); activity.startActivity(intent); }); @@ -988,9 +987,9 @@ public static void installMod(Activity activity, boolean customJavaArgs) { } /** Display and return a progress dialog, instructing to wait */ - private static ProgressDialog getWaitingDialog(Context ctx){ + public static ProgressDialog getWaitingDialog(Context ctx, int message){ final ProgressDialog barrier = new ProgressDialog(ctx); - barrier.setMessage(ctx.getString(R.string.global_waiting)); + barrier.setMessage(ctx.getString(message)); barrier.setProgressStyle(ProgressDialog.STYLE_SPINNER); barrier.setCancelable(false); barrier.show(); @@ -998,30 +997,13 @@ private static ProgressDialog getWaitingDialog(Context ctx){ return barrier; } - /** Copy the mod file, and launch the mod installer activity */ + /** Launch the mod installer activity. The Uri must be from our own content provider or + * from ACTION_OPEN_DOCUMENT + */ public static void launchModInstaller(Activity activity, @NonNull Uri uri){ - final ProgressDialog alertDialog = getWaitingDialog(activity); - - alertDialog.setMessage(activity.getString(R.string.multirt_progress_caching)); - sExecutorService.execute(() -> { - try { - final String name = getFileName(activity, uri); - final File modInstallerFile = new File(Tools.DIR_CACHE, name); - FileOutputStream fos = new FileOutputStream(modInstallerFile); - InputStream input = activity.getContentResolver().openInputStream(uri); - IOUtils.copy(input, fos); - input.close(); - fos.close(); - activity.runOnUiThread(() -> { - alertDialog.dismiss(); - Intent intent = new Intent(activity, JavaGUILauncherActivity.class); - intent.putExtra("modFile", modInstallerFile); - activity.startActivity(intent); - }); - }catch(IOException e) { - Tools.showError(activity, e); - } - }); + Intent intent = new Intent(activity, JavaGUILauncherActivity.class); + intent.putExtra("modUri", uri); + activity.startActivity(intent); } diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/modloaders/ForgeUtils.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/modloaders/ForgeUtils.java index 5925d9b4d0..3fcd6a98ac 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/modloaders/ForgeUtils.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/modloaders/ForgeUtils.java @@ -57,12 +57,10 @@ public static void addAutoInstallArgs(Intent intent, File modInstallerJar, boole intent.putExtra("javaArgs", "-javaagent:"+ Tools.DIR_DATA+"/forge_installer/forge_installer.jar" + (createProfile ? "=NPS" : "") + // No Profile Suppression " -jar "+modInstallerJar.getAbsolutePath()); - intent.putExtra("skipDetectMod", true); } public static void addAutoInstallArgs(Intent intent, File modInstallerJar, String modpackFixupId) { intent.putExtra("javaArgs", "-javaagent:"+ Tools.DIR_DATA+"/forge_installer/forge_installer.jar" + "=\"" + modpackFixupId +"\"" + " -jar "+modInstallerJar.getAbsolutePath()); - intent.putExtra("skipDetectMod", true); } } diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/modloaders/OptiFineUtils.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/modloaders/OptiFineUtils.java index 32cf9938b0..511d798591 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/modloaders/OptiFineUtils.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/modloaders/OptiFineUtils.java @@ -25,7 +25,6 @@ public static void addAutoInstallArgs(Intent intent, File modInstallerJar) { intent.putExtra("javaArgs", "-javaagent:"+ Tools.DIR_DATA+"/forge_installer/forge_installer.jar" + "=OFNPS" +// No Profile Suppression " -jar "+modInstallerJar.getAbsolutePath()); - intent.putExtra("skipDetectMod", true); } public static class OptiFineVersions {