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;
26
27import javax.obex.HeaderSet;
28import javax.obex.ResponseCodes;
29
30final class BluetoothPbapRequestPullVcardEntry extends BluetoothPbapRequest {
31
32    private static final String TAG = "BluetoothPbapRequestPullVcardEntry";
33
34    private static final String TYPE = "x-bt/vcard";
35
36    private BluetoothPbapVcardList mResponse;
37
38    private final byte mFormat;
39
40    public BluetoothPbapRequestPullVcardEntry(String handle, long filter, byte format) {
41        mHeaderSet.setHeader(HeaderSet.NAME, handle);
42
43        mHeaderSet.setHeader(HeaderSet.TYPE, TYPE);
44
45        /* make sure format is one of allowed values */
46        if (format != BluetoothPbapClient.VCARD_TYPE_21
47                && format != BluetoothPbapClient.VCARD_TYPE_30) {
48            format = BluetoothPbapClient.VCARD_TYPE_21;
49        }
50
51        ObexAppParameters oap = new ObexAppParameters();
52
53        if (filter != 0) {
54            oap.add(OAP_TAGID_FILTER, filter);
55        }
56
57        oap.add(OAP_TAGID_FORMAT, format);
58        oap.addToHeaderSet(mHeaderSet);
59
60        mFormat = format;
61    }
62
63    @Override
64    protected void readResponse(InputStream stream) throws IOException {
65        Log.v(TAG, "readResponse");
66
67        mResponse = new BluetoothPbapVcardList(stream, mFormat);
68    }
69    @Override
70    protected void checkResponseCode(int responseCode) throws IOException {
71        Log.v(TAG, "checkResponseCode");
72
73        if (mResponse.getCount() == 0) {
74            if (responseCode != ResponseCodes.OBEX_HTTP_NOT_FOUND) {
75                throw new IOException("Invalid response received");
76            } else {
77                Log.v(TAG, "Vcard Entry not found");
78            }
79        }
80    }
81
82    public VCardEntry getVcard() {
83        return mResponse.getFirst();
84    }
85}
86