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 {
areSameOptions(Bundle options1, Bundle options2)25     public static boolean areSameOptions(Bundle options1, Bundle options2) {
26         if (options1 == options2) {
27             return true;
28         } else if (options1 == null) {
29             return options2.getInt(MediaBrowser.EXTRA_PAGE, -1) == -1
30                     && options2.getInt(MediaBrowser.EXTRA_PAGE_SIZE, -1) == -1;
31         } else if (options2 == null) {
32             return options1.getInt(MediaBrowser.EXTRA_PAGE, -1) == -1
33                     && options1.getInt(MediaBrowser.EXTRA_PAGE_SIZE, -1) == -1;
34         } else {
35             return options1.getInt(MediaBrowser.EXTRA_PAGE, -1)
36                     == options2.getInt(MediaBrowser.EXTRA_PAGE, -1)
37                     && options1.getInt(MediaBrowser.EXTRA_PAGE_SIZE, -1)
38                     == options2.getInt(MediaBrowser.EXTRA_PAGE_SIZE, -1);
39         }
40     }
41 
hasDuplicatedItems(Bundle options1, Bundle options2)42     public static boolean hasDuplicatedItems(Bundle options1, Bundle options2) {
43         int page1 = options1 == null ? -1 : options1.getInt(MediaBrowser.EXTRA_PAGE, -1);
44         int page2 = options2 == null ? -1 : options2.getInt(MediaBrowser.EXTRA_PAGE, -1);
45         int pageSize1 = options1 == null ? -1 : options1.getInt(MediaBrowser.EXTRA_PAGE_SIZE, -1);
46         int pageSize2 = options2 == null ? -1 : options2.getInt(MediaBrowser.EXTRA_PAGE_SIZE, -1);
47 
48         int startIndex1, startIndex2, endIndex1, endIndex2;
49         if (page1 == -1 || pageSize1 == -1) {
50             startIndex1 = 0;
51             endIndex1 = Integer.MAX_VALUE;
52         } else {
53             startIndex1 = pageSize1 * page1;
54             endIndex1 = startIndex1 + pageSize1 - 1;
55         }
56 
57         if (page2 == -1 || pageSize2 == -1) {
58             startIndex2 = 0;
59             endIndex2 = Integer.MAX_VALUE;
60         } else {
61             startIndex2 = pageSize2 * page2;
62             endIndex2 = startIndex2 + pageSize2 - 1;
63         }
64 
65         if (startIndex1 <= startIndex2 && startIndex2 <= endIndex1) {
66             return true;
67         } else if (startIndex1 <= endIndex2 && endIndex2 <= endIndex1) {
68             return true;
69         }
70         return false;
71     }
72 }
73