From 8e28172942f3dd20aff94e1ea895024f1bcc46a1 Mon Sep 17 00:00:00 2001 From: Peter Serwylo Date: Mon, 21 Mar 2016 20:42:13 +1100 Subject: [PATCH] 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. --- .../views/fragments/AvailableAppsFragment.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) 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 7b4750dc5..24f87f224 100644 --- a/F-Droid/src/org/fdroid/fdroid/views/fragments/AvailableAppsFragment.java +++ b/F-Droid/src/org/fdroid/fdroid/views/fragments/AvailableAppsFragment.java @@ -100,7 +100,7 @@ public class AvailableAppsFragment extends AppListFragment implements protected void onPostExecute(List 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 translateCategories(List categories) { + private static List translateCategories(Context context, List categories) { List 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 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);