Skip to content

Commit

Permalink
🔨 Fix android timeout issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Kangkang committed Mar 3, 2023
1 parent ca2cb33 commit eb81303
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 104 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ that can be found in the LICENSE file. -->

# Changelog

## 0.6.0
### 🔨 Fix android timeout issue

* 🙇🏻‍♂️️ loginWithConfig with timeout

## 0.5.0
### 💥 Improve code

Expand Down
1 change: 0 additions & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ dependencies{
// https://mvnrepository.com/artifact/com.google.code.gson/gson
implementation 'com.google.code.gson:gson:2.9.0'


implementation(name:'auth_number_product-2.12.11.2-log-online-standard-release', ext:'aar')
implementation(name:'crashshield-2.1.4-release', ext:'aar')
implementation(name:'main-2.1.4-release', ext:'aar')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ public class AuthClient {

private boolean initSdkSuccess = false;

private int mLoginTimeout = 5;
///超时,默认5000,单位毫秒
private int mLoginTimeout = 5000;

private static volatile AuthClient instance;

Expand Down Expand Up @@ -118,17 +119,16 @@ public void initSdk(Object arguments, @NonNull MethodChannel.Result result) {
public void onTokenSuccess(String s) {
TokenRet tokenRet;
try {
System.out.println("String s: " + s);
Log.d(TAG, "initSdk onTokenSuccess: " + s);
tokenRet = TokenRet.fromJson(s);
Log.d(TAG, "onTokenSuccess: " + tokenRet);
AuthResponseModel responseModel = AuthResponseModel.fromTokenRect(tokenRet);
//消息回调到flutter
mChannel.invokeMethod(DART_CALL_METHOD_ON_INIT, responseModel.toJson());
if (ResultCode.CODE_SUCCESS.equals(tokenRet.getCode())) {
sdkAvailable = true;
} else if (ResultCode.CODE_ERROR_ENV_CHECK_SUCCESS.equals(tokenRet.getCode())) {
//终端支持认证 当前环境可以进行一键登录 并且加速拉起授权页面
accelerateLoginPage();
accelerateLoginPage(mLoginTimeout);
}
} catch (Exception e) {
Log.e(TAG, "错误", e);
Expand All @@ -142,7 +142,7 @@ public void onTokenSuccess(String s) {

@Override
public void onTokenFailed(String s) {
Log.i(TAG, "onTokenFailed:" + s);
Log.d(TAG, "initSdk onTokenFailed: " + s);
sdkAvailable = false;
TokenRet tokenRet;
try {
Expand Down Expand Up @@ -179,46 +179,25 @@ public void onTokenFailed(String s) {
}
}

/**
* 检查环境是否可用
*/
// public void checkEnv() {
// mAuthHelper.checkEnvAvailable(PhoneNumberAuthHelper.SERVICE_TYPE_LOGIN);
// }

/**
* 在不是一进app就需要登录的场景 建议调用此接口 加速拉起一键登录页面
* 等到用户点击登录的时候 授权页可以秒拉
* 预取号的成功与否不影响一键登录功能,所以不需要等待预取号的返回。
*/
public void accelerateLoginPage() {
public void accelerateLoginPage(int timeout) {
if (Objects.isNull(mAuthHelper) || !sdkAvailable) {
AuthResponseModel responseModel = AuthResponseModel.initFailed(initFailedMsg);
// eventSink.success(responseModel.toJson());
mChannel.invokeMethod(DART_CALL_METHOD_ON_INIT, responseModel.toJson());
return;
}
mAuthHelper.accelerateLoginPage(mLoginTimeout, new PreLoginResultListener() {
mAuthHelper.accelerateLoginPage(timeout, new PreLoginResultListener() {
@Override
public void onTokenSuccess(String s) {
try {
Log.d(TAG, "accelerateLoginPage onTokenSuccess: " + s);
TokenRet tokenRet = TokenRet.fromJson(s);
if (ResultCode.CODE_SUCCESS.equals(tokenRet.getCode())) {
AuthResponseModel responseModel = AuthResponseModel.customModel(
MSG_GET_MASK_SUCCESS, preLoginSuccessMsg
);
mChannel.invokeMethod(DART_CALL_METHOD_ON_INIT, responseModel.toJson());
} else {
AuthResponseModel responseModel = AuthResponseModel.fromTokenRect(tokenRet);
mChannel.invokeMethod(DART_CALL_METHOD_ON_INIT, responseModel.toJson());
}
} catch (Exception e) {
AuthResponseModel responseModel = AuthResponseModel.tokenDecodeFailed();
// eventSink.success(responseModel.toJson());
mChannel.invokeMethod(DART_CALL_METHOD_ON_INIT, responseModel.toJson());
e.printStackTrace();
}
Log.d(TAG, "accelerateLoginPage onTokenSuccess: " + s);
AuthResponseModel responseModel = AuthResponseModel.customModel(
MSG_GET_MASK_SUCCESS, preLoginSuccessMsg
);
mChannel.invokeMethod(DART_CALL_METHOD_ON_INIT, responseModel.toJson());
}

@Override
Expand All @@ -244,7 +223,6 @@ public void onTokenFailed(String s, String s1) {
public void getLoginToken(Object arguments, @NonNull MethodChannel.Result result) {
if (Objects.isNull(mAuthHelper) || !sdkAvailable) {
AuthResponseModel responseModel = AuthResponseModel.initFailed(initFailedMsg);
// eventSink.success(responseModel.toJson());
result.error(responseModel.getResultCode(), responseModel.getMsg(), null);
return;
}
Expand Down Expand Up @@ -273,6 +251,7 @@ public void onTokenSuccess(String s) {
activity.runOnUiThread(() -> {
TokenRet tokenRet;
try {

if (s != null && !s.equals("")) {
tokenRet = TokenRet.fromJson(s);
AuthResponseModel responseModel = AuthResponseModel.fromTokenRect(tokenRet);
Expand Down Expand Up @@ -375,7 +354,7 @@ public void getLoginTokenWithConfig(Object arguments, @NonNull MethodChannel.Res
tokenResultListener = new TokenResultListener() {
@Override
public void onTokenSuccess(String s) {
// Log.w(TAG,"获取Token成功:"+s);
Log.w(TAG, "getLoginTokenWithConfig onTokenSuccess:" + s);
activity.runOnUiThread(() -> {
TokenRet tokenRet;
try {
Expand All @@ -397,7 +376,6 @@ public void onTokenSuccess(String s) {

}
}

Log.i(TAG, "onTokenSuccess tokenRet:" + tokenRet);
} else {
if (decoyMaskActivity != null) {
Expand All @@ -419,7 +397,7 @@ public void onTokenSuccess(String s) {

@Override
public void onTokenFailed(String s) {
Log.w(TAG, "获取Token失败:" + s + " ," + "DecoyMaskActivity.isRunning:" + decoyMaskActivity);
Log.w(TAG, "getLoginTokenWithConfig onTokenFailed:" + s + " ," + "DecoyMaskActivity.isRunning:" + decoyMaskActivity);
if (decoyMaskActivity != null) {
decoyMaskActivity.finish();

Expand Down Expand Up @@ -484,10 +462,11 @@ public void setFlutterPluginBinding(FlutterPlugin.FlutterPluginBinding flutterPl
}

public void setLoginTimeout(int timeout) {
this.mLoginTimeout = timeout;
this.mLoginTimeout = timeout * 1000;

}

public int getLoginTimeout() {
public Integer getLoginTimeout() {
return mLoginTimeout;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBindin

@Override
public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
Log.i(TAG,"call.method:"+call.method);
switch (call.method) {

case "getPlatformVersion":
result.success("Android " + android.os.Build.VERSION.RELEASE);
break;
Expand Down Expand Up @@ -113,6 +111,7 @@ public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBindin
public void onDetachedFromActivity() {
authClient.setFlutterPluginBinding(null);
authClient.getChannel().setMethodCallHandler(null);
authClient.setActivity(null);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,48 +19,50 @@
public class DecoyMaskActivity extends Activity {


public static String TAG = DecoyMaskActivity.class.getSimpleName();
public static final String TAG = DecoyMaskActivity.class.getSimpleName();

// public static boolean isRunning = false;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
// isRunning = true;

Log.i(TAG,"onCreate");
Log.i(TAG, "onCreate");
AuthClient authClient = AuthClient.getInstance();
AuthClient.decoyMaskActivity = this;
// override the auth path open enter animation
if (authClient.getAuthModel().getAuthUIStyle() == Constant.DIALOG_PORT){
overridePendingTransition(R.anim.zoom_in,R.anim.stay_animation);
}else{
overridePendingTransition(R.anim.slide_up,R.anim.stay_animation);
if (authClient.getAuthModel().getAuthUIStyle() == Constant.DIALOG_PORT) {
overridePendingTransition(R.anim.zoom_in, R.anim.stay_animation);
} else {
overridePendingTransition(R.anim.slide_up, R.anim.stay_animation);
}
PhoneNumberAuthHelper authHelper = authClient.mAuthHelper;
authHelper.getLoginToken(this.getBaseContext(),authClient.getLoginTimeout());
authHelper.getLoginToken(this.getBaseContext(), authClient.getLoginTimeout());
super.onCreate(savedInstanceState);
}

boolean isPause = false;

@Override
protected void onPause() {
Log.i(TAG,"onPause");
Log.i(TAG, "onPause");
isPause = true;
super.onPause();
}

@Override
protected void onResume() {
if (isPause){
Log.i(TAG, "onResume");

if (isPause) {
//TopActivityBack
Runnable runnable = new Runnable(){
Runnable runnable = new Runnable() {
@Override
public void run() {
finish();
finish();
}
};
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
scheduledExecutorService.schedule(runnable,0, TimeUnit.MILLISECONDS);
scheduledExecutorService.schedule(runnable, 0, TimeUnit.MILLISECONDS);
}
super.onResume();
}
Expand All @@ -69,7 +71,7 @@ public void run() {
@Override
public void finish() {
super.finish();
Log.i(TAG,"finish");
Log.i(TAG, "finish");
AuthClient.decoyMaskActivity = null;
}

Expand Down
48 changes: 4 additions & 44 deletions example/lib/pages/debug_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:flutter_ali_auth/flutter_ali_auth.dart';
import 'package:flutter_ali_auth_example/main.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';

///TODO:导入自己的[iosSdk][androidSdk]
import 'config.dart';

class DebugPage extends StatefulWidget {
Expand Down Expand Up @@ -170,11 +171,7 @@ class _DebugPageState extends State<DebugPage> {
AliAuthClient.handleEvent(onEvent: _onEvent);
bool? initSuccess = await AliAuthClient.initSdk(
authConfig: _authConfig,
).timeout(const Duration(seconds: 10), onTimeout: () {
_addLog(
"初始化SDK出现错误:${AuthResultCode.interfaceTimeout.message}");
return null;
});
);
if (!(initSuccess ?? false)) {
_addLog("初始化SDK失败");
}
Expand All @@ -187,43 +184,6 @@ class _DebugPageState extends State<DebugPage> {
}
},
),
// ElevatedButton(
// child: const Text('检查环境是否支持认证'),
// onPressed: () async {
// try {
// await AliAuthClient.checkVerifyEnable();
// } catch (e) {
// SmartDialog.dismiss(status: SmartStatus.loading);
// }
// },
// ),
// ElevatedButton(
// child: const Text('加速一键登录授权页弹起'),
// onPressed: () async {
// try {
// await AliAuthClient.accelerateLoginPage();
// } catch (e) {
// SmartDialog.dismiss(status: SmartStatus.loading);
// }
// },
// ),
// ElevatedButton(
// child: const Text('取消登录事件监听'),
// onPressed: () async {
// try {
// final success = await AliAuthClient.removeListener();
// if (!success) {
// SmartDialog.showToast("你还没对登录事件进行监听");
// _addLog('你还没对登录事件进行监听');
// } else {
// SmartDialog.showToast("取消监听成功");
// _addLog('取消监听成功');
// }
// } catch (e) {
// SmartDialog.dismiss(status: SmartStatus.loading);
// }
// },
// ),
],
),
),
Expand Down Expand Up @@ -340,7 +300,7 @@ class _DebugPageState extends State<DebugPage> {
);
await AliAuthClient.loginWithConfig(
authConfig: _authConfig,
timeout: 10,
timeout: 10000,
);
} on PlatformException catch (e) {
final AuthResultCode resultCode = AuthResultCode.fromCode(
Expand Down Expand Up @@ -412,7 +372,7 @@ class _DebugPageState extends State<DebugPage> {
navConfig: const NavConfig(
navIsHidden: true,
),
// backgroundImage: "images/app_bg.png",
backgroundImage: "images/app_bg.png",
logoConfig: LogoConfig(
logoIsHidden: false,
logoImage: "images/flutter_candies_logo.png",
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ packages:
path: ".."
relative: true
source: path
version: "0.4.1"
version: "0.5.0"
flutter_lints:
dependency: "direct dev"
description:
Expand Down
11 changes: 9 additions & 2 deletions ios/Classes/AuthUIBuilder/BuildFullScreenModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ extension AuthUIBuilder {
// MARK: - 构建全屏授权页面

func buildFullScreenModel(config: AuthUIConfig) -> TXCustomModel {
print("\(String(describing: config))")
var kHorizontal: Bool?
var kLoginButtonSize = CGSize()
let model = TXCustomModel()
Expand All @@ -35,6 +36,14 @@ extension AuthUIBuilder {
model.backgroundImage = image
model.backgroundImageContentMode = UIView.ContentMode.scaleAspectFill
}
} else {
if let backgroundColor = config.backgroundColor {
model.backgroundColor = backgroundColor.uicolor()
} else {
if #available(iOS 13.0, *) {
model.backgroundColor = UIColor.systemBackground
}
}
}

// customViewBlock
Expand Down Expand Up @@ -69,7 +78,6 @@ extension AuthUIBuilder {
model.logoIsHidden = false

if let logoImage = config.logoImage {

if let logoImageAssets = FlutterAssetImage(logoImage) {
model.logoImage = logoImageAssets
}
Expand Down Expand Up @@ -291,5 +299,4 @@ extension AuthUIBuilder {

return model
}

}
3 changes: 3 additions & 0 deletions ios/Classes/SwiftFlutterAliAuthPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,9 @@ public class SwiftFlutterAliAuthPlugin: NSObject, FlutterPlugin {
if let timeout = argumentList.last as? TimeInterval{
_timeout = timeout;
}
}else{
result(FlutterError(code: PNSCodeLoginControllerPresentFailed, message: "初始化失败,SDK未初始化或参数不正确", details: nil))
return
}


Expand Down
Loading

0 comments on commit eb81303

Please sign in to comment.