Ocean
Ocean::Platform::Android::Utilities Class Reference

This class implements helper functions for android platforms. More...

Static Public Member Functions

static std::string toAString (JNIEnv *env, jstring javaString)
 Converts a Java native string to a std string. More...
 
static jstring toJavaString (JNIEnv *env, const std::string &stdString)
 Converts a std string to a Java native string. More...
 
static jobjectArray toJavaStringArray (JNIEnv *env, const std::vector< std::string > &strings)
 Converts a vector of std strings to a Java array with native strings. More...
 
static bool toVector (JNIEnv *env, jobject javaStringList, std::vector< std::string > &strings)
 Converts a Java native list with string to a vector of strings. More...
 
static bool toVector (JNIEnv *env, jobject javaIntegerList, std::vector< int > &values)
 Converts a Java native list with integer to a vector of integers. More...
 
static bool className (JNIEnv *env, jobject object, std::string &name)
 Returns the class name of an object. More...
 
static bool manifestVersion (JavaVM *javaVM, jobject activity, int &versionCode, std::string &versionName)
 Returns the version code stored in the application manifest. More...
 
static bool manifestVersionCode (JavaVM *javaVM, jobject activity, int &versionCode)
 Deprecated. More...
 
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. More...
 
static bool androidSdkVersion (JNIEnv *env, unsigned int &version)
 Return the Android SDK version of the system. More...
 
static bool systemPropertyValue (const std::string &name, std::string &value)
 Returns the value of a specific system property. More...
 
static bool deviceBrand (JNIEnv *env, std::string &brand)
 Returns the brand string of the device. More...
 
static bool deviceModel (JNIEnv *env, std::string &model)
 Returns the model string of the device. More...
 
static bool determinePackageName (std::string &packageName)
 Returns the name of the package. More...
 
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. More...
 
static bool startActivity (JNIEnv *env, jobject rootActivity, const std::string &activityClassName)
 Starts an new activity from a root activity. More...
 
static bool connectToWifi (JNIEnv *env, jobject activity, const std::string &ssid, const std::string &password)
 Joins a Wi-Fi network (WPA2-PSK). More...
 
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). More...
 
static bool currentWifiSsid (JNIEnv *env, jobject activity, std::string &ssid)
 Returns current Wi-Fi network name (SSID). More...
 
static bool triggerVibration (JNIEnv *env, jobject activity, unsigned int intensity=1u, const unsigned int duration=50u)
 Triggers a vibration. More...
 

Detailed Description

This class implements helper functions for android platforms.

Member Function Documentation

◆ androidSdkVersion()

static bool Ocean::Platform::Android::Utilities::androidSdkVersion ( JNIEnv *  env,
unsigned int &  version 
)
static

Return the Android SDK version of the system.

Parameters
envThe Java environment, must be valid
versionThe resulting Android SDK version, only valid if this function returns 'true'
Returns
True, if succeeded, otherwise false

◆ className()

static bool Ocean::Platform::Android::Utilities::className ( JNIEnv *  env,
jobject  object,
std::string &  name 
)
static

Returns the class name of an object.

Parameters
envThe Java environment, must be valid
objectThe object for which the class name will be returned, must be valid
nameThe resulting class name
Returns
True, if succeeded

◆ connectToWifi()

static bool Ocean::Platform::Android::Utilities::connectToWifi ( JNIEnv *  env,
jobject  activity,
const std::string &  ssid,
const std::string &  password 
)
static

Joins a Wi-Fi network (WPA2-PSK).

This only works with Android SDK version 28 or below.

Parameters
envThe Java environment, must be valid
activityThe root activity from which the new activity will be started, must be valid
ssidThe Wi-Fi network name (SSID), the network must be using WPA2, must be valid
passwordThe Wi-Fi network password, must be valid
Returns
True, if joined successfully, otherwise false

◆ currentWifiSsid()

static bool Ocean::Platform::Android::Utilities::currentWifiSsid ( JNIEnv *  env,
jobject  activity,
std::string &  ssid 
)
static

Returns current Wi-Fi network name (SSID).

Parameters
envThe Java environment, must be valid
activityThe root activity from which the new activity will be started, must be valid
ssidThe resulting Wi-Fi network name (SSID), will be empty if there is no Wi-Fi connection
Returns
True, if succeeded

◆ determinePackageName()

static bool Ocean::Platform::Android::Utilities::determinePackageName ( std::string &  packageName)
static

Returns the name of the package.

Parameters
packageNameThe resulting package name
Returns
True, if succeeded

◆ deviceBrand()

static bool Ocean::Platform::Android::Utilities::deviceBrand ( JNIEnv *  env,
std::string &  brand 
)
static

Returns the brand string of the device.

Parameters
envThe Java environment, must be valid
brandThe resulting model string
Returns
True, if succeeded

◆ deviceModel()

static bool Ocean::Platform::Android::Utilities::deviceModel ( JNIEnv *  env,
std::string &  model 
)
static

Returns the model string of the device.

Parameters
envThe Java environment, must be valid
modelThe resulting model string
Returns
True, if succeeded

◆ manifestSdkVersions()

static bool Ocean::Platform::Android::Utilities::manifestSdkVersions ( JNIEnv *  env,
jobject  activity,
unsigned int &  minSdkVersion,
unsigned int &  targetSdkVersion 
)
static

Returns the minimum and target SDK versions stored in the application manifest.

Parameters
envThe Java environment, must be valid
activityThe Android main activity, must be valid
minSdkVersionThe resulting minimum SDK version of the application as defined in its manifest
targetSdkVersionThe resulting target SDK version of the application as defined in its manifest
Returns
True, if succeeded

◆ manifestVersion()

static bool Ocean::Platform::Android::Utilities::manifestVersion ( JavaVM *  javaVM,
jobject  activity,
int &  versionCode,
std::string &  versionName 
)
static

Returns the version code stored in the application manifest.

Parameters
javaVMThe Java virtual machine, must be valid
activityThe Android main activity, must be valid
versionCodeThe resulting version code of the application
versionNameThe resulting version name of the application
Returns
True, if succeeded

◆ manifestVersionCode()

static bool Ocean::Platform::Android::Utilities::manifestVersionCode ( JavaVM *  javaVM,
jobject  activity,
int &  versionCode 
)
static

Deprecated.

Returns the version code stored in the application manifest.

Parameters
javaVMThe Java virtual machine, must be valid
activityThe Android main activity, must be valid
versionCodeThe version code of the application
Returns
True, if succeeded

◆ sendIntentToComponent()

static bool Ocean::Platform::Android::Utilities::sendIntentToComponent ( JNIEnv *  env,
jobject  activity,
const std::string &  packageName,
const std::string &  className,
const std::string &  extraText 
)
static

Sends an explicit intent to another component or application.

Parameters
envThe Java environment, must be valid
activityThe Android main activity, must be valid
packageNameThe name of the package to which the intent will be sent, must be valid
classNameThe name of the class or activity to which the intent will be sent, must be valid
extraTextThe data that will be sent with the intent, must be valid
Returns
True, if the intent was sent successfully, otherwise false

◆ sendIntentToConnectToWifi()

static bool Ocean::Platform::Android::Utilities::sendIntentToConnectToWifi ( JNIEnv *  env,
jobject  activity,
const std::string &  ssid,
const std::string &  password 
)
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:

  • The Wi-Fi network must use the security protocol WPA2-PSK.
  • This function requires Android SDK version 29 or higher.
  • This function requires the permissions android.permission.CHANGE_NETWORK_STATE and android.permission.WRITE_SETTINGS to be set in the app manifest to work.
  • Rejecting a new network does NOT revoke the permission android.permission.CHANGE_NETWORK_STATE again. Re-tries are possible.
  • The newly added networks do NOT have to reachable at the time of adding them (they will be stored for when they become available).
  • The system app receiving the intent sent here must implement a handler for the action type Settings.ACTION_WIFI_ADD_NETWORKS. Otherwise this function will have no effect (and still return true).
    Parameters
    envThe Java environment, must be valid
    activityThe root activity from which the new activity will be started, must be valid
    ssidThe Wi-Fi network name (SSID), the network must be using WPA2, must be valid
    passwordThe Wi-Fi network password, must be valid
    Returns
    True, if the intent was sent successfully, otherwise false

◆ startActivity()

static bool Ocean::Platform::Android::Utilities::startActivity ( JNIEnv *  env,
jobject  rootActivity,
const std::string &  activityClassName 
)
static

Starts an new activity from a root activity.

Parameters
envThe Java environment, must be valid
rootActivityThe root activity from which the new activity will be started, must be valid
activityClassNameThe name of the class of the activity to start, must be valid
Returns
True, if succeeded

◆ systemPropertyValue()

static bool Ocean::Platform::Android::Utilities::systemPropertyValue ( const std::string &  name,
std::string &  value 
)
static

Returns the value of a specific system property.

Parameters
nameThe name of the system property, must be valid
valueThe resulting value
Returns
True, if succeeded

◆ toAString()

static std::string Ocean::Platform::Android::Utilities::toAString ( JNIEnv *  env,
jstring  javaString 
)
static

Converts a Java native string to a std string.

Parameters
envThe Java environment, must be valid
javaStringJava string to be converted, may be nullptr
Returns
Resulting std string

◆ toJavaString()

static jstring Ocean::Platform::Android::Utilities::toJavaString ( JNIEnv *  env,
const std::string &  stdString 
)
static

Converts a std string to a Java native string.

Parameters
envThe Java environment, must be valid
stdStringStd string to be converted
Returns
Resulting Java native string

◆ toJavaStringArray()

static jobjectArray Ocean::Platform::Android::Utilities::toJavaStringArray ( JNIEnv *  env,
const std::vector< std::string > &  strings 
)
static

Converts a vector of std strings to a Java array with native strings.

Parameters
envThe Java environment, must be valid
stringsStd strings to be converted, can be empty
Returns
Resulting Java native string

◆ toVector() [1/2]

static bool Ocean::Platform::Android::Utilities::toVector ( JNIEnv *  env,
jobject  javaIntegerList,
std::vector< int > &  values 
)
static

Converts a Java native list with integer to a vector of integers.

Parameters
envThe Java environment, must be valid
javaIntegerListJava list with integers to be converted, must be valid
valuesThe resulting vector of integers
Returns
True, if succeeded

◆ toVector() [2/2]

static bool Ocean::Platform::Android::Utilities::toVector ( JNIEnv *  env,
jobject  javaStringList,
std::vector< std::string > &  strings 
)
static

Converts a Java native list with string to a vector of strings.

Parameters
envThe Java environment, must be valid
javaStringListJava list with strings to be converted, must be valid
stringsThe resulting vector of strings
Returns
True, if succeeded

◆ triggerVibration()

static bool Ocean::Platform::Android::Utilities::triggerVibration ( JNIEnv *  env,
jobject  activity,
unsigned int  intensity = 1u,
const unsigned int  duration = 50u 
)
static

Triggers a vibration.

This function needs permission 'android.permission.VIBRATE'.

Parameters
envThe Java environment, must be valid
activityThe root activity from which the new activity will be started, must be valid
intensityThe intensity of the vibration, the higher the stronger, with range [0, 2]
durationThe duration of the vibration in milliseconds, with range [1, infinity)
Returns
True, if succeeded

The documentation for this class was generated from the following file: