PhotoViewer handles single URIs more robustly.

Change-Id: I9395030335bbc8ff9a509154f76be33002510b5b
diff --git a/res/layout/photo_spacer_view.xml b/res/layout/photo_spacer_view.xml
deleted file mode 100644
index 3697e39..0000000
--- a/res/layout/photo_spacer_view.xml
+++ /dev/null
@@ -1,22 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!--
-     Copyright (C) 2011 Google Inc.
-     Licensed to 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.
--->
-<View xmlns:android="http://schemas.android.com/apk/res/android"
-    android:layout_width="match_parent"
-    android:layout_height="0dp"
-    android:background="@color/photo_background_color"
-    />
diff --git a/src/com/android/mail/browse/MessageHeaderAttachment.java b/src/com/android/mail/browse/MessageHeaderAttachment.java
index e1597e1..cf762e6 100644
--- a/src/com/android/mail/browse/MessageHeaderAttachment.java
+++ b/src/com/android/mail/browse/MessageHeaderAttachment.java
@@ -459,7 +459,7 @@
             final PhotoViewIntentBuilder builder =
                     Intents.newPhotoViewActivityIntentBuilder(getContext());
             builder.setAlbumName(mAttachment.name)
-                .setPhotosUri(mAttachment.contentUri.toString());
+                .setResolvedPhotoUri(mAttachment.contentUri.toString());
 
             getContext().startActivity(builder.build());
             return;
diff --git a/src/com/android/mail/photo/Intents.java b/src/com/android/mail/photo/Intents.java
index e5a956f..5ca4f75 100644
--- a/src/com/android/mail/photo/Intents.java
+++ b/src/com/android/mail/photo/Intents.java
@@ -19,6 +19,7 @@
 
 import android.content.Context;
 import android.content.Intent;
+import android.text.TextUtils;
 
 import com.android.mail.photo.fragments.PhotoViewFragment;
 import com.android.mail.photo.loaders.PhotoCursorLoader;
@@ -34,7 +35,7 @@
     public static final String EXTRA_PHOTO_INDEX = "photo_index";
     public static final String EXTRA_PHOTO_ID = "photo_id";
     public static final String EXTRA_PHOTOS_URI = "photos_uri";
-    public static final String EXTRA_PHOTO_URL = "photo_url";
+    public static final String EXTRA_RESOLVED_PHOTO_URI = "resolved_photo_uri";
     public static final String EXTRA_ALBUM_NAME = "album_name";
     public static final String EXTRA_OWNER_ID = "owner_id";
     public static final String EXTRA_TAG = "tag";
@@ -89,7 +90,7 @@
         /** The URI of the group of photos to display */
         private String mPhotosUri;
         /** The URL of the photo to display */
-        private String mPhotoUrl;
+        private String mResolvedPhotoUri;
 
         private PhotoViewIntentBuilder(Context context, Class<?> cls) {
             mIntent = new Intent(context, cls);
@@ -142,16 +143,26 @@
             return this;
         }
 
-        /** Sets the photo URL */
-        public PhotoViewIntentBuilder setPhotoUrl(String photoUrl) {
-            mPhotoUrl = photoUrl;
+        /** Sets the resolved photo URI. This method is for the case
+         *  where the URI given to {@link PhotoViewActivity} points directly
+         *  to a single image and does not need to be resolved via a query
+         *  to the {@link ContentProvider}. If this value is set, it supersedes
+         *  {@link #setPhotosUri(String)}. */
+        public PhotoViewIntentBuilder setResolvedPhotoUri(String resolvedPhotoUri) {
+            mResolvedPhotoUri = resolvedPhotoUri;
             return this;
         }
 
         /** Build the intent */
         public Intent build() {
+            if (TextUtils.isEmpty(mPhotosUri) && TextUtils.isEmpty(mResolvedPhotoUri)) {
+                throw new IllegalArgumentException("Either PhotosUri or ResolvedPhotoUri must be set.");
+            }
+
             mIntent.setAction(Intent.ACTION_VIEW);
 
+            mIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
+
             if (mAlbumName != null) {
                 mIntent.putExtra(EXTRA_ALBUM_NAME, mAlbumName);
             }
@@ -182,8 +193,8 @@
                 mIntent.putExtra(EXTRA_PHOTOS_URI, mPhotosUri);
             }
 
-            if (mPhotoUrl != null) {
-                mIntent.putExtra(EXTRA_PHOTO_URL, mPhotoUrl);
+            if (mResolvedPhotoUri != null) {
+                mIntent.putExtra(EXTRA_RESOLVED_PHOTO_URI, mResolvedPhotoUri);
             }
 
             return mIntent;
diff --git a/src/com/android/mail/photo/PhotoViewActivity.java b/src/com/android/mail/photo/PhotoViewActivity.java
index fd09cd6..22f4518 100644
--- a/src/com/android/mail/photo/PhotoViewActivity.java
+++ b/src/com/android/mail/photo/PhotoViewActivity.java
@@ -148,9 +148,10 @@
 
     public static int sMemoryClass;
 
-    // TODO(toddke) This will need to be replaced by an array of MediaRefs to support local photos
     /** The URI of the photos we're viewing; may be {@code null} */
     private String mPhotosUri;
+    /** The resolved URI of the photo to view; may be {@code null}. */
+    private String mResolvedPhotoUri;
     /** The index of the currently viewed photo */
     private int mPhotoIndex;
     /** A hint for which cursor page the photo is located on */
@@ -230,11 +231,16 @@
             mAlbumName = getResources().getString(R.string.photo_view_default_title);
         }
 
-        // id of the photos to view; optional
+        // uri of the photos to view; optional
         if (mIntent.hasExtra(Intents.EXTRA_PHOTOS_URI)) {
             mPhotosUri = mIntent.getStringExtra(Intents.EXTRA_PHOTOS_URI);
         }
 
+        // resolved uri of the photo to view; optional; supersedes mPhotosUri
+        if (mIntent.hasExtra(Intents.EXTRA_RESOLVED_PHOTO_URI)) {
+            mResolvedPhotoUri = mIntent.getStringExtra(Intents.EXTRA_RESOLVED_PHOTO_URI);
+        }
+
         // the loader page hint
         if (mIntent.hasExtra(Intents.EXTRA_PAGE_HINT) && currentItem < 0) {
             mPageHint = mIntent.getIntExtra(Intents.EXTRA_PAGE_HINT,
@@ -497,7 +503,10 @@
     public Loader<Cursor> onCreateLoader(int id, Bundle args) {
         if (id == LOADER_PHOTO_LIST) {
             mFragmentIsLoading = true;
-            return new PhotoPagerLoader(this, Uri.parse(mPhotosUri), mPageHint);
+            final Uri uri =
+                    TextUtils.isEmpty(mResolvedPhotoUri) ?
+                    Uri.parse(mPhotosUri) : Uri.parse(mResolvedPhotoUri);
+            return new PhotoPagerLoader(this, uri, mResolvedPhotoUri, mPageHint);
         }
         return null;
     }
diff --git a/src/com/android/mail/photo/adapters/PhotoPagerAdapter.java b/src/com/android/mail/photo/adapters/PhotoPagerAdapter.java
index 39ae919..8ce24fd 100644
--- a/src/com/android/mail/photo/adapters/PhotoPagerAdapter.java
+++ b/src/com/android/mail/photo/adapters/PhotoPagerAdapter.java
@@ -65,7 +65,7 @@
         final PhotoViewIntentBuilder builder =
                 Intents.newPhotoViewFragmentIntentBuilder(mContext);
           builder.setPhotoId(photoId)
-            .setPhotoUrl(photoUrl)
+            .setResolvedPhotoUri(photoUrl)
             .setAlbumName(mDefaultAlbumName)
             .setForceLoadId(mForceLoadId);
 
diff --git a/src/com/android/mail/photo/fragments/PhotoViewFragment.java b/src/com/android/mail/photo/fragments/PhotoViewFragment.java
index fc39d7b..281707e 100644
--- a/src/com/android/mail/photo/fragments/PhotoViewFragment.java
+++ b/src/com/android/mail/photo/fragments/PhotoViewFragment.java
@@ -178,7 +178,7 @@
     /** The gaia ID of the photo owner */
     private String mOwnerId;
     /** The URL of a photo to display */
-    private String mPhotoUrl;
+    private String mResolvedPhotoUri;
     /** Name of the photo */
     private String mDisplayName;
     /** Album name used if the photo doesn't have one. See b/5678229. */
@@ -274,7 +274,7 @@
 
         mPhotoId = mIntent.getLongExtra(Intents.EXTRA_PHOTO_ID, INVALID_ID);
         mOwnerId = mIntent.getStringExtra(Intents.EXTRA_OWNER_ID);
-        mPhotoUrl = mIntent.getStringExtra(Intents.EXTRA_PHOTO_URL);
+        mResolvedPhotoUri = mIntent.getStringExtra(Intents.EXTRA_RESOLVED_PHOTO_URI);
         mDefaultAlbumName = mIntent.getStringExtra(Intents.EXTRA_ALBUM_NAME);
 
         setHasOptionsMenu(true);
@@ -354,7 +354,7 @@
     @Override
     public Loader<Bitmap> onCreateLoader(int id, Bundle args) {
         if (id == LOADER_ID_PHOTO) {
-            return new PhotoBitmapLoader(getActivity(), mPhotoUrl);
+            return new PhotoBitmapLoader(getActivity(), mResolvedPhotoUri);
         } else {
             return null;
         }
diff --git a/src/com/android/mail/photo/loaders/PhotoPagerLoader.java b/src/com/android/mail/photo/loaders/PhotoPagerLoader.java
index 12ba712..4b88df5 100644
--- a/src/com/android/mail/photo/loaders/PhotoPagerLoader.java
+++ b/src/com/android/mail/photo/loaders/PhotoPagerLoader.java
@@ -21,6 +21,7 @@
 import android.database.Cursor;
 import android.database.MatrixCursor;
 import android.net.Uri;
+import android.text.TextUtils;
 
 import com.android.mail.photo.provider.PhotoContract.PhotoQuery;
 
@@ -28,38 +29,30 @@
  * Loader for a set of photo IDs.
  */
 public class PhotoPagerLoader extends PhotoCursorLoader {
-
+    private String mResolvedPhotoUri;
     public PhotoPagerLoader(
-            Context context, Uri photosUri, int pageHint) {
+            Context context, Uri photosUri, String resolvedPhotoUri, int pageHint) {
         super(context, photosUri, pageHint != LOAD_LIMIT_UNLIMITED, pageHint);
+        mResolvedPhotoUri = resolvedPhotoUri;
     }
 
     @Override
     public Cursor esLoadInBackground() {
         Cursor returnCursor = null;
 
-        final Uri loaderUri = getLoaderUri();
-
-        if (true) {
+        if (!TextUtils.isEmpty(mResolvedPhotoUri)) {
             returnCursor = new MatrixCursor(PhotoQuery.PROJECTION);
             ((MatrixCursor) returnCursor).newRow()
             .add(0)             // _id
-            .add(loaderUri.toString());
+            .add(mResolvedPhotoUri);
             return returnCursor;
         }
 
+        final Uri loaderUri = getLoaderUri();
         setUri(loaderUri);
         setProjection(PhotoQuery.PROJECTION);
         returnCursor = super.esLoadInBackground();
 
-        if (returnCursor == null || returnCursor.getCount() == 0) {
-            returnCursor.close();
-            returnCursor = new MatrixCursor(PhotoQuery.PROJECTION);
-            ((MatrixCursor) returnCursor).newRow()
-            .add(0)             // _id
-            .add(loaderUri.toString());      // url
-        }
-
         return returnCursor;
     }
 }