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.ContentResolver;
20 import android.content.Context;
21 import android.graphics.Bitmap;
22 import android.provider.MediaStore.Video.Thumbnails;
23 
24 import com.android.messaging.Factory;
25 import com.android.messaging.util.MediaMetadataRetrieverWrapper;
26 import com.android.messaging.util.MediaUtil;
27 import com.android.messaging.util.OsUtil;
28 
29 import java.io.FileNotFoundException;
30 import java.io.IOException;
31 import java.io.InputStream;
32 
33 /**
34  * Class to request a video thumbnail.
35  * Users of this class as responsible for checking {@link #shouldShowIncomingVideoThumbnails}
36  */
37 public class VideoThumbnailRequest extends ImageRequest<UriImageRequestDescriptor> {
38 
VideoThumbnailRequest(final Context context, final UriImageRequestDescriptor descriptor)39     public VideoThumbnailRequest(final Context context,
40             final UriImageRequestDescriptor descriptor) {
41         super(context, descriptor);
42     }
43 
shouldShowIncomingVideoThumbnails()44     public static boolean shouldShowIncomingVideoThumbnails() {
45         return MediaUtil.canAutoAccessIncomingMedia();
46     }
47 
48     @Override
getInputStreamForResource()49     protected InputStream getInputStreamForResource() throws FileNotFoundException {
50         return null;
51     }
52 
53     @Override
hasBitmapObject()54     protected boolean hasBitmapObject() {
55         return true;
56     }
57 
58     @Override
getBitmapForResource()59     protected Bitmap getBitmapForResource() throws IOException {
60         final Long mediaId = mDescriptor.getMediaStoreId();
61         Bitmap bitmap = null;
62         if (mediaId != null) {
63             final ContentResolver cr = Factory.get().getApplicationContext().getContentResolver();
64             bitmap = Thumbnails.getThumbnail(cr, mediaId, Thumbnails.MICRO_KIND, null);
65         } else {
66             final MediaMetadataRetrieverWrapper retriever = new MediaMetadataRetrieverWrapper();
67             try {
68                 retriever.setDataSource(mDescriptor.uri);
69                 bitmap = retriever.getFrameAtTime();
70             } finally {
71                 retriever.release();
72             }
73         }
74         if (bitmap != null) {
75             mDescriptor.updateSourceDimensions(bitmap.getWidth(), bitmap.getHeight());
76         }
77         return bitmap;
78     }
79 }
80