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
@@ -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.
@@ -445,17 +446,11 @@ public class ContactUtil {
return null;
}
String firstName = null;
Cursor nameCursor = null;
try {
nameCursor = ContactUtil.lookupStructuredName(context, contactId, true)
.performSynchronousQuery();
try (Cursor nameCursor = ContactUtil.lookupStructuredName(context, contactId, true)
.performSynchronousQuery()) {
if (nameCursor != null && nameCursor.moveToFirst()) {
firstName = nameCursor.getString(ContactUtil.INDEX_STRUCTURED_NAME_GIVEN_NAME);
}
} finally {
if (nameCursor != null) {
nameCursor.close();
}
}
return firstName;
}
@@ -56,15 +56,12 @@ public class DebugUtils {
final File inputFile = getDebugFile(dumpFileName, false);
if (inputFile != null) {
final FileInputStream fis = new FileInputStream(inputFile);
final BufferedInputStream bis = new BufferedInputStream(fis);
try {
try (BufferedInputStream bis = new BufferedInputStream(fis)) {
// dump file
data = ByteStreams.toByteArray(bis);
if (data == null || data.length < 1) {
LogUtil.e(LogUtil.BUGLE_TAG, "receiveFromDumpFile: empty data");
}
} finally {
bis.close();
}
}
} catch (final IOException e) {
+2 -14
View File
@@ -222,18 +222,12 @@ public class ImageUtils {
public static String getContentType(final ContentResolver cr, final Uri uri) {
// Figure out the content type of media.
String contentType = null;
Cursor cursor = null;
if (UriUtil.isMediaStoreUri(uri)) {
try {
cursor = cr.query(uri, MEDIA_CONTENT_PROJECTION, null, null, null);
try (Cursor cursor = cr.query(uri, MEDIA_CONTENT_PROJECTION, null, null, null)) {
if (cursor != null && cursor.moveToFirst()) {
contentType = cursor.getString(INDEX_CONTENT_TYPE);
}
} finally {
if (cursor != null) {
cursor.close();
}
}
}
if (contentType == null) {
@@ -315,7 +309,7 @@ public class ImageUtils {
*/
public static boolean isGif(InputStream inputStream) {
if (inputStream != null) {
try {
try (inputStream) {
byte[] gifHeaderBytes = new byte[6];
int value = inputStream.read(gifHeaderBytes, 0, 6);
if (value == 6) {
@@ -324,12 +318,6 @@ public class ImageUtils {
}
} catch (IOException e) {
return false;
} finally {
try {
inputStream.close();
} catch (IOException e) {
// Ignore
}
}
}
return false;
@@ -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.
@@ -37,17 +38,14 @@ public class MediaMetadataRetrieverWrapper {
public void setDataSource(Uri uri) throws IOException {
ContentResolver resolver = Factory.get().getApplicationContext().getContentResolver();
AssetFileDescriptor fd = resolver.openAssetFileDescriptor(uri, "r");
if (fd == null) {
throw new IOException("openAssetFileDescriptor returned null for " + uri);
}
try {
try (AssetFileDescriptor fd = resolver.openAssetFileDescriptor(uri, "r")) {
if (fd == null) {
throw new IOException("openAssetFileDescriptor returned null for " + uri);
}
mRetriever.setDataSource(fd.getFileDescriptor());
} catch (RuntimeException e) {
release();
throw new IOException(e);
} finally {
fd.close();
}
}
+5 -12
View File
@@ -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,21 +156,13 @@ public class UriUtil {
public static long getContentSize(final Uri uri) {
Assert.isNotMainThread();
if (isLocalResourceUri(uri)) {
ParcelFileDescriptor pfd = null;
try {
pfd = Factory.get().getApplicationContext()
.getContentResolver().openFileDescriptor(uri, "r");
try (ParcelFileDescriptor pfd = Factory.get().getApplicationContext()
.getContentResolver().openFileDescriptor(uri, "r")) {
return Math.max(pfd.getStatSize(), 0);
} catch (final FileNotFoundException e) {
LogUtil.e(LogUtil.BUGLE_TAG, "Error getting content size", e);
} finally {
if (pfd != null) {
try {
pfd.close();
} catch (final IOException e) {
// Do nothing.
}
}
} catch (final IOException e) {
// Do nothing.
}
} else {
Assert.fail("Unsupported uri type!");