• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.providers.media.photopicker.util;
18 
19 import static android.provider.CloudMediaProviderContract.AlbumColumns.ALBUM_ID_CAMERA;
20 import static android.provider.CloudMediaProviderContract.AlbumColumns.ALBUM_ID_DOWNLOADS;
21 import static android.provider.CloudMediaProviderContract.AlbumColumns.ALBUM_ID_FAVORITES;
22 import static android.provider.CloudMediaProviderContract.AlbumColumns.ALBUM_ID_SCREENSHOTS;
23 import static android.provider.CloudMediaProviderContract.AlbumColumns.ALBUM_ID_VIDEOS;
24 
25 import com.android.providers.media.photopicker.data.model.Category;
26 
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30 
31 /**
32  * Reorders categories per requirements.
33  */
34 public class CategoryOrganiserUtils {
35     static final int DEFAULT_PRIORITY = 100;
36     static Map<String, Integer> sCategoryPriorityMapping;
37 
38     /**
39      * Rearranges categoryList in the required order: Favourites, camera, videos,
40      * screenshots, downloads, ... cloud albums ordered by last modified time stamp.
41      */
getReorganisedCategoryList(List<Category> categoryList)42     public static void getReorganisedCategoryList(List<Category> categoryList) {
43         // Items having the same priority will not be modified in order.
44         categoryList.sort(new CategoryComparator());
45     }
46 
populateCategoryPriorityMapping()47     private static void populateCategoryPriorityMapping() {
48 
49         // DO NOT ALTER THIS ORDER.
50         // These priorities decide the order in which the categories will be displayed on UI.
51         sCategoryPriorityMapping = new HashMap<String, Integer>();
52         sCategoryPriorityMapping.put(ALBUM_ID_FAVORITES, 0);
53         sCategoryPriorityMapping.put(ALBUM_ID_CAMERA, 1);
54         sCategoryPriorityMapping.put(ALBUM_ID_VIDEOS, 2);
55         sCategoryPriorityMapping.put(ALBUM_ID_SCREENSHOTS, 3);
56         sCategoryPriorityMapping.put(ALBUM_ID_DOWNLOADS, 4);
57     }
58 
getPriority(Category category)59     private static int getPriority(Category category) {
60         if (sCategoryPriorityMapping == null) {
61             populateCategoryPriorityMapping();
62         }
63         if (sCategoryPriorityMapping.containsKey(category.getId())) {
64             return sCategoryPriorityMapping.get(category.getId());
65         }
66         return DEFAULT_PRIORITY;
67     }
68 
69     static class CategoryComparator implements java.util.Comparator<Category> {
70         @Override
compare(Category category1, Category category2)71         public int compare(Category category1, Category category2) {
72             return getPriority(category1) - getPriority(category2);
73         }
74     }
75 }
76