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.
This commit is contained in:
Peter Serwylo 2015-04-08 07:36:13 +10:00
parent 51a091fe44
commit 1a114c6c45
2 changed files with 33 additions and 19 deletions

View File

@ -18,6 +18,8 @@
* Split up search terms when querying the app database - "fire fox" now * Split up search terms when querying the app database - "fire fox" now
matches FireFox 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) * Misc fixes to the "swap" workflow (especially on Android 2.3 devices)
### 0.83 (2015-03-26) ### 0.83 (2015-03-26)

View File

@ -41,6 +41,7 @@ public class AvailableAppsFragment extends AppListFragment implements
public static final String CATEGORY_KEY = "Selection"; public static final String CATEGORY_KEY = "Selection";
public static String DEFAULT_CATEGORY; public static String DEFAULT_CATEGORY;
private List<String> categories;
private Spinner categorySpinner; private Spinner categorySpinner;
private String currentCategory = null; private String currentCategory = null;
private AppListAdapter adapter = 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 List<String> translateCategories(List<String> categories) {
private Spinner createCategorySpinner() {
List<String> categories = AppProvider.Helper.categories(getActivity());
// attempt to translate category names with fallback to default name
List<String> translatedCategories = new ArrayList<>(categories.size()); List<String> translatedCategories = new ArrayList<>(categories.size());
Resources res = getResources(); Resources res = getResources();
for (final String category : categories) { for (final String category : categories) {
int id = res.getIdentifier(category.replace(" & ", "_"), "string", getActivity().getPackageName()); int id = res.getIdentifier(category.replace(" & ", "_"), "string", getActivity().getPackageName());
translatedCategories.add(id == 0 ? category : getString(id)); translatedCategories.add(id == 0 ? category : getString(id));
} }
return translatedCategories;
}
categorySpinner = new Spinner(getActivity()); /**
// Giving it an ID lets the default save/restore state * With holo, the menu gets lost since it looks the same as an app list item.
// functionality do its stuff. * Suppress deprecation warnings because:
categorySpinner.setId(R.id.categorySpinner); * * setBackgroundDrawable(Drawable) -> setBackground(Drawable) was only in API 16
// with holo, the menu gets lost since it looks the same as an app list item */
@SuppressWarnings("deprecation")
private void styleSpinner(Spinner spinner) {
if (Build.VERSION.SDK_INT >= 14) { if (Build.VERSION.SDK_INT >= 14) {
Drawable menuButton = getResources().getDrawable(android.R.drawable.btn_dropdown); Drawable menuButton = getResources().getDrawable(android.R.drawable.btn_dropdown);
if (FDroidApp.getCurTheme() == FDroidApp.Theme.dark) { if (FDroidApp.getCurTheme() == FDroidApp.Theme.dark) {
menuButton.setAlpha(32); // make it darker via alpha menuButton.setAlpha(32); // make it darker via alpha
} }
if (Build.VERSION.SDK_INT >= 16) { if (Build.VERSION.SDK_INT >= 16) {
categorySpinner.setBackground(menuButton); spinner.setBackground(menuButton);
} else { } 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<String> adapter = new ArrayAdapter<>( ArrayAdapter<String> adapter = new ArrayAdapter<>(
getActivity(), android.R.layout.simple_spinner_item, translatedCategories); getActivity(), android.R.layout.simple_spinner_item, translateCategories(categories));
adapter.setDropDownViewResource( adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
android.R.layout.simple_spinner_dropdown_item);
categorySpinner.setAdapter(adapter); categorySpinner.setAdapter(adapter);
getActivity().getContentResolver().registerContentObserver( getActivity().getContentResolver().registerContentObserver(
@ -149,7 +160,8 @@ public class AvailableAppsFragment extends AppListFragment implements
@Override @Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
getListView().setSelection(0); getListView().setSelection(0);
setCurrentCategory(categorySpinner.getItemAtPosition(pos).toString()); Log.d(TAG, "Selected category: " + categories.get(pos));
setCurrentCategory(categories.get(pos));
} }
@Override @Override
public void onNothingSelected(AdapterView<?> parent) { public void onNothingSelected(AdapterView<?> parent) {