-
Notifications
You must be signed in to change notification settings - Fork 33
/
FlurryAndroid.cs
556 lines (514 loc) · 17.2 KB
/
FlurryAndroid.cs
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Mateusz Majchrzak
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
namespace Analytics
{
/// <summary>
/// Flurry Android SDK 5.4.0 implementation
/// </summary>
public static class FlurryAndroid
{
#region [Internal]
#if UNITY_ANDROID && !UNITY_EDITOR
private static readonly string s_FlurryAgentClassName = "com.flurry.android.FlurryAgent";
private static readonly string s_UnityPlayerClassName = "com.unity3d.player.UnityPlayer";
private static readonly string s_UnityPlayerActivityName = "currentActivity";
#endif
#if UNITY_ANDROID && !UNITY_EDITOR
/// <summary>
/// FlurryAgent instance
/// </summary>
private static AndroidJavaClass s_FlurryAgent;
#endif
#if UNITY_ANDROID && !UNITY_EDITOR
/// <summary>
/// TODO:
/// </summary>
private static AndroidJavaClass FlurryAgent
{
get
{
if (Application.platform != RuntimePlatform.Android)
return null;
if (s_FlurryAgent == null)
s_FlurryAgent = new AndroidJavaClass(s_FlurryAgentClassName);
return s_FlurryAgent;
}
}
#endif
/// <summary>
/// Dispose Android Agent class
/// </summary>
public static void Dispose()
{
#if UNITY_ANDROID && !UNITY_EDITOR
if (s_FlurryAgent != null)
s_FlurryAgent.Dispose();
s_FlurryAgent = null;
#endif
}
#endregion
#region [Session Calls]
/// <summary>
/// Initialize the Flurry SDK.
/// </summary>
/// <param name="apiKey">
/// The API key for your application
/// </param>
public static void Init(string apiKey)
{
#if UNITY_ANDROID && !UNITY_EDITOR
using (AndroidJavaClass unityPlayer = new AndroidJavaClass(s_UnityPlayerClassName))
{
using (AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>(s_UnityPlayerActivityName))
{
FlurryAgent.CallStatic("init", activity, apiKey);
}
}
#endif
}
/// <summary>
/// Start or continue a Flurry session for the project.
/// </summary>
public static void OnStartSession()
{
#if UNITY_ANDROID && !UNITY_EDITOR
using (AndroidJavaClass unityPlayer = new AndroidJavaClass(s_UnityPlayerClassName))
{
using (AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>(s_UnityPlayerActivityName))
{
FlurryAgent.CallStatic("onStartSession", activity);
}
}
#endif
}
/// <summary>
/// End a Flurry session.
/// </summary>
public static void OnEndSession()
{
#if UNITY_ANDROID && !UNITY_EDITOR
using (AndroidJavaClass unityPlayer = new AndroidJavaClass(s_UnityPlayerClassName))
{
using (AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>(s_UnityPlayerActivityName))
{
FlurryAgent.CallStatic("onEndSession", activity);
}
}
#endif
}
/// <summary>
/// Check to see if there is an active session.
/// </summary>
/// <returns>true if a session is currently active, false if not.</returns>
public static bool IsSessionActive()
{
#if UNITY_ANDROID && !UNITY_EDITOR
return FlurryAgent.CallStatic<bool>("isSessionActive");
#else
return false;
#endif
}
/// <summary>
/// Check to see if there is an active session.
/// </summary>
/// <returns>The current session id, or null if no session is currently active.</returns>
public static string GetSessionId()
{
#if UNITY_ANDROID && !UNITY_EDITOR
return FlurryAgent.CallStatic<string>("getSessionId");
#else
return string.Empty;
#endif
}
#endregion
#region [SDK Version information]
/// <summary>
/// Get the version of the Flurry SDK.
/// </summary>
/// <returns>
/// The version of the Flurry SDK.
/// </returns>
public static int GetAgentVersion()
{
#if UNITY_ANDROID && !UNITY_EDITOR
return FlurryAgent.CallStatic<int>("getAgentVersion");
#else
return -1;
#endif
}
/// <summary>
/// Get the release version of the Flurry SDK.
/// </summary>
/// <returns>
/// The release version GA/Beta of the Flurry SDK with prefix FLurry_Android.
/// </returns>
public static string GetReleaseVersion()
{
#if UNITY_ANDROID && !UNITY_EDITOR
return FlurryAgent.CallStatic<string>("getReleaseVersion");
#else
return string.Empty;
#endif
}
#endregion
#region [Pre-Init Calls - Optional SDK settings that should be called before init]
/// <summary>
/// true to enable or false to disable the internal logging for the Flurry SDK.
/// </summary>
/// <param name="isEnabled">
/// true to enable logging, false to disable it.
/// </param>
public static void SetLogEnabled(bool isEnabled)
{
#if UNITY_ANDROID && !UNITY_EDITOR
FlurryAgent.CallStatic("setLogEnabled", isEnabled);
#endif
}
/// <summary>
/// Set the log level of the internal Flurry SDK logging.
/// </summary>
/// <param name="logLevel">The level to set it to.</param>
public static void SetLogLevel(LogLevel logLevel)
{
#if UNITY_ANDROID && !UNITY_EDITOR
FlurryAgent.CallStatic("setLogLevel", (int)logLevel);
#endif
}
/// <summary>
/// Set the version name of the app. Set the name of the this version of the app.
/// This name will appear in the http://dev.flurry.com as a filtering option by
/// version for many charts.
/// </summary>
/// <param name="versionName">
/// The version of the app.
/// </param>
public static void SetVersionName(string versionName)
{
#if UNITY_ANDROID && !UNITY_EDITOR
FlurryAgent.CallStatic("setVersionName", versionName);
#endif
}
/// <summary>
/// Set whether Flurry should record location via GPS. Defaults to true.
/// </summary>
/// <param name="reportLocation">
/// True to allow Flurry to record location via GPS, false otherwise
/// </param>
public static void SetReportLocation(bool reportLocation)
{
#if UNITY_ANDROID && !UNITY_EDITOR
FlurryAgent.CallStatic("setReportLocation", reportLocation);
#endif
}
/// <summary>
/// Set the default location. Useful when it is necessary to test analytics
/// from other countries or places, or if your app is location-aware and you
/// do not wish the Flurry SDK to determine the user location via GPS.
/// </summary>
/// <param name="lat">The latitude</param>
/// <param name="lon">The longitude</param>
public static void SetLocation(float lat, float lon)
{
#if UNITY_ANDROID && !UNITY_EDITOR
FlurryAgent.CallStatic("setLocation", lat, lon);
#endif
}
/// <summary>
/// Clear the default location.
/// </summary>
public static void ClearLocation()
{
#if UNITY_ANDROID && !UNITY_EDITOR
FlurryAgent.CallStatic("clearLocation");
#endif
}
/// <summary>
/// Set the timeout for expiring a Flurry session.
/// </summary>
/// <param name="millis">
/// The time in milliseconds to set the session timeout to. Minimum value of 5000.
/// </param>
public static void SetContinueSessionMillis(long millis)
{
#if UNITY_ANDROID && !UNITY_EDITOR
FlurryAgent.CallStatic("setContinueSessionMillis", millis);
#endif
}
/// <summary>
/// true to enable event logging or false to disable event logging.
/// </summary>
/// <param name="logEvents">
/// true to log events, false to disable event logging.
/// </param>
public static void SetLogEvents(bool logEvents)
{
#if UNITY_ANDROID && !UNITY_EDITOR
FlurryAgent.CallStatic("setLogEvents", logEvents);
#endif
}
/// <summary>
/// true to enable or false to disable the ability to catch all uncaught
/// exceptions and have them reported back to Flurry.
/// </summary>
/// <param name="isEnabled">true to enable, false to disable.</param>
public static void SetCaptureUncaughtExceptions(bool isEnabled)
{
#if UNITY_ANDROID && !UNITY_EDITOR
FlurryAgent.CallStatic("setCaptureUncaughtExceptions", isEnabled);
#endif
}
/// <summary>
/// Add origin attribution.
/// </summary>
/// <param name="originName">
/// The name/id of the origin you wish to attribute.
/// </param>
/// <param name="originVersion">
/// The version of the origin you wish to attribute.
/// </param>
public static void AddOrigin(string originName, string originVersion)
{
#if UNITY_ANDROID && !UNITY_EDITOR
FlurryAgent.CallStatic("addOrigin", originName, originVersion);
#endif
}
/// <summary>
/// Add origin attribution with parameters.
/// </summary>
/// <param name="originName">
/// The name/id of the origin you wish to attribute.
/// </param>
/// <param name="originVersion">
/// The version of the origin you wish to attribute.
/// </param>
/// <param name="originParameters">
/// Add origin attribution with parameters.
/// </param>
public static void AddOrigin(string originName, string originVersion, Dictionary<string, string> originParameters)
{
#if UNITY_ANDROID && !UNITY_EDITOR
using (var hashMap = DictionaryToJavaHashMap(originParameters))
{
FlurryAgent.CallStatic("addOrigin", originName, originVersion, hashMap);
}
#endif
}
/// <summary>
/// Disable or enable Flurry Pulse.
/// </summary>
/// <param name="isEnabled">true to enable, false to disable.</param>
public static void SetPulseEnabled(bool isEnabled)
{
#if UNITY_ANDROID && !UNITY_EDITOR
FlurryAgent.CallStatic("setPulseEnabled", isEnabled);
#endif
}
#endregion
#region [Event and Error Logging Methods for reporting custom events and errors during the session]
/// <summary>
/// Log an event.
/// </summary>
/// <param name="eventId">The name/id of the event.</param>
public static EventRecordStatus LogEvent(string eventId)
{
#if UNITY_ANDROID && !UNITY_EDITOR
return JavaObjectToEventRecordStatus(FlurryAgent.CallStatic<AndroidJavaObject>("logEvent", eventId));
#else
return EventRecordStatus.Failed;
#endif
}
/// <summary>
/// Log an event with parameters.
/// </summary>
/// <param name="eventId">The name/id of the event.</param>
/// <param name="parameters">
/// A Dictionary of the parameters which should be submitted with this event.
/// </param>
public static EventRecordStatus LogEvent(string eventId, Dictionary<string, string> parameters)
{
#if UNITY_ANDROID && !UNITY_EDITOR
using (var hashMap = DictionaryToJavaHashMap(parameters))
{
return JavaObjectToEventRecordStatus(FlurryAgent.CallStatic<AndroidJavaObject>("logEvent", eventId, hashMap, false));
}
#else
return EventRecordStatus.Failed;
#endif
}
/// <summary>
/// Log a timed event.
/// </summary>
/// <param name="eventId">The name/id of the event.</param>
/// <param name="timed">True if the event should be timed, false otherwise.</param>
public static EventRecordStatus LogEvent(string eventId, bool timed)
{
#if UNITY_ANDROID && !UNITY_EDITOR
return JavaObjectToEventRecordStatus(FlurryAgent.CallStatic<AndroidJavaObject>("logEvent", eventId, timed));
#else
return EventRecordStatus.Failed;
#endif
}
/// <summary>
/// Log a timed event with parameters. Log a timed event with parameters with the
/// Flurry service. To end the timer, call endTimedEvent(String) with this eventId.
/// </summary>
/// <param name="eventId">The name/id of the event.</param>
/// <param name="parameters">
/// A Dictionary of parameters to log with this event.
/// </param>
/// <param name="timed">True if this event is timed, false otherwise.</param>
public static EventRecordStatus LogEvent(string eventId, Dictionary<string, string> parameters, bool timed)
{
#if UNITY_ANDROID && !UNITY_EDITOR
using (var hashMap = DictionaryToJavaHashMap(parameters))
{
return JavaObjectToEventRecordStatus(FlurryAgent.CallStatic<AndroidJavaObject>("logEvent", eventId, hashMap, timed));
}
#else
return EventRecordStatus.Failed;
#endif
}
/// <summary>
/// End a timed event.
/// </summary>
/// <param name="eventId">The name/id of the event to end the timer on.</param>
public static void EndTimedEvent(string eventId)
{
#if UNITY_ANDROID && !UNITY_EDITOR
FlurryAgent.CallStatic("endTimedEvent", eventId);
#endif
}
/// <summary>
/// End a timed event.
/// </summary>
/// <param name="eventId">The name/id of the event to end the timer on.</param>
/// <param name="parameters">
/// A Dictionary of parameters to log with this event.
/// </param>
public static void EndTimedEvent(string eventId, Dictionary<string, string> parameters)
{
#if UNITY_ANDROID && !UNITY_EDITOR
using (var hashMap = DictionaryToJavaHashMap(parameters))
{
FlurryAgent.CallStatic("endTimedEvent", eventId, hashMap);
}
#endif
}
/// <summary>
/// Log an error with the Flurry service.
/// </summary>
/// <param name="errorId">The name/ID of the error</param>
/// <param name="message">The message of the error</param>
/// <param name="errorClass">The class of the error</param>
public static void OnError(string errorId, string message, string errorClass)
{
#if UNITY_ANDROID && !UNITY_EDITOR
FlurryAgent.CallStatic("onError", errorId, message, errorClass);
#endif
}
#endregion
#region [Page View Methods Count page views]
/// <summary>
/// Log a page view.
/// </summary>
public static void OnPageView()
{
#if UNITY_ANDROID && !UNITY_EDITOR
FlurryAgent.CallStatic("onPageView");
#endif
}
#endregion
#region [User Info Methods to set user information]
/// <summary>
/// Sets the age of the user at the time of this session.
/// </summary>
/// <param name="age">valid values are 0-110</param>
public static void SetAge(int age)
{
#if UNITY_ANDROID && !UNITY_EDITOR
FlurryAgent.CallStatic("setAge", age);
#endif
}
/// <summary>
/// Sets the gender of the user.
/// </summary>
/// <param name="gender"></param>
public static void SetGender(byte gender)
{
#if UNITY_ANDROID && !UNITY_EDITOR
FlurryAgent.CallStatic("setGender", gender);
#endif
}
/// <summary>
/// Sets the Flurry userId for this session. The Flurry userId can be used to
/// tie a user back to your internal systems. This userId will travel along with
/// any event reporting that the Flurry Dev Portal provides.
/// </summary>
/// <param name="userId">User id</param>
public static void SetUserId(string userId)
{
#if UNITY_ANDROID && !UNITY_EDITOR
FlurryAgent.CallStatic("setUserId", userId);
#endif
}
#endregion
#region [Helpers]
#if UNITY_ANDROID && !UNITY_EDITOR
/// <summary>
/// Converts Dictionary<string, string> to java HashMap object
/// </summary>
private static AndroidJavaObject DictionaryToJavaHashMap(Dictionary<string, string> dictionary)
{
var javaObject = new AndroidJavaObject("java.util.HashMap");
var put = AndroidJNIHelper.GetMethodID(javaObject.GetRawClass(), "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
foreach (KeyValuePair<string, string> entry in dictionary)
{
using (var key = new AndroidJavaObject("java.lang.String", entry.Key))
{
using (var value = new AndroidJavaObject("java.lang.String", entry.Value))
{
AndroidJNI.CallObjectMethod(javaObject.GetRawObject(), put, AndroidJNIHelper.CreateJNIArgArray(new object[] { key, value }));
}
}
}
return javaObject;
}
/// <summary>
/// Converts java EventRecordStatus to EventRecordStatus
/// </summary>
/// <param name="javaObject">java object</param>
/// <returns></returns>
private static EventRecordStatus JavaObjectToEventRecordStatus(AndroidJavaObject javaObject)
{
return (EventRecordStatus) javaObject.Call<int>("ordinal");
}
#endif
#endregion
}
}