aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/tv/tuner/ChannelScanFileParser.java
blob: a255de3e845c990c4c67634aaf95dbbe83ee66b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.tv.tuner;

import android.util.Log;

import com.android.tv.tuner.data.nano.Channel;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Parses plain text formatted scan files, which contain the list of channels.
 */
public class ChannelScanFileParser {
    private static final String TAG = "ChannelScanFileParser";

    public static final class ScanChannel {
        public final int type;
        public final int frequency;
        public final String modulation;
        public final String filename;
        /**
         * Radio frequency (channel) number specified at
         * https://en.wikipedia.org/wiki/North_American_television_frequencies
         * This can be {@code null} for cases like cable signal.
         */
        public final Integer radioFrequencyNumber;

        public static ScanChannel forTuner(int frequency, String modulation,
                Integer radioFrequencyNumber) {
            return new ScanChannel(Channel.TYPE_TUNER, frequency, modulation, null,
                    radioFrequencyNumber);
        }

        public static ScanChannel forFile(int frequency, String filename) {
            return new ScanChannel(Channel.TYPE_FILE, frequency, "file:", filename, null);
        }

        private ScanChannel(int type, int frequency, String modulation, String filename,
                Integer radioFrequencyNumber) {
            this.type = type;
            this.frequency = frequency;
            this.modulation = modulation;
            this.filename = filename;
            this.radioFrequencyNumber = radioFrequencyNumber;
        }
    }

    /**
     * Parses a given scan file and returns the list of {@link ScanChannel} objects.
     *
     * @param is {@link InputStream} of a scan file. Each line matches one channel.
     *           The line format of the scan file is as follows:<br>
     *           "A &lt;frequency&gt; &lt;modulation&gt;".
     * @return a list of {@link ScanChannel} objects parsed
     */
    public static List<ScanChannel> parseScanFile(InputStream is) {
        BufferedReader in = new BufferedReader(new InputStreamReader(is));
        String line;
        List<ScanChannel> scanChannelList = new ArrayList<>();
        try {
            while ((line = in.readLine()) != null) {
                if (line.isEmpty()) {
                    continue;
                }
                if (line.charAt(0) == '#') {
                    // Skip comment line
                    continue;
                }
                String[] tokens = line.split("\\s+");
                if (tokens.length != 3 && tokens.length != 4) {
                    continue;
                }
                scanChannelList.add(ScanChannel.forTuner(Integer.parseInt(tokens[1]), tokens[2],
                        tokens.length == 4 ? Integer.parseInt(tokens[3]) : null));
            }
        } catch (IOException e) {
            Log.e(TAG, "error on parseScanFile()", e);
        }
        return scanChannelList;
    }
}