Fix android.os.strictmode.LeakedClosableViolation

Test: Check log.

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