diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/LauncherActivity.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/LauncherActivity.java index 9b8834d17..26d01c6c4 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/LauncherActivity.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/LauncherActivity.java @@ -1,6 +1,8 @@ package net.kdt.pojavlaunch; import static android.content.res.Configuration.ORIENTATION_PORTRAIT; +import static net.kdt.pojavlaunch.Tools.hasNoOnlineProfileDialog; + import android.Manifest; import android.app.NotificationManager; import android.content.Context; @@ -139,7 +141,7 @@ public class LauncherActivity extends BaseActivity { } if (isOlderThan13) { - Toast.makeText(this, R.string.toast_not_available_demo, Toast.LENGTH_LONG).show(); + hasNoOnlineProfileDialog(this, getString(R.string.global_error), "This version is not available in demo mode."); return false; } } diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/PojavProfile.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/PojavProfile.java index ae9d5421e..efd57bb8d 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/PojavProfile.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/PojavProfile.java @@ -8,6 +8,13 @@ import androidx.annotation.Nullable; import net.kdt.pojavlaunch.value.MinecraftAccount; +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Objects; + public class PojavProfile { private static final String PROFILE_PREF = "pojav_profile"; private static final String PROFILE_PREF_FILE = "file"; @@ -29,6 +36,25 @@ public class PojavProfile { } return name; } + + public static List getAllProfiles(){ + List mcAccountList = new ArrayList<>();; + for (String accountName : getAllProfilesList()){ + mcAccountList.add(MinecraftAccount.load(accountName)); + } + return mcAccountList; + } + + public static List getAllProfilesList(){ + List accountList = new ArrayList<>(); + File accountFolder = new File(Tools.DIR_ACCOUNT_NEW); + if(accountFolder.exists() && accountFolder.list() != null){ + for (String fileName : Objects.requireNonNull(accountFolder.list())) { + accountList.add(fileName.substring(0, fileName.length() - 5)); + } + } + return accountList; + } public static void setCurrentProfile(@NonNull Context ctx, @Nullable Object obj) { SharedPreferences.Editor pref = getPrefs(ctx).edit(); 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 8ed093b26..c50f89a9c 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/Tools.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/Tools.java @@ -3,6 +3,7 @@ package net.kdt.pojavlaunch; import static android.os.Build.VERSION.SDK_INT; import static android.os.Build.VERSION_CODES.P; import static net.kdt.pojavlaunch.PojavApplication.sExecutorService; +import static net.kdt.pojavlaunch.PojavProfile.getAllProfiles; import static net.kdt.pojavlaunch.prefs.LauncherPreferences.PREF_IGNORE_NOTCH; import static net.kdt.pojavlaunch.prefs.LauncherPreferences.PREF_NOTCH_SIZE; @@ -1452,4 +1453,30 @@ public final class Tools { MinecraftAccount currentProfile = PojavProfile.getCurrentProfileContent(ctx, null); return currentProfile == null || currentProfile.isLocal(); } + public static boolean hasOnlineProfile(){ + for (MinecraftAccount accountToCheck : getAllProfiles()) { + if (!accountToCheck.isLocal() || !accountToCheck.isDemo()) { + return true; + } + } + return false; + } + public static void hasNoOnlineProfileDialog(Activity activity){ + if (hasOnlineProfile()){ + } else dialogOnUiThread(activity, "No Minecraft Account Found", "Please log into your Minecraft: Java Edition account to use this feature."); + } + public static void hasNoOnlineProfileDialog(Activity activity, String customTitle, String customMessage){ + if (hasOnlineProfile()){ + } else dialogOnUiThread(activity, customTitle, customMessage); + } + public static void hasNoOnlineProfileDialog(Activity activity, Runnable run){ + if (hasOnlineProfile()){ + run.run(); + } else dialogOnUiThread(activity, "No Minecraft Account Found", "Please log into your Minecraft: Java Edition account to use this feature."); + } + public static void hasNoOnlineProfileDialog(Activity activity, Runnable run, String customTitle, String customMessage){ + if (hasOnlineProfile()){ + run.run(); + } else dialogOnUiThread(activity, customTitle, customMessage); + } } diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/fragments/LocalLoginFragment.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/fragments/LocalLoginFragment.java index 86a00e375..e4e2e5d4f 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/fragments/LocalLoginFragment.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/fragments/LocalLoginFragment.java @@ -1,5 +1,7 @@ package net.kdt.pojavlaunch.fragments; +import static net.kdt.pojavlaunch.Tools.hasOnlineProfile; + import android.content.Context; import android.os.Bundle; import android.view.View; @@ -31,6 +33,10 @@ public class LocalLoginFragment extends Fragment { @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { + // This is overkill but meh + if (!hasOnlineProfile()){ + Tools.swapFragment(requireActivity(), MainMenuFragment.class, MainMenuFragment.TAG, null); + } mUsernameEditText = view.findViewById(R.id.login_edit_email); view.findViewById(R.id.login_button).setOnClickListener(v -> { if(!checkEditText()) { diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/fragments/MainMenuFragment.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/fragments/MainMenuFragment.java index 364c3d0b1..a029d82c3 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/fragments/MainMenuFragment.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/fragments/MainMenuFragment.java @@ -1,5 +1,7 @@ package net.kdt.pojavlaunch.fragments; +import static net.kdt.pojavlaunch.Tools.hasNoOnlineProfileDialog; +import static net.kdt.pojavlaunch.Tools.hasOnlineProfile; import static net.kdt.pojavlaunch.Tools.openPath; import static net.kdt.pojavlaunch.Tools.shareLog; @@ -53,11 +55,13 @@ public class MainMenuFragment extends Fragment { mNewsButton.setOnClickListener(v -> Tools.openURL(requireActivity(), Tools.URL_HOME)); mDiscordButton.setOnClickListener(v -> Tools.openURL(requireActivity(), getString(R.string.discord_invite))); mCustomControlButton.setOnClickListener(v -> startActivity(new Intent(requireContext(), CustomControlsActivity.class))); - mInstallJarButton.setOnClickListener(v -> runInstallerWithConfirmation(false)); - mInstallJarButton.setOnLongClickListener(v->{ - runInstallerWithConfirmation(true); - return true; - }); + if (hasOnlineProfile()) { + mInstallJarButton.setOnClickListener(v -> runInstallerWithConfirmation(false)); + mInstallJarButton.setOnLongClickListener(v -> { + runInstallerWithConfirmation(true); + return true; + }); + } else mInstallJarButton.setOnClickListener(v -> hasNoOnlineProfileDialog(requireActivity())); mEditProfileButton.setOnClickListener(v -> mVersionSpinner.openProfileEditor(requireActivity())); mPlayButton.setOnClickListener(v -> ExtraCore.setValue(ExtraConstants.LAUNCH_GAME, true)); @@ -65,13 +69,12 @@ public class MainMenuFragment extends Fragment { mShareLogsButton.setOnClickListener((v) -> shareLog(requireContext())); mOpenDirectoryButton.setOnClickListener((v)-> { - Tools.switchDemo(Tools.isDemoProfile(v.getContext())); // avoid switching accounts being able to access - if(Tools.isDemoProfile(v.getContext())){ - Toast.makeText(v.getContext(), R.string.toast_not_available_demo, Toast.LENGTH_LONG).show(); - return; - } - - openPath(v.getContext(), getCurrentProfileDirectory(), false); + if (hasOnlineProfile()) { + if (Tools.isDemoProfile(v.getContext())){ + hasNoOnlineProfileDialog(getActivity(), "Demo Profile not supported", "Please change accounts to use this function"); + } + openPath(v.getContext(), getCurrentProfileDirectory(), false); + } else hasNoOnlineProfileDialog(requireActivity()); }); @@ -97,12 +100,6 @@ public class MainMenuFragment extends Fragment { } private void runInstallerWithConfirmation(boolean isCustomArgs) { - // avoid using custom installers to install a version - if(Tools.isLocalProfile(requireContext()) || Tools.isDemoProfile(requireContext())){ - Toast.makeText(requireContext(), R.string.toast_not_available_demo, Toast.LENGTH_LONG).show(); - return; - } - if (ProgressKeeper.getTaskCount() == 0) Tools.installMod(requireActivity(), isCustomArgs); else diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/fragments/ProfileTypeSelectFragment.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/fragments/ProfileTypeSelectFragment.java index bd8575069..9a82ccb9f 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/fragments/ProfileTypeSelectFragment.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/fragments/ProfileTypeSelectFragment.java @@ -1,5 +1,8 @@ package net.kdt.pojavlaunch.fragments; +import static net.kdt.pojavlaunch.Tools.hasNoOnlineProfileDialog; +import static net.kdt.pojavlaunch.Tools.hasOnlineProfile; + import android.os.Bundle; import android.view.View; import android.widget.Toast; @@ -42,8 +45,8 @@ public class ProfileTypeSelectFragment extends Fragment { } private void tryInstall(Class fragmentClass, String tag){ - if(Tools.isLocalProfile(requireContext()) || Tools.isDemoProfile(requireContext())){ - Toast.makeText(requireContext(), R.string.toast_not_available_demo, Toast.LENGTH_LONG).show(); + if(!hasOnlineProfile()){ + hasNoOnlineProfileDialog(requireActivity()); } else { Tools.swapFragment(requireActivity(), fragmentClass, tag, null); }