Home | History | Annotate | Download | only in pbapclient
      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 com.android.bluetooth.pbapclient.BluetoothPbapClient;
     19 import android.content.ContentProviderOperation;
     20 import android.content.ContentValues;
     21 import android.content.Context;
     22 import android.content.OperationApplicationException;
     23 import android.os.RemoteException;
     24 import android.provider.CallLog;
     25 import android.util.Log;
     26 import android.util.Pair;
     27 
     28 import com.android.vcard.VCardEntry;
     29 import com.android.vcard.VCardEntry.PhoneData;
     30 
     31 import java.text.ParseException;
     32 import java.text.SimpleDateFormat;
     33 import java.util.ArrayList;
     34 import java.util.Date;
     35 import java.util.List;
     36 
     37 public class CallLogPullRequest extends PullRequest {
     38     private static boolean DBG = true;
     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 with " + mEntries.size() + " count.");
     59         }
     60         int type;
     61         try {
     62             if (path.equals(BluetoothPbapClient.ICH_PATH)) {
     63                 type = CallLog.Calls.INCOMING_TYPE;
     64             } else if (path.equals(BluetoothPbapClient.OCH_PATH)) {
     65                 type = CallLog.Calls.OUTGOING_TYPE;
     66             } else if (path.equals(BluetoothPbapClient.MCH_PATH)) {
     67                 type = CallLog.Calls.MISSED_TYPE;
     68             } else {
     69                 Log.w(TAG, "Unknown path type:" + path);
     70                 return;
     71             }
     72 
     73             ArrayList<ContentProviderOperation> ops = new ArrayList<>();
     74             for (VCardEntry vcard : mEntries) {
     75                 List<PhoneData> phones = vcard.getPhoneList();
     76                 if (phones == null || phones.size() != 1) {
     77                     Log.d(TAG, "Incorrect number of phones: " + vcard);
     78                     continue;
     79                 }
     80 
     81                 List<Pair<String, String>> irmc = vcard.getUnknownXData();
     82                 Date date = null;
     83                 SimpleDateFormat parser = new SimpleDateFormat(TIMESTAMP_FORMAT);
     84                 for (Pair<String, String> pair : irmc) {
     85                     if (pair.first.startsWith(TIMESTAMP_PROPERTY)) {
     86                         try {
     87                             date = parser.parse(pair.second);
     88                         } catch (ParseException e) {
     89                             Log.d(TAG, "Failed to parse date " + pair.second);
     90                         }
     91                     }
     92                 }
     93 
     94                 ContentValues values = new ContentValues();
     95                 values.put(CallLog.Calls.TYPE, type);
     96                 values.put(CallLog.Calls.NUMBER, phones.get(0).getNumber());
     97                 if (date != null) {
     98                     values.put(CallLog.Calls.DATE, date.getTime());
     99                 }
    100                 ops.add(ContentProviderOperation.newInsert(CallLog.Calls.CONTENT_URI)
    101                         .withValues(values).withYieldAllowed(true).build());
    102             }
    103             mContext.getContentResolver().applyBatch(CallLog.AUTHORITY, ops);
    104             Log.d(TAG, "Updated call logs.");
    105         } catch (RemoteException | OperationApplicationException e) {
    106             Log.d(TAG, "Failed to update call log for path=" + path, e);
    107         } finally {
    108             synchronized (this) {
    109                 this.notify();
    110             }
    111         }
    112     }
    113 }
    114