Move category helper functions to CategoryProvider
Don't change anything yet, just move them.
This commit is contained in:
		
							parent
							
								
									a7a7f77b42
								
							
						
					
					
						commit
						634fe1084a
					
				@ -10,7 +10,6 @@ import android.text.TextUtils;
 | 
			
		||||
import android.util.Log;
 | 
			
		||||
 | 
			
		||||
import org.fdroid.fdroid.Preferences;
 | 
			
		||||
import org.fdroid.fdroid.R;
 | 
			
		||||
import org.fdroid.fdroid.Utils;
 | 
			
		||||
import org.fdroid.fdroid.data.Schema.ApkTable;
 | 
			
		||||
import org.fdroid.fdroid.data.Schema.AppPrefsTable;
 | 
			
		||||
@ -24,7 +23,6 @@ import org.fdroid.fdroid.data.Schema.RepoTable;
 | 
			
		||||
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.Arrays;
 | 
			
		||||
import java.util.Collections;
 | 
			
		||||
import java.util.HashSet;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.Set;
 | 
			
		||||
@ -94,51 +92,6 @@ public class AppProvider extends FDroidProvider {
 | 
			
		||||
            return apps;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public static String getCategoryAll(Context context) {
 | 
			
		||||
            return context.getString(R.string.category_All);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public static String getCategoryWhatsNew(Context context) {
 | 
			
		||||
            return context.getString(R.string.category_Whats_New);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public static String getCategoryRecentlyUpdated(Context context) {
 | 
			
		||||
            return context.getString(R.string.category_Recently_Updated);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public static List<String> categories(Context context) {
 | 
			
		||||
            final ContentResolver resolver = context.getContentResolver();
 | 
			
		||||
            final Uri uri = getContentUri();
 | 
			
		||||
            final String[] projection = {Cols.Categories.CATEGORIES};
 | 
			
		||||
            final Cursor cursor = resolver.query(uri, projection, null, null, null);
 | 
			
		||||
            final Set<String> categorySet = new HashSet<>();
 | 
			
		||||
            if (cursor != null) {
 | 
			
		||||
                if (cursor.getCount() > 0) {
 | 
			
		||||
                    cursor.moveToFirst();
 | 
			
		||||
                    while (!cursor.isAfterLast()) {
 | 
			
		||||
                        final String categoriesString = cursor.getString(0);
 | 
			
		||||
                        String[] categoriesList = Utils.parseCommaSeparatedString(categoriesString);
 | 
			
		||||
                        if (categoriesList != null) {
 | 
			
		||||
                            Collections.addAll(categorySet, categoriesList);
 | 
			
		||||
                        }
 | 
			
		||||
                        cursor.moveToNext();
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
                cursor.close();
 | 
			
		||||
            }
 | 
			
		||||
            final List<String> categories = new ArrayList<>(categorySet);
 | 
			
		||||
            Collections.sort(categories);
 | 
			
		||||
 | 
			
		||||
            // Populate the category list with the real categories, and the
 | 
			
		||||
            // locally generated meta-categories for "What's New", "Recently
 | 
			
		||||
            // Updated" and "All"...
 | 
			
		||||
            categories.add(0, getCategoryAll(context));
 | 
			
		||||
            categories.add(0, getCategoryRecentlyUpdated(context));
 | 
			
		||||
            categories.add(0, getCategoryWhatsNew(context));
 | 
			
		||||
 | 
			
		||||
            return categories;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public static App findHighestPriorityMetadata(ContentResolver resolver, String packageName) {
 | 
			
		||||
            final Uri uri = getHighestPriorityMetadataUri(packageName);
 | 
			
		||||
            return cursorToApp(resolver.query(uri, Cols.ALL, null, null, null));
 | 
			
		||||
 | 
			
		||||
@ -1,5 +1,6 @@
 | 
			
		||||
package org.fdroid.fdroid.data;
 | 
			
		||||
 | 
			
		||||
import android.content.ContentResolver;
 | 
			
		||||
import android.content.ContentValues;
 | 
			
		||||
import android.content.Context;
 | 
			
		||||
import android.content.UriMatcher;
 | 
			
		||||
@ -7,8 +8,16 @@ import android.database.Cursor;
 | 
			
		||||
import android.net.Uri;
 | 
			
		||||
import android.support.annotation.NonNull;
 | 
			
		||||
 | 
			
		||||
import org.fdroid.fdroid.R;
 | 
			
		||||
import org.fdroid.fdroid.Utils;
 | 
			
		||||
import org.fdroid.fdroid.data.Schema.CategoryTable.Cols;
 | 
			
		||||
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.Collections;
 | 
			
		||||
import java.util.HashSet;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.Set;
 | 
			
		||||
 | 
			
		||||
public class CategoryProvider extends FDroidProvider {
 | 
			
		||||
 | 
			
		||||
    public static final class Helper {
 | 
			
		||||
@ -43,6 +52,51 @@ public class CategoryProvider extends FDroidProvider {
 | 
			
		||||
                cursor.close();
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public static String getCategoryAll(Context context) {
 | 
			
		||||
            return context.getString(R.string.category_All);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public static String getCategoryWhatsNew(Context context) {
 | 
			
		||||
            return context.getString(R.string.category_Whats_New);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public static String getCategoryRecentlyUpdated(Context context) {
 | 
			
		||||
            return context.getString(R.string.category_Recently_Updated);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public static List<String> categories(Context context) {
 | 
			
		||||
            final ContentResolver resolver = context.getContentResolver();
 | 
			
		||||
            final Uri uri = AppProvider.getContentUri();
 | 
			
		||||
            final String[] projection = {Schema.AppMetadataTable.Cols.Categories.CATEGORIES};
 | 
			
		||||
            final Cursor cursor = resolver.query(uri, projection, null, null, null);
 | 
			
		||||
            final Set<String> categorySet = new HashSet<>();
 | 
			
		||||
            if (cursor != null) {
 | 
			
		||||
                if (cursor.getCount() > 0) {
 | 
			
		||||
                    cursor.moveToFirst();
 | 
			
		||||
                    while (!cursor.isAfterLast()) {
 | 
			
		||||
                        final String categoriesString = cursor.getString(0);
 | 
			
		||||
                        String[] categoriesList = Utils.parseCommaSeparatedString(categoriesString);
 | 
			
		||||
                        if (categoriesList != null) {
 | 
			
		||||
                            Collections.addAll(categorySet, categoriesList);
 | 
			
		||||
                        }
 | 
			
		||||
                        cursor.moveToNext();
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
                cursor.close();
 | 
			
		||||
            }
 | 
			
		||||
            final List<String> categories = new ArrayList<>(categorySet);
 | 
			
		||||
            Collections.sort(categories);
 | 
			
		||||
 | 
			
		||||
            // Populate the category list with the real categories, and the
 | 
			
		||||
            // locally generated meta-categories for "What's New", "Recently
 | 
			
		||||
            // Updated" and "All"...
 | 
			
		||||
            categories.add(0, getCategoryAll(context));
 | 
			
		||||
            categories.add(0, getCategoryRecentlyUpdated(context));
 | 
			
		||||
            categories.add(0, getCategoryWhatsNew(context));
 | 
			
		||||
 | 
			
		||||
            return categories;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private class Query extends QueryBuilder {
 | 
			
		||||
 | 
			
		||||
@ -26,6 +26,7 @@ import org.fdroid.fdroid.Utils;
 | 
			
		||||
import org.fdroid.fdroid.compat.ArrayAdapterCompat;
 | 
			
		||||
import org.fdroid.fdroid.compat.CursorAdapterCompat;
 | 
			
		||||
import org.fdroid.fdroid.data.AppProvider;
 | 
			
		||||
import org.fdroid.fdroid.data.CategoryProvider;
 | 
			
		||||
import org.fdroid.fdroid.views.AppListAdapter;
 | 
			
		||||
import org.fdroid.fdroid.views.AvailableAppListAdapter;
 | 
			
		||||
 | 
			
		||||
@ -94,7 +95,7 @@ public class AvailableAppsFragment extends AppListFragment implements
 | 
			
		||||
            new AsyncTask<Void, Void, List<String>>() {
 | 
			
		||||
                @Override
 | 
			
		||||
                protected List<String> doInBackground(Void... params) {
 | 
			
		||||
                    return AppProvider.Helper.categories(activity);
 | 
			
		||||
                    return CategoryProvider.Helper.categories(activity);
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                @Override
 | 
			
		||||
@ -132,7 +133,7 @@ public class AvailableAppsFragment extends AppListFragment implements
 | 
			
		||||
        categorySpinner = spinner;
 | 
			
		||||
        categorySpinner.setId(R.id.category_spinner);
 | 
			
		||||
 | 
			
		||||
        categories = AppProvider.Helper.categories(getActivity());
 | 
			
		||||
        categories = CategoryProvider.Helper.categories(getActivity());
 | 
			
		||||
 | 
			
		||||
        ArrayAdapter<String> adapter = new ArrayAdapter<>(
 | 
			
		||||
                getActivity(), android.R.layout.simple_spinner_item, translateCategories(getActivity(), categories));
 | 
			
		||||
@ -163,20 +164,20 @@ public class AvailableAppsFragment extends AppListFragment implements
 | 
			
		||||
 | 
			
		||||
        categoryWrapper = view.findViewById(R.id.category_wrapper);
 | 
			
		||||
        setupCategorySpinner((Spinner) view.findViewById(R.id.category_spinner));
 | 
			
		||||
        defaultCategory = AppProvider.Helper.getCategoryWhatsNew(getActivity());
 | 
			
		||||
        defaultCategory = CategoryProvider.Helper.getCategoryWhatsNew(getActivity());
 | 
			
		||||
 | 
			
		||||
        return view;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    protected Uri getDataUri() {
 | 
			
		||||
        if (currentCategory == null || currentCategory.equals(AppProvider.Helper.getCategoryAll(getActivity()))) {
 | 
			
		||||
        if (currentCategory == null || currentCategory.equals(CategoryProvider.Helper.getCategoryAll(getActivity()))) {
 | 
			
		||||
            return AppProvider.getContentUri();
 | 
			
		||||
        }
 | 
			
		||||
        if (currentCategory.equals(AppProvider.Helper.getCategoryRecentlyUpdated(getActivity()))) {
 | 
			
		||||
        if (currentCategory.equals(CategoryProvider.Helper.getCategoryRecentlyUpdated(getActivity()))) {
 | 
			
		||||
            return AppProvider.getRecentlyUpdatedUri();
 | 
			
		||||
        }
 | 
			
		||||
        if (currentCategory.equals(AppProvider.Helper.getCategoryWhatsNew(getActivity()))) {
 | 
			
		||||
        if (currentCategory.equals(CategoryProvider.Helper.getCategoryWhatsNew(getActivity()))) {
 | 
			
		||||
            return AppProvider.getNewlyAddedUri();
 | 
			
		||||
        }
 | 
			
		||||
        return AppProvider.getCategoryUri(currentCategory);
 | 
			
		||||
 | 
			
		||||
@ -93,7 +93,7 @@ public class CategoryProviderTest extends FDroidProviderTest {
 | 
			
		||||
        insertAppWithCategory("com.rock", "Rock", "Mineral");
 | 
			
		||||
        insertAppWithCategory("com.banana", "Banana", "Vegetable");
 | 
			
		||||
 | 
			
		||||
        List<String> categories = AppProvider.Helper.categories(context);
 | 
			
		||||
        List<String> categories = CategoryProvider.Helper.categories(context);
 | 
			
		||||
        String[] expected = new String[] {
 | 
			
		||||
                context.getResources().getString(R.string.category_Whats_New),
 | 
			
		||||
                context.getResources().getString(R.string.category_Recently_Updated),
 | 
			
		||||
@ -111,7 +111,7 @@ public class CategoryProviderTest extends FDroidProviderTest {
 | 
			
		||||
        insertAppWithCategory("com.dog.rock.apple", "Dog-Rock-Apple", "Animal,Mineral,Vegetable");
 | 
			
		||||
        insertAppWithCategory("com.banana.apple", "Banana", "Vegetable,Vegetable");
 | 
			
		||||
 | 
			
		||||
        List<String> categories = AppProvider.Helper.categories(context);
 | 
			
		||||
        List<String> categories = CategoryProvider.Helper.categories(context);
 | 
			
		||||
        String[] expected = new String[] {
 | 
			
		||||
                context.getResources().getString(R.string.category_Whats_New),
 | 
			
		||||
                context.getResources().getString(R.string.category_Recently_Updated),
 | 
			
		||||
@ -127,7 +127,7 @@ public class CategoryProviderTest extends FDroidProviderTest {
 | 
			
		||||
                "Running,Shooting,Jumping,Bleh,Sneh,Pleh,Blah,Test category," +
 | 
			
		||||
                "The quick brown fox jumps over the lazy dog,With apostrophe's");
 | 
			
		||||
 | 
			
		||||
        List<String> categoriesLonger = AppProvider.Helper.categories(context);
 | 
			
		||||
        List<String> categoriesLonger = CategoryProvider.Helper.categories(context);
 | 
			
		||||
        String[] expectedLonger = new String[] {
 | 
			
		||||
                context.getResources().getString(R.string.category_Whats_New),
 | 
			
		||||
                context.getResources().getString(R.string.category_Recently_Updated),
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user