public static void CheckException() { IntPtr localref = AndroidJNI.ExceptionOccurred(); if (!(localref != IntPtr.Zero)) { return; } AndroidJNI.ExceptionClear(); IntPtr class1 = AndroidJNI.FindClass("java/lang/Throwable"); IntPtr class2 = AndroidJNI.FindClass("android/util/Log"); try { IntPtr methodId = AndroidJNI.GetMethodID(class1, "toString", "()Ljava/lang/String;"); IntPtr staticmethodId = AndroidJNI.GetStaticMethodID(class2, "getStackTraceString", "(Ljava/lang/Throwable;)Ljava/lang/String;"); string message = AndroidJNI.CallStringMethod(localref, methodId, new jvalue[0]); jvalue[] args = new jvalue[1]; args[0].l = localref; string javaStackTrace = AndroidJNI.CallStaticStringMethod(class2, staticmethodId, args); throw new AndroidJavaException(message, javaStackTrace); } finally { DeleteLocalRef(localref); DeleteLocalRef(class1); DeleteLocalRef(class2); } }
static OuyaContent() { try { { string strName = "tv/ouya/console/api/content/OuyaContent"; IntPtr localRef = AndroidJNI.FindClass(strName); if (localRef != IntPtr.Zero) { #if VERBOSE_LOGGING Debug.Log(string.Format("Found {0} class", strName)); #endif _jcOuyaContent = AndroidJNI.NewGlobalRef(localRef); AndroidJNI.DeleteLocalRef(localRef); } else { Debug.LogError(string.Format("Failed to find {0} class", strName)); } } } catch (System.Exception ex) { Debug.LogError(string.Format("Exception loading JNI - {0}", ex)); } }
public static string GetStaticStringField(string clsName, string name) { IntPtr clazz = AndroidJNI.FindClass(clsName); IntPtr fieldID = AndroidJNI.GetStaticFieldID(clazz, name, "Ljava/lang/String;"); return(AndroidJNI.GetStaticStringField(clazz, fieldID)); }
/// <summary> /// Gets the value of a static float field. /// </summary> /// <returns>The static float field.</returns> /// <param name="clsName">Cls name.</param> /// <param name="name">Name.</param> public static float GetStaticFloatField(string clsName, string name) { IntPtr rawClass = AndroidJNI.FindClass(clsName); IntPtr method = AndroidJNI.GetStaticFieldID(rawClass, name, "F"); return(AndroidJNI.GetStaticFloatField(rawClass, method)); }
public static T StaticInvokeObjectCall <T>(string type, string name, string sig, params object[] args) { IntPtr intPtr = AndroidJNI.FindClass(type); IntPtr staticMethodID = AndroidJNI.GetStaticMethodID(intPtr, name, sig); jvalue[] array = ConstructArgArray(args); IntPtr intPtr2 = AndroidJNI.CallStaticObjectMethod(intPtr, staticMethodID, array); ConstructorInfo constructor = typeof(T).GetConstructor(new Type[1] { intPtr2.GetType() }); if (constructor != null) { return((T)constructor.Invoke(new object[1] { intPtr2 })); } if (typeof(T).IsArray) { return(AndroidJNIHelper.ConvertFromJNIArray <T>(intPtr2)); } Debug.Log((object)"Trying cast...."); Type typeFromHandle = typeof(T); return((T)Marshal.PtrToStructure(intPtr2, typeFromHandle)); }
static CompressFormat() { try { IntPtr localRef; { string strName = "android/graphics/Bitmap$CompressFormat"; localRef = AndroidJNI.FindClass(strName); if (localRef != IntPtr.Zero) { #if VERBOSE_LOGGING Debug.Log(string.Format("Found {0} class", strName)); #endif _jcCompressFormat = AndroidJNI.NewGlobalRef(localRef); AndroidJNI.DeleteLocalRef(localRef); } else { Debug.LogError(string.Format("Failed to find {0} class", strName)); return; } } } catch (System.Exception ex) { Debug.LogError(string.Format("Exception loading JNI - {0}", ex)); } }
/// <summary> /// Calls a static method with an object return type. /// </summary> /// <returns>The invoke call.</returns> /// <param name="type">Type.</param> /// <param name="name">Name.</param> /// <param name="sig">Sig.</param> /// <param name="args">Arguments.</param> /// <typeparam name="T">The 1st type parameter.</typeparam> public static T StaticInvokeObjectCall <T>( string type, string name, string sig, params object[] args) { IntPtr rawClass = AndroidJNI.FindClass(type); IntPtr method = AndroidJNI.GetStaticMethodID(rawClass, name, sig); jvalue[] jArgs = ConstructArgArray(args); try { IntPtr val = AndroidJNI.CallStaticObjectMethod(rawClass, method, jArgs); ConstructorInfo c = typeof(T).GetConstructor(new Type[] { val.GetType() }); if (c != null) { return((T)c.Invoke(new object[] { val })); } if (typeof(T).IsArray) { // make an array //TODO: handle arrays of objects return(AndroidJNIHelper.ConvertFromJNIArray <T>(val)); } Debug.Log("Trying cast...."); Type t = typeof(T); return((T)Marshal.PtrToStructure(val, t)); } finally { AndroidJNIHelper.DeleteJNIArgArray(args, jArgs); } }
void Start() { #if ANDROID_DEVICE && !UNITY_EDITOR string appPath = ""; try { IntPtr obj_context = AndroidJNI.FindClass("android/content/ContextWrapper"); IntPtr method_getFilesDir = AndroidJNIHelper.GetMethodID(obj_context, "getFilesDir", "()Ljava/io/File;"); using (AndroidJavaClass cls_UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) { using (AndroidJavaObject obj_Activity = cls_UnityPlayer.GetStatic <AndroidJavaObject>("currentActivity")) { IntPtr file = AndroidJNI.CallObjectMethod(obj_Activity.GetRawObject(), method_getFilesDir, new jvalue[0]); IntPtr obj_file = AndroidJNI.FindClass("java/io/File"); IntPtr method_getAbsolutePath = AndroidJNIHelper.GetMethodID(obj_file, "getAbsolutePath", "()Ljava/lang/String;"); appPath = AndroidJNI.CallStringMethod(file, method_getAbsolutePath, new jvalue[0]); if (appPath == null) { return; } Debug.Log("Android app path: " + appPath); } } appPath = appPath.Replace("/files", "/lib/"); } catch (Exception e) { Debug.Log(e.ToString()); } Pvr_SpatializerUnlock(appPath); #endif }
public static string AppData() { if (Application.platform == RuntimePlatform.Android) { try { System.IntPtr obj_context = AndroidJNI.FindClass("android/content/ContextWrapper"); System.IntPtr method_getFilesDir = AndroidJNIHelper.GetMethodID(obj_context, "getFilesDir", "()Ljava/io/File;"); using (AndroidJavaClass cls_UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) { using (AndroidJavaObject obj_Activity = cls_UnityPlayer.GetStatic <AndroidJavaObject>("currentActivity")) { System.IntPtr file = AndroidJNI.CallObjectMethod(obj_Activity.GetRawObject(), method_getFilesDir, new jvalue[0]); System.IntPtr obj_file = AndroidJNI.FindClass("java/io/File"); System.IntPtr method_getAbsolutePath = AndroidJNIHelper.GetMethodID(obj_file, "getAbsolutePath", "()Ljava/lang/String;"); string path = AndroidJNI.CallStringMethod(file, method_getAbsolutePath, new jvalue[0]); if (path != null) { ValkyrieDebug.Log(path); return(path); } } } } catch (System.Exception e) { ValkyrieDebug.Log(e.ToString()); } return(""); } return(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "/Valkyrie"); }
string getDataPath() { #if UNITY_ANDROID && !UNITY_EDITOR string path = ""; try { IntPtr obj_context = AndroidJNI.FindClass("android/content/ContextWrapper"); IntPtr method_getFilesDir = AndroidJNIHelper.GetMethodID(obj_context, "getFilesDir", "()Ljava/io/File;"); using (AndroidJavaClass cls_UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) { using (AndroidJavaObject obj_Activity = cls_UnityPlayer.GetStatic <AndroidJavaObject>("currentActivity")) { IntPtr file = AndroidJNI.CallObjectMethod(obj_Activity.GetRawObject(), method_getFilesDir, new jvalue[0]); IntPtr obj_file = AndroidJNI.FindClass("java/io/File"); IntPtr method_getAbsolutePath = AndroidJNIHelper.GetMethodID(obj_file, "getAbsolutePath", "()Ljava/lang/String;"); path = AndroidJNI.CallStringMethod(file, method_getAbsolutePath, new jvalue[0]); if (path != null) { ////Debug.Log("Got internal path: " + path); } else { ////Debug.Log("Using fallback path"); path = "/data/data/com.DefaultCompany.sdkGame/files"; } } } return(path); } catch (Exception e) { ////Debug.Log(e.ToString()); return(""); } #else return(Application.persistentDataPath); #endif }
static ByteArrayOutputStream() { try { { string strName = "java/io/ByteArrayOutputStream"; IntPtr localRef = AndroidJNI.FindClass(strName); if (localRef != IntPtr.Zero) { #if VERBOSE_LOGGING Debug.Log(string.Format("Found {0} class", strName)); #endif _jcByteArrayOutputStream = AndroidJNI.NewGlobalRef(localRef); AndroidJNI.DeleteLocalRef(localRef); } else { Debug.LogError(string.Format("Failed to find {0} class", strName)); return; } } } catch (System.Exception ex) { Debug.LogError(string.Format("Exception loading JNI - {0}", ex)); } }
private static JavaActivity GetJavaActivity() { var unityPlayerClass = AndroidJNI.FindClass("com/unity3d/player/UnityPlayer"); var currentActivityField = AndroidJNI.GetStaticFieldID(unityPlayerClass, "currentActivity", "Landroid/app/Activity;"); return(AndroidJNI.GetStaticObjectField(unityPlayerClass, currentActivityField)); }
static JSONArray() { try { { string strName = "org/json/JSONArray"; _jcJsonArray = AndroidJNI.FindClass(strName); if (_jcJsonArray != IntPtr.Zero) { #if VERBOSE_LOGGING Debug.Log(string.Format("Found {0} class", strName)); #endif _jcJsonArray = AndroidJNI.NewGlobalRef(_jcJsonArray); } else { Debug.LogError(string.Format("Failed to find {0} class", strName)); return; } } } catch (System.Exception ex) { Debug.LogError(string.Format("Exception loading JNI - {0}", ex)); } }
// -- https://developers.helpshift.com/unity/sdk-configuration-ios/#install-config public void Init() { IntPtr clazzPtr = AndroidJNI.FindClass("com.snow.dbt"); if (clazzPtr != IntPtr.Zero) { Debug.Log("=====================================Got"); } else { Debug.Log("=====================================Not Got"); } CSharpButton.onClick.AddListener(ClickCSharpButton); JavaButton.onClick.AddListener(ClickJavaButton); CppButton.onClick.AddListener(ClickCppButton); help = HelpshiftSdk.getInstance(); Dictionary <string, object> configMap = new Dictionary <string, object>(); configMap.Add("unityGameObject", "UIGetVersion"); configMap.Add("enableInAppNotification", "yes"); #if UNITY_IOS help.registerForLocalNotifications(); help.registerHelpshiftDeepLinking(); #endif help.install(GateKeeper.HelpShiftApiKey, GateKeeper.HelpShiftDomainName, GateKeeper.HelpShiftAppId, configMap); help.setNameAndEmail("Snow Li", "*****@*****.**"); help.setUserIdentifier("userid.10015"); getNofify(); }
public static float GetStaticFloatField(string clsName, string name) { IntPtr num = AndroidJNI.FindClass(clsName); IntPtr staticFieldId = AndroidJNI.GetStaticFieldID(num, name, "F"); return(AndroidJNI.GetStaticFloatField(num, staticFieldId)); }
#pragma warning restore 0414 public static void AndroidApplicationContextSet() { #if UNITY_ANDROID && !UNITY_EDITOR if (haveSetApplicationContext) { return; } haveSetApplicationContext = true; AndroidJavaClass player = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); AndroidJavaObject activity = player.GetStatic <AndroidJavaObject>("currentActivity"); // retrieve the Unity Android context AndroidJavaObject[] args = new AndroidJavaObject[1]; args[0] = activity.Call <AndroidJavaObject>("getApplicationContext"); // Retrieve the zappar library IntPtr zcv_JNI = AndroidJNI.FindClass("com/zappar/zcv/ZCV"); bool isStatic = true; string methodSignature = "(Ljava/lang/Object;)V"; string methodName = "android_application_context_set"; // Retrieve the function we want to call IntPtr method_AndroidApplicationContextSet = AndroidJNIHelper.GetMethodID(zcv_JNI, methodName, methodSignature, isStatic); // pass the Unity context to the zappar library AndroidJNI.CallStaticVoidMethod(zcv_JNI, method_AndroidApplicationContextSet, AndroidJNIHelper.CreateJNIArgArray(args)); #endif }
public static T StaticInvokeObjectCall <T>(string type, string name, string sig, params object[] args) { IntPtr num = AndroidJNI.FindClass(type); IntPtr staticMethodId = AndroidJNI.GetStaticMethodID(num, name, sig); jvalue[] jvalueArray = JavaObjWrapper.ConstructArgArray(args); IntPtr ptr = AndroidJNI.CallStaticObjectMethod(num, staticMethodId, jvalueArray); ConstructorInfo constructor = typeof(T).GetConstructor(new Type[1] { ptr.GetType() }); if ((object)constructor != null) { return((T)constructor.Invoke(new object[1] { (object)ptr })); } if (typeof(T).IsArray) { return(AndroidJNIHelper.ConvertFromJNIArray <T>(ptr)); } Debug.Log((object)"Trying cast...."); Type structureType = typeof(T); return((T)Marshal.PtrToStructure(ptr, structureType)); }
public static string GetSysTime() { AndroidJNI.AttachCurrentThread(); IntPtr javaClass = AndroidJNI.FindClass("com/unity3d/player/UnityPlayer"); IntPtr fid = AndroidJNI.GetStaticFieldID(javaClass, "currentActivity", "Landroid/app/Activity;"); IntPtr obj = AndroidJNI.GetStaticObjectField(javaClass, fid); IntPtr pluginClass = AndroidJNI.FindClass("com/hiscene/androidsysinfo/AndroidPlugin"); IntPtr initMethod = AndroidJNI.GetMethodID(pluginClass, "<init>", "(Landroid/content/Context;)V"); jvalue[] jv = new jvalue[1]; //TODO IntPtr pobj = AndroidJNI.NewObject(pluginClass, initMethod, jv); IntPtr enableMethod = AndroidJNI.GetMethodID(pluginClass, "getSysTime", "()Ljava/lang/String;"); return(AndroidJNI.CallStringMethod(pobj, enableMethod, new jvalue[1])); }
private static string InternalGetPersistentDataPath() { string path = ""; try { IntPtr obj_context = AndroidJNI.FindClass("android/os/Environment"); IntPtr method_getExternalStorageDirectory = AndroidJNIHelper.GetMethodID(obj_context, "getExternalStorageDirectory", "()Ljava/io/File;", true); IntPtr file = AndroidJNI.CallStaticObjectMethod(obj_context, method_getExternalStorageDirectory, new jvalue[0]); IntPtr obj_file = AndroidJNI.FindClass("java/io/File"); IntPtr method_getAbsolutePath = AndroidJNIHelper.GetMethodID(obj_file, "getAbsolutePath", "()Ljava/lang/String;"); path = AndroidJNI.CallStringMethod(file, method_getAbsolutePath, new jvalue[0]); if (path != null) { path += "/Android/data/" + GetPackageName() + "/files"; } else { path = ""; } } catch (Exception e) { Debug.Log(e.ToString()); } return(path); }
protected PurchasableVirtualItem(AndroidJavaObject jniVirtualItem) : base(jniVirtualItem) { StoreUtils.LogDebug(TAG, "Trying to create PurchasableVirtualItem with itemId: " + jniVirtualItem.Call <string>("getItemId")); using (AndroidJavaObject jniPurchaseType = jniVirtualItem.Call <AndroidJavaObject>("getPurchaseType")) { System.IntPtr cls = AndroidJNI.FindClass("com/soomla/store/purchaseTypes/PurchaseWithMarket"); if (AndroidJNI.IsInstanceOf(jniPurchaseType.GetRawObject(), cls)) { using (AndroidJavaObject jniMarketItem = jniPurchaseType.Call <AndroidJavaObject>("getMarketItem")) { PurchaseType = new PurchaseWithMarket(jniMarketItem.Call <string>("getProductId"), jniMarketItem.Call <double>("getPrice")); } } else { cls = AndroidJNI.FindClass("com/soomla/store/purchaseTypes/PurchaseWithVirtualItem"); if (AndroidJNI.IsInstanceOf(jniPurchaseType.GetRawObject(), cls)) { string itemId = jniPurchaseType.Call <string>("getTargetItemId"); int amount = jniPurchaseType.Call <int>("getAmount"); PurchaseType = new PurchaseWithVirtualItem(itemId, amount); } else { StoreUtils.LogError(TAG, "Couldn't determine what type of class is the given purchaseType."); } } } }
/// <summary> /// Gets the value of a static string field. /// </summary> /// <returns>The static string field.</returns> /// <param name="clsName">Cls name.</param> /// <param name="name">Name.</param> public static string GetStaticStringField(string clsName, string name) { IntPtr rawClass = AndroidJNI.FindClass(clsName); IntPtr method = AndroidJNI.GetStaticFieldID(rawClass, name, "Ljava/lang/String;"); return(AndroidJNI.GetStaticStringField(rawClass, method)); }
static JSONObject() { try { { string strName = "org/json/JSONObject"; IntPtr localRef = AndroidJNI.FindClass(strName); if (localRef != IntPtr.Zero) { #if VERBOSE_LOGGING Debug.Log(string.Format("Found {0} class", strName)); #endif _jcJsonObject = AndroidJNI.NewGlobalRef(localRef); AndroidJNI.DeleteLocalRef(localRef); } else { Debug.LogError(string.Format("Failed to find {0} class", strName)); return; } } } catch (System.Exception ex) { Debug.LogError(string.Format("Exception loading JNI - {0}", ex)); } }
public static float GetStaticFloatField(string clsName, string name) { IntPtr intPtr = AndroidJNI.FindClass(clsName); IntPtr staticFieldID = AndroidJNI.GetStaticFieldID(intPtr, name, "F"); return(AndroidJNI.GetStaticFloatField(intPtr, staticFieldID)); }
static AndroidFileSystemStream() { AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); if (unityPlayer == null) { throw new Exception("Unity player is invalid."); } AndroidJavaObject currentActivity = unityPlayer.GetStatic <AndroidJavaObject>("currentActivity"); if (currentActivity == null) { throw new Exception("Current activity is invalid."); } AndroidJavaObject assetManager = currentActivity.Call <AndroidJavaObject>("getAssets"); if (assetManager == null) { throw new Exception("Asset manager is invalid."); } s_AssetManager = assetManager; IntPtr inputStreamClassPtr = AndroidJNI.FindClass("java/io/InputStream"); s_InternalReadMethodId = AndroidJNIHelper.GetMethodID(inputStreamClassPtr, "read", "([BII)I"); s_InternalReadArgs = new jvalue[3]; AndroidJNI.DeleteLocalRef(inputStreamClassPtr); currentActivity.Dispose(); unityPlayer.Dispose(); }
public static void GetApps() { IntPtr clas = AndroidJNI.FindClass("MakeImageCache"); clas = AndroidJNI.NewGlobalRef(clas); IntPtr funcID = AndroidJNI.GetMethodID(clas, "getApps", "()V"); AndroidJNI.CallStaticVoidMethod(clas, funcID, null); IntPtr nameID = AndroidJNI.GetMethodID(clas, "getName", "(I)Ljava/lang/String;"); nameID = AndroidJNI.NewGlobalRef(nameID); IntPtr length = AndroidJNI.GetMethodID(clas, "getTotalApps", "()I"); length = AndroidJNI.NewGlobalRef(length); int j = AndroidJNI.CallIntMethod(clas, length, null); Debug.Log("hehehe " + j); jvalue[] val = new jvalue[1]; val[0] = new jvalue(); Debug.Log("loading apps ..."); for (int i = 0; i < j; i++) { val[0].i = i; Debug.Log("hehehe"); Debug.Log(AndroidJNI.CallStaticStringMethod(clas, nameID, val)); } Debug.Log("loaded apps!"); }
private void AndroidJNISafeCheckException() { IntPtr intPtr = AndroidJNI.ExceptionOccurred(); if (intPtr != IntPtr.Zero) { AndroidJNI.ExceptionClear(); IntPtr intPtr2 = AndroidJNI.FindClass("java/lang/Throwable"); IntPtr intPtr3 = AndroidJNI.FindClass("android/util/Log"); try { IntPtr methodID = AndroidJNI.GetMethodID(intPtr2, "toString", "()Ljava/lang/String;"); IntPtr staticMethodID = AndroidJNI.GetStaticMethodID(intPtr3, "getStackTraceString", "(Ljava/lang/Throwable;)Ljava/lang/String;"); string message = AndroidJNI.CallStringMethod(intPtr, methodID, new jvalue[0]); jvalue[] array = new jvalue[1]; array [0].l = intPtr; string javaStackTrace = AndroidJNI.CallStaticStringMethod(intPtr3, staticMethodID, array); throw new AndroidJavaException(message, javaStackTrace); } finally { AndroidJNISafeDeleteLocalRef(intPtr); AndroidJNISafeDeleteLocalRef(intPtr2); AndroidJNISafeDeleteLocalRef(intPtr3); } } }
public static T StaticInvokeObjectCall <T>(string type, string name, string sig, params object[] args) { IntPtr clazz = AndroidJNI.FindClass(type); IntPtr methodID = AndroidJNI.GetStaticMethodID(clazz, name, sig); jvalue[] jvalueArray = ConstructArgArray(args); IntPtr array = AndroidJNI.CallStaticObjectMethod(clazz, methodID, jvalueArray); Type[] types = new Type[] { array.GetType() }; ConstructorInfo constructor = typeof(T).GetConstructor(types); if (constructor != null) { object[] parameters = new object[] { array }; return((T)constructor.Invoke(parameters)); } if (typeof(T).IsArray) { return(AndroidJNIHelper.ConvertFromJNIArray <T>(array)); } Debug.Log("Trying cast...."); Type structureType = typeof(T); return((T)Marshal.PtrToStructure(array, structureType)); }
public static string GetStaticStringField(string clsName, string name) { IntPtr num = AndroidJNI.FindClass(clsName); IntPtr staticFieldId = AndroidJNI.GetStaticFieldID(num, name, "Ljava/lang/String;"); return(AndroidJNI.GetStaticStringField(num, staticFieldId)); }
public static int GetStaticIntField(string clsName, string name) { IntPtr clazz = AndroidJNI.FindClass(clsName); IntPtr fieldID = AndroidJNI.GetStaticFieldID(clazz, name, "I"); return(AndroidJNI.GetStaticIntField(clazz, fieldID)); }
static OuyaUnityActivity() { try { { string strName = "tv/ouya/sdk/OuyaUnityActivity"; _jcOuyaUnityActivity = AndroidJNI.FindClass(strName); if (_jcOuyaUnityActivity != IntPtr.Zero) { #if VERBOSE_LOGGING Debug.Log(string.Format("Found {0} class", strName)); #endif _jcOuyaUnityActivity = AndroidJNI.NewGlobalRef(_jcOuyaUnityActivity); } else { Debug.LogError(string.Format("Failed to find {0} class", strName)); return; } } } catch (System.Exception ex) { Debug.LogError(string.Format("Exception loading JNI - {0}", ex)); } }