foreground service optional
This commit is contained in:
parent
9a2f7a4735
commit
ec86a581d5
@ -1,12 +1,3 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_CONNECTED_DEVICE" />
|
||||
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
|
||||
<application>
|
||||
<service
|
||||
android:name="software.eskimo.capacitor.sockets.SocketForegroundService"
|
||||
android:enabled="true"
|
||||
android:exported="false"
|
||||
android:foregroundServiceType="connectedDevice" />
|
||||
</application>
|
||||
<application></application>
|
||||
</manifest>
|
@ -9,12 +9,17 @@ import com.getcapacitor.Plugin;
|
||||
import com.getcapacitor.PluginCall;
|
||||
import com.getcapacitor.annotation.CapacitorPlugin;
|
||||
import com.getcapacitor.PluginMethod;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@CapacitorPlugin(name = "Sockets")
|
||||
public class SocketsPlugin extends Plugin {
|
||||
|
||||
private Sockets implementation = new Sockets(this);
|
||||
|
||||
private Map<String, Boolean> socketForegroundUsage = new HashMap<>();
|
||||
private int foregroundServiceConnectionCount = 0;
|
||||
|
||||
@PluginMethod
|
||||
public void create(PluginCall call) {
|
||||
String id = call.getString("id", java.util.UUID.randomUUID().toString());
|
||||
@ -30,8 +35,15 @@ public class SocketsPlugin extends Plugin {
|
||||
boolean useTLS = call.getBoolean("useTLS", false);
|
||||
boolean acceptInvalidCertificates = call.getBoolean("acceptInvalidCertificates", false);
|
||||
String delimiter = call.getString("delimiter", "\r\n");
|
||||
|
||||
startSocketForegroundService();
|
||||
|
||||
boolean useForeground = call.getBoolean("useForegroundService", false);
|
||||
if (useForeground) {
|
||||
if (foregroundServiceConnectionCount == 0) {
|
||||
startSocketForegroundService();
|
||||
}
|
||||
foregroundServiceConnectionCount++;
|
||||
}
|
||||
socketForegroundUsage.put(id, useForeground);
|
||||
|
||||
implementation.connect(id, host, port, useTLS, acceptInvalidCertificates, delimiter);
|
||||
call.resolve();
|
||||
@ -49,9 +61,12 @@ public class SocketsPlugin extends Plugin {
|
||||
@PluginMethod
|
||||
public void disconnect(PluginCall call) {
|
||||
String id = call.getString("id", "");
|
||||
|
||||
Boolean usedForeground = socketForegroundUsage.remove(id);
|
||||
if (usedForeground != null && usedForeground) {
|
||||
foregroundServiceConnectionCount--;
|
||||
}
|
||||
implementation.disconnect(id);
|
||||
if (implementation.getActiveSocketCount() == 0) {
|
||||
if (foregroundServiceConnectionCount <= 0) {
|
||||
stopSocketForegroundService();
|
||||
}
|
||||
call.resolve();
|
||||
@ -85,9 +100,7 @@ public class SocketsPlugin extends Plugin {
|
||||
JSObject data = new JSObject();
|
||||
data.put("id", id);
|
||||
data.put("message", message);
|
||||
|
||||
Log.d("SocketsPlugin", "Message to JS: " + data.toString());
|
||||
|
||||
notifyListeners("message", data);
|
||||
}
|
||||
|
||||
@ -113,4 +126,4 @@ public class SocketsPlugin extends Plugin {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "capacitor-sockets",
|
||||
"version": "0.0.4",
|
||||
"version": "0.0.5",
|
||||
"description": "Sockets",
|
||||
"main": "dist/plugin.cjs.js",
|
||||
"module": "dist/esm/index.js",
|
||||
|
Loading…
Reference in New Issue
Block a user