summaryrefslogtreecommitdiff
path: root/platform/android/java/lib
diff options
context:
space:
mode:
authorFabio Alessandrelli <fabio.alessandrelli@gmail.com>2019-11-25 01:35:55 +0100
committerFabio Alessandrelli <fabio.alessandrelli@gmail.com>2019-12-14 18:10:36 +0100
commitfafda80a4b7c3c588ae0e0937f2d7982e39b2d74 (patch)
tree98ff97a8af3bfc80a7dbc0c019d15aef85db3809 /platform/android/java/lib
parent9269d6be328e5ca0be3ce9bdb45017e97c799890 (diff)
Add GodotNetUtils Java class for Android.
Provides access to a MulticastLock. As specified by the Android API, broadcast/multicast packets may be filtered on some phones unless the application explicitly acquires a "MulticastLock".
Diffstat (limited to 'platform/android/java/lib')
-rw-r--r--platform/android/java/lib/src/org/godotengine/godot/Godot.java3
-rw-r--r--platform/android/java/lib/src/org/godotengine/godot/utils/GodotNetUtils.java84
-rw-r--r--platform/android/java/lib/src/org/godotengine/godot/utils/PermissionsUtil.java18
3 files changed, 105 insertions, 0 deletions
diff --git a/platform/android/java/lib/src/org/godotengine/godot/Godot.java b/platform/android/java/lib/src/org/godotengine/godot/Godot.java
index 4dae2dcc53..d951998632 100644
--- a/platform/android/java/lib/src/org/godotengine/godot/Godot.java
+++ b/platform/android/java/lib/src/org/godotengine/godot/Godot.java
@@ -96,6 +96,7 @@ import java.util.Locale;
import javax.microedition.khronos.opengles.GL10;
import org.godotengine.godot.input.GodotEditText;
import org.godotengine.godot.payments.PaymentsManager;
+import org.godotengine.godot.utils.GodotNetUtils;
import org.godotengine.godot.utils.PermissionsUtil;
import org.godotengine.godot.xr.XRMode;
@@ -243,6 +244,7 @@ public abstract class Godot extends Activity implements SensorEventListener, IDo
private Sensor mGyroscope;
public static GodotIO io;
+ public static GodotNetUtils netUtils;
static SingletonBase[] singletons = new SingletonBase[MAX_SINGLETONS];
static int singleton_count = 0;
@@ -502,6 +504,7 @@ public abstract class Godot extends Activity implements SensorEventListener, IDo
io = new GodotIO(this);
io.unique_id = Secure.getString(getContentResolver(), Secure.ANDROID_ID);
GodotLib.io = io;
+ netUtils = new GodotNetUtils(this);
mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_GAME);
diff --git a/platform/android/java/lib/src/org/godotengine/godot/utils/GodotNetUtils.java b/platform/android/java/lib/src/org/godotengine/godot/utils/GodotNetUtils.java
new file mode 100644
index 0000000000..0dc6962ca6
--- /dev/null
+++ b/platform/android/java/lib/src/org/godotengine/godot/utils/GodotNetUtils.java
@@ -0,0 +1,84 @@
+/*************************************************************************/
+/* GodotNetUtils.java */
+/*************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/*************************************************************************/
+/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
+/* */
+/* Permission is hereby granted, free of charge, to any person obtaining */
+/* a copy of this software and associated documentation files (the */
+/* "Software"), to deal in the Software without restriction, including */
+/* without limitation the rights to use, copy, modify, merge, publish, */
+/* distribute, sublicense, and/or sell copies of the Software, and to */
+/* permit persons to whom the Software is furnished to do so, subject to */
+/* the following conditions: */
+/* */
+/* The above copyright notice and this permission notice shall be */
+/* included in all copies or substantial portions of the Software. */
+/* */
+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
+/*************************************************************************/
+
+package org.godotengine.godot.utils;
+
+import android.content.Context;
+import android.net.wifi.WifiManager;
+import android.util.Log;
+
+import org.godotengine.godot.Godot;
+
+/**
+ * This class handles Android-specific networking functions.
+ * For now, it only provides access to WifiManager.MulticastLock, which is needed on some devices
+ * to receive broadcast and multicast packets.
+ */
+public class GodotNetUtils {
+
+ /* A single, reference counted, multicast lock, or null if permission CHANGE_WIFI_MULTICAST_STATE is missing */
+ private WifiManager.MulticastLock multicastLock;
+
+ public GodotNetUtils(Godot p_activity) {
+ if (PermissionsUtil.hasManifestPermission(p_activity, "android.permission.CHANGE_WIFI_MULTICAST_STATE")) {
+ WifiManager wifi = (WifiManager)p_activity.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
+ multicastLock = wifi.createMulticastLock("GodotMulticastLock");
+ multicastLock.setReferenceCounted(true);
+ }
+ }
+
+ /**
+ * Acquire the multicast lock. This is required on some devices to receive broadcast/multicast packets.
+ * This is done automatically by Godot when enabling broadcast or joining a multicast group on a socket.
+ */
+ public void multicastLockAcquire() {
+ if (multicastLock == null)
+ return;
+ try {
+ multicastLock.acquire();
+ } catch (RuntimeException e) {
+ Log.e("Godot", "Exception during multicast lock acquire: " + e);
+ }
+ }
+
+ /**
+ * Release the multicast lock.
+ * This is done automatically by Godot when the lock is no longer needed by a socket.
+ */
+ public void multicastLockRelease() {
+ if (multicastLock == null)
+ return;
+ try {
+ multicastLock.release();
+ } catch (RuntimeException e) {
+ Log.e("Godot", "Exception during multicast lock release: " + e);
+ }
+ }
+}
diff --git a/platform/android/java/lib/src/org/godotengine/godot/utils/PermissionsUtil.java b/platform/android/java/lib/src/org/godotengine/godot/utils/PermissionsUtil.java
index 21df5a91b0..7e35417c6a 100644
--- a/platform/android/java/lib/src/org/godotengine/godot/utils/PermissionsUtil.java
+++ b/platform/android/java/lib/src/org/godotengine/godot/utils/PermissionsUtil.java
@@ -162,6 +162,24 @@ public final class PermissionsUtil {
}
/**
+ * Check if the given permission is in the AndroidManifest.xml file.
+ * @param activity the caller activity for this method.
+ * @param permission the permession to look for in the manifest file.
+ * @return "true" if the permission is in the manifest file of the activity, "false" otherwise.
+ */
+ public static boolean hasManifestPermission(Godot activity, String permission) {
+ try {
+ for (String p : getManifestPermissions(activity)) {
+ if (permission.equals(p))
+ return true;
+ }
+ } catch (PackageManager.NameNotFoundException e) {
+ }
+
+ return false;
+ }
+
+ /**
* Returns the permissions defined in the AndroidManifest.xml file.
* @param activity the caller activity for this method.
* @return manifest permissions list