qol[Demo]: Turn demo mode error into box

TODO: actually make these translatable
This commit is contained in:
alexytomi 2025-06-19 18:49:57 +08:00
parent e7ec20a79c
commit 17c435da39
6 changed files with 82 additions and 21 deletions

View File

@ -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;
}
}

View File

@ -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<MinecraftAccount> getAllProfiles(){
List<MinecraftAccount> mcAccountList = new ArrayList<>();;
for (String accountName : getAllProfilesList()){
mcAccountList.add(MinecraftAccount.load(accountName));
}
return mcAccountList;
}
public static List<String> getAllProfilesList(){
List<String> 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();

View File

@ -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);
}
}

View File

@ -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()) {

View File

@ -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

View File

@ -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<? extends Fragment> 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);
}