|
Ocean
|
This class implements helper functions for android platforms. More...
#include <Utilities.h>
Static Public Member Functions | |
| static bool | clearPotentialException (JNIEnv &jniEnvironment) |
| Checks whether a Java exception is pending on the calling thread, and clears it. | |
| static ScopedJClass | findClass (JNIEnv &jniEnvironment, const std::string &className) |
| Returns the class with a given name, and handles a potential exception. | |
| static jmethodID | getMethodId (JNIEnv &jniEnvironment, jclass javaClass, const std::string &name, const std::string &signature) |
| Returns the id of a non-static method of a class, and handles a potential exception. | |
| static jmethodID | getStaticMethodId (JNIEnv &jniEnvironment, jclass javaClass, const std::string &name, const std::string &signature) |
| Returns the id of a static method of a class, and handles a potential exception. | |
| static jfieldID | getFieldId (JNIEnv &jniEnvironment, jclass javaClass, const std::string &name, const std::string &signature) |
| Returns the id of a non-static field of a class, and handles a potential exception. | |
| static jfieldID | getStaticFieldId (JNIEnv &jniEnvironment, jclass javaClass, const std::string &name, const std::string &signature) |
| Returns the id of a static field of a class, and handles a potential exception. | |
| template<typename T = jobject, typename... TArgs> | |
| static ScopedJNILocalObject< T > | callObjectMethod (JNIEnv &jniEnvironment, jobject object, jmethodID methodId, TArgs &&... args) |
| Calls a non-static Java method returning an object, and handles a potential exception. | |
| template<typename T = jobject, typename... TArgs> | |
| static ScopedJNILocalObject< T > | callStaticObjectMethod (JNIEnv &jniEnvironment, jclass javaClass, jmethodID methodId, TArgs &&... args) |
| Calls a static Java method returning an object, and handles a potential exception. | |
| template<typename T = jobject, typename... TArgs> | |
| static ScopedJNILocalObject< T > | newObject (JNIEnv &jniEnvironment, jclass javaClass, jmethodID methodId, TArgs &&... args) |
| Creates a new instance of a class, and handles a potential exception. | |
| template<typename... TArgs> | |
| static bool | callVoidMethod (JNIEnv &jniEnvironment, jobject object, jmethodID methodId, TArgs &&... args) |
| Calls a non-static Java method without return value, and handles a potential exception. | |
| template<typename... TArgs> | |
| static bool | callBooleanMethod (JNIEnv &jniEnvironment, jobject object, jmethodID methodId, bool &returnValue, TArgs &&... args) |
| Calls a non-static Java method returning a boolean, and handles a potential exception. | |
| template<typename... TArgs> | |
| static bool | callStaticBooleanMethod (JNIEnv &jniEnvironment, jclass javaClass, jmethodID methodId, bool &returnValue, TArgs &&... args) |
| Calls a static Java method returning a boolean, and handles a potential exception. | |
| template<typename... TArgs> | |
| static bool | callIntMethod (JNIEnv &jniEnvironment, jobject object, jmethodID methodId, int32_t &returnValue, TArgs &&... args) |
| Calls a non-static Java method returning an integer, and handles a potential exception. | |
| template<typename... TArgs> | |
| static bool | callStaticIntMethod (JNIEnv &jniEnvironment, jclass javaClass, jmethodID methodId, int32_t &returnValue, TArgs &&... args) |
| Calls a static Java method returning an integer, and handles a potential exception. | |
| template<typename... TArgs> | |
| static bool | callFloatMethod (JNIEnv &jniEnvironment, jobject object, jmethodID methodId, float &returnValue, TArgs &&... args) |
| Calls a non-static Java method returning a float, and handles a potential exception. | |
| static std::string | toAString (JNIEnv *env, jstring javaString) |
| Converts a Java native string to a std string. | |
| static jstring | toJavaString (JNIEnv *env, const std::string &stdString) |
| Converts a std string to a Java native string. | |
| static jobjectArray | toJavaStringArray (JNIEnv *env, const Strings &strings) |
| Converts a vector of std strings to a Java array with native strings. | |
| static bool | toVector (JNIEnv *env, jobject javaStringList, Strings &strings) |
| Converts a Java native list with string to a vector of strings. | |
| static bool | toVector (JNIEnv *env, jobject javaIntegerList, std::vector< int > &values) |
| Converts a Java native list with integer to a vector of integers. | |
| static bool | className (JNIEnv *env, jobject object, std::string &name) |
| Returns the class name of an object. | |
| static bool | manifestVersion (JavaVM *javaVM, jobject activity, int &versionCode, std::string &versionName) |
| Returns the version code stored in the application manifest. | |
| static bool | manifestVersionCode (JavaVM *javaVM, jobject activity, int &versionCode) |
| Deprecated. | |
| static bool | manifestSdkVersions (JNIEnv *env, jobject activity, unsigned int &minSdkVersion, unsigned int &targetSdkVersion) |
| Returns the minimum and target SDK versions stored in the application manifest. | |
| static bool | androidReleaseVersion (JNIEnv *env, std::string &version) |
| Return the Android release version of the system (android.os.Build.VERSION.RELEASE). | |
| static bool | androidSdkVersion (JNIEnv *env, unsigned int &version) |
| Return the Android SDK version of the system (android.os.Build.VERSION.SDK). | |
| static bool | systemPropertyValue (const std::string &name, std::string &value) |
| Returns the value of a specific system property. | |
| static bool | deviceBrand (JNIEnv *env, std::string &brand) |
| Returns the brand string of the device. | |
| static bool | deviceModel (JNIEnv *env, std::string &model) |
| Returns the model string of the device. | |
| static bool | deviceSerialNumber (std::string &serialNumber) |
| Returns the serial number of the device. | |
| static bool | determinePackageName (std::string &packageName) |
| Returns the name of the package. | |
| static bool | sendIntentToComponent (JNIEnv *env, jobject activity, const std::string &packageName, const std::string &className, const std::string &extraText) |
| Sends an explicit intent to another component or application. | |
| static bool | startActivity (JNIEnv *env, jobject rootActivity, const std::string &activityClassName) |
| Starts an new activity from a root activity. | |
| static bool | connectToWifi (JNIEnv *env, jobject activity, const std::string &ssid, const std::string &password) |
| Joins a Wi-Fi network (WPA2-PSK). | |
| static bool | sendIntentToConnectToWifi (JNIEnv *env, jobject activity, const std::string &ssid, const std::string &password) |
| Sends an intent to the OS to request it to join a Wi-Fi network (WPA2-PSK). | |
| static bool | currentWifiSsid (JNIEnv *env, jobject activity, std::string &ssid) |
| Returns current Wi-Fi network name (SSID). | |
| static bool | triggerVibration (JNIEnv *env, jobject activity, unsigned int intensity=1u, const unsigned int duration=50u) |
| Triggers a vibration. | |
| static bool | displayRefreshRate (JNIEnv *env, jobject activity, float &refreshRateHz) |
| Returns the refresh rate of the display in Hz. | |
Static Protected Member Functions | |
| template<typename... TArgs> | |
| static constexpr bool | isValidArgumentTypes () |
| Returns whether all given data types can be handed to a JNI function call with variable arguments. | |
This class implements helper functions for android platforms.
|
static |
|
static |
|
static |
Calls a non-static Java method returning a boolean, and handles a potential exception.
The return value is provided as a parameter, as a thrown exception and a returned 'false' cannot be distinguished otherwise.
| jniEnvironment | The JNI environment of the calling thread |
| object | The object on which the method will be called, must be valid |
| methodId | The id of the method to be called, must be valid |
| returnValue | The resulting return value of the method, only valid if this function succeeds |
| args | The arguments of the method, must match the signature of the method |
| TArgs | The data types of the arguments of the method |
|
static |
Calls a non-static Java method returning a float, and handles a potential exception.
The return value is provided as a parameter, as a thrown exception and a returned '0' cannot be distinguished otherwise.
| jniEnvironment | The JNI environment of the calling thread |
| object | The object on which the method will be called, must be valid |
| methodId | The id of the method to be called, must be valid |
| returnValue | The resulting return value of the method, only valid if this function succeeds |
| args | The arguments of the method, must match the signature of the method |
| TArgs | The data types of the arguments of the method |
|
static |
Calls a non-static Java method returning an integer, and handles a potential exception.
The return value is provided as a parameter, as a thrown exception and a returned '0' cannot be distinguished otherwise.
| jniEnvironment | The JNI environment of the calling thread |
| object | The object on which the method will be called, must be valid |
| methodId | The id of the method to be called, must be valid |
| returnValue | The resulting return value of the method, only valid if this function succeeds |
| args | The arguments of the method, must match the signature of the method |
| TArgs | The data types of the arguments of the method |
|
static |
Calls a non-static Java method returning an object, and handles a potential exception.
| jniEnvironment | The JNI environment of the calling thread |
| object | The object on which the method will be called, must be valid |
| methodId | The id of the method to be called, must be valid |
| args | The arguments of the method, must match the signature of the method |
| T | The data type of the returned reference, e.g., jobject or jstring |
| TArgs | The data types of the arguments of the method |
|
static |
Calls a static Java method returning a boolean, and handles a potential exception.
The return value is provided as a parameter, as a thrown exception and a returned 'false' cannot be distinguished otherwise.
| jniEnvironment | The JNI environment of the calling thread |
| javaClass | The class providing the method, must be valid |
| methodId | The id of the method to be called, must be valid |
| returnValue | The resulting return value of the method, only valid if this function succeeds |
| args | The arguments of the method, must match the signature of the method |
| TArgs | The data types of the arguments of the method |
|
static |
Calls a static Java method returning an integer, and handles a potential exception.
The return value is provided as a parameter, as a thrown exception and a returned '0' cannot be distinguished otherwise.
| jniEnvironment | The JNI environment of the calling thread |
| javaClass | The class providing the method, must be valid |
| methodId | The id of the method to be called, must be valid |
| returnValue | The resulting return value of the method, only valid if this function succeeds |
| args | The arguments of the method, must match the signature of the method |
| TArgs | The data types of the arguments of the method |
|
static |
Calls a static Java method returning an object, and handles a potential exception.
| jniEnvironment | The JNI environment of the calling thread |
| javaClass | The class providing the method, must be valid |
| methodId | The id of the method to be called, must be valid |
| args | The arguments of the method, must match the signature of the method |
| T | The data type of the returned reference, e.g., jobject or jstring |
| TArgs | The data types of the arguments of the method |
|
static |
Calls a non-static Java method without return value, and handles a potential exception.
| jniEnvironment | The JNI environment of the calling thread |
| object | The object on which the method will be called, must be valid |
| methodId | The id of the method to be called, must be valid |
| args | The arguments of the method, must match the signature of the method |
| TArgs | The data types of the arguments of the method |
|
static |
Returns the class name of an object.
| env | The Java environment, must be valid |
| object | The object for which the class name will be returned, must be valid |
| name | The resulting class name |
|
static |
Checks whether a Java exception is pending on the calling thread, and clears it.
A pending exception makes almost every following JNI call undefined, and it terminates the process in case the thread detaches while the exception is still pending. An exception must therefore be cleared before the calling thread makes any further JNI call, returns to Java, or ends. The description of the exception is written to the error log, as Ocean cannot handle a Java exception in any other way. This function is safe to call at any time, also when no exception is pending.
| jniEnvironment | The JNI environment of the calling thread |
|
static |
Joins a Wi-Fi network (WPA2-PSK).
This only works with Android SDK version 28 or below.
| env | The Java environment, must be valid |
| activity | The root activity from which the new activity will be started, must be valid |
| ssid | The Wi-Fi network name (SSID), the network must be using WPA2, must be valid |
| password | The Wi-Fi network password, must be valid |
|
static |
Returns current Wi-Fi network name (SSID).
| env | The Java environment, must be valid |
| activity | The root activity from which the new activity will be started, must be valid |
| ssid | The resulting Wi-Fi network name (SSID), will be empty if there is no Wi-Fi connection |
|
static |
Returns the name of the package.
| packageName | The resulting package name |
|
static |
Returns the brand string of the device.
| env | The Java environment, must be valid |
| brand | The resulting model string |
|
static |
Returns the model string of the device.
| env | The Java environment, must be valid |
| model | The resulting model string |
|
static |
Returns the serial number of the device.
On Android 8.0 (API 26) and above, access to the serial number is restricted to system apps.
| serialNumber | The resulting device's serial number |
|
static |
Returns the refresh rate of the display in Hz.
On Android 30 (API 30) and above, uses Activity.getDisplay(). On older versions, uses WindowManager.getDefaultDisplay().
| env | The Java environment, must be valid |
| activity | The Android main activity, must be valid |
| refreshRateHz | The resulting refresh rate in Hz, will be 0 if the display could not be determined |
|
static |
Returns the class with a given name, and handles a potential exception.
Beware: A thread which has been attached by Ocean has no Java frames on the stack, so that the class is resolved with the bootstrap class loader and an application class cannot be found. Resolve application classes on a thread which has been called from Java, and keep them as a global reference.
| jniEnvironment | The JNI environment of the calling thread |
| className | The name of the class to be returned, e.g., "android/content/Intent", must be valid |
|
static |
Returns the id of a non-static field of a class, and handles a potential exception.
| jniEnvironment | The JNI environment of the calling thread |
| javaClass | The class providing the field, must be valid |
| name | The name of the field, must be valid |
| signature | The signature of the field, e.g., "Ljava/lang/String;", must be valid |
|
static |
Returns the id of a non-static method of a class, and handles a potential exception.
| jniEnvironment | The JNI environment of the calling thread |
| javaClass | The class providing the method, must be valid |
| name | The name of the method, must be valid |
| signature | The signature of the method, e.g., "(Ljava/lang/String;)I", must be valid |
|
static |
Returns the id of a static field of a class, and handles a potential exception.
| jniEnvironment | The JNI environment of the calling thread |
| javaClass | The class providing the field, must be valid |
| name | The name of the field, must be valid |
| signature | The signature of the field, e.g., "Ljava/lang/String;", must be valid |
|
static |
Returns the id of a static method of a class, and handles a potential exception.
| jniEnvironment | The JNI environment of the calling thread |
| javaClass | The class providing the method, must be valid |
| name | The name of the method, must be valid |
| signature | The signature of the method, e.g., "(Ljava/lang/String;)I", must be valid |
|
staticconstexprprotected |
Returns whether all given data types can be handed to a JNI function call with variable arguments.
A JNI argument is either a reference like jobject or jstring, or a primitive like jint or jboolean, as any other type would be pushed onto the variable argument list of the runtime. Beware: This does not verify that the arguments match the signature of the Java method, e.g., an jint handed to a method expecting a jlong is still wrong.
| TArgs | The data types to be checked, can be empty |
|
static |
Returns the minimum and target SDK versions stored in the application manifest.
| env | The Java environment, must be valid |
| activity | The Android main activity, must be valid |
| minSdkVersion | The resulting minimum SDK version of the application as defined in its manifest |
| targetSdkVersion | The resulting target SDK version of the application as defined in its manifest |
|
static |
Returns the version code stored in the application manifest.
| javaVM | The Java virtual machine, must be valid |
| activity | The Android main activity, must be valid |
| versionCode | The resulting version code of the application |
| versionName | The resulting version name of the application |
|
static |
Deprecated.
Returns the version code stored in the application manifest.
| javaVM | The Java virtual machine, must be valid |
| activity | The Android main activity, must be valid |
| versionCode | The version code of the application |
|
static |
Creates a new instance of a class, and handles a potential exception.
| jniEnvironment | The JNI environment of the calling thread |
| javaClass | The class to be instantiated, must be valid |
| methodId | The id of the constructor to be called, must be valid |
| args | The arguments of the constructor, must match the signature of the constructor |
| T | The data type of the returned reference, e.g., jobject or jstring |
| TArgs | The data types of the arguments of the constructor |
|
static |
Sends an explicit intent to another component or application.
| env | The Java environment, must be valid |
| activity | The Android main activity, must be valid |
| packageName | The name of the package to which the intent will be sent, must be valid |
| className | The name of the class or activity to which the intent will be sent, must be valid |
| extraText | The data that will be sent with the intent, must be valid |
|
static |
Sends an intent to the OS to request it to join a Wi-Fi network (WPA2-PSK).
The intent is send to the system settings app to save and connect to a new Wi-Fi network. This will display a notification to user to accept or reject the new Wi-Fi network. Additional notes:
android.permission.CHANGE_NETWORK_STATE and android.permission.WRITE_SETTINGS to be set in the app manifest to work.android.permission.CHANGE_NETWORK_STATE again. Re-tries are possible.Settings.ACTION_WIFI_ADD_NETWORKS. Otherwise this function will have no effect (and still return true). | env | The Java environment, must be valid |
| activity | The root activity from which the new activity will be started, must be valid |
| ssid | The Wi-Fi network name (SSID), the network must be using WPA2, must be valid |
| password | The Wi-Fi network password, must be valid |
|
static |
Starts an new activity from a root activity.
| env | The Java environment, must be valid |
| rootActivity | The root activity from which the new activity will be started, must be valid |
| activityClassName | The name of the class of the activity to start, must be valid |
|
static |
Returns the value of a specific system property.
| name | The name of the system property, must be valid |
| value | The resulting value |
|
static |
Converts a Java native string to a std string.
| env | The Java environment, must be valid |
| javaString | Java string to be converted, may be nullptr |
|
static |
Converts a std string to a Java native string.
| env | The Java environment, must be valid |
| stdString | Std string to be converted |
|
static |
Converts a vector of std strings to a Java array with native strings.
| env | The Java environment, must be valid |
| strings | Std strings to be converted, can be empty |
|
static |
Converts a Java native list with integer to a vector of integers.
| env | The Java environment, must be valid |
| javaIntegerList | Java list with integers to be converted, must be valid |
| values | The resulting vector of integers |
|
static |
Converts a Java native list with string to a vector of strings.
| env | The Java environment, must be valid |
| javaStringList | Java list with strings to be converted, must be valid |
| strings | The resulting vector of strings |
|
static |
Triggers a vibration.
This function needs permission 'android.permission.VIBRATE'.
| env | The Java environment, must be valid |
| activity | The root activity from which the new activity will be started, must be valid |
| intensity | The intensity of the vibration, the higher the stronger, with range [0, 2] |
| duration | The duration of the vibration in milliseconds, with range [1, infinity) |