From 1a114c6c4557f890eda135846668c6c00123e02d Mon Sep 17 00:00:00 2001 From: Peter Serwylo Date: Wed, 8 Apr 2015 07:36:13 +1000 Subject: [PATCH] Show app categories correctly. Fixes #214. The categories were previously selected in the Spinner, then passed directly to the database. Once the categories became translated, this meant we were sending the translated category to the database. However, the database only knows about English categories, and so we instead need to look up the English translation for the selected category before passing to the database. This is done by keeping a list of original category names which is indexed the same as the translated ones. --- CHANGELOG.md | 2 + .../fragments/AvailableAppsFragment.java | 50 ++++++++++++------- 2 files changed, 33 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 725458595..bf5c824af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,8 @@ * Split up search terms when querying the app database - "fire fox" now matches FireFox +* Fixed bug where categories were always empty on non-english locales + * Misc fixes to the "swap" workflow (especially on Android 2.3 devices) ### 0.83 (2015-03-26) diff --git a/F-Droid/src/org/fdroid/fdroid/views/fragments/AvailableAppsFragment.java b/F-Droid/src/org/fdroid/fdroid/views/fragments/AvailableAppsFragment.java index 66cc470a5..351406e15 100644 --- a/F-Droid/src/org/fdroid/fdroid/views/fragments/AvailableAppsFragment.java +++ b/F-Droid/src/org/fdroid/fdroid/views/fragments/AvailableAppsFragment.java @@ -41,6 +41,7 @@ public class AvailableAppsFragment extends AppListFragment implements public static final String CATEGORY_KEY = "Selection"; public static String DEFAULT_CATEGORY; + private List categories; private Spinner categorySpinner; private String currentCategory = null; private AppListAdapter adapter = null; @@ -103,43 +104,53 @@ public class AvailableAppsFragment extends AppListFragment implements } } - /* Suppress deprecation warnings because: - * * setBackgroundDrawable(Drawable) -> setBackground(Drawable) was only in API 16 + /** + * Attempt to translate category names with fallback to default name if no translation available */ - @SuppressWarnings("deprecation") - private Spinner createCategorySpinner() { - - List categories = AppProvider.Helper.categories(getActivity()); - - // attempt to translate category names with fallback to default name + private List translateCategories(List categories) { List translatedCategories = new ArrayList<>(categories.size()); Resources res = getResources(); for (final String category : categories) { int id = res.getIdentifier(category.replace(" & ", "_"), "string", getActivity().getPackageName()); translatedCategories.add(id == 0 ? category : getString(id)); } + return translatedCategories; + } - categorySpinner = new Spinner(getActivity()); - // Giving it an ID lets the default save/restore state - // functionality do its stuff. - categorySpinner.setId(R.id.categorySpinner); - // with holo, the menu gets lost since it looks the same as an app list item + /** + * With holo, the menu gets lost since it looks the same as an app list item. + * Suppress deprecation warnings because: + * * setBackgroundDrawable(Drawable) -> setBackground(Drawable) was only in API 16 + */ + @SuppressWarnings("deprecation") + private void styleSpinner(Spinner spinner) { if (Build.VERSION.SDK_INT >= 14) { Drawable menuButton = getResources().getDrawable(android.R.drawable.btn_dropdown); if (FDroidApp.getCurTheme() == FDroidApp.Theme.dark) { menuButton.setAlpha(32); // make it darker via alpha } if (Build.VERSION.SDK_INT >= 16) { - categorySpinner.setBackground(menuButton); + spinner.setBackground(menuButton); } else { - categorySpinner.setBackgroundDrawable(menuButton); + spinner.setBackgroundDrawable(menuButton); } } + } + + private Spinner createCategorySpinner() { + + categories = AppProvider.Helper.categories(getActivity()); + + categorySpinner = new Spinner(getActivity()); + + // Giving it an ID lets the default save/restore state functionality do its stuff. + categorySpinner.setId(R.id.categorySpinner); + + styleSpinner(categorySpinner); ArrayAdapter adapter = new ArrayAdapter<>( - getActivity(), android.R.layout.simple_spinner_item, translatedCategories); - adapter.setDropDownViewResource( - android.R.layout.simple_spinner_dropdown_item); + getActivity(), android.R.layout.simple_spinner_item, translateCategories(categories)); + adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); categorySpinner.setAdapter(adapter); getActivity().getContentResolver().registerContentObserver( @@ -149,7 +160,8 @@ public class AvailableAppsFragment extends AppListFragment implements @Override public void onItemSelected(AdapterView parent, View view, int pos, long id) { getListView().setSelection(0); - setCurrentCategory(categorySpinner.getItemAtPosition(pos).toString()); + Log.d(TAG, "Selected category: " + categories.get(pos)); + setCurrentCategory(categories.get(pos)); } @Override public void onNothingSelected(AdapterView parent) {