Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6b1de62b authored by Andrew Cheng's avatar Andrew Cheng Committed by android-build-merger
Browse files

Merge "Splitting over-the-air downloading into batches" am: 89a3f052 am:...

Merge "Splitting over-the-air downloading into batches" am: 89a3f052 am: b200b59f am: 14dc5485
am: 82e556ea

Change-Id: I14dcf500578ee42206e4086657b5c401b1f0f5f7
parents 215ccfa3 82e556ea
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -73,7 +73,7 @@ final class BluetoothPbapRequestPullPhoneBook extends BluetoothPbapRequest {
        oap.add(OAP_TAGID_FORMAT, format);

        /*
         * maxListCount is a special case which is handled in
         * maxListCount == 0 is a special case which is handled in
         * BluetoothPbapRequestPullPhoneBookSize
         */
        if (maxListCount > 0) {
+66 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.bluetooth.pbapclient;

import android.util.Log;

import javax.obex.HeaderSet;

final class BluetoothPbapRequestPullPhoneBookSize extends BluetoothPbapRequest {

    private static final boolean VDBG = Utils.VDBG;

    private static final String TAG = "BtPbapReqPullPhoneBookSize";

    private static final String TYPE = "x-bt/phonebook";

    private int mSize;

    BluetoothPbapRequestPullPhoneBookSize(String pbName, long filter) {
        mHeaderSet.setHeader(HeaderSet.NAME, pbName);

        mHeaderSet.setHeader(HeaderSet.TYPE, TYPE);

        ObexAppParameters oap = new ObexAppParameters();
        // Set MaxListCount in the request to 0 to get PhonebookSize in the response.
        // If a vCardSelector is present in the request, then the result shall
        // contain the number of items that satisfy the selector’s criteria.
        // See PBAP v1.2.3, Sec. 5.1.4.5.
        oap.add(OAP_TAGID_MAX_LIST_COUNT, (short) 0);
        if (filter != 0) {
            oap.add(OAP_TAGID_FILTER, filter);
        }
        oap.addToHeaderSet(mHeaderSet);
    }

    @Override
    protected void readResponseHeaders(HeaderSet headerset) {
        if (VDBG) {
            Log.v(TAG, "readResponseHeaders");
        }

        ObexAppParameters oap = ObexAppParameters.fromHeaderSet(headerset);

        if (oap.exists(OAP_TAGID_PHONEBOOK_SIZE)) {
            mSize = oap.getShort(OAP_TAGID_PHONEBOOK_SIZE);
        }
    }

    public int getSize() {
        return mSize;
    }
}
+60 −23
Original line number Diff line number Diff line
@@ -46,6 +46,15 @@ import javax.obex.ResponseCodes;
 * controlling state machine.
 */
class PbapClientConnectionHandler extends Handler {
    // Tradeoff: larger BATCH_SIZE leads to faster download rates, while smaller
    // BATCH_SIZE is less prone to IO Exceptions if there is a download in
    // progress when Bluetooth stack is torn down.
    private static final int DEFAULT_BATCH_SIZE = 250;

    // Upper limit on the indices of the vcf cards/entries, inclusive,
    // i.e., valid indices are [0, 1, ... , UPPER_LIMIT]
    private static final int UPPER_LIMIT = 65535;

    static final String TAG = "PbapClientConnHandler";
    static final boolean DBG = Utils.DBG;
    static final boolean VDBG = Utils.VDBG;
@@ -239,29 +248,12 @@ class PbapClientConnectionHandler extends Handler {
                break;

            case MSG_DOWNLOAD:
                try {
                    mAccountCreated = addAccount(mAccount);
                    if (!mAccountCreated) {
                        Log.e(TAG, "Account creation failed.");
                        return;
                    }
                    // Start at contact 1 to exclued Owner Card PBAP 1.1 sec 3.1.5.2
                    BluetoothPbapRequestPullPhoneBook request =
                            new BluetoothPbapRequestPullPhoneBook(PB_PATH, mAccount,
                                    PBAP_REQUESTED_FIELDS, VCARD_TYPE_30, 0, 1);
                    request.execute(mObexSession);
                    PhonebookPullRequest processor =
                            new PhonebookPullRequest(mPbapClientStateMachine.getContext(),
                                    mAccount);
                    processor.setResults(request.getList());
                    processor.onPullComplete();
                downloadContacts();

                HashMap<String, Integer> callCounter = new HashMap<>();
                downloadCallLog(MCH_PATH, callCounter);
                downloadCallLog(ICH_PATH, callCounter);
                downloadCallLog(OCH_PATH, callCounter);
                } catch (IOException e) {
                    Log.w(TAG, "DOWNLOAD_CONTACTS Failure" + e.toString());
                }
                break;

            default:
@@ -366,6 +358,51 @@ class PbapClientConnectionHandler extends Handler {
        }
    }

    void downloadContacts() {
        try {
            mAccountCreated = addAccount(mAccount);
            if (!mAccountCreated) {
                Log.e(TAG, "Account creation failed.");
                return;
            }
            PhonebookPullRequest processor =
                    new PhonebookPullRequest(mPbapClientStateMachine.getContext(),
                            mAccount);

            // Download contacts in batches of size DEFAULT_BATCH_SIZE
            BluetoothPbapRequestPullPhoneBookSize requestPbSize =
                    new BluetoothPbapRequestPullPhoneBookSize(PB_PATH,
                            PBAP_REQUESTED_FIELDS);
            requestPbSize.execute(mObexSession);

            // "-1" because Owner Card is also included in PhoneBookSize
            int numberOfContactsRemaining = requestPbSize.getSize() - 1;

            // Start at contact 1 to exclude Owner Card PBAP 1.1 sec 3.1.5.2
            int startOffset = 1;
            while ((numberOfContactsRemaining > 0) && (startOffset <= UPPER_LIMIT)) {
                int numberOfContactsToDownload =
                        Math.min(Math.min(DEFAULT_BATCH_SIZE, numberOfContactsRemaining),
                        UPPER_LIMIT - startOffset + 1);
                BluetoothPbapRequestPullPhoneBook request =
                        new BluetoothPbapRequestPullPhoneBook(PB_PATH, mAccount,
                                PBAP_REQUESTED_FIELDS, VCARD_TYPE_30,
                                numberOfContactsToDownload, startOffset);
                request.execute(mObexSession);
                processor.setResults(request.getList());
                processor.onPullComplete();

                startOffset += numberOfContactsToDownload;
                numberOfContactsRemaining -= numberOfContactsToDownload;
            }
            if ((startOffset > UPPER_LIMIT) && (numberOfContactsRemaining > 0)) {
                Log.w(TAG, "Download contacts incomplete, index exceeded upper limit.");
            }
        } catch (IOException e) {
            Log.w(TAG, "Download contacts failure" + e.toString());
        }
    }

    void downloadCallLog(String path, HashMap<String, Integer> callCounter) {
        try {
            BluetoothPbapRequestPullPhoneBook request =