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