mirror of
https://github.com/AngelAuraMC/Amethyst-Android.git
synced 2025-09-12 22:26:56 -04:00
Refactor[intent]: use ActivityResultLauncher instead of request codes
This commit is contained in:
parent
9484d0c52d
commit
27223c885a
@ -12,8 +12,8 @@ import android.view.View;
|
||||
import android.widget.ImageButton;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.activity.result.ActivityResultLauncher;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.core.app.ActivityCompat;
|
||||
import androidx.core.content.ContextCompat;
|
||||
@ -25,16 +25,15 @@ import com.kdt.mcgui.ProgressLayout;
|
||||
import com.kdt.mcgui.mcAccountSpinner;
|
||||
|
||||
import net.kdt.pojavlaunch.contextexecutor.ContextExecutor;
|
||||
import net.kdt.pojavlaunch.fragments.MainMenuFragment;
|
||||
import net.kdt.pojavlaunch.fragments.MicrosoftLoginFragment;
|
||||
import net.kdt.pojavlaunch.contracts.OpenDocumentWithExtension;
|
||||
import net.kdt.pojavlaunch.extra.ExtraConstants;
|
||||
import net.kdt.pojavlaunch.extra.ExtraCore;
|
||||
import net.kdt.pojavlaunch.extra.ExtraListener;
|
||||
|
||||
import net.kdt.pojavlaunch.fragments.MainMenuFragment;
|
||||
import net.kdt.pojavlaunch.fragments.MicrosoftLoginFragment;
|
||||
import net.kdt.pojavlaunch.fragments.SelectAuthFragment;
|
||||
import net.kdt.pojavlaunch.modloaders.modpacks.ModloaderInstallTracker;
|
||||
import net.kdt.pojavlaunch.modloaders.modpacks.imagecache.IconCacheJanitor;
|
||||
import net.kdt.pojavlaunch.multirt.MultiRTConfigDialog;
|
||||
import net.kdt.pojavlaunch.prefs.LauncherPreferences;
|
||||
import net.kdt.pojavlaunch.prefs.screens.LauncherPreferenceFragment;
|
||||
import net.kdt.pojavlaunch.progresskeeper.ProgressKeeper;
|
||||
@ -47,6 +46,11 @@ import net.kdt.pojavlaunch.value.launcherprofiles.MinecraftProfile;
|
||||
public class LauncherActivity extends BaseActivity {
|
||||
public static final String SETTING_FRAGMENT_TAG = "SETTINGS_FRAGMENT";
|
||||
|
||||
public final ActivityResultLauncher<Object> modInstallerLauncher =
|
||||
registerForActivityResult(new OpenDocumentWithExtension("jar"), (data)->{
|
||||
if(data != null) Tools.launchModInstaller(this, data);
|
||||
});
|
||||
|
||||
private final int REQUEST_STORAGE_REQUEST_CODE = 1;
|
||||
private final Object mLockStoragePerm = new Object();
|
||||
|
||||
@ -219,19 +223,6 @@ public class LauncherActivity extends BaseActivity {
|
||||
getSupportFragmentManager().unregisterFragmentLifecycleCallbacks(mFragmentCallbackListener);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
if(resultCode != RESULT_OK) return;
|
||||
if(requestCode == Tools.RUN_MOD_INSTALLER && data != null){
|
||||
Tools.launchModInstaller(this, data);
|
||||
return;
|
||||
}
|
||||
if(requestCode == MultiRTConfigDialog.MULTIRT_PICK_RUNTIME && data != null){
|
||||
Tools.installRuntimeFromUri(this, data.getData());
|
||||
}
|
||||
}
|
||||
|
||||
/** Custom implementation to feel more natural when a backstack isn't present */
|
||||
@Override
|
||||
public void onBackPressed() {
|
||||
|
@ -33,7 +33,6 @@ import android.util.DisplayMetrics;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.view.WindowManager;
|
||||
import android.webkit.MimeTypeMap;
|
||||
import android.widget.EditText;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
@ -117,7 +116,6 @@ public final class Tools {
|
||||
public static String OBSOLETE_RESOURCES_PATH;
|
||||
public static String CTRLMAP_PATH;
|
||||
public static String CTRLDEF_FILE;
|
||||
public static final int RUN_MOD_INSTALLER = 2050;
|
||||
private static RenderersList sCompatibleRenderers;
|
||||
|
||||
|
||||
@ -928,12 +926,10 @@ public final class Tools {
|
||||
}
|
||||
|
||||
if(!customJavaArgs){ // Launch the intent to get the jar file
|
||||
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
|
||||
intent.addCategory(Intent.CATEGORY_OPENABLE);
|
||||
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension("jar");
|
||||
if(mimeType == null) mimeType = "*/*";
|
||||
intent.setType(mimeType);
|
||||
activity.startActivityForResult(intent, RUN_MOD_INSTALLER);
|
||||
if(!(activity instanceof LauncherActivity))
|
||||
throw new IllegalStateException("Cannot start Mod Installer without LauncherActivity");
|
||||
LauncherActivity launcherActivity = (LauncherActivity)activity;
|
||||
launcherActivity.modInstallerLauncher.launch(null);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -967,10 +963,9 @@ public final class Tools {
|
||||
}
|
||||
|
||||
/** Copy the mod file, and launch the mod installer activity */
|
||||
public static void launchModInstaller(Activity activity, @NonNull Intent data){
|
||||
public static void launchModInstaller(Activity activity, @NonNull Uri uri){
|
||||
final ProgressDialog alertDialog = getWaitingDialog(activity);
|
||||
|
||||
final Uri uri = data.getData();
|
||||
alertDialog.setMessage(activity.getString(R.string.multirt_progress_caching));
|
||||
sExecutorService.execute(() -> {
|
||||
try {
|
||||
@ -994,18 +989,18 @@ public final class Tools {
|
||||
}
|
||||
|
||||
|
||||
public static void installRuntimeFromUri(Activity activity, Uri uri){
|
||||
public static void installRuntimeFromUri(Context context, Uri uri){
|
||||
sExecutorService.execute(() -> {
|
||||
try {
|
||||
String name = getFileName(activity, uri);
|
||||
String name = getFileName(context, uri);
|
||||
MultiRTUtils.installRuntimeNamed(
|
||||
NATIVE_LIB_DIR,
|
||||
activity.getContentResolver().openInputStream(uri),
|
||||
context.getContentResolver().openInputStream(uri),
|
||||
name);
|
||||
|
||||
MultiRTUtils.postPrepare(name);
|
||||
} catch (IOException e) {
|
||||
Tools.showError(activity, e);
|
||||
Tools.showError(context, e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -0,0 +1,45 @@
|
||||
package net.kdt.pojavlaunch.contracts;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.webkit.MimeTypeMap;
|
||||
|
||||
import androidx.activity.result.contract.ActivityResultContract;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
// Android's OpenDocument contract is the basicmost crap that doesn't allow
|
||||
// you to specify practically anything. So i made this instead.
|
||||
public class OpenDocumentWithExtension extends ActivityResultContract<Object, Uri> {
|
||||
private final String mimeType;
|
||||
public OpenDocumentWithExtension(String extension) {
|
||||
String extensionMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
|
||||
if(extensionMimeType == null) extensionMimeType = "*/*";
|
||||
mimeType = extensionMimeType;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Intent createIntent(@NonNull Context context, @NonNull Object input) {
|
||||
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
|
||||
intent.addCategory(Intent.CATEGORY_OPENABLE);
|
||||
intent.setType(mimeType);
|
||||
return intent;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public final SynchronousResult<Uri> getSynchronousResult(@NonNull Context context,
|
||||
@NonNull Object input) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public final Uri parseResult(int resultCode, @Nullable Intent intent) {
|
||||
if (intent == null || resultCode != Activity.RESULT_OK) return null;
|
||||
return intent.getData();
|
||||
}
|
||||
}
|
@ -1,22 +1,17 @@
|
||||
package net.kdt.pojavlaunch.multirt;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.Activity;
|
||||
import android.app.AlertDialog;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.view.View;
|
||||
import android.webkit.MimeTypeMap;
|
||||
import android.content.Context;
|
||||
import android.widget.Button;
|
||||
|
||||
import androidx.activity.result.ActivityResultLauncher;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
|
||||
import net.kdt.pojavlaunch.R;
|
||||
|
||||
public class MultiRTConfigDialog {
|
||||
public static final int MULTIRT_PICK_RUNTIME = 2048;
|
||||
private AlertDialog mDialog;
|
||||
private RecyclerView mDialogView;
|
||||
|
||||
@ -33,7 +28,7 @@ public class MultiRTConfigDialog {
|
||||
}
|
||||
|
||||
/** Build the dialog behavior and style */
|
||||
public void prepare(Activity activity) {
|
||||
public void prepare(Context activity, ActivityResultLauncher<Object> installJvmLauncher) {
|
||||
mDialogView = new RecyclerView(activity);
|
||||
mDialogView.setLayoutManager(new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false));
|
||||
RTRecyclerViewAdapter adapter = new RTRecyclerViewAdapter();
|
||||
@ -42,7 +37,7 @@ public class MultiRTConfigDialog {
|
||||
mDialog = new AlertDialog.Builder(activity)
|
||||
.setTitle(R.string.multirt_config_title)
|
||||
.setView(mDialogView)
|
||||
.setPositiveButton(R.string.multirt_config_add, (dialog, which) -> openRuntimeSelector(activity,MULTIRT_PICK_RUNTIME))
|
||||
.setPositiveButton(R.string.multirt_config_add, (dialog, which) -> installJvmLauncher.launch(null))
|
||||
.setNeutralButton(R.string.multirt_delete_runtime, null)
|
||||
.create();
|
||||
|
||||
@ -56,13 +51,4 @@ public class MultiRTConfigDialog {
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
public static void openRuntimeSelector(Activity activity, int code) {
|
||||
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
|
||||
intent.addCategory(Intent.CATEGORY_OPENABLE);
|
||||
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension("xz");
|
||||
if (mimeType == null) mimeType = "*/*";
|
||||
intent.setType(mimeType);
|
||||
activity.startActivityForResult(intent, code);
|
||||
}
|
||||
}
|
||||
|
@ -1,32 +0,0 @@
|
||||
package net.kdt.pojavlaunch.prefs;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.util.AttributeSet;
|
||||
|
||||
import androidx.preference.Preference;
|
||||
|
||||
|
||||
import net.kdt.pojavlaunch.multirt.MultiRTConfigDialog;
|
||||
|
||||
public class RuntimeManagerPreference extends Preference{
|
||||
private MultiRTConfigDialog mDialogScreen;
|
||||
|
||||
@SuppressWarnings("unused") public RuntimeManagerPreference(Context ctx) {
|
||||
this(ctx, null);
|
||||
}
|
||||
|
||||
public RuntimeManagerPreference(Context ctx, AttributeSet attrs) {
|
||||
super(ctx, attrs);
|
||||
setPersistent(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onClick() {
|
||||
if(mDialogScreen == null){
|
||||
mDialogScreen = new MultiRTConfigDialog();
|
||||
mDialogScreen.prepare(((Activity) getContext()));
|
||||
}
|
||||
mDialogScreen.show();
|
||||
}
|
||||
}
|
@ -6,13 +6,26 @@ import static net.kdt.pojavlaunch.Tools.getTotalDeviceMemory;
|
||||
import android.os.Bundle;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.activity.result.ActivityResultLauncher;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.preference.EditTextPreference;
|
||||
import androidx.preference.Preference;
|
||||
|
||||
import net.kdt.pojavlaunch.R;
|
||||
import net.kdt.pojavlaunch.Tools;
|
||||
import net.kdt.pojavlaunch.contracts.OpenDocumentWithExtension;
|
||||
import net.kdt.pojavlaunch.multirt.MultiRTConfigDialog;
|
||||
import net.kdt.pojavlaunch.prefs.CustomSeekBarPreference;
|
||||
import net.kdt.pojavlaunch.prefs.LauncherPreferences;
|
||||
|
||||
public class LauncherPreferenceJavaFragment extends LauncherPreferenceFragment {
|
||||
private MultiRTConfigDialog mDialogScreen;
|
||||
private Preference mMultiRTPreference;
|
||||
private final ActivityResultLauncher<Object> mVmInstallLauncher =
|
||||
registerForActivityResult(new OpenDocumentWithExtension("xz"), (data)->{
|
||||
if(data != null) Tools.installRuntimeFromUri(getContext(), data);
|
||||
});
|
||||
|
||||
@Override
|
||||
public void onCreatePreferences(Bundle b, String str) {
|
||||
int ramAllocation = LauncherPreferences.PREF_RAM_ALLOCATION;
|
||||
@ -39,5 +52,22 @@ public class LauncherPreferenceJavaFragment extends LauncherPreferenceFragment {
|
||||
editJVMArgs.setOnBindEditTextListener(TextView::setSingleLine);
|
||||
}
|
||||
|
||||
mMultiRTPreference = findPreference("install_jre");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPreferenceTreeClick(@NonNull Preference preference) {
|
||||
if(preference.equals(mMultiRTPreference)) {
|
||||
openMultiRTDialog();
|
||||
}
|
||||
return super.onPreferenceTreeClick(preference);
|
||||
}
|
||||
|
||||
private void openMultiRTDialog() {
|
||||
if (mDialogScreen == null) {
|
||||
mDialogScreen = new MultiRTConfigDialog();
|
||||
mDialogScreen.prepare(getContext(), mVmInstallLauncher);
|
||||
}
|
||||
mDialogScreen.show();
|
||||
}
|
||||
}
|
||||
|
@ -5,7 +5,9 @@
|
||||
<net.kdt.pojavlaunch.prefs.BackButtonPreference/>
|
||||
|
||||
<PreferenceCategory android:title="Java Tweaks">
|
||||
<net.kdt.pojavlaunch.prefs.RuntimeManagerPreference
|
||||
<Preference
|
||||
android:key="install_jre"
|
||||
android:persistent="false"
|
||||
android:summary="@string/multirt_subtitle"
|
||||
android:title="@string/multirt_title"/>
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user