1 /* 2 * Copyright (C) 2008 Esmertec AG. 3 * Copyright (C) 2008 The Android Open Source Project 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package com.android.mms.ui; 19 20 import android.content.Context; 21 22 import com.android.mms.LogTag; 23 import com.android.mms.model.AudioModel; 24 import com.android.mms.model.ImageModel; 25 import com.android.mms.model.Model; 26 import com.android.mms.model.SlideModel; 27 import com.android.mms.model.SlideshowModel; 28 import com.android.mms.model.VideoModel; 29 import com.android.mms.util.ItemLoadedCallback; 30 import com.android.mms.util.ItemLoadedFuture; 31 import com.android.mms.util.ThumbnailManager.ImageLoaded; 32 33 public class MmsThumbnailPresenter extends Presenter { 34 private static final String TAG = LogTag.TAG; 35 private ItemLoadedCallback mOnLoadedCallback; 36 private ItemLoadedFuture mItemLoadedFuture; 37 38 public MmsThumbnailPresenter(Context context, ViewInterface view, Model model) { 39 super(context, view, model); 40 } 41 42 @Override 43 public void present(ItemLoadedCallback callback) { 44 mOnLoadedCallback = callback; 45 SlideModel slide = ((SlideshowModel) mModel).get(0); 46 if (slide != null) { 47 presentFirstSlide((SlideViewInterface) mView, slide); 48 } 49 } 50 51 private void presentFirstSlide(SlideViewInterface view, SlideModel slide) { 52 view.reset(); 53 54 if (slide.hasImage()) { 55 presentImageThumbnail(view, slide.getImage()); 56 } else if (slide.hasVideo()) { 57 presentVideoThumbnail(view, slide.getVideo()); 58 } else if (slide.hasAudio()) { 59 presentAudioThumbnail(view, slide.getAudio()); 60 } 61 } 62 63 private ItemLoadedCallback<ImageLoaded> mImageLoadedCallback = 64 new ItemLoadedCallback<ImageLoaded>() { 65 public void onItemLoaded(ImageLoaded imageLoaded, Throwable exception) { 66 if (exception == null) { 67 if (mItemLoadedFuture != null) { 68 synchronized(mItemLoadedFuture) { 69 mItemLoadedFuture.setIsDone(true); 70 } 71 } 72 if (mOnLoadedCallback != null) { 73 mOnLoadedCallback.onItemLoaded(imageLoaded, exception); 74 } else { 75 // Right now we're only handling image and video loaded callbacks. 76 SlideModel slide = ((SlideshowModel) mModel).get(0); 77 if (slide != null) { 78 if (slide.hasVideo() && imageLoaded.mIsVideo) { 79 ((SlideViewInterface)mView).setVideoThumbnail(null, 80 imageLoaded.mBitmap); 81 } else if (slide.hasImage() && !imageLoaded.mIsVideo) { 82 ((SlideViewInterface)mView).setImage(null, imageLoaded.mBitmap); 83 } 84 } 85 } 86 } 87 } 88 }; 89 90 private void presentVideoThumbnail(SlideViewInterface view, VideoModel video) { 91 mItemLoadedFuture = video.loadThumbnailBitmap(mImageLoadedCallback); 92 } 93 94 private void presentImageThumbnail(SlideViewInterface view, ImageModel image) { 95 mItemLoadedFuture = image.loadThumbnailBitmap(mImageLoadedCallback); 96 } 97 98 protected void presentAudioThumbnail(SlideViewInterface view, AudioModel audio) { 99 view.setAudio(audio.getUri(), audio.getSrc(), audio.getExtras()); 100 } 101 102 public void onModelChanged(Model model, boolean dataChanged) { 103 // TODO Auto-generated method stub 104 } 105 106 public void cancelBackgroundLoading() { 107 // Currently we only support background loading of thumbnails. If we extend background 108 // loading to other media types, we should add a cancelLoading API to Model. 109 SlideModel slide = ((SlideshowModel) mModel).get(0); 110 if (slide != null && slide.hasImage()) { 111 slide.getImage().cancelThumbnailLoading(); 112 } 113 } 114 115 } 116