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