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 package com.android.bluetooth.pbapclient; 17 18 import android.accounts.Account; 19 import android.content.ContentProviderOperation; 20 import android.content.ContentValues; 21 import android.content.Context; 22 import android.content.OperationApplicationException; 23 import android.database.Cursor; 24 import android.net.Uri; 25 import android.os.RemoteException; 26 import android.provider.CallLog; 27 import android.provider.CallLog.Calls; 28 import android.provider.ContactsContract; 29 import android.util.Log; 30 import android.util.Pair; 31 32 import com.android.vcard.VCardEntry; 33 import com.android.vcard.VCardEntry.PhoneData; 34 35 import java.text.ParseException; 36 import java.text.SimpleDateFormat; 37 import java.util.ArrayList; 38 import java.util.HashMap; 39 import java.util.List; 40 41 public class CallLogPullRequest extends PullRequest { 42 private static final boolean DBG = true; 43 private static final boolean VDBG = false; 44 private static final String TAG = "PbapCallLogPullRequest"; 45 private static final String TIMESTAMP_PROPERTY = "X-IRMC-CALL-DATETIME"; 46 private static final String TIMESTAMP_FORMAT = "yyyyMMdd'T'HHmmss"; 47 48 private final Account mAccount; 49 private Context mContext; 50 private HashMap<String, Integer> mCallCounter; 51 52 public CallLogPullRequest(Context context, String path, HashMap<String, Integer> map, 53 Account account) { 54 mContext = context; 55 this.path = path; 56 mCallCounter = map; 57 mAccount = account; 58 } 59 60 @Override 61 public void onPullComplete() { 62 if (mEntries == null) { 63 Log.e(TAG, "onPullComplete entries is null."); 64 return; 65 } 66 67 if (DBG) { 68 Log.d(TAG, "onPullComplete"); 69 if (VDBG) { 70 Log.d(TAG, " with " + mEntries.size() + " count."); 71 } 72 } 73 int type; 74 try { 75 if (path.equals(PbapClientConnectionHandler.ICH_PATH)) { 76 type = CallLog.Calls.INCOMING_TYPE; 77 } else if (path.equals(PbapClientConnectionHandler.OCH_PATH)) { 78 type = CallLog.Calls.OUTGOING_TYPE; 79 } else if (path.equals(PbapClientConnectionHandler.MCH_PATH)) { 80 type = CallLog.Calls.MISSED_TYPE; 81 } else { 82 Log.w(TAG, "Unknown path type:" + path); 83 return; 84 } 85 86 ArrayList<ContentProviderOperation> ops = new ArrayList<>(); 87 for (VCardEntry vcard : mEntries) { 88 ContentValues values = new ContentValues(); 89 90 values.put(CallLog.Calls.TYPE, type); 91 values.put(Calls.PHONE_ACCOUNT_ID, mAccount.hashCode()); 92 List<PhoneData> phones = vcard.getPhoneList(); 93 if (phones == null || phones.get(0).getNumber().equals(";")) { 94 values.put(CallLog.Calls.NUMBER, ""); 95 } else { 96 String phoneNumber = phones.get(0).getNumber(); 97 values.put(CallLog.Calls.NUMBER, phoneNumber); 98 if (mCallCounter.get(phoneNumber) != null) { 99 int updateCounter = mCallCounter.get(phoneNumber) + 1; 100 mCallCounter.put(phoneNumber, updateCounter); 101 } else { 102 mCallCounter.put(phoneNumber, 1); 103 } 104 } 105 List<Pair<String, String>> irmc = vcard.getUnknownXData(); 106 SimpleDateFormat parser = new SimpleDateFormat(TIMESTAMP_FORMAT); 107 if (irmc != null) { 108 for (Pair<String, String> pair : irmc) { 109 if (pair.first.startsWith(TIMESTAMP_PROPERTY)) { 110 try { 111 values.put(CallLog.Calls.DATE, parser.parse(pair.second).getTime()); 112 } catch (ParseException e) { 113 Log.d(TAG, "Failed to parse date "); 114 if (VDBG) { 115 Log.d(TAG, pair.second); 116 } 117 } 118 } 119 } 120 } 121 ops.add(ContentProviderOperation.newInsert(CallLog.Calls.CONTENT_URI) 122 .withValues(values) 123 .withYieldAllowed(true) 124 .build()); 125 } 126 mContext.getContentResolver().applyBatch(CallLog.AUTHORITY, ops); 127 Log.d(TAG, "Updated call logs."); 128 //OUTGOING_TYPE is the last callLog we fetched. 129 if (type == CallLog.Calls.OUTGOING_TYPE) { 130 updateTimesContacted(); 131 } 132 } catch (RemoteException | OperationApplicationException e) { 133 Log.d(TAG, "Failed to update call log for path=" + path, e); 134 } finally { 135 synchronized (this) { 136 this.notify(); 137 } 138 } 139 } 140 141 private void updateTimesContacted() { 142 for (String key : mCallCounter.keySet()) { 143 ContentValues values = new ContentValues(); 144 values.put(ContactsContract.RawContacts.TIMES_CONTACTED, mCallCounter.get(key)); 145 Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, 146 Uri.encode(key)); 147 Cursor c = mContext.getContentResolver().query(uri, null, null, null); 148 if (c != null && c.getCount() > 0) { 149 c.moveToNext(); 150 String contactId = c.getString(c.getColumnIndex( 151 ContactsContract.PhoneLookup.CONTACT_ID)); 152 if (VDBG) { 153 Log.d(TAG, "onPullComplete: ID " + contactId + " key : " + key); 154 } 155 String where = ContactsContract.RawContacts.CONTACT_ID + "=" + contactId; 156 mContext.getContentResolver().update( 157 ContactsContract.RawContacts.CONTENT_URI, values, where, null); 158 } 159 } 160 if (DBG) { 161 Log.d(TAG, "Updated TIMES_CONTACTED"); 162 } 163 } 164 165 } 166