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