Android: Improve handling of portrait mode

This commit is contained in:
Connor McLaughlin 2020-09-10 19:20:49 +10:00
parent 5cc91dc78b
commit 9b942de47e
4 changed files with 40 additions and 1 deletions

View file

@ -723,3 +723,10 @@ DEFINE_JNI_ARGS_METHOD(void, AndroidHostInterface_saveState, jobject obj, jboole
AndroidHostInterface* hi = AndroidHelpers::GetNativeClass(env, obj);
hi->RunOnEmulationThread([hi, global, slot]() { hi->SaveState(global, slot); });
}
DEFINE_JNI_ARGS_METHOD(void, AndroidHostInterface_setDisplayAlignment, jobject obj, jint alignment)
{
AndroidHostInterface* hi = AndroidHelpers::GetNativeClass(env, obj);
hi->RunOnEmulationThread([hi, alignment]() { hi->GetDisplay()->SetDisplayAlignment(static_cast<HostDisplay::Alignment>(alignment)); });
}

View file

@ -18,7 +18,8 @@
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/title_activity_emulation"
android:parentActivityName=".MainActivity"
android:theme="@style/FullscreenTheme">
android:theme="@style/FullscreenTheme"
android:immersive="true">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.github.stenzek.duckstation.MainActivity" />

View file

@ -9,6 +9,10 @@ import android.widget.Toast;
import com.google.android.material.snackbar.Snackbar;
public class AndroidHostInterface {
public final static int DISPLAY_ALIGNMENT_TOP_OR_LEFT = 0;
public final static int DISPLAY_ALIGNMENT_CENTER = 1;
public final static int DISPLAY_ALIGNMENT_RIGHT_OR_BOTTOM = 2;
private long mNativePointer;
private Context mContext;
@ -57,6 +61,8 @@ public class AndroidHostInterface {
public native void applySettings();
public native void setDisplayAlignment(int alignment);
static {
System.loadLibrary("duckstation-native");
}

View file

@ -2,12 +2,14 @@ package com.github.stenzek.duckstation;
import android.annotation.SuppressLint;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
@ -102,6 +104,7 @@ public class EmulationActivity extends AppCompatActivity implements SurfaceHolde
// Once we get a surface, we can boot.
if (AndroidHostInterface.getInstance().isEmulationThreadRunning()) {
AndroidHostInterface.getInstance().surfaceChanged(holder.getSurface(), format, width, height);
updateOrientation();
return;
}
@ -110,6 +113,7 @@ public class EmulationActivity extends AppCompatActivity implements SurfaceHolde
final String bootSaveStatePath = getIntent().getStringExtra("saveStatePath");
AndroidHostInterface.getInstance().startEmulationThread(this, holder.getSurface(), bootPath, resumeState, bootSaveStatePath);
updateOrientation();
}
@Override
@ -227,6 +231,27 @@ public class EmulationActivity extends AppCompatActivity implements SurfaceHolde
showSystemUI();
}
@Override
public void onConfigurationChanged(@NonNull Configuration newConfig) {
super.onConfigurationChanged(newConfig);
updateOrientation(newConfig.orientation);
}
private void updateOrientation() {
final int orientation = getResources().getConfiguration().orientation;
updateOrientation(orientation);
}
private void updateOrientation(int newOrientation) {
if (!AndroidHostInterface.getInstance().isEmulationThreadRunning())
return;
if (newOrientation == Configuration.ORIENTATION_PORTRAIT)
AndroidHostInterface.getInstance().setDisplayAlignment(AndroidHostInterface.DISPLAY_ALIGNMENT_TOP_OR_LEFT);
else
AndroidHostInterface.getInstance().setDisplayAlignment(AndroidHostInterface.DISPLAY_ALIGNMENT_CENTER);
}
/**
* Some older devices needs a small delay between UI widget updates
* and a change of the status and navigation bar.