River Jiang
2020-10-21 c1d12cdfd23933a0db431a70ff5e145924864782
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
using UnityEngine;
using System.Collections.Generic;
#if UNITY_ANDROID
using System;
#endif
#if UNITY_IPHONE
using System.Runtime.InteropServices;
using System.Collections;
#endif
 
 
public static class TalkingDataGA
{
#if UNITY_ANDROID
    private static readonly string GAME_ANALYTICS_CLASS = "com.tendcloud.tenddata.TalkingDataGA";
    private static AndroidJavaClass gameAnalyticsClass;
    private static AndroidJavaClass unityPlayerClass;
#endif
 
#if UNITY_IPHONE
    [DllImport("__Internal")]
    private static extern string TDGAGetDeviceId();
 
    [DllImport("__Internal")]
    private static extern void TDGASetVerboseLogDisabled();
 
    [DllImport("__Internal")]
    private static extern void TDGABackgroundSessionEnabled();
 
    [DllImport("__Internal")]
    private static extern void TDGAOnStart(string appId, string channelId);
 
    [DllImport("__Internal")]
    private static extern void TDGASetLocation(double latitude, double longitude);
 
#if TDGA_CUSTOM
    [DllImport("__Internal")]
    private static extern void TDGAOnEvent(string eventId, string parameters);
#endif
 
#if TDGA_PUSH
    [DllImport("__Internal")]
    private static extern void TDGASetDeviceToken(byte[] deviceToken, int length);
 
    [DllImport("__Internal")]
    private static extern void TDGAHandlePushMessage(string message);
 
    private static bool hasTokenBeenObtained = false;
#endif
#endif
 
#if UNITY_ANDROID
    private static AndroidJavaObject GetCurrentActivity()
    {
        if (unityPlayerClass == null)
        {
            unityPlayerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        }
        AndroidJavaObject activity = unityPlayerClass.GetStatic<AndroidJavaObject>("currentActivity");
        return activity;
    }
#endif
 
    private static string deviceId = null;
    public static string GetDeviceId()
    {
        if (deviceId == null && Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
        {
#if UNITY_ANDROID
            if (gameAnalyticsClass == null)
            {
                gameAnalyticsClass = new AndroidJavaClass(GAME_ANALYTICS_CLASS);
            }
            deviceId = gameAnalyticsClass.CallStatic<string>("getDeviceId", GetCurrentActivity());
#endif
#if UNITY_IPHONE
            deviceId = TDGAGetDeviceId();
#endif
        }
        return deviceId;
    }
 
    private static string oaid = null;
    public static string GetOAID()
    {
        if (oaid == null && Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
        {
#if UNITY_ANDROID
            if (gameAnalyticsClass == null)
            {
                gameAnalyticsClass = new AndroidJavaClass(GAME_ANALYTICS_CLASS);
            }
            oaid = gameAnalyticsClass.CallStatic<string>("getOAID", GetCurrentActivity());
#endif
        }
        return oaid;
    }
 
    public static void SetVerboseLogDisabled()
    {
        if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
        {
#if UNITY_ANDROID
            if (gameAnalyticsClass == null)
            {
                gameAnalyticsClass = new AndroidJavaClass(GAME_ANALYTICS_CLASS);
            }
            gameAnalyticsClass.CallStatic("setVerboseLogDisabled");
#endif
#if UNITY_IPHONE
            TDGASetVerboseLogDisabled();
#endif
        }
    }
 
    public static void BackgroundSessionEnabled()
    {
        if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
        {
#if UNITY_IPHONE
            TDGABackgroundSessionEnabled();
#endif
        }
    }
 
    public static void OnStart(string appId, string channelId)
    {
        if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
        {
            Debug.Log("TalkingData Game Analytics Unity SDK.");
#if UNITY_ANDROID
            using (AndroidJavaClass dz = new AndroidJavaClass("com.tendcloud.tenddata.game.dz"))
            {
                dz.SetStatic("a", 2);
            }
            if (gameAnalyticsClass == null)
            {
                gameAnalyticsClass = new AndroidJavaClass(GAME_ANALYTICS_CLASS);
            }
            AndroidJavaObject activity = GetCurrentActivity();
            gameAnalyticsClass.CallStatic("init", activity, appId, channelId);
            gameAnalyticsClass.CallStatic("onResume", activity);
#endif
#if UNITY_IPHONE
            TDGAOnStart(appId, channelId);
#endif
        }
    }
 
    public static void OnEnd()
    {
        if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
        {
#if UNITY_ANDROID
            if (gameAnalyticsClass != null)
            {
                gameAnalyticsClass.CallStatic("onPause", GetCurrentActivity());
                gameAnalyticsClass = null;
                unityPlayerClass = null;
            }
#endif
        }
    }
 
    public static void OnKill()
    {
        if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
        {
#if UNITY_ANDROID
            if (gameAnalyticsClass != null)
            {
                gameAnalyticsClass.CallStatic("onKill", GetCurrentActivity());
                gameAnalyticsClass = null;
                unityPlayerClass = null;
            }
#endif
        }
    }
 
    public static void SetLocation(double latitude, double longitude)
    {
        if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
        {
#if UNITY_IPHONE
            TDGASetLocation(latitude, longitude);
#endif
        }
    }
 
#if TDGA_CUSTOM
    public static void OnEvent(string actionId, Dictionary<string, object> parameters)
    {
        if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
        {
#if UNITY_ANDROID
            if (gameAnalyticsClass != null)
            {
                if (parameters != null && parameters.Count > 0)
                {
                    int count = parameters.Count;
                    AndroidJavaObject map = new AndroidJavaObject("java.util.HashMap", count);
                    IntPtr method_Put = AndroidJNIHelper.GetMethodID(map.GetRawClass(), "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
                    object[] args = new object[2];
                    foreach (KeyValuePair<string, object> kvp in parameters)
                    {
                        args[0] = new AndroidJavaObject("java.lang.String", kvp.Key);
                        args[1] = typeof(string).IsInstanceOfType(kvp.Value)
                            ? new AndroidJavaObject("java.lang.String", kvp.Value)
                            : new AndroidJavaObject("java.lang.Double", "" + kvp.Value);
                        AndroidJNI.CallObjectMethod(map.GetRawObject(), method_Put, AndroidJNIHelper.CreateJNIArgArray(args));
                    }
                    gameAnalyticsClass.CallStatic("onEvent", actionId, map);
                    map.Dispose();
                }
                else
                {
                    gameAnalyticsClass.CallStatic("onEvent", actionId, null);
                }
            }
#endif
#if UNITY_IPHONE
            if (parameters != null && parameters.Count > 0)
            {
                string parameterStr = "{";
                foreach (KeyValuePair<string, object> kvp in parameters)
                {
                    if (kvp.Value is string)
                    {
                        parameterStr += "\"" + kvp.Key + "\":\"" + kvp.Value + "\",";
                    }
                    else
                    {
                        try
                        {
                            double tmp = System.Convert.ToDouble(kvp.Value);
                            parameterStr += "\"" + kvp.Key + "\":" + tmp + ",";
                        }
                        catch (System.Exception)
                        {
                        }
                    }
                }
                parameterStr = parameterStr.TrimEnd(',');
                parameterStr += "}";
                TDGAOnEvent(actionId, parameterStr);
            }
            else
            {
                TDGAOnEvent(actionId, null);
            }
#endif
        }
    }
#endif
 
#if TDGA_PUSH
    public static void SetDeviceToken()
    {
#if UNITY_IPHONE
        if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
        {
            if (!hasTokenBeenObtained)
            {
                byte[] deviceToken = UnityEngine.iOS.NotificationServices.deviceToken;
                if (deviceToken != null)
                {
                    TDGASetDeviceToken(deviceToken, deviceToken.Length);
                    hasTokenBeenObtained = true;
                }
            }
        }
#endif
    }
 
    public static void HandlePushMessage()
    {
#if UNITY_IPHONE
        if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
        {
            UnityEngine.iOS.RemoteNotification[] notifications = UnityEngine.iOS.NotificationServices.remoteNotifications;
            if (notifications != null)
            {
                UnityEngine.iOS.NotificationServices.ClearRemoteNotifications();
                foreach (UnityEngine.iOS.RemoteNotification rn in notifications)
                {
                    foreach (DictionaryEntry de in rn.userInfo)
                    {
                        if (de.Key.ToString().Equals("sign"))
                        {
                            string sign = de.Value.ToString();
                            TDGAHandlePushMessage(sign);
                        }
                    }
                }
            }
        }
#endif
    }
#endif
}