private HelpshiftAndroidInboxMessage()
 {
     // Lazily init inboxInterfaceClass since we need to be sure that this class is loaded
     // even with dex loading delays.
     if (inboxInterfaceClass == null || inboxInterfaceClass.GetRawObject().ToInt32() == 0)
     {
         inboxInterfaceClass = new AndroidJavaClass("com.helpshift.campaigns.models.InboxMessage");
     }
 }
Esempio n. 2
0
    protected AndroidJavaObject dictionaryToHashMap(Dictionary <string, object> dictionary)
    {
        // Convert the C# Dictionary<string,object> to a Java Map<String,Object>
        AndroidJavaObject mapInstance = new AndroidJavaClass("java.util.HashMap");

        if (mapInstance == null)
        {
            UnityEngine.Debug.LogError("NewRelicAndroid: Could not instantiate HashMap class.");
        }

        // Call 'put' via the JNI instead of using helper classes to avoid:
        //  "JNI: Init'd AndroidJavaObject with null ptr!"
        IntPtr putMethod = AndroidJNIHelper.GetMethodID(mapInstance.GetRawClass(), "put",
                                                        "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");

        object[] putCallArgs = new object[2];

        if (dictionary != null)
        {
            foreach (KeyValuePair <string, object> kvp in dictionary)
            {
                var value = kvp.Value;                  // kvp.Value is read-only

                if (!(value is string || kvp.Value is double))
                {
                    try {
                        value = Double.Parse(value.ToString());
                    } catch (Exception e) {
                        UnityEngine.Debug.LogErrorFormat("Coercion from [{0}] to [{1}] failed: {2}",
                                                         kvp.Key.GetType(), Double.MaxValue.GetType(), e.Message);
                    }
                }

                if (value is string || value is double)
                {
                    using (AndroidJavaObject k = new AndroidJavaObject("java.lang.String", (object)kvp.Key)) {
                        if (value is string)
                        {
                            using (AndroidJavaObject v = new AndroidJavaObject("java.lang.String", (object)value)) {
                                putCallArgs [0] = k;
                                putCallArgs [1] = v;
                                AndroidJNI.CallObjectMethod(mapInstance.GetRawObject(),
                                                            putMethod, AndroidJNIHelper.CreateJNIArgArray(putCallArgs));
                            }
                        }
                        else
                        {
                            using (AndroidJavaObject v = new AndroidJavaObject("java.lang.Double", (object)value)) {
                                putCallArgs [0] = k;
                                putCallArgs [1] = v;
                                AndroidJNI.CallObjectMethod(mapInstance.GetRawObject(),
                                                            putMethod, AndroidJNIHelper.CreateJNIArgArray(putCallArgs));
                            }
                        }
                    }
                }
                else
                {
                    UnityEngine.Debug.LogError("NewRelicAndroid: Unsupported type - value must be either string or double: " + kvp.Value);
                    return(null);
                }
            }
        }

        return(mapInstance);
    }