Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#855 Press Back button twice to exit #864

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 124 additions & 3 deletions app/src/main/java/org/gnucash/android/ui/account/AccountsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
Expand All @@ -42,11 +43,13 @@
import android.support.v7.preference.PreferenceManager;
import android.util.Log;
import android.util.SparseArray;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.crashlytics.android.Crashlytics;
Expand Down Expand Up @@ -129,12 +132,39 @@ public class AccountsActivity extends BaseDrawerActivity implements OnAccountCli
*/
private SparseArray<Refreshable> mFragmentPageReferenceMap = new SparseArray<>();

/**
* DoubleBackPressed attributes
*
* @author warrott
*/

// true if backPress button has already been pressed recently
private boolean mDoubleBackButtonPressedOnce;

// Runnable to consider BackPress button not more pressed recently
private final Runnable mResetDoubleBackPressedStatusRunnable = new Runnable() {
@Override
public void run() {

// BackPress button is not more considered pressed recently
mDoubleBackButtonPressedOnce = false;
}
};

// Android handler to delay actions
private Handler mHandler = new Handler();

// Toast
private Toast mToast;
/**
* ViewPager which manages the different tabs
*/
@BindView(R.id.pager) ViewPager mViewPager;
@BindView(R.id.fab_create_account) FloatingActionButton mFloatingActionButton;
@BindView(R.id.coordinatorLayout) CoordinatorLayout mCoordinatorLayout;
@BindView(R.id.pager)
ViewPager mViewPager;
@BindView(R.id.fab_create_account)
FloatingActionButton mFloatingActionButton;
@BindView(R.id.coordinatorLayout)
CoordinatorLayout mCoordinatorLayout;

/**
* Configuration for rating the app
Expand Down Expand Up @@ -221,6 +251,7 @@ public int getTitleRes() {

@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

final Intent intent = getIntent();
Expand Down Expand Up @@ -267,6 +298,18 @@ public void onClick(View v) {
startActivityForResult(addAccountIntent, AccountsActivity.REQUEST_EDIT_ACCOUNT);
}
});

// Prepare a Toast message
mToast = Toast.makeText(getApplicationContext(),
R.string.double_back_press_exit_msg,
Toast.LENGTH_SHORT);

// Align-Center text inside the Toast
TextView toastTextView = (TextView) mToast.getView()
.findViewById(android.R.id.message);
if (toastTextView != null) {
toastTextView.setGravity(Gravity.CENTER);
}
}

@Override
Expand Down Expand Up @@ -344,6 +387,14 @@ private void init() {

//default to using double entry and save the preference explicitly
prefs.edit().putBoolean(getString(R.string.key_use_double_entry), true).apply();

// Default preference to use double back button press to exit
prefs.edit()
.putBoolean(getString(R.string.key_use_double_back_button_press_to_quit),
true)
.apply();

// Finish Activity
finish();
return;
}
Expand All @@ -357,9 +408,79 @@ private void init() {

@Override
protected void onDestroy() {

super.onDestroy();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
preferences.edit().putInt(LAST_OPEN_TAB_INDEX, mViewPager.getCurrentItem()).apply();

//
// Remove callback to avoid memory leak
//

if (mHandler != null) {
// There is an Android handler

mHandler.removeCallbacks(mResetDoubleBackPressedStatusRunnable);
}

}

/**
* Gérer un double BackPressed pour quitter l'application
*/
@Override
public void onBackPressed() {

if (isNavigationViewOpen()) {
// The main navigation menu is open

// Close the main navigation menu
super.onBackPressed();

} else {
// The main navigation menu is closed

// Get Preference about double back button press to exit
boolean prefShallUseDoubleBackPressToExit = PreferenceManager.getDefaultSharedPreferences(this)
.getBoolean(getString(R.string.key_use_double_back_button_press_to_quit),
true);

if (mDoubleBackButtonPressedOnce || !prefShallUseDoubleBackPressToExit) {
// BackPress button has already been pressed recently OR shall not use double back press to exit

//
// Do not show the Toast anymore
//

if (mToast != null) {
// There is a Toast

// Do not show the Toast anymore
mToast.cancel();

} else {
// There is no Toast

// NTD
}

// Perform BackPress
super.onBackPressed();

} else {
// BackPress button has been pressed for the first time AND shall use double back press to exit

// Notice that button has been pressed once
this.mDoubleBackButtonPressedOnce = true;

// Show a message to explain that user must press again to exit
mToast.show();

// After two seconds, it is not more considered as already pressed
mHandler.postDelayed(mResetDoubleBackPressedStatusRunnable,
2000);
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,11 @@ public void onConfigurationChanged(Configuration newConfig) {
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home){
if (!mDrawerLayout.isDrawerOpen(mNavigationView))
if (!isNavigationViewOpen()) {
mDrawerLayout.openDrawer(mNavigationView);
else
mDrawerLayout.closeDrawer(mNavigationView);
} else {
closeNavigationView();
}
return true;
}

Expand Down Expand Up @@ -286,7 +287,7 @@ protected void onDrawerMenuItemClicked(int itemId) {
UserVoice.launchUserVoice(this);
break;
}
mDrawerLayout.closeDrawer(mNavigationView);
closeNavigationView();
}

@Override
Expand Down Expand Up @@ -319,7 +320,7 @@ public boolean onMenuItemClick(MenuItem item) {
Intent intent = new Intent(this, PreferenceActivity.class);
intent.setAction(PreferenceActivity.ACTION_MANAGE_BOOKS);
startActivity(intent);
mDrawerLayout.closeDrawer(mNavigationView);
closeNavigationView();
return true;
}
BooksDbAdapter booksDbAdapter = BooksDbAdapter.getInstance();
Expand All @@ -332,9 +333,17 @@ public boolean onMenuItemClick(MenuItem item) {
return true;
}

public void onClickAppTitle(View view){
protected void onClickAppTitle(View view) {

closeNavigationView();

// Do not launch AccountsActivity to stay on current Activity
// AccountsActivity.start(this);
}

protected void closeNavigationView() {

mDrawerLayout.closeDrawer(mNavigationView);
AccountsActivity.start(this);
}

public void onClickBook(View view){
Expand All @@ -354,4 +363,32 @@ public void onClickBook(View view){

popup.show();
}

@Override
public void onBackPressed() {

if (isNavigationViewOpen()) {
// The main navigation menu is open

// Close the main navigation menu
// mDrawerLayout.closeDrawer(mNavigationView);
onClickAppTitle(getCurrentFocus());

} else {
// The main navigation menu is closed

// Close the Activity
super.onBackPressed();
}
}

/**
* Return true if main navigation menu is open
*
* @return true if main navigation menu is open
*/
protected boolean isNavigationViewOpen() {

return mDrawerLayout.isDrawerOpen(mNavigationView);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,22 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
.putBoolean(getString(R.string.key_use_account_color), Boolean.valueOf(newValue.toString()))
.commit();
}

//
// Preference : use_double_back_button_press_to_quit
//

if (preference.getKey()
.equals(getString(R.string.key_use_double_back_button_press_to_quit))) {

// Store the new value of the Preference
getPreferenceManager().getSharedPreferences()
.edit()
.putBoolean(getString(R.string.key_use_double_back_button_press_to_quit),
Boolean.valueOf(newValue.toString()))
.commit();
}

return true;
}

Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -446,4 +446,8 @@
<string name="label_select_destination_after_export">Sélectionnez la destination une fois l\'exportation terminée</string>
<string name="label_dropbox_export_destination">Exporter dans le dossier \'/Apps/GnuCash Android/\' sur Dropbox</string>
<string name="title_section_preferences">Préférences</string>
<string name="double_back_press_exit_msg">Cliquez à nouveau sur BACK pour Quitter\n(ou modifiez les Préférences Générales)</string>
<string name="prefs_ui_category_title">Préférences d\'ergonomie</string>
<string name="prefs_ui_title_use_double_back_button_press_to_quit">Double confirmation pour Quitter</string>
<string name="prefs_ui_summary_use_double_back_button_press_to_quit">Appuyer deux fois de suite sur le bouton \"BACK\" pour Quitter l\'application</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/donottranslate.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<string name="key_google_drive_app_folder_id" translatable="false">google_drive_app_folder</string>
<string name="key_enable_crashlytics" translatable="false">enable_crashlytics</string>
<string name="key_use_account_color" translatable="false">use_account_color</string>
<string name="key_use_double_back_button_press_to_quit" translatable="false">use_double_back_button_press_to_quit</string>
<string name="key_last_export_destination">last_export_destination</string>
<string name="key_use_compact_list">use_compact_list</string>
<string name="key_prefs_header_general">prefs_header_general</string>
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -500,4 +500,8 @@
<item>Tax</item>
<item>Placeholder</item>
</string-array>
<string name="double_back_press_exit_msg">Please click BACK again to Quit\n(or Change General Preferences)</string>
<string name="prefs_ui_category_title">User Interface Preferences</string>
<string name="prefs_ui_title_use_double_back_button_press_to_quit">Use double back button press to quit</string>
<string name="prefs_ui_summary_use_double_back_button_press_to_quit">Shall click twice on back button to actually quit GnuCashAndroid</string>
</resources>
20 changes: 15 additions & 5 deletions app/src/main/res/xml/fragment_general_preferences.xml
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

<PreferenceCategory android:title="@string/prefs_ui_category_title">
<CheckBoxPreference
android:key="@string/key_use_double_back_button_press_to_quit"
android:title="@string/prefs_ui_title_use_double_back_button_press_to_quit"
android:summary="@string/prefs_ui_summary_use_double_back_button_press_to_quit" />
</PreferenceCategory>

<PreferenceCategory android:title="@string/title_passcode_preferences">
<CheckBoxPreference android:key="@string/key_enable_passcode"
android:title="@string/title_enable_passcode"/>
<Preference android:key="@string/key_change_passcode"
<CheckBoxPreference
android:key="@string/key_enable_passcode"
android:title="@string/title_enable_passcode" />
<Preference
android:key="@string/key_change_passcode"
android:title="@string/title_change_passcode"
android:dependency="@string/key_enable_passcode" />
</PreferenceCategory>

<PreferenceCategory android:title="@string/title_report_prefs">
<CheckBoxPreference android:key="@string/key_use_account_color"
<CheckBoxPreference
android:key="@string/key_use_account_color"
android:title="@string/title_use_account_color"
android:summary="@string/summary_use_account_color" />
</PreferenceCategory>


</PreferenceScreen>
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.2'
classpath 'com.android.tools.build:gradle:3.5.3'
classpath 'io.fabric.tools:gradle:1.31.2'
classpath 'com.stanfy.spoon:spoon-gradle-plugin:1.2.2'
}
Expand Down