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
1 changed files with 57 additions and 76 deletions

View File

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