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             {
53                 put(ALBUM_ID_FAVORITES, 0);
54                 put(ALBUM_ID_CAMERA, 1);
55                 put(ALBUM_ID_VIDEOS, 2);
56                 put(ALBUM_ID_SCREENSHOTS, 3);
57                 put(ALBUM_ID_DOWNLOADS, 4);
58             }
59         };
60     }
61 
getPriority(Category category)62     private static int getPriority(Category category) {
63         if (sCategoryPriorityMapping == null) {
64             populateCategoryPriorityMapping();
65         }
66         if (sCategoryPriorityMapping.containsKey(category.getId())) {
67             return sCategoryPriorityMapping.get(category.getId());
68         }
69         return DEFAULT_PRIORITY;
70     }
71 
72     static class CategoryComparator implements java.util.Comparator<Category> {
73         @Override
compare(Category category1, Category category2)74         public int compare(Category category1, Category category2) {
75             return getPriority(category1) - getPriority(category2);
76         }
77     }
78 }
79