MediaBrowserCompatUtils.java revision c39d9c75590eca86a5e7e32a8824ba04a0d42e9b
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
17package android.support.v4.media;
18
19import android.os.Bundle;
20import android.support.annotation.RestrictTo;
21
22import static android.support.annotation.RestrictTo.Scope.GROUP_ID;
23
24/**
25 * @hide
26 */
27@RestrictTo(GROUP_ID)
28public class MediaBrowserCompatUtils {
29    public static boolean areSameOptions(Bundle options1, Bundle options2) {
30        if (options1 == options2) {
31            return true;
32        } else if (options1 == null) {
33            return options2.getInt(MediaBrowserCompat.EXTRA_PAGE, -1) == -1
34                    && options2.getInt(MediaBrowserCompat.EXTRA_PAGE_SIZE, -1) == -1;
35        } else if (options2 == null) {
36            return options1.getInt(MediaBrowserCompat.EXTRA_PAGE, -1) == -1
37                    && options1.getInt(MediaBrowserCompat.EXTRA_PAGE_SIZE, -1) == -1;
38        } else {
39            return options1.getInt(MediaBrowserCompat.EXTRA_PAGE, -1)
40                    == options2.getInt(MediaBrowserCompat.EXTRA_PAGE, -1)
41                    && options1.getInt(MediaBrowserCompat.EXTRA_PAGE_SIZE, -1)
42                    == options2.getInt(MediaBrowserCompat.EXTRA_PAGE_SIZE, -1);
43        }
44    }
45
46    public static boolean hasDuplicatedItems(Bundle options1, Bundle options2) {
47        int page1 = options1 == null ? -1 : options1.getInt(MediaBrowserCompat.EXTRA_PAGE, -1);
48        int page2 = options2 == null ? -1 :options2.getInt(MediaBrowserCompat.EXTRA_PAGE, -1);
49        int pageSize1 = options1 == null
50                ? -1 :options1.getInt(MediaBrowserCompat.EXTRA_PAGE_SIZE, -1);
51        int pageSize2 = options2 == null
52                ? -1 :options2.getInt(MediaBrowserCompat.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