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">
 | 
					<manifest xmlns:android="http://schemas.android.com/apk/res/android">
 | 
				
			||||||
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
 | 
					    <application></application>
 | 
				
			||||||
    <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>
 | 
					 | 
				
			||||||
</manifest>
 | 
					</manifest>
 | 
				
			||||||
@ -9,12 +9,17 @@ import com.getcapacitor.Plugin;
 | 
				
			|||||||
import com.getcapacitor.PluginCall;
 | 
					import com.getcapacitor.PluginCall;
 | 
				
			||||||
import com.getcapacitor.annotation.CapacitorPlugin;
 | 
					import com.getcapacitor.annotation.CapacitorPlugin;
 | 
				
			||||||
import com.getcapacitor.PluginMethod;
 | 
					import com.getcapacitor.PluginMethod;
 | 
				
			||||||
 | 
					import java.util.HashMap;
 | 
				
			||||||
 | 
					import java.util.Map;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@CapacitorPlugin(name = "Sockets")
 | 
					@CapacitorPlugin(name = "Sockets")
 | 
				
			||||||
public class SocketsPlugin extends Plugin {
 | 
					public class SocketsPlugin extends Plugin {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private Sockets implementation = new Sockets(this);
 | 
					    private Sockets implementation = new Sockets(this);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private Map<String, Boolean> socketForegroundUsage = new HashMap<>();
 | 
				
			||||||
 | 
					    private int foregroundServiceConnectionCount = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @PluginMethod
 | 
					    @PluginMethod
 | 
				
			||||||
    public void create(PluginCall call) {
 | 
					    public void create(PluginCall call) {
 | 
				
			||||||
        String id = call.getString("id", java.util.UUID.randomUUID().toString());
 | 
					        String id = call.getString("id", java.util.UUID.randomUUID().toString());
 | 
				
			||||||
@ -31,7 +36,14 @@ public class SocketsPlugin extends Plugin {
 | 
				
			|||||||
        boolean acceptInvalidCertificates = call.getBoolean("acceptInvalidCertificates", false);
 | 
					        boolean acceptInvalidCertificates = call.getBoolean("acceptInvalidCertificates", false);
 | 
				
			||||||
        String delimiter = call.getString("delimiter", "\r\n");
 | 
					        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);
 | 
					        implementation.connect(id, host, port, useTLS, acceptInvalidCertificates, delimiter);
 | 
				
			||||||
        call.resolve();
 | 
					        call.resolve();
 | 
				
			||||||
@ -49,9 +61,12 @@ public class SocketsPlugin extends Plugin {
 | 
				
			|||||||
    @PluginMethod
 | 
					    @PluginMethod
 | 
				
			||||||
    public void disconnect(PluginCall call) {
 | 
					    public void disconnect(PluginCall call) {
 | 
				
			||||||
        String id = call.getString("id", "");
 | 
					        String id = call.getString("id", "");
 | 
				
			||||||
 | 
					        Boolean usedForeground = socketForegroundUsage.remove(id);
 | 
				
			||||||
 | 
					        if (usedForeground != null && usedForeground) {
 | 
				
			||||||
 | 
					            foregroundServiceConnectionCount--;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
        implementation.disconnect(id);
 | 
					        implementation.disconnect(id);
 | 
				
			||||||
        if (implementation.getActiveSocketCount() == 0) {
 | 
					        if (foregroundServiceConnectionCount <= 0) {
 | 
				
			||||||
            stopSocketForegroundService();
 | 
					            stopSocketForegroundService();
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        call.resolve();
 | 
					        call.resolve();
 | 
				
			||||||
@ -85,9 +100,7 @@ public class SocketsPlugin extends Plugin {
 | 
				
			|||||||
        JSObject data = new JSObject();
 | 
					        JSObject data = new JSObject();
 | 
				
			||||||
        data.put("id", id);
 | 
					        data.put("id", id);
 | 
				
			||||||
        data.put("message", message);
 | 
					        data.put("message", message);
 | 
				
			||||||
        
 | 
					 | 
				
			||||||
        Log.d("SocketsPlugin", "Message to JS: " + data.toString());
 | 
					        Log.d("SocketsPlugin", "Message to JS: " + data.toString());
 | 
				
			||||||
 | 
					 | 
				
			||||||
        notifyListeners("message", data);
 | 
					        notifyListeners("message", data);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -1,6 +1,6 @@
 | 
				
			|||||||
{
 | 
					{
 | 
				
			||||||
  "name": "capacitor-sockets",
 | 
					  "name": "capacitor-sockets",
 | 
				
			||||||
  "version": "0.0.4",
 | 
					  "version": "0.0.5",
 | 
				
			||||||
  "description": "Sockets",
 | 
					  "description": "Sockets",
 | 
				
			||||||
  "main": "dist/plugin.cjs.js",
 | 
					  "main": "dist/plugin.cjs.js",
 | 
				
			||||||
  "module": "dist/esm/index.js",
 | 
					  "module": "dist/esm/index.js",
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user