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.tuner; 18 19 import android.util.Log; 20 import com.android.tv.tuner.data.nano.Channel; 21 import java.io.BufferedReader; 22 import java.io.IOException; 23 import java.io.InputStream; 24 import java.io.InputStreamReader; 25 import java.util.ArrayList; 26 import java.util.List; 27 28 /** Parses plain text formatted scan files, which contain the list of channels. */ 29 public class ChannelScanFileParser { 30 private static final String TAG = "ChannelScanFileParser"; 31 32 public static final class ScanChannel { 33 public final int type; 34 public final int frequency; 35 public final String modulation; 36 public final String filename; 37 /** 38 * Radio frequency (channel) number specified at 39 * https://en.wikipedia.org/wiki/North_American_television_frequencies This can be {@code 40 * null} for cases like cable signal. 41 */ 42 public final Integer radioFrequencyNumber; 43 44 public static ScanChannel forTuner( 45 int frequency, String modulation, Integer radioFrequencyNumber) { 46 return new ScanChannel( 47 Channel.TunerType.TYPE_TUNER, 48 frequency, 49 modulation, 50 null, 51 radioFrequencyNumber); 52 } 53 54 public static ScanChannel forFile(int frequency, String filename) { 55 return new ScanChannel(Channel.TunerType.TYPE_FILE, frequency, "file:", filename, null); 56 } 57 58 private ScanChannel( 59 int type, 60 int frequency, 61 String modulation, 62 String filename, 63 Integer radioFrequencyNumber) { 64 this.type = type; 65 this.frequency = frequency; 66 this.modulation = modulation; 67 this.filename = filename; 68 this.radioFrequencyNumber = radioFrequencyNumber; 69 } 70 } 71 72 /** 73 * Parses a given scan file and returns the list of {@link ScanChannel} objects. 74 * 75 * @param is {@link InputStream} of a scan file. Each line matches one channel. The line format 76 * of the scan file is as follows:<br> 77 * "A <frequency> <modulation>". 78 * @return a list of {@link ScanChannel} objects parsed 79 */ 80 public static List<ScanChannel> parseScanFile(InputStream is) { 81 BufferedReader in = new BufferedReader(new InputStreamReader(is)); 82 String line; 83 List<ScanChannel> scanChannelList = new ArrayList<>(); 84 try { 85 while ((line = in.readLine()) != null) { 86 if (line.isEmpty()) { 87 continue; 88 } 89 if (line.charAt(0) == '#') { 90 // Skip comment line 91 continue; 92 } 93 String[] tokens = line.split("\\s+"); 94 if (tokens.length != 3 && tokens.length != 4) { 95 continue; 96 } 97 scanChannelList.add( 98 ScanChannel.forTuner( 99 Integer.parseInt(tokens[1]), 100 tokens[2], 101 tokens.length == 4 ? Integer.parseInt(tokens[3]) : null)); 102 } 103 } catch (IOException e) { 104 Log.e(TAG, "error on parseScanFile()", e); 105 } 106 return scanChannelList; 107 } 108 } 109