Merge pull request #8833 from Ebola16/Panic

Android: Use DialogFragment for AlertMessage
This commit is contained in:
JosJuice 2020-10-10 21:03:52 +02:00 committed by GitHub
commit 5a939ccfaf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 182 additions and 67 deletions

View File

@ -8,10 +8,10 @@ package org.dolphinemu.dolphinemu;
import android.util.DisplayMetrics; import android.util.DisplayMetrics;
import android.view.Surface; import android.view.Surface;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import org.dolphinemu.dolphinemu.activities.EmulationActivity; import org.dolphinemu.dolphinemu.activities.EmulationActivity;
import org.dolphinemu.dolphinemu.dialogs.AlertMessage;
import org.dolphinemu.dolphinemu.utils.CompressCallback; import org.dolphinemu.dolphinemu.utils.CompressCallback;
import org.dolphinemu.dolphinemu.utils.Log; import org.dolphinemu.dolphinemu.utils.Log;
import org.dolphinemu.dolphinemu.utils.Rumble; import org.dolphinemu.dolphinemu.utils.Rumble;
@ -25,6 +25,9 @@ import java.util.LinkedHashMap;
*/ */
public final class NativeLibrary public final class NativeLibrary
{ {
private static final Object sAlertMessageLock = new Object();
private static boolean sIsShowingAlertMessage = false;
private static WeakReference<EmulationActivity> sEmulationActivity = new WeakReference<>(null); private static WeakReference<EmulationActivity> sEmulationActivity = new WeakReference<>(null);
/** /**
@ -393,6 +396,8 @@ public final class NativeLibrary
*/ */
public static native void StopEmulation(); public static native void StopEmulation();
public static native boolean IsBooting();
public static native void WaitUntilDoneBooting(); public static native void WaitUntilDoneBooting();
/** /**
@ -440,10 +445,8 @@ public final class NativeLibrary
public static native void SetObscuredPixelsTop(int height); public static native void SetObscuredPixelsTop(int height);
private static boolean alertResult = false;
public static boolean displayAlertMsg(final String caption, final String text, public static boolean displayAlertMsg(final String caption, final String text,
final boolean yesNo) final boolean yesNo, final boolean isWarning)
{ {
Log.error("[NativeLibrary] Alert: " + text); Log.error("[NativeLibrary] Alert: " + text);
final EmulationActivity emulationActivity = sEmulationActivity.get(); final EmulationActivity emulationActivity = sEmulationActivity.get();
@ -452,64 +455,35 @@ public final class NativeLibrary
{ {
Log.warning("[NativeLibrary] EmulationActivity is null, can't do panic alert."); Log.warning("[NativeLibrary] EmulationActivity is null, can't do panic alert.");
} }
else else if (emulationActivity.isIgnoringWarnings() && isWarning)
{ {
// Create object used for waiting. return true;
final Object lock = new Object();
AlertDialog.Builder builder = new AlertDialog.Builder(emulationActivity,
R.style.DolphinDialogBase)
.setTitle(caption)
.setMessage(text);
// If not yes/no dialog just have one button that dismisses modal,
// otherwise have a yes and no button that sets alertResult accordingly.
if (!yesNo)
{
builder
.setCancelable(false)
.setPositiveButton("OK", (dialog, whichButton) ->
{
dialog.dismiss();
synchronized (lock)
{
lock.notify();
}
});
} }
else else
{ {
alertResult = false; // AlertMessages while the core is booting will deadlock when WaitUntilDoneBooting is called.
// Report the AlertMessage text as a toast instead.
if (IsBooting())
{
emulationActivity.runOnUiThread(
() -> Toast.makeText(emulationActivity.getApplicationContext(), text,
Toast.LENGTH_LONG)
.show());
}
else
{
sIsShowingAlertMessage = true;
builder emulationActivity.runOnUiThread(
.setPositiveButton("Yes", (dialog, whichButton) -> () -> AlertMessage.newInstance(caption, text, yesNo, isWarning)
{ .show(emulationActivity.getSupportFragmentManager(), "AlertMessage"));
alertResult = true;
dialog.dismiss();
synchronized (lock)
{
lock.notify();
}
})
.setNegativeButton("No", (dialog, whichButton) ->
{
alertResult = false;
dialog.dismiss();
synchronized (lock)
{
lock.notify();
}
});
}
// Show the AlertDialog on the main thread.
emulationActivity.runOnUiThread(builder::show);
// Wait for the lock to notify that it is complete. // Wait for the lock to notify that it is complete.
synchronized (lock) synchronized (sAlertMessageLock)
{ {
try try
{ {
lock.wait(); sAlertMessageLock.wait();
} }
catch (Exception ignored) catch (Exception ignored)
{ {
@ -517,11 +491,28 @@ public final class NativeLibrary
} }
if (yesNo) if (yesNo)
result = alertResult; {
result = AlertMessage.getAlertResult();
} }
}
}
sIsShowingAlertMessage = false;
return result; return result;
} }
public static boolean IsShowingAlertMessage()
{
return sIsShowingAlertMessage;
}
public static void NotifyAlertMessageLock()
{
synchronized (sAlertMessageLock)
{
sAlertMessageLock.notify();
}
}
public static void setEmulationActivity(EmulationActivity emulationActivity) public static void setEmulationActivity(EmulationActivity emulationActivity)
{ {
Log.verbose("[NativeLibrary] Registering EmulationActivity."); Log.verbose("[NativeLibrary] Registering EmulationActivity.");

View File

@ -85,12 +85,14 @@ public final class EmulationActivity extends AppCompatActivity
private String mSelectedGameId; private String mSelectedGameId;
private int mPlatform; private int mPlatform;
private String[] mPaths; private String[] mPaths;
private boolean mIgnoreWarnings;
private static boolean sUserPausedEmulation; private static boolean sUserPausedEmulation;
public static final String EXTRA_SELECTED_GAMES = "SelectedGames"; public static final String EXTRA_SELECTED_GAMES = "SelectedGames";
public static final String EXTRA_SELECTED_TITLE = "SelectedTitle"; public static final String EXTRA_SELECTED_TITLE = "SelectedTitle";
public static final String EXTRA_SELECTED_GAMEID = "SelectedGameId"; public static final String EXTRA_SELECTED_GAMEID = "SelectedGameId";
public static final String EXTRA_PLATFORM = "Platform"; public static final String EXTRA_PLATFORM = "Platform";
public static final String EXTRA_IGNORE_WARNINGS = "IgnoreWarnings";
public static final String EXTRA_USER_PAUSED_EMULATION = "sUserPausedEmulation"; public static final String EXTRA_USER_PAUSED_EMULATION = "sUserPausedEmulation";
@Retention(SOURCE) @Retention(SOURCE)
@ -272,8 +274,10 @@ public final class EmulationActivity extends AppCompatActivity
mSelectedTitle = gameToEmulate.getStringExtra(EXTRA_SELECTED_TITLE); mSelectedTitle = gameToEmulate.getStringExtra(EXTRA_SELECTED_TITLE);
mSelectedGameId = gameToEmulate.getStringExtra(EXTRA_SELECTED_GAMEID); mSelectedGameId = gameToEmulate.getStringExtra(EXTRA_SELECTED_GAMEID);
mPlatform = gameToEmulate.getIntExtra(EXTRA_PLATFORM, 0); mPlatform = gameToEmulate.getIntExtra(EXTRA_PLATFORM, 0);
mIgnoreWarnings = gameToEmulate.getBooleanExtra(EXTRA_IGNORE_WARNINGS, false);
sUserPausedEmulation = gameToEmulate.getBooleanExtra(EXTRA_USER_PAUSED_EMULATION, false); sUserPausedEmulation = gameToEmulate.getBooleanExtra(EXTRA_USER_PAUSED_EMULATION, false);
activityRecreated = false; activityRecreated = false;
Toast.makeText(this, R.string.emulation_menu_help, Toast.LENGTH_LONG).show();
} }
else else
{ {
@ -297,8 +301,6 @@ public final class EmulationActivity extends AppCompatActivity
// Set these options now so that the SurfaceView the game renders into is the right size. // Set these options now so that the SurfaceView the game renders into is the right size.
enableFullscreenImmersive(); enableFullscreenImmersive();
Toast.makeText(this, getString(R.string.emulation_menu_help), Toast.LENGTH_LONG).show();
Rumble.initRumble(this); Rumble.initRumble(this);
setContentView(R.layout.activity_emulation); setContentView(R.layout.activity_emulation);
@ -328,6 +330,7 @@ public final class EmulationActivity extends AppCompatActivity
outState.putString(EXTRA_SELECTED_TITLE, mSelectedTitle); outState.putString(EXTRA_SELECTED_TITLE, mSelectedTitle);
outState.putString(EXTRA_SELECTED_GAMEID, mSelectedGameId); outState.putString(EXTRA_SELECTED_GAMEID, mSelectedGameId);
outState.putInt(EXTRA_PLATFORM, mPlatform); outState.putInt(EXTRA_PLATFORM, mPlatform);
outState.putBoolean(EXTRA_USER_PAUSED_EMULATION, mIgnoreWarnings);
outState.putBoolean(EXTRA_USER_PAUSED_EMULATION, sUserPausedEmulation); outState.putBoolean(EXTRA_USER_PAUSED_EMULATION, sUserPausedEmulation);
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
} }
@ -338,6 +341,7 @@ public final class EmulationActivity extends AppCompatActivity
mSelectedTitle = savedInstanceState.getString(EXTRA_SELECTED_TITLE); mSelectedTitle = savedInstanceState.getString(EXTRA_SELECTED_TITLE);
mSelectedGameId = savedInstanceState.getString(EXTRA_SELECTED_GAMEID); mSelectedGameId = savedInstanceState.getString(EXTRA_SELECTED_GAMEID);
mPlatform = savedInstanceState.getInt(EXTRA_PLATFORM); mPlatform = savedInstanceState.getInt(EXTRA_PLATFORM);
mIgnoreWarnings = savedInstanceState.getBoolean(EXTRA_IGNORE_WARNINGS);
sUserPausedEmulation = savedInstanceState.getBoolean(EXTRA_USER_PAUSED_EMULATION); sUserPausedEmulation = savedInstanceState.getBoolean(EXTRA_USER_PAUSED_EMULATION);
} }
@ -672,6 +676,16 @@ public final class EmulationActivity extends AppCompatActivity
} }
} }
public boolean isIgnoringWarnings()
{
return mIgnoreWarnings;
}
public void setIgnoreWarnings(boolean value)
{
mIgnoreWarnings = value;
}
public static boolean getHasUserPausedEmulation() public static boolean getHasUserPausedEmulation()
{ {
return sUserPausedEmulation; return sUserPausedEmulation;

View File

@ -0,0 +1,96 @@
package org.dolphinemu.dolphinemu.dialogs;
import android.app.Dialog;
import android.os.Bundle;
import org.dolphinemu.dolphinemu.NativeLibrary;
import org.dolphinemu.dolphinemu.R;
import org.dolphinemu.dolphinemu.activities.EmulationActivity;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;
public final class AlertMessage extends DialogFragment
{
private static boolean sAlertResult = false;
private static final String ARG_TITLE = "title";
private static final String ARG_MESSAGE = "message";
private static final String ARG_YES_NO = "yesNo";
private static final String ARG_IS_WARNING = "isWarning";
public static AlertMessage newInstance(String title, String message, boolean yesNo,
boolean isWarning)
{
AlertMessage fragment = new AlertMessage();
Bundle args = new Bundle();
args.putString(ARG_TITLE, title);
args.putString(ARG_MESSAGE, message);
args.putBoolean(ARG_YES_NO, yesNo);
args.putBoolean(ARG_IS_WARNING, isWarning);
fragment.setArguments(args);
return fragment;
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
final EmulationActivity emulationActivity = NativeLibrary.getEmulationActivity();
String title = requireArguments().getString(ARG_TITLE);
String message = requireArguments().getString(ARG_MESSAGE);
boolean yesNo = requireArguments().getBoolean(ARG_YES_NO);
boolean isWarning = requireArguments().getBoolean(ARG_IS_WARNING);
setCancelable(false);
AlertDialog.Builder builder = new AlertDialog.Builder(emulationActivity,
R.style.DolphinDialogBase)
.setTitle(title)
.setMessage(message);
// If not yes/no dialog just have one button that dismisses modal,
// otherwise have a yes and no button that sets sAlertResult accordingly.
if (!yesNo)
{
builder.setPositiveButton(android.R.string.ok, (dialog, which) ->
{
dialog.dismiss();
NativeLibrary.NotifyAlertMessageLock();
});
}
else
{
builder.setPositiveButton(android.R.string.yes, (dialog, which) ->
{
sAlertResult = true;
dialog.dismiss();
NativeLibrary.NotifyAlertMessageLock();
})
.setNegativeButton(android.R.string.no, (dialog, which) ->
{
sAlertResult = false;
dialog.dismiss();
NativeLibrary.NotifyAlertMessageLock();
});
}
if (isWarning)
{
builder.setNeutralButton(R.string.ignore_warning_alert_messages, (dialog, which) ->
{
emulationActivity.setIgnoreWarnings(true);
dialog.dismiss();
NativeLibrary.NotifyAlertMessageLock();
});
}
return builder.create();
}
public static boolean getAlertResult()
{
return sAlertResult;
}
}

View File

@ -112,7 +112,7 @@ public final class EmulationFragment extends Fragment implements SurfaceHolder.C
@Override @Override
public void onPause() public void onPause()
{ {
if (mEmulationState.isRunning()) if (mEmulationState.isRunning() && !NativeLibrary.IsShowingAlertMessage())
mEmulationState.pause(); mEmulationState.pause();
super.onPause(); super.onPause();
} }
@ -323,7 +323,7 @@ public final class EmulationFragment extends Fragment implements SurfaceHolder.C
mSurface = null; mSurface = null;
Log.debug("[EmulationFragment] Surface destroyed."); Log.debug("[EmulationFragment] Surface destroyed.");
if (state != State.STOPPED) if (state != State.STOPPED && !NativeLibrary.IsShowingAlertMessage())
{ {
// In order to avoid dereferencing nullptr, we must not destroy the surface while booting // In order to avoid dereferencing nullptr, we must not destroy the surface while booting
// the core, so wait here if necessary. An easy (but not 100% consistent) way to reach // the core, so wait here if necessary. An easy (but not 100% consistent) way to reach
@ -362,7 +362,8 @@ public final class EmulationFragment extends Fragment implements SurfaceHolder.C
else if (state == State.PAUSED) else if (state == State.PAUSED)
{ {
NativeLibrary.SurfaceChanged(mSurface); NativeLibrary.SurfaceChanged(mSurface);
if (!EmulationActivity.getHasUserPausedEmulation()) if (!EmulationActivity.getHasUserPausedEmulation() &&
!NativeLibrary.IsShowingAlertMessage())
{ {
Log.debug("[EmulationFragment] Resuming emulation."); Log.debug("[EmulationFragment] Resuming emulation.");
NativeLibrary.UnPauseEmulation(); NativeLibrary.UnPauseEmulation();

View File

@ -438,5 +438,6 @@ It can efficiently compress both junk data and encrypted Wii data.
<string name="slider_setting_value">%1$d%2$s</string> <string name="slider_setting_value">%1$d%2$s</string>
<string name="disc_number">Disc %1$d</string> <string name="disc_number">Disc %1$d</string>
<string name="disabled_gc_overlay_notice">GameCube Controller 1 is set to \"None\"</string> <string name="disabled_gc_overlay_notice">GameCube Controller 1 is set to \"None\"</string>
<string name="ignore_warning_alert_messages">Ignore for this session</string>
</resources> </resources>

View File

@ -210,7 +210,7 @@ jint JNI_OnLoad(JavaVM* vm, void* reserved)
const jclass native_library_class = env->FindClass("org/dolphinemu/dolphinemu/NativeLibrary"); const jclass native_library_class = env->FindClass("org/dolphinemu/dolphinemu/NativeLibrary");
s_native_library_class = reinterpret_cast<jclass>(env->NewGlobalRef(native_library_class)); s_native_library_class = reinterpret_cast<jclass>(env->NewGlobalRef(native_library_class));
s_display_alert_msg = env->GetStaticMethodID(s_native_library_class, "displayAlertMsg", s_display_alert_msg = env->GetStaticMethodID(s_native_library_class, "displayAlertMsg",
"(Ljava/lang/String;Ljava/lang/String;Z)Z"); "(Ljava/lang/String;Ljava/lang/String;ZZ)Z");
s_do_rumble = env->GetStaticMethodID(s_native_library_class, "rumble", "(ID)V"); s_do_rumble = env->GetStaticMethodID(s_native_library_class, "rumble", "(ID)V");
s_get_update_touch_pointer = s_get_update_touch_pointer =
env->GetStaticMethodID(s_native_library_class, "updateTouchPointer", "()V"); env->GetStaticMethodID(s_native_library_class, "updateTouchPointer", "()V");

View File

@ -151,14 +151,14 @@ void Host_TitleChanged()
{ {
} }
static bool MsgAlert(const char* caption, const char* text, bool yes_no, Common::MsgType /*style*/) static bool MsgAlert(const char* caption, const char* text, bool yes_no, Common::MsgType style)
{ {
JNIEnv* env = IDCache::GetEnvForThread(); JNIEnv* env = IDCache::GetEnvForThread();
// Execute the Java method. // Execute the Java method.
jboolean result = env->CallStaticBooleanMethod( jboolean result = env->CallStaticBooleanMethod(
IDCache::GetNativeLibraryClass(), IDCache::GetDisplayAlertMsg(), ToJString(env, caption), IDCache::GetNativeLibraryClass(), IDCache::GetDisplayAlertMsg(), ToJString(env, caption),
ToJString(env, text), yes_no ? JNI_TRUE : JNI_FALSE); ToJString(env, text), yes_no ? JNI_TRUE : JNI_FALSE, style == Common::MsgType::Warning);
return result != JNI_FALSE; return result != JNI_FALSE;
} }
@ -300,6 +300,12 @@ JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_StopEmulatio
s_emulation_end_event.Wait(); s_emulation_end_event.Wait();
} }
JNIEXPORT jboolean JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_IsBooting(JNIEnv* env,
jobject obj)
{
return static_cast<jboolean>(Core::IsBooting());
}
JNIEXPORT void JNICALL JNIEXPORT void JNICALL
Java_org_dolphinemu_dolphinemu_NativeLibrary_WaitUntilDoneBooting(JNIEnv* env, jobject obj) Java_org_dolphinemu_dolphinemu_NativeLibrary_WaitUntilDoneBooting(JNIEnv* env, jobject obj)
{ {

View File

@ -170,6 +170,11 @@ void DisplayMessage(std::string message, int time_in_ms)
OSD::AddMessage(std::move(message), time_in_ms); OSD::AddMessage(std::move(message), time_in_ms);
} }
bool IsBooting()
{
return s_is_booting.IsSet() || !s_hardware_initialized;
}
bool IsRunning() bool IsRunning()
{ {
return (GetState() != State::Uninitialized || s_hardware_initialized) && !s_is_stopping; return (GetState() != State::Uninitialized || s_hardware_initialized) && !s_is_stopping;
@ -674,7 +679,7 @@ State GetState()
void WaitUntilDoneBooting() void WaitUntilDoneBooting()
{ {
if (s_is_booting.IsSet() || !s_hardware_initialized) if (IsBooting())
s_done_booting.Wait(); s_done_booting.Wait();
} }

View File

@ -99,6 +99,7 @@ void UndeclareAsCPUThread();
std::string StopMessage(bool main_thread, std::string_view message); std::string StopMessage(bool main_thread, std::string_view message);
bool IsBooting();
bool IsRunning(); bool IsRunning();
bool IsRunningAndStarted(); // is running and the CPU loop has been entered bool IsRunningAndStarted(); // is running and the CPU loop has been entered
bool IsRunningInCurrentThread(); // this tells us whether we are running in the CPU thread. bool IsRunningInCurrentThread(); // this tells us whether we are running in the CPU thread.