package com.secondworld.univeralsdk; 
 | 
  
 | 
import android.app.Activity; 
 | 
import android.os.Handler; 
 | 
import android.os.Message; 
 | 
import android.util.Log; 
 | 
  
 | 
import com.mgbase.utils.BaseAnchorImpl; 
 | 
import com.mgbase.utils.PreferenceUtils; 
 | 
import com.mgbase.utils.StringUtils; 
 | 
import com.mgpay.utils.MGPayAnchor; 
 | 
import com.reyun.tracking.sdk.Tracking; 
 | 
//import com.ss.android.common.lib.EventUtils; 
 | 
import com.xy.xypay.bean.LoginResult; 
 | 
import com.xy.xypay.bean.LogoutResult; 
 | 
import com.xy.xypay.bean.PayArgsCheckResult; 
 | 
import com.xy.xypay.bean.XPayArg; 
 | 
import com.xy.xypay.inters.XYLoginCallback; 
 | 
import com.xy.xypay.inters.XYLogoutCallback; 
 | 
import com.xy.xypay.inters.XYPayCallback; 
 | 
import com.xy.xypay.utils.XYPaySDK; 
 | 
  
 | 
import org.json.JSONException; 
 | 
import org.json.JSONObject; 
 | 
  
 | 
import java.util.HashMap; 
 | 
import java.util.Map; 
 | 
  
 | 
/** 
 | 
 * Created by Administrator on 2018/9/3 0003. 
 | 
 */ 
 | 
  
 | 
public class KYPlatformUtil 
 | 
{ 
 | 
    private static final String TAG = "KYPlatformUtil"; 
 | 
  
 | 
    private boolean m_Init = false; 
 | 
  
 | 
    private static KYPlatformUtil s_Instance; 
 | 
  
 | 
    public static KYPlatformUtil getInstance() 
 | 
    { 
 | 
        if (s_Instance == null) 
 | 
        { 
 | 
            s_Instance = new KYPlatformUtil(); 
 | 
        } 
 | 
        return s_Instance; 
 | 
    } 
 | 
  
 | 
    public boolean payProcessing = false; 
 | 
  
 | 
    private Map<String, Object> m_Message = new HashMap<>(); 
 | 
  
 | 
    private static final int CALL_QUIT = 1;//退出游戏 
 | 
    private static final int CALL_RESUME = 2;//返回按键处理 
 | 
  
 | 
    public Handler handler = new Handler(new Handler.Callback() 
 | 
    { 
 | 
        @Override 
 | 
        public boolean handleMessage(Message msg) 
 | 
        { 
 | 
            switch (msg.what) 
 | 
            { 
 | 
                case CALL_QUIT: 
 | 
                    m_Message.clear(); 
 | 
                    m_Message.put("code", CodeA2U.FreePlatformLogoutOk); 
 | 
                    UniversalUtil.sendMessageToUnity(m_Message); 
 | 
                    //悬浮球里切换帐号时退出游戏这里处理 
 | 
                    XYPaySDK.hideFloatWindow(mainActivity); 
 | 
  
 | 
                    break; 
 | 
                case CALL_RESUME: 
 | 
                    //返回处理 
 | 
                    break; 
 | 
                default: 
 | 
                    break; 
 | 
  
 | 
            } 
 | 
            return false; 
 | 
        } 
 | 
    }); 
 | 
  
 | 
    private Activity mainActivity; 
 | 
  
 | 
    public void init(final Activity activity) 
 | 
    { 
 | 
        if (m_Init) 
 | 
        { 
 | 
            return; 
 | 
        } 
 | 
        mainActivity = activity; 
 | 
  
 | 
        XYPaySDK.init(activity.getApplication(), new BaseAnchorImpl(), new MGPayAnchor()); 
 | 
        XYPaySDK.setHandler(handler); 
 | 
    } 
 | 
  
 | 
    private String m_AccountID; 
 | 
  
 | 
    public void login() 
 | 
    { 
 | 
        XYPaySDK.login(mainActivity, new XYLoginCallback() 
 | 
        { 
 | 
            @Override 
 | 
            public void onLogin(LoginResult loginResult) 
 | 
            { 
 | 
                //登录成功 
 | 
                if (loginResult.getCode() == LoginResult.LOGIN_CODE_SUCCESS) 
 | 
                { 
 | 
                    //Toast.makeText(mainActivity,"登录成功:"+loginResult.isBindPhone(),Toast.LENGTH_LONG).show(); 
 | 
                    String _uid = PreferenceUtils.getUid(mainActivity); 
 | 
                    String _token = PreferenceUtils.getToken(mainActivity);// 
 | 
                    String _appId = PreferenceUtils.getAppid(mainActivity); 
 | 
  
 | 
                    m_AccountID = _uid; 
 | 
  
 | 
                    try 
 | 
                    { 
 | 
                        JSONObject _info = new JSONObject(); 
 | 
                        _info.put("account", m_AccountID); 
 | 
                        _info.put("token", _token); 
 | 
                        _info.put("account_id", m_AccountID); 
 | 
                        _info.put("appId", _appId); 
 | 
                        m_Message.put("code", CodeA2U.FreePlatformLoginOk); 
 | 
                        m_Message.put("info", _info); 
 | 
                        UniversalUtil.sendMessageToUnity(m_Message); 
 | 
                    } catch (JSONException e) 
 | 
                    { 
 | 
                        e.printStackTrace(); 
 | 
                    } 
 | 
                } 
 | 
                else if (loginResult.getCode() == LoginResult.LOGIN_CODE_APPID_NOT_FOUND) 
 | 
                { 
 | 
                    m_Message.clear(); 
 | 
                    m_Message.put("code", CodeA2U.FreePlatformLoginFail); 
 | 
                    UniversalUtil.sendMessageToUnity(m_Message); 
 | 
                } 
 | 
                else if (loginResult.getCode() == LoginResult.LOGIN_CODE_FAILED) 
 | 
                { 
 | 
                    // Toast.makeText(mainActivity,"登录失败",Toast.LENGTH_LONG).show(); 
 | 
                    PreferenceUtils.setHasChangeAccount(mainActivity, "Y"); 
 | 
                    login(); 
 | 
                } 
 | 
                else if (loginResult.getCode() == LoginResult.LOGIN_CODE_CANCEL) 
 | 
                { 
 | 
                    m_Message.clear(); 
 | 
                    m_Message.put("code", CodeA2U.FreePlatformLoginFail); 
 | 
                    UniversalUtil.sendMessageToUnity(m_Message); 
 | 
                } 
 | 
            } 
 | 
        }); 
 | 
    } 
 | 
  
 | 
    // public void logout(final Activity activity) { } 
 | 
  
 | 
    public void pay(String productId, 
 | 
                    String productName, 
 | 
                    String money, 
 | 
                    String roleID, 
 | 
                    String roleName, 
 | 
                    String orderId, 
 | 
                    String extraData, 
 | 
                    String serverID, 
 | 
                    String notifyurl) 
 | 
    { 
 | 
        XPayArg payArg = new XPayArg(); 
 | 
        payArg.APP_USER_ID = roleID; 
 | 
        payArg.NOTIFY_URI = notifyurl; 
 | 
        String amount = money; 
 | 
        if (StringUtils.isEmpty(amount)) 
 | 
        { 
 | 
            payArg.AMOUNT = "0.01"; 
 | 
        } 
 | 
        else 
 | 
        { 
 | 
            payArg.AMOUNT = amount; 
 | 
        } 
 | 
        payArg.APP_EXT1 = extraData; 
 | 
        payArg.APP_NAME = UniversalUtil.getAppName(mainActivity); 
 | 
        payArg.APP_ORDER_ID = orderId; 
 | 
        payArg.APP_USER_NAME = roleName; 
 | 
        payArg.PRODUCT_ID = productId; 
 | 
        payArg.OPEN_UID = PreferenceUtils.getUid(mainActivity); 
 | 
        payArg.SID = serverID; 
 | 
        payArg.PRODUCT_NAME = productName; 
 | 
        XYPaySDK.pay(mainActivity, payArg, new XYPayCallback() 
 | 
        { 
 | 
            @Override 
 | 
            public void onPayFinished(int status) 
 | 
            { 
 | 
                Log.i(TAG, "支付回调结果: " + status); 
 | 
            } 
 | 
  
 | 
            //检测参数是否正确 
 | 
            public void onStart(PayArgsCheckResult result) 
 | 
            { 
 | 
                switch (result.getCode()) 
 | 
                { 
 | 
                    case PayArgsCheckResult.CHECK_RESULT_OK: 
 | 
                        break; 
 | 
                    case PayArgsCheckResult.NOT_INIT: 
 | 
                        Log.i(TAG, "未调用XYPaySDK.init()方法"); 
 | 
                        break; 
 | 
                    case PayArgsCheckResult.CHECK_RESULT_NULL: 
 | 
                        Log.i(TAG, "支付参数未配置"); 
 | 
                        break; 
 | 
                    case PayArgsCheckResult.CHECK_RESULT_PAY_CALLBACK_NULL: 
 | 
                        Log.i(TAG, "支付回调函数未配置"); 
 | 
                        break; 
 | 
                    case PayArgsCheckResult.CHECK_RESULT_PAY_INVALID_AMOUNT: 
 | 
                        Log.i(TAG, "金额无效"); 
 | 
                        break; 
 | 
                    case PayArgsCheckResult.CHECK_RESULT_PAY_INVALID_APP_ID: 
 | 
                        Log.i(TAG, "app_id无效,请检查AndroidManifest.xml文件"); 
 | 
                        break; 
 | 
                    case PayArgsCheckResult.CHECK_RESULT_PAY_INVALID_APP_NAME: 
 | 
                        Log.i(TAG, "应用名称无效"); 
 | 
                        break; 
 | 
                    case PayArgsCheckResult.CHECK_RESULT_PAY_INVALID_APP_USER_ID: 
 | 
                        Log.i(TAG, "用户编号(角色)为空"); 
 | 
                        break; 
 | 
                    case PayArgsCheckResult.CHECK_RESULT_PAY_INVALID_APP_USER_NAME: 
 | 
                        Log.i(TAG, "角色名为空"); 
 | 
                        break; 
 | 
                    case PayArgsCheckResult.CHECK_RESULT_PAY_INVALID_NOTIFY_URI: 
 | 
                        Log.i(TAG, "通知服务器地址为空"); 
 | 
                        break; 
 | 
                    case PayArgsCheckResult.CHECK_RESULT_PAY_INVALID_OPEN_UID: 
 | 
                        Log.i(TAG, "用户open_uid为空"); 
 | 
                        break; 
 | 
                    case PayArgsCheckResult.CHECK_RESULT_PAY_INVALID_ORDER_ID: 
 | 
                        Log.i(TAG, "订单号无效"); 
 | 
                        break; 
 | 
                    case PayArgsCheckResult.CHECK_RESULT_PAY_INVALID_PRODUCT_ID: 
 | 
                        Log.i(TAG, "产品号为空"); 
 | 
                        break; 
 | 
                    case PayArgsCheckResult.CHECK_RESULT_PAY_INVALID_PRODUCT_NAME: 
 | 
                        Log.i(TAG, "产品名称为空"); 
 | 
                        break; 
 | 
                    case PayArgsCheckResult.CHECK_RESULT_PAY_INVALID_RESOURCE_ID: 
 | 
                        Log.i(TAG, "未登录或APP_KEY无效"); 
 | 
                        break; 
 | 
                } 
 | 
            } 
 | 
        }); 
 | 
    } 
 | 
  
 | 
    public void logout() 
 | 
    { 
 | 
        XYPaySDK.logout(mainActivity, new XYLogoutCallback() 
 | 
        { 
 | 
            @Override 
 | 
  
 | 
            public void onLogout(LogoutResult logoutResult) 
 | 
            { 
 | 
                switch (logoutResult.getCode()){ 
 | 
                    case LogoutResult.LOGOUT_CODE_OUT: 
 | 
                        //Toast.makeText(MyActivity.this,"退出游戏",Toast.LENGTH_LONG).show(); 
 | 
                        android.os.Process.killProcess(android.os.Process.myPid()); 
 | 
                        break; 
 | 
                    case LogoutResult.LOGOUT_CODE_BBS: 
 | 
                       // Toast.makeText(MyActivity.this,"进入论坛",Toast.LENGTH_LONG).show(); 
 | 
                        break; 
 | 
                } 
 | 
            } 
 | 
        }); 
 | 
    } 
 | 
  
 | 
    public void switchAccount() 
 | 
    { 
 | 
        XYPaySDK.changAccount(mainActivity, new XYLoginCallback() 
 | 
        { 
 | 
            @Override 
 | 
            public void onLogin(LoginResult loginResult) 
 | 
            { 
 | 
                //登录成功 
 | 
                if (loginResult.getCode() == LoginResult.LOGIN_CODE_SUCCESS) 
 | 
                { 
 | 
                    String _uid = PreferenceUtils.getUid(mainActivity); 
 | 
                    String _token = PreferenceUtils.getToken(mainActivity);// 
 | 
                    String _appId = PreferenceUtils.getAppid(mainActivity); 
 | 
                    m_AccountID = _uid; 
 | 
                    try 
 | 
                    { 
 | 
                        JSONObject _info = new JSONObject(); 
 | 
                        _info.put("account", m_AccountID); 
 | 
                        _info.put("token", _token); 
 | 
                        _info.put("account_id", m_AccountID); 
 | 
                        _info.put("appId", _appId); 
 | 
                        m_Message.put("code", CodeA2U.FreePlatformLoginOk); 
 | 
                        m_Message.put("info", _info); 
 | 
                        UniversalUtil.sendMessageToUnity(m_Message); 
 | 
                    } catch (JSONException e) 
 | 
                    { 
 | 
                        e.printStackTrace(); 
 | 
                    } 
 | 
                    //Toast.makeText(mainActivity, "登录成功:"+loginResult.isBindPhone(), Toast.LENGTH_LONG).show(); 
 | 
                } 
 | 
                else if (loginResult.getCode() == LoginResult.LOGIN_CODE_APPID_NOT_FOUND) 
 | 
                { 
 | 
                    Log.i(TAG,"APP_ID未在AndroidManifest.xml中配置"); 
 | 
                } 
 | 
                else if (loginResult.getCode() == LoginResult.LOGIN_CODE_FAILED) 
 | 
                { 
 | 
                    Log.i(TAG,"登录失败"); 
 | 
                    switchAccount(); 
 | 
                } 
 | 
                else if (loginResult.getCode() == LoginResult.LOGIN_CODE_CANCEL) 
 | 
                { 
 | 
                    Log.i(TAG,"登录取消"); 
 | 
                } 
 | 
                //  txt.setText(loginResult.getCode() + ";" + loginResult.getMessage() + ";" + loginResult.getOpenUid()); 
 | 
            } 
 | 
        }); 
 | 
    } 
 | 
  
 | 
    public void createRole(String roleId, String sid, String roleName) 
 | 
    { 
 | 
        XYPaySDK.createRole(roleId, sid, roleName); 
 | 
    } 
 | 
  
 | 
    public void enterWorld(String roleID, 
 | 
                           String roleName, 
 | 
                           String roleLv, 
 | 
                           String serverID, 
 | 
                           String job) 
 | 
    { 
 | 
        XYPaySDK.roleLogin(roleID, serverID, roleName, roleLv, job); 
 | 
    } 
 | 
  
 | 
    public void SendRegisterEvent() 
 | 
    { 
 | 
        Tracking.setRegisterWithAccountID(m_AccountID); 
 | 
//        EventUtils.setRegister("mobile", true); 
 | 
    } 
 | 
  
 | 
    public void SendLoginEvent() 
 | 
    { 
 | 
        Tracking.setLoginSuccessBusiness(m_AccountID); 
 | 
    } 
 | 
  
 | 
    public void DownloadStart() 
 | 
    { 
 | 
        String type = "hotStart"; 
 | 
        XYPaySDK.hotActivation(mainActivity, type); 
 | 
    } 
 | 
  
 | 
    public void DownloadEnd() 
 | 
    { 
 | 
        String type = "hotEnd"; 
 | 
        XYPaySDK.hotActivation(mainActivity, type); 
 | 
    } 
 | 
  
 | 
} 
 |