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
17package com.android.usbtuner;
18
19import android.util.Log;
20
21import com.android.usbtuner.data.Channel;
22
23import java.io.BufferedReader;
24import java.io.IOException;
25import java.io.InputStream;
26import java.io.InputStreamReader;
27import java.util.ArrayList;
28import java.util.List;
29
30/**
31 * Parses plain text formatted scan files, which contain the list of channels.
32 */
33public 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        public static ScanChannel forTuner(int frequency, String modulation) {
43            return new ScanChannel(Channel.TYPE_TUNER, frequency, modulation, null);
44        }
45
46        public static ScanChannel forFile(int frequency, String filename) {
47            return new ScanChannel(Channel.TYPE_FILE, frequency, "file:", filename);
48        }
49
50        private ScanChannel(int type, int frequency, String modulation, String filename) {
51            this.type = type;
52            this.frequency = frequency;
53            this.modulation = modulation;
54            this.filename = filename;
55        }
56    }
57
58    /**
59     * Parses a given scan file and returns the list of {@link ScanChannel} objects.
60     *
61     * @param is {@link InputStream} of a scan file. Each line matches one channel.
62     *           The line format of the scan file is as follows:<br>
63     *           "A &lt;frequency&gt; &lt;modulation&gt;".
64     * @return a list of {@link ScanChannel} objects parsed
65     */
66    public static List<ScanChannel> parseScanFile(InputStream is) {
67        BufferedReader in = new BufferedReader(new InputStreamReader(is));
68        String line;
69        List<ScanChannel> scanChannelList = new ArrayList<>();
70        try {
71            while ((line = in.readLine()) != null) {
72                if (line.isEmpty()) {
73                    continue;
74                }
75                if (line.charAt(0) == '#') {
76                    // Skip comment line
77                    continue;
78                }
79                String[] tokens = line.split("\\s+");
80                if (tokens.length != 3) {
81                    continue;
82                }
83                if (!tokens[0].equals("A")) {
84                    // Only support ATSC
85                    continue;
86                }
87                scanChannelList.add(ScanChannel.forTuner(Integer.parseInt(tokens[1]), tokens[2]));
88            }
89        } catch (IOException e) {
90            Log.e(TAG, "error on parseScanFile()", e);
91        }
92        return scanChannelList;
93    }
94}
95