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
17package android.bluetooth.client.pbap;
18
19import android.util.Log;
20
21import android.bluetooth.client.pbap.utils.ObexAppParameters;
22import android.bluetooth.client.pbap.BluetoothPbapVcardListing;
23
24import java.io.IOException;
25import java.io.InputStream;
26import java.util.ArrayList;
27
28import javax.obex.HeaderSet;
29
30final class BluetoothPbapRequestPullVcardListing extends BluetoothPbapRequest {
31
32    private static final String TAG = "BluetoothPbapRequestPullVcardListing";
33
34    private static final String TYPE = "x-bt/vcard-listing";
35
36    private BluetoothPbapVcardListing mResponse = null;
37
38    private int mNewMissedCalls = -1;
39
40    public BluetoothPbapRequestPullVcardListing(String folder, byte order, byte searchAttr,
41            String searchVal, int maxListCount, int listStartOffset) {
42
43        if (maxListCount < 0 || maxListCount > 65535) {
44            throw new IllegalArgumentException("maxListCount should be [0..65535]");
45        }
46
47        if (listStartOffset < 0 || listStartOffset > 65535) {
48            throw new IllegalArgumentException("listStartOffset should be [0..65535]");
49        }
50
51        if (folder == null) {
52            folder = "";
53        }
54
55        mHeaderSet.setHeader(HeaderSet.NAME, folder);
56
57        mHeaderSet.setHeader(HeaderSet.TYPE, TYPE);
58
59        ObexAppParameters oap = new ObexAppParameters();
60
61        if (order >= 0) {
62            oap.add(OAP_TAGID_ORDER, order);
63        }
64
65        if (searchVal != null) {
66            oap.add(OAP_TAGID_SEARCH_ATTRIBUTE, searchAttr);
67            oap.add(OAP_TAGID_SEARCH_VALUE, searchVal);
68        }
69
70        /*
71         * maxListCount is a special case which is handled in
72         * BluetoothPbapRequestPullVcardListingSize
73         */
74        if (maxListCount > 0) {
75            oap.add(OAP_TAGID_MAX_LIST_COUNT, (short) maxListCount);
76        }
77
78        if (listStartOffset > 0) {
79            oap.add(OAP_TAGID_LIST_START_OFFSET, (short) listStartOffset);
80        }
81
82        oap.addToHeaderSet(mHeaderSet);
83    }
84
85    @Override
86    protected void readResponse(InputStream stream) throws IOException {
87        Log.v(TAG, "readResponse");
88
89        mResponse = new BluetoothPbapVcardListing(stream);
90    }
91
92    @Override
93    protected void readResponseHeaders(HeaderSet headerset) {
94        Log.v(TAG, "readResponseHeaders");
95
96        ObexAppParameters oap = ObexAppParameters.fromHeaderSet(headerset);
97
98        if (oap.exists(OAP_TAGID_NEW_MISSED_CALLS)) {
99            mNewMissedCalls = oap.getByte(OAP_TAGID_NEW_MISSED_CALLS);
100        }
101    }
102
103    public ArrayList<BluetoothPbapCard> getList() {
104        return mResponse.getList();
105    }
106
107    public int getNewMissedCalls() {
108        return mNewMissedCalls;
109    }
110}
111