Improved android networking

This commit is contained in:
Yousef Khadadeh 2026-05-14 11:53:36 +03:00
parent a533494b3a
commit ad1844adc7
27 changed files with 568 additions and 69 deletions

View file

@ -58,6 +58,9 @@
<!-- if you want to connect to the network or internet, uncomment this. -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE"/>
<!-- Create a Java class extending SDLActivity and place it in a
directory under app/src/main/java matching the package, e.g. app/src/main/java/com/gamemaker/game/MyGame.java
@ -76,6 +79,13 @@
<!-- Example of setting SDL hints from AndroidManifest.xml:
<meta-data android:name="SDL_ENV.SDL_ANDROID_TRAP_BACK_BUTTON" android:value="0"/>
-->
<service
android:name=".InfiniPaintNetworkService"
android:foregroundServiceType="specialUse"
android:exported="false">
<property android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE"
android:value="When connected to a server/hosting a server, keep the connection alive even when the application is in the background."/>
</service>
<activity android:name="com.erroratline0.infinipaint.InfiniPaint"
android:label="@string/app_name"

View file

@ -19,6 +19,7 @@
package com.erroratline0.infinipaint;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
@ -142,4 +143,10 @@ public class InfiniPaint extends SDLActivity {
static public void updateTextboxAndCursorPos(String str, int selectionBegin, int selectionEnd) {
mSingleton.commandHandler.post(new InfiniPaintUpdateTextboxAndCursorPosTask(str, selectionBegin, selectionEnd));
}
static public void startNetworkService() {
mSingleton.startForegroundService(new Intent(mSingleton, InfiniPaintNetworkService.class));
}
static public void stopNetworkService() {
mSingleton.stopService(new Intent(mSingleton, InfiniPaintNetworkService.class));
}
}

View file

@ -0,0 +1,58 @@
package com.erroratline0.infinipaint;
import android.app.ForegroundServiceStartNotAllowedException;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.content.pm.ServiceInfo;
import android.os.Build;
import android.os.IBinder;
import android.view.KeyEvent;
public class InfiniPaintNetworkService extends Service {
public static native void networkServiceStarted();
public static native void networkServiceDestroyed();
private static final String CHANNEL_ID = "NetworkServiceChannel";
@Override
public void onCreate() {
createNotificationChannel();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Notification notification = new Notification.Builder(this, CHANNEL_ID)
.setContentTitle("Online canvas")
.setContentText("Currently online")
.build();
startForeground(1, notification);
networkServiceStarted();
// For now, don't try restarting the connection after we've been disconnected
return START_NOT_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
// We don't provide binding, so return null
return null;
}
@Override
public void onDestroy() {
networkServiceDestroyed();
}
private void createNotificationChannel() {
NotificationChannel serviceChannel = new NotificationChannel(
CHANNEL_ID,
"Network Service Channel",
NotificationManager.IMPORTANCE_DEFAULT
);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(serviceChannel);
}
}