summaryrefslogtreecommitdiffstats
path: root/src/com/android/gallery3d/filtershow/cache/ImageLoader.java
blob: d284a03382f3c0e05b906abe86a3a19cd5d48cb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.gallery3d.filtershow.cache;

import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.database.sqlite.SQLiteException;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapRegionDecoder;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.RectF;
import android.media.ExifInterface;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;

import com.adobe.xmp.XMPException;
import com.adobe.xmp.XMPMeta;

import com.android.gallery3d.R;
import com.android.gallery3d.common.Utils;
import com.android.gallery3d.exif.ExifInvalidFormatException;
import com.android.gallery3d.exif.ExifParser;
import com.android.gallery3d.exif.ExifTag;
import com.android.gallery3d.filtershow.CropExtras;
import com.android.gallery3d.filtershow.FilterShowActivity;
import com.android.gallery3d.filtershow.HistoryAdapter;
import com.android.gallery3d.filtershow.imageshow.GeometryMetadata;
import com.android.gallery3d.filtershow.imageshow.ImageCrop;
import com.android.gallery3d.filtershow.imageshow.ImageShow;
import com.android.gallery3d.filtershow.imageshow.MasterImage;
import com.android.gallery3d.filtershow.presets.ImagePreset;
import com.android.gallery3d.filtershow.tools.BitmapTask;
import com.android.gallery3d.filtershow.tools.SaveCopyTask;
import com.android.gallery3d.util.InterruptableOutputStream;
import com.android.gallery3d.util.XmpUtilHelper;

import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Vector;
import java.util.concurrent.locks.ReentrantLock;


// TODO: this class has waaaay to much bitmap copying.  Cleanup.
public class ImageLoader {

    private static final String LOGTAG = "ImageLoader";
    private final Vector<ImageShow> mListeners = new Vector<ImageShow>();
    private Bitmap mOriginalBitmapSmall = null;
    private Bitmap mOriginalBitmapLarge = null;
    private Bitmap mBackgroundBitmap = null;

    private final ZoomCache mZoomCache = new ZoomCache();

    private int mOrientation = 0;
    private HistoryAdapter mAdapter = null;

    private FilterShowActivity mActivity = null;

    public static final String JPEG_MIME_TYPE = "image/jpeg";

    public static final String DEFAULT_SAVE_DIRECTORY = "EditedOnlinePhotos";
    public static final int DEFAULT_COMPRESS_QUALITY = 95;

    public static final int ORI_NORMAL = ExifInterface.ORIENTATION_NORMAL;
    public static final int ORI_ROTATE_90 = ExifInterface.ORIENTATION_ROTATE_90;
    public static final int ORI_ROTATE_180 = ExifInterface.ORIENTATION_ROTATE_180;
    public static final int ORI_ROTATE_270 = ExifInterface.ORIENTATION_ROTATE_270;
    public static final int ORI_FLIP_HOR = ExifInterface.ORIENTATION_FLIP_HORIZONTAL;
    public static final int ORI_FLIP_VERT = ExifInterface.ORIENTATION_FLIP_VERTICAL;
    public static final int ORI_TRANSPOSE = ExifInterface.ORIENTATION_TRANSPOSE;
    public static final int ORI_TRANSVERSE = ExifInterface.ORIENTATION_TRANSVERSE;

    private static final int BITMAP_LOAD_BACKOUT_ATTEMPTS = 5;
    private Context mContext = null;
    private Uri mUri = null;

    private Rect mOriginalBounds = null;
    private static int mZoomOrientation = ORI_NORMAL;

    private ReentrantLock mLoadingLock = new ReentrantLock();

    public ImageLoader(FilterShowActivity activity, Context context) {
        mActivity = activity;
        mContext = context;
    }

    public static int getZoomOrientation() {
        return mZoomOrientation;
    }

    public FilterShowActivity getActivity() {
        return mActivity;
    }

    public boolean loadBitmap(Uri uri, int size) {
        mLoadingLock.lock();
        mUri = uri;
        mOrientation = getOrientation(mContext, uri);
        mOriginalBitmapSmall = loadScaledBitmap(uri, 160);
        if (mOriginalBitmapSmall == null) {
            // Couldn't read the bitmap, let's exit
            mLoadingLock.unlock();
            return false;
        }
        mOriginalBitmapLarge = loadScaledBitmap(uri, size);
        if (mOriginalBitmapLarge == null) {
            mLoadingLock.unlock();
            return false;
        }
        updateBitmaps();
        // TODO: cleanup
        GeometryMetadata geo = MasterImage.getImage().getPreset().mGeoData;
        float w = mOriginalBitmapLarge.getWidth();
        float h = mOriginalBitmapLarge.getHeight();
        RectF r = new RectF(0, 0, w, h);
        geo.setPhotoBounds(r);
        geo.setCropBounds(r);
        MasterImage.getImage().getPreset().setGeometry(geo);
        MasterImage.getImage().notifyGeometryChange();
        mLoadingLock.unlock();
        return true;
    }

    public Uri getUri() {
        return mUri;
    }

    public Rect getOriginalBounds() {
        return mOriginalBounds;
    }

    public static int getOrientation(Context context, Uri uri) {
        if (ContentResolver.SCHEME_FILE.equals(uri.getScheme())) {
            String mimeType = context.getContentResolver().getType(uri);
            if (mimeType != ImageLoader.JPEG_MIME_TYPE) {
                return -1;
            }
            String path = uri.getPath();
            int orientation = -1;
            InputStream is = null;
            try {
                is = new FileInputStream(path);
                ExifParser parser = ExifParser.parse(is, ExifParser.OPTION_IFD_0);
                int event = parser.next();
                while (event != ExifParser.EVENT_END) {
                    if (event == ExifParser.EVENT_NEW_TAG) {
                        ExifTag tag = parser.getTag();
                        if (tag.getTagId() == ExifTag.TAG_ORIENTATION) {
                            orientation = (int) tag.getValueAt(0);
                            break;
                        }
                    }
                    event = parser.next();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ExifInvalidFormatException e) {
                e.printStackTrace();
            } finally {
                Utils.closeSilently(is);
            }
            return orientation;
        }
        Cursor cursor = null;
        try {
            cursor = context.getContentResolver().query(uri,
                    new String[] {
                        MediaStore.Images.ImageColumns.ORIENTATION
                    },
                    null, null, null);
            if (cursor.moveToNext()) {
                int ori = cursor.getInt(0);

                switch (ori) {
                    case 0:
                        return ORI_NORMAL;
                    case 90:
                        return ORI_ROTATE_90;
                    case 270:
                        return ORI_ROTATE_270;
                    case 180:
                        return ORI_ROTATE_180;
                    default:
                        return -1;
                }
            } else {
                return -1;
            }
        } catch (SQLiteException e) {
            return ExifInterface.ORIENTATION_UNDEFINED;
        } catch (IllegalArgumentException e) {
            return ExifInterface.ORIENTATION_UNDEFINED;
        } finally {
            Utils.closeSilently(cursor);
        }
    }

    private void updateBitmaps() {
        if (mOrientation > 1) {
            mOriginalBitmapSmall = rotateToPortrait(mOriginalBitmapSmall, mOrientation);
            mOriginalBitmapLarge = rotateToPortrait(mOriginalBitmapLarge, mOrientation);
        }
        mZoomOrientation = mOrientation;
        warnListeners();
    }

    public Bitmap decodeImage(int id, BitmapFactory.Options options) {
        return BitmapFactory.decodeResource(mContext.getResources(), id, options);
    }

    public static Bitmap rotateToPortrait(Bitmap bitmap, int ori) {
        Matrix matrix = new Matrix();
        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        if (ori == ORI_ROTATE_90 ||
                ori == ORI_ROTATE_270 ||
                ori == ORI_TRANSPOSE ||
                ori == ORI_TRANSVERSE) {
            int tmp = w;
            w = h;
            h = tmp;
        }
        switch (ori) {
            case ORI_ROTATE_90:
                matrix.setRotate(90, w / 2f, h / 2f);
                break;
            case ORI_ROTATE_180:
                matrix.setRotate(180, w / 2f, h / 2f);
                break;
            case ORI_ROTATE_270:
                matrix.setRotate(270, w / 2f, h / 2f);
                break;
            case ORI_FLIP_HOR:
                matrix.preScale(-1, 1);
                break;
            case ORI_FLIP_VERT:
                matrix.preScale(1, -1);
                break;
            case ORI_TRANSPOSE:
                matrix.setRotate(90, w / 2f, h / 2f);
                matrix.preScale(1, -1);
                break;
            case ORI_TRANSVERSE:
                matrix.setRotate(270, w / 2f, h / 2f);
                matrix.preScale(1, -1);
                break;
            case ORI_NORMAL:
            default:
                return bitmap;
        }

        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
                bitmap.getHeight(), matrix, true);
    }

    private Bitmap loadRegionBitmap(Uri uri, BitmapFactory.Options options, Rect bounds) {
        InputStream is = null;
        try {
            is = mContext.getContentResolver().openInputStream(uri);
            BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(is, false);
            return decoder.decodeRegion(bounds, options);
        } catch (FileNotFoundException e) {
            Log.e(LOGTAG, "FileNotFoundException: " + uri);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            Utils.closeSilently(is);
        }
        return null;
    }

    static final int MAX_BITMAP_DIM = 900;

    private Bitmap loadScaledBitmap(Uri uri, int size) {
        InputStream is = null;
        try {
            is = mContext.getContentResolver().openInputStream(uri);
            Log.v(LOGTAG, "loading uri " + uri.getPath() + " input stream: "
                    + is);
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(is, null, o);

            int width_tmp = o.outWidth;
            int height_tmp = o.outHeight;

            mOriginalBounds = new Rect(0, 0, width_tmp, height_tmp);

            int scale = 1;
            while (true) {
                if (width_tmp <= MAX_BITMAP_DIM && height_tmp <= MAX_BITMAP_DIM) {
                    if (width_tmp / 2 < size || height_tmp / 2 < size) {
                        break;
                    }
                }
                width_tmp /= 2;
                height_tmp /= 2;
                scale *= 2;
            }

            // decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            o2.inMutable = true;

            Utils.closeSilently(is);
            is = mContext.getContentResolver().openInputStream(uri);
            return BitmapFactory.decodeStream(is, null, o2);
        } catch (FileNotFoundException e) {
            Log.e(LOGTAG, "FileNotFoundException: " + uri);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            Utils.closeSilently(is);
        }
        return null;
    }

    public Bitmap getBackgroundBitmap(Resources resources) {
        if (mBackgroundBitmap == null) {
            mBackgroundBitmap = BitmapFactory.decodeResource(resources,
                    R.drawable.filtershow_background);
        }
        return mBackgroundBitmap;

    }

    public Bitmap getOriginalBitmapSmall() {
        return mOriginalBitmapSmall;
    }

    public Bitmap getOriginalBitmapLarge() {
        return mOriginalBitmapLarge;
    }

    public void addListener(ImageShow imageShow) {
        mLoadingLock.lock();
        if (!mListeners.contains(imageShow)) {
            mListeners.add(imageShow);
        }
        mLoadingLock.unlock();
    }

    private void warnListeners() {
        mActivity.runOnUiThread(mWarnListenersRunnable);
    }

    private Runnable mWarnListenersRunnable = new Runnable() {

        @Override
        public void run() {
            for (int i = 0; i < mListeners.size(); i++) {
                ImageShow imageShow = mListeners.elementAt(i);
                imageShow.imageLoaded();
            }
        }
    };

    // FIXME: this currently does the loading + filtering on the UI thread --
    // need to move this to a background thread.
    public Bitmap getScaleOneImageForPreset(ImageShow caller, ImagePreset imagePreset, Rect bounds,
                                            Rect destination, boolean force) {
        mLoadingLock.lock();
        Bitmap bmp = mZoomCache.getImage(imagePreset, bounds);
        if (force || bmp == null) {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inMutable = true;
            if (destination != null) {
                if (bounds.width() > destination.width()) {
                    int sampleSize = 1;
                    int w = bounds.width();
                    while (w > destination.width()) {
                        sampleSize *= 2;
                        w /= sampleSize;
                    }
                    options.inSampleSize = sampleSize;
                }
            }
            bmp = loadRegionBitmap(mUri, options, bounds);
            if (destination != null) {
                mLoadingLock.unlock();
                return bmp;
            }
            if (bmp != null) {
                float scaleFactor = imagePreset.getScaleFactor();
                imagePreset.setScaleFactor(1.0f);
                bmp = imagePreset.apply(bmp);
                imagePreset.setScaleFactor(scaleFactor);
                mZoomCache.setImage(imagePreset, bounds, bmp);
                mLoadingLock.unlock();
                return bmp;
            }
        }
        mLoadingLock.unlock();
        return bmp;
    }

    public void saveImage(ImagePreset preset, final FilterShowActivity filterShowActivity,
            File destination) {
        preset.setQuality(ImagePreset.QUALITY_FINAL);
        preset.setScaleFactor(1.0f);
        new SaveCopyTask(mContext, mUri, destination, new SaveCopyTask.Callback() {

            @Override
            public void onComplete(Uri result) {
                filterShowActivity.completeSaveImage(result);
            }

        }).execute(preset);
    }

    public static Bitmap loadMutableBitmap(Context context, Uri sourceUri) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        return loadMutableBitmap(context, sourceUri, options);
    }

    public static Bitmap loadMutableBitmap(Context context, Uri sourceUri,
            BitmapFactory.Options options) {
        // TODO: on <3.x we need a copy of the bitmap (inMutable doesn't
        // exist)
        options.inMutable = true;

        Bitmap bitmap = decodeUriWithBackouts(context, sourceUri, options);
        if (bitmap == null) {
            return null;
        }
        int orientation = ImageLoader.getOrientation(context, sourceUri);
        bitmap = ImageLoader.rotateToPortrait(bitmap, orientation);
        return bitmap;
    }

    public static Bitmap decodeUriWithBackouts(Context context, Uri sourceUri,
            BitmapFactory.Options options) {
        boolean noBitmap = true;
        int num_tries = 0;
        InputStream is = getInputStream(context, sourceUri);

        if (options.inSampleSize < 1) {
            options.inSampleSize = 1;
        }
        // Stopgap fix for low-memory devices.
        Bitmap bmap = null;
        while (noBitmap) {
            if (is == null) {
                return null;
            }
            try {
                // Try to decode, downsample if low-memory.
                bmap = BitmapFactory.decodeStream(is, null, options);
                noBitmap = false;
            } catch (java.lang.OutOfMemoryError e) {
                // Try 5 times before failing for good.
                if (++num_tries >= BITMAP_LOAD_BACKOUT_ATTEMPTS) {
                    throw e;
                }
                is = null;
                bmap = null;
                System.gc();
                is = getInputStream(context, sourceUri);
                options.inSampleSize *= 2;
            }
        }
        Utils.closeSilently(is);
        return bmap;
    }

    private static InputStream getInputStream(Context context, Uri sourceUri) {
        InputStream is = null;
        try {
            is = context.getContentResolver().openInputStream(sourceUri);
        } catch (FileNotFoundException e) {
            Log.w(LOGTAG, "could not load bitmap ", e);
            Utils.closeSilently(is);
            is = null;
        }
        return is;
    }

    public static Bitmap decodeResourceWithBackouts(Resources res, BitmapFactory.Options options,
            int id) {
        boolean noBitmap = true;
        int num_tries = 0;
        if (options.inSampleSize < 1) {
            options.inSampleSize = 1;
        }
        // Stopgap fix for low-memory devices.
        Bitmap bmap = null;
        while (noBitmap) {
            try {
                // Try to decode, downsample if low-memory.
                bmap = BitmapFactory.decodeResource(
                        res, id, options);
                noBitmap = false;
            } catch (java.lang.OutOfMemoryError e) {
                // Try 5 times before failing for good.
                if (++num_tries >= BITMAP_LOAD_BACKOUT_ATTEMPTS) {
                    throw e;
                }
                bmap = null;
                System.gc();
                options.inSampleSize *= 2;
            }
        }
        return bmap;
    }

    public void returnFilteredResult(ImagePreset preset,
            final FilterShowActivity filterShowActivity) {
        preset.setQuality(ImagePreset.QUALITY_FINAL);
        preset.setScaleFactor(1.0f);

        BitmapTask.Callbacks<ImagePreset> cb = new BitmapTask.Callbacks<ImagePreset>() {

            @Override
            public void onComplete(Bitmap result) {
                filterShowActivity.onFilteredResult(result);
            }

            @Override
            public void onCancel() {
            }

            @Override
            public Bitmap onExecute(ImagePreset param) {
                if (param == null || mUri == null) {
                    return null;
                }
                BitmapFactory.Options options = new BitmapFactory.Options();
                boolean noBitmap = true;
                int num_tries = 0;
                if (options.inSampleSize < 1) {
                    options.inSampleSize = 1;
                }
                Bitmap bitmap = null;
                // Stopgap fix for low-memory devices.
                while (noBitmap) {
                    try {
                        // Try to do bitmap operations, downsample if low-memory
                        bitmap = loadMutableBitmap(mContext, mUri, options);
                        if (bitmap == null) {
                            Log.w(LOGTAG, "Failed to save image!");
                            return null;
                        }
                        bitmap = param.applyGeometry(bitmap);
                        bitmap = param.apply(bitmap);
                        noBitmap = false;
                    } catch (java.lang.OutOfMemoryError e) {
                        // Try 5 times before failing for good.
                        if (++num_tries >= 5) {
                            throw e;
                        }
                        bitmap = null;
                        System.gc();
                        options.inSampleSize *= 2;
                    }
                }
                return bitmap;
            }
        };

        (new BitmapTask<ImagePreset>(cb)).execute(preset);
    }

    private String getFileExtension(String requestFormat) {
        String outputFormat = (requestFormat == null)
                ? "jpg"
                : requestFormat;
        outputFormat = outputFormat.toLowerCase();
        return (outputFormat.equals("png") || outputFormat.equals("gif"))
                ? "png" // We don't support gif compression.
                : "jpg";
    }

    private CompressFormat convertExtensionToCompressFormat(String extension) {
        return extension.equals("png") ? CompressFormat.PNG : CompressFormat.JPEG;
    }

    public void saveToUri(Bitmap bmap, Uri uri, final String outputFormat,
            final FilterShowActivity filterShowActivity) {

        OutputStream out = null;
        try {
            out = filterShowActivity.getContentResolver().openOutputStream(uri);
        } catch (FileNotFoundException e) {
            Log.w(LOGTAG, "cannot write output", e);
            out = null;
        } finally {
            if (bmap == null || out == null) {
                return;
            }
        }

        final InterruptableOutputStream ios = new InterruptableOutputStream(out);

        BitmapTask.Callbacks<Bitmap> cb = new BitmapTask.Callbacks<Bitmap>() {

            @Override
            public void onComplete(Bitmap result) {
                filterShowActivity.done();
            }

            @Override
            public void onCancel() {
                ios.interrupt();
            }

            @Override
            public Bitmap onExecute(Bitmap param) {
                CompressFormat cf = convertExtensionToCompressFormat(getFileExtension(outputFormat));
                param.compress(cf, DEFAULT_COMPRESS_QUALITY, ios);
                Utils.closeSilently(ios);
                return null;
            }
        };

        (new BitmapTask<Bitmap>(cb)).execute(bmap);
    }

    public void setAdapter(HistoryAdapter adapter) {
        mAdapter = adapter;
    }

    public HistoryAdapter getHistory() {
        return mAdapter;
    }

    public XMPMeta getXmpObject() {
        try {
            InputStream is = mContext.getContentResolver().openInputStream(getUri());
            return XmpUtilHelper.extractXMPMeta(is);
        } catch (FileNotFoundException e) {
            return null;
        }
    }

    /**
     * Determine if this is a light cycle 360 image
     *
     * @return true if it is a light Cycle image that is full 360
     */
    public boolean queryLightCycle360() {
        InputStream is = null;
        try {
            is = mContext.getContentResolver().openInputStream(getUri());
            XMPMeta meta = XmpUtilHelper.extractXMPMeta(is);
            if (meta == null) {
                return false;
            }
            String name = meta.getPacketHeader();
            String namespace = "http://ns.google.com/photos/1.0/panorama/";
            String cropWidthName = "GPano:CroppedAreaImageWidthPixels";
            String fullWidthName = "GPano:FullPanoWidthPixels";

            if (!meta.doesPropertyExist(namespace, cropWidthName)) {
                return false;
            }
            if (!meta.doesPropertyExist(namespace, fullWidthName)) {
                return false;
            }

            Integer cropValue = meta.getPropertyInteger(namespace, cropWidthName);
            Integer fullValue = meta.getPropertyInteger(namespace, fullWidthName);

            // Definition of a 360:
            // GFullPanoWidthPixels == CroppedAreaImageWidthPixels
            if (cropValue != null && fullValue != null) {
                return cropValue.equals(fullValue);
            }

            return false;
        } catch (FileNotFoundException e) {
            return false;
        } catch (XMPException e) {
            return false;
        } finally {
            Utils.closeSilently(is);
        }
    }
}