Android: Remove the EmulationState class

The purpose of this class was to keep track of state which the
emulation core was already keeping track of. This is rather risky -
if we update the state of one of the two without updating the other,
the two become out of sync, leading to some rather confusing problems.

This duplicated state was removed from EmulationState in the
previous commits, so now there isn't much left in the class.
Might as well move its members directly into EmulationFragment.
This commit is contained in:
JosJuice 2021-08-08 15:01:12 +02:00
parent 2cd09b8eb3
commit 53d7d595e6

View File

@ -32,7 +32,9 @@ public final class EmulationFragment extends Fragment implements SurfaceHolder.C
private InputOverlay mInputOverlay;
private EmulationState mEmulationState;
private String[] mGamePaths;
private boolean mRunWhenSurfaceIsValid;
private boolean mLoadPreviousTemporaryState;
private EmulationActivity activity;
@ -73,8 +75,7 @@ public final class EmulationFragment extends Fragment implements SurfaceHolder.C
// So this fragment doesn't restart on configuration changes; i.e. rotation.
setRetainInstance(true);
String[] gamePaths = getArguments().getStringArray(KEY_GAMEPATHS);
mEmulationState = new EmulationState(gamePaths, getTemporaryStateFilePath());
mGamePaths = getArguments().getStringArray(KEY_GAMEPATHS);
}
/**
@ -117,7 +118,7 @@ public final class EmulationFragment extends Fragment implements SurfaceHolder.C
public void onResume()
{
super.onResume();
mEmulationState.run(activity.isActivityRecreated());
run(activity.isActivityRecreated());
}
@Override
@ -178,7 +179,10 @@ public final class EmulationFragment extends Fragment implements SurfaceHolder.C
{
Log.debug("[EmulationFragment] Surface changed. Resolution: " + width + "x" + height);
NativeLibrary.SurfaceChanged(holder.getSurface());
mEmulationState.newSurface();
if (mRunWhenSurfaceIsValid)
{
runWithValidSurface();
}
}
@Override
@ -218,39 +222,26 @@ public final class EmulationFragment extends Fragment implements SurfaceHolder.C
return mInputOverlay != null && mInputOverlay.isInEditMode();
}
private static class EmulationState
{
private final String[] mGamePaths;
private boolean mRunWhenSurfaceIsValid;
private boolean loadPreviousTemporaryState;
private final String temporaryStatePath;
EmulationState(String[] gamePaths, String temporaryStatePath)
{
mGamePaths = gamePaths;
this.temporaryStatePath = temporaryStatePath;
}
public void run(boolean isActivityRecreated)
private void run(boolean isActivityRecreated)
{
if (isActivityRecreated)
{
if (NativeLibrary.IsRunning())
{
loadPreviousTemporaryState = false;
deleteFile(temporaryStatePath);
mLoadPreviousTemporaryState = false;
deleteFile(getTemporaryStateFilePath());
}
else
{
loadPreviousTemporaryState = true;
mLoadPreviousTemporaryState = true;
}
}
else
{
Log.debug("[EmulationFragment] activity resumed or fresh start");
loadPreviousTemporaryState = false;
mLoadPreviousTemporaryState = false;
// activity resumed without being killed or this is the first run
deleteFile(temporaryStatePath);
deleteFile(getTemporaryStateFilePath());
}
// If the surface is set, run now. Otherwise, wait for it to get set.
@ -264,14 +255,6 @@ public final class EmulationFragment extends Fragment implements SurfaceHolder.C
}
}
public void newSurface()
{
if (mRunWhenSurfaceIsValid)
{
runWithValidSurface();
}
}
private void runWithValidSurface()
{
mRunWhenSurfaceIsValid = false;
@ -281,10 +264,10 @@ public final class EmulationFragment extends Fragment implements SurfaceHolder.C
Thread emulationThread = new Thread(() ->
{
if (loadPreviousTemporaryState)
if (mLoadPreviousTemporaryState)
{
Log.debug("[EmulationFragment] Starting emulation thread from previous state.");
NativeLibrary.Run(mGamePaths, temporaryStatePath, true);
NativeLibrary.Run(mGamePaths, getTemporaryStateFilePath(), true);
}
else
{
@ -297,15 +280,13 @@ public final class EmulationFragment extends Fragment implements SurfaceHolder.C
}
else
{
if (!EmulationActivity.getHasUserPausedEmulation() &&
!NativeLibrary.IsShowingAlertMessage())
if (!EmulationActivity.getHasUserPausedEmulation() && !NativeLibrary.IsShowingAlertMessage())
{
Log.debug("[EmulationFragment] Resuming emulation.");
NativeLibrary.UnPauseEmulation();
}
}
}
}
public void saveTemporaryState()
{