1/*
2 * Copyright (C) 2012 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 */
16package com.android.gallery3d.app;
17
18import com.android.gallery3d.data.MediaObject;
19import com.android.gallery3d.data.MediaObject.PanoramaSupportCallback;
20import com.android.gallery3d.data.PanoramaMetadataJob;
21import com.android.gallery3d.util.Future;
22import com.android.gallery3d.util.FutureListener;
23import com.android.gallery3d.util.LightCycleHelper;
24import com.android.gallery3d.util.LightCycleHelper.PanoramaMetadata;
25
26import java.util.ArrayList;
27
28/**
29 * This class breaks out the off-thread panorama support checks so that the
30 * complexity can be shared between UriImage and LocalImage, which need to
31 * support panoramas.
32 */
33public class PanoramaMetadataSupport implements FutureListener<PanoramaMetadata> {
34    private Object mLock = new Object();
35    private Future<PanoramaMetadata> mGetPanoMetadataTask;
36    private PanoramaMetadata mPanoramaMetadata;
37    private ArrayList<PanoramaSupportCallback> mCallbacksWaiting;
38    private MediaObject mMediaObject;
39
40    public PanoramaMetadataSupport(MediaObject mediaObject) {
41        mMediaObject = mediaObject;
42    }
43
44    public void getPanoramaSupport(GalleryApp app, PanoramaSupportCallback callback) {
45        synchronized (mLock) {
46            if (mPanoramaMetadata != null) {
47                callback.panoramaInfoAvailable(mMediaObject, mPanoramaMetadata.mUsePanoramaViewer,
48                        mPanoramaMetadata.mIsPanorama360);
49            } else {
50                if (mCallbacksWaiting == null) {
51                    mCallbacksWaiting = new ArrayList<PanoramaSupportCallback>();
52                    mGetPanoMetadataTask = app.getThreadPool().submit(
53                            new PanoramaMetadataJob(app.getAndroidContext(),
54                                    mMediaObject.getContentUri()), this);
55
56                }
57                mCallbacksWaiting.add(callback);
58            }
59        }
60    }
61
62    public void clearCachedValues() {
63        synchronized (mLock) {
64            if (mPanoramaMetadata != null) {
65                mPanoramaMetadata = null;
66            } else if (mGetPanoMetadataTask != null) {
67                mGetPanoMetadataTask.cancel();
68                for (PanoramaSupportCallback cb : mCallbacksWaiting) {
69                    cb.panoramaInfoAvailable(mMediaObject, false, false);
70                }
71                mGetPanoMetadataTask = null;
72                mCallbacksWaiting = null;
73            }
74        }
75    }
76
77    @Override
78    public void onFutureDone(Future<PanoramaMetadata> future) {
79        synchronized (mLock) {
80            mPanoramaMetadata = future.get();
81            if (mPanoramaMetadata == null) {
82                // Error getting panorama data from file. Treat as not panorama.
83                mPanoramaMetadata = LightCycleHelper.NOT_PANORAMA;
84            }
85            for (PanoramaSupportCallback cb : mCallbacksWaiting) {
86                cb.panoramaInfoAvailable(mMediaObject, mPanoramaMetadata.mUsePanoramaViewer,
87                        mPanoramaMetadata.mIsPanorama360);
88            }
89            mGetPanoMetadataTask = null;
90            mCallbacksWaiting = null;
91        }
92    }
93}
94