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
18package com.android.mms.ui;
19
20import android.content.Context;
21
22import com.android.mms.model.AudioModel;
23import com.android.mms.model.ImageModel;
24import com.android.mms.model.Model;
25import com.android.mms.model.SlideModel;
26import com.android.mms.model.SlideshowModel;
27import com.android.mms.model.VideoModel;
28import com.android.mms.util.ItemLoadedCallback;
29import com.android.mms.util.ItemLoadedFuture;
30import com.android.mms.util.ThumbnailManager.ImageLoaded;
31
32public class MmsThumbnailPresenter extends Presenter {
33    private static final String TAG = "MmsThumbnailPresenter";
34    private ItemLoadedCallback mOnLoadedCallback;
35    private ItemLoadedFuture mItemLoadedFuture;
36
37    public MmsThumbnailPresenter(Context context, ViewInterface view, Model model) {
38        super(context, view, model);
39    }
40
41    @Override
42    public void present(ItemLoadedCallback callback) {
43        mOnLoadedCallback = callback;
44        SlideModel slide = ((SlideshowModel) mModel).get(0);
45        if (slide != null) {
46            presentFirstSlide((SlideViewInterface) mView, slide);
47        }
48    }
49
50    private void presentFirstSlide(SlideViewInterface view, SlideModel slide) {
51        view.reset();
52
53        if (slide.hasImage()) {
54            presentImageThumbnail(view, slide.getImage());
55        } else if (slide.hasVideo()) {
56            presentVideoThumbnail(view, slide.getVideo());
57        } else if (slide.hasAudio()) {
58            presentAudioThumbnail(view, slide.getAudio());
59        }
60    }
61
62    private ItemLoadedCallback<ImageLoaded> mImageLoadedCallback =
63            new ItemLoadedCallback<ImageLoaded>() {
64        public void onItemLoaded(ImageLoaded imageLoaded, Throwable exception) {
65            if (exception == null) {
66                if (mItemLoadedFuture != null) {
67                    synchronized(mItemLoadedFuture) {
68                        mItemLoadedFuture.setIsDone(true);
69                    }
70                }
71                if (mOnLoadedCallback != null) {
72                    mOnLoadedCallback.onItemLoaded(imageLoaded, exception);
73                } else {
74                    // Right now we're only handling image and video loaded callbacks.
75                    SlideModel slide = ((SlideshowModel) mModel).get(0);
76                    if (slide != null) {
77                        if (slide.hasVideo() && imageLoaded.mIsVideo) {
78                            ((SlideViewInterface)mView).setVideoThumbnail(null,
79                                    imageLoaded.mBitmap);
80                        } else if (slide.hasImage() && !imageLoaded.mIsVideo) {
81                            ((SlideViewInterface)mView).setImage(null, imageLoaded.mBitmap);
82                        }
83                    }
84                }
85            }
86        }
87    };
88
89    private void presentVideoThumbnail(SlideViewInterface view, VideoModel video) {
90        mItemLoadedFuture = video.loadThumbnailBitmap(mImageLoadedCallback);
91    }
92
93    private void presentImageThumbnail(SlideViewInterface view, ImageModel image) {
94        mItemLoadedFuture = image.loadThumbnailBitmap(mImageLoadedCallback);
95    }
96
97    protected void presentAudioThumbnail(SlideViewInterface view, AudioModel audio) {
98        view.setAudio(audio.getUri(), audio.getSrc(), audio.getExtras());
99    }
100
101    public void onModelChanged(Model model, boolean dataChanged) {
102        // TODO Auto-generated method stub
103    }
104
105    public void cancelBackgroundLoading() {
106        // Currently we only support background loading of thumbnails. If we extend background
107        // loading to other media types, we should add a cancelLoading API to Model.
108        SlideModel slide = ((SlideshowModel) mModel).get(0);
109        if (slide != null && slide.hasImage()) {
110            slide.getImage().cancelThumbnailLoading();
111        }
112    }
113
114}
115