re login when necessary

This commit is contained in:
Tungstend 2022-11-27 17:37:52 +08:00
parent 944cf97486
commit 38e8a618af
7 changed files with 163 additions and 23 deletions

View File

@ -214,21 +214,25 @@ public class AccountListItem {
if (account instanceof ClassicAccount) {
CountDownLatch latch = new CountDownLatch(1);
AtomicReference<AuthInfo> res = new AtomicReference<>(null);
ClassicAccountLoginDialog dialog = new ClassicAccountLoginDialog(FCLPath.CONTEXT, (ClassicAccount) account, it -> {
res.set(it);
latch.countDown();
}, latch::countDown);
dialog.show();
Schedulers.androidUIThread().execute(() -> {
ClassicAccountLoginDialog dialog = new ClassicAccountLoginDialog(FCLPath.CONTEXT, (ClassicAccount) account, it -> {
res.set(it);
latch.countDown();
}, latch::countDown);
dialog.show();
});
latch.await();
return Optional.ofNullable(res.get()).orElseThrow(CancellationException::new);
} else if (account instanceof OAuthAccount) {
CountDownLatch latch = new CountDownLatch(1);
AtomicReference<AuthInfo> res = new AtomicReference<>(null);
OAuthAccountLoginDialog dialog = new OAuthAccountLoginDialog(FCLPath.CONTEXT, (OAuthAccount) account, it -> {
res.set(it);
latch.countDown();
}, latch::countDown);
dialog.show();
Schedulers.androidUIThread().execute(() -> {
OAuthAccountLoginDialog dialog = new OAuthAccountLoginDialog(FCLPath.CONTEXT, (OAuthAccount) account, it -> {
res.set(it);
latch.countDown();
}, latch::countDown);
dialog.show();
});
latch.await();
return Optional.ofNullable(res.get()).orElseThrow(CancellationException::new);
}

View File

@ -41,6 +41,7 @@ import com.tungsten.fclcore.task.Task;
import com.tungsten.fclcore.task.TaskExecutor;
import com.tungsten.fclcore.util.StringUtils;
import com.tungsten.fcllibrary.component.FCLAdapter;
import com.tungsten.fcllibrary.component.dialog.FCLAlertDialog;
import com.tungsten.fcllibrary.component.dialog.FCLDialog;
import com.tungsten.fcllibrary.component.view.FCLButton;
import com.tungsten.fcllibrary.component.view.FCLEditText;
@ -182,7 +183,12 @@ public class CreateAccountDialog extends FCLDialog implements View.OnClickListen
if (exception instanceof NoSelectedCharacterException) {
dismiss();
} else {
Toast.makeText(getContext(), Accounts.localizeErrorMessage(getContext(), exception), Toast.LENGTH_SHORT).show();
FCLAlertDialog.Builder builder = new FCLAlertDialog.Builder(getContext());
builder.setAlertLevel(FCLAlertDialog.AlertLevel.ALERT);
builder.setMessage(Accounts.localizeErrorMessage(getContext(), exception));
builder.setCancelable(false);
builder.setNegativeButton(getContext().getString(com.tungsten.fcllibrary.R.string.dialog_positive), null);
builder.create().show();
}
login.setEnabled(true);
cancel.setEnabled(true);
@ -263,16 +269,12 @@ public class CreateAccountDialog extends FCLDialog implements View.OnClickListen
this.view = LayoutInflater.from(context).inflate(R.layout.dialog_create_account_microsoft, null);
Handler handler = new Handler();
FXUtils.onChangeAndOperate(CreateAccountDialog.getInstance().deviceCode, deviceCode -> {
handler.post(() -> {
if (deviceCode != null) {
AndroidUtils.copyText(context, deviceCode.getUserCode());
}
});
});
holder.add(Accounts.OAUTH_CALLBACK.onGrantDeviceCode.registerWeak(value -> {
CreateAccountDialog.getInstance().deviceCode.set(value);
FXUtils.onChangeAndOperate(CreateAccountDialog.getInstance().deviceCode, deviceCode -> handler.post(() -> {
if (deviceCode != null) {
AndroidUtils.copyText(context, deviceCode.getUserCode());
}
}));
holder.add(Accounts.OAUTH_CALLBACK.onGrantDeviceCode.registerWeak(value -> CreateAccountDialog.getInstance().deviceCode.set(value)));
}
@Override

View File

@ -1,20 +1,91 @@
package com.tungsten.fcl.ui.account;
import static com.tungsten.fclcore.util.Logging.LOG;
import android.content.Context;
import android.view.View;
import androidx.annotation.NonNull;
import com.tungsten.fcl.R;
import com.tungsten.fcl.game.OAuthServer;
import com.tungsten.fcl.setting.Accounts;
import com.tungsten.fcl.util.AndroidUtils;
import com.tungsten.fcl.util.FXUtils;
import com.tungsten.fcl.util.WeakListenerHolder;
import com.tungsten.fclcore.auth.AuthInfo;
import com.tungsten.fclcore.auth.OAuthAccount;
import com.tungsten.fclcore.fakefx.beans.property.ObjectProperty;
import com.tungsten.fclcore.fakefx.beans.property.SimpleObjectProperty;
import com.tungsten.fclcore.task.Schedulers;
import com.tungsten.fclcore.task.Task;
import com.tungsten.fcllibrary.component.dialog.FCLAlertDialog;
import com.tungsten.fcllibrary.component.dialog.FCLDialog;
import com.tungsten.fcllibrary.component.view.FCLButton;
import java.util.function.Consumer;
import java.util.logging.Level;
public class OAuthAccountLoginDialog extends FCLDialog {
public class OAuthAccountLoginDialog extends FCLDialog implements View.OnClickListener {
private FCLButton positive;
private FCLButton negative;
private final OAuthAccount account;
private final Consumer<AuthInfo> success;
private final Runnable failed;
private final ObjectProperty<OAuthServer.GrantDeviceCodeEvent> deviceCode = new SimpleObjectProperty<>();
private final WeakListenerHolder holder = new WeakListenerHolder();
public OAuthAccountLoginDialog(@NonNull Context context, OAuthAccount account, Consumer<AuthInfo> success, Runnable failed) {
super(context);
this.account = account;
this.success = success;
this.failed = failed;
setContentView(R.layout.dialog_relogin_oauth);
FXUtils.onChangeAndOperate(deviceCode, deviceCode -> Schedulers.androidUIThread().execute(() -> {
if (deviceCode != null) {
AndroidUtils.copyText(getContext(), deviceCode.getUserCode());
}
}));
holder.add(Accounts.OAUTH_CALLBACK.onGrantDeviceCode.registerWeak(deviceCode::set));
positive = findViewById(R.id.login);
negative = findViewById(R.id.cancel);
positive.setOnClickListener(this);
negative.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (view == positive) {
positive.setEnabled(false);
negative.setEnabled(false);
Task.supplyAsync(account::logInWhenCredentialsExpired)
.whenComplete(Schedulers.androidUIThread(), (authInfo, exception) -> {
if (exception == null) {
success.accept(authInfo);
dismiss();
} else {
LOG.log(Level.INFO, "Failed to login when credentials expired: " + account, exception);
FCLAlertDialog.Builder builder = new FCLAlertDialog.Builder(getContext());
builder.setAlertLevel(FCLAlertDialog.AlertLevel.ALERT);
builder.setMessage(Accounts.localizeErrorMessage(getContext(), exception));
builder.setCancelable(false);
builder.setNegativeButton(getContext().getString(com.tungsten.fcllibrary.R.string.dialog_positive), null);
builder.create().show();
}
positive.setEnabled(true);
negative.setEnabled(true);
}).start();
}
if (view == negative) {
failed.run();
dismiss();
}
}
}

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -0,0 +1,57 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="400dp"
android:layout_height="match_parent"
android:padding="10dp"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.tungsten.fcllibrary.component.view.FCLTextView
android:id="@+id/title"
android:text="@string/account_login_refresh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.5"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/title"
app:layout_constraintBottom_toTopOf="@+id/login">
<androidx.appcompat.widget.LinearLayoutCompat
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.tungsten.fcllibrary.component.view.FCLTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/account_login_refresh_microsoft_hint"
android:textSize="13sp"/>
</androidx.appcompat.widget.LinearLayoutCompat>
</ScrollView>
<com.tungsten.fcllibrary.component.view.FCLButton
android:id="@+id/login"
android:text="@string/account_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
<com.tungsten.fcllibrary.component.view.FCLButton
android:id="@+id/cancel"
android:text="@string/dialog_negative"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -52,7 +52,7 @@
<string name="account_methods_microsoft_close_page">Microsoft 账户登录完成</string>
<string name="account_methods_microsoft_hint">请点击“登录”按钮,稍后将自动复制一段代码,在打开的浏览器页面中输入复制的代码并完成登录过程。</string>
<string name="account_login_refresh">重新登录</string>
<string name="account.login.refresh.microsoft.hint">由于账号授权无效,需要重新登录您的微软账号\n请点击“确认”按钮稍后将自动复制一段代码在打开的浏览器页面中输入复制的代码并完成登录过程。</string>
<string name="account_login_refresh_microsoft_hint">由于账号授权无效,需要重新登录您的微软账号\n请点击“确认”按钮稍后将自动复制一段代码在打开的浏览器页面中输入复制的代码并完成登录过程。</string>
<string name="account_select_character">选择角色</string>
<string name="account_skin_upload">选择皮肤文件</string>
<string name="account_state_no_account">没有账户</string>

View File

@ -60,7 +60,7 @@
<string name="account_methods_microsoft_close_page">Microsoft account authorization is now completed.</string>
<string name="account_methods_microsoft_hint">Please click on the "Login" button, a piece of code will be automatically copied later, enter the code in the opened browser to finish the login process.</string>
<string name="account_login_refresh">Re-login</string>
<string name="account.login.refresh.microsoft.hint">Because the account authorization is invalid, you need to re-add your Microsoft account\nPlease click on the "OK" button, a piece of code will be automatically copied later, enter the code in the opened browser to finish the login process.</string>
<string name="account_login_refresh_microsoft_hint">Because the account authorization is invalid, you need to re-add your Microsoft account\nPlease click on the "OK" button, a piece of code will be automatically copied later, enter the code in the opened browser to finish the login process.</string>
<string name="account_select_character">Select character</string>
<string name="account_skin_upload">Select skin file</string>
<string name="account_state_no_account">No account</string>