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 
17 package com.android.messaging.datamodel.media;
18 
19 import android.content.Context;
20 import android.graphics.Bitmap;
21 
22 import com.android.messaging.util.MediaMetadataRetrieverWrapper;
23 import com.android.messaging.util.MediaUtil;
24 
25 import java.io.FileNotFoundException;
26 import java.io.IOException;
27 import java.io.InputStream;
28 
29 /**
30  * Class to request a video thumbnail.
31  * Users of this class as responsible for checking {@link #shouldShowIncomingVideoThumbnails}
32  */
33 public class VideoThumbnailRequest extends ImageRequest<UriImageRequestDescriptor> {
34 
VideoThumbnailRequest(final Context context, final UriImageRequestDescriptor descriptor)35     public VideoThumbnailRequest(final Context context,
36             final UriImageRequestDescriptor descriptor) {
37         super(context, descriptor);
38     }
39 
shouldShowIncomingVideoThumbnails()40     public static boolean shouldShowIncomingVideoThumbnails() {
41         return MediaUtil.canAutoAccessIncomingMedia();
42     }
43 
44     @Override
getInputStreamForResource()45     protected InputStream getInputStreamForResource() throws FileNotFoundException {
46         return null;
47     }
48 
49     @Override
hasBitmapObject()50     protected boolean hasBitmapObject() {
51         return true;
52     }
53 
54     @Override
getBitmapForResource()55     protected Bitmap getBitmapForResource() throws IOException {
56         Bitmap bitmap = null;
57         // Get a thumbnail through MediaMetadataRetriever to get a representative frame at any time
58         // position instead.
59         final MediaMetadataRetrieverWrapper retriever = new MediaMetadataRetrieverWrapper();
60         try {
61             retriever.setDataSource(mDescriptor.uri);
62             bitmap = retriever.getFrameAtTime();
63         } finally {
64             retriever.release();
65         }
66         if (bitmap != null) {
67             mDescriptor.updateSourceDimensions(bitmap.getWidth(), bitmap.getHeight());
68         }
69         return bitmap;
70     }
71 }
72