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.setup;
18 
19 import android.util.Log;
20 import com.android.tv.tuner.api.ScanChannel;
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 final class ChannelScanFileParser {
30     private static final String TAG = "ChannelScanFileParser";
31 
32     /**
33      * Parses a given scan file and returns the list of {@link ScanChannel} objects.
34      *
35      * @param is {@link InputStream} of a scan file. Each line matches one channel. The line format
36      *     of the scan file is as follows:<br>
37      *     "A &lt;frequency&gt; &lt;modulation&gt;".
38      * @return a list of {@link ScanChannel} objects parsed
39      */
parseScanFile(InputStream is)40     public static List<ScanChannel> parseScanFile(InputStream is) {
41         BufferedReader in = new BufferedReader(new InputStreamReader(is));
42         String line;
43         List<ScanChannel> scanChannelList = new ArrayList<>();
44         try {
45             while ((line = in.readLine()) != null) {
46                 if (line.isEmpty()) {
47                     continue;
48                 }
49                 if (line.charAt(0) == '#') {
50                     // Skip comment line
51                     continue;
52                 }
53                 String[] tokens = line.split("\\s+");
54                 if (tokens.length != 3 && tokens.length != 4) {
55                     continue;
56                 }
57                 scanChannelList.add(
58                         ScanChannel.forTuner(
59                                 tokens[0],
60                                 Integer.parseInt(tokens[1]),
61                                 tokens[2],
62                                 tokens.length == 4 ? Integer.parseInt(tokens[3]) : null));
63             }
64         } catch (IOException e) {
65             Log.e(TAG, "error on parseScanFile()", e);
66         }
67         return scanChannelList;
68     }
69 
ChannelScanFileParser()70    private ChannelScanFileParser(){}
71 }
72