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.bluetooth.client.pbap;
18
19import android.util.Log;
20import android.util.Xml;
21
22import org.xmlpull.v1.XmlPullParser;
23import org.xmlpull.v1.XmlPullParserException;
24
25import java.io.IOException;
26import java.io.InputStream;
27import java.util.ArrayList;
28
29class BluetoothPbapVcardListing {
30
31    private static final String TAG = "BluetoothPbapVcardListing";
32
33    ArrayList<BluetoothPbapCard> mCards = new ArrayList<BluetoothPbapCard>();
34
35    public BluetoothPbapVcardListing(InputStream in) throws IOException {
36        parse(in);
37    }
38
39    private void parse(InputStream in) throws IOException {
40        XmlPullParser parser = Xml.newPullParser();
41
42        try {
43            parser.setInput(in, "UTF-8");
44
45            int eventType = parser.getEventType();
46
47            while (eventType != XmlPullParser.END_DOCUMENT) {
48
49                if (eventType == XmlPullParser.START_TAG && parser.getName().equals("card")) {
50                    BluetoothPbapCard card = new BluetoothPbapCard(
51                            parser.getAttributeValue(null, "handle"),
52                            parser.getAttributeValue(null, "name"));
53                    mCards.add(card);
54                }
55
56                eventType = parser.next();
57            }
58        } catch (XmlPullParserException e) {
59            Log.e(TAG, "XML parser error when parsing XML", e);
60        }
61    }
62
63    public ArrayList<BluetoothPbapCard> getList() {
64        return mCards;
65    }
66}
67