| | |
| | | package com.yeshi.location.plugins; |
| | | |
| | | import android.app.Activity; |
| | | import android.app.AlertDialog; |
| | | import android.content.DialogInterface; |
| | | import android.graphics.Color; |
| | | import android.util.Log; |
| | | import android.widget.ImageView; |
| | | |
| | | import com.mobile.auth.gatewayauth.AuthUIConfig; |
| | | import com.mobile.auth.gatewayauth.PhoneNumberAuthHelper; |
| | | import com.mobile.auth.gatewayauth.ResultCode; |
| | | import com.mobile.auth.gatewayauth.TokenResultListener; |
| | | import com.mobile.auth.gatewayauth.model.TokenRet; |
| | | import com.yeshi.location.R; |
| | | import com.yeshi.location.utils.DimenUtils; |
| | | |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | |
| | | import androidx.annotation.NonNull; |
| | | import androidx.annotation.Nullable; |
| | | import io.flutter.plugin.common.BasicMessageChannel; |
| | | import io.flutter.plugin.common.BinaryMessenger; |
| | | import io.flutter.plugin.common.MethodCall; |
| | | import io.flutter.plugin.common.MethodChannel; |
| | | import io.flutter.plugin.common.StandardMessageCodec; |
| | | import io.flutter.plugin.common.StringCodec; |
| | | |
| | | public class FlutterAliyunPhoneNumberAuthPlugins implements MethodChannel.MethodCallHandler { |
| | | public static String CHANNEL = "com.yeshi.location/aliyunOneKeyLogin"; // 分析1 |
| | | public class FlutterAliyunPhoneNumberAuthPlugins implements BasicMessageChannel.MessageHandler<Object> { |
| | | |
| | | static MethodChannel channel; |
| | | private Activity activity; |
| | | private static final String TAG = "AliyunPhoneNumberAuth"; |
| | | |
| | | private final Activity activity; |
| | | private final BasicMessageChannel<Object> messageChannel; |
| | | |
| | | private PhoneNumberAuthHelper mPhoneNumberAuthHelper; |
| | | private BasicMessageChannel.Reply<Object> reply; |
| | | |
| | | private FlutterAliyunPhoneNumberAuthPlugins(Activity activity) { |
| | | |
| | | private FlutterAliyunPhoneNumberAuthPlugins(Activity activity, BinaryMessenger messager) { |
| | | this.activity = activity; |
| | | this.messageChannel = new BasicMessageChannel<Object>(messager, "AliyunPhoneNumberAuth", StandardMessageCodec.INSTANCE); |
| | | messageChannel.setMessageHandler(this); |
| | | } |
| | | |
| | | public static void registerWith(Activity activity, BinaryMessenger messager) { |
| | | channel = new MethodChannel(messager, CHANNEL); |
| | | FlutterAliyunPhoneNumberAuthPlugins instance = new FlutterAliyunPhoneNumberAuthPlugins(activity); |
| | | channel.setMethodCallHandler(instance); |
| | | public static FlutterAliyunPhoneNumberAuthPlugins registerWith(Activity activity, BinaryMessenger messager) { |
| | | return new FlutterAliyunPhoneNumberAuthPlugins(activity, messager); |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) { // 分析 2 |
| | | String method = methodCall.method; |
| | | public void onMessage(@Nullable Object message, @NonNull BasicMessageChannel.Reply<Object> reply) { |
| | | this.reply = reply; |
| | | Map<String, String> arguments = (Map<String, String>) message; |
| | | String method = arguments.get("method"); |
| | | switch (method) { |
| | | //初始化 |
| | | case "init": |
| | | initOneKeyLogin(); |
| | | result.success(true); |
| | | initOneKeyLogin(arguments.get("secret"), arguments.get("privacy"), arguments.get("protocol")); |
| | | Map<String, Object> map = new HashMap<>(); |
| | | map.put("code", 0); |
| | | reply.reply(map); |
| | | break; |
| | | default: |
| | | result.notImplemented(); |
| | | |
| | | case "checkEnv": |
| | | mPhoneNumberAuthHelper.checkEnvAvailable(2); |
| | | break; |
| | | case "startLogin": |
| | | mPhoneNumberAuthHelper.getLoginToken(activity, 5000); |
| | | break; |
| | | } |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 向Dart发送消息,并接受Dart的反馈 |
| | | * |
| | | * @param message 要给Dart发送的消息内容 |
| | | * @param callback 来自Dart的反馈 |
| | | */ |
| | | void send(String message, BasicMessageChannel.Reply<Object> callback) { |
| | | messageChannel.send(message, callback); |
| | | } |
| | | |
| | | |
| | | private void initOneKeyLogin() { |
| | | sdkInit(""); |
| | | initUI(); |
| | | private void initOneKeyLogin(String secretInfo, String privacyUrl, String userProtocolUrl) { |
| | | sdkInit(secretInfo); |
| | | initUI(privacyUrl, userProtocolUrl); |
| | | } |
| | | |
| | | private void initUI(String privacy, String protocol) { |
| | | |
| | | |
| | | private void initUI() { |
| | | |
| | | int width = DimenUtils.getScreenWidth(this); |
| | | int widthDP = DimenUtils.px2dip(this, width); |
| | | int width = DimenUtils.getScreenWidth(activity); |
| | | int widthDP = DimenUtils.px2dip(activity, width); |
| | | AuthUIConfig uiCOnfig = new AuthUIConfig.Builder() |
| | | .setPageBackgroundPath("shape_login_onkey_bg") |
| | | |
| | |
| | | .setNavColor(Color.WHITE) |
| | | .setNavHidden(false) |
| | | .setNavReturnImgPath("ic_login_close") |
| | | .setNavReturnImgWidth(22) |
| | | .setNavReturnImgHeight(22) |
| | | .setNavReturnImgWidth(32) |
| | | .setNavReturnImgHeight(32) |
| | | .setNavReturnScaleType(ImageView.ScaleType.FIT_XY) |
| | | .setWebNavColor(Color.BLACK) |
| | | .setWebNavReturnImgPath("ic_back_two") |
| | | // .setWebNavReturnImgPath("ic_back_two") |
| | | .setWebViewStatusBarColor(Color.WHITE) |
| | | .setStatusBarColor(Color.WHITE) |
| | | |
| | | .setLogoWidth(147) |
| | | .setLogoHeight(36) |
| | | .setLogoImgPath("ic_login_logo") |
| | | .setLogoWidth(0) |
| | | .setLogoHeight(0) |
| | | // .setLogoImgPath("ic_login_logo") |
| | | .setLogoOffsetY(24 - 10) |
| | | |
| | | .setSloganOffsetY(171 - 10) |
| | | .setSloganOffsetY(125 - 10) |
| | | .setSloganTextSize(12) |
| | | .setSloganTextColor(Color.parseColor("#F804F5")) |
| | | .setSloganTextColor(activity.getResources().getColor(R.color.onekey_login_theme_color)) |
| | | |
| | | .setLogBtnText("⼀键登录") |
| | | .setLogBtnTextSize(17) |
| | | .setLogBtnTextColor(Color.parseColor("#F4DE4A")) |
| | | .setLogBtnTextColor(Color.WHITE) |
| | | .setLogBtnBackgroundPath("shape_login_btn") |
| | | .setLogBtnOffsetY(229 - 10) |
| | | .setLogBtnOffsetY(170 - 10) |
| | | .setLogBtnHeight(44) |
| | | |
| | | |
| | | .setDialogWidth(305 * widthDP / 375) |
| | | .setDialogHeight(454) |
| | | .setDialogHeight(378) |
| | | .setNavColor(Color.RED) |
| | | .setSwitchAccHidden(true) |
| | | .setNumberColor(Color.parseColor("#0052F6")) |
| | | .setNumberColor(activity.getResources().getColor(R.color.onekey_login_theme_color)) |
| | | .setNumberSize(36) |
| | | .setNumFieldOffsetY(128 - 10) |
| | | .setNumFieldOffsetY(65) |
| | | |
| | | .setAppPrivacyOne("《用户服务协议》", BeibeiConstant.USER_AGREEMENT) |
| | | .setAppPrivacyTwo("《隐私政策》", BeibeiConstant.PRIVACY_POLICY) |
| | | .setAppPrivacyColor(Color.parseColor("#B3B8D3"), Color.parseColor("#51B3FF")) |
| | | .setAppPrivacyOne("《用户服务协议》", protocol) |
| | | .setAppPrivacyTwo("《隐私政策》", privacy) |
| | | .setAppPrivacyColor(activity.getResources().getColor(R.color.onekey_login_notify_color), activity.getResources().getColor(R.color.onekey_login_theme_color)) |
| | | .setPrivacyBefore("登录即表示同意") |
| | | .setPrivacyEnd("") |
| | | .setCheckedImgPath("ic_login_privacy_checked") |
| | | .setUncheckedImgPath("ic_login_privacy_unchecked") |
| | | .setPrivacyTextSize(11) |
| | | .setPrivacyState(false) |
| | | .setVendorPrivacyPrefix("《") |
| | |
| | | |
| | | mPhoneNumberAuthHelper.setAuthUIConfig(uiCOnfig); |
| | | } |
| | | |
| | | private void sdkInit(String secretInfo) { |
| | | mPhoneNumberAuthHelper = PhoneNumberAuthHelper.getInstance(activity.getApplicationContext(), new TokenResultListener(){ |
| | | mPhoneNumberAuthHelper = PhoneNumberAuthHelper.getInstance(activity.getApplicationContext(), new TokenResultListener() { |
| | | |
| | | @Override |
| | | public void onTokenSuccess(String s) { |
| | | Log.i(TAG, "onTokenSuccess:" + s); |
| | | TokenRet tokenRet = null; |
| | | try { |
| | | tokenRet = TokenRet.fromJson(s); |
| | | |
| | | switch (tokenRet.getCode()) { |
| | | case ResultCode.CODE_ERROR_ENV_CHECK_SUCCESS: { |
| | | Log.i(TAG |
| | | , "终端环境校验成功:" + s); |
| | | Map<String, Object> map = new HashMap<>(); |
| | | map.put("code", 0); |
| | | map.put("msg", "环境监测正常"); |
| | | reply.reply(map); |
| | | } |
| | | break; |
| | | |
| | | case ResultCode.CODE_START_AUTHPAGE_SUCCESS: |
| | | Log.i(TAG, "唤起授权页成功:" + s); |
| | | break; |
| | | |
| | | case ResultCode.CODE_ERROR_USER_CANCEL: { |
| | | Log.i(TAG, "用户取消操作:" + s); |
| | | Map<String, Object> map = new HashMap<>(); |
| | | map.put("code", Integer.parseInt(tokenRet.getCode())); |
| | | map.put("msg", "取消操作"); |
| | | reply.reply(map); |
| | | } |
| | | break; |
| | | |
| | | |
| | | case ResultCode.CODE_SUCCESS: { |
| | | Log.i(TAG, "获取token成功:" + s); |
| | | mPhoneNumberAuthHelper.setAuthListener(null); |
| | | Map<String, Object> map = new HashMap<>(); |
| | | map.put("code", 0); |
| | | map.put("token", tokenRet.getToken()); |
| | | map.put("msg", "获取token成功"); |
| | | reply.reply(map); |
| | | } |
| | | break; |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void onTokenFailed(String s) { |
| | | |
| | | Log.i(TAG, "onTokenFailed:" + s); |
| | | TokenRet |
| | | tokenRet = TokenRet.fromJson(s); |
| | | Map<String, Object> map = new HashMap<>(); |
| | | map.put("code", Integer.parseInt(tokenRet.getCode())); |
| | | map.put("msg", tokenRet.getMsg()); |
| | | reply.reply(map); |
| | | } |
| | | }); |
| | | mPhoneNumberAuthHelper.getReporter().setLoggerEnable(true); |
| | |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | } |