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 /**
22  * @hide
23  */
24 public class MediaBrowserUtils {
25     /**
26      * Compares whether two bundles are the same.
27      */
areSameOptions(Bundle options1, Bundle options2)28     public static boolean areSameOptions(Bundle options1, Bundle options2) {
29         if (options1 == options2) {
30             return true;
31         } else if (options1 == null) {
32             return options2.getInt(MediaBrowser.EXTRA_PAGE, -1) == -1
33                     && options2.getInt(MediaBrowser.EXTRA_PAGE_SIZE, -1) == -1;
34         } else if (options2 == null) {
35             return options1.getInt(MediaBrowser.EXTRA_PAGE, -1) == -1
36                     && options1.getInt(MediaBrowser.EXTRA_PAGE_SIZE, -1) == -1;
37         } else {
38             return options1.getInt(MediaBrowser.EXTRA_PAGE, -1)
39                     == options2.getInt(MediaBrowser.EXTRA_PAGE, -1)
40                     && options1.getInt(MediaBrowser.EXTRA_PAGE_SIZE, -1)
41                     == options2.getInt(MediaBrowser.EXTRA_PAGE_SIZE, -1);
42         }
43     }
44 
45     /**
46      * Returnes true if the page options has duplicated items.
47      */
hasDuplicatedItems(Bundle options1, Bundle options2)48     public static boolean hasDuplicatedItems(Bundle options1, Bundle options2) {
49         int page1 = options1 == null ? -1 : options1.getInt(MediaBrowser.EXTRA_PAGE, -1);
50         int page2 = options2 == null ? -1 : options2.getInt(MediaBrowser.EXTRA_PAGE, -1);
51         int pageSize1 = options1 == null ? -1 : options1.getInt(MediaBrowser.EXTRA_PAGE_SIZE, -1);
52         int pageSize2 = options2 == null ? -1 : options2.getInt(MediaBrowser.EXTRA_PAGE_SIZE, -1);
53 
54         int startIndex1, startIndex2, endIndex1, endIndex2;
55         if (page1 == -1 || pageSize1 == -1) {
56             startIndex1 = 0;
57             endIndex1 = Integer.MAX_VALUE;
58         } else {
59             startIndex1 = pageSize1 * page1;
60             endIndex1 = startIndex1 + pageSize1 - 1;
61         }
62 
63         if (page2 == -1 || pageSize2 == -1) {
64             startIndex2 = 0;
65             endIndex2 = Integer.MAX_VALUE;
66         } else {
67             startIndex2 = pageSize2 * page2;
68             endIndex2 = startIndex2 + pageSize2 - 1;
69         }
70 
71         if (startIndex1 <= startIndex2 && startIndex2 <= endIndex1) {
72             return true;
73         } else if (startIndex1 <= endIndex2 && endIndex2 <= endIndex1) {
74             return true;
75         }
76         return false;
77     }
78 }
79