From a6bacbea548c115599e27a0100e027f51fb4145b Mon Sep 17 00:00:00 2001 From: Martin Hibdon Date: Tue, 16 Oct 2012 17:23:39 -0700 Subject: Allow PhotoViewActivity to choose inital photo by uri This ensures that the correct photo will be displayed even if the contents of the cursor changes between the time the activity is launched and the cursor gets loaded. Change-Id: I5f00ebde072587929f01e34c8b4b84e5eb0e9bd3 --- photoviewer/src/com/android/ex/photo/Intents.java | 18 +++++++++++++++++- .../src/com/android/ex/photo/PhotoViewActivity.java | 20 ++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/photoviewer/src/com/android/ex/photo/Intents.java b/photoviewer/src/com/android/ex/photo/Intents.java index e1e77d3..24c48fe 100644 --- a/photoviewer/src/com/android/ex/photo/Intents.java +++ b/photoviewer/src/com/android/ex/photo/Intents.java @@ -29,7 +29,7 @@ import com.android.ex.photo.fragments.PhotoViewFragment; public class Intents { // Intent extras public static final String EXTRA_PHOTO_INDEX = "photo_index"; - public static final String EXTRA_PHOTO_ID = "photo_id"; + public static final String EXTRA_INITIAL_PHOTO_URI = "initial_photo_uri"; public static final String EXTRA_PHOTOS_URI = "photos_uri"; public static final String EXTRA_RESOLVED_PHOTO_URI = "resolved_photo_uri"; public static final String EXTRA_PROJECTION = "projection"; @@ -68,6 +68,8 @@ public class Intents { /** The index of the photo to show */ private Integer mPhotoIndex; + /** The URI of the initial photo to show */ + private String mInitialPhotoUri; /** The URI of the group of photos to display */ private String mPhotosUri; /** The URL of the photo to display */ @@ -89,6 +91,12 @@ public class Intents { return this; } + /** Sets the initial photo URI */ + public PhotoViewIntentBuilder setInitialPhotoUri(String initialPhotoUri) { + mInitialPhotoUri = initialPhotoUri; + return this; + } + /** Sets the photos URI */ public PhotoViewIntentBuilder setPhotosUri(String photosUri) { mPhotosUri = photosUri; @@ -137,6 +145,14 @@ public class Intents { mIntent.putExtra(EXTRA_PHOTO_INDEX, (int) mPhotoIndex); } + if (mInitialPhotoUri != null) { + mIntent.putExtra(EXTRA_INITIAL_PHOTO_URI, mInitialPhotoUri); + } + if (mInitialPhotoUri != null && mPhotoIndex != null) { + throw new IllegalStateException( + "specified both photo index and photo uri"); + } + if (mPhotosUri != null) { mIntent.putExtra(EXTRA_PHOTOS_URI, mPhotosUri); } diff --git a/photoviewer/src/com/android/ex/photo/PhotoViewActivity.java b/photoviewer/src/com/android/ex/photo/PhotoViewActivity.java index 5e45f5e..30017da 100644 --- a/photoviewer/src/com/android/ex/photo/PhotoViewActivity.java +++ b/photoviewer/src/com/android/ex/photo/PhotoViewActivity.java @@ -31,6 +31,7 @@ import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.support.v4.view.ViewPager.OnPageChangeListener; +import android.text.TextUtils; import android.view.MenuItem; import android.view.View; @@ -112,6 +113,8 @@ public class PhotoViewActivity extends Activity implements /** The URI of the photos we're viewing; may be {@code null} */ private String mPhotosUri; + /** The URI of the photo currently viewed photo */ + private String mInitialPhotoUri; /** The index of the currently viewed photo */ private int mPhotoIndex; /** The query projection to use; may be {@code null} */ @@ -179,6 +182,9 @@ public class PhotoViewActivity extends Activity implements if (mIntent.hasExtra(Intents.EXTRA_PHOTO_INDEX) && currentItem < 0) { currentItem = mIntent.getIntExtra(Intents.EXTRA_PHOTO_INDEX, -1); } + if (mIntent.hasExtra(Intents.EXTRA_INITIAL_PHOTO_URI) && currentItem < 0) { + mInitialPhotoUri = mIntent.getStringExtra(Intents.EXTRA_INITIAL_PHOTO_URI); + } // Set the max initial scale, defaulting to 1x mMaxInitialScale = mIntent.getFloatExtra(Intents.EXTRA_MAX_INITIAL_SCALE, 1.0f); @@ -313,6 +319,20 @@ public class PhotoViewActivity extends Activity implements } else { mAlbumCount = data.getCount(); + if (mInitialPhotoUri != null) { + int index = 0; + int uriIndex = data.getColumnIndex(PhotoContract.PhotoViewColumns.URI); + while (data.moveToNext()) { + String uri = data.getString(uriIndex); + if (TextUtils.equals(uri, mInitialPhotoUri)) { + mInitialPhotoUri = null; + mPhotoIndex = index; + break; + } + index++; + } + } + // We're paused; don't do anything now, we'll get re-invoked // when the activity becomes active again // TODO(pwestbro): This shouldn't be necessary, as the loader manager should -- cgit v1.2.3