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 = Utils.DBG; 43 private static final boolean VDBG = Utils.VDBG; 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 CallLogPullRequest(Context context, String path, HashMap<String, Integer> map, Account account)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 onPullComplete()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.name); 92 List<PhoneData> phones = vcard.getPhoneList(); 93 if (phones == null || phones.get(0).getNumber().equals(";") 94 || phones.get(0).getNumber().length() == 0) { 95 values.put(CallLog.Calls.NUMBER, ""); 96 } else { 97 String phoneNumber = phones.get(0).getNumber(); 98 values.put(CallLog.Calls.NUMBER, phoneNumber); 99 if (mCallCounter.get(phoneNumber) != null) { 100 int updateCounter = mCallCounter.get(phoneNumber) + 1; 101 mCallCounter.put(phoneNumber, updateCounter); 102 } else { 103 mCallCounter.put(phoneNumber, 1); 104 } 105 } 106 List<Pair<String, String>> irmc = vcard.getUnknownXData(); 107 SimpleDateFormat parser = new SimpleDateFormat(TIMESTAMP_FORMAT); 108 if (irmc != null) { 109 for (Pair<String, String> pair : irmc) { 110 if (pair.first.startsWith(TIMESTAMP_PROPERTY)) { 111 try { 112 values.put(CallLog.Calls.DATE, parser.parse(pair.second).getTime()); 113 } catch (ParseException e) { 114 Log.d(TAG, "Failed to parse date "); 115 if (VDBG) { 116 Log.d(TAG, pair.second); 117 } 118 } 119 } 120 } 121 } 122 ops.add(ContentProviderOperation.newInsert(CallLog.Calls.CONTENT_URI) 123 .withValues(values) 124 .withYieldAllowed(true) 125 .build()); 126 } 127 mContext.getContentResolver().applyBatch(CallLog.AUTHORITY, ops); 128 Log.d(TAG, "Updated call logs."); 129 //OUTGOING_TYPE is the last callLog we fetched. 130 if (type == CallLog.Calls.OUTGOING_TYPE) { 131 updateTimesContacted(); 132 } 133 } catch (RemoteException | OperationApplicationException e) { 134 Log.d(TAG, "Failed to update call log for path=" + path, e); 135 } finally { 136 synchronized (this) { 137 this.notify(); 138 } 139 } 140 } 141 updateTimesContacted()142 private void updateTimesContacted() { 143 for (String key : mCallCounter.keySet()) { 144 ContentValues values = new ContentValues(); 145 values.put(ContactsContract.RawContacts.TIMES_CONTACTED, mCallCounter.get(key)); 146 Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, 147 Uri.encode(key)); 148 Cursor c = mContext.getContentResolver().query(uri, null, null, null); 149 if (c != null && c.getCount() > 0) { 150 c.moveToNext(); 151 String contactId = c.getString(c.getColumnIndex( 152 ContactsContract.PhoneLookup.CONTACT_ID)); 153 if (VDBG) { 154 Log.d(TAG, "onPullComplete: ID " + contactId + " key : " + key); 155 } 156 String where = ContactsContract.RawContacts.CONTACT_ID + "=" + contactId; 157 mContext.getContentResolver().update( 158 ContactsContract.RawContacts.CONTENT_URI, values, where, null); 159 } 160 } 161 if (DBG) { 162 Log.d(TAG, "Updated TIMES_CONTACTED"); 163 } 164 } 165 166 } 167