Skip to content

Commit

Permalink
调整[启动游戏]: 采用更为便捷的方案,而不是设置延迟
Browse files Browse the repository at this point in the history
  • Loading branch information
Vera-Firefly committed Oct 10, 2024
1 parent c4c2b30 commit 258c6c8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ public class LauncherActivity extends BaseActivity {
private ProgressServiceKeeper mProgressServiceKeeper;
private ModloaderInstallTracker mInstallTracker;
private NotificationManager mNotificationManager;
private static volatile boolean onStartLaunchGame = false;

/* Allows to switch from one button "type" to another */
private final FragmentManager.FragmentLifecycleCallbacks mFragmentCallbackListener = new FragmentManager.FragmentLifecycleCallbacks() {
Expand Down Expand Up @@ -115,55 +114,48 @@ public void onFragmentResumed(@NonNull FragmentManager fm, @NonNull Fragment f)
.setNeutralButton(R.string.global_delete, (dialog, which) -> mAccountSpinner.removeCurrentAccount())
.show();

private final ExtraListener<Boolean> mLaunchGameListener = (key, value) -> {

private final ExtraListener<Boolean> mStartDownloadMinecraft = (key, value) -> {
mLaunchGame(true);
return false;
};

private final ExtraListener<Boolean> mSkipDownloadMinecraft = (key, value) -> {
mLaunchGame(false);
return false;
};

private void mLaunchGame(boolean downloader) {
if (mProgressLayout.hasProcesses()) {
Toast.makeText(this, R.string.tasks_ongoing, Toast.LENGTH_LONG).show();
return false;
return;
}

String selectedProfile = LauncherPreferences.DEFAULT_PREF.getString(LauncherPreferences.PREF_KEY_CURRENT_PROFILE, "");
if (LauncherProfiles.mainProfileJson == null || !LauncherProfiles.mainProfileJson.profiles.containsKey(selectedProfile)) {
Toast.makeText(this, R.string.error_no_version, Toast.LENGTH_LONG).show();
return false;
return;
}
MinecraftProfile prof = LauncherProfiles.mainProfileJson.profiles.get(selectedProfile);
if (prof == null || prof.lastVersionId == null || "Unknown".equals(prof.lastVersionId)) {
Toast.makeText(this, R.string.error_no_version, Toast.LENGTH_LONG).show();
return false;
return;
}

if (mAccountSpinner.getSelectedAccount() == null) {
Toast.makeText(this, R.string.no_saved_accounts, Toast.LENGTH_LONG).show();
ExtraCore.setValue(ExtraConstants.SELECT_AUTH_METHOD, true);
return false;
}
onStartLaunchGame = true;
return false;
};

private final ExtraListener<Boolean> mStartDownloadMinecraft = (key, value) -> {
if (mProgressLayout.hasProcesses()) {
// Stops repeating operations if a process is running
return false;
}
if (!onStartLaunchGame) {
return false;
}
onTryDownloadGame(true);
return false;
};

private final ExtraListener<Boolean> mSkipDownloadMinecraft = (key, value) -> {
if (mProgressLayout.hasProcesses()) {
// Stops repeating operations if a process is running
return false;
}
if (!onStartLaunchGame) {
return false;
return;
}
onTryDownloadGame(false);
return false;
};
String normalizedVersionId = AsyncMinecraftDownloader.normalizeVersionId(prof.lastVersionId);
JMinecraftVersionList.Version mcVersion = AsyncMinecraftDownloader.getListedVersion(normalizedVersionId);
new MinecraftDownloader().start(
downloader,
mcVersion,
normalizedVersionId,
new ContextAwareDoneListener(this, normalizedVersionId)
);
}

private final TaskCountListener mDoubleLaunchPreventionListener = taskCount -> {
// Hide the notification that starts the game if there are tasks executing.
Expand Down Expand Up @@ -223,7 +215,7 @@ protected void onCreate(Bundle savedInstanceState) {
ExtraCore.addExtraListener(ExtraConstants.BACK_PREFERENCE, mBackPreferenceListener);
ExtraCore.addExtraListener(ExtraConstants.SELECT_AUTH_METHOD, mSelectAuthMethod);

ExtraCore.addExtraListener(ExtraConstants.LAUNCH_GAME, mLaunchGameListener);
// ExtraCore.addExtraListener(ExtraConstants.LAUNCH_GAME, mLaunchGameListener);
ExtraCore.addExtraListener(ExtraConstants.START_DOWNLOADER, mStartDownloadMinecraft);
ExtraCore.addExtraListener(ExtraConstants.SKIP_DOWNLOADER, mSkipDownloadMinecraft);

Expand Down Expand Up @@ -274,7 +266,7 @@ protected void onDestroy() {
ProgressKeeper.removeTaskCountListener(mProgressServiceKeeper);
ExtraCore.removeExtraListenerFromValue(ExtraConstants.BACK_PREFERENCE, mBackPreferenceListener);
ExtraCore.removeExtraListenerFromValue(ExtraConstants.SELECT_AUTH_METHOD, mSelectAuthMethod);
ExtraCore.removeExtraListenerFromValue(ExtraConstants.LAUNCH_GAME, mLaunchGameListener);
// ExtraCore.removeExtraListenerFromValue(ExtraConstants.LAUNCH_GAME, mLaunchGameListener);
ExtraCore.removeExtraListenerFromValue(ExtraConstants.START_DOWNLOADER, mStartDownloadMinecraft);
ExtraCore.removeExtraListenerFromValue(ExtraConstants.SKIP_DOWNLOADER, mSkipDownloadMinecraft);

Expand Down Expand Up @@ -323,20 +315,6 @@ private Fragment getVisibleFragment(int id) {
return null;
}

private void onTryDownloadGame(boolean downloader) {
onStartLaunchGame = false;
String selectedProfile = LauncherPreferences.DEFAULT_PREF.getString(LauncherPreferences.PREF_KEY_CURRENT_PROFILE, "");
MinecraftProfile prof = LauncherProfiles.mainProfileJson.profiles.get(selectedProfile);
String normalizedVersionId = AsyncMinecraftDownloader.normalizeVersionId(prof.lastVersionId);
JMinecraftVersionList.Version mcVersion = AsyncMinecraftDownloader.getListedVersion(normalizedVersionId);
new MinecraftDownloader().start(
downloader,
mcVersion,
normalizedVersionId,
new ContextAwareDoneListener(this, normalizedVersionId)
);
}

private void checkNotificationPermission() {
if (LauncherPreferences.PREF_SKIP_NOTIFICATION_PERMISSION_CHECK ||
checkForNotificationPermission()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.provider.Settings;
import android.view.View;
import android.widget.Button;
Expand Down Expand Up @@ -86,12 +84,10 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
mEditProfileButton.setOnClickListener(v -> mVersionSpinner.openProfileEditor(requireActivity()));

mPlayButton.setOnClickListener(v -> {
ExtraCore.setValue(ExtraConstants.LAUNCH_GAME, true);
new Handler(Looper.getMainLooper()).postDelayed(() -> ExtraCore.setValue(ExtraConstants.START_DOWNLOADER, true), 10);
ExtraCore.setValue(ExtraConstants.START_DOWNLOADER, true);
});
mPlayButton.setOnLongClickListener(v -> {
ExtraCore.setValue(ExtraConstants.LAUNCH_GAME, true);
new Handler(Looper.getMainLooper()).postDelayed(() -> ExtraCore.setValue(ExtraConstants.SKIP_DOWNLOADER, true), 10);
ExtraCore.setValue(ExtraConstants.SKIP_DOWNLOADER, true);
return true;
});

Expand Down

0 comments on commit 258c6c8

Please sign in to comment.