mirror of
https://github.com/godotengine/godot.git
synced 2025-01-18 20:40:57 +08:00
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".
This commit is contained in:
parent
9269d6be32
commit
fafda80a4b
@ -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);
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
@ -161,6 +161,24 @@ public final class PermissionsUtil {
|
||||
return dangerousPermissions.toArray(new String[0]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
|
Loading…
Reference in New Issue
Block a user