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
17package com.android.bluetooth.pbapclient;
18
19import android.accounts.Account;
20import android.util.Log;
21
22import com.android.vcard.VCardEntry;
23import com.android.bluetooth.pbapclient.ObexAppParameters;
24
25import java.io.IOException;
26import java.io.InputStream;
27import java.util.ArrayList;
28
29import javax.obex.HeaderSet;
30
31final class BluetoothPbapRequestPullPhoneBook extends BluetoothPbapRequest {
32
33    private static final boolean VDBG = false;
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 != PbapClientConnectionHandler.VCARD_TYPE_21
67                && format != PbapClientConnectionHandler.VCARD_TYPE_30) {
68            format = PbapClientConnectionHandler.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 (VDBG) {
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