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