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