Handle configuration changes to resize the surface;

Deal with surface creation/destruction;
This commit is contained in:
SerpentSpirale 2022-03-19 18:31:46 +01:00 committed by Boulay Mathias
parent 88f31df1f1
commit 8c7bba530e
5 changed files with 54 additions and 17 deletions

View File

@ -11,11 +11,13 @@ import static org.lwjgl.glfw.CallbackBridge.windowWidth;
import android.app.*;
import android.content.*;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.os.*;
import android.util.*;
import android.view.*;
import android.widget.*;
import androidx.annotation.NonNull;
import androidx.drawerlayout.widget.*;
import com.google.android.material.navigation.*;
import com.kdt.LoggerView;
@ -44,6 +46,7 @@ public class BaseMainActivity extends BaseActivity {
private boolean mIsResuming = false;
ControlLayout mControlLayout;
private MinecraftGLSurfaceView minecraftGLView;
private static Touchpad touchpad;
private LoggerView loggerView;
@ -69,6 +72,7 @@ public class BaseMainActivity extends BaseActivity {
GLOBAL_CLIPBOARD = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
touchCharInput = findViewById(R.id.mainTouchCharInput);
loggerView = findViewById(R.id.mainLoggerView);
mControlLayout = findViewById(R.id.main_control_layout);
mProfile = PojavProfile.getCurrentProfileContent(this);
minecraftProfile = LauncherProfiles.mainProfileJson.profiles.get(mProfile.selectedProfile);
@ -187,6 +191,13 @@ public class BaseMainActivity extends BaseActivity {
super.onStop();
}
@Override
public void onConfigurationChanged(@NonNull Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Tools.getDisplayMetrics(this);
minecraftGLView.refreshSize();
}
public static void fullyExit() {
android.os.Process.killProcess(android.os.Process.myPid());
}

View File

@ -47,10 +47,8 @@ public class MinecraftGLSurfaceView extends SurfaceView {
private TextView mPointerDebugTextView;
/* Resolution scaler option, allow downsizing a window */
private final float mScaleFactor = LauncherPreferences.DEFAULT_PREF.getInt("resolutionRatio",100)/100f;
/* Display properties, such as resolution and DPI */
private final DisplayMetrics mDisplayMetrics = Tools.getDisplayMetrics((Activity) getContext());
/* Sensitivity, adjusted according to screen size */
private final double mSensitivityFactor = (1.4 * (1080f/ mDisplayMetrics.heightPixels));
private final double mSensitivityFactor = (1.4 * (1080f/ Tools.getDisplayMetrics((Activity) getContext()).heightPixels));
/* Use to detect simple and double taps */
private final TapDetector mSingleTapDetector = new TapDetector(1, TapDetector.DETECTION_METHOD_BOTH);
private final TapDetector mDoubleTapDetector = new TapDetector(2, TapDetector.DETECTION_METHOD_DOWN);
@ -140,19 +138,23 @@ public class MinecraftGLSurfaceView extends SurfaceView {
@Override
public void surfaceCreated(@NonNull SurfaceHolder holder) {
//Load Minecraft options:
MCOptionUtils.load();
MCOptionUtils.set("fullscreen", "off");
MCOptionUtils.set("overrideWidth", String.valueOf(windowWidth));
MCOptionUtils.set("overrideHeight", String.valueOf(windowHeight));
MCOptionUtils.save();
getMcScale();
// Should we do that?
if(isCalled) return;
// Resize stuff
if(isCalled){
//getHolder().setFixedSize(windowWidth, windowHeight);
//JREUtils.setupBridgeWindow(getHolder().getSurface());
return;
}
isCalled = true;
getHolder().setFixedSize(windowWidth, windowHeight);
refreshSize();
JREUtils.setupBridgeWindow(getHolder().getSurface());
new Thread(() -> {
@ -169,18 +171,12 @@ public class MinecraftGLSurfaceView extends SurfaceView {
@Override
public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {
windowWidth = Tools.getDisplayFriendlyRes(mDisplayMetrics.widthPixels, mScaleFactor);
windowHeight = Tools.getDisplayFriendlyRes(mDisplayMetrics.heightPixels, mScaleFactor);
CallbackBridge.sendUpdateWindowSize(windowWidth, windowHeight);
getMcScale();
Toast.makeText(getContext(), "width: " + width, Toast.LENGTH_SHORT).show();
Toast.makeText(getContext(), "height: " + height, Toast.LENGTH_SHORT).show();
getHolder().setFixedSize(windowWidth, windowHeight);
refreshSize();
}
@Override
public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
JREUtils.releaseBridgeWindow();
}
});
}
@ -588,6 +584,26 @@ public class MinecraftGLSurfaceView extends SurfaceView {
mPointerDebugTextView.setVisibility(mPointerDebugTextView.getVisibility() == View.GONE ? View.VISIBLE : View.GONE);
}
/** Called when the size need to be set at any point during the surface lifecycle **/
public void refreshSize(){
windowWidth = Tools.getDisplayFriendlyRes(Tools.currentDisplayMetrics.widthPixels, mScaleFactor);
windowHeight = Tools.getDisplayFriendlyRes(Tools.currentDisplayMetrics.heightPixels, mScaleFactor);
getHolder().setFixedSize(windowWidth, windowHeight);
// Set the new frame size
MCOptionUtils.load();
MCOptionUtils.set("overrideWidth", String.valueOf(windowWidth));
MCOptionUtils.set("overrideHeight", String.valueOf(windowHeight));
MCOptionUtils.save();
CallbackBridge.sendUpdateWindowSize(windowWidth, windowHeight);
getMcScale();
//Toast.makeText(getContext(), "width: " + width, Toast.LENGTH_SHORT).show();
//Toast.makeText(getContext(), "height: " + height, Toast.LENGTH_SHORT).show();
}
/** A small interface called when the listener is ready for the first time */
public interface SurfaceReadyListener {
void isReady();

View File

@ -243,14 +243,17 @@ public final class Tools {
}
}
}
/*
minecraftArgs.add("--width");
minecraftArgs.add(Integer.toString(CallbackBridge.windowWidth));
minecraftArgs.add("--height");
minecraftArgs.add(Integer.toString(CallbackBridge.windowHeight));
minecraftArgs.add("--fullscreenWidth");
minecraftArgs.add(Integer.toString(CallbackBridge.windowWidth));
minecraftArgs.add("--fullscreenHeight");
minecraftArgs.add(Integer.toString(CallbackBridge.windowHeight));
*/
String[] argsFromJson = JSONUtils.insertJSONValueList(
splitAndFilterEmpty(
@ -280,7 +283,7 @@ public final class Tools {
strList.add(arg);
}
}
strList.add("--fullscreen");
//strList.add("--fullscreen");
return strList.toArray(new String[0]);
}

View File

@ -547,6 +547,7 @@ public class JREUtils {
public static native boolean dlopen(String libPath);
public static native void setLdLibraryPath(String ldLibraryPath);
public static native void setupBridgeWindow(Object surface);
public static native void releaseBridgeWindow();
public static native void setupExitTrap(Context context);
// Obtain AWT screen pixels to render on Android SurfaceView
public static native int[] renderAWTScreenFrame(/* Object canvas, int width, int height */);

View File

@ -674,6 +674,12 @@ JNIEXPORT void JNICALL Java_net_kdt_pojavlaunch_utils_JREUtils_setupBridgeWindow
free(ptrStr);
}
JNIEXPORT void JNICALL
Java_net_kdt_pojavlaunch_utils_JREUtils_releaseBridgeWindow(JNIEnv *env, jclass clazz) {
ANativeWindow_release(potatoBridge.androidWindow);
}
void* pojavGetCurrentContext() {
switch (config_renderer) {
case RENDERER_GL4ES: