Ocean
Loading...
Searching...
No Matches
platform/android/Utilities.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#ifndef META_OCEAN_PLATFORM_ANDROID_UTILITIES_H
9#define META_OCEAN_PLATFORM_ANDROID_UTILITIES_H
10
12
13namespace Ocean
14{
15
16namespace Platform
17{
18
19namespace Android
20{
21
22/**
23 * This class implements helper functions for android platforms.
24 * @ingroup platformandroid
25 */
26class OCEAN_PLATFORM_ANDROID_EXPORT Utilities
27{
28 public:
29
30 /**
31 * Converts a Java native string to a std string.
32 * @param env The Java environment, must be valid
33 * @param javaString Java string to be converted, may be nullptr
34 * @return Resulting std string
35 */
36 static std::string toAString(JNIEnv* env, jstring javaString);
37
38 /**
39 * Converts a std string to a Java native string.
40 * @param env The Java environment, must be valid
41 * @param stdString Std string to be converted
42 * @return Resulting Java native string
43 */
44 static jstring toJavaString(JNIEnv* env, const std::string& stdString);
45
46 /**
47 * Converts a vector of std strings to a Java array with native strings.
48 * @param env The Java environment, must be valid
49 * @param strings Std strings to be converted, can be empty
50 * @return Resulting Java native string
51 */
52 static jobjectArray toJavaStringArray(JNIEnv* env, const std::vector<std::string>& strings);
53
54 /**
55 * Converts a Java native list with string to a vector of strings.
56 * @param env The Java environment, must be valid
57 * @param javaStringList Java list with strings to be converted, must be valid
58 * @param strings The resulting vector of strings
59 * @return True, if succeeded
60 */
61 static bool toVector(JNIEnv* env, jobject javaStringList, std::vector<std::string>& strings);
62
63 /**
64 * Converts a Java native list with integer to a vector of integers.
65 * @param env The Java environment, must be valid
66 * @param javaIntegerList Java list with integers to be converted, must be valid
67 * @param values The resulting vector of integers
68 * @return True, if succeeded
69 */
70 static bool toVector(JNIEnv* env, jobject javaIntegerList, std::vector<int>& values);
71
72 /**
73 * Returns the class name of an object.
74 * @param env The Java environment, must be valid
75 * @param object The object for which the class name will be returned, must be valid
76 * @param name The resulting class name
77 * @return True, if succeeded
78 */
79 static bool className(JNIEnv* env, jobject object, std::string& name);
80
81 /**
82 * Returns the version code stored in the application manifest.
83 * @param javaVM The Java virtual machine, must be valid
84 * @param activity The Android main activity, must be valid
85 * @param versionCode The resulting version code of the application
86 * @param versionName The resulting version name of the application
87 * @return True, if succeeded
88 */
89 static bool manifestVersion(JavaVM* javaVM, jobject activity, int& versionCode, std::string& versionName);
90
91 /**
92 * Deprecated.
93 *
94 * Returns the version code stored in the application manifest.
95 * @param javaVM The Java virtual machine, must be valid
96 * @param activity The Android main activity, must be valid
97 * @param versionCode The version code of the application
98 * @return True, if succeeded
99 */
100 static bool manifestVersionCode(JavaVM* javaVM, jobject activity, int& versionCode);
101
102 /**
103 * Returns the minimum and target SDK versions stored in the application manifest.
104 * @param env The Java environment, must be valid
105 * @param activity The Android main activity, must be valid
106 * @param minSdkVersion The resulting minimum SDK version of the application as defined in its manifest
107 * @param targetSdkVersion The resulting target SDK version of the application as defined in its manifest
108 * @return True, if succeeded
109 */
110 static bool manifestSdkVersions(JNIEnv* env, jobject activity, unsigned int& minSdkVersion, unsigned int& targetSdkVersion);
111
112 /**
113 * Return the Android SDK version of the system.
114 * @param env The Java environment, must be valid
115 * @param version The resulting Android SDK version, only valid if this function returns 'true'
116 * @return True, if succeeded, otherwise false
117 */
118 static bool androidSdkVersion(JNIEnv* env, unsigned int& version);
119
120 /**
121 * Returns the value of a specific system property.
122 * @param name The name of the system property, must be valid
123 * @param value The resulting value
124 * @return True, if succeeded
125 */
126 static bool systemPropertyValue(const std::string& name, std::string& value);
127
128 /**
129 * Returns the brand string of the device.
130 * @param env The Java environment, must be valid
131 * @param brand The resulting model string
132 * @return True, if succeeded
133 */
134 static bool deviceBrand(JNIEnv* env, std::string& brand);
135
136 /**
137 * Returns the model string of the device.
138 * @param env The Java environment, must be valid
139 * @param model The resulting model string
140 * @return True, if succeeded
141 */
142 static bool deviceModel(JNIEnv* env, std::string& model);
143
144 /**
145 * Returns the name of the package.
146 * @param packageName The resulting package name
147 * @return True, if succeeded
148 */
149 static bool determinePackageName(std::string& packageName);
150
151 /**
152 * Sends an explicit intent to another component or application.
153 * @param env The Java environment, must be valid
154 * @param activity The Android main activity, must be valid
155 * @param packageName The name of the package to which the intent will be sent, must be valid
156 * @param className The name of the class or activity to which the intent will be sent, must be valid
157 * @param extraText The data that will be sent with the intent, must be valid
158 * @return True, if the intent was sent successfully, otherwise false
159 */
160 static bool sendIntentToComponent(JNIEnv* env, jobject activity, const std::string& packageName, const std::string& className, const std::string& extraText);
161
162 /**
163 * Starts an new activity from a root activity.
164 * @param env The Java environment, must be valid
165 * @param rootActivity The root activity from which the new activity will be started, must be valid
166 * @param activityClassName The name of the class of the activity to start, must be valid
167 * @return True, if succeeded
168 */
169 static bool startActivity(JNIEnv* env, jobject rootActivity, const std::string& activityClassName);
170
171 /**
172 * Joins a Wi-Fi network (WPA2-PSK).
173 * This only works with Android SDK version 28 or below.
174 * @param env The Java environment, must be valid
175 * @param activity The root activity from which the new activity will be started, must be valid
176 * @param ssid The Wi-Fi network name (SSID), the network must be using WPA2, must be valid
177 * @param password The Wi-Fi network password, must be valid
178 * @return True, if joined successfully, otherwise false
179 **/
180 static bool connectToWifi(JNIEnv* env, jobject activity, const std::string& ssid, const std::string& password);
181
182 /**
183 * Sends an intent to the OS to request it to join a Wi-Fi network (WPA2-PSK).
184 * 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.
185 * Additional notes:
186 * * The Wi-Fi network must use the security protocol WPA2-PSK.
187 * * This function requires Android SDK version 29 or higher.
188 * * This function requires the permissions `android.permission.CHANGE_NETWORK_STATE` and `android.permission.WRITE_SETTINGS` to be set in the app manifest to work.
189 * * Rejecting a new network does NOT revoke the permission `android.permission.CHANGE_NETWORK_STATE` again. Re-tries are possible.
190 * * The newly added networks do NOT have to reachable at the time of adding them (they will be stored for when they become available).
191 * * 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`).
192 * @param env The Java environment, must be valid
193 * @param activity The root activity from which the new activity will be started, must be valid
194 * @param ssid The Wi-Fi network name (SSID), the network must be using WPA2, must be valid
195 * @param password The Wi-Fi network password, must be valid
196 * @return True, if the intent was sent successfully, otherwise false
197 **/
198 static bool sendIntentToConnectToWifi(JNIEnv* env, jobject activity, const std::string& ssid, const std::string& password);
199
200 /**
201 * Returns current Wi-Fi network name (SSID).
202 * @param env The Java environment, must be valid
203 * @param activity The root activity from which the new activity will be started, must be valid
204 * @param ssid The resulting Wi-Fi network name (SSID), will be empty if there is no Wi-Fi connection
205 * @return True, if succeeded
206 */
207 static bool currentWifiSsid(JNIEnv* env, jobject activity, std::string& ssid);
208
209 /**
210 * Triggers a vibration.
211 * This function needs permission 'android.permission.VIBRATE'.
212 * @param env The Java environment, must be valid
213 * @param activity The root activity from which the new activity will be started, must be valid
214 * @param intensity The intensity of the vibration, the higher the stronger, with range [0, 2]
215 * @param duration The duration of the vibration in milliseconds, with range [1, infinity)
216 * @return True, if succeeded
217 */
218 static bool triggerVibration(JNIEnv* env, jobject activity, unsigned int intensity = 1u, const unsigned int duration = 50u);
219};
220
221}
222
223}
224
225}
226
227#endif // META_OCEAN_PLATFORM_ANDROID_UTILITIES_H
This class implements helper functions for android platforms.
Definition platform/android/Utilities.h:27
static bool deviceModel(JNIEnv *env, std::string &model)
Returns the model string of the device.
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 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 manifestVersionCode(JavaVM *javaVM, jobject activity, int &versionCode)
Deprecated.
static bool determinePackageName(std::string &packageName)
Returns the name of the package.
static bool deviceBrand(JNIEnv *env, std::string &brand)
Returns the brand string of the device.
static jobjectArray toJavaStringArray(JNIEnv *env, const std::vector< std::string > &strings)
Converts a vector of std strings to a Java array with native strings.
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 manifestVersion(JavaVM *javaVM, jobject activity, int &versionCode, std::string &versionName)
Returns the version code stored in the application manifest.
static bool androidSdkVersion(JNIEnv *env, unsigned int &version)
Return the Android SDK version of the system.
static bool startActivity(JNIEnv *env, jobject rootActivity, const std::string &activityClassName)
Starts an new activity from a root activity.
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 connectToWifi(JNIEnv *env, jobject activity, const std::string &ssid, const std::string &password)
Joins a Wi-Fi network (WPA2-PSK).
static bool toVector(JNIEnv *env, jobject javaStringList, std::vector< std::string > &strings)
Converts a Java native list with string to a vector of strings.
static bool currentWifiSsid(JNIEnv *env, jobject activity, std::string &ssid)
Returns current Wi-Fi network name (SSID).
static jstring toJavaString(JNIEnv *env, const std::string &stdString)
Converts a std string to a Java native string.
static bool triggerVibration(JNIEnv *env, jobject activity, unsigned int intensity=1u, const unsigned int duration=50u)
Triggers a vibration.
static bool className(JNIEnv *env, jobject object, std::string &name)
Returns the class name of an object.
static bool systemPropertyValue(const std::string &name, std::string &value)
Returns the value of a specific system property.
static std::string toAString(JNIEnv *env, jstring javaString)
Converts a Java native string to a std string.
The namespace covering the entire Ocean framework.
Definition Accessor.h:15