#if UNITY_ANDROID
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace Touka
{
///
/// TKG Native SDK Android platform interface call
///
public class TKGNativeInterfaceAndroid : TKGNativeInterface
{
private AndroidJavaObject jo;
public TKGNativeInterfaceAndroid()
{
// java interface class
using (AndroidJavaClass jc = new AndroidJavaClass("com.touka.tkg.TKGProxyJava"))
{
jo = jc.CallStatic("getInstance");
}
}
private T SDKCall(string _method, params object[] _param)
{
try
{
return jo.Call(_method, _param);
}
catch (Exception e)
{
Debug.LogError(e);
}
return default(T);
}
private void SDKCall(string _method, params object[] _param)
{
try
{
jo.Call(_method, _param);
}
catch (Exception e)
{
Debug.LogError(e);
}
}
#region init
///
/// init
///
///
public override void Init(Action _initCallback = null)
{
TKGDebugger.LogDebug("init ----- 00");
base.Init(_initCallback);
TKGDebugger.LogDebug("init ----- 01");
SetBannerAdCallback(new AndroidBannerADTKGAdCallback());
TKGDebugger.LogDebug("init ----- 01 ------- a");
setInterstitalAdCallback(new AndroidInterADTKGAdCallback());
TKGDebugger.LogDebug("init ----- 01 ------- b");
SetRewardAdCallback(new AndroidRewardADTKGAdCallback());
TKGDebugger.LogDebug("init ----- 01 ------- c");
SetSDKCommonCallback(new AndroidTKGCommonCalllback());
TKGDebugger.LogDebug("init ----- 01 ------- d");
SetSecondPurchaseCallback();
SetAdsRevenueCallback();
SDKCall("init", new AndroidProxyCallback());
TKGDebugger.LogDebug("init ----- 02");
}
private void SetSecondPurchaseCallback()
{
#if USE_IAP
SDKCall("setSecondPurchaseCallback", new SecondPurchaseCallback());
#endif
}
private void SetAdsRevenueCallback()
{
SDKCall("setAdsRevenueCallback", new AdsRevenueCallback());
}
#endregion
///
/// Get Channel
///
///
public override string GetChannel()
{
#if UNITY_ANDROID
return SDKCall("getChannel");
#endif
return "AndroidRoot";
}
#region Ads
public override void PurchasedRemoveAds()
{
#if UNITY_ANDROID
SDKCall("purchasedRemoveAds");
#endif
}
public override void SetUnitySDKVersion(string sdkVersion)
{
#if UNITY_ANDROID
SDKCall("setUnitySDKVersion", "UnitySDK", sdkVersion);
#endif
}
#region Ads Show
///
/// Show banner Ad
///
/// bannerAlign
public override void showBannerAd(TKGBannerAlign _pos)
{
int _posIndex = 2;
if (_pos == TKGBannerAlign.BannerCenterTopAlign)
{
_posIndex = 1;
}
else if (_pos == TKGBannerAlign.BannerCenterBottomAlign)
{
_posIndex = 2;
}
#if UNITY_ANDROID
SDKCall("showBannerAd", _posIndex);
#endif
}
///
/// Hide Banner Ad
///
public override void hideBannerAd()
{
#if UNITY_ANDROID
SDKCall("hideBannerAd");
#endif
}
public override void RemoveNativeAd()
{
#if UNITY_ANDROID
SDKCall("hideNative");
#endif
}
///
/// Show Interstitial Ad
///
public override void showInterstitialAd()
{
#if UNITY_ANDROID
SDKCall("showInterstitialAd");
#endif
}
///
/// Show Interstitial Ad
///
///
public override void showInterstitialAd(string _adPos, IVADType _IvType = IVADType.IV1)
{
#if UNITY_ANDROID
SDKCall("showInterstitialAd", _adPos, (int)_IvType);
#endif
}
///
/// Show Reward Ad
///
///
///
public override void showRewardAd(string _adPos, int _itemCount = -1)
{
#if UNITY_ANDROID
SDKCall("showRewardAd", _adPos, _itemCount);
#endif
}
///
/// Show Reward Ad
///
///
///
///
public override void showRewardAd(string _adPos, int _itemCount, Dictionary _extraEvent)
{
#if UNITY_ANDROID
SDKCall("showRewardAd", _adPos, _itemCount, _extraEvent);
#endif
}
///
/// Show Video Ad
///
///
///
public override void showVideoAd(string _adPos, int _itemCount = -1)
{
#if UNITY_ANDROID
SDKCall("showVideoAd", _adPos, _itemCount);
#endif
}
public override void SetShowSDKToast(bool _useSDKToast)
{
#if UNITY_ANDROID
SDKCall("setShowSDKRewardTips", _useSDKToast);
#endif
}
public override void ShowNativeAd(RectTransform pRect, Camera pCam = null, string pAdPos = "")
{
#if UNITY_ANDROID
Vector3[] tWorldCorners = new Vector3[4];
pRect.GetWorldCorners(tWorldCorners);
Vector2 tTopLeft = RectTransformUtility.WorldToScreenPoint(pCam, tWorldCorners[1]);
Vector2 tBottomRight = RectTransformUtility.WorldToScreenPoint(pCam, tWorldCorners[3]);
float tWidth = Mathf.Abs(tBottomRight.x - tTopLeft.x);
float tHeight = Mathf.Abs(tBottomRight.y - tTopLeft.y);
// x , y , width , height
// ((tTopLeft.x, Screen.height - tTopLeft.y, tWidth, tHeight)
SDKCall("showNative", pAdPos, tTopLeft.x, Screen.height - tTopLeft.y, tWidth, tHeight);
#endif
}
#endregion
#region Ads isready
///
/// banner isReady
///
///
public override bool isBannerAdReady()
{
#if UNITY_ANDROID
bool isReady = SDKCall("isBannerAdReady");
return isReady;
#else
return false;
#endif
}
///
/// Interstitial isReady
///
///
public override bool isInterstitialAdReady()
{
#if UNITY_ANDROID
bool isReady = SDKCall("isInterstitialAdReady");
return isReady;
#else
return false;
#endif
}
///
/// Reward isReady
///
///
public override bool isRewardAdReady()
{
#if UNITY_ANDROID
bool isReady = SDKCall("isRewardAdReady");
return isReady;
#else
return false;
#endif
}
///
/// FullScreen isReady
///
///
public override bool isVideoAdReady()
{
#if UNITY_ANDROID
bool isReady = SDKCall("isVideoAdReady");
return isReady;
#else
return false;
#endif
}
public override bool IsReadyNativeAd()
{
#if UNITY_ANDROID
bool isReady = SDKCall("isNativeReady");
return isReady;
#else
return false;
#endif
}
#endregion
#region Set ads callback
///
/// Set Banner Ad Callback
///
///
public override void SetBannerAdCallback(AndroidBannerADTKGAdCallback _bannerCallback)
{
#if UNITY_ANDROID
SDKCall("setBannerAdCallback", _bannerCallback);
#endif
}
///
/// Set Interstital Ad Callback
///
///
public override void setInterstitalAdCallback(AndroidInterADTKGAdCallback _interCallback)
{
#if UNITY_ANDROID
SDKCall("setInterstitalAdCallback", _interCallback);
#endif
}
///
/// Set reward Ad Callback
///
///
public override void SetRewardAdCallback(AndroidRewardADTKGAdCallback _rewardCallback)
{
#if UNITY_ANDROID
SDKCall("setRewardAdCallback", _rewardCallback);
#endif
}
///
/// Set video Ad Callback(full screen ad, reward ad with no reward callback)
///
///
public override void setVideoAdCallback(AndroidInterADTKGAdCallback _videoCallback)
{
#if UNITY_ANDROID
SDKCall("setVideoAdCallback", _videoCallback);
#endif
}
#endregion
#region load ads
///
/// load interstitial ad
/// (retry 3 times, per time interval 10s)
///
public override void loadInterstitialAD()
{
#if UNITY_ANDROID
SDKCall("loadInterstitialAD");
#endif
}
///
/// load reward ad
/// (retry 3 times, per time interval 10s)
///
public override void loadRewardAD()
{
#if UNITY_ANDROID
SDKCall("loadRewardAD");
#endif
}
///
/// load video ad
/// (retry 3 times, per time interval 10s)
///
public override void loadVideoAD()
{
#if UNITY_ANDROID
SDKCall("loadVideoAD");
#endif
}
#endregion
#endregion
#region Log Event
#region Normal
///
/// onEvent
///
///
///
public override void onEvent(string _eventName)
{
#if UNITY_ANDROID
SDKCall("onEvent", _eventName);
#endif
}
///
/// onEvent
///
///
///
///
///
public override void onEvent(string _eventName, string _key01, string _value01)
{
#if UNITY_ANDROID
SDKCall("onEvent", _eventName, _key01, _value01);
#endif
}
///
/// onEvent
///
///
///
///
///
///
///
public override void onEvent(string _eventName, string _key01, string _value01, string _key02, string _value02)
{
#if UNITY_ANDROID
SDKCall("onEvent", _eventName, _key01, _value01, _key02, _value02);
#endif
}
///
/// onEvent
///
///
///
public override void onEvent(string _eventName, Dictionary _keyValues)
{
#if UNITY_ANDROID
SDKCall("onEvent", _eventName, Analytics.ToJavaHashMap(_keyValues));
#endif
}
#endregion
#region Level Event
///
/// Level Start
///
///
public override void LevelStart(string _levelId)
{
#if UNITY_ANDROID
SDKCall("levelStart", _levelId);
#endif
}
///
/// Level end
///
///
///
public override void LevelEnd(string _levelId, StageResult _stageResult)
{
#if UNITY_ANDROID
SDKCall("levelEnd", _levelId, (int)_stageResult);
#endif
}
#endregion
#region Tracking Event
///
/// Log Tracking Event
///
///
public override void LogTrackingEvent(TrackingEventType _eventType)
{
#if UNITY_ANDROID
SDKCall("LogTrackingEvent", (int)_eventType);
#endif
}
#endregion
#endregion
#region Online Config
///
/// get config - int
///
///
///
public override int GetConfigInt(string _key, int _default)
{
TKGDebugger.LogDebug("int _key : " + _key + " , _default : " + _default);
int newValue = SDKCall("getConfigInt", _key, _default);
return newValue;
}
///
/// get config - string
///
///
///
public override string GetConfigString(string _key, string _default)
{
TKGDebugger.LogDebug("string _key : " + _key + " , _default : " + _default);
string newValue = SDKCall("getConfigString", _key, _default);
return newValue;
}
///
/// get config - bool
///
///
///
public override bool GetConfigBool(string _key, bool _default)
{
TKGDebugger.LogDebug("bool _key : " + _key + " , _default : " + _default);
bool newValue = SDKCall("getConfigBool", _key, _default);
return newValue;
}
#endregion
#region Others (Common)
///
/// review
///
public override void Review()
{
SDKCall("review");
}
///
/// open Privacy
///
public override void OpenPrivacyURL()
{
SDKCall("openPrivacy");
}
///
/// open agreement
///
public override void OpenUserTermURL()
{
SDKCall("openAgreement");
}
///
/// open policy pop
///
public override void OpenPolicyPop()
{
SDKCall("openPrivacyDialog");
}
///
/// More Games
///
public override void OpenMoreGame()
{
SDKCall("moreGames");
}
///
/// Set Log Enable
///
///
public override void SetLogEnable(bool _enable)
{
SDKCall("setEnableLog", _enable);
}
///
/// get a iap product list for sdk config file
///
public override Dictionary GetIAPProductList()
{
Dictionary dic = new Dictionary();
return dic;
}
public override bool IsDebugMode()
{
return SDKCall("isDebugMode");
}
///
/// get user name and user id card
///
public override string GetUserNameAndUserIDCard()
{
return "0_0";
}
///
/// set segment type to sdk
///
/// segment type
public override void SetSegment(SegmentType type)
{
SDKCall("setSegment", type.ToString());
}
#endregion
#region Others (Uncommon)
///
/// shake(no repeat)
///
/// shake time
public override void shake(int _shakeType, float _intensity = 1)
{
long tShakeMS = _shakeType * _shakeType * 20 + (int)(_intensity * (_shakeType + 1) * 10);
SDKCall("shake", tShakeMS);
}
///
/// shake(can repeat)
///
/// repeat times : -1
/// wait time,shake time(100,1000,100,111)
//public override void shake(int _repeatTimes, params int[] _shakeTime)
//{
// SDKCall("shake", _repeatTimes, _shakeTime);
//}
///
/// cancel shake
///
public override void cancelShake()
{
SDKCall("cancelShake");
}
///
/// Show Tips
/// Pop-up prompt (the one with black transparent background) unified style, Chinese, Japanese, Korean and English, support multi-language, multi-language text
///
/// game judges different languages by itself and passes different strings
///
/// title
/// content
public override void dialogTips(string _titleStr, string _contentStr = "")
{
SDKCall("dialogTips", _titleStr);
}
///
/// Open ULR by Browser
///
///
public override void openUrlBrowser(string _url)
{
SDKCall("openUrlBrowser", _url);
}
///
/// Open ULR By WEB POP
///
///
public override void openWebUrl(string _url)
{
SDKCall("openWebUrl", _url);
}
///
/// show Toast
///
///
public override void toast(string _content)
{
SDKCall("dialogTips", _content);
}
///
/// push message
///
public override void pushMsg()
{
SDKCall("pushMsg");
}
//
/// share txt
///
///
public override void ShareTxt(string _shareTxt)
{
SDKCall("localShareTxt", _shareTxt);
}
public override void RegistAPNS()
{
}
///
/// show picture cross
///
public override bool showPictureCross()
{
return SDKCall("showPictureCross");
}
///
/// remove picture cross
///
public override void removePictureCross()
{
SDKCall("removePictureCross");
}
///
/// show more game icon
///
public override bool showMoreGameIcon()
{
return SDKCall("showMoreGameIcon");
}
///
/// remove more game icon
///
public override void removeMoreGameIcon()
{
SDKCall("removeMoreGameIcon");
}
public override void RegistNotification(string notiId, string body, string fireDate, int badge, string title,
string subTitle)
{
SDKCall("registNotification", notiId, body, fireDate, badge, title, subTitle);
}
public override void RemoveNotification(string notiId)
{
SDKCall("removeNotification", notiId);
}
public override void RemoveAllNotifications()
{
SDKCall("removeAllNotifications");
}
#endregion
#region 数据中台
public override void LogPurchasePrice(string price, string currency)
{
SDKCall("logPurchasePrice", price, currency);
}
public override void LogRobuxCashOut()
{
SDKCall("logRobuxCashOut");
}
public override void LogNoviceGuideFinish()
{
SDKCall("logNoviceGuideFinish");
}
#endregion
#region common callback
public override void SetSDKCommonCallback(AndroidTKGCommonCalllback _commonCallback)
{
SDKCall("setTkgCallback", _commonCallback);
}
public override void SetUserSourceCallback(AndroidTKGUserSourceCalllback _userSourceCallback)
{
SDKCall("getUserSource", _userSourceCallback);
}
public override void SetUserSourceCallback(AndroidTKGUserSourceCalllbackWithCampaignName _userSourceCallback)
{
SDKCall("getUserSource", _userSourceCallback);
}
public override void SetFunctionSwitchCalllback(List _functionKeys,
AndroidFunctionSwitchCalllback _functionSwitchCalllback)
{
string keys = "";
for (int i = 0; i < _functionKeys.Count; i++)
{
FunctionType enumType = _functionKeys[i];
string enumTypeString = enumType.ToString();
if (keys != "")
{
keys = keys + "@";
}
keys = keys + enumTypeString;
}
SDKCall("setGameSwitchListener", keys, _functionSwitchCalllback);
}
public override bool GuidGpComment()
{
return SDKCall("guidGpComment");
}
#endregion
#region 用户登录
public override void Login(LoginType loginType,
Action _loginResultCallback = null)
{
TKGSDKCallback.mLoginCallback = _loginResultCallback;
SDKCall("login", (int)loginType, new AndroidLoginCalllback());
}
public override void Logout(Action _logoutCallback = null)
{
TKGSDKCallback.mLogoutCallback = _logoutCallback;
SDKCall("logout", new AndroidLoginCalllback());
}
public override List AvailableLoginChannelList()
{
var availableLoginChannelList = SDKCall("availableLoginChannelList");
return availableLoginChannelList.Select(channel => (LoginType)Enum.ToObject(typeof(LoginType), channel))
.ToList();
}
public override void DeleteAccount(Action deleteAccountCallback = null)
{
TKGSDKCallback.mDeleteAccountCallback = deleteAccountCallback;
SDKCall("deleteAccount", new AndroidLoginCalllback());
}
public override void BindAccount(LoginType type, BindType isForce, string userId,
Action
bindAccountCallback)
{
TKGSDKCallback.mBindAccountCallback = bindAccountCallback;
SDKCall("bindAccount", (int)type, (int)isForce, userId, new AndroidLoginCalllback());
}
public override void AutoLogin(
Action loginResultCallback)
{
TKGSDKCallback.mAutoLoginCallback = loginResultCallback;
SDKCall("autoLogin", new AndroidLoginCalllback(isAutoLogin: true));
}
public override LoginType GetAccountType()
{
var userType = SDKCall("getUserType");
return (LoginType)Enum.ToObject(typeof(LoginType), userType);
}
public override bool IsCanAutoLogin()
{
return SDKCall("isCanAutoLogin");
}
#endregion
#region IAP Event
///
/// Log IAP button show
///
public override void LogIAPBtnShow(string _productName, string _productID)
{
Dictionary _keyValues = new Dictionary();
_keyValues.Add("IAP", _productName);
_keyValues.Add("ID", _productID);
//_keyValues.Add("Currency", _currency);
//_keyValues.Add("Price", _price);
onEvent("IAP_Button_Show", _keyValues);
}
///
/// Log IAP button Click
///
public override void LogIAPBtnClick(string _productName, string _productID)
{
Dictionary _keyValues = new Dictionary();
_keyValues.Add("IAP", _productName);
_keyValues.Add("ID", _productID);
//_keyValues.Add("Currency", _currency);
//_keyValues.Add("Price", _price);
onEvent("IAP_Button_Click", _keyValues);
}
///
/// Log IAP First payment successful
///
public override void LogIAPFirstPurchase(string _iap, string _id, string _paymentChannelOrderid,
string _currency, string _price)
{
// 放到native层实现了
//Dictionary _keyValues = new Dictionary();
//_keyValues.Add("IAP", _iap);
//_keyValues.Add("ID", _id);
//_keyValues.Add("Currency", _currency);
//_keyValues.Add("Price", _price);
//_keyValues.Add("order_id", _paymentChannelOrderid);
//onEvent("First_purchase", _keyValues);
}
///
/// Log IAP payment successful
///
public override void LogIAPSuccess(string _iap, string _id, string _gameOrderid, string _paymentChannelOrderid,
string _productToken, string _paymentMethod, string _currency, string _price)
{
// 放到native层实现了
//Dictionary _keyValues = new Dictionary();
//_keyValues.Add("IAP", _iap);
//_keyValues.Add("ID", _id);
//_keyValues.Add("Currency", _currency);
//_keyValues.Add("Price", _price);
//_keyValues.Add("order_id", _paymentChannelOrderid);
//onEvent("IAP_Success", _keyValues);
//SDKCall("logIAPSuccess", _price, _id, _gameOrderid, _paymentChannelOrderid, _productToken, _currency, _paymentMethod, _iap);
}
public override void SaveCloudArchiving(string content, Action callback)
{
void Action(CommonCallbackCode code, string s)
{
if (code == CommonCallbackCode.SaveCloudArchiving_Succ ||
code == CommonCallbackCode.SaveCloudArchiving_Fail)
{
callback.Invoke(code == CommonCallbackCode.SaveCloudArchiving_Succ ? 0 : -1, s);
TKGSDKCallback.RemoveTKGCommonCallback(Action);
}
}
TKGSDKCallback.SetTKGCommonCallback(Action);
SDKCall("saveCloudArchiving", content);
}
public override void DetailCloudArchiving(Action callback)
{
void Action(CommonCallbackCode code, string s)
{
if (code == CommonCallbackCode.DetailCloudArchiving_Succ ||
code == CommonCallbackCode.DetailCloudArchiving_Fail)
{
callback.Invoke(code == CommonCallbackCode.DetailCloudArchiving_Succ ? 0 : -1, s);
TKGSDKCallback.RemoveTKGCommonCallback(Action);
}
}
TKGSDKCallback.SetTKGCommonCallback(Action);
SDKCall("detailCloudArchiving");
}
#endregion
#region serve for wz
///
/// Set userid and third account
///
/// userID (Unique user id)
/// third account
/// third account type
public override void SetUserIDAndThirdAccount(string _userID, string _thirdAccount,
ThirdAccountType _thirdAccountType)
{
Debug.Log("【TKGNativeInterfaceAndroid】 SetUserIDAndThirdAccount _userID : " + _userID +
" , _thirdAccount : " + _thirdAccount + " , _thirdAccountType : " + _thirdAccountType);
SDKCall("setUserIDAndThirdAccount", _userID, _thirdAccount, _thirdAccountType.ToString());
}
#endregion
#region client iap pay result
///
/// Client IAP Success
///
public override void ClientIAPSuccess(string _productName, string _productID, string _purchaseOrderID,
string _purchaseToken, string _currency, string _price, string _gameExtra)
{
SDKCall("ClientIAPSuccess", _productName, _productID, _purchaseOrderID, _purchaseToken, _currency, _price,
_gameExtra);
}
///
/// Client IAP Failed
///
public override void ClientIAPFailed(string _productName, string _productID, string _purchaseOrderID,
string _purchaseToken, string _currency, string _price, string _gameExtra,
IAPClientFailReasonType _failType)
{
SDKCall("ClientIAPFailed", _productName, _productID, _purchaseOrderID, _purchaseToken, _currency, _price,
_gameExtra, _failType.ToString());
}
#endregion
}
}
#endif