Add configurable behavior of the back button

This PR allows users to change how the back button functions. Closes #2196

Possible choices are following:

 - **Default** - back button functions how it currently functions (closes the app if there is nowhere to go back to)

 - **Open navigation drawer** - back button always opens the navigation drawer instead of closing the app

 - **Double tap to exit** - like default, but requires two taps to close the app

 - **Confirm to exit** - like default, but prompts user if they really want to exit
This commit is contained in:
Petar Kukolj 2018-11-14 16:18:58 +01:00
parent 68b245701e
commit b3fbb0ec75
5 changed files with 78 additions and 1 deletions

View File

@ -27,6 +27,7 @@ import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.ListView; import android.widget.ListView;
import android.widget.Toast;
import com.bumptech.glide.Glide; import com.bumptech.glide.Glide;
@ -123,6 +124,8 @@ public class MainActivity extends CastEnabledActivity implements NavDrawerActivi
private Subscription subscription; private Subscription subscription;
private long lastBackButtonPressTime = 0;
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
setTheme(UserPreferences.getNoTitleTheme()); setTheme(UserPreferences.getNoTitleTheme());
@ -646,7 +649,33 @@ public class MainActivity extends CastEnabledActivity implements NavDrawerActivi
if(isDrawerOpen()) { if(isDrawerOpen()) {
drawerLayout.closeDrawer(navDrawer); drawerLayout.closeDrawer(navDrawer);
} else { } else {
super.onBackPressed(); switch (UserPreferences.getBackButtonBehavior()) {
case OPEN_DRAWER:
drawerLayout.openDrawer(navDrawer);
break;
case SHOW_PROMPT:
new AlertDialog.Builder(this)
.setMessage(R.string.close_prompt)
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
MainActivity.super.onBackPressed();
}
})
.setNegativeButton(R.string.no, null)
.setCancelable(false)
.show();
break;
case DOUBLE_TAP:
if(lastBackButtonPressTime < System.currentTimeMillis() - 2000) {
Toast.makeText(this, R.string.double_tap_toast, Toast.LENGTH_SHORT).show();
lastBackButtonPressTime = System.currentTimeMillis();
} else {
super.onBackPressed();
}
break;
default: super.onBackPressed();
}
} }
} }

View File

@ -57,4 +57,14 @@
android:summary="@string/pref_lockscreen_background_sum" android:summary="@string/pref_lockscreen_background_sum"
android:title="@string/pref_lockscreen_background_title"/> android:title="@string/pref_lockscreen_background_title"/>
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory android:title="@string/behavior">
<ListPreference
android:entryValues="@array/back_button_behavior_values"
android:entries="@array/back_button_behavior_options"
android:key="prefBackButtonBehavior"
android:title="@string/pref_back_button_behavior_title"
android:summary="@string/pref_back_button_behavior_sum"
android:defaultValue="default"
app:useStockLayout="true"/>
</PreferenceCategory>
</PreferenceScreen> </PreferenceScreen>

View File

@ -52,6 +52,7 @@ public class UserPreferences {
public static final String PREF_COMPACT_NOTIFICATION_BUTTONS = "prefCompactNotificationButtons"; public static final String PREF_COMPACT_NOTIFICATION_BUTTONS = "prefCompactNotificationButtons";
public static final String PREF_LOCKSCREEN_BACKGROUND = "prefLockscreenBackground"; public static final String PREF_LOCKSCREEN_BACKGROUND = "prefLockscreenBackground";
private static final String PREF_SHOW_DOWNLOAD_REPORT = "prefShowDownloadReport"; private static final String PREF_SHOW_DOWNLOAD_REPORT = "prefShowDownloadReport";
private static final String PREF_BACK_BUTTON_BEHAVIOR = "prefBackButtonBehavior";
// Queue // Queue
private static final String PREF_QUEUE_ADD_TO_FRONT = "prefQueueAddToFront"; private static final String PREF_QUEUE_ADD_TO_FRONT = "prefQueueAddToFront";
@ -809,4 +810,18 @@ public class UserPreferences {
public enum VideoBackgroundBehavior { public enum VideoBackgroundBehavior {
STOP, PICTURE_IN_PICTURE, CONTINUE_PLAYING STOP, PICTURE_IN_PICTURE, CONTINUE_PLAYING
} }
public enum BackButtonBehavior {
DEFAULT, OPEN_DRAWER, DOUBLE_TAP, SHOW_PROMPT
}
public static BackButtonBehavior getBackButtonBehavior() {
switch (prefs.getString(PREF_BACK_BUTTON_BEHAVIOR, "default")) {
case "default": return BackButtonBehavior.DEFAULT;
case "drawer": return BackButtonBehavior.OPEN_DRAWER;
case "doubletap": return BackButtonBehavior.DOUBLE_TAP;
case "prompt": return BackButtonBehavior.SHOW_PROMPT;
default: return BackButtonBehavior.DEFAULT;
}
}
} }

View File

@ -277,4 +277,18 @@
<item>@string/select_all_above</item> <item>@string/select_all_above</item>
<item>@string/select_all_below</item> <item>@string/select_all_below</item>
</string-array> </string-array>
<string-array name="back_button_behavior_options">
<item>@string/back_button_default</item>
<item>@string/back_button_open_drawer</item>
<item>@string/back_button_double_tap</item>
<item>@string/back_button_show_prompt</item>
</string-array>
<string-array name="back_button_behavior_values">
<item>default</item>
<item>drawer</item>
<item>doubletap</item>
<item>prompt</item>
</string-array>
</resources> </resources>

View File

@ -470,6 +470,15 @@
<string name="pref_videoBehavior_sum">Behavior when leaving video playback</string> <string name="pref_videoBehavior_sum">Behavior when leaving video playback</string>
<string name="stop_playback">Stop playback</string> <string name="stop_playback">Stop playback</string>
<string name="continue_playback">Continue audio playback</string> <string name="continue_playback">Continue audio playback</string>
<string name="behavior">Behavior</string>
<string name="pref_back_button_behavior_title">Back button behavior</string>
<string name="pref_back_button_behavior_sum">Change behavior of the back button.</string>
<string name="back_button_default">Default</string>
<string name="back_button_open_drawer">Open navigation drawer</string>
<string name="back_button_double_tap">Double tap to exit</string>
<string name="back_button_show_prompt">Confirm to exit</string>
<string name="close_prompt">Are you sure you want to close AntennaPod?</string>
<string name="double_tap_toast">Tap back button again to exit</string>
<!-- Auto-Flattr dialog --> <!-- Auto-Flattr dialog -->
<string name="auto_flattr_enable">Enable automatic flattring</string> <string name="auto_flattr_enable">Enable automatic flattring</string>