Merge pull request #8596 from Robyt3/Android-Restart-App

Implement client restarting on Android
This commit is contained in:
Dennis Felsing 2024-07-16 13:43:51 +00:00 committed by GitHub
commit 3c43dcd636
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 46 additions and 0 deletions

View file

@ -3,10 +3,13 @@ package org.ddnet.client;
import android.app.NativeActivity; import android.app.NativeActivity;
import org.libsdl.app.SDLActivity; import org.libsdl.app.SDLActivity;
import android.os.Bundle; import android.os.Bundle;
import android.content.Intent;
import android.content.pm.ActivityInfo; import android.content.pm.ActivityInfo;
public class NativeMain extends SDLActivity { public class NativeMain extends SDLActivity {
private static final int COMMAND_RESTART_APP = SDLActivity.COMMAND_USER + 1;
@Override @Override
protected String[] getLibraries() { protected String[] getLibraries() {
return new String[] { return new String[] {
@ -19,4 +22,24 @@ public class NativeMain extends SDLActivity {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
super.onCreate(SavedInstanceState); super.onCreate(SavedInstanceState);
} }
@Override
protected boolean onUnhandledMessage(int command, Object param) {
if(command == COMMAND_RESTART_APP) {
restartApp();
return true;
}
return false;
}
private void restartApp() {
Intent restartIntent =
Intent.makeRestartActivityTask(
getPackageManager().getLaunchIntentForPackage(
getPackageName()
).getComponent()
);
restartIntent.setPackage(getPackageName());
startActivity(restartIntent);
}
} }

View file

@ -225,3 +225,12 @@ const char *InitAndroid()
return nullptr; return nullptr;
} }
// See NativeMain.java
constexpr uint32_t COMMAND_USER = 0x8000;
constexpr uint32_t COMMAND_RESTART_APP = COMMAND_USER + 1;
void RestartAndroidApp()
{
SDL_AndroidSendMessage(COMMAND_RESTART_APP, 0);
}

View file

@ -20,4 +20,12 @@
*/ */
const char *InitAndroid(); const char *InitAndroid();
/**
* Sends an intent to the Android system to restart the app.
*
* This will restart the main activity in a new task. The current process
* must immediately terminate after this function is called.
*/
void RestartAndroidApp();
#endif // ANDROID_ANDROID_MAIN_H #endif // ANDROID_ANDROID_MAIN_H

View file

@ -4594,11 +4594,13 @@ int main(int argc, const char **argv)
pClient->Run(); pClient->Run();
const bool Restarting = pClient->State() == CClient::STATE_RESTARTING; const bool Restarting = pClient->State() == CClient::STATE_RESTARTING;
#if !defined(CONF_PLATFORM_ANDROID)
char aRestartBinaryPath[IO_MAX_PATH_LENGTH]; char aRestartBinaryPath[IO_MAX_PATH_LENGTH];
if(Restarting) if(Restarting)
{ {
pStorage->GetBinaryPath(PLAT_CLIENT_EXEC, aRestartBinaryPath, sizeof(aRestartBinaryPath)); pStorage->GetBinaryPath(PLAT_CLIENT_EXEC, aRestartBinaryPath, sizeof(aRestartBinaryPath));
} }
#endif
std::vector<SWarning> vQuittingWarnings = pClient->QuittingWarnings(); std::vector<SWarning> vQuittingWarnings = pClient->QuittingWarnings();
@ -4611,7 +4613,11 @@ int main(int argc, const char **argv)
if(Restarting) if(Restarting)
{ {
#if defined(CONF_PLATFORM_ANDROID)
RestartAndroidApp();
#else
shell_execute(aRestartBinaryPath, EShellExecuteWindowState::FOREGROUND); shell_execute(aRestartBinaryPath, EShellExecuteWindowState::FOREGROUND);
#endif
} }
PerformFinalCleanup(); PerformFinalCleanup();