Home | History | Annotate | Download | only in pbap
      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 
     17 package android.bluetooth.client.pbap;
     18 
     19 import com.android.vcard.VCardEntry;
     20 import com.android.vcard.VCardEntryConstructor;
     21 import com.android.vcard.VCardEntryCounter;
     22 import com.android.vcard.VCardEntryHandler;
     23 import com.android.vcard.VCardParser;
     24 import com.android.vcard.VCardParser_V21;
     25 import com.android.vcard.VCardParser_V30;
     26 import com.android.vcard.exception.VCardException;
     27 
     28 import java.io.IOException;
     29 import java.io.InputStream;
     30 import java.util.ArrayList;
     31 
     32 class BluetoothPbapVcardList {
     33 
     34     private final ArrayList<VCardEntry> mCards = new ArrayList<VCardEntry>();
     35 
     36     class CardEntryHandler implements VCardEntryHandler {
     37         @Override
     38         public void onStart() {
     39         }
     40 
     41         @Override
     42         public void onEntryCreated(VCardEntry entry) {
     43             mCards.add(entry);
     44         }
     45 
     46         @Override
     47         public void onEnd() {
     48         }
     49     }
     50 
     51     public BluetoothPbapVcardList(InputStream in, byte format) throws IOException {
     52         parse(in, format);
     53     }
     54 
     55     private void parse(InputStream in, byte format) throws IOException {
     56         VCardParser parser;
     57 
     58         if (format == BluetoothPbapClient.VCARD_TYPE_30) {
     59             parser = new VCardParser_V30();
     60         } else {
     61             parser = new VCardParser_V21();
     62         }
     63 
     64         VCardEntryConstructor constructor = new VCardEntryConstructor();
     65         VCardEntryCounter counter = new VCardEntryCounter();
     66         CardEntryHandler handler = new CardEntryHandler();
     67 
     68         constructor.addEntryHandler(handler);
     69 
     70         parser.addInterpreter(constructor);
     71         parser.addInterpreter(counter);
     72 
     73         try {
     74             parser.parse(in);
     75         } catch (VCardException e) {
     76             e.printStackTrace();
     77         }
     78     }
     79 
     80     public int getCount() {
     81         return mCards.size();
     82     }
     83 
     84     public ArrayList<VCardEntry> getList() {
     85         return mCards;
     86     }
     87 
     88     public VCardEntry getFirst() {
     89         return mCards.get(0);
     90     }
     91 }
     92