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  */
16 package com.android.gallery3d.app;
17 
18 import com.android.gallery3d.data.MediaObject;
19 import com.android.gallery3d.data.MediaObject.PanoramaSupportCallback;
20 import com.android.gallery3d.data.PanoramaMetadataJob;
21 import com.android.gallery3d.util.Future;
22 import com.android.gallery3d.util.FutureListener;
23 import com.android.gallery3d.util.LightCycleHelper;
24 import com.android.gallery3d.util.LightCycleHelper.PanoramaMetadata;
25 
26 import 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  */
33 public 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 
PanoramaMetadataSupport(MediaObject mediaObject)40     public PanoramaMetadataSupport(MediaObject mediaObject) {
41         mMediaObject = mediaObject;
42     }
43 
getPanoramaSupport(GalleryApp app, PanoramaSupportCallback callback)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 
clearCachedValues()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
onFutureDone(Future<PanoramaMetadata> future)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