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

Commit 69b0c65b authored by Kai Wang's avatar Kai Wang Committed by android-build-merger
Browse files

Merge "PBAP update TIMES_CONTACTS after pull CallLog"

am: 7bfb1e39

Change-Id: Ibf10f3f73888aded042d690c703f322e04aa4650
parents db0dc011 7bfb1e39
Loading
Loading
Loading
Loading
+45 −4
Original line number Diff line number Diff line
@@ -19,8 +19,11 @@ import android.content.ContentProviderOperation;
import android.content.ContentValues;
import android.content.Context;
import android.content.OperationApplicationException;
import android.database.Cursor;
import android.net.Uri;
import android.os.RemoteException;
import android.provider.CallLog;
import android.provider.ContactsContract;
import android.util.Log;
import android.util.Pair;

@@ -30,6 +33,7 @@ import com.android.vcard.VCardEntry.PhoneData;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class CallLogPullRequest extends PullRequest {
@@ -39,11 +43,14 @@ public class CallLogPullRequest extends PullRequest {
    private static final String TIMESTAMP_PROPERTY = "X-IRMC-CALL-DATETIME";
    private static final String TIMESTAMP_FORMAT = "yyyyMMdd'T'HHmmss";


    private Context mContext;
    private HashMap<String, Integer> mCallCounter;

    public CallLogPullRequest(Context context, String path) {
    public CallLogPullRequest(Context context, String path, HashMap<String, Integer> map) {
        mContext = context;
        this.path = path;
        mCallCounter = map;
    }

    @Override
@@ -82,9 +89,15 @@ public class CallLogPullRequest extends PullRequest {
                if (phones == null || phones.get(0).getNumber().equals(";")) {
                    values.put(CallLog.Calls.NUMBER, "");
                } else {
                    values.put(CallLog.Calls.NUMBER, phones.get(0).getNumber());
                    String phoneNumber = phones.get(0).getNumber();
                    values.put(CallLog.Calls.NUMBER, phoneNumber);
                    if (mCallCounter.get(phoneNumber) != null) {
                        int updateCounter = mCallCounter.get(phoneNumber) + 1;
                        mCallCounter.put(phoneNumber, updateCounter);
                    } else {
                        mCallCounter.put(phoneNumber, 1);
                    }
                }

                List<Pair<String, String>> irmc = vcard.getUnknownXData();
                SimpleDateFormat parser = new SimpleDateFormat(TIMESTAMP_FORMAT);
                if (irmc != null) {
@@ -101,7 +114,6 @@ public class CallLogPullRequest extends PullRequest {
                        }
                    }
                }

                ops.add(ContentProviderOperation.newInsert(CallLog.Calls.CONTENT_URI)
                        .withValues(values)
                        .withYieldAllowed(true)
@@ -109,6 +121,10 @@ public class CallLogPullRequest extends PullRequest {
            }
            mContext.getContentResolver().applyBatch(CallLog.AUTHORITY, ops);
            Log.d(TAG, "Updated call logs.");
            //OUTGOING_TYPE is the last callLog we fetched.
            if (type == CallLog.Calls.OUTGOING_TYPE) {
                updateTimesContacted();
            }
        } catch (RemoteException | OperationApplicationException e) {
            Log.d(TAG, "Failed to update call log for path=" + path, e);
        } finally {
@@ -117,4 +133,29 @@ public class CallLogPullRequest extends PullRequest {
            }
        }
    }

    private void updateTimesContacted() {
        for (String key : mCallCounter.keySet()) {
            ContentValues values = new ContentValues();
            values.put(ContactsContract.RawContacts.TIMES_CONTACTED, mCallCounter.get(key));
            Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
                    Uri.encode(key));
            Cursor c = mContext.getContentResolver().query(uri, null, null, null);
            if (c != null && c.getCount() > 0) {
                c.moveToNext();
                String contactId = c.getString(c.getColumnIndex(
                        ContactsContract.PhoneLookup.CONTACT_ID));
                if (VDBG) {
                    Log.d(TAG, "onPullComplete: ID " + contactId + " key : " + key);
                }
                String where = ContactsContract.RawContacts.CONTACT_ID + "=" + contactId;
                mContext.getContentResolver().update(
                        ContactsContract.RawContacts.CONTENT_URI, values, where, null);
            }
        }
        if (DBG) {
            Log.d(TAG, "Updated TIMES_CONTACTED");
        }
    }

}
+8 −6
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ import com.android.bluetooth.BluetoothObexTransport;
import com.android.bluetooth.R;

import java.io.IOException;
import java.util.HashMap;

import javax.obex.ClientSession;
import javax.obex.HeaderSet;
@@ -96,6 +97,7 @@ class PbapClientConnectionHandler extends Handler {
    public static final String MCH_PATH = "telecom/mch.vcf";
    public static final String ICH_PATH = "telecom/ich.vcf";
    public static final String OCH_PATH = "telecom/och.vcf";

    public static final byte VCARD_TYPE_21 = 0;
    public static final byte VCARD_TYPE_30 = 1;

@@ -251,10 +253,10 @@ class PbapClientConnectionHandler extends Handler {
                                    mAccount);
                    processor.setResults(request.getList());
                    processor.onPullComplete();

                    downloadCallLog(MCH_PATH);
                    downloadCallLog(ICH_PATH);
                    downloadCallLog(OCH_PATH);
                    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());
                }
@@ -362,13 +364,13 @@ class PbapClientConnectionHandler extends Handler {
        }
    }

    void downloadCallLog(String path) {
    void downloadCallLog(String path, HashMap<String, Integer> callCounter) {
        try {
            BluetoothPbapRequestPullPhoneBook request =
                    new BluetoothPbapRequestPullPhoneBook(path, mAccount, 0, VCARD_TYPE_30, 0, 0);
            request.execute(mObexSession);
            CallLogPullRequest processor =
                    new CallLogPullRequest(mPbapClientStateMachine.getContext(), path);
                    new CallLogPullRequest(mPbapClientStateMachine.getContext(), path, callCounter);
            processor.setResults(request.getList());
            processor.onPullComplete();
        } catch (IOException e) {
+7 −3
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ import org.junit.runner.RunWith;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.TimeZone;

@MediumTest
@@ -68,7 +69,8 @@ public class PbapParserTest {
                PbapClientConnectionHandler.VCARD_TYPE_30);
        Assert.assertEquals(1, pbapVCardList.getCount());
        CallLogPullRequest processor =
                new CallLogPullRequest(mTargetContext, PbapClientConnectionHandler.MCH_PATH);
                new CallLogPullRequest(mTargetContext, PbapClientConnectionHandler.MCH_PATH,
                    new HashMap<>());
        processor.setResults(pbapVCardList.getList());

        // Verify that these entries aren't in the call log to start.
@@ -89,7 +91,8 @@ public class PbapParserTest {
                PbapClientConnectionHandler.VCARD_TYPE_30);
        Assert.assertEquals(1, pbapVCardList.getCount());
        CallLogPullRequest processor =
                new CallLogPullRequest(mTargetContext, PbapClientConnectionHandler.MCH_PATH);
                new CallLogPullRequest(mTargetContext, PbapClientConnectionHandler.MCH_PATH,
                    new HashMap<>());
        processor.setResults(pbapVCardList.getList());

        // Verify that these entries aren't in the call log to start.
@@ -109,7 +112,8 @@ public class PbapParserTest {
                PbapClientConnectionHandler.VCARD_TYPE_30);
        Assert.assertEquals(2, pbapVCardList.getCount());
        CallLogPullRequest processor =
                new CallLogPullRequest(mTargetContext, PbapClientConnectionHandler.MCH_PATH);
                new CallLogPullRequest(mTargetContext, PbapClientConnectionHandler.MCH_PATH,
                    new HashMap<>());
        processor.setResults(pbapVCardList.getList());

        // Verify that these entries aren't in the call log to start.