mCallbacksWaiting;
private Uri mMediaUri;
/**
* Instantiated the meta data loader for the image resource with the given
* URI.
*/
public PanoramaMetadataLoader(Uri uri) {
mMediaUri = uri;
}
/**
* Asynchronously extract and return panorama metadata from the item with
* the given URI.
*
* NOTE: This call is backed by a cache to speed up successive calls, which
* will return immediately. Use {@link #clearCachedValues()} is called.
*/
public synchronized void getPanoramaMetadata(final Context context,
PanoramaMetadataCallback callback) {
if (mPanoramaMetadata != null) {
// Return the cached data right away, no need to fetch it again.
callback.onPanoramaMetadataLoaded(mPanoramaMetadata);
} else {
if (mCallbacksWaiting == null) {
mCallbacksWaiting = new ArrayList();
// TODO: Don't create a new thread each time, use a pool or
// single instance.
(new Thread() {
@Override
public void run() {
onLoadingDone(PhotoSphereHelper.getPanoramaMetadata(context,
mMediaUri));
}
}).start();
}
mCallbacksWaiting.add(callback);
}
}
/**
* Clear cached value and stop all running loading threads.
*/
public synchronized void clearCachedValues() {
if (mPanoramaMetadata != null) {
mPanoramaMetadata = null;
}
// TODO: Cancel running loading thread if active.
}
private synchronized void onLoadingDone(PanoramaMetadata metadata) {
mPanoramaMetadata = metadata;
if (mPanoramaMetadata == null) {
// Error getting panorama data from file. Treat as not panorama.
mPanoramaMetadata = PhotoSphereHelper.NOT_PANORAMA;
}
for (PanoramaMetadataCallback cb : mCallbacksWaiting) {
cb.onPanoramaMetadataLoaded(mPanoramaMetadata);
}
mCallbacksWaiting = null;
}
}