Skip to content

Commit

Permalink
Merge pull request ddnet#8596 from Robyt3/Android-Restart-App
Browse files Browse the repository at this point in the history
Implement client restarting on Android
  • Loading branch information
def- authored Jul 16, 2024
2 parents 804e87a + d4f47c2 commit 3c43dcd
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
23 changes: 23 additions & 0 deletions scripts/android/files/java/org/ddnet/client/NativeMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
import android.app.NativeActivity;
import org.libsdl.app.SDLActivity;
import android.os.Bundle;
import android.content.Intent;
import android.content.pm.ActivityInfo;

public class NativeMain extends SDLActivity {

private static final int COMMAND_RESTART_APP = SDLActivity.COMMAND_USER + 1;

@Override
protected String[] getLibraries() {
return new String[] {
Expand All @@ -19,4 +22,24 @@ public void onCreate(Bundle SavedInstanceState) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
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);
}
}
9 changes: 9 additions & 0 deletions src/android/android_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,12 @@ const char *InitAndroid()

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);
}
8 changes: 8 additions & 0 deletions src/android/android_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,12 @@
*/
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
6 changes: 6 additions & 0 deletions src/engine/client/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4594,11 +4594,13 @@ int main(int argc, const char **argv)
pClient->Run();

const bool Restarting = pClient->State() == CClient::STATE_RESTARTING;
#if !defined(CONF_PLATFORM_ANDROID)
char aRestartBinaryPath[IO_MAX_PATH_LENGTH];
if(Restarting)
{
pStorage->GetBinaryPath(PLAT_CLIENT_EXEC, aRestartBinaryPath, sizeof(aRestartBinaryPath));
}
#endif

std::vector<SWarning> vQuittingWarnings = pClient->QuittingWarnings();

Expand All @@ -4611,7 +4613,11 @@ int main(int argc, const char **argv)

if(Restarting)
{
#if defined(CONF_PLATFORM_ANDROID)
RestartAndroidApp();
#else
shell_execute(aRestartBinaryPath, EShellExecuteWindowState::FOREGROUND);
#endif
}

PerformFinalCleanup();
Expand Down

0 comments on commit 3c43dcd

Please sign in to comment.