Merge branch 'fix-603--translate-categories-crash' into 'master'

Pass through a known good `Context` to `translateCategories`

All the good work to make sure that `getActivity()` actually returned
a proper context, obtaining a final reference to that known good activity
object, and then using that in the background thread is thrown away.
The reason was because the `translateCategories` method would go and
call `getActivity()` all over again. This changes the `translateCategories`
helper function so that it asks for a `Context`. This way, a known
good `Context` can be passed in, rather than having to perform a check
to see if `getActivity()` is good again.

Fixes #603 (Hopefully)

See merge request !231
This commit is contained in:
Daniel Martí 2016-03-21 15:08:46 +00:00
commit a61fb677bc

View File

@ -100,7 +100,7 @@ public class AvailableAppsFragment extends AppListFragment implements
protected void onPostExecute(List<String> loadedCategories) {
adapter.clear();
categories = loadedCategories;
ArrayAdapterCompat.addAll(adapter, translateCategories(loadedCategories));
ArrayAdapterCompat.addAll(adapter, translateCategories(activity, loadedCategories));
}
}.execute();
}
@ -114,14 +114,14 @@ public class AvailableAppsFragment extends AppListFragment implements
/**
* Attempt to translate category names with fallback to default name if no translation available
*/
private List<String> translateCategories(List<String> categories) {
private static List<String> translateCategories(Context context, List<String> categories) {
List<String> translatedCategories = new ArrayList<>(categories.size());
Resources res = getResources();
String pkgName = getActivity().getPackageName();
Resources res = context.getResources();
String pkgName = context.getPackageName();
for (String category : categories) {
String resId = category.replace(" & ", "_").replace(" ", "_").replace("'", "");
int id = res.getIdentifier("category_" + resId, "string", pkgName);
translatedCategories.add(id == 0 ? category : getString(id));
translatedCategories.add(id == 0 ? category : context.getString(id));
}
return translatedCategories;
}
@ -134,7 +134,7 @@ public class AvailableAppsFragment extends AppListFragment implements
categories = AppProvider.Helper.categories(getActivity());
ArrayAdapter<String> adapter = new ArrayAdapter<>(
getActivity(), android.R.layout.simple_spinner_item, translateCategories(categories));
getActivity(), android.R.layout.simple_spinner_item, translateCategories(getActivity(), categories));
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
categorySpinner.setAdapter(adapter);