Messaging: Use try-with-resource

Change-Id: I6649517cbd990502dfcb54f078764cf05f210268
This commit is contained in:
Michael W
2024-12-26 15:54:37 +01:00
parent 2a09fa3cb9
commit 9538179c75
24 changed files with 199 additions and 455 deletions
@@ -251,22 +251,16 @@ public class BugleDatabaseOperations {
Assert.isNotMainThread();
String conversationId = null;
Cursor cursor = null;
try {
try (Cursor cursor = dbWrapper.rawQuery("SELECT " + ConversationColumns._ID
+ " FROM " + DatabaseHelper.CONVERSATIONS_TABLE
+ " WHERE " + ConversationColumns.SMS_THREAD_ID + "=" + threadId,
null)) {
// Look for an existing conversation in the db with this thread id
cursor = dbWrapper.rawQuery("SELECT " + ConversationColumns._ID
+ " FROM " + DatabaseHelper.CONVERSATIONS_TABLE
+ " WHERE " + ConversationColumns.SMS_THREAD_ID + "=" + threadId,
null);
if (cursor.moveToFirst()) {
Assert.isTrue(cursor.getCount() == 1);
conversationId = cursor.getString(0);
}
} finally {
if (cursor != null) {
cursor.close();
}
}
return conversationId;
@@ -285,13 +279,11 @@ public class BugleDatabaseOperations {
Assert.isNotMainThread();
long threadId = -1;
Cursor cursor = null;
try {
cursor = dbWrapper.query(DatabaseHelper.CONVERSATIONS_TABLE,
new String[] { ConversationColumns.SMS_THREAD_ID },
ConversationColumns._ID + " =?",
new String[] { conversationId },
null, null, null);
try (Cursor cursor = dbWrapper.query(DatabaseHelper.CONVERSATIONS_TABLE,
new String[]{ConversationColumns.SMS_THREAD_ID},
ConversationColumns._ID + " =?",
new String[]{conversationId},
null, null, null)) {
if (cursor.moveToFirst()) {
Assert.isTrue(cursor.getCount() == 1);
@@ -299,10 +291,6 @@ public class BugleDatabaseOperations {
threadId = cursor.getLong(0);
}
}
} finally {
if (cursor != null) {
cursor.close();
}
}
return threadId;
@@ -320,23 +308,17 @@ public class BugleDatabaseOperations {
static boolean isBlockedParticipant(final DatabaseWrapper db, final String value,
final String column) {
Cursor cursor = null;
try {
cursor = db.query(DatabaseHelper.PARTICIPANTS_TABLE,
new String[] { ParticipantColumns.BLOCKED },
column + "=? AND " + ParticipantColumns.SUB_ID + "=?",
new String[] { value,
Integer.toString(ParticipantData.OTHER_THAN_SELF_SUB_ID) },
null, null, null);
try (Cursor cursor = db.query(DatabaseHelper.PARTICIPANTS_TABLE,
new String[]{ParticipantColumns.BLOCKED},
column + "=? AND " + ParticipantColumns.SUB_ID + "=?",
new String[]{value,
Integer.toString(ParticipantData.OTHER_THAN_SELF_SUB_ID)},
null, null, null)) {
Assert.inRange(cursor.getCount(), 0, 1);
if (cursor.moveToFirst()) {
return cursor.getInt(0) == 1;
}
} finally {
if (cursor != null) {
cursor.close();
}
}
return false; // if there's no row, it's not blocked :-)
}
@@ -524,12 +506,10 @@ public class BugleDatabaseOperations {
new String[]{ conversationId },
null, null, null);
if (cursor != null) {
try {
try (cursor) {
if (cursor.moveToFirst()) {
return cursor.getLong(0);
}
} finally {
cursor.close();
}
}
return 0;
@@ -675,21 +655,15 @@ public class BugleDatabaseOperations {
// Make sure the selfId passed in is valid and active.
final String selection = ParticipantColumns._ID + "=? AND " +
ParticipantColumns.SIM_SLOT_ID + "<>?";
Cursor cursor = null;
try {
cursor = dbWrapper.query(DatabaseHelper.PARTICIPANTS_TABLE,
new String[] { ParticipantColumns._ID }, selection,
new String[] { selfId, String.valueOf(ParticipantData.INVALID_SLOT_ID) },
null, null, null);
try (Cursor cursor = dbWrapper.query(DatabaseHelper.PARTICIPANTS_TABLE,
new String[]{ParticipantColumns._ID}, selection,
new String[]{selfId, String.valueOf(ParticipantData.INVALID_SLOT_ID)},
null, null, null)) {
if (cursor != null && cursor.getCount() > 0) {
values.put(ConversationColumns.CURRENT_SELF_ID, selfId);
return true;
}
} finally {
if (cursor != null) {
cursor.close();
}
}
return false;
}
@@ -700,22 +674,17 @@ public class BugleDatabaseOperations {
Assert.isTrue(dbWrapper.getDatabase().inTransaction());
long sortTimestamp = 0L;
Cursor cursor = null;
try {
try (Cursor cursor = dbWrapper.query(DatabaseHelper.MESSAGES_TABLE,
REFRESH_CONVERSATION_MESSAGE_PROJECTION,
MessageColumns.CONVERSATION_ID + "=?",
new String[]{conversationId}, null, null,
MessageColumns.RECEIVED_TIMESTAMP + " DESC", "1" /* limit */)) {
// Check to find the latest message in the conversation
cursor = dbWrapper.query(DatabaseHelper.MESSAGES_TABLE,
REFRESH_CONVERSATION_MESSAGE_PROJECTION,
MessageColumns.CONVERSATION_ID + "=?",
new String[]{conversationId}, null, null,
MessageColumns.RECEIVED_TIMESTAMP + " DESC", "1" /* limit */);
/* limit */
if (cursor.moveToFirst()) {
sortTimestamp = cursor.getLong(1);
}
} finally {
if (cursor != null) {
cursor.close();
}
}
@@ -854,21 +823,15 @@ public class BugleDatabaseOperations {
public static String getConversationSelfId(final DatabaseWrapper dbWrapper,
final String conversationId) {
Assert.isNotMainThread();
Cursor cursor = null;
try {
cursor = dbWrapper.query(DatabaseHelper.CONVERSATIONS_TABLE,
new String[] { ConversationColumns.CURRENT_SELF_ID },
ConversationColumns._ID + "=?",
new String[] { conversationId },
null, null, null);
try (Cursor cursor = dbWrapper.query(DatabaseHelper.CONVERSATIONS_TABLE,
new String[]{ConversationColumns.CURRENT_SELF_ID},
ConversationColumns._ID + "=?",
new String[]{conversationId},
null, null, null)) {
Assert.inRange(cursor.getCount(), 0, 1);
if (cursor.moveToFirst()) {
return cursor.getString(0);
}
} finally {
if (cursor != null) {
cursor.close();
}
}
return null;
}
@@ -903,21 +866,15 @@ public class BugleDatabaseOperations {
public static String getSmsServiceCenterForConversation(final DatabaseWrapper dbWrapper,
final String conversationId) {
Assert.isNotMainThread();
Cursor cursor = null;
try {
cursor = dbWrapper.query(DatabaseHelper.CONVERSATIONS_TABLE,
new String[] { ConversationColumns.SMS_SERVICE_CENTER },
ConversationColumns._ID + "=?",
new String[] { conversationId },
null, null, null);
try (Cursor cursor = dbWrapper.query(DatabaseHelper.CONVERSATIONS_TABLE,
new String[]{ConversationColumns.SMS_SERVICE_CENTER},
ConversationColumns._ID + "=?",
new String[]{conversationId},
null, null, null)) {
Assert.inRange(cursor.getCount(), 0, 1);
if (cursor.moveToFirst()) {
return cursor.getString(0);
}
} finally {
if (cursor != null) {
cursor.close();
}
}
return null;
}
@@ -927,20 +884,14 @@ public class BugleDatabaseOperations {
final String participantId) {
Assert.isNotMainThread();
ParticipantData participant = null;
Cursor cursor = null;
try {
cursor = dbWrapper.query(DatabaseHelper.PARTICIPANTS_TABLE,
ParticipantData.ParticipantsQuery.PROJECTION,
ParticipantColumns._ID + " =?",
new String[] { participantId }, null, null, null);
try (Cursor cursor = dbWrapper.query(DatabaseHelper.PARTICIPANTS_TABLE,
ParticipantData.ParticipantsQuery.PROJECTION,
ParticipantColumns._ID + " =?",
new String[]{participantId}, null, null, null)) {
Assert.inRange(cursor.getCount(), 0, 1);
if (cursor.moveToFirst()) {
participant = ParticipantData.getFromCursor(cursor);
}
} finally {
if (cursor != null) {
cursor.close();
}
}
return participant;
@@ -964,24 +915,18 @@ public class BugleDatabaseOperations {
Assert.isNotMainThread();
final ArrayList<ParticipantData> participants =
new ArrayList<ParticipantData>();
Cursor cursor = null;
try {
cursor = dbWrapper.query(DatabaseHelper.PARTICIPANTS_TABLE,
ParticipantData.ParticipantsQuery.PROJECTION,
ParticipantColumns._ID + " IN ( " + "SELECT "
+ ConversationParticipantsColumns.PARTICIPANT_ID + " AS "
+ ParticipantColumns._ID
+ " FROM " + DatabaseHelper.CONVERSATION_PARTICIPANTS_TABLE
+ " WHERE " + ConversationParticipantsColumns.CONVERSATION_ID + " =? )",
new String[] { conversationId }, null, null, null);
try (Cursor cursor = dbWrapper.query(DatabaseHelper.PARTICIPANTS_TABLE,
ParticipantData.ParticipantsQuery.PROJECTION,
ParticipantColumns._ID + " IN ( " + "SELECT "
+ ConversationParticipantsColumns.PARTICIPANT_ID + " AS "
+ ParticipantColumns._ID
+ " FROM " + DatabaseHelper.CONVERSATION_PARTICIPANTS_TABLE
+ " WHERE " + ConversationParticipantsColumns.CONVERSATION_ID + " =? )",
new String[]{conversationId}, null, null, null)) {
while (cursor.moveToNext()) {
participants.add(ParticipantData.getFromCursor(cursor));
}
} finally {
if (cursor != null) {
cursor.close();
}
}
return participants;
@@ -1001,19 +946,13 @@ public class BugleDatabaseOperations {
static MessagePartData readMessagePartData(final DatabaseWrapper dbWrapper,
final String partId) {
MessagePartData messagePartData = null;
Cursor cursor = null;
try {
cursor = dbWrapper.query(DatabaseHelper.PARTS_TABLE,
MessagePartData.getProjection(), PartColumns._ID + "=?",
new String[] { partId }, null, null, null);
try (Cursor cursor = dbWrapper.query(DatabaseHelper.PARTS_TABLE,
MessagePartData.getProjection(), PartColumns._ID + "=?",
new String[]{partId}, null, null, null)) {
Assert.inRange(cursor.getCount(), 0, 1);
if (cursor.moveToFirst()) {
messagePartData = MessagePartData.createFromCursor(cursor);
}
} finally {
if (cursor != null) {
cursor.close();
}
}
return messagePartData;
}
@@ -1023,20 +962,14 @@ public class BugleDatabaseOperations {
final Uri smsMessageUri) {
Assert.isNotMainThread();
MessageData message = null;
Cursor cursor = null;
try {
cursor = dbWrapper.query(DatabaseHelper.MESSAGES_TABLE,
MessageData.getProjection(), MessageColumns.SMS_MESSAGE_URI + "=?",
new String[] { smsMessageUri.toString() }, null, null, null);
try (Cursor cursor = dbWrapper.query(DatabaseHelper.MESSAGES_TABLE,
MessageData.getProjection(), MessageColumns.SMS_MESSAGE_URI + "=?",
new String[]{smsMessageUri.toString()}, null, null, null)) {
Assert.inRange(cursor.getCount(), 0, 1);
if (cursor.moveToFirst()) {
message = new MessageData();
message.bind(cursor);
}
} finally {
if (cursor != null) {
cursor.close();
}
}
return message;
}
@@ -1046,20 +979,14 @@ public class BugleDatabaseOperations {
final String messageId) {
Assert.isNotMainThread();
MessageData message = null;
Cursor cursor = null;
try {
cursor = dbWrapper.query(DatabaseHelper.MESSAGES_TABLE,
MessageData.getProjection(), MessageColumns._ID + "=?",
new String[] { messageId }, null, null, null);
try (Cursor cursor = dbWrapper.query(DatabaseHelper.MESSAGES_TABLE,
MessageData.getProjection(), MessageColumns._ID + "=?",
new String[]{messageId}, null, null, null)) {
Assert.inRange(cursor.getCount(), 0, 1);
if (cursor.moveToFirst()) {
message = new MessageData();
message.bind(cursor);
}
} finally {
if (cursor != null) {
cursor.close();
}
}
return message;
}
@@ -1074,11 +1001,9 @@ public class BugleDatabaseOperations {
final MessageData message, final boolean checkAttachmentFilesExist) {
final ContentResolver contentResolver =
Factory.get().getApplicationContext().getContentResolver();
Cursor cursor = null;
try {
cursor = dbWrapper.query(DatabaseHelper.PARTS_TABLE,
MessagePartData.getProjection(), PartColumns.MESSAGE_ID + "=?",
new String[] { message.getMessageId() }, null, null, null);
try (Cursor cursor = dbWrapper.query(DatabaseHelper.PARTS_TABLE,
MessagePartData.getProjection(), PartColumns.MESSAGE_ID + "=?",
new String[]{message.getMessageId()}, null, null, null)) {
while (cursor.moveToNext()) {
final MessagePartData messagePartData = MessagePartData.createFromCursor(cursor);
if (checkAttachmentFilesExist && messagePartData.isAttachment() &&
@@ -1104,10 +1029,6 @@ public class BugleDatabaseOperations {
message.addPart(messagePartData);
}
}
} finally {
if (cursor != null) {
cursor.close();
}
}
}
@@ -1262,31 +1183,26 @@ public class BugleDatabaseOperations {
final String conversationId) {
Assert.isNotMainThread();
Assert.isTrue(dbWrapper.getDatabase().inTransaction());
Cursor cursor = null;
try {
try (Cursor cursor = dbWrapper.query(DatabaseHelper.MESSAGES_TABLE,
REFRESH_CONVERSATION_MESSAGE_PROJECTION,
MessageColumns.CONVERSATION_ID + "=? AND " +
MessageColumns.STATUS + "!=" + MessageData.BUGLE_STATUS_OUTGOING_DRAFT,
new String[]{conversationId}, null, null,
MessageColumns.RECEIVED_TIMESTAMP + " DESC", "1" /* limit */)) {
// TODO: The refreshConversationMetadataInTransaction method below uses this
// same query; maybe they should share this logic?
// Check to see if there are any (non-draft) messages in the conversation
cursor = dbWrapper.query(DatabaseHelper.MESSAGES_TABLE,
REFRESH_CONVERSATION_MESSAGE_PROJECTION,
MessageColumns.CONVERSATION_ID + "=? AND " +
MessageColumns.STATUS + "!=" + MessageData.BUGLE_STATUS_OUTGOING_DRAFT,
new String[] { conversationId }, null, null,
MessageColumns.RECEIVED_TIMESTAMP + " DESC", "1" /* limit */);
/* limit */
if (cursor.getCount() == 0) {
dbWrapper.delete(DatabaseHelper.CONVERSATIONS_TABLE,
ConversationColumns._ID + "=?", new String[] { conversationId });
ConversationColumns._ID + "=?", new String[]{conversationId});
LogUtil.i(TAG,
"BugleDatabaseOperations: Deleted empty conversation " + conversationId);
return true;
} else {
return false;
}
} finally {
if (cursor != null) {
cursor.close();
}
}
}
@@ -1306,15 +1222,14 @@ public class BugleDatabaseOperations {
boolean keepArchived) {
Assert.isNotMainThread();
Assert.isTrue(dbWrapper.getDatabase().inTransaction());
Cursor cursor = null;
try {
try (Cursor cursor = dbWrapper.query(DatabaseHelper.MESSAGES_TABLE,
REFRESH_CONVERSATION_MESSAGE_PROJECTION,
MessageColumns.CONVERSATION_ID + "=? AND " +
MessageColumns.STATUS + "!=" + MessageData.BUGLE_STATUS_OUTGOING_DRAFT,
new String[]{conversationId}, null, null,
MessageColumns.RECEIVED_TIMESTAMP + " DESC", "1" /* limit */)) {
// Check to see if there are any (non-draft) messages in the conversation
cursor = dbWrapper.query(DatabaseHelper.MESSAGES_TABLE,
REFRESH_CONVERSATION_MESSAGE_PROJECTION,
MessageColumns.CONVERSATION_ID + "=? AND " +
MessageColumns.STATUS + "!=" + MessageData.BUGLE_STATUS_OUTGOING_DRAFT,
new String[] { conversationId }, null, null,
MessageColumns.RECEIVED_TIMESTAMP + " DESC", "1" /* limit */);
/* limit */
if (cursor.moveToFirst()) {
// Refresh latest message in conversation
@@ -1326,10 +1241,6 @@ public class BugleDatabaseOperations {
latestMessageId, latestMessageTimestamp, senderBlocked || keepArchived,
shouldAutoSwitchSelfId);
}
} finally {
if (cursor != null) {
cursor.close();
}
}
}
@@ -1351,21 +1262,15 @@ public class BugleDatabaseOperations {
if (!TextUtils.isEmpty(messageId)) {
refresh = false;
// Look for an existing conversation in the db with this conversation id
Cursor cursor = null;
try {
cursor = dbWrapper.query(DatabaseHelper.CONVERSATIONS_TABLE,
new String[] { ConversationColumns.LATEST_MESSAGE_ID },
ConversationColumns._ID + "=?",
new String[] { conversationId },
null, null, null);
try (Cursor cursor = dbWrapper.query(DatabaseHelper.CONVERSATIONS_TABLE,
new String[]{ConversationColumns.LATEST_MESSAGE_ID},
ConversationColumns._ID + "=?",
new String[]{conversationId},
null, null, null)) {
Assert.inRange(cursor.getCount(), 0, 1);
if (cursor.moveToFirst()) {
refresh = TextUtils.equals(cursor.getString(0), messageId);
}
} finally {
if (cursor != null) {
cursor.close();
}
}
}
if (refresh) {
@@ -1457,18 +1362,13 @@ public class BugleDatabaseOperations {
static boolean getConversationExists(final DatabaseWrapper dbWrapper,
final String conversationId) {
// Look for an existing conversation in the db with this conversation id
Cursor cursor = null;
try {
cursor = dbWrapper.query(DatabaseHelper.CONVERSATIONS_TABLE,
new String[] { /* No projection */},
ConversationColumns._ID + "=?",
new String[] { conversationId },
null, null, null);
try (Cursor cursor = dbWrapper.query(DatabaseHelper.CONVERSATIONS_TABLE,
new String[]{ /* No projection */},
ConversationColumns._ID + "=?",
new String[]{conversationId},
null, null, null)) {
/* No projection */
return cursor.getCount() == 1;
} finally {
if (cursor != null) {
cursor.close();
}
}
}
@@ -1578,16 +1478,14 @@ public class BugleDatabaseOperations {
final String conversationId, final String conversationSelfId) {
Assert.isNotMainThread();
MessageData message = null;
Cursor cursor = null;
dbWrapper.beginTransaction();
try {
cursor = dbWrapper.query(DatabaseHelper.MESSAGES_TABLE,
MessageData.getProjection(),
MessageColumns.STATUS + "=? AND " + MessageColumns.CONVERSATION_ID + "=?",
new String[] {
try (Cursor cursor = dbWrapper.query(DatabaseHelper.MESSAGES_TABLE,
MessageData.getProjection(),
MessageColumns.STATUS + "=? AND " + MessageColumns.CONVERSATION_ID + "=?",
new String[]{
Integer.toString(MessageData.BUGLE_STATUS_OUTGOING_DRAFT),
conversationId
}, null, null, null);
}, null, null, null)) {
Assert.inRange(cursor.getCount(), 0, 1);
if (cursor.moveToFirst()) {
message = new MessageData();
@@ -1603,9 +1501,6 @@ public class BugleDatabaseOperations {
dbWrapper.setTransactionSuccessful();
} finally {
dbWrapper.endTransaction();
if (cursor != null) {
cursor.close();
}
}
return message;
}
@@ -1770,20 +1665,14 @@ public class BugleDatabaseOperations {
public static String getConversationFromOtherParticipantDestination(
final DatabaseWrapper db, final String otherDestination) {
Assert.isNotMainThread();
Cursor cursor = null;
try {
cursor = db.query(DatabaseHelper.CONVERSATIONS_TABLE,
new String[] { ConversationColumns._ID },
ConversationColumns.OTHER_PARTICIPANT_NORMALIZED_DESTINATION + "=?",
new String[] { otherDestination }, null, null, null);
try (Cursor cursor = db.query(DatabaseHelper.CONVERSATIONS_TABLE,
new String[]{ConversationColumns._ID},
ConversationColumns.OTHER_PARTICIPANT_NORMALIZED_DESTINATION + "=?",
new String[]{otherDestination}, null, null, null)) {
Assert.inRange(cursor.getCount(), 0, 1);
if (cursor.moveToFirst()) {
return cursor.getString(0);
}
} finally {
if (cursor != null) {
cursor.close();
}
}
return null;
}
@@ -1146,15 +1146,13 @@ public abstract class MessageNotificationState extends NotificationState {
public static void checkFailedMessages() {
final DatabaseWrapper db = DataModel.get().getDatabase();
final Cursor messageDataCursor = db.query(DatabaseHelper.MESSAGES_TABLE,
MessageData.getProjection(),
FailedMessageQuery.FAILED_MESSAGES_WHERE_CLAUSE,
null /*selectionArgs*/,
null /*groupBy*/,
null /*having*/,
FailedMessageQuery.FAILED_ORDER_BY);
try {
try (Cursor messageDataCursor = db.query(DatabaseHelper.MESSAGES_TABLE,
MessageData.getProjection(),
FailedMessageQuery.FAILED_MESSAGES_WHERE_CLAUSE,
null /*selectionArgs*/,
null /*groupBy*/,
null /*having*/,
FailedMessageQuery.FAILED_ORDER_BY)) {
final Context context = Factory.get().getApplicationContext();
final Resources resources = context.getResources();
final NotificationManagerCompat notificationManager =
@@ -1192,8 +1190,8 @@ public abstract class MessageNotificationState extends NotificationState {
LogUtil.d(TAG, "Found " + failedMessages.size() + " failed messages");
}
if (failedMessages.size() > 0) {
final NotificationCompat.Builder builder =
new NotificationCompat.Builder(context,
final Builder builder =
new Builder(context,
NotificationsUtil.DEFAULT_CHANNEL_ID);
CharSequence line1;
@@ -1204,7 +1202,7 @@ public abstract class MessageNotificationState extends NotificationState {
if (failedMessages.size() == 1) {
messageDataCursor.moveToPosition(cursorPosition);
messageData.bind(messageDataCursor);
final String conversationId = messageData.getConversationId();
final String conversationId = messageData.getConversationId();
// We have a single conversation, go directly to that conversation.
destinationIntent = UIIntents.get()
@@ -1235,7 +1233,7 @@ public abstract class MessageNotificationState extends NotificationState {
// We have notifications for multiple conversation, go to the conversation
// list.
destinationIntent = UIIntents.get()
.getPendingIntentForConversationListActivity(context);
.getPendingIntentForConversationListActivity(context);
int line1StringId;
int line2PluralsId;
@@ -1266,13 +1264,13 @@ public abstract class MessageNotificationState extends NotificationState {
0);
builder
.setContentTitle(line1)
.setTicker(line1)
.setWhen(when > 0 ? when : System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_failed_light)
.setDeleteIntent(pendingIntentForDelete)
.setContentIntent(destinationIntent)
.setSound(UriUtil.getUriForResourceId(context, R.raw.message_failure));
.setContentTitle(line1)
.setTicker(line1)
.setWhen(when > 0 ? when : System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_failed_light)
.setDeleteIntent(pendingIntentForDelete)
.setContentIntent(destinationIntent)
.setSound(UriUtil.getUriForResourceId(context, R.raw.message_failure));
if (isRichContent && !TextUtils.isEmpty(line2)) {
final NotificationCompat.InboxStyle inboxStyle =
new NotificationCompat.InboxStyle(builder);
@@ -1298,10 +1296,6 @@ public abstract class MessageNotificationState extends NotificationState {
PendingIntentConstants.MSG_SEND_ERROR);
}
}
} finally {
if (messageDataCursor != null) {
messageDataCursor.close();
}
}
}
}
@@ -308,11 +308,9 @@ public class ParticipantRefresh {
final DatabaseWrapper db = DataModel.get().getDatabase();
final HashSet<Integer> existingSubIds = new HashSet<Integer>();
Cursor cursor = null;
try {
cursor = db.query(DatabaseHelper.PARTICIPANTS_TABLE,
ParticipantsQuery.PROJECTION,
SELF_PARTICIPANTS_CLAUSE, null, null, null, null);
try (Cursor cursor = db.query(DatabaseHelper.PARTICIPANTS_TABLE,
ParticipantsQuery.PROJECTION,
SELF_PARTICIPANTS_CLAUSE, null, null, null, null)) {
if (cursor != null) {
while (cursor.moveToNext()) {
@@ -320,10 +318,6 @@ public class ParticipantRefresh {
existingSubIds.add(subId);
}
}
} finally {
if (cursor != null) {
cursor.close();
}
}
return existingSubIds;
}
@@ -445,9 +439,7 @@ public class ParticipantRefresh {
// For self participant, try getting name/avatar from self profile in CP2 first.
// TODO: in case of multi-sim, profile would not be able to be used for
// different numbers. Need to figure out that.
Cursor selfCursor = null;
try {
selfCursor = ContactUtil.getSelf(db.getContext()).performSynchronousQuery();
try (Cursor selfCursor = ContactUtil.getSelf(db.getContext()).performSynchronousQuery()) {
if (selfCursor != null && selfCursor.getCount() > 0) {
selfCursor.moveToNext();
final long selfContactId = selfCursor.getLong(ContactUtil.INDEX_CONTACT_ID);
@@ -467,10 +459,6 @@ public class ParticipantRefresh {
// However, we need to at least log the exception so we know something was wrong.
LogUtil.e(LogUtil.BUGLE_DATAMODEL_TAG, "Participant refresh: failed to refresh " +
"participant. exception=" + exception);
} finally {
if (selfCursor != null) {
selfCursor.close();
}
}
return changed;
}
@@ -619,12 +607,10 @@ public class ParticipantRefresh {
final String selection = ParticipantColumns.SIM_SLOT_ID + "=? AND " +
SELF_PARTICIPANTS_CLAUSE;
Cursor cursor = null;
try {
cursor = db.query(DatabaseHelper.PARTICIPANTS_TABLE,
new String[] { ParticipantColumns._ID },
selection, new String[] { String.valueOf(ParticipantData.INVALID_SLOT_ID) },
null, null, null);
try (Cursor cursor = db.query(DatabaseHelper.PARTICIPANTS_TABLE,
new String[]{ParticipantColumns._ID},
selection, new String[]{String.valueOf(ParticipantData.INVALID_SLOT_ID)},
null, null, null)) {
if (cursor != null) {
while (cursor.moveToNext()) {
@@ -632,10 +618,6 @@ public class ParticipantRefresh {
inactiveSelf.add(participantId);
}
}
} finally {
if (cursor != null) {
cursor.close();
}
}
return inactiveSelf;
@@ -148,13 +148,11 @@ public class DeleteConversationAction extends Action implements Parcelable {
Assert.notNull(conversationId);
final List<Uri> messageUris = new ArrayList<>();
Cursor cursor = null;
try {
cursor = db.query(DatabaseHelper.MESSAGES_TABLE,
new String[] { MessageColumns.SMS_MESSAGE_URI },
MessageColumns.CONVERSATION_ID + "=?",
new String[] { conversationId },
null, null, null);
try (Cursor cursor = db.query(DatabaseHelper.MESSAGES_TABLE,
new String[]{MessageColumns.SMS_MESSAGE_URI},
MessageColumns.CONVERSATION_ID + "=?",
new String[]{conversationId},
null, null, null)) {
while (cursor.moveToNext()) {
String messageUri = cursor.getString(0);
try {
@@ -164,10 +162,6 @@ public class DeleteConversationAction extends Action implements Parcelable {
+ messageUri);
}
}
} finally {
if (cursor != null) {
cursor.close();
}
}
for (Uri messageUri : messageUris) {
int count = MmsUtils.deleteMessage(messageUri);
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2015 The Android Open Source Project
* Copyright (C) 2024 The LineageOS Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -298,21 +299,15 @@ class SyncMessageBatch {
// with those details.
String foundConversationId = null;
Cursor cursor = null;
try {
try (Cursor cursor = db.rawQuery("SELECT " + ConversationColumns._ID
+ " FROM " + DatabaseHelper.CONVERSATIONS_TABLE
+ " WHERE " + ConversationColumns._ID + "=" + conversationId,
null)) {
// Look for an existing conversation in the db with the conversation id
cursor = db.rawQuery("SELECT " + ConversationColumns._ID
+ " FROM " + DatabaseHelper.CONVERSATIONS_TABLE
+ " WHERE " + ConversationColumns._ID + "=" + conversationId,
null);
if (cursor != null && cursor.moveToFirst()) {
Assert.isTrue(cursor.getCount() == 1);
foundConversationId = cursor.getString(0);
}
} finally {
if (cursor != null) {
cursor.close();
}
}
ParticipantData foundSelfParticipant =
@@ -466,23 +466,17 @@ public class ConversationListItemData {
ConversationListItemData conversation = null;
// Look for an existing conversation in the db with this conversation id
Cursor cursor = null;
try {
try (Cursor cursor = dbWrapper.query(getConversationListView(),
PROJECTION,
ConversationColumns._ID + "=?",
new String[]{conversationId},
null, null, null)) {
// TODO: Should we be able to read a row from just the conversation table?
cursor = dbWrapper.query(getConversationListView(),
PROJECTION,
ConversationColumns._ID + "=?",
new String[] { conversationId },
null, null, null);
Assert.inRange(cursor.getCount(), 0, 1);
if (cursor.moveToFirst()) {
conversation = new ConversationListItemData();
conversation.bind(cursor);
}
} finally {
if (cursor != null) {
cursor.close();
}
}
return conversation;
@@ -152,22 +152,16 @@ public class ParticipantData implements Parcelable {
public static ParticipantData getFromId(final DatabaseWrapper dbWrapper,
final String participantId) {
Cursor cursor = null;
try {
cursor = dbWrapper.query(DatabaseHelper.PARTICIPANTS_TABLE,
ParticipantsQuery.PROJECTION,
ParticipantColumns._ID + " =?",
new String[] { participantId }, null, null, null);
try (Cursor cursor = dbWrapper.query(DatabaseHelper.PARTICIPANTS_TABLE,
ParticipantsQuery.PROJECTION,
ParticipantColumns._ID + " =?",
new String[]{participantId}, null, null, null)) {
if (cursor.moveToFirst()) {
return ParticipantData.getFromCursor(cursor);
} else {
return null;
}
} finally {
if (cursor != null) {
cursor.close();
}
}
}
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2015 The Android Open Source Project
* Copyright (C) 2024 The LineageOS Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -155,7 +156,7 @@ public abstract class ImageRequest<D extends ImageRequestDescriptor>
if (unknownSize) {
final InputStream inputStream = getInputStreamForResource();
if (inputStream != null) {
try {
try (inputStream) {
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(inputStream, null, options);
// This is called when dimensions of image were unknown to allow db update
@@ -164,8 +165,6 @@ public abstract class ImageRequest<D extends ImageRequestDescriptor>
} else {
mDescriptor.updateSourceDimensions(options.outWidth, options.outHeight);
}
} finally {
inputStream.close();
}
} else {
throw new FileNotFoundException();
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2015 The Android Open Source Project
* Copyright (C) 2024 The LineageOS Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -265,19 +266,14 @@ public class VCardRequest implements MediaRequest<VCardResource> {
for (final VCardEntry.PhotoData photo : photos) {
final byte[] photoBytes = photo.getBytes();
if (photoBytes != null) {
final InputStream inputStream = new ByteArrayInputStream(photoBytes);
try {
try (InputStream inputStream = new ByteArrayInputStream(photoBytes)) {
avatarUri = UriUtil.persistContentToScratchSpace(inputStream);
if (avatarUri != null) {
// Just load the first avatar and be done. Want more? wait for V2.
break;
}
} finally {
try {
inputStream.close();
} catch (final IOException e) {
// Do nothing.
}
} catch (IOException e) {
// Do nothing.
}
}
}