diff options
author | Kim Schulz <k.schulz@samsung.com> | 2014-12-10 12:51:46 -0800 |
---|---|---|
committer | Andre Eisenbach <eisenbach@google.com> | 2014-12-11 12:19:09 -0800 |
commit | 28446a56718e80574ba7b81901fc5a1368bf5839 (patch) | |
tree | 6fbe47368f75824e3ff4317b43cbaf573180b8c9 /src/com/android/bluetooth | |
parent | 95ada14315a8ed5650508342a673710b3504af7e (diff) | |
download | android_packages_apps_Bluetooth-28446a56718e80574ba7b81901fc5a1368bf5839.tar.gz android_packages_apps_Bluetooth-28446a56718e80574ba7b81901fc5a1368bf5839.tar.bz2 android_packages_apps_Bluetooth-28446a56718e80574ba7b81901fc5a1368bf5839.zip |
Prevent Cursor leakage in Bluetooth MAP
Bug: 18415378
Change-Id: Id513ee848dd21c641d77be38bdc6be85b5ef87a8
Diffstat (limited to 'src/com/android/bluetooth')
4 files changed, 485 insertions, 623 deletions
diff --git a/src/com/android/bluetooth/map/BluetoothMapContent.java b/src/com/android/bluetooth/map/BluetoothMapContent.java index 41df206e6..540cbbb36 100644 --- a/src/com/android/bluetooth/map/BluetoothMapContent.java +++ b/src/com/android/bluetooth/map/BluetoothMapContent.java @@ -44,6 +44,7 @@ import com.google.android.mms.pdu.PduHeaders; import com.android.bluetooth.map.BluetoothMapAppParams; import java.io.ByteArrayOutputStream; +import java.io.Closeable; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; @@ -216,148 +217,13 @@ public class BluetoothMapContent { mBaseEmailUri = emailBaseUri; } - - - private void printSms(Cursor c) { - String body = c.getString(c.getColumnIndex(Sms.BODY)); - if (V) Log.v(TAG, "printSms " + BaseColumns._ID + ": " + c.getLong(c.getColumnIndex(BaseColumns._ID)) + - "\n " + Sms.THREAD_ID + " : " + c.getLong(c.getColumnIndex(Sms.THREAD_ID)) + - "\n " + Sms.ADDRESS + " : " + c.getString(c.getColumnIndex(Sms.ADDRESS)) + - "\n " + Sms.BODY + " : " + body.substring(0, Math.min(body.length(), 8)) + - "\n " + Sms.DATE + " : " + c.getLong(c.getColumnIndex(Sms.DATE)) + - "\n " + Sms.READ + " : " + c.getLong(c.getColumnIndex(Sms.READ)) + - "\n " + Sms.TYPE + " : " + c.getInt(c.getColumnIndex(Sms.TYPE)) + - "\n " + Sms.STATUS + " : " + c.getInt(c.getColumnIndex(Sms.STATUS)) + - "\n " + Sms.LOCKED + " : " + c.getInt(c.getColumnIndex(Sms.LOCKED)) + - "\n " + Sms.ERROR_CODE + " : " + c.getInt(c.getColumnIndex(Sms.ERROR_CODE))); - - - } - - private void printMms(Cursor c) { - if (V) Log.v(TAG, "printMms " + BaseColumns._ID + ": " + c.getLong(c.getColumnIndex(BaseColumns._ID)) + - "\n " + Mms.THREAD_ID + " : " + c.getLong(c.getColumnIndex(Mms.THREAD_ID)) + - "\n " + Mms.MESSAGE_ID + " : " + c.getString(c.getColumnIndex(Mms.MESSAGE_ID)) + - "\n " + Mms.SUBJECT + " : " + c.getString(c.getColumnIndex(Mms.SUBJECT)) + - "\n " + Mms.CONTENT_TYPE + " : " + c.getString(c.getColumnIndex(Mms.CONTENT_TYPE)) + - "\n " + Mms.TEXT_ONLY + " : " + c.getInt(c.getColumnIndex(Mms.TEXT_ONLY)) + - "\n " + Mms.DATE + " : " + c.getLong(c.getColumnIndex(Mms.DATE)) + - "\n " + Mms.DATE_SENT + " : " + c.getLong(c.getColumnIndex(Mms.DATE_SENT)) + - "\n " + Mms.READ + " : " + c.getInt(c.getColumnIndex(Mms.READ)) + - "\n " + Mms.MESSAGE_BOX + " : " + c.getInt(c.getColumnIndex(Mms.MESSAGE_BOX)) + - "\n " + Mms.STATUS + " : " + c.getInt(c.getColumnIndex(Mms.STATUS)) + - "\n " + Mms.PRIORITY + " : " + c.getInt(c.getColumnIndex(Mms.PRIORITY)) + - "\n " + Mms.MESSAGE_SIZE + " : " + c.getInt(c.getColumnIndex(Mms.MESSAGE_SIZE))); - } - - private String getDateTimeString(long timestamp) { - SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd'T'HHmmss"); - Date date = new Date(timestamp); - return format.format(date); // Format to YYYYMMDDTHHMMSS local time - } - private void printEmail(Cursor c) { - if (V) Log.v(TAG, "printEmail " + BaseColumns._ID + ": " + c.getLong(c.getColumnIndex(BaseColumns._ID)) + - "\n " + BluetoothMapContract.MessageColumns.DATE + " : " + getDateTimeString(c.getLong(c.getColumnIndex(BluetoothMapContract.MessageColumns.DATE))) + - "\n " + BluetoothMapContract.MessageColumns.SUBJECT + " : " + c.getString(c.getColumnIndex(BluetoothMapContract.MessageColumns.SUBJECT)) + - "\n " + BluetoothMapContract.MessageColumns.FLAG_READ + " : " + c.getInt(c.getColumnIndex(BluetoothMapContract.MessageColumns.FLAG_READ)) + - "\n " + BluetoothMapContract.MessageColumns.FLAG_HIGH_PRIORITY + " : " + c.getInt(c.getColumnIndex(BluetoothMapContract.MessageColumns.FLAG_HIGH_PRIORITY)) + - "\n " + BluetoothMapContract.MessageColumns.RECEPTION_STATE + " : " + c.getInt(c.getColumnIndex(BluetoothMapContract.MessageColumns.RECEPTION_STATE)) + - "\n " + BluetoothMapContract.MessageColumns.FLAG_ATTACHMENT + " : " + c.getInt(c.getColumnIndex(BluetoothMapContract.MessageColumns.FLAG_ATTACHMENT)) + - "\n " + BluetoothMapContract.MessageColumns.MESSAGE_SIZE + " : " + c.getLong(c.getColumnIndex(BluetoothMapContract.MessageColumns.MESSAGE_SIZE)) + - "\n " + BluetoothMapContract.MessageColumns.FLAG_PROTECTED + " : " + c.getInt(c.getColumnIndex(BluetoothMapContract.MessageColumns.FLAG_PROTECTED)) + - "\n " + BluetoothMapContract.MessageColumns.FROM_LIST + " : " + c.getString(c.getColumnIndex(BluetoothMapContract.MessageColumns.FROM_LIST)) + - "\n " + BluetoothMapContract.MessageColumns.TO_LIST + " : " + c.getString(c.getColumnIndex(BluetoothMapContract.MessageColumns.TO_LIST)) + - "\n " + BluetoothMapContract.MessageColumns.CC_LIST + " : " + c.getString(c.getColumnIndex(BluetoothMapContract.MessageColumns.CC_LIST)) + - "\n " + BluetoothMapContract.MessageColumns.BCC_LIST + " : " + c.getString(c.getColumnIndex(BluetoothMapContract.MessageColumns.BCC_LIST)) + - "\n " + BluetoothMapContract.MessageColumns.REPLY_TO_LIST + " : " + c.getString(c.getColumnIndex(BluetoothMapContract.MessageColumns.REPLY_TO_LIST)) + - "\n " + BluetoothMapContract.MessageColumns.FOLDER_ID + " : " + c.getInt(c.getColumnIndex(BluetoothMapContract.MessageColumns.FOLDER_ID)) + - "\n " + BluetoothMapContract.MessageColumns.ACCOUNT_ID + " : " + c.getInt(c.getColumnIndex(BluetoothMapContract.MessageColumns.ACCOUNT_ID)) ); - } - - private void printMmsAddr(long id) { - final String[] projection = null; - String selection = new String("msg_id=" + id); - String uriStr = new String(Mms.CONTENT_URI + "/" + id + "/addr"); - Uri uriAddress = Uri.parse(uriStr); - Cursor c = mResolver.query(uriAddress, projection, selection, null, null); - - if (c.moveToFirst()) { - do { - String add = c.getString(c.getColumnIndex("address")); - Integer type = c.getInt(c.getColumnIndex("type")); - if (type == MMS_TO) { - if (D) Log.d(TAG, " recipient: " + add + " (type: " + type + ")"); - } else if (type == MMS_FROM) { - if (D) Log.d(TAG, " originator: " + add + " (type: " + type + ")"); - } else { - if (D) Log.d(TAG, " address other: " + add + " (type: " + type + ")"); - } - - } while(c.moveToNext()); - } - } - - private void printMmsPartImage(long partid) { - String uriStr = new String(Mms.CONTENT_URI + "/part/" + partid); - Uri uriAddress = Uri.parse(uriStr); - int ch; - StringBuffer sb = new StringBuffer(""); - InputStream is = null; - + private static void close(Closeable c) { try { - is = mResolver.openInputStream(uriAddress); - - while ((ch = is.read()) != -1) { - sb.append((char)ch); - } - if (D) Log.d(TAG, sb.toString()); - + if (c != null) c.close(); } catch (IOException e) { - // do nothing for now - e.printStackTrace(); } } - private void printMmsParts(long id) { - final String[] projection = null; - String selection = new String("mid=" + id); - String uriStr = new String(Mms.CONTENT_URI + "/" + id + "/part"); - Uri uriAddress = Uri.parse(uriStr); - Cursor c = mResolver.query(uriAddress, projection, selection, null, null); - - if (D) Log.d(TAG, " parts:"); - if (c.moveToFirst()) { - do { - Long partid = c.getLong(c.getColumnIndex(BaseColumns._ID)); - String ct = c.getString(c.getColumnIndex("ct")); - String name = c.getString(c.getColumnIndex("name")); - String charset = c.getString(c.getColumnIndex("chset")); - String filename = c.getString(c.getColumnIndex("fn")); - String text = c.getString(c.getColumnIndex("text")); - Integer fd = c.getInt(c.getColumnIndex("_data")); - String cid = c.getString(c.getColumnIndex("cid")); - String cl = c.getString(c.getColumnIndex("cl")); - String cdisp = c.getString(c.getColumnIndex("cd")); - - if (D) Log.d(TAG, " _id : " + partid + - "\n ct : " + ct + - "\n partname : " + name + - "\n charset : " + charset + - "\n filename : " + filename + - "\n text : " + text + - "\n fd : " + fd + - "\n cid : " + cid + - "\n cl : " + cl + - "\n cdisp : " + cdisp); - - /* if (ct.equals("image/jpeg")) { */ - /* printMmsPartImage(partid); */ - /* } */ - } while(c.moveToNext()); - } - } - - private void setProtected(BluetoothMapMessageListingElement e, Cursor c, FilterInfo fi, BluetoothMapAppParams ap) { if ((ap.getParameterMask() & MASK_PROTECTED) != 0) { @@ -747,7 +613,6 @@ public class BluetoothMapContent { date = c.getLong(fi.mEmailColDate); } e.setDateTime(date); - if (V) Log.v(TAG, "setDateTime: " + e.getDateTimeString()); } } @@ -757,11 +622,10 @@ public class BluetoothMapContent { String uriStr = new String(Mms.CONTENT_URI + "/" + id + "/part"); Uri uriAddress = Uri.parse(uriStr); // TODO: maybe use a projection with only "ct" and "text" - Cursor c = mResolver.query(uriAddress, null, selection, - null, null); - if (c != null && c.moveToFirst()) { - do { + Cursor c = mResolver.query(uriAddress, null, selection, null, null); + try { + while(c != null && c.moveToNext()) { String ct = c.getString(c.getColumnIndex("ct")); if (ct.equals("text/plain")) { String part = c.getString(c.getColumnIndex("text")); @@ -769,10 +633,9 @@ public class BluetoothMapContent { text += part; } } - } while(c.moveToNext()); - } - if (c != null) { - c.close(); + } + } finally { + close(c); } return text; } @@ -844,13 +707,13 @@ public class BluetoothMapContent { String orderBy = Contacts.DISPLAY_NAME + " ASC"; Cursor c = mResolver.query(uri, projection, selection, null, orderBy); - - if (c != null && c.getCount() >= 1) { - c.moveToFirst(); - name = c.getString(c.getColumnIndex(Contacts.DISPLAY_NAME)); + try { + if (c != null && c.moveToFirst()) { + name = c.getString(c.getColumnIndex(Contacts.DISPLAY_NAME)); + }; + } finally { + close(c); } - - c.close(); return name; } @@ -859,17 +722,17 @@ public class BluetoothMapContent { String uriStr = new String(Mms.CONTENT_URI + "/" + id + "/addr"); Uri uriAddress = Uri.parse(uriStr); String addr = null; - Cursor c = r.query(uriAddress, null, selection, null, null); - if (c != null && c.moveToFirst()) { - addr = c.getString(c.getColumnIndex(Mms.Addr.ADDRESS)); - if(addr.equals(INSERT_ADDRES_TOKEN)) - addr = ""; + Cursor c = r.query(uriAddress, null, selection, null, null); + try { + if (c != null && c.moveToFirst()) { + addr = c.getString(c.getColumnIndex(Mms.Addr.ADDRESS)); + if (addr.equals(INSERT_ADDRES_TOKEN)) addr = ""; + } + } finally { + close(c); } - if (c != null) { - c.close(); - } return addr; } @@ -1173,29 +1036,32 @@ public class BluetoothMapContent { new String[]{str}, ContactsContract.Contacts.DISPLAY_NAME + " ASC"); - while (c != null && c.moveToNext()) { - String contactId = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID)); + try { + while (c != null && c.moveToNext()) { + String contactId = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID)); - Cursor p = mResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, - ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", - new String[]{contactId}, - null); + Cursor p = mResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, + ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", + new String[]{contactId}, + null); - while (p != null && p.moveToNext()) { - String number = p.getString( - p.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); + try { + while (p != null && p.moveToNext()) { + String number = p.getString( + p.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); - where += " address = " + "'" + number + "'"; - if (!p.isLast()) { - where += " OR "; + where += " address = " + "'" + number + "'"; + if (!p.isLast()) where += " OR "; + } + } finally { + close(p); } + + if (!c.isLast()) where += " OR "; } - if (!c.isLast()) { - where += " OR "; - } - p.close(); + } finally { + close(c); } - c.close(); if (str != null && str.length() > 0) { if (where.length() > 0) { @@ -1382,153 +1248,155 @@ public class BluetoothMapContent { Cursor smsCursor = null; Cursor mmsCursor = null; Cursor emailCursor = null; - String limit = ""; - int countNum = ap.getMaxListCount(); - int offsetNum = ap.getStartOffset(); - if(ap.getMaxListCount()>0){ - limit=" LIMIT "+ (ap.getMaxListCount()+ap.getStartOffset()); - } - if (smsSelected(fi, ap) && folderElement.hasSmsMmsContent()) { - if(ap.getFilterMessageType() == (BluetoothMapAppParams.FILTER_NO_EMAIL| - BluetoothMapAppParams.FILTER_NO_MMS| - BluetoothMapAppParams.FILTER_NO_SMS_GSM)|| - ap.getFilterMessageType() == (BluetoothMapAppParams.FILTER_NO_EMAIL| - BluetoothMapAppParams.FILTER_NO_MMS| - BluetoothMapAppParams.FILTER_NO_SMS_CDMA)){ - //set real limit and offset if only this type is used (only if offset/limit is used - limit = " LIMIT " + ap.getMaxListCount()+" OFFSET "+ ap.getStartOffset(); - if(D) Log.d(TAG, "SMS Limit => "+limit); - offsetNum = 0; + try { + String limit = ""; + int countNum = ap.getMaxListCount(); + int offsetNum = ap.getStartOffset(); + if(ap.getMaxListCount()>0){ + limit=" LIMIT "+ (ap.getMaxListCount()+ap.getStartOffset()); } - fi.mMsgType = FilterInfo.TYPE_SMS; - if(ap.getFilterPriority() != 1){ /*SMS cannot have high priority*/ + + if (smsSelected(fi, ap) && folderElement.hasSmsMmsContent()) { + if(ap.getFilterMessageType() == (BluetoothMapAppParams.FILTER_NO_EMAIL| + BluetoothMapAppParams.FILTER_NO_MMS| + BluetoothMapAppParams.FILTER_NO_SMS_GSM)|| + ap.getFilterMessageType() == (BluetoothMapAppParams.FILTER_NO_EMAIL| + BluetoothMapAppParams.FILTER_NO_MMS| + BluetoothMapAppParams.FILTER_NO_SMS_CDMA)){ + //set real limit and offset if only this type is used (only if offset/limit is used + limit = " LIMIT " + ap.getMaxListCount()+" OFFSET "+ ap.getStartOffset(); + if(D) Log.d(TAG, "SMS Limit => "+limit); + offsetNum = 0; + } + fi.mMsgType = FilterInfo.TYPE_SMS; + if(ap.getFilterPriority() != 1){ /*SMS cannot have high priority*/ + String where = setWhereFilter(folderElement, fi, ap); + if(!where.isEmpty()) { + if (D) Log.d(TAG, "msgType: " + fi.mMsgType); + smsCursor = mResolver.query(Sms.CONTENT_URI, + SMS_PROJECTION, where, null, Sms.DATE + " DESC" + limit); + if (smsCursor != null) { + BluetoothMapMessageListingElement e = null; + // store column index so we dont have to look them up anymore (optimization) + if(D) Log.d(TAG, "Found " + smsCursor.getCount() + " sms messages."); + fi.setSmsColumns(smsCursor); + while (smsCursor.moveToNext()) { + if (matchAddresses(smsCursor, fi, ap)) { + e = element(smsCursor, fi, ap); + bmList.add(e); + } + } + } + } + } + } + + if (mmsSelected(fi, ap) && folderElement.hasSmsMmsContent()) { + if(ap.getFilterMessageType() == (BluetoothMapAppParams.FILTER_NO_EMAIL| + BluetoothMapAppParams.FILTER_NO_SMS_CDMA| + BluetoothMapAppParams.FILTER_NO_SMS_GSM)){ + //set real limit and offset if only this type is used (only if offset/limit is used + limit = " LIMIT " + ap.getMaxListCount()+" OFFSET "+ ap.getStartOffset(); + if(D) Log.d(TAG, "MMS Limit => "+limit); + offsetNum = 0; + } + fi.mMsgType = FilterInfo.TYPE_MMS; String where = setWhereFilter(folderElement, fi, ap); if(!where.isEmpty()) { if (D) Log.d(TAG, "msgType: " + fi.mMsgType); - smsCursor = mResolver.query(Sms.CONTENT_URI, - SMS_PROJECTION, where, null, Sms.DATE + " DESC" + limit); - if (smsCursor != null) { + mmsCursor = mResolver.query(Mms.CONTENT_URI, + MMS_PROJECTION, where, null, Mms.DATE + " DESC" + limit); + if (mmsCursor != null) { BluetoothMapMessageListingElement e = null; // store column index so we dont have to look them up anymore (optimization) - if(D) Log.d(TAG, "Found " + smsCursor.getCount() + " sms messages."); - fi.setSmsColumns(smsCursor); - while (smsCursor.moveToNext()) { - if (matchAddresses(smsCursor, fi, ap)) { - if(V) printSms(smsCursor); - e = element(smsCursor, fi, ap); + fi.setMmsColumns(mmsCursor); + int cnt = 0; + if(D) Log.d(TAG, "Found " + mmsCursor.getCount() + " mms messages."); + while (mmsCursor.moveToNext()) { + if (matchAddresses(mmsCursor, fi, ap)) { + e = element(mmsCursor, fi, ap); bmList.add(e); } } } } } - } - if (mmsSelected(fi, ap) && folderElement.hasSmsMmsContent()) { - if(ap.getFilterMessageType() == (BluetoothMapAppParams.FILTER_NO_EMAIL| - BluetoothMapAppParams.FILTER_NO_SMS_CDMA| - BluetoothMapAppParams.FILTER_NO_SMS_GSM)){ - //set real limit and offset if only this type is used (only if offset/limit is used - limit = " LIMIT " + ap.getMaxListCount()+" OFFSET "+ ap.getStartOffset(); - if(D) Log.d(TAG, "MMS Limit => "+limit); - offsetNum = 0; - } - fi.mMsgType = FilterInfo.TYPE_MMS; - String where = setWhereFilter(folderElement, fi, ap); - if(!where.isEmpty()) { - if (D) Log.d(TAG, "msgType: " + fi.mMsgType); - mmsCursor = mResolver.query(Mms.CONTENT_URI, - MMS_PROJECTION, where, null, Mms.DATE + " DESC" + limit); - if (mmsCursor != null) { - BluetoothMapMessageListingElement e = null; - // store column index so we dont have to look them up anymore (optimization) - fi.setMmsColumns(mmsCursor); - int cnt = 0; - if(D) Log.d(TAG, "Found " + mmsCursor.getCount() + " mms messages."); - while (mmsCursor.moveToNext()) { - if (matchAddresses(mmsCursor, fi, ap)) { - if(V) printMms(mmsCursor); - e = element(mmsCursor, fi, ap); + if (emailSelected(fi, ap) && folderElement.getEmailFolderId() != -1) { + if(ap.getFilterMessageType() == (BluetoothMapAppParams.FILTER_NO_MMS| + BluetoothMapAppParams.FILTER_NO_SMS_CDMA| + BluetoothMapAppParams.FILTER_NO_SMS_GSM)){ + //set real limit and offset if only this type is used (only if offset/limit is used + limit = " LIMIT " + ap.getMaxListCount()+" OFFSET "+ ap.getStartOffset(); + if(D) Log.d(TAG, "Email Limit => "+limit); + offsetNum = 0; + } + fi.mMsgType = FilterInfo.TYPE_EMAIL; + String where = setWhereFilter(folderElement, fi, ap); + + if(!where.isEmpty()) { + if (D) Log.d(TAG, "msgType: " + fi.mMsgType); + Uri contentUri = Uri.parse(mBaseEmailUri + BluetoothMapContract.TABLE_MESSAGE); + emailCursor = mResolver.query(contentUri, BluetoothMapContract.BT_MESSAGE_PROJECTION, + where, null, BluetoothMapContract.MessageColumns.DATE + " DESC" + limit); + if (emailCursor != null) { + BluetoothMapMessageListingElement e = null; + // store column index so we dont have to look them up anymore (optimization) + fi.setEmailColumns(emailCursor); + int cnt = 0; + while (emailCursor.moveToNext()) { + if(D) Log.d(TAG, "Found " + emailCursor.getCount() + " email messages."); + e = element(emailCursor, fi, ap); bmList.add(e); } + // emailCursor.close(); } } } - } - if (emailSelected(fi, ap) && folderElement.getEmailFolderId() != -1) { - if(ap.getFilterMessageType() == (BluetoothMapAppParams.FILTER_NO_MMS| - BluetoothMapAppParams.FILTER_NO_SMS_CDMA| - BluetoothMapAppParams.FILTER_NO_SMS_GSM)){ - //set real limit and offset if only this type is used (only if offset/limit is used - limit = " LIMIT " + ap.getMaxListCount()+" OFFSET "+ ap.getStartOffset(); - if(D) Log.d(TAG, "Email Limit => "+limit); - offsetNum = 0; - } - fi.mMsgType = FilterInfo.TYPE_EMAIL; - String where = setWhereFilter(folderElement, fi, ap); + /* Enable this if post sorting and segmenting needed */ + bmList.sort(); + bmList.segment(ap.getMaxListCount(), offsetNum); + List<BluetoothMapMessageListingElement> list = bmList.getList(); + int listSize = list.size(); + Cursor tmpCursor = null; + for (int x=0; x<listSize; x++){ + BluetoothMapMessageListingElement ele = list.get(x); + if ((ele.getType().equals(TYPE.SMS_GSM)||ele.getType().equals(TYPE.SMS_CDMA)) && smsCursor != null){ + tmpCursor = smsCursor; + fi.mMsgType = FilterInfo.TYPE_SMS; + } else if (ele.getType().equals(TYPE.MMS) && mmsCursor != null){ + tmpCursor = mmsCursor; + fi.mMsgType = FilterInfo.TYPE_MMS; + } else if (ele.getType().equals(TYPE.EMAIL) && emailCursor != null){ + tmpCursor = emailCursor; + fi.mMsgType = FilterInfo.TYPE_EMAIL; + } - if(!where.isEmpty()) { - if (D) Log.d(TAG, "msgType: " + fi.mMsgType); - Uri contentUri = Uri.parse(mBaseEmailUri + BluetoothMapContract.TABLE_MESSAGE); - emailCursor = mResolver.query(contentUri, BluetoothMapContract.BT_MESSAGE_PROJECTION, - where, null, BluetoothMapContract.MessageColumns.DATE + " DESC" + limit); - if (emailCursor != null) { - BluetoothMapMessageListingElement e = null; - // store column index so we dont have to look them up anymore (optimization) - fi.setEmailColumns(emailCursor); - int cnt = 0; - while (emailCursor.moveToNext()) { - if(V) printEmail(emailCursor); - if(D) Log.d(TAG, "Found " + emailCursor.getCount() + " email messages."); - e = element(emailCursor, fi, ap); - bmList.add(e); - } - // emailCursor.close(); + if (tmpCursor != null && tmpCursor.moveToPosition(ele.getCursorIndex())) { + setSenderAddressing(ele, tmpCursor, fi, ap); + setSenderName(ele, tmpCursor, fi, ap); + setRecipientAddressing(ele, tmpCursor, fi, ap); + setRecipientName(ele, tmpCursor, fi, ap); + setSubject(ele, tmpCursor, fi, ap); + setSize(ele, tmpCursor, fi, ap); + setReceptionStatus(ele, tmpCursor, fi, ap); + setText(ele, tmpCursor, fi, ap); + setAttachmentSize(ele, tmpCursor, fi, ap); + setPriority(ele, tmpCursor, fi, ap); + setSent(ele, tmpCursor, fi, ap); + setProtected(ele, tmpCursor, fi, ap); + setThreadId(ele, tmpCursor, fi, ap); } } + } finally { + close(emailCursor); + close(smsCursor); + close(mmsCursor); } - /* Enable this if post sorting and segmenting needed */ - bmList.sort(); - bmList.segment(ap.getMaxListCount(), offsetNum); - List<BluetoothMapMessageListingElement> list = bmList.getList(); - int listSize = list.size(); - Cursor tmpCursor = null; - for(int x=0;x<listSize;x++){ - BluetoothMapMessageListingElement ele = list.get(x); - if((ele.getType().equals(TYPE.SMS_GSM)||ele.getType().equals(TYPE.SMS_CDMA)) && smsCursor != null){ - tmpCursor = smsCursor; - fi.mMsgType = FilterInfo.TYPE_SMS; - }else if(ele.getType().equals(TYPE.MMS) && mmsCursor != null){ - tmpCursor = mmsCursor; - fi.mMsgType = FilterInfo.TYPE_MMS; - }else if(ele.getType().equals(TYPE.EMAIL) && emailCursor != null){ - tmpCursor = emailCursor; - fi.mMsgType = FilterInfo.TYPE_EMAIL; - } - if(tmpCursor != null){ - tmpCursor.moveToPosition(ele.getCursorIndex()); - setSenderAddressing(ele, tmpCursor, fi, ap); - setSenderName(ele, tmpCursor, fi, ap); - setRecipientAddressing(ele, tmpCursor, fi, ap); - setRecipientName(ele, tmpCursor, fi, ap); - setSubject(ele, tmpCursor, fi, ap); - setSize(ele, tmpCursor, fi, ap); - setReceptionStatus(ele, tmpCursor, fi, ap); - setText(ele, tmpCursor, fi, ap); - setAttachmentSize(ele, tmpCursor, fi, ap); - setPriority(ele, tmpCursor, fi, ap); - setSent(ele, tmpCursor, fi, ap); - setProtected(ele, tmpCursor, fi, ap); - setThreadId(ele, tmpCursor, fi, ap); - } - } - if(emailCursor != null)emailCursor.close(); - if(smsCursor != null)smsCursor.close(); - if(mmsCursor != null)mmsCursor.close(); - if(D)Log.d(TAG, "messagelisting end"); + if (D) Log.d(TAG, "messagelisting end"); return bmList; } @@ -1547,40 +1415,34 @@ public class BluetoothMapContent { FilterInfo fi = new FilterInfo(); setFilterInfo(fi); - if (smsSelected(fi, ap) && folderElement.hasSmsMmsContent()) { + if (smsSelected(fi, ap) && folderElement.hasSmsMmsContent()) { fi.mMsgType = FilterInfo.TYPE_SMS; String where = setWhereFilter(folderElement, fi, ap); Cursor c = mResolver.query(Sms.CONTENT_URI, SMS_PROJECTION, where, null, Sms.DATE + " DESC"); - if (c != null) { - cnt = c.getCount(); - c.close(); - } + if (c != null) cnt = c.getCount(); + close(c); } - if (mmsSelected(fi, ap) && folderElement.hasSmsMmsContent()) { + if (mmsSelected(fi, ap) && folderElement.hasSmsMmsContent()) { fi.mMsgType = FilterInfo.TYPE_MMS; String where = setWhereFilter(folderElement, fi, ap); Cursor c = mResolver.query(Mms.CONTENT_URI, MMS_PROJECTION, where, null, Mms.DATE + " DESC"); - if (c != null) { - cnt += c.getCount(); - c.close(); - } + if (c != null) cnt += c.getCount(); + close(c); } if (emailSelected(fi, ap) && folderElement.getEmailFolderId() != -1) { fi.mMsgType = FilterInfo.TYPE_EMAIL; String where = setWhereFilter(folderElement, fi, ap); - if(!where.isEmpty()) { + if (!where.isEmpty()) { Uri contentUri = Uri.parse(mBaseEmailUri + BluetoothMapContract.TABLE_MESSAGE); Cursor c = mResolver.query(contentUri, BluetoothMapContract.BT_MESSAGE_PROJECTION, where, null, BluetoothMapContract.MessageColumns.DATE + " DESC"); - if (c != null) { - cnt += c.getCount(); - c.close(); - } + if (c != null) cnt += c.getCount(); + close(c); } } @@ -1611,10 +1473,8 @@ public class BluetoothMapContent { Cursor c = mResolver.query(Sms.CONTENT_URI, SMS_PROJECTION, where, null, Sms.DATE + " DESC"); - if (c != null) { - cnt = c.getCount(); - c.close(); - } + if (c != null) cnt += c.getCount(); + close(c); } if (mmsSelected(fi, ap) && folderElement.hasSmsMmsContent()) { @@ -1625,10 +1485,8 @@ public class BluetoothMapContent { Cursor c = mResolver.query(Mms.CONTENT_URI, MMS_PROJECTION, where, null, Sms.DATE + " DESC"); - if (c != null) { - cnt += c.getCount(); - c.close(); - } + if (c != null) cnt += c.getCount(); + close(c); } @@ -1641,10 +1499,8 @@ public class BluetoothMapContent { Uri contentUri = Uri.parse(mBaseEmailUri + BluetoothMapContract.TABLE_MESSAGE); Cursor c = mResolver.query(contentUri, BluetoothMapContract.BT_MESSAGE_PROJECTION, where, null, BluetoothMapContract.MessageColumns.DATE + " DESC"); - if (c != null) { - cnt += c.getCount(); - c.close(); - } + if (c != null) cnt += c.getCount(); + close(c); } } @@ -1701,7 +1557,6 @@ public class BluetoothMapContent { String contactId = null, contactName = null; String[] phoneNumbers = null; String[] emailAddresses = null; - Cursor p; Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phone)); @@ -1711,40 +1566,44 @@ public class BluetoothMapContent { String orderBy = Contacts._ID + " ASC"; // Get the contact _ID and name - p = mResolver.query(uri, projection, selection, null, orderBy); - if (p != null && p.getCount() >= 1) { - p.moveToFirst(); - contactId = p.getString(p.getColumnIndex(Contacts._ID)); - contactName = p.getString(p.getColumnIndex(Contacts.DISPLAY_NAME)); - } - p.close(); - - // Bail out if we are unable to find a contact, based on the phone number - if(contactId == null) { - phoneNumbers = new String[1]; - phoneNumbers[0] = phone; - } else { - // use only actual phone number - phoneNumbers = new String[1]; - phoneNumbers[0] = phone; + Cursor p = mResolver.query(uri, projection, selection, null, orderBy); - // Fetch contact e-mail addresses - p = mResolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, - ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", - new String[]{contactId}, - null); - if(p != null) { - int i = 0; - emailAddresses = new String[p.getCount()]; - while (p != null && p.moveToNext()) { - String emailAddress = p.getString( - p.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS)); - emailAddresses[i++] = emailAddress; + try { + if (p != null && p.moveToFirst()) { + contactId = p.getString(p.getColumnIndex(Contacts._ID)); + contactName = p.getString(p.getColumnIndex(Contacts.DISPLAY_NAME)); + } + + // Bail out if we are unable to find a contact, based on the phone number + if(contactId == null) { + phoneNumbers = new String[1]; + phoneNumbers[0] = phone; + } else { + // use only actual phone number + phoneNumbers = new String[1]; + phoneNumbers[0] = phone; + + // Fetch contact e-mail addresses + close (p); + p = mResolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, + ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", + new String[]{contactId}, + null); + if (p != null) { + int i = 0; + emailAddresses = new String[p.getCount()]; + while (p != null && p.moveToNext()) { + String emailAddress = p.getString( + p.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS)); + emailAddresses[i++] = emailAddress; + } } - p.close(); } + } finally { + close(p); } - if(incoming == true) { + + if (incoming == true) { if(V) Log.d(TAG, "Adding originator for phone:" + phone); message.addOriginator(contactName, contactName, phoneNumbers, emailAddresses); // Use version 3.0 as we only have a formatted name } else { @@ -1763,10 +1622,13 @@ public class BluetoothMapContent { String msgBody; BluetoothMapbMessageSms message = new BluetoothMapbMessageSms(); TelephonyManager tm = (TelephonyManager)mContext.getSystemService(Context.TELEPHONY_SERVICE); + Cursor c = mResolver.query(Sms.CONTENT_URI, SMS_PROJECTION, "_ID = " + id, null, null); + if (c == null || !c.moveToFirst()) { + throw new IllegalArgumentException("SMS handle not found"); + } - if(c != null && c.moveToFirst()) - { + try { if(V) Log.v(TAG,"c.count: " + c.getCount()); if (tm.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM) { @@ -1803,12 +1665,11 @@ public class BluetoothMapContent { } else /*if (charset == MAP_MESSAGE_CHARSET_UTF8)*/ { message.setSmsBody(msgBody); } - - c.close(); - - return message.encode(); + } finally { + close(c); } - throw new IllegalArgumentException("SMS handle not found"); + + return message.encode(); } private void extractMmsAddresses(long id, BluetoothMapbMessageMms message) { @@ -1817,9 +1678,10 @@ public class BluetoothMapContent { String uriStr = new String(Mms.CONTENT_URI + "/" + id + "/addr"); Uri uriAddress = Uri.parse(uriStr); String contactName = null; + Cursor c = mResolver.query( uriAddress, projection, selection, null, null); - if (c.moveToFirst()) { - do { + try { + while (c != null && c.moveToNext()) { String address = c.getString(c.getColumnIndex(Mms.Addr.ADDRESS)); if(address.equals(INSERT_ADDRES_TOKEN)) continue; @@ -1844,7 +1706,9 @@ public class BluetoothMapContent { default: break; } - } while(c.moveToNext()); + } + } finally { + close (c); } } @@ -1873,11 +1737,8 @@ public class BluetoothMapContent { // do nothing for now Log.w(TAG,"Error reading part data",e); } finally { - try { - os.close(); - is.close(); - } catch (IOException e) { - } + close(os); + close(is); } return retVal; } @@ -1896,14 +1757,10 @@ public class BluetoothMapContent { String uriStr = new String(Mms.CONTENT_URI + "/"+ id + "/part"); Uri uriAddress = Uri.parse(uriStr); BluetoothMapbMessageMms.MimePart part; - Cursor c = mResolver.query( - uriAddress, - projection, - selection, - null, null); - - if (c.moveToFirst()) { - do { + Cursor c = mResolver.query(uriAddress, projection, selection, null, null); + + try { + while(c != null && c.moveToNext()) { Long partId = c.getLong(c.getColumnIndex(BaseColumns._ID)); String contentType = c.getString(c.getColumnIndex(Mms.Part.CONTENT_TYPE)); String name = c.getString(c.getColumnIndex(Mms.Part.NAME)); @@ -1950,11 +1807,13 @@ public class BluetoothMapContent { Log.d(TAG,"extractMmsParts",e); part.mData = null; part.mCharsetName = null; - } finally { } part.mFileName = filename; - } while(c.moveToNext()); + } + } finally { + close(c); } + message.updateCharset(); } @@ -1973,8 +1832,11 @@ public class BluetoothMapContent { BluetoothMapbMessageMms message = new BluetoothMapbMessageMms(); Cursor c = mResolver.query(Mms.CONTENT_URI, MMS_PROJECTION, "_ID = " + id, null, null); - if(c != null && c.moveToFirst()) - { + if (c == null || !c.moveToFirst()) { + throw new IllegalArgumentException("MMS handle not found"); + } + + try { message.setType(TYPE.MMS); // The MMS info: @@ -1993,23 +1855,14 @@ public class BluetoothMapContent { message.setDate(c.getLong(c.getColumnIndex(Mms.DATE)) * 1000L); message.setTextOnly(c.getInt(c.getColumnIndex(Mms.TEXT_ONLY)) == 0 ? false : true); message.setIncludeAttachments(appParams.getAttachment() == 0 ? false : true); - // c.getLong(c.getColumnIndex(Mms.DATE_SENT)); - this is never used - // c.getInt(c.getColumnIndex(Mms.STATUS)); - don't know what this is - // The parts extractMmsParts(id, message); - - // The addresses extractMmsAddresses(id, message); - - c.close(); - - return message.encode(); - } else if(c != null) { - c.close(); + } finally { + close(c); } - throw new IllegalArgumentException("MMS handle not found"); + return message.encode(); } /** @@ -2037,11 +1890,12 @@ public class BluetoothMapContent { BluetoothMapbMessageEmail message = new BluetoothMapbMessageEmail(); Uri contentUri = Uri.parse(mBaseEmailUri + BluetoothMapContract.TABLE_MESSAGE); Cursor c = mResolver.query(contentUri, BluetoothMapContract.BT_MESSAGE_PROJECTION, "_ID = " + id, null, null); - if(c != null && c.moveToFirst()) - { + if (c != null && c.moveToFirst()) { + throw new IllegalArgumentException("EMAIL handle not found"); + } + + try { BluetoothMapFolderElement folderElement; - FileInputStream is = null; - ParcelFileDescriptor fd = null; // Handle fraction requests int fractionRequest = appParams.getFractionRequest(); @@ -2106,7 +1960,6 @@ public class BluetoothMapContent { i++; } } - c.close(); // Find out if we get attachments String attStr = (appParams.getAttachment() == 0) ? "/" + BluetoothMapContract.FILE_MSG_NO_ATTACHMENTS : ""; @@ -2114,6 +1967,9 @@ public class BluetoothMapContent { // Get email message body content int count = 0; + FileInputStream is = null; + ParcelFileDescriptor fd = null; + try { fd = mResolver.openFileDescriptor(uri, "r"); is = new FileInputStream(fd.getFileDescriptor()); @@ -2132,21 +1988,14 @@ public class BluetoothMapContent { Log.w(TAG, e); } catch (IOException e) { Log.w(TAG, e); + } finally { + close(is); + close(fd); } - finally { - try { - if(is != null) - is.close(); - } catch (IOException e) {} - try { - if(fd != null) - fd.close(); - } catch (IOException e) {} - } - return message.encode(); - } else if(c != null) { - c.close(); + } finally { + close(c); } - throw new IllegalArgumentException("EMAIL handle not found"); + + return message.encode(); } } diff --git a/src/com/android/bluetooth/map/BluetoothMapContentObserver.java b/src/com/android/bluetooth/map/BluetoothMapContentObserver.java index 7101f220b..dd14c82be 100644 --- a/src/com/android/bluetooth/map/BluetoothMapContentObserver.java +++ b/src/com/android/bluetooth/map/BluetoothMapContentObserver.java @@ -14,6 +14,7 @@ */ package com.android.bluetooth.map; +import java.io.Closeable; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; @@ -116,6 +117,13 @@ public class BluetoothMapContentObserver { private TYPE mSmsType; + private static void close(Closeable c) { + try { + if (c != null) c.close(); + } catch (IOException e) { + } + } + static final String[] SMS_PROJECTION = new String[] { Sms._ID, Sms.THREAD_ID, @@ -452,17 +460,19 @@ public class BluetoothMapContentObserver { Cursor c = mResolver.query(Sms.CONTENT_URI, SMS_PROJECTION_SHORT, null, null, null); - if (c != null && c.moveToFirst()) { - do { + try { + while (c != null && c.moveToNext()) { long id = c.getLong(c.getColumnIndex(Sms._ID)); int type = c.getInt(c.getColumnIndex(Sms.TYPE)); int threadId = c.getInt(c.getColumnIndex(Sms.THREAD_ID)); Msg msg = new Msg(id, type, threadId); msgListSms.put(id, msg); - } while (c.moveToNext()); - c.close(); + } + } finally { + close(c); } + synchronized(mMsgListSms) { mMsgListSms.clear(); mMsgListSms = msgListSms; @@ -472,17 +482,19 @@ public class BluetoothMapContentObserver { c = mResolver.query(Mms.CONTENT_URI, MMS_PROJECTION_SHORT, null, null, null); - if (c != null && c.moveToFirst()) { - do { + try { + while (c != null && c.moveToNext()) { long id = c.getLong(c.getColumnIndex(Mms._ID)); int type = c.getInt(c.getColumnIndex(Mms.MESSAGE_BOX)); int threadId = c.getInt(c.getColumnIndex(Mms.THREAD_ID)); Msg msg = new Msg(id, type, threadId); msgListMms.put(id, msg); - } while (c.moveToNext()); - c.close(); + } + } finally { + close(c); } + synchronized(mMsgListMms) { mMsgListMms.clear(); mMsgListMms = msgListMms; @@ -494,16 +506,18 @@ public class BluetoothMapContentObserver { Uri uri = mMessageUri; Cursor c = mProviderClient.query(uri, EMAIL_PROJECTION_SHORT, null, null, null); - if (c != null && c.moveToFirst()) { - do { + try { + while (c != null && c.moveToNext()) { long id = c.getLong(c.getColumnIndex(MessageColumns._ID)); long folderId = c.getInt(c.getColumnIndex(BluetoothMapContract.MessageColumns.FOLDER_ID)); Msg msg = new Msg(id, folderId); msgListEmail.put(id, msg); - } while (c.moveToNext()); - c.close(); + } + } finally { + close(c); } + synchronized(mMsgListEmail) { mMsgListEmail.clear(); mMsgListEmail = msgListEmail; @@ -520,8 +534,8 @@ public class BluetoothMapContentObserver { SMS_PROJECTION_SHORT, null, null, null); synchronized(mMsgListSms) { - if (c != null && c.moveToFirst()) { - do { + try { + while (c != null && c.moveToNext()) { long id = c.getLong(c.getColumnIndex(Sms._ID)); int type = c.getInt(c.getColumnIndex(Sms.TYPE)); int threadId = c.getInt(c.getColumnIndex(Sms.THREAD_ID)); @@ -570,8 +584,9 @@ public class BluetoothMapContentObserver { } msgListSms.put(id, msg); } - } while (c.moveToNext()); - c.close(); + } + } finally { + close(c); } for (Msg msg : mMsgListSms.values()) { @@ -594,8 +609,8 @@ public class BluetoothMapContentObserver { MMS_PROJECTION_SHORT, null, null, null); synchronized(mMsgListMms) { - if (c != null && c.moveToFirst()) { - do { + try { + while (c != null && c.moveToNext()) { long id = c.getLong(c.getColumnIndex(Mms._ID)); int type = c.getInt(c.getColumnIndex(Mms.MESSAGE_BOX)); int mtype = c.getInt(c.getColumnIndex(Mms.MESSAGE_TYPE)); @@ -657,8 +672,9 @@ public class BluetoothMapContentObserver { } msgListMms.put(id, msg); } - } while (c.moveToNext()); - c.close(); + } + } finally { + close(c); } for (Msg msg : mMsgListMms.values()) { @@ -681,8 +697,8 @@ public class BluetoothMapContentObserver { Cursor c = mProviderClient.query(mMessageUri, EMAIL_PROJECTION_SHORT, null, null, null); synchronized(mMsgListEmail) { - if (c != null && c.moveToFirst()) { - do { + try { + while (c != null && c.moveToNext()) { long id = c.getLong(c.getColumnIndex(BluetoothMapContract.MessageColumns._ID)); int folderId = c.getInt(c.getColumnIndex( BluetoothMapContract.MessageColumns.FOLDER_ID)); @@ -750,8 +766,9 @@ public class BluetoothMapContentObserver { } msgListEmail.put(id, msg); } - } while (c.moveToNext()); - c.close(); + } + } finally { + close(c); } // For all messages no longer in the database send a delete notification @@ -893,31 +910,34 @@ public class BluetoothMapContentObserver { boolean res = false; Uri uri = ContentUris.withAppendedId(Mms.CONTENT_URI, handle); Cursor c = mResolver.query(uri, null, null, null, null); - if (c != null && c.moveToFirst()) { - /* Move to deleted folder, or delete if already in deleted folder */ - int threadId = c.getInt(c.getColumnIndex(Mms.THREAD_ID)); - if (threadId != DELETED_THREAD_ID) { - /* Set deleted thread id */ - synchronized(mMsgListMms) { - Msg msg = mMsgListMms.get(handle); - if(msg != null) { // This will always be the case - msg.threadId = DELETED_THREAD_ID; + + try { + if (c != null && c.moveToFirst()) { + /* Move to deleted folder, or delete if already in deleted folder */ + int threadId = c.getInt(c.getColumnIndex(Mms.THREAD_ID)); + if (threadId != DELETED_THREAD_ID) { + /* Set deleted thread id */ + synchronized(mMsgListMms) { + Msg msg = mMsgListMms.get(handle); + if(msg != null) { // This will always be the case + msg.threadId = DELETED_THREAD_ID; + } } + updateThreadId(uri, Mms.THREAD_ID, DELETED_THREAD_ID); + } else { + /* Delete from observer message list to avoid delete notifications */ + synchronized(mMsgListMms) { + mMsgListMms.remove(handle); + } + /* Delete message */ + mResolver.delete(uri, null, null); } - updateThreadId(uri, Mms.THREAD_ID, DELETED_THREAD_ID); - } else { - /* Delete from observer message list to avoid delete notifications */ - synchronized(mMsgListMms) { - mMsgListMms.remove(handle); - } - /* Delete message */ - mResolver.delete(uri, null, null); + res = true; } - res = true; - } - if (c != null) { - c.close(); + } finally { + close(c); } + return res; } @@ -926,40 +946,42 @@ public class BluetoothMapContentObserver { Uri uri = ContentUris.withAppendedId(Mms.CONTENT_URI, handle); Cursor c = mResolver.query(uri, null, null, null, null); - if (c != null && c.moveToFirst()) { - int threadId = c.getInt(c.getColumnIndex(Mms.THREAD_ID)); - if (threadId == DELETED_THREAD_ID) { - /* Restore thread id from address, or if no thread for address - * create new thread by insert and remove of fake message */ - String address; - long id = c.getLong(c.getColumnIndex(Mms._ID)); - int msgBox = c.getInt(c.getColumnIndex(Mms.MESSAGE_BOX)); - if (msgBox == Mms.MESSAGE_BOX_INBOX) { - address = BluetoothMapContent.getAddressMms(mResolver, id, - BluetoothMapContent.MMS_FROM); - } else { - address = BluetoothMapContent.getAddressMms(mResolver, id, - BluetoothMapContent.MMS_TO); - } - Set<String> recipients = new HashSet<String>(); - recipients.addAll(Arrays.asList(address)); - Long oldThreadId = Telephony.Threads.getOrCreateThreadId(mContext, recipients); - synchronized(mMsgListMms) { - Msg msg = mMsgListMms.get(handle); - if(msg != null) { // This will always be the case - msg.threadId = oldThreadId.intValue(); + try { + if (c != null && c.moveToFirst()) { + int threadId = c.getInt(c.getColumnIndex(Mms.THREAD_ID)); + if (threadId == DELETED_THREAD_ID) { + /* Restore thread id from address, or if no thread for address + * create new thread by insert and remove of fake message */ + String address; + long id = c.getLong(c.getColumnIndex(Mms._ID)); + int msgBox = c.getInt(c.getColumnIndex(Mms.MESSAGE_BOX)); + if (msgBox == Mms.MESSAGE_BOX_INBOX) { + address = BluetoothMapContent.getAddressMms(mResolver, id, + BluetoothMapContent.MMS_FROM); + } else { + address = BluetoothMapContent.getAddressMms(mResolver, id, + BluetoothMapContent.MMS_TO); + } + Set<String> recipients = new HashSet<String>(); + recipients.addAll(Arrays.asList(address)); + Long oldThreadId = Telephony.Threads.getOrCreateThreadId(mContext, recipients); + synchronized(mMsgListMms) { + Msg msg = mMsgListMms.get(handle); + if(msg != null) { // This will always be the case + msg.threadId = oldThreadId.intValue(); + } } + updateThreadId(uri, Mms.THREAD_ID, oldThreadId); + } else { + Log.d(TAG, "Message not in deleted folder: handle " + handle + + " threadId " + threadId); } - updateThreadId(uri, Mms.THREAD_ID, oldThreadId); - } else { - Log.d(TAG, "Message not in deleted folder: handle " + handle - + " threadId " + threadId); + res = true; } - res = true; - } - if (c != null) { - c.close(); + } finally { + close(c); } + return res; } @@ -968,31 +990,33 @@ public class BluetoothMapContentObserver { Uri uri = ContentUris.withAppendedId(Sms.CONTENT_URI, handle); Cursor c = mResolver.query(uri, null, null, null, null); - if (c != null && c.moveToFirst()) { - /* Move to deleted folder, or delete if already in deleted folder */ - int threadId = c.getInt(c.getColumnIndex(Sms.THREAD_ID)); - if (threadId != DELETED_THREAD_ID) { - synchronized(mMsgListSms) { - Msg msg = mMsgListSms.get(handle); - if(msg != null) { // This will always be the case - msg.threadId = DELETED_THREAD_ID; + try { + if (c != null && c.moveToFirst()) { + /* Move to deleted folder, or delete if already in deleted folder */ + int threadId = c.getInt(c.getColumnIndex(Sms.THREAD_ID)); + if (threadId != DELETED_THREAD_ID) { + synchronized(mMsgListSms) { + Msg msg = mMsgListSms.get(handle); + if(msg != null) { // This will always be the case + msg.threadId = DELETED_THREAD_ID; + } } + /* Set deleted thread id */ + updateThreadId(uri, Sms.THREAD_ID, DELETED_THREAD_ID); + } else { + /* Delete from observer message list to avoid delete notifications */ + synchronized(mMsgListSms) { + mMsgListSms.remove(handle); + } + /* Delete message */ + mResolver.delete(uri, null, null); } - /* Set deleted thread id */ - updateThreadId(uri, Sms.THREAD_ID, DELETED_THREAD_ID); - } else { - /* Delete from observer message list to avoid delete notifications */ - synchronized(mMsgListSms) { - mMsgListSms.remove(handle); - } - /* Delete message */ - mResolver.delete(uri, null, null); + res = true; } - res = true; - } - if (c != null) { - c.close(); + } finally { + close(c); } + return res; } @@ -1001,29 +1025,31 @@ public class BluetoothMapContentObserver { Uri uri = ContentUris.withAppendedId(Sms.CONTENT_URI, handle); Cursor c = mResolver.query(uri, null, null, null, null); - if (c != null && c.moveToFirst()) { - int threadId = c.getInt(c.getColumnIndex(Sms.THREAD_ID)); - if (threadId == DELETED_THREAD_ID) { - String address = c.getString(c.getColumnIndex(Sms.ADDRESS)); - Set<String> recipients = new HashSet<String>(); - recipients.addAll(Arrays.asList(address)); - Long oldThreadId = Telephony.Threads.getOrCreateThreadId(mContext, recipients); - synchronized(mMsgListSms) { - Msg msg = mMsgListSms.get(handle); - if(msg != null) { // This will always be the case - msg.threadId = oldThreadId.intValue(); // The threadId is specified as an int, so it is safe to truncate + try { + if (c != null && c.moveToFirst()) { + int threadId = c.getInt(c.getColumnIndex(Sms.THREAD_ID)); + if (threadId == DELETED_THREAD_ID) { + String address = c.getString(c.getColumnIndex(Sms.ADDRESS)); + Set<String> recipients = new HashSet<String>(); + recipients.addAll(Arrays.asList(address)); + Long oldThreadId = Telephony.Threads.getOrCreateThreadId(mContext, recipients); + synchronized(mMsgListSms) { + Msg msg = mMsgListSms.get(handle); + if(msg != null) { // This will always be the case + msg.threadId = oldThreadId.intValue(); // The threadId is specified as an int, so it is safe to truncate + } } + updateThreadId(uri, Sms.THREAD_ID, oldThreadId); + } else { + Log.d(TAG, "Message not in deleted folder: handle " + handle + + " threadId " + threadId); } - updateThreadId(uri, Sms.THREAD_ID, oldThreadId); - } else { - Log.d(TAG, "Message not in deleted folder: handle " + handle - + " threadId " + threadId); + res = true; } - res = true; - } - if (c != null) { - c.close(); + } finally { + close(c); } + return res; } @@ -1073,7 +1099,6 @@ public class BluetoothMapContentObserver { if (type == TYPE.SMS_GSM || type == TYPE.SMS_CDMA) { Uri uri = Sms.Inbox.CONTENT_URI;//ContentUris.withAppendedId(Sms.CONTENT_URI, handle); - Cursor c = mResolver.query(uri, null, null, null, null); ContentValues contentValues = new ContentValues(); contentValues.put(Sms.READ, statusValue); contentValues.put(Sms.SEEN, statusValue); @@ -1082,15 +1107,15 @@ public class BluetoothMapContentObserver { if (D) Log.d(TAG, " -> SMS Uri: " + uri.toString() + " Where " + where + " values " + values); count = mResolver.update(uri, contentValues, where, null); if (D) Log.d(TAG, " -> "+count +" rows updated!"); + } else if (type == TYPE.MMS) { Uri uri = ContentUris.withAppendedId(Mms.CONTENT_URI, handle); - Cursor c = mResolver.query(uri, null, null, null, null); if (D) Log.d(TAG, " -> MMS Uri: " + uri.toString()); ContentValues contentValues = new ContentValues(); contentValues.put(Mms.READ, statusValue); count = mResolver.update(uri, contentValues, null, null); - if (D) Log.d(TAG, " -> "+count +" rows updated!"); + } if (type == TYPE.EMAIL) { Uri uri = mMessageUri; ContentValues contentValues = new ContentValues(); @@ -1098,10 +1123,8 @@ public class BluetoothMapContentObserver { contentValues.put(BluetoothMapContract.MessageColumns._ID, handle); count = mProviderClient.update(uri, contentValues, null, null); } - if(count < 1) { - return false; - } - return true; + + return (count > 0); } private class PushMsgInfo { @@ -1267,18 +1290,20 @@ public class BluetoothMapContentObserver { return -1; } Cursor c = mResolver.query(uri, SMS_PROJECTION_SHORT, null, null, null); - - /* Extract the data for the inserted message, and store in local mirror, to - * avoid sending a NewMessage Event. */ - if (c != null && c.moveToFirst()) { - long id = c.getLong(c.getColumnIndex(Sms._ID)); - int type = c.getInt(c.getColumnIndex(Sms.TYPE)); - int threadId = c.getInt(c.getColumnIndex(Sms.THREAD_ID)); - Msg newMsg = new Msg(id, type, threadId); - mMsgListSms.put(id, newMsg); - c.close(); - } else { - return -1; // This can only happen, if the message is deleted just as it is added + try { + /* Extract the data for the inserted message, and store in local mirror, to + * avoid sending a NewMessage Event. */ + if (c != null && c.moveToFirst()) { + long id = c.getLong(c.getColumnIndex(Sms._ID)); + int type = c.getInt(c.getColumnIndex(Sms.TYPE)); + int threadId = c.getInt(c.getColumnIndex(Sms.THREAD_ID)); + Msg newMsg = new Msg(id, type, threadId); + mMsgListSms.put(id, newMsg); + } else { + return -1; // This can only happen, if the message is deleted just as it is added + } + } finally { + close(c); } handle = Long.parseLong(uri.getLastPathSegment()); @@ -1335,28 +1360,28 @@ public class BluetoothMapContentObserver { } } - private void moveDraftToOutbox(long handle) { /*Move message by changing the msg_box value in the content provider database */ - if (handle != -1) { - String whereClause = " _id= " + handle; - Uri uri = Mms.CONTENT_URI; - Cursor queryResult = mResolver.query(uri, null, whereClause, null, null); - if (queryResult != null) { - if (queryResult.getCount() > 0) { - queryResult.moveToFirst(); - ContentValues data = new ContentValues(); - /* set folder to be outbox */ - data.put(Mms.MESSAGE_BOX, Mms.MESSAGE_BOX_OUTBOX); - mResolver.update(uri, data, whereClause, null); - if (D) Log.d(TAG, "moved draft MMS to outbox"); - } - queryResult.close(); - }else { + if (handle != -1) return; + + String whereClause = " _id= " + handle; + Uri uri = Mms.CONTENT_URI; + Cursor queryResult = mResolver.query(uri, null, whereClause, null, null); + try { + if (queryResult != null && queryResult.moveToFirst()) { + ContentValues data = new ContentValues(); + /* set folder to be outbox */ + data.put(Mms.MESSAGE_BOX, Mms.MESSAGE_BOX_OUTBOX); + mResolver.update(uri, data, whereClause, null); + if (D) Log.d(TAG, "Moved draft MMS to outbox"); + } else { if (D) Log.d(TAG, "Could not move draft to outbox "); } + } finally { + queryResult.close(); } } + private long pushMmsToFolder(int folder, String to_address, BluetoothMapbMessageMms msg) { /** * strategy: @@ -1400,7 +1425,6 @@ public class BluetoothMapContentObserver { Uri uri = Mms.CONTENT_URI; synchronized (mMsgListMms) { - uri = mResolver.insert(uri, values); if (uri == null) { @@ -1412,19 +1436,21 @@ public class BluetoothMapContentObserver { doing the query ensures we get any changes made by the content provider at insert. */ Cursor c = mResolver.query(uri, MMS_PROJECTION_SHORT, null, null, null); + try { + if (c != null && c.moveToFirst()) { + long id = c.getLong(c.getColumnIndex(Mms._ID)); + int type = c.getInt(c.getColumnIndex(Mms.MESSAGE_BOX)); + int threadId = c.getInt(c.getColumnIndex(Mms.THREAD_ID)); - if (c != null && c.moveToFirst()) { - long id = c.getLong(c.getColumnIndex(Mms._ID)); - int type = c.getInt(c.getColumnIndex(Mms.MESSAGE_BOX)); - int threadId = c.getInt(c.getColumnIndex(Mms.THREAD_ID)); - - /* We must filter out any actions made by the MCE. Add the new message to - * the list of known messages. */ + /* We must filter out any actions made by the MCE. Add the new message to + * the list of known messages. */ - Msg newMsg = new Msg(id, type, threadId); - newMsg.localInitiatedSend = true; - mMsgListMms.put(id, newMsg); - c.close(); + Msg newMsg = new Msg(id, type, threadId); + newMsg.localInitiatedSend = true; + mMsgListMms.put(id, newMsg); + } + } finally { + close(c); } } // Done adding changes, unlock access to mMsgListMms to allow sending MMS events again @@ -1437,9 +1463,9 @@ public class BluetoothMapContentObserver { Log.w(TAG, "No MMS parts present..."); } else { if(V) Log.v(TAG, "Adding " + msg.getMimeParts().size() + " parts to the data base."); + int count = 0; for(MimePart part : msg.getMimeParts()) { - int count = 0; - count++; + ++count; values.clear(); if(part.mContentType != null && part.mContentType.toUpperCase().contains("TEXT")) { values.put(Mms.Part.CONTENT_TYPE, "text/plain"); @@ -1912,11 +1938,10 @@ public class BluetoothMapContentObserver { private void resendPendingMessages() { /* Send pending messages in outbox */ String where = "type = " + Sms.MESSAGE_TYPE_OUTBOX; - Cursor c = mResolver.query(Sms.CONTENT_URI, SMS_PROJECTION, where, null, - null); + Cursor c = mResolver.query(Sms.CONTENT_URI, SMS_PROJECTION, where, null, null); - if (c != null && c.moveToFirst()) { - do { + try { + while (c!= null && c.moveToNext()) { long id = c.getLong(c.getColumnIndex(Sms._ID)); String msgBody = c.getString(c.getColumnIndex(Sms.BODY)); PushMsgInfo msgInfo = mPushMsgList.get(id); @@ -1925,19 +1950,20 @@ public class BluetoothMapContentObserver { } msgInfo.sendInProgress = true; sendMessage(msgInfo, msgBody); - } while (c.moveToNext()); - c.close(); + } + } finally { + close(c); } } private void failPendingMessages() { /* Move pending messages from outbox to failed */ String where = "type = " + Sms.MESSAGE_TYPE_OUTBOX; - Cursor c = mResolver.query(Sms.CONTENT_URI, SMS_PROJECTION, where, null, - null); + Cursor c = mResolver.query(Sms.CONTENT_URI, SMS_PROJECTION, where, null, null); + if (c == null) return; - if (c != null && c.moveToFirst()) { - do { + try { + while (c!= null && c.moveToNext()) { long id = c.getLong(c.getColumnIndex(Sms._ID)); String msgBody = c.getString(c.getColumnIndex(Sms.BODY)); PushMsgInfo msgInfo = mPushMsgList.get(id); @@ -1946,9 +1972,10 @@ public class BluetoothMapContentObserver { } Sms.moveMessageToFolder(mContext, msgInfo.uri, Sms.MESSAGE_TYPE_FAILED, 0); - } while (c.moveToNext()); + } + } finally { + close(c); } - if (c != null) c.close(); } private void removeDeletedMessages() { @@ -1989,5 +2016,4 @@ public class BluetoothMapContentObserver { } return false; } - } diff --git a/src/com/android/bluetooth/map/BluetoothMapEmailSettingsLoader.java b/src/com/android/bluetooth/map/BluetoothMapEmailSettingsLoader.java index d42a8088b..c6a6ca70f 100644 --- a/src/com/android/bluetooth/map/BluetoothMapEmailSettingsLoader.java +++ b/src/com/android/bluetooth/map/BluetoothMapEmailSettingsLoader.java @@ -151,28 +151,16 @@ public class BluetoothMapEmailSettingsLoader { } mProviderClient.setDetectNotResponding(PROVIDER_ANR_TIMEOUT); - - Uri uri = Uri.parse(app.mBase_uri_no_account + "/" + BluetoothMapContract.TABLE_ACCOUNT); - - c = mProviderClient.query(uri, BluetoothMapContract.BT_ACCOUNT_PROJECTION, null, null, BluetoothMapContract.AccountColumns._ID+" DESC"); - } catch (RemoteException e){ - if(D)Log.d(TAG,"Could not establish ContentProviderClient for "+app.getPackageName()+ - " - returning empty account list" ); - return children; - } - - if (c != null) { - c.moveToPosition(-1); - while (c.moveToNext()) { - if(D)Log.d(TAG,"Adding account " +c.getString(c.getColumnIndex(BluetoothMapContract.AccountColumns.ACCOUNT_DISPLAY_NAME))+ - " with ID "+String.valueOf((c.getInt(c.getColumnIndex(BluetoothMapContract.AccountColumns._ID))))); - + Uri uri = Uri.parse(app.mBase_uri_no_account + "/" + BluetoothMapContract.TABLE_ACCOUNT); + c = mProviderClient.query(uri, BluetoothMapContract.BT_ACCOUNT_PROJECTION, null, null, + BluetoothMapContract.AccountColumns._ID+" DESC"); + while (c != null && c.moveToNext()) { BluetoothMapEmailSettingsItem child = new BluetoothMapEmailSettingsItem( - /*id*/ String.valueOf((c.getInt(c.getColumnIndex(BluetoothMapContract.AccountColumns._ID)))), - /*name*/ c.getString(c.getColumnIndex(BluetoothMapContract.AccountColumns.ACCOUNT_DISPLAY_NAME)) , - /*package name*/ app.getPackageName(), - /*providerMeta*/ app.getProviderAuthority(), - /*icon*/ null); + String.valueOf((c.getInt(c.getColumnIndex(BluetoothMapContract.AccountColumns._ID)))), + c.getString(c.getColumnIndex(BluetoothMapContract.AccountColumns.ACCOUNT_DISPLAY_NAME)) , + app.getPackageName(), + app.getProviderAuthority(), + null); child.mIsChecked = (c.getInt(c.getColumnIndex(BluetoothMapContract.AccountColumns.FLAG_EXPOSE))!=0); /*update the account counter so we can make sure that not to many accounts are checked. */ @@ -182,9 +170,11 @@ public class BluetoothMapEmailSettingsLoader { } children.add(child); } - c.close(); - } else { - if(D)Log.d(TAG, "query failed"); + } catch (RemoteException e){ + if(D)Log.d(TAG,"Could not establish ContentProviderClient for "+app.getPackageName()+ + " - returning empty account list" ); + } finally { + if (c != null) c.close(); } return children; } @@ -198,5 +188,4 @@ public class BluetoothMapEmailSettingsLoader { if(D)Log.d(TAG,"Enabled Accounts count:"+ mAccountsEnabledCount); return mAccountsEnabledCount; } - } diff --git a/src/com/android/bluetooth/map/BluetoothMapObexServer.java b/src/com/android/bluetooth/map/BluetoothMapObexServer.java index fe2207e4e..7b321d1fb 100644 --- a/src/com/android/bluetooth/map/BluetoothMapObexServer.java +++ b/src/com/android/bluetooth/map/BluetoothMapObexServer.java @@ -212,17 +212,15 @@ public class BluetoothMapObexServer extends ServerRequestHandler { " = " + parentFolder.getEmailFolderId(); Cursor c = mProviderClient.query(mEmailFolderUri, BluetoothMapContract.BT_FOLDER_PROJECTION, where, null, null); - if (c != null) { - c.moveToPosition(-1); - while (c.moveToNext()) { + try { + while (c != null && c.moveToNext()) { String name = c.getString(c.getColumnIndex(BluetoothMapContract.FolderColumns.NAME)); long id = c.getLong(c.getColumnIndex(BluetoothMapContract.FolderColumns._ID)); newFolder = parentFolder.addEmailFolder(name, id); addEmailFolders(newFolder); // Use recursion to add any sub folders } - c.close(); - } else { - if (D) Log.d(TAG, "addEmailFolders(): no elements found"); + } finally { + if (c != null) c.close(); } } |