Skip to content

Commit 29bbc17

Browse files
author
Fredia Huya-Kouadio
committed
Godot Android re-architecture
Decouples the Godot java entry point from the Android Fragment component. This enables the Godot component to be more easily reused across different types of Android components including Activities and Services.
1 parent 00c782d commit 29bbc17

19 files changed

Lines changed: 1378 additions & 1154 deletions

platform/android/java/app/src/com/godot/game/GodotApp.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@
3030

3131
package com.godot.game;
3232

33-
import org.godotengine.godot.FullScreenGodotApp;
33+
import org.godotengine.godot.GodotActivity;
3434

3535
import android.os.Bundle;
3636

3737
/**
3838
* Template activity for Godot Android builds.
3939
* Feel free to extend and modify this class for your custom logic.
4040
*/
41-
public class GodotApp extends FullScreenGodotApp {
41+
public class GodotApp extends GodotActivity {
4242
@Override
4343
public void onCreate(Bundle savedInstanceState) {
4444
setTheme(R.style.GodotAppMainTheme);

platform/android/java/editor/src/main/java/org/godotengine/editor/GodotEditor.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import android.os.*
3939
import android.util.Log
4040
import android.widget.Toast
4141
import androidx.window.layout.WindowMetricsCalculator
42-
import org.godotengine.godot.FullScreenGodotApp
42+
import org.godotengine.godot.GodotActivity
4343
import org.godotengine.godot.GodotLib
4444
import org.godotengine.godot.utils.PermissionsUtil
4545
import org.godotengine.godot.utils.ProcessPhoenix
@@ -55,7 +55,7 @@ import kotlin.math.min
5555
*
5656
* It also plays the role of the primary editor window.
5757
*/
58-
open class GodotEditor : FullScreenGodotApp() {
58+
open class GodotEditor : GodotActivity() {
5959

6060
companion object {
6161
private val TAG = GodotEditor::class.java.simpleName
@@ -115,7 +115,7 @@ open class GodotEditor : FullScreenGodotApp() {
115115

116116
runOnUiThread {
117117
// Enable long press, panning and scaling gestures
118-
godotFragment?.renderView?.inputHandler?.apply {
118+
godotFragment?.godot?.renderView?.inputHandler?.apply {
119119
enableLongPress(longPressEnabled)
120120
enablePanningAndScalingGestures(panScaleEnabled)
121121
}
@@ -318,7 +318,7 @@ open class GodotEditor : FullScreenGodotApp() {
318318

319319
override fun onRequestPermissionsResult(
320320
requestCode: Int,
321-
permissions: Array<String?>,
321+
permissions: Array<String>,
322322
grantResults: IntArray
323323
) {
324324
super.onRequestPermissionsResult(requestCode, permissions, grantResults)

platform/android/java/lib/src/org/godotengine/godot/FullScreenGodotApp.java

Lines changed: 4 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -30,156 +30,10 @@
3030

3131
package org.godotengine.godot;
3232

33-
import org.godotengine.godot.utils.ProcessPhoenix;
34-
35-
import android.content.Intent;
36-
import android.os.Bundle;
37-
import android.util.Log;
38-
39-
import androidx.annotation.CallSuper;
40-
import androidx.annotation.NonNull;
41-
import androidx.annotation.Nullable;
42-
import androidx.fragment.app.Fragment;
43-
import androidx.fragment.app.FragmentActivity;
44-
4533
/**
46-
* Base activity for Android apps intending to use Godot as the primary and only screen.
34+
* Base abstract activity for Android apps intending to use Godot as the primary screen.
4735
*
48-
* It's also a reference implementation for how to setup and use the {@link Godot} fragment
49-
* within an Android app.
36+
* @deprecated Use {@link GodotActivity}
5037
*/
51-
public abstract class FullScreenGodotApp extends FragmentActivity implements GodotHost {
52-
private static final String TAG = FullScreenGodotApp.class.getSimpleName();
53-
54-
protected static final String EXTRA_FORCE_QUIT = "force_quit_requested";
55-
protected static final String EXTRA_NEW_LAUNCH = "new_launch_requested";
56-
57-
@Nullable
58-
private Godot godotFragment;
59-
60-
@Override
61-
public void onCreate(Bundle savedInstanceState) {
62-
super.onCreate(savedInstanceState);
63-
setContentView(R.layout.godot_app_layout);
64-
65-
handleStartIntent(getIntent(), true);
66-
67-
Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.godot_fragment_container);
68-
if (currentFragment instanceof Godot) {
69-
Log.v(TAG, "Reusing existing Godot fragment instance.");
70-
godotFragment = (Godot)currentFragment;
71-
} else {
72-
Log.v(TAG, "Creating new Godot fragment instance.");
73-
godotFragment = initGodotInstance();
74-
getSupportFragmentManager().beginTransaction().replace(R.id.godot_fragment_container, godotFragment).setPrimaryNavigationFragment(godotFragment).commitNowAllowingStateLoss();
75-
}
76-
}
77-
78-
@Override
79-
public void onDestroy() {
80-
Log.v(TAG, "Destroying Godot app...");
81-
super.onDestroy();
82-
terminateGodotInstance(godotFragment);
83-
}
84-
85-
@Override
86-
public final void onGodotForceQuit(Godot instance) {
87-
runOnUiThread(() -> {
88-
terminateGodotInstance(instance);
89-
});
90-
}
91-
92-
private void terminateGodotInstance(Godot instance) {
93-
if (instance == godotFragment) {
94-
Log.v(TAG, "Force quitting Godot instance");
95-
ProcessPhoenix.forceQuit(FullScreenGodotApp.this);
96-
}
97-
}
98-
99-
@Override
100-
public final void onGodotRestartRequested(Godot instance) {
101-
runOnUiThread(() -> {
102-
if (instance == godotFragment) {
103-
// It's very hard to properly de-initialize Godot on Android to restart the game
104-
// from scratch. Therefore, we need to kill the whole app process and relaunch it.
105-
//
106-
// Restarting only the activity, wouldn't be enough unless it did proper cleanup (including
107-
// releasing and reloading native libs or resetting their state somehow and clearing static data).
108-
Log.v(TAG, "Restarting Godot instance...");
109-
ProcessPhoenix.triggerRebirth(FullScreenGodotApp.this);
110-
}
111-
});
112-
}
113-
114-
@Override
115-
public void onNewIntent(Intent intent) {
116-
super.onNewIntent(intent);
117-
setIntent(intent);
118-
119-
handleStartIntent(intent, false);
120-
121-
if (godotFragment != null) {
122-
godotFragment.onNewIntent(intent);
123-
}
124-
}
125-
126-
private void handleStartIntent(Intent intent, boolean newLaunch) {
127-
boolean forceQuitRequested = intent.getBooleanExtra(EXTRA_FORCE_QUIT, false);
128-
if (forceQuitRequested) {
129-
Log.d(TAG, "Force quit requested, terminating..");
130-
ProcessPhoenix.forceQuit(this);
131-
return;
132-
}
133-
134-
if (!newLaunch) {
135-
boolean newLaunchRequested = intent.getBooleanExtra(EXTRA_NEW_LAUNCH, false);
136-
if (newLaunchRequested) {
137-
Log.d(TAG, "New launch requested, restarting..");
138-
139-
Intent restartIntent = new Intent(intent).putExtra(EXTRA_NEW_LAUNCH, false);
140-
ProcessPhoenix.triggerRebirth(this, restartIntent);
141-
return;
142-
}
143-
}
144-
}
145-
146-
@CallSuper
147-
@Override
148-
public void onActivityResult(int requestCode, int resultCode, Intent data) {
149-
super.onActivityResult(requestCode, resultCode, data);
150-
if (godotFragment != null) {
151-
godotFragment.onActivityResult(requestCode, resultCode, data);
152-
}
153-
}
154-
155-
@CallSuper
156-
@Override
157-
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
158-
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
159-
if (godotFragment != null) {
160-
godotFragment.onRequestPermissionsResult(requestCode, permissions, grantResults);
161-
}
162-
}
163-
164-
@Override
165-
public void onBackPressed() {
166-
if (godotFragment != null) {
167-
godotFragment.onBackPressed();
168-
} else {
169-
super.onBackPressed();
170-
}
171-
}
172-
173-
/**
174-
* Used to initialize the Godot fragment instance in {@link FullScreenGodotApp#onCreate(Bundle)}.
175-
*/
176-
@NonNull
177-
protected Godot initGodotInstance() {
178-
return new Godot();
179-
}
180-
181-
@Nullable
182-
protected final Godot getGodotFragment() {
183-
return godotFragment;
184-
}
185-
}
38+
@Deprecated
39+
public abstract class FullScreenGodotApp extends GodotActivity {}

0 commit comments

Comments
 (0)