1/*
2 * Copyright (C) 2015 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 com.android.tv.data;
18
19import android.media.tv.TvInputInfo;
20
21import com.android.tv.util.SetupUtils;
22import com.android.tv.util.TvInputManagerHelper;
23
24import java.util.Comparator;
25
26/**
27 * Compares TV input such that the new input comes first.
28 */
29public class TvInputNewComparator implements Comparator<TvInputInfo> {
30    private final SetupUtils mSetupUtils;
31    private final TvInputManagerHelper mInputManager;
32
33    public TvInputNewComparator(SetupUtils setupUtils, TvInputManagerHelper inputManager) {
34        mSetupUtils = setupUtils;
35        mInputManager = inputManager;
36    }
37
38    @Override
39    public int compare(TvInputInfo lhs, TvInputInfo rhs) {
40        boolean lhsIsNewInput = mSetupUtils.isNewInput(lhs.getId());
41        boolean rhsIsNewInput = mSetupUtils.isNewInput(rhs.getId());
42        if (lhsIsNewInput != rhsIsNewInput) {
43            // New input first.
44            return lhsIsNewInput ? -1 : 1;
45        }
46        if (!lhsIsNewInput) {
47            // Checks only when the inputs are not new.
48            boolean lhsSetupDone = mSetupUtils.isSetupDone(lhs.getId());
49            boolean rhsSetupDone = mSetupUtils.isSetupDone(rhs.getId());
50            if (lhsSetupDone != rhsSetupDone) {
51                // An input which has not been setup comes first.
52                return lhsSetupDone ? 1 : -1;
53            }
54        }
55        return mInputManager.getDefaultTvInputInfoComparator().compare(lhs, rhs);
56    }
57}
58