1 /* 2 * Copyright (C) 2017 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.google.android.tv.partner.support; 18 19 import android.support.annotation.VisibleForTesting; 20 import android.text.TextUtils; 21 import android.util.Log; 22 import android.util.Pair; 23 import java.util.ArrayList; 24 import java.util.Arrays; 25 import java.util.Collections; 26 import java.util.Comparator; 27 import java.util.List; 28 import java.util.regex.Pattern; 29 30 /** Utility class for providing tuner setup. */ 31 public class TunerSetupUtils { 32 private static final String TAG = "TunerSetupUtils"; 33 34 private static final Pattern CHANNEL_NUMBER_DELIMITER = Pattern.compile("([ .-])"); 35 lineupChannelMatchCount( List<Lineup> lineups, List<String> localChannels)36 public static List<Pair<Lineup, Integer>> lineupChannelMatchCount( 37 List<Lineup> lineups, List<String> localChannels) { 38 List<Pair<Lineup, Integer>> result = new ArrayList<>(); 39 List<List<String>> parsedLocalChannels = parseChannelNumbers(localChannels); 40 for (Lineup lineup : lineups) { 41 result.add( 42 new Pair<>(lineup, getMatchCount(lineup.getChannels(), parsedLocalChannels))); 43 } 44 // sort in decreasing order 45 Collections.sort( 46 result, 47 new Comparator<Pair<Lineup, Integer>>() { 48 @Override 49 public int compare(Pair<Lineup, Integer> pair, Pair<Lineup, Integer> other) { 50 return Integer.compare(other.second, pair.second); 51 } 52 }); 53 return result; 54 } 55 56 @VisibleForTesting getMatchCount(List<String> lineupChannels, List<List<String>> parsedLocalChannels)57 static int getMatchCount(List<String> lineupChannels, List<List<String>> parsedLocalChannels) { 58 int count = 0; 59 List<List<String>> parsedLineupChannels = parseChannelNumbers(lineupChannels); 60 for (List<String> parsedLineupChannel : parsedLineupChannels) { 61 for (List<String> parsedLocalChannel : parsedLocalChannels) { 62 if (matchChannelNumber(parsedLineupChannel, parsedLocalChannel)) { 63 count++; 64 break; 65 } 66 } 67 } 68 return count; 69 } 70 71 /** 72 * Parses the channel number string to a list of numbers (major number, minor number, etc.). 73 * 74 * @param channelNumber the display number of the channel 75 * @return a list of numbers 76 */ 77 @VisibleForTesting parseChannelNumber(String channelNumber)78 static List<String> parseChannelNumber(String channelNumber) { 79 List<String> numbers = 80 new ArrayList<>( 81 Arrays.asList(TextUtils.split(channelNumber, CHANNEL_NUMBER_DELIMITER))); 82 numbers.removeAll(Collections.singleton("")); 83 if (numbers.size() < 1 || numbers.size() > 2) { 84 Log.w(TAG, "unsupported channel number format: " + channelNumber); 85 return new ArrayList<>(); 86 } 87 return numbers; 88 } 89 90 /** 91 * Parses a list of channel numbers. See {@link #parseChannelNumber(String)}. 92 * 93 * @param channelNumbers a list of channel display numbers 94 */ 95 @VisibleForTesting parseChannelNumbers(List<String> channelNumbers)96 static List<List<String>> parseChannelNumbers(List<String> channelNumbers) { 97 List<List<String>> numbers = new ArrayList<>(channelNumbers.size()); 98 for (String channelNumber : channelNumbers) { 99 if (!TextUtils.isEmpty(channelNumber)) { 100 numbers.add(parseChannelNumber(channelNumber)); 101 } 102 } 103 return numbers; 104 } 105 106 /** 107 * Checks whether two lists of channel numbers match or not. If the sizes are different, 108 * additional elements are ignore. 109 */ 110 @VisibleForTesting matchChannelNumber(List<String> numbers, List<String> other)111 static boolean matchChannelNumber(List<String> numbers, List<String> other) { 112 if (numbers.isEmpty() || other.isEmpty()) { 113 return false; 114 } 115 int i = 0; 116 int j = 0; 117 while (i < numbers.size() && j < other.size()) { 118 if (!numbers.get(i++).equals(other.get(j++))) { 119 return false; 120 } 121 } 122 return true; 123 } 124 } 125