From fd5e4c464d6c1c09e3024598230c3d77c6f2f310 Mon Sep 17 00:00:00 2001 From: Ngewi Fet Date: Mon, 20 Aug 2012 17:03:03 +0200 Subject: [PATCH] Fixed: crash when clicking home button in SettingsActivity.java in API level 10 and below Show preference setting in summary Set ActionBar title of preference screens --- GnucashMobile/res/menu/account_actions.xml | 2 +- GnucashMobile/res/values/strings.xml | 10 ++- .../res/xml/fragment_about_preferences.xml | 2 +- .../res/xml/fragment_general_preferences.xml | 2 +- .../android/ui/accounts/AccountsActivity.java | 2 +- .../android/ui/settings/SettingsActivity.java | 83 +++++++++++++++---- 6 files changed, 79 insertions(+), 22 deletions(-) diff --git a/GnucashMobile/res/menu/account_actions.xml b/GnucashMobile/res/menu/account_actions.xml index 724d56294..48b1106ed 100644 --- a/GnucashMobile/res/menu/account_actions.xml +++ b/GnucashMobile/res/menu/account_actions.xml @@ -22,7 +22,7 @@ android:showAsAction="always"/> diff --git a/GnucashMobile/res/values/strings.xml b/GnucashMobile/res/values/strings.xml index 1428f5620..0a6ae79a9 100644 --- a/GnucashMobile/res/values/strings.xml +++ b/GnucashMobile/res/values/strings.xml @@ -58,7 +58,7 @@ Export Delete after export All exported transactions will be deleted when exporting is completed - + Settings Share file… SD Card @@ -68,7 +68,7 @@ Destination Account Access SD Card Share OFX export with… - default_currency + default_currency Cannot move transactions.\nThe destination account uses a different currency from origin account General About @@ -452,13 +452,15 @@ Build version 1.0.beta1 License - Details to the application license + Apache License v2.0. Click for details + General app_license Select Account There are no transactions available to export About GnucashMobile - About GnucashMobile + About Gnucash GnucashMobile is a mobile finance expense tracker application for Android.\nIt enables flexible tracking of expenses on-the-go which can be exported to the Open Financial eXchange (OFX) format and imported into Gnucash for the desktop. + About \ No newline at end of file diff --git a/GnucashMobile/res/xml/fragment_about_preferences.xml b/GnucashMobile/res/xml/fragment_about_preferences.xml index cc212c38e..b7fc5da7f 100644 --- a/GnucashMobile/res/xml/fragment_about_preferences.xml +++ b/GnucashMobile/res/xml/fragment_about_preferences.xml @@ -16,7 +16,7 @@ --> - + diff --git a/GnucashMobile/res/xml/fragment_general_preferences.xml b/GnucashMobile/res/xml/fragment_general_preferences.xml index d1288c4a2..0062606a9 100644 --- a/GnucashMobile/res/xml/fragment_general_preferences.xml +++ b/GnucashMobile/res/xml/fragment_general_preferences.xml @@ -16,7 +16,7 @@ --> - + \ No newline at end of file diff --git a/GnucashMobile/src/org/gnucash/android/ui/accounts/AccountsActivity.java b/GnucashMobile/src/org/gnucash/android/ui/accounts/AccountsActivity.java index 7e7c67b9e..9c4453e33 100644 --- a/GnucashMobile/src/org/gnucash/android/ui/accounts/AccountsActivity.java +++ b/GnucashMobile/src/org/gnucash/android/ui/accounts/AccountsActivity.java @@ -92,7 +92,7 @@ public void onCreate(Bundle savedInstanceState) { setContentView(R.layout.activity_accounts); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); - String currencyCode = prefs.getString(getString(R.string.pref_default_currency), + String currencyCode = prefs.getString(getString(R.string.key_default_currency), Currency.getInstance(Locale.getDefault()).getCurrencyCode()); Money.DEFAULT_CURRENCY_CODE = currencyCode; diff --git a/GnucashMobile/src/org/gnucash/android/ui/settings/SettingsActivity.java b/GnucashMobile/src/org/gnucash/android/ui/settings/SettingsActivity.java index aecf8b59b..a938aa6d7 100644 --- a/GnucashMobile/src/org/gnucash/android/ui/settings/SettingsActivity.java +++ b/GnucashMobile/src/org/gnucash/android/ui/settings/SettingsActivity.java @@ -19,10 +19,15 @@ import java.util.List; import org.gnucash.android.R; +import org.gnucash.android.data.Money; +import android.content.SharedPreferences; import android.os.Build; import android.os.Bundle; +import android.preference.Preference; +import android.preference.Preference.OnPreferenceChangeListener; import android.preference.PreferenceFragment; +import android.preference.PreferenceManager; import com.actionbarsherlock.app.ActionBar; import com.actionbarsherlock.app.SherlockPreferenceActivity; @@ -33,7 +38,7 @@ * @author Ngewi Fet * */ -public class SettingsActivity extends SherlockPreferenceActivity{ +public class SettingsActivity extends SherlockPreferenceActivity implements OnPreferenceChangeListener{ /** * Constructs the headers to display in the header list when the Settings activity is first opened @@ -49,25 +54,32 @@ public void onBuildHeaders(List
target) { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - getSupportActionBar().setHomeButtonEnabled(true); - getSupportActionBar().setDisplayHomeAsUpEnabled(true); + ActionBar actionBar = getSupportActionBar(); + actionBar.setTitle(R.string.title_settings); + actionBar.setHomeButtonEnabled(true); + actionBar.setDisplayHomeAsUpEnabled(true); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB){ addPreferencesFromResource(R.xml.fragment_general_preferences); addPreferencesFromResource(R.xml.fragment_about_preferences); - } + setDefaultCurrencyListener(); + } } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { - case android.R.id.home: - android.app.FragmentManager fm = getFragmentManager(); - if (fm.getBackStackEntryCount() > 0) { - fm.popBackStack(); - } else { - finish(); - } + case android.R.id.home: + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { + android.app.FragmentManager fm = getFragmentManager(); + if (fm.getBackStackEntryCount() > 0) { + fm.popBackStack(); + } else { + finish(); + } + } else { + finish(); + } return true; default: @@ -75,26 +87,68 @@ public boolean onOptionsItemSelected(MenuItem item) { } } + + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + preference.setSummary(newValue.toString()); + if (preference.getKey().equals(getString(R.string.key_default_currency))){ + Money.DEFAULT_CURRENCY_CODE = newValue.toString(); + } + return true; + } + + private void setDefaultCurrencyListener() { + SharedPreferences manager = PreferenceManager.getDefaultSharedPreferences(this); + String defaultCurrency = manager.getString(getString(R.string.key_default_currency), Money.DEFAULT_CURRENCY_CODE); + @SuppressWarnings("deprecation") + Preference pref = findPreference(getString(R.string.key_default_currency)); + pref.setSummary(defaultCurrency); + pref.setOnPreferenceChangeListener(this); + } + /** * Fragment for displaying general preferences * @author Ngewi Fet * */ - public static class GeneralPreferenceFragment extends PreferenceFragment{ + public static class GeneralPreferenceFragment extends PreferenceFragment implements OnPreferenceChangeListener{ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + addPreferencesFromResource(R.xml.fragment_general_preferences); ActionBar actionBar = ((SherlockPreferenceActivity) getActivity()).getSupportActionBar(); actionBar.setHomeButtonEnabled(true); actionBar.setDisplayHomeAsUpEnabled(true); - } + actionBar.setTitle(R.string.title_general_prefs); + + } + + @Override + public void onResume() { + super.onResume(); + SharedPreferences manager = PreferenceManager.getDefaultSharedPreferences(getActivity()); + String defaultCurrency = manager.getString(getString(R.string.key_default_currency), Money.DEFAULT_CURRENCY_CODE); + Preference pref = findPreference(getString(R.string.key_default_currency)); + pref.setSummary(defaultCurrency); + pref.setOnPreferenceChangeListener(this); + } + + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + preference.setSummary(newValue.toString()); + if (preference.getKey().equals(getString(R.string.key_default_currency))){ + Money.DEFAULT_CURRENCY_CODE = newValue.toString(); + } + return true; + } + } /** * Fragment for displaying information about the application - * @author Ngewi + * @author Ngewi Fet * */ public static class AboutPreferenceFragment extends PreferenceFragment{ @@ -106,6 +160,7 @@ public void onCreate(Bundle savedInstanceState) { ActionBar actionBar = ((SherlockPreferenceActivity) getActivity()).getSupportActionBar(); actionBar.setHomeButtonEnabled(true); actionBar.setDisplayHomeAsUpEnabled(true); + actionBar.setTitle(R.string.title_about_gnucash); } }