Text descriptions during Microsoft login

This commit is contained in:
artdeell 2023-01-25 21:43:55 +03:00
parent ca2e9f8199
commit 843e13bd6b
5 changed files with 67 additions and 11 deletions

View File

@ -2,7 +2,6 @@ package com.kdt.mcgui;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
@ -30,11 +29,11 @@ import net.kdt.pojavlaunch.Tools;
import net.kdt.pojavlaunch.authenticator.listener.DoneListener;
import net.kdt.pojavlaunch.authenticator.listener.ErrorListener;
import net.kdt.pojavlaunch.authenticator.listener.ProgressListener;
import net.kdt.pojavlaunch.authenticator.microsoft.PresentedException;
import net.kdt.pojavlaunch.authenticator.microsoft.MicrosoftBackgroundLogin;
import net.kdt.pojavlaunch.extra.ExtraConstants;
import net.kdt.pojavlaunch.extra.ExtraCore;
import net.kdt.pojavlaunch.extra.ExtraListener;
import net.kdt.pojavlaunch.services.ProgressService;
import net.kdt.pojavlaunch.value.MinecraftAccount;
import java.io.File;
@ -93,7 +92,12 @@ public class mcAccountSpinner extends AppCompatSpinner implements AdapterView.On
private final ErrorListener mErrorListener = errorMessage -> {
mLoginBarPaint.setColor(Color.RED);
Tools.showError(getContext(), errorMessage);
if(errorMessage instanceof PresentedException) {
PresentedException exception = (PresentedException) errorMessage;
Tools.showError(getContext(), exception.toString(getContext()), exception.getCause());
}else {
Tools.showError(getContext(), errorMessage);
}
invalidate();
};

View File

@ -567,21 +567,24 @@ public final class Tools {
}
public static void showError(final Context ctx, final Throwable e, final boolean exitIfOk) {
showError(ctx, R.string.global_error, 0 ,e, exitIfOk, false);
showError(ctx, R.string.global_error, null ,e, exitIfOk, false);
}
public static void showError(final Context ctx, final int rolledMessage, final Throwable e) {
showError(ctx, R.string.global_error, ctx.getString(rolledMessage), e, false, false);
}
public static void showError(final Context ctx, final String rolledMessage, final Throwable e) {
showError(ctx, R.string.global_error, rolledMessage, e, false, false);
}
public static void showError(final Context ctx, final int titleId, final Throwable e, final boolean exitIfOk) {
showError(ctx, titleId, 0, e, exitIfOk, false);
showError(ctx, titleId, null, e, exitIfOk, false);
}
private static void showError(final Context ctx, final int titleId, final int rolledMessage, final Throwable e, final boolean exitIfOk, final boolean showMore) {
private static void showError(final Context ctx, final int titleId, final String rolledMessage, final Throwable e, final boolean exitIfOk, final boolean showMore) {
e.printStackTrace();
Runnable runnable = () -> {
final String errMsg = showMore ? printToString(e) : rolledMessage != 0 ? ctx.getString(rolledMessage) : e.getMessage();
final String errMsg = showMore ? printToString(e) : rolledMessage != null ? rolledMessage : e.getMessage();
AlertDialog.Builder builder = new AlertDialog.Builder((Context) ctx)
.setTitle(titleId)
.setMessage(errMsg)

View File

@ -3,12 +3,14 @@ package net.kdt.pojavlaunch.authenticator.microsoft;
import static net.kdt.pojavlaunch.PojavApplication.sExecutorService;
import android.os.Looper;
import android.util.ArrayMap;
import android.util.Log;
import androidx.annotation.Nullable;
import com.kdt.mcgui.ProgressLayout;
import net.kdt.pojavlaunch.R;
import net.kdt.pojavlaunch.Tools;
import net.kdt.pojavlaunch.value.MinecraftAccount;
import net.kdt.pojavlaunch.authenticator.listener.*;
@ -39,6 +41,15 @@ public class MicrosoftBackgroundLogin {
private final boolean mIsRefresh;
private final String mAuthCode;
private final android.os.Handler mHandler = new android.os.Handler(Looper.getMainLooper());
private static final Map<Long, Integer> XSTS_ERRORS;
static {
XSTS_ERRORS = new ArrayMap<>();
XSTS_ERRORS.put(2148916233L, R.string.xerr_no_account);
XSTS_ERRORS.put(2148916235L, R.string.xerr_not_available);
XSTS_ERRORS.put(2148916236L ,R.string.xerr_adult_verification);
XSTS_ERRORS.put(2148916237L ,R.string.xerr_adult_verification);
XSTS_ERRORS.put(2148916238L ,R.string.xerr_child);
}
/* Fields used to fill the account */
public boolean isRefresh;
@ -181,7 +192,7 @@ public class MicrosoftBackgroundLogin {
}
/** @return [uhs, token]*/
private String[] acquireXsts(String xblToken) throws IOException, JSONException {
private String[] acquireXsts(String xblToken) throws IOException, JSONException, PresentedException {
URL url = new URL(xstsAuthUrl);
JSONObject data = new JSONObject();
@ -211,6 +222,15 @@ public class MicrosoftBackgroundLogin {
Log.i("MicrosoftLogin","Xbl Xsts = " + token + "; Uhs = " + uhs);
return new String[]{uhs, token};
//acquireMinecraftToken(uhs,jo.getString("Token"));
}else if(conn.getResponseCode() == 401) {
String responseContents = Tools.read(conn.getErrorStream());
JSONObject jo = new JSONObject(responseContents);
long xerr = jo.optLong("XErr", -1);
Integer locale_id = XSTS_ERRORS.get(xerr);
if(locale_id != null) {
throw new PresentedException(new RuntimeException(responseContents), locale_id);
}
throw new PresentedException(new RuntimeException(responseContents), R.string.xerr_unknown, xerr);
}else{
throwResponseError(conn);
}
@ -248,7 +268,7 @@ public class MicrosoftBackgroundLogin {
return null;
}
private void checkMcProfile(String mcAccessToken) throws IOException, JSONException {
private void checkMcProfile(String mcAccessToken) throws IOException, JSONException, PresentedException {
URL url = new URL(mcProfileUrl);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
@ -274,8 +294,8 @@ public class MicrosoftBackgroundLogin {
}else{
Log.i("MicrosoftLogin","It seems that this Microsoft Account does not own the game.");
doesOwnGame = false;
throwResponseError(conn);
throw new PresentedException(new RuntimeException(conn.getResponseMessage()), R.string.minecraft_not_owned);
//throwResponseError(conn);
}
}

View File

@ -0,0 +1,23 @@
package net.kdt.pojavlaunch.authenticator.microsoft;
import android.content.Context;
public class PresentedException extends Exception {
final int localizationStringId;
final Object[] extraArgs;
public PresentedException(int localizationStringId, Object... extraArgs) {
this.localizationStringId = localizationStringId;
this.extraArgs = extraArgs;
}
public PresentedException(Throwable throwable, int localizationStringId, Object... extraArgs) {
super(throwable);
this.localizationStringId = localizationStringId;
this.extraArgs = extraArgs;
}
public String toString(Context context) {
return context.getString(localizationStringId, extraArgs);
}
}

View File

@ -325,4 +325,10 @@
<string name="no_saved_accounts">No saved accounts</string>
<string name="downloading_versions">Downloading version list…</string>
<string name="mc_download_failed">Failed to download Minecraft! This could be due to bad network connection, incorrectly placed files, etc..</string>
<string name="xerr_unknown">Unknown Xbox Live API error %d</string>
<string name="xerr_no_account">You don\'t seem to have an Xbox Live account. Please log in once on https://minecraft.net/ and try again.</string>
<string name="xerr_adult_verification">An adult needs to verify your account.</string>
<string name="xerr_child">Your account is a child account, and needs to be added into a Family in order to log in.</string>
<string name="minecraft_not_owned">It seems like this account does not have a Minecraft profile. If you have Xbox Game Pass, please log in on https://minecraft.net/ and set it up.</string>
<string name="xerr_not_available">Xbox Live is not available in your country</string>
</resources>