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 import android.util.Log;
     21 
     22 import com.android.vcard.VCardEntry;
     23 
     24 import java.io.IOException;
     25 import java.io.InputStream;
     26 import java.util.ArrayList;
     27 
     28 import javax.obex.HeaderSet;
     29 
     30 final class BluetoothPbapRequestPullPhoneBook extends BluetoothPbapRequest {
     31 
     32     private static final boolean VDBG = false;
     33 
     34     private static final String TAG = "BluetoothPbapRequestPullPhoneBook";
     35 
     36     private static final String TYPE = "x-bt/phonebook";
     37 
     38     private BluetoothPbapVcardList mResponse;
     39 
     40     private Account mAccount;
     41 
     42     private int mNewMissedCalls = -1;
     43 
     44     private final byte mFormat;
     45 
     46     BluetoothPbapRequestPullPhoneBook(String pbName, Account account, long filter, byte format,
     47             int maxListCount, int listStartOffset) {
     48         mAccount = account;
     49         if (maxListCount < 0 || maxListCount > 65535) {
     50             throw new IllegalArgumentException("maxListCount should be [0..65535]");
     51         }
     52 
     53         if (listStartOffset < 0 || listStartOffset > 65535) {
     54             throw new IllegalArgumentException("listStartOffset should be [0..65535]");
     55         }
     56 
     57         mHeaderSet.setHeader(HeaderSet.NAME, pbName);
     58 
     59         mHeaderSet.setHeader(HeaderSet.TYPE, TYPE);
     60 
     61         ObexAppParameters oap = new ObexAppParameters();
     62 
     63         /* make sure format is one of allowed values */
     64         if (format != PbapClientConnectionHandler.VCARD_TYPE_21
     65                 && format != PbapClientConnectionHandler.VCARD_TYPE_30) {
     66             format = PbapClientConnectionHandler.VCARD_TYPE_21;
     67         }
     68 
     69         if (filter != 0) {
     70             oap.add(OAP_TAGID_FILTER, filter);
     71         }
     72 
     73         oap.add(OAP_TAGID_FORMAT, format);
     74 
     75         /*
     76          * maxListCount is a special case which is handled in
     77          * BluetoothPbapRequestPullPhoneBookSize
     78          */
     79         if (maxListCount > 0) {
     80             oap.add(OAP_TAGID_MAX_LIST_COUNT, (short) maxListCount);
     81         } else {
     82             oap.add(OAP_TAGID_MAX_LIST_COUNT, (short) 65535);
     83         }
     84 
     85         if (listStartOffset > 0) {
     86             oap.add(OAP_TAGID_LIST_START_OFFSET, (short) listStartOffset);
     87         }
     88 
     89         oap.addToHeaderSet(mHeaderSet);
     90 
     91         mFormat = format;
     92     }
     93 
     94     @Override
     95     protected void readResponse(InputStream stream) throws IOException {
     96         Log.v(TAG, "readResponse");
     97 
     98         mResponse = new BluetoothPbapVcardList(mAccount, stream, mFormat);
     99         if (VDBG) {
    100             Log.d(TAG, "Read " + mResponse.getCount() + " entries.");
    101         }
    102     }
    103 
    104     @Override
    105     protected void readResponseHeaders(HeaderSet headerset) {
    106         Log.v(TAG, "readResponseHeaders");
    107 
    108         ObexAppParameters oap = ObexAppParameters.fromHeaderSet(headerset);
    109 
    110         if (oap.exists(OAP_TAGID_NEW_MISSED_CALLS)) {
    111             mNewMissedCalls = oap.getByte(OAP_TAGID_NEW_MISSED_CALLS);
    112         }
    113     }
    114 
    115     public ArrayList<VCardEntry> getList() {
    116         return mResponse.getList();
    117     }
    118 
    119     public int getNewMissedCalls() {
    120         return mNewMissedCalls;
    121     }
    122 }
    123