Skip to content

Commit

Permalink
Added "Trap Back button" option to Android prelaunch menu
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhilkinSerg committed Feb 20, 2020
1 parent 55cabb7 commit 741773f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public class SplashScreen extends Activity {
private static final int INSTALL_DIALOG_ID = 0;
private ProgressDialog installDialog;

public CharSequence[] mSettingsNames = { "Software rendering", "Force fullscreen" };
public boolean[] mSettingsValues = { false, false};
public CharSequence[] mSettingsNames = { "Software rendering", "Force fullscreen", "Trap Back button" };
public boolean[] mSettingsValues = { false, false, true };

private String getVersionName() {
try {
Expand Down
6 changes: 6 additions & 0 deletions src/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2157,6 +2157,12 @@ void options_manager::add_options_android()

add_empty_line();

add( "ANDROID_TRAP_BACK_BUTTON", "android", translate_marker( "Trap Back button" ),
translate_marker( "If true, the back button will NOT back out of the app and will be passed to the application as SDL_SCANCODE_AC_BACK. Requires restart." ),
// take default setting from pre-game settings screen - important as there are issues with Back button on Android 9 with specific devices
android_get_default_setting( "Trap Back button", true )
);

add( "ANDROID_AUTO_KEYBOARD", "android", translate_marker( "Auto-manage virtual keyboard" ),
translate_marker( "If true, automatically show/hide the virtual keyboard when necessary based on context. If false, virtual keyboard must be toggled manually." ),
true
Expand Down
35 changes: 23 additions & 12 deletions src/sdltiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,21 @@ static std::weak_ptr<void> winBuffer; //tracking last drawn window to fix the fr
static int fontScaleBuffer; //tracking zoom levels to fix framebuffer w/tiles
extern catacurses::window w_hit_animation; //this window overlays w_terrain which can be oversized

#if defined (__ANDROID__)
// Display an Android toast message
static void android_toast( const std::string &message )
{
JNIEnv *env = ( JNIEnv * )SDL_AndroidGetJNIEnv();
jobject activity = ( jobject )SDL_AndroidGetActivity();
jclass clazz( env->GetObjectClass( activity ) );
jstring toast_message = env->NewStringUTF( message.c_str() );
jmethodID method_id = env->GetMethodID( clazz, "toast", "(Ljava/lang/String;)V" );
env->CallVoidMethod( activity, method_id, toast_message );
env->DeleteLocalRef( activity );
env->DeleteLocalRef( clazz );
}
#endif

//***********************************
//Non-curses, Window functions *
//***********************************
Expand Down Expand Up @@ -403,6 +418,13 @@ static void WinCreate()
SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 6 );
SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );

// Fix Back button crash on Android 9
#if defined(SDL_HINT_ANDROID_TRAP_BACK_BUTTON )
const bool trap_back_button = get_option<bool>( "ANDROID_TRAP_BACK_BUTTON" );
SDL_SetHint( SDL_HINT_ANDROID_TRAP_BACK_BUTTON, trap_back_button ? "1" : "0" );
android_toast( trap_back_button ? "Back button is trapped" : "Back button is NOT trapped" );
#endif

// Prevent mouse|touch input confusion
#if defined(SDL_HINT_ANDROID_SEPARATE_MOUSE_AND_TOUCH)
SDL_SetHint( SDL_HINT_ANDROID_SEPARATE_MOUSE_AND_TOUCH, "1" );
Expand Down Expand Up @@ -2771,18 +2793,7 @@ static void CheckMessages()
quick_shortcuts_toggle_handled = true;
refresh_display();

// Display an Android toast message
{
JNIEnv *env = ( JNIEnv * )SDL_AndroidGetJNIEnv();
jobject activity = ( jobject )SDL_AndroidGetActivity();
jclass clazz( env->GetObjectClass( activity ) );
jstring toast_message = env->NewStringUTF( quick_shortcuts_enabled ? "Shortcuts visible" :
"Shortcuts hidden" );
jmethodID method_id = env->GetMethodID( clazz, "toast", "(Ljava/lang/String;)V" );
env->CallVoidMethod( activity, method_id, toast_message );
env->DeleteLocalRef( activity );
env->DeleteLocalRef( clazz );
}
android_toast( quick_shortcuts_enabled ? "Shortcuts visible" : "Shortcuts hidden" );
}
}

Expand Down

0 comments on commit 741773f

Please sign in to comment.