BluetoothPbapCallLogComposer.java revision cff3303d1878037f35c9feb58e0c747aef403d4d
1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16package com.android.bluetooth.pbap; 17 18import android.content.ContentResolver; 19import android.content.Context; 20import android.database.Cursor; 21import android.database.sqlite.SQLiteException; 22import android.net.Uri; 23import android.pim.vcard.VCardBuilder; 24import android.pim.vcard.VCardConfig; 25import android.pim.vcard.VCardConstants; 26import android.pim.vcard.VCardUtils; 27import android.pim.vcard.VCardComposer.OneEntryHandler; 28import android.provider.CallLog; 29import android.provider.CallLog.Calls; 30import android.text.TextUtils; 31import android.text.format.Time; 32import android.util.Log; 33 34import java.util.ArrayList; 35import java.util.Arrays; 36import java.util.List; 37 38/** 39 * VCard composer especially for Call Log used in Bluetooth. 40 */ 41public class BluetoothPbapCallLogComposer { 42 private static final String TAG = "CallLogComposer"; 43 44 private static final String FAILURE_REASON_FAILED_TO_GET_DATABASE_INFO = 45 "Failed to get database information"; 46 47 private static final String FAILURE_REASON_NO_ENTRY = 48 "There's no exportable in the database"; 49 50 private static final String FAILURE_REASON_NOT_INITIALIZED = 51 "The vCard composer object is not correctly initialized"; 52 53 /** Should be visible only from developers... (no need to translate, hopefully) */ 54 private static final String FAILURE_REASON_UNSUPPORTED_URI = 55 "The Uri vCard composer received is not supported by the composer."; 56 57 private static final String NO_ERROR = "No error"; 58 59 /** The projection to use when querying the call log table */ 60 private static final String[] sCallLogProjection = new String[] { 61 Calls.NUMBER, Calls.DATE, Calls.TYPE, Calls.CACHED_NAME, Calls.CACHED_NUMBER_TYPE, 62 Calls.CACHED_NUMBER_LABEL 63 }; 64 private static final int NUMBER_COLUMN_INDEX = 0; 65 private static final int DATE_COLUMN_INDEX = 1; 66 private static final int CALL_TYPE_COLUMN_INDEX = 2; 67 private static final int CALLER_NAME_COLUMN_INDEX = 3; 68 private static final int CALLER_NUMBERTYPE_COLUMN_INDEX = 4; 69 private static final int CALLER_NUMBERLABEL_COLUMN_INDEX = 5; 70 71 // Property for call log entry 72 private static final String VCARD_PROPERTY_X_TIMESTAMP = "X-IRMC-CALL-DATETIME"; 73 private static final String VCARD_PROPERTY_CALLTYPE_INCOMING = "INCOMING"; 74 private static final String VCARD_PROPERTY_CALLTYPE_OUTGOING = "OUTGOING"; 75 private static final String VCARD_PROPERTY_CALLTYPE_MISSED = "MISSED"; 76 77 private static final String FLAG_TIMEZONE_UTC = "Z"; 78 79 private final Context mContext; 80 private ContentResolver mContentResolver; 81 private Cursor mCursor; 82 private final boolean mCareHandlerErrors; 83 84 private boolean mTerminateIsCalled; 85 private final List<OneEntryHandler> mHandlerList; 86 87 private String mErrorReason = NO_ERROR; 88 89 public BluetoothPbapCallLogComposer(final Context context, boolean careHandlerErrors) { 90 mContext = context; 91 mContentResolver = context.getContentResolver(); 92 mCareHandlerErrors = careHandlerErrors; 93 mHandlerList = new ArrayList<OneEntryHandler>(); 94 } 95 96 public boolean init(final Uri contentUri, final String selection, 97 final String[] selectionArgs, final String sortOrder) { 98 final String[] projection; 99 if (CallLog.Calls.CONTENT_URI.equals(contentUri)) { 100 projection = sCallLogProjection; 101 } else { 102 mErrorReason = FAILURE_REASON_UNSUPPORTED_URI; 103 return false; 104 } 105 106 mCursor = mContentResolver.query( 107 contentUri, projection, selection, selectionArgs, sortOrder); 108 109 if (mCursor == null) { 110 mErrorReason = FAILURE_REASON_FAILED_TO_GET_DATABASE_INFO; 111 return false; 112 } 113 114 if (mCareHandlerErrors) { 115 List<OneEntryHandler> finishedList = new ArrayList<OneEntryHandler>( 116 mHandlerList.size()); 117 for (OneEntryHandler handler : mHandlerList) { 118 if (!handler.onInit(mContext)) { 119 for (OneEntryHandler finished : finishedList) { 120 finished.onTerminate(); 121 } 122 return false; 123 } 124 } 125 } else { 126 // Just ignore the false returned from onInit(). 127 for (OneEntryHandler handler : mHandlerList) { 128 handler.onInit(mContext); 129 } 130 } 131 132 if (mCursor.getCount() == 0 || !mCursor.moveToFirst()) { 133 try { 134 mCursor.close(); 135 } catch (SQLiteException e) { 136 Log.e(TAG, "SQLiteException on Cursor#close(): " + e.getMessage()); 137 } finally { 138 mErrorReason = FAILURE_REASON_NO_ENTRY; 139 mCursor = null; 140 } 141 return false; 142 } 143 144 return true; 145 } 146 147 public void addHandler(OneEntryHandler handler) { 148 if (handler != null) { 149 mHandlerList.add(handler); 150 } 151 } 152 153 public boolean createOneEntry() { 154 if (mCursor == null || mCursor.isAfterLast()) { 155 mErrorReason = FAILURE_REASON_NOT_INITIALIZED; 156 return false; 157 } 158 159 final String vcard; 160 try { 161 vcard = createOneCallLogEntryInternal(); 162 } catch (OutOfMemoryError error) { 163 Log.e(TAG, "OutOfMemoryError occured. Ignore the entry"); 164 System.gc(); 165 return true; 166 } finally { 167 mCursor.moveToNext(); 168 } 169 170 if (mCareHandlerErrors) { 171 for (OneEntryHandler handler : mHandlerList) { 172 if (!handler.onEntryCreated(vcard)) { 173 return false; 174 } 175 } 176 } else { 177 for (OneEntryHandler handler : mHandlerList) { 178 handler.onEntryCreated(vcard); 179 } 180 } 181 182 return true; 183 } 184 185 private String createOneCallLogEntryInternal() { 186 final VCardBuilder builder = new VCardBuilder(VCardConfig.VCARD_TYPE_V21_GENERIC_UTF8); 187 String name = mCursor.getString(CALLER_NAME_COLUMN_INDEX); 188 if (TextUtils.isEmpty(name)) { 189 name = mCursor.getString(NUMBER_COLUMN_INDEX); 190 } 191 final boolean needCharset = !(VCardUtils.containsOnlyPrintableAscii(name)); 192 builder.appendLine(VCardConstants.PROPERTY_FN, name, needCharset, false); 193 builder.appendLine(VCardConstants.PROPERTY_N, name, needCharset, false); 194 195 final String number = mCursor.getString(NUMBER_COLUMN_INDEX); 196 final int type = mCursor.getInt(CALLER_NUMBERTYPE_COLUMN_INDEX); 197 String label = mCursor.getString(CALLER_NUMBERLABEL_COLUMN_INDEX); 198 if (TextUtils.isEmpty(label)) { 199 label = Integer.toString(type); 200 } 201 builder.appendTelLine(type, label, number, false); 202 tryAppendCallHistoryTimeStampField(builder); 203 204 return builder.toString(); 205 } 206 207 /** 208 * This static function is to compose vCard for phone own number 209 */ 210 public String composeVCardForPhoneOwnNumber(int phonetype, String phoneName, 211 String phoneNumber, boolean vcardVer21) { 212 final int vcardType = (vcardVer21 ? 213 VCardConfig.VCARD_TYPE_V21_GENERIC_UTF8 : 214 VCardConfig.VCARD_TYPE_V30_GENERIC_UTF8); 215 final VCardBuilder builder = new VCardBuilder(vcardType); 216 boolean needCharset = false; 217 if (!(VCardUtils.containsOnlyPrintableAscii(phoneName))) { 218 needCharset = true; 219 } 220 builder.appendLine(VCardConstants.PROPERTY_FN, phoneName, needCharset, false); 221 builder.appendLine(VCardConstants.PROPERTY_N, phoneName, needCharset, false); 222 223 if (!TextUtils.isEmpty(phoneNumber)) { 224 String label = Integer.toString(phonetype); 225 builder.appendTelLine(phonetype, label, phoneNumber, false); 226 } 227 228 return builder.toString(); 229 } 230 231 /** 232 * Format according to RFC 2445 DATETIME type. 233 * The format is: ("%Y%m%dT%H%M%SZ"). 234 */ 235 private final String toRfc2455Format(final long millSecs) { 236 Time startDate = new Time(); 237 startDate.set(millSecs); 238 String date = startDate.format2445(); 239 return date + FLAG_TIMEZONE_UTC; 240 } 241 242 /** 243 * Try to append the property line for a call history time stamp field if possible. 244 * Do nothing if the call log type gotton from the database is invalid. 245 */ 246 private void tryAppendCallHistoryTimeStampField(final VCardBuilder builder) { 247 // Extension for call history as defined in 248 // in the Specification for Ic Mobile Communcation - ver 1.1, 249 // Oct 2000. This is used to send the details of the call 250 // history - missed, incoming, outgoing along with date and time 251 // to the requesting device (For example, transferring phone book 252 // when connected over bluetooth) 253 // 254 // e.g. "X-IRMC-CALL-DATETIME;MISSED:20050320T100000Z" 255 final int callLogType = mCursor.getInt(CALL_TYPE_COLUMN_INDEX); 256 final String callLogTypeStr; 257 switch (callLogType) { 258 case Calls.INCOMING_TYPE: { 259 callLogTypeStr = VCARD_PROPERTY_CALLTYPE_INCOMING; 260 break; 261 } 262 case Calls.OUTGOING_TYPE: { 263 callLogTypeStr = VCARD_PROPERTY_CALLTYPE_OUTGOING; 264 break; 265 } 266 case Calls.MISSED_TYPE: { 267 callLogTypeStr = VCARD_PROPERTY_CALLTYPE_MISSED; 268 break; 269 } 270 default: { 271 Log.w(TAG, "Call log type not correct."); 272 return; 273 } 274 } 275 276 final long dateAsLong = mCursor.getLong(DATE_COLUMN_INDEX); 277 builder.appendLine(VCARD_PROPERTY_X_TIMESTAMP, 278 Arrays.asList(callLogTypeStr), toRfc2455Format(dateAsLong)); 279 } 280 281 public void terminate() { 282 for (OneEntryHandler handler : mHandlerList) { 283 handler.onTerminate(); 284 } 285 286 if (mCursor != null) { 287 try { 288 mCursor.close(); 289 } catch (SQLiteException e) { 290 Log.e(TAG, "SQLiteException on Cursor#close(): " + e.getMessage()); 291 } 292 mCursor = null; 293 } 294 295 mTerminateIsCalled = true; 296 } 297 298 @Override 299 public void finalize() { 300 if (!mTerminateIsCalled) { 301 terminate(); 302 } 303 } 304 305 public int getCount() { 306 if (mCursor == null) { 307 return 0; 308 } 309 return mCursor.getCount(); 310 } 311 312 public boolean isAfterLast() { 313 if (mCursor == null) { 314 return false; 315 } 316 return mCursor.isAfterLast(); 317 } 318 319 public String getErrorReason() { 320 return mErrorReason; 321 } 322} 323