1/*
2 * Copyright (C) 2014 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 android.alsa;
18
19import android.util.Slog;
20import java.io.BufferedReader;
21import java.io.File;
22import java.io.FileNotFoundException;
23import java.io.FileReader;
24import java.io.IOException;
25import java.util.Vector;
26
27/**
28 * @hide Retrieves information from an ALSA "cards" file.
29 */
30public class AlsaCardsParser {
31    private static final String TAG = "AlsaCardsParser";
32
33    private static LineTokenizer tokenizer_ = new LineTokenizer(" :[]");
34
35    public class AlsaCardRecord {
36        public int mCardNum = -1;
37        public String mField1 = "";
38        public String mCardName = "";
39        public String mCardDescription = "";
40
41        public AlsaCardRecord() {}
42
43        public boolean parse(String line, int lineIndex) {
44            int tokenIndex = 0;
45            int delimIndex = 0;
46            if (lineIndex == 0) {
47                // line # (skip)
48                tokenIndex = tokenizer_.nextToken(line, tokenIndex);
49                delimIndex = tokenizer_.nextDelimiter(line, tokenIndex);
50
51                // mField1
52                tokenIndex = tokenizer_.nextToken(line, delimIndex);
53                delimIndex = tokenizer_.nextDelimiter(line, tokenIndex);
54                mField1 = line.substring(tokenIndex, delimIndex);
55
56                // mCardName
57                tokenIndex = tokenizer_.nextToken(line, delimIndex);
58                // delimIndex = tokenizer_.nextDelimiter(line, tokenIndex);
59                mCardName = line.substring(tokenIndex);
60                // done
61              } else if (lineIndex == 1) {
62                  tokenIndex = tokenizer_.nextToken(line, 0);
63                  if (tokenIndex != -1) {
64                      mCardDescription = line.substring(tokenIndex);
65                  }
66            }
67
68            return true;
69        }
70
71        public String textFormat() {
72          return mCardName + " : " + mCardDescription;
73        }
74    }
75
76    private Vector<AlsaCardRecord> cardRecords_ = new Vector<AlsaCardRecord>();
77
78    public void scan() {
79          cardRecords_.clear();
80          final String cardsFilePath = "/proc/asound/cards";
81          File cardsFile = new File(cardsFilePath);
82          try {
83              FileReader reader = new FileReader(cardsFile);
84              BufferedReader bufferedReader = new BufferedReader(reader);
85              String line = "";
86              while ((line = bufferedReader.readLine()) != null) {
87                  AlsaCardRecord cardRecord = new AlsaCardRecord();
88                  cardRecord.parse(line, 0);
89                  cardRecord.parse(line = bufferedReader.readLine(), 1);
90                  cardRecords_.add(cardRecord);
91              }
92              reader.close();
93          } catch (FileNotFoundException e) {
94              e.printStackTrace();
95          } catch (IOException e) {
96              e.printStackTrace();
97          }
98      }
99
100      public AlsaCardRecord getCardRecordAt(int index) {
101          return cardRecords_.get(index);
102      }
103
104      public int getNumCardRecords() {
105          return cardRecords_.size();
106      }
107
108    public void Log() {
109      int numCardRecs = getNumCardRecords();
110      for (int index = 0; index < numCardRecs; ++index) {
111          Slog.w(TAG, "usb:" + getCardRecordAt(index).textFormat());
112      }
113    }
114
115    public AlsaCardsParser() {}
116}
117