1 /*
2  * Copyright (C) 2016 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.bluetooth.pbapclient;
18 
19 import android.util.Log;
20 import android.util.Xml;
21 
22 import org.xmlpull.v1.XmlPullParser;
23 import org.xmlpull.v1.XmlPullParserException;
24 
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.util.ArrayList;
28 
29 class BluetoothPbapVcardListing {
30 
31     private static final String TAG = "BluetoothPbapVcardListing";
32 
33     ArrayList<BluetoothPbapCard> mCards = new ArrayList<BluetoothPbapCard>();
34 
BluetoothPbapVcardListing(InputStream in)35     public BluetoothPbapVcardListing(InputStream in) throws IOException {
36         parse(in);
37     }
38 
parse(InputStream in)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 
getList()63     public ArrayList<BluetoothPbapCard> getList() {
64         return mCards;
65     }
66 }
67