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