1 /*
2  * Copyright (C) 2016 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 android.media.browse;
18 
19 import android.os.Bundle;
20 
21 import java.util.Collections;
22 import java.util.List;
23 
24 /**
25  * @hide
26  */
27 public class MediaBrowserUtils {
28     /**
29      * Compares whether two bundles are the same.
30      */
areSameOptions(Bundle options1, Bundle options2)31     public static boolean areSameOptions(Bundle options1, Bundle options2) {
32         if (options1 == options2) {
33             return true;
34         } else if (options1 == null) {
35             return options2.getInt(MediaBrowser.EXTRA_PAGE, -1) == -1
36                     && options2.getInt(MediaBrowser.EXTRA_PAGE_SIZE, -1) == -1;
37         } else if (options2 == null) {
38             return options1.getInt(MediaBrowser.EXTRA_PAGE, -1) == -1
39                     && options1.getInt(MediaBrowser.EXTRA_PAGE_SIZE, -1) == -1;
40         } else {
41             return options1.getInt(MediaBrowser.EXTRA_PAGE, -1)
42                     == options2.getInt(MediaBrowser.EXTRA_PAGE, -1)
43                     && options1.getInt(MediaBrowser.EXTRA_PAGE_SIZE, -1)
44                     == options2.getInt(MediaBrowser.EXTRA_PAGE_SIZE, -1);
45         }
46     }
47 
48     /**
49      * Returnes true if the page options has duplicated items.
50      */
hasDuplicatedItems(Bundle options1, Bundle options2)51     public static boolean hasDuplicatedItems(Bundle options1, Bundle options2) {
52         int page1 = options1 == null ? -1 : options1.getInt(MediaBrowser.EXTRA_PAGE, -1);
53         int page2 = options2 == null ? -1 : options2.getInt(MediaBrowser.EXTRA_PAGE, -1);
54         int pageSize1 = options1 == null ? -1 : options1.getInt(MediaBrowser.EXTRA_PAGE_SIZE, -1);
55         int pageSize2 = options2 == null ? -1 : options2.getInt(MediaBrowser.EXTRA_PAGE_SIZE, -1);
56 
57         int startIndex1, startIndex2, endIndex1, endIndex2;
58         if (page1 == -1 || pageSize1 == -1) {
59             startIndex1 = 0;
60             endIndex1 = Integer.MAX_VALUE;
61         } else {
62             startIndex1 = pageSize1 * page1;
63             endIndex1 = startIndex1 + pageSize1 - 1;
64         }
65 
66         if (page2 == -1 || pageSize2 == -1) {
67             startIndex2 = 0;
68             endIndex2 = Integer.MAX_VALUE;
69         } else {
70             startIndex2 = pageSize2 * page2;
71             endIndex2 = startIndex2 + pageSize2 - 1;
72         }
73 
74         if (startIndex1 <= startIndex2 && startIndex2 <= endIndex1) {
75             return true;
76         } else if (startIndex1 <= endIndex2 && endIndex2 <= endIndex1) {
77             return true;
78         }
79         return false;
80     }
81 
82     /**
83      * Returns a paged version of the given {@code list}, using the paging parameters in {@code
84      * options}.
85      */
applyPagingOptions( List<MediaBrowser.MediaItem> list, final Bundle options)86     public static List<MediaBrowser.MediaItem> applyPagingOptions(
87             List<MediaBrowser.MediaItem> list, final Bundle options) {
88         if (list == null) {
89             return null;
90         }
91         int page = options.getInt(MediaBrowser.EXTRA_PAGE, -1);
92         int pageSize = options.getInt(MediaBrowser.EXTRA_PAGE_SIZE, -1);
93         if (page == -1 && pageSize == -1) {
94             return list;
95         }
96         int fromIndex = pageSize * page;
97         int toIndex = fromIndex + pageSize;
98         if (page < 0 || pageSize < 1 || fromIndex >= list.size()) {
99             return Collections.EMPTY_LIST;
100         }
101         if (toIndex > list.size()) {
102             toIndex = list.size();
103         }
104         return list.subList(fromIndex, toIndex);
105     }
106 }
107