Use AppCompat to implement future-compatible Back handling.

This commit is contained in:
Henrik Rydgård
2025-09-02 14:25:46 +02:00
parent ebe4fccbb1
commit d5682910cc
4 changed files with 32 additions and 22 deletions
-9
View File
@@ -1,9 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="black">#FF000000</color>
<style name="ppsspp_style" parent="android:Theme.DeviceDefault">
<item name="android:windowFullscreen">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:navigationBarColor">@color/black</item>
</style>
</resources>
+1 -1
View File
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="black">#FF000000</color>
<style name="ppsspp_style" parent="android:Theme.DeviceDefault">
<style name="ppsspp_style" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowFullscreen">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:navigationBarColor">@color/black</item>
+2 -1
View File
@@ -1,8 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="black">#FF000000</color>
<style name="ppsspp_style">
<style name="ppsspp_style" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowFullscreen">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:navigationBarColor">@color/black</item>
</style>
</resources>
@@ -32,8 +32,10 @@ import android.os.PowerManager;
import android.os.Vibrator;
import android.provider.MediaStore;
import androidx.activity.OnBackPressedCallback;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.documentfile.provider.DocumentFile;
import android.text.InputType;
import android.util.Log;
@@ -60,7 +62,7 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
@SuppressWarnings("ConstantConditions")
public abstract class NativeActivity extends Activity implements SensorEventListener {
public abstract class NativeActivity extends AppCompatActivity implements SensorEventListener {
// Remember to loadLibrary your JNI .so in a static {} block
// Adjust these as necessary
@@ -648,7 +650,7 @@ public abstract class NativeActivity extends Activity implements SensorEventList
} else {
updateSystemUiVisibility();
mSurfaceView = new NativeSurfaceView(NativeActivity.this);
mSurfaceView = new NativeSurfaceView(this);
sizeManager.setSurfaceView(mSurfaceView);
setContentView(mSurfaceView);
startRenderLoopThread();
@@ -661,6 +663,29 @@ public abstract class NativeActivity extends Activity implements SensorEventList
shortcutParam = null;
}
// Set up the back key handling to be future-compatible
OnBackPressedCallback callback = new OnBackPressedCallback(true) {
// Note: For "pretty" back handling internally, we could handle things like handleOnBackProgressed etc
// if we want to implement our own back previews.
@Override
public void handleOnBackPressed() {
if (NativeApp.isAtTopLevel()) {
// Pass through to normal logic, allowing backing out of the main screen.
// The setEnabled dance seems to be the normal way of handling this, to avoid recursive loops.
setEnabled(false);
NativeActivity.this.getOnBackPressedDispatcher().onBackPressed();
setEnabled(true);
} else {
// Pass straight into the native code.
NativeApp.keyDown(NativeApp.DEVICE_ID_DEFAULT, KeyEvent.KEYCODE_BACK, false);
NativeApp.keyUp(NativeApp.DEVICE_ID_DEFAULT, KeyEvent.KEYCODE_BACK);
}
}
};
// Add the callback to the dispatcher
getOnBackPressedDispatcher().addCallback(this, callback);
Log.i(TAG, "onCreate end");
}
@@ -1109,12 +1134,8 @@ public abstract class NativeActivity extends Activity implements SensorEventList
case KeyEvent.KEYCODE_BACK:
if (event.isAltPressed()) {
NativeApp.keyDown(NativeApp.DEVICE_ID_PAD_0, 1004, repeat); // special custom keycode for the O button on Xperia Play
} else if (NativeApp.isAtTopLevel()) {
Log.i(TAG, "IsAtTopLevel returned true.");
// Pass through the back event.
return super.onKeyDown(keyCode, event);
} else {
NativeApp.keyDown(NativeApp.DEVICE_ID_DEFAULT, keyCode, repeat);
super.onKeyDown(keyCode, event);
}
return true;
case KeyEvent.KEYCODE_MENU:
@@ -1147,11 +1168,8 @@ public abstract class NativeActivity extends Activity implements SensorEventList
case KeyEvent.KEYCODE_BACK:
if (event.isAltPressed()) {
NativeApp.keyUp(NativeApp.DEVICE_ID_PAD_0, 1004); // special custom keycode
} else if (NativeApp.isAtTopLevel()) {
Log.i(TAG, "IsAtTopLevel returned true.");
return super.onKeyUp(keyCode, event);
} else {
NativeApp.keyUp(NativeApp.DEVICE_ID_DEFAULT, keyCode);
return super.onKeyUp(keyCode, event);
}
return true;
case KeyEvent.KEYCODE_MENU: