1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.messaging.util;
17 
18 import android.content.ContentResolver;
19 import android.content.res.AssetFileDescriptor;
20 import android.graphics.Bitmap;
21 import android.media.MediaMetadataRetriever;
22 import android.net.Uri;
23 import android.text.TextUtils;
24 
25 import com.android.messaging.Factory;
26 
27 import java.io.IOException;
28 
29 /**
30  * Convenience wrapper for {@link MediaMetadataRetriever} to help with its eccentric error handling.
31  */
32 public class MediaMetadataRetrieverWrapper {
33     private final MediaMetadataRetriever mRetriever = new MediaMetadataRetriever();
34 
MediaMetadataRetrieverWrapper()35     public MediaMetadataRetrieverWrapper() {
36     }
37 
setDataSource(Uri uri)38     public void setDataSource(Uri uri) throws IOException {
39         ContentResolver resolver = Factory.get().getApplicationContext().getContentResolver();
40         AssetFileDescriptor fd = resolver.openAssetFileDescriptor(uri, "r");
41         if (fd == null) {
42             throw new IOException("openAssetFileDescriptor returned null for " + uri);
43         }
44         try {
45             mRetriever.setDataSource(fd.getFileDescriptor());
46         } catch (RuntimeException e) {
47             release();
48             throw new IOException(e);
49         } finally {
50             fd.close();
51         }
52     }
53 
extractInteger(final int key, final int defaultValue)54     public int extractInteger(final int key, final int defaultValue) {
55         final String s = mRetriever.extractMetadata(key);
56         if (TextUtils.isEmpty(s)) {
57             return defaultValue;
58         }
59         return Integer.parseInt(s);
60     }
61 
extractMetadata(final int key)62     public String extractMetadata(final int key) {
63         return mRetriever.extractMetadata(key);
64     }
65 
getFrameAtTime()66     public Bitmap getFrameAtTime() {
67         return mRetriever.getFrameAtTime();
68     }
69 
getFrameAtTime(final long timeUs)70     public Bitmap getFrameAtTime(final long timeUs) {
71         return mRetriever.getFrameAtTime(timeUs);
72     }
73 
release()74     public void release() {
75         try {
76             mRetriever.release();
77         } catch (RuntimeException e) {
78             LogUtil.e(LogUtil.BUGLE_TAG, "MediaMetadataRetriever.release failed", e);
79         }
80     }
81 }
82