Fix android.os.strictmode.LeakedClosableViolation

Test: Check log.

Change-Id: I488fc6c4288bc3843a2be35a8e2988738441263e
Signed-off-by: Taesu Lee <taesu82.lee@samsung.com>
This commit is contained in:
Taesu Lee
2020-02-12 09:54:01 +09:00
parent ce85205894
commit 050bc461fc
5 changed files with 57 additions and 27 deletions
@@ -625,10 +625,13 @@ public abstract class MessageNotificationState extends NotificationState {
final Context context = Factory.get().getApplicationContext(); final Context context = Factory.get().getApplicationContext();
final Uri uri = final Uri uri =
MessagingContentProvider.buildConversationParticipantsUri(conversationId); MessagingContentProvider.buildConversationParticipantsUri(conversationId);
final Cursor participantsCursor = context.getContentResolver().query(
uri, ParticipantData.ParticipantsQuery.PROJECTION, null, null, null);
final ConversationParticipantsData participantsData = new ConversationParticipantsData(); final ConversationParticipantsData participantsData = new ConversationParticipantsData();
try (final Cursor participantsCursor = context.getContentResolver().query(
uri, ParticipantData.ParticipantsQuery.PROJECTION, null, null, null)) {
participantsData.bind(participantsCursor); participantsData.bind(participantsCursor);
}
final Iterator<ParticipantData> iter = participantsData.iterator(); final Iterator<ParticipantData> iter = participantsData.iterator();
final HashMap<String, Integer> firstNames = new HashMap<String, Integer>(); final HashMap<String, Integer> firstNames = new HashMap<String, Integer>();
@@ -156,20 +156,27 @@ public class ContactRecipientAutoCompleteView extends RecipientEditTextView {
ContactRecipientEntryUtils.isSendToDestinationContact(entry)) { ContactRecipientEntryUtils.isSendToDestinationContact(entry)) {
// This is a generated/send-to contact chip, try to look it up and // This is a generated/send-to contact chip, try to look it up and
// display a chip for the corresponding local contact. // display a chip for the corresponding local contact.
final Cursor lookupResult = ContactUtil.lookupDestination(getContext(), try (final Cursor lookupResult =
entry.getDestination()).performSynchronousQuery(); ContactUtil.lookupDestination(
getContext(), entry.getDestination())
.performSynchronousQuery()) {
if (lookupResult != null && lookupResult.moveToNext()) { if (lookupResult != null && lookupResult.moveToNext()) {
// Found a match, remove the generated entry and replace with // Found a match, remove the generated entry and replace with a
// a better local entry. // better local entry.
publishProgress(new ChipReplacementTuple(recipient, publishProgress(
new ChipReplacementTuple(
recipient,
ContactUtil.createRecipientEntryForPhoneQuery( ContactUtil.createRecipientEntryForPhoneQuery(
lookupResult, true))); lookupResult, true)));
} else if (PhoneUtils.isValidSmsMmsDestination( } else if (PhoneUtils.isValidSmsMmsDestination(
entry.getDestination())){ entry.getDestination())) {
// No match was found, but we have a valid destination so let's at // No match was found, but we have a valid destination so let's
// least create an entry that shows an avatar. // at least create an entry that shows an avatar.
publishProgress(new ChipReplacementTuple(recipient, publishProgress(
ContactRecipientEntryUtils.constructNumberWithAvatarEntry( new ChipReplacementTuple(
recipient,
ContactRecipientEntryUtils
.constructNumberWithAvatarEntry(
entry.getDestination()))); entry.getDestination())));
} else { } else {
// Not a valid contact. Remove and show an error. // Not a valid contact. Remove and show an error.
@@ -177,6 +184,7 @@ public class ContactRecipientAutoCompleteView extends RecipientEditTextView {
invalidChipsRemoved++; invalidChipsRemoved++;
} }
} }
}
} else { } else {
publishProgress(new ChipReplacementTuple(recipient, null)); publishProgress(new ChipReplacementTuple(recipient, null));
invalidChipsRemoved++; invalidChipsRemoved++;
@@ -636,6 +636,7 @@ class CameraManager implements FocusOverlayManager.Listener {
} }
} }
mMediaRecorder.closeVideoFileDescriptor();
mMediaRecorder.release(); mMediaRecorder.release();
mMediaRecorder = null; mMediaRecorder = null;
@@ -20,6 +20,7 @@ import android.hardware.Camera;
import android.media.CamcorderProfile; import android.media.CamcorderProfile;
import android.media.MediaRecorder; import android.media.MediaRecorder;
import android.net.Uri; import android.net.Uri;
import android.os.ParcelFileDescriptor;
import com.android.messaging.Factory; import com.android.messaging.Factory;
import com.android.messaging.datamodel.MediaScratchFileProvider; import com.android.messaging.datamodel.MediaScratchFileProvider;
@@ -27,6 +28,7 @@ import com.android.messaging.util.ContentType;
import com.android.messaging.util.SafeAsyncTask; import com.android.messaging.util.SafeAsyncTask;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException;
class MmsVideoRecorder extends MediaRecorder { class MmsVideoRecorder extends MediaRecorder {
private static final float VIDEO_OVERSHOOT_SLOP = .85F; private static final float VIDEO_OVERSHOOT_SLOP = .85F;
@@ -39,6 +41,8 @@ class MmsVideoRecorder extends MediaRecorder {
/** The uri where video is being recorded to */ /** The uri where video is being recorded to */
private Uri mTempVideoUri; private Uri mTempVideoUri;
private ParcelFileDescriptor mVideoFD;
/** The settings used for video recording */ /** The settings used for video recording */
private final CamcorderProfile mCamcorderProfile; private final CamcorderProfile mCamcorderProfile;
@@ -75,9 +79,9 @@ class MmsVideoRecorder extends MediaRecorder {
setAudioSource(MediaRecorder.AudioSource.CAMCORDER); setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
setVideoSource(MediaRecorder.VideoSource.CAMERA); setVideoSource(MediaRecorder.VideoSource.CAMERA);
setOutputFormat(mCamcorderProfile.fileFormat); setOutputFormat(mCamcorderProfile.fileFormat);
setOutputFile( mVideoFD = Factory.get().getApplicationContext().getContentResolver()
Factory.get().getApplicationContext().getContentResolver().openFileDescriptor( .openFileDescriptor(mTempVideoUri, "w");
mTempVideoUri, "w").getFileDescriptor()); setOutputFile(mVideoFD.getFileDescriptor());
// Copy settings from CamcorderProfile to MediaRecorder // Copy settings from CamcorderProfile to MediaRecorder
setAudioEncodingBitRate(audioBitRate); setAudioEncodingBitRate(audioBitRate);
@@ -124,4 +128,15 @@ class MmsVideoRecorder extends MediaRecorder {
return ContentType.VIDEO_3GPP; return ContentType.VIDEO_3GPP;
} }
} }
public void closeVideoFileDescriptor() {
if (mVideoFD != null) {
try {
mVideoFD.close();
} catch (IOException e) {
// Ignore
}
mVideoFD = null;
}
}
} }
@@ -682,8 +682,11 @@ public class ImageUtils {
if (mScaled == null) { if (mScaled == null) {
if (mDecoded == null) { if (mDecoded == null) {
mOptions.inSampleSize = mSampleSize; mOptions.inSampleSize = mSampleSize;
final InputStream inputStream = cr.openInputStream(mUri); try (final InputStream inputStream = cr.openInputStream(mUri)) {
mDecoded = BitmapFactory.decodeStream(inputStream, null, mOptions); mDecoded = BitmapFactory.decodeStream(inputStream, null, mOptions);
} catch (IOException e) {
// Ignore
}
if (mDecoded == null) { if (mDecoded == null) {
if (logv) { if (logv) {
LogUtil.v(LogUtil.BUGLE_IMAGE_TAG, LogUtil.v(LogUtil.BUGLE_IMAGE_TAG,