forked from ddnet/ddnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Support starting local server on Android
Compile server as separate library. Use foreground service to run native main function of server using JNI, avoiding dependency on SDL. Server service must run in a different process because it needs to be terminated to restart the server. Add detailed description why the service uses `android:foregroundServiceType="specialUse"` as this must convince the reviewer that the use case is valid. None of the other foreground service types cover hosting local game servers or anything related. Support stopping server via notification action.
- Loading branch information
Showing
16 changed files
with
649 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
scripts/android/files/java/org/ddnet/client/ClientActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package org.ddnet.client; | ||
|
||
import android.app.NativeActivity; | ||
import android.content.*; | ||
import android.content.pm.ActivityInfo; | ||
import android.os.*; | ||
|
||
import androidx.core.content.ContextCompat; | ||
|
||
import org.libsdl.app.SDLActivity; | ||
|
||
public class ClientActivity extends SDLActivity { | ||
|
||
private static final int COMMAND_RESTART_APP = SDLActivity.COMMAND_USER + 1; | ||
|
||
private String[] launchArguments = new String[0]; | ||
|
||
private final Object serverServiceMonitor = new Object(); | ||
private Messenger serverServiceMessenger = null; | ||
private final ServiceConnection serverServiceConnection = new ServiceConnection() { | ||
@Override | ||
public void onServiceConnected(ComponentName name, IBinder service) { | ||
synchronized(serverServiceMonitor) { | ||
serverServiceMessenger = new Messenger(service); | ||
} | ||
} | ||
|
||
@Override | ||
public void onServiceDisconnected(ComponentName name) { | ||
synchronized(serverServiceMonitor) { | ||
serverServiceMessenger = null; | ||
} | ||
} | ||
}; | ||
|
||
@Override | ||
protected String[] getLibraries() { | ||
return new String[] { | ||
"DDNet", | ||
}; | ||
} | ||
|
||
@Override | ||
public void onCreate(Bundle savedInstanceState) { | ||
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); | ||
|
||
Intent intent = getIntent(); | ||
if(intent != null) { | ||
String gfxBackend = intent.getStringExtra("gfx-backend"); | ||
if(gfxBackend != null) { | ||
if(gfxBackend.equals("Vulkan")) { | ||
launchArguments = new String[] {"gfx_backend Vulkan"}; | ||
} else if(gfxBackend.equals("GLES")) { | ||
launchArguments = new String[] {"gfx_backend GLES"}; | ||
} | ||
} | ||
} | ||
|
||
super.onCreate(savedInstanceState); | ||
} | ||
|
||
@Override | ||
protected void onDestroy() { | ||
super.onDestroy(); | ||
synchronized(serverServiceMonitor) { | ||
if(serverServiceMessenger != null) { | ||
unbindService(serverServiceConnection); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
protected String[] getArguments() { | ||
return launchArguments; | ||
} | ||
|
||
@Override | ||
protected boolean onUnhandledMessage(int command, Object param) { | ||
switch(command) { | ||
case COMMAND_RESTART_APP: | ||
restartApp(); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
private void restartApp() { | ||
Intent restartIntent = | ||
Intent.makeRestartActivityTask( | ||
getPackageManager().getLaunchIntentForPackage( | ||
getPackageName() | ||
).getComponent() | ||
); | ||
restartIntent.setPackage(getPackageName()); | ||
startActivity(restartIntent); | ||
} | ||
|
||
// Called from native code, see android_main.cpp | ||
public void startServer() { | ||
synchronized(serverServiceMonitor) { | ||
Intent startIntent = new Intent(this, ServerService.class); | ||
ContextCompat.startForegroundService(this, startIntent); | ||
bindService(startIntent, serverServiceConnection, 0); | ||
} | ||
} | ||
|
||
// Called from native code, see android_main.cpp | ||
public void stopServer() { | ||
synchronized(serverServiceMonitor) { | ||
if(serverServiceMessenger != null) { | ||
try { | ||
serverServiceMessenger.send(Message.obtain(null, ServerService.MESSAGE_STOP, 0, 0)); | ||
} catch (RemoteException e) { | ||
// Server already stopped or connection otherwise broken | ||
unbindService(serverServiceConnection); | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Called from native code, see android_main.cpp | ||
public boolean isServerRunning() { | ||
synchronized(serverServiceMonitor) { | ||
return serverServiceMessenger != null; | ||
} | ||
} | ||
} |
65 changes: 0 additions & 65 deletions
65
scripts/android/files/java/org/ddnet/client/NativeMain.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.