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