Advertisement
tomasslavicek

JNI binding Flurry .jar on Mono for Android

Jan 18th, 2013
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.80 KB | None | 0 0
  1. using System;
  2. //using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. using Android.App;
  7. using Android.Content;
  8. using Android.OS;
  9. using Android.Runtime;
  10. using Android.Views;
  11. using Android.Widget;
  12. using System.Threading;
  13. using Android.Util;
  14. using System.Collections;
  15. using System.Collections.Generic;
  16. //using Tasty;
  17. //using Java.Util;
  18.  
  19. namespace FlurryWrapper
  20. {
  21.     public class FlurryClient
  22.     {
  23.         public const string ApiKeyValue = "XXXXXXXXXXXXXXXXX";
  24.  
  25.         private readonly IntPtr _flurryClass;
  26.  
  27.         // SESSIONS
  28.         private readonly IntPtr _flurryOnStartSession;
  29.         private readonly IntPtr _flurryOnEndSession;
  30.         private readonly IntPtr _flurrySetContinueSessionMillis;
  31.  
  32.         // SIMPLE EVENTS
  33.         private readonly IntPtr _flurryLogEvent;
  34.         private readonly IntPtr _flurryLogEventMap;
  35.  
  36.         // TIMED EVENTS
  37.         private readonly IntPtr _flurryLogTimedEvent;
  38.         private readonly IntPtr _flurryLogTimedEventMap;
  39.         private readonly IntPtr _flurryEndTimedEvent;
  40.  
  41.         // ADDITIONAL USER INFORMATION
  42.         private readonly IntPtr _flurrySetUserId;
  43.         private readonly IntPtr _flurrySetAge;
  44.         private readonly IntPtr _flurrySetGender;
  45.  
  46.         // LOCATION
  47.         private readonly IntPtr _flurrySetReportLocation;
  48.  
  49.         // ERROR / LOG
  50.         private readonly IntPtr _flurrySetLogEnabled;
  51.         private readonly IntPtr _flurryOnError;
  52.  
  53.         // CONTINUE SESSION MILLIS
  54.  
  55.  
  56.         public FlurryClient()
  57.         {
  58.             _flurryClass = JNIEnv.FindClass("com/flurry/android/FlurryAgent");
  59.  
  60.             // SESSIONS
  61.             _flurryOnStartSession = JNIEnv.GetStaticMethodID(_flurryClass, "onStartSession", "(Landroid/content/Context;Ljava/lang/String;)V");
  62.             _flurryOnEndSession = JNIEnv.GetStaticMethodID(_flurryClass, "onEndSession", "(Landroid/content/Context;)V");
  63.             _flurrySetContinueSessionMillis = JNIEnv.GetStaticMethodID(_flurryClass, "setContinueSessionMillis", "(J)V");
  64.  
  65.             // SIMPLE EVENTS
  66.             _flurryLogEvent = JNIEnv.GetStaticMethodID(_flurryClass, "logEvent", "(Ljava/lang/String;)V");
  67.             _flurryLogEventMap = JNIEnv.GetStaticMethodID(_flurryClass, "logEvent", "(Ljava/lang/String;Ljava/util/Map;)V");
  68.  
  69.             // TIMED EVENTS
  70.             _flurryLogTimedEvent = JNIEnv.GetStaticMethodID(_flurryClass, "logEvent", "(Ljava/lang/String;Z)V");
  71.             _flurryLogTimedEventMap = JNIEnv.GetStaticMethodID(_flurryClass, "logEvent", "(Ljava/lang/String;Ljava/util/Map;Z)V");
  72.             _flurryEndTimedEvent = JNIEnv.GetStaticMethodID(_flurryClass, "endTimedEvent", "(Ljava/lang/String;)V");
  73.  
  74.             // ADDITIONAL USER INFORMATION
  75.             _flurrySetUserId = JNIEnv.GetStaticMethodID(_flurryClass, "setUserId", "(Ljava/lang/String;)V");
  76.             _flurrySetAge = JNIEnv.GetStaticMethodID(_flurryClass, "setAge", "(I)V");
  77.             _flurrySetGender = JNIEnv.GetStaticMethodID(_flurryClass, "setGender", "(B)V");
  78.  
  79.             // LOCATION
  80.             _flurrySetReportLocation = JNIEnv.GetStaticMethodID(_flurryClass, "setReportLocation", "(Z)V");
  81.  
  82.             // ERRROR / LOG
  83.             _flurrySetLogEnabled = JNIEnv.GetStaticMethodID(_flurryClass, "setLogEnabled", "(Z)V");
  84.             _flurryOnError = JNIEnv.GetStaticMethodID(_flurryClass, "onError", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
  85.         }
  86.  
  87.         #region SESSIONS
  88.         //public void StartSession()
  89.         //{
  90.             // not used in Android - Android relies on Activity tracking instead
  91.         //}
  92.  
  93.         public void OnStartActivity(Activity activity)
  94.         {
  95.             ExceptionSafe(() => JNIEnv.CallStaticVoidMethod(_flurryClass, _flurryOnStartSession, new JValue(activity), new JValue(new Java.Lang.String(ApiKeyValue))));
  96.         }
  97.  
  98.         public void OnStopActivity(Activity activity)
  99.         {
  100.             ExceptionSafe(() => JNIEnv.CallStaticVoidMethod(_flurryClass, _flurryOnEndSession, new JValue(activity)));
  101.         }
  102.  
  103.         public void setContinueSessionMillis(long millis)
  104.         {
  105.             ExceptionSafe(() => JNIEnv.CallStaticVoidMethod(_flurryClass, _flurrySetContinueSessionMillis, new JValue(millis)));
  106.         }
  107.         #endregion
  108.  
  109.         #region SIMPLE EVENTS
  110.         public void LogEvent(string eventName)
  111.         {
  112.             ExceptionSafe(() => JNIEnv.CallStaticVoidMethod(_flurryClass, _flurryLogEvent, new JValue(new Java.Lang.String(eventName))));
  113.         }
  114.        
  115.         public void LogEvent(string eventName, Dictionary<string, string> parameters)
  116.         {
  117.             JavaDictionary<string, string> actualParams = new JavaDictionary<string, string>(parameters);
  118.             ExceptionSafe(() => JNIEnv.CallStaticVoidMethod(_flurryClass, _flurryLogEventMap, new JValue(new Java.Lang.String(eventName)), new JValue(actualParams)));
  119.  
  120.         }
  121.         #endregion
  122.  
  123.         #region TIMED EVENTS
  124.         public void LogTimedEvent(string eventName, bool timed)
  125.         {
  126.             ExceptionSafe(() => JNIEnv.CallStaticVoidMethod(_flurryClass, _flurryLogTimedEvent, new JValue(new Java.Lang.String(eventName)), new JValue(timed)));
  127.         }
  128.        
  129.         public void LogTimedEvent(string eventName, Dictionary<string, string> parameters, bool timed)
  130.         {
  131.             JavaDictionary<string, string> actualParams = new JavaDictionary<string, string>(parameters);
  132.             ExceptionSafe(() => JNIEnv.CallStaticVoidMethod(_flurryClass, _flurryLogTimedEventMap, new JValue(new Java.Lang.String(eventName)), new JValue(actualParams), new JValue(timed)));
  133.         }
  134.  
  135.         public void EndTimedEvent(string eventName)
  136.         {
  137.             ExceptionSafe(() => JNIEnv.CallStaticVoidMethod(_flurryClass, _flurryEndTimedEvent, new JValue(new Java.Lang.String(eventName))));
  138.         }
  139.         #endregion
  140.  
  141.         #region ADDITIONAL USER INFORMATION
  142.         public void setUserId(string userId)
  143.         {
  144.             ExceptionSafe(() => JNIEnv.CallStaticVoidMethod(_flurryClass, _flurrySetUserId, new JValue(new Java.Lang.String(userId))));
  145.         }
  146.  
  147.         public void setAge(int age)
  148.         {
  149.             ExceptionSafe(() => JNIEnv.CallStaticVoidMethod(_flurryClass, _flurrySetAge, new JValue(age)));
  150.         }
  151.  
  152.         public void setGender(byte gender)
  153.         {
  154.             ExceptionSafe(() => JNIEnv.CallStaticVoidMethod(_flurryClass, _flurrySetGender, new JValue(gender)));
  155.         }
  156.         #endregion
  157.  
  158.         #region LOCATION
  159.         public void setReportLocation(bool enabled)
  160.         {
  161.             ExceptionSafe(() => JNIEnv.CallStaticVoidMethod(_flurryClass, _flurrySetReportLocation, new JValue(enabled)));
  162.         }
  163.         #endregion
  164.  
  165.         #region ERROR / LOG
  166.         public void setLogEnabled(bool enabled)
  167.         {
  168.             ExceptionSafe(() => JNIEnv.CallStaticVoidMethod(_flurryClass, _flurrySetLogEnabled, new JValue(enabled)));
  169.         }
  170.  
  171.         public void onError(string errorId, string message, string errorClass)
  172.         {
  173.             ExceptionSafe(() => JNIEnv.CallStaticVoidMethod(_flurryClass, _flurryOnError, new JValue(new Java.Lang.String(errorId)), new JValue(new Java.Lang.String(message)), new JValue(new Java.Lang.String(errorClass))));
  174.         }
  175.         #endregion
  176.  
  177.         #region HELPERS
  178.         private static void ExceptionSafe(Action action)
  179.         {
  180.             try
  181.             {
  182.                 action();
  183.             }
  184.             catch (ThreadAbortException)
  185.             {
  186.                 throw;
  187.             }
  188.             catch (Exception exception)
  189.             {
  190.                 Log.Info("FlurryClient", "Exception seen in calling Flurry through JNI {0}", exception.ToString());
  191.             }
  192.         }
  193.         #endregion
  194.  
  195.         ~FlurryClient()
  196.         {
  197.             JNIEnv.DeleteGlobalRef(_flurryClass);
  198.         }
  199.     }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement