CallLogPullRequest.java revision 18ef7045b04916a7b29b50371de6b1b3efb0cbe9
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 */
16package com.android.bluetooth.pbapclient;
17
18import android.content.ContentProviderOperation;
19import android.content.ContentValues;
20import android.content.Context;
21import android.content.OperationApplicationException;
22import android.os.RemoteException;
23import android.provider.CallLog;
24import android.util.Log;
25import android.util.Pair;
26
27import com.android.vcard.VCardEntry;
28import com.android.vcard.VCardEntry.PhoneData;
29
30import java.text.ParseException;
31import java.text.SimpleDateFormat;
32import java.util.ArrayList;
33import java.util.Date;
34import java.util.List;
35
36public class CallLogPullRequest extends PullRequest {
37    private static boolean DBG = true;
38    private static boolean VDBG = false;
39    private static String TAG = "PbapCallLogPullRequest";
40    private static final String TIMESTAMP_PROPERTY = "X-IRMC-CALL-DATETIME";
41    private static final String TIMESTAMP_FORMAT = "yyyyMMdd'T'HHmmss";
42
43    private Context mContext;
44
45    public CallLogPullRequest(Context context, String path) {
46        mContext = context;
47        this.path = path;
48    }
49
50    @Override
51    public void onPullComplete() {
52        if (mEntries == null) {
53            Log.e(TAG, "onPullComplete entries is null.");
54            return;
55        }
56
57        if (DBG) {
58            Log.d(TAG, "onPullComplete");
59            if (VDBG) {
60                Log.d(TAG, " with " + mEntries.size() + " count.");
61            }
62        }
63        int type;
64        try {
65            if (path.equals(PbapClientConnectionHandler.ICH_PATH)) {
66                type = CallLog.Calls.INCOMING_TYPE;
67            } else if (path.equals(PbapClientConnectionHandler.OCH_PATH)) {
68                type = CallLog.Calls.OUTGOING_TYPE;
69            } else if (path.equals(PbapClientConnectionHandler.MCH_PATH)) {
70                type = CallLog.Calls.MISSED_TYPE;
71            } else {
72                Log.w(TAG, "Unknown path type:" + path);
73                return;
74            }
75
76            ArrayList<ContentProviderOperation> ops = new ArrayList<>();
77            for (VCardEntry vcard : mEntries) {
78                List<PhoneData> phones = vcard.getPhoneList();
79                if (phones == null || phones.size() != 1) {
80                    if (VDBG) {
81                        Log.d(TAG, "Incorrect number of phones: " + vcard);
82                    }
83                    continue;
84                }
85
86                List<Pair<String, String>> irmc = vcard.getUnknownXData();
87                Date date = null;
88                SimpleDateFormat parser = new SimpleDateFormat(TIMESTAMP_FORMAT);
89                for (Pair<String, String> pair : irmc) {
90                    if (pair.first.startsWith(TIMESTAMP_PROPERTY)) {
91                        try {
92                            date = parser.parse(pair.second);
93                        } catch (ParseException e) {
94                            Log.d(TAG, "Failed to parse date ");
95                            if (VDBG) {
96                                Log.d(TAG, pair.second);
97                            }
98                        }
99                    }
100                }
101
102                ContentValues values = new ContentValues();
103                values.put(CallLog.Calls.TYPE, type);
104                values.put(CallLog.Calls.NUMBER, phones.get(0).getNumber());
105                if (date != null) {
106                    values.put(CallLog.Calls.DATE, date.getTime());
107                }
108                ops.add(ContentProviderOperation.newInsert(CallLog.Calls.CONTENT_URI)
109                        .withValues(values).withYieldAllowed(true).build());
110            }
111            mContext.getContentResolver().applyBatch(CallLog.AUTHORITY, ops);
112            Log.d(TAG, "Updated call logs.");
113        } catch (RemoteException | OperationApplicationException e) {
114            Log.d(TAG, "Failed to update call log for path=" + path, e);
115        } finally {
116            synchronized (this) {
117                this.notify();
118            }
119        }
120    }
121}
122