PhotoViewActivity.java revision e9825b89f8d8a61c747c5dc8aeaac9004a07d755
1/* 2 * Copyright (C) 2011 Google Inc. 3 * Licensed to The Android Open Source Project. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package com.android.ex.photo; 19 20import android.app.Activity; 21import android.app.ActivityManager; 22import android.content.Context; 23import android.content.Intent; 24import android.content.res.Resources; 25import android.database.Cursor; 26import android.graphics.Bitmap; 27import android.graphics.drawable.BitmapDrawable; 28import android.graphics.drawable.Drawable; 29import android.net.Uri; 30import android.os.Build; 31import android.os.Bundle; 32import android.os.Handler; 33import android.support.v4.app.Fragment; 34import android.support.v4.app.LoaderManager; 35import android.support.v4.content.Loader; 36import android.support.v4.view.ViewPager.OnPageChangeListener; 37import android.view.MenuItem; 38import android.support.v7.app.ActionBar; 39import android.support.v7.app.ActionBar.OnMenuVisibilityListener; 40import android.support.v7.app.ActionBarActivity; 41import android.text.TextUtils; 42import android.util.Log; 43import android.view.View; 44import android.view.ViewPropertyAnimator; 45import android.view.ViewTreeObserver.OnGlobalLayoutListener; 46import android.view.animation.AlphaAnimation; 47import android.view.animation.Animation; 48import android.view.animation.AnimationSet; 49import android.view.animation.ScaleAnimation; 50import android.view.animation.TranslateAnimation; 51import android.view.animation.Animation.AnimationListener; 52import android.widget.ImageView; 53 54import com.android.ex.photo.PhotoViewPager.InterceptType; 55import com.android.ex.photo.PhotoViewPager.OnInterceptTouchListener; 56import com.android.ex.photo.adapters.PhotoPagerAdapter; 57import com.android.ex.photo.fragments.PhotoViewFragment; 58import com.android.ex.photo.loaders.PhotoBitmapLoader; 59import com.android.ex.photo.loaders.PhotoBitmapLoaderInterface.BitmapResult; 60import com.android.ex.photo.loaders.PhotoPagerLoader; 61import com.android.ex.photo.provider.PhotoContract; 62 63import java.util.HashMap; 64import java.util.HashSet; 65import java.util.Map; 66import java.util.Set; 67 68/** 69 * Activity to view the contents of an album. 70 */ 71public class PhotoViewActivity extends ActionBarActivity implements 72 LoaderManager.LoaderCallbacks<Cursor>, OnPageChangeListener, OnInterceptTouchListener, 73 OnMenuVisibilityListener, PhotoViewCallbacks, 74 PhotoViewController.PhotoViewControllerCallbacks { 75 76 private final static String TAG = "PhotoViewActivity"; 77 78 private final static String STATE_CURRENT_URI_KEY = 79 "com.google.android.apps.plus.PhotoViewFragment.CURRENT_URI"; 80 private final static String STATE_CURRENT_INDEX_KEY = 81 "com.google.android.apps.plus.PhotoViewFragment.CURRENT_INDEX"; 82 private final static String STATE_FULLSCREEN_KEY = 83 "com.google.android.apps.plus.PhotoViewFragment.FULLSCREEN"; 84 private final static String STATE_ACTIONBARTITLE_KEY = 85 "com.google.android.apps.plus.PhotoViewFragment.ACTIONBARTITLE"; 86 private final static String STATE_ACTIONBARSUBTITLE_KEY = 87 "com.google.android.apps.plus.PhotoViewFragment.ACTIONBARTITLE"; 88 private final static String STATE_ENTERANIMATIONFINISHED_KEY = 89 "com.google.android.apps.plus.PhotoViewFragment.SCALEANIMATIONFINISHED"; 90 91 protected final static String ARG_IMAGE_URI = "image_uri"; 92 93 private static final int LOADER_PHOTO_LIST = 100; 94 95 /** Count used when the real photo count is unknown [but, may be determined] */ 96 public static final int ALBUM_COUNT_UNKNOWN = -1; 97 98 public static final int ENTER_ANIMATION_DURATION_MS = 250; 99 public static final int EXIT_ANIMATION_DURATION_MS = 250; 100 101 /** Argument key for the dialog message */ 102 public static final String KEY_MESSAGE = "dialog_message"; 103 104 public static int sMemoryClass; 105 106 /** The URI of the photos we're viewing; may be {@code null} */ 107 private String mPhotosUri; 108 /** The index of the currently viewed photo */ 109 private int mCurrentPhotoIndex; 110 /** The uri of the currently viewed photo */ 111 private String mCurrentPhotoUri; 112 /** The query projection to use; may be {@code null} */ 113 private String[] mProjection; 114 /** The total number of photos; only valid if {@link #mIsEmpty} is {@code false}. */ 115 protected int mAlbumCount = ALBUM_COUNT_UNKNOWN; 116 /** {@code true} if the view is empty. Otherwise, {@code false}. */ 117 protected boolean mIsEmpty; 118 /** the main root view */ 119 protected View mRootView; 120 /** Background image that contains nothing, so it can be alpha faded from 121 * transparent to black without affecting any other views. */ 122 protected View mBackground; 123 /** The main pager; provides left/right swipe between photos */ 124 protected PhotoViewPager mViewPager; 125 /** The temporary image so that we can quickly scale up the fullscreen thumbnail */ 126 protected ImageView mTemporaryImage; 127 /** Adapter to create pager views */ 128 protected PhotoPagerAdapter mAdapter; 129 /** Whether or not we're in "full screen" mode */ 130 protected boolean mFullScreen; 131 /** The listeners wanting full screen state for each screen position */ 132 private final Map<Integer, OnScreenListener> 133 mScreenListeners = new HashMap<Integer, OnScreenListener>(); 134 /** The set of listeners wanting full screen state */ 135 private final Set<CursorChangedListener> mCursorListeners = new HashSet<CursorChangedListener>(); 136 /** When {@code true}, restart the loader when the activity becomes active */ 137 private boolean mRestartLoader; 138 /** Whether or not this activity is paused */ 139 protected boolean mIsPaused = true; 140 /** The maximum scale factor applied to images when they are initially displayed */ 141 protected float mMaxInitialScale; 142 /** The title in the actionbar */ 143 protected String mActionBarTitle; 144 /** The subtitle in the actionbar */ 145 protected String mActionBarSubtitle; 146 147 private boolean mEnterAnimationFinished; 148 protected boolean mScaleAnimationEnabled; 149 protected int mAnimationStartX; 150 protected int mAnimationStartY; 151 protected int mAnimationStartWidth; 152 protected int mAnimationStartHeight; 153 154 protected boolean mActionBarHiddenInitially; 155 protected boolean mDisplayThumbsFullScreen; 156 157 protected BitmapCallback mBitmapCallback; 158 protected final Handler mHandler = new Handler(); 159 160 // TODO Find a better way to do this. We basically want the activity to display the 161 // "loading..." progress until the fragment takes over and shows it's own "loading..." 162 // progress [located in photo_header_view.xml]. We could potentially have all status displayed 163 // by the activity, but, that gets tricky when it comes to screen rotation. For now, we 164 // track the loading by this variable which is fragile and may cause phantom "loading..." 165 // text. 166 private long mEnterFullScreenDelayTime; 167 168 private PhotoViewController mController; 169 170 protected PhotoPagerAdapter createPhotoPagerAdapter(Context context, 171 android.support.v4.app.FragmentManager fm, Cursor c, float maxScale) { 172 PhotoPagerAdapter adapter = new PhotoPagerAdapter(context, fm, c, maxScale, 173 mDisplayThumbsFullScreen); 174 return adapter; 175 } 176 177 @Override 178 protected void onCreate(Bundle savedInstanceState) { 179 super.onCreate(savedInstanceState); 180 181 final ActivityManager mgr = (ActivityManager) getApplicationContext(). 182 getSystemService(Activity.ACTIVITY_SERVICE); 183 sMemoryClass = mgr.getMemoryClass(); 184 185 mController = new PhotoViewController(this); 186 187 final Intent intent = getIntent(); 188 // uri of the photos to view; optional 189 if (intent.hasExtra(Intents.EXTRA_PHOTOS_URI)) { 190 mPhotosUri = intent.getStringExtra(Intents.EXTRA_PHOTOS_URI); 191 } 192 if (intent.getBooleanExtra(Intents.EXTRA_SCALE_UP_ANIMATION, false)) { 193 mScaleAnimationEnabled = true; 194 mAnimationStartX = intent.getIntExtra(Intents.EXTRA_ANIMATION_START_X, 0); 195 mAnimationStartY = intent.getIntExtra(Intents.EXTRA_ANIMATION_START_Y, 0); 196 mAnimationStartWidth = intent.getIntExtra(Intents.EXTRA_ANIMATION_START_WIDTH, 0); 197 mAnimationStartHeight = intent.getIntExtra(Intents.EXTRA_ANIMATION_START_HEIGHT, 0); 198 } 199 mActionBarHiddenInitially = intent.getBooleanExtra( 200 Intents.EXTRA_ACTION_BAR_HIDDEN_INITIALLY, false); 201 mDisplayThumbsFullScreen = intent.getBooleanExtra( 202 Intents.EXTRA_DISPLAY_THUMBS_FULLSCREEN, false); 203 204 // projection for the query; optional 205 // If not set, the default projection is used. 206 // This projection must include the columns from the default projection. 207 if (intent.hasExtra(Intents.EXTRA_PROJECTION)) { 208 mProjection = intent.getStringArrayExtra(Intents.EXTRA_PROJECTION); 209 } else { 210 mProjection = null; 211 } 212 213 // Set the max initial scale, defaulting to 1x 214 mMaxInitialScale = intent.getFloatExtra(Intents.EXTRA_MAX_INITIAL_SCALE, 1.0f); 215 mCurrentPhotoUri = null; 216 mCurrentPhotoIndex = -1; 217 218 // We allow specifying the current photo by either index or uri. 219 // This is because some users may have live datasets that can change, 220 // adding new items to either the beginning or end of the set. For clients 221 // that do not need that capability, ability to specify the current photo 222 // by index is offered as a convenience. 223 if (intent.hasExtra(Intents.EXTRA_PHOTO_INDEX)) { 224 mCurrentPhotoIndex = intent.getIntExtra(Intents.EXTRA_PHOTO_INDEX, -1); 225 } 226 if (intent.hasExtra(Intents.EXTRA_INITIAL_PHOTO_URI)) { 227 mCurrentPhotoUri = intent.getStringExtra(Intents.EXTRA_INITIAL_PHOTO_URI); 228 } 229 mIsEmpty = true; 230 231 if (savedInstanceState != null) { 232 mCurrentPhotoUri = savedInstanceState.getString(STATE_CURRENT_URI_KEY); 233 mCurrentPhotoIndex = savedInstanceState.getInt(STATE_CURRENT_INDEX_KEY); 234 mFullScreen = savedInstanceState.getBoolean(STATE_FULLSCREEN_KEY, false); 235 mActionBarTitle = savedInstanceState.getString(STATE_ACTIONBARTITLE_KEY); 236 mActionBarSubtitle = savedInstanceState.getString(STATE_ACTIONBARSUBTITLE_KEY); 237 mEnterAnimationFinished = savedInstanceState.getBoolean( 238 STATE_ENTERANIMATIONFINISHED_KEY, false); 239 } else { 240 mFullScreen = mActionBarHiddenInitially; 241 } 242 243 setContentView(R.layout.photo_activity_view); 244 245 // Create the adapter and add the view pager 246 mAdapter = 247 createPhotoPagerAdapter(this, getSupportFragmentManager(), null, mMaxInitialScale); 248 final Resources resources = getResources(); 249 mRootView = findViewById(R.id.photo_activity_root_view); 250 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 251 mRootView.setOnSystemUiVisibilityChangeListener( 252 mController.getSystemUiVisibilityChangeListener()); 253 } 254 mBackground = findViewById(R.id.photo_activity_background); 255 mTemporaryImage = (ImageView) findViewById(R.id.photo_activity_temporary_image); 256 mViewPager = (PhotoViewPager) findViewById(R.id.photo_view_pager); 257 mViewPager.setAdapter(mAdapter); 258 mViewPager.setOnPageChangeListener(this); 259 mViewPager.setOnInterceptTouchListener(this); 260 mViewPager.setPageMargin(resources.getDimensionPixelSize(R.dimen.photo_page_margin)); 261 262 mBitmapCallback = new BitmapCallback(); 263 if (!mScaleAnimationEnabled || mEnterAnimationFinished) { 264 // We are not running the scale up animation. Just let the fragments 265 // display and handle the animation. 266 getSupportLoaderManager().initLoader(LOADER_PHOTO_LIST, null, this); 267 // Make the background opaque immediately so that we don't see the activity 268 // behind this one. 269 mBackground.setVisibility(View.VISIBLE); 270 } else { 271 // Attempt to load the initial image thumbnail. Once we have the 272 // image, animate it up. Once the animation is complete, we can kick off 273 // loading the ViewPager. After the primary fullres image is loaded, we will 274 // make our temporary image invisible and display the ViewPager. 275 mViewPager.setVisibility(View.GONE); 276 Bundle args = new Bundle(); 277 args.putString(ARG_IMAGE_URI, mCurrentPhotoUri); 278 getSupportLoaderManager().initLoader(BITMAP_LOADER_THUMBNAIL, args, mBitmapCallback); 279 } 280 281 mEnterFullScreenDelayTime = 282 resources.getInteger(R.integer.reenter_fullscreen_delay_time_in_millis); 283 284 final ActionBar actionBar = getSupportActionBar(); 285 if (actionBar != null) { 286 actionBar.setDisplayHomeAsUpEnabled(true); 287 actionBar.addOnMenuVisibilityListener(this); 288 final int showTitle = ActionBar.DISPLAY_SHOW_TITLE; 289 actionBar.setDisplayOptions(showTitle, showTitle); 290 // Set the title and subtitle immediately here, rather than waiting 291 // for the fragment to be initialized. 292 setActionBarTitles(actionBar); 293 } 294 295 if (!mScaleAnimationEnabled) { 296 setLightsOutMode(mFullScreen); 297 } else { 298 // Keep lights out mode as false. This is to prevent jank cause by concurrent 299 // animations during the enter animation. 300 setLightsOutMode(false); 301 } 302 } 303 304 @Override 305 protected void onResume() { 306 super.onResume(); 307 setFullScreen(mFullScreen, false); 308 309 mIsPaused = false; 310 if (mRestartLoader) { 311 mRestartLoader = false; 312 getSupportLoaderManager().restartLoader(LOADER_PHOTO_LIST, null, this); 313 } 314 } 315 316 @Override 317 protected void onPause() { 318 mIsPaused = true; 319 super.onPause(); 320 } 321 322 @Override 323 public void onBackPressed() { 324 // If we are in fullscreen mode, and the default is not full screen, then 325 // switch back to actionBar display mode. 326 if (mFullScreen && !mActionBarHiddenInitially) { 327 toggleFullScreen(); 328 } else { 329 if (mScaleAnimationEnabled) { 330 runExitAnimation(); 331 } else { 332 super.onBackPressed(); 333 } 334 } 335 } 336 337 @Override 338 public void onSaveInstanceState(Bundle outState) { 339 super.onSaveInstanceState(outState); 340 341 outState.putString(STATE_CURRENT_URI_KEY, mCurrentPhotoUri); 342 outState.putInt(STATE_CURRENT_INDEX_KEY, mCurrentPhotoIndex); 343 outState.putBoolean(STATE_FULLSCREEN_KEY, mFullScreen); 344 outState.putString(STATE_ACTIONBARTITLE_KEY, mActionBarTitle); 345 outState.putString(STATE_ACTIONBARSUBTITLE_KEY, mActionBarSubtitle); 346 outState.putBoolean(STATE_ENTERANIMATIONFINISHED_KEY, mEnterAnimationFinished); 347 } 348 349 @Override 350 public boolean onOptionsItemSelected(MenuItem item) { 351 switch (item.getItemId()) { 352 case android.R.id.home: 353 finish(); 354 return true; 355 default: 356 return super.onOptionsItemSelected(item); 357 } 358 } 359 360 @Override 361 public void addScreenListener(int position, OnScreenListener listener) { 362 mScreenListeners.put(position, listener); 363 } 364 365 @Override 366 public void removeScreenListener(int position) { 367 mScreenListeners.remove(position); 368 } 369 370 @Override 371 public synchronized void addCursorListener(CursorChangedListener listener) { 372 mCursorListeners.add(listener); 373 } 374 375 @Override 376 public synchronized void removeCursorListener(CursorChangedListener listener) { 377 mCursorListeners.remove(listener); 378 } 379 380 @Override 381 public boolean isFragmentFullScreen(Fragment fragment) { 382 if (mViewPager == null || mAdapter == null || mAdapter.getCount() == 0) { 383 return mFullScreen; 384 } 385 return mFullScreen || (mViewPager.getCurrentItem() != mAdapter.getItemPosition(fragment)); 386 } 387 388 @Override 389 public void toggleFullScreen() { 390 setFullScreen(!mFullScreen, true); 391 } 392 393 public void onPhotoRemoved(long photoId) { 394 final Cursor data = mAdapter.getCursor(); 395 if (data == null) { 396 // Huh?! How would this happen? 397 return; 398 } 399 400 final int dataCount = data.getCount(); 401 if (dataCount <= 1) { 402 finish(); 403 return; 404 } 405 406 getSupportLoaderManager().restartLoader(LOADER_PHOTO_LIST, null, this); 407 } 408 409 @Override 410 public Loader<Cursor> onCreateLoader(int id, Bundle args) { 411 if (id == LOADER_PHOTO_LIST) { 412 return new PhotoPagerLoader(this, Uri.parse(mPhotosUri), mProjection); 413 } 414 return null; 415 } 416 417 @Override 418 public Loader<BitmapResult> onCreateBitmapLoader(int id, Bundle args, String uri) { 419 switch (id) { 420 case BITMAP_LOADER_AVATAR: 421 case BITMAP_LOADER_THUMBNAIL: 422 case BITMAP_LOADER_PHOTO: 423 return new PhotoBitmapLoader(this, uri); 424 default: 425 return null; 426 } 427 } 428 429 @Override 430 public void onLoadFinished(Loader<Cursor> loader, Cursor data) { 431 432 final int id = loader.getId(); 433 if (id == LOADER_PHOTO_LIST) { 434 if (data == null || data.getCount() == 0) { 435 mIsEmpty = true; 436 } else { 437 mAlbumCount = data.getCount(); 438 if (mCurrentPhotoUri != null) { 439 int index = 0; 440 // Clear query params. Compare only the path. 441 final int uriIndex = data.getColumnIndex(PhotoContract.PhotoViewColumns.URI); 442 final Uri currentPhotoUri; 443 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 444 currentPhotoUri = Uri.parse(mCurrentPhotoUri).buildUpon() 445 .clearQuery().build(); 446 } else { 447 currentPhotoUri = Uri.parse(mCurrentPhotoUri).buildUpon() 448 .query(null).build(); 449 } 450 while (data.moveToNext()) { 451 final String uriString = data.getString(uriIndex); 452 final Uri uri; 453 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 454 uri = Uri.parse(uriString).buildUpon().clearQuery().build(); 455 } else { 456 uri = Uri.parse(uriString).buildUpon().query(null).build(); 457 } 458 if (currentPhotoUri != null && currentPhotoUri.equals(uri)) { 459 mCurrentPhotoIndex = index; 460 break; 461 } 462 index++; 463 } 464 } 465 466 // We're paused; don't do anything now, we'll get re-invoked 467 // when the activity becomes active again 468 // TODO(pwestbro): This shouldn't be necessary, as the loader manager should 469 // restart the loader 470 if (mIsPaused) { 471 mRestartLoader = true; 472 return; 473 } 474 boolean wasEmpty = mIsEmpty; 475 mIsEmpty = false; 476 477 mAdapter.swapCursor(data); 478 if (mViewPager.getAdapter() == null) { 479 mViewPager.setAdapter(mAdapter); 480 } 481 notifyCursorListeners(data); 482 483 // Use an index of 0 if the index wasn't specified or couldn't be found 484 if (mCurrentPhotoIndex < 0) { 485 mCurrentPhotoIndex = 0; 486 } 487 488 mViewPager.setCurrentItem(mCurrentPhotoIndex, false); 489 if (wasEmpty) { 490 setViewActivated(mCurrentPhotoIndex); 491 } 492 } 493 // Update the any action items 494 updateActionItems(); 495 } 496 } 497 498 @Override 499 public void onLoaderReset(android.support.v4.content.Loader<Cursor> loader) { 500 // If the loader is reset, remove the reference in the adapter to this cursor 501 // TODO(pwestbro): reenable this when b/7075236 is fixed 502 // mAdapter.swapCursor(null); 503 } 504 505 protected void updateActionItems() { 506 // Do nothing, but allow extending classes to do work 507 } 508 509 private synchronized void notifyCursorListeners(Cursor data) { 510 // tell all of the objects listening for cursor changes 511 // that the cursor has changed 512 for (CursorChangedListener listener : mCursorListeners) { 513 listener.onCursorChanged(data); 514 } 515 } 516 517 @Override 518 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 519 } 520 521 @Override 522 public void onPageSelected(int position) { 523 mCurrentPhotoIndex = position; 524 setViewActivated(position); 525 } 526 527 @Override 528 public void onPageScrollStateChanged(int state) { 529 } 530 531 @Override 532 public boolean isFragmentActive(Fragment fragment) { 533 if (mViewPager == null || mAdapter == null) { 534 return false; 535 } 536 return mViewPager.getCurrentItem() == mAdapter.getItemPosition(fragment); 537 } 538 539 @Override 540 public void onFragmentVisible(PhotoViewFragment fragment) { 541 // Do nothing, we handle this in setViewActivated 542 } 543 544 @Override 545 public InterceptType onTouchIntercept(float origX, float origY) { 546 boolean interceptLeft = false; 547 boolean interceptRight = false; 548 549 for (OnScreenListener listener : mScreenListeners.values()) { 550 if (!interceptLeft) { 551 interceptLeft = listener.onInterceptMoveLeft(origX, origY); 552 } 553 if (!interceptRight) { 554 interceptRight = listener.onInterceptMoveRight(origX, origY); 555 } 556 } 557 558 if (interceptLeft) { 559 if (interceptRight) { 560 return InterceptType.BOTH; 561 } 562 return InterceptType.LEFT; 563 } else if (interceptRight) { 564 return InterceptType.RIGHT; 565 } 566 return InterceptType.NONE; 567 } 568 569 /** 570 * Updates the title bar according to the value of {@link #mFullScreen}. 571 */ 572 protected void setFullScreen(boolean fullScreen, boolean setDelayedRunnable) { 573 final boolean fullScreenChanged = (fullScreen != mFullScreen); 574 mFullScreen = fullScreen; 575 576 if (mFullScreen) { 577 setLightsOutMode(true); 578 cancelEnterFullScreenRunnable(); 579 } else { 580 setLightsOutMode(false); 581 if (setDelayedRunnable) { 582 postEnterFullScreenRunnableWithDelay(); 583 } 584 } 585 586 if (fullScreenChanged) { 587 for (OnScreenListener listener : mScreenListeners.values()) { 588 listener.onFullScreenChanged(mFullScreen); 589 } 590 } 591 } 592 593 private void postEnterFullScreenRunnableWithDelay() { 594 mHandler.postDelayed(mEnterFullScreenRunnable, mEnterFullScreenDelayTime); 595 } 596 597 private void cancelEnterFullScreenRunnable() { 598 mHandler.removeCallbacks(mEnterFullScreenRunnable); 599 } 600 601 protected void setLightsOutMode(boolean enabled) { 602 mController.setImmersiveMode(enabled); 603 } 604 605 private final Runnable mEnterFullScreenRunnable = new Runnable() { 606 @Override 607 public void run() { 608 setFullScreen(true, true); 609 } 610 }; 611 612 @Override 613 public void setViewActivated(int position) { 614 OnScreenListener listener = mScreenListeners.get(position); 615 if (listener != null) { 616 listener.onViewActivated(); 617 } 618 final Cursor cursor = getCursorAtProperPosition(); 619 mCurrentPhotoIndex = position; 620 // FLAG: get the column indexes once in onLoadFinished(). 621 // That would make this more efficient, instead of looking these up 622 // repeatedly whenever we want them. 623 int uriIndex = cursor.getColumnIndex(PhotoContract.PhotoViewColumns.URI); 624 mCurrentPhotoUri = cursor.getString(uriIndex); 625 updateActionBar(); 626 627 // Restart the timer to return to fullscreen. 628 cancelEnterFullScreenRunnable(); 629 postEnterFullScreenRunnableWithDelay(); 630 } 631 632 /** 633 * Adjusts the activity title and subtitle to reflect the photo name and count. 634 */ 635 protected void updateActionBar() { 636 final int position = mViewPager.getCurrentItem() + 1; 637 final boolean hasAlbumCount = mAlbumCount >= 0; 638 639 final Cursor cursor = getCursorAtProperPosition(); 640 if (cursor != null) { 641 // FLAG: We should grab the indexes when we first get the cursor 642 // and store them so we don't need to do it each time. 643 final int photoNameIndex = cursor.getColumnIndex(PhotoContract.PhotoViewColumns.NAME); 644 mActionBarTitle = cursor.getString(photoNameIndex); 645 } else { 646 mActionBarTitle = null; 647 } 648 649 if (mIsEmpty || !hasAlbumCount || position <= 0) { 650 mActionBarSubtitle = null; 651 } else { 652 mActionBarSubtitle = 653 getResources().getString(R.string.photo_view_count, position, mAlbumCount); 654 } 655 656 setActionBarTitles(getSupportActionBar()); 657 } 658 659 /** 660 * Sets the Action Bar title to {@link #mActionBarTitle} and the subtitle to 661 * {@link #mActionBarSubtitle} 662 */ 663 protected final void setActionBarTitles(ActionBar actionBar) { 664 if (actionBar == null) { 665 return; 666 } 667 actionBar.setTitle(getInputOrEmpty(mActionBarTitle)); 668 actionBar.setSubtitle(getInputOrEmpty(mActionBarSubtitle)); 669 } 670 671 /** 672 * If the input string is non-null, it is returned, otherwise an empty string is returned; 673 * @param in 674 * @return 675 */ 676 private static final String getInputOrEmpty(String in) { 677 if (in == null) { 678 return ""; 679 } 680 return in; 681 } 682 683 /** 684 * Utility method that will return the cursor that contains the data 685 * at the current position so that it refers to the current image on screen. 686 * @return the cursor at the current position or 687 * null if no cursor exists or if the {@link PhotoViewPager} is null. 688 */ 689 public Cursor getCursorAtProperPosition() { 690 if (mViewPager == null) { 691 return null; 692 } 693 694 final int position = mViewPager.getCurrentItem(); 695 final Cursor cursor = mAdapter.getCursor(); 696 697 if (cursor == null) { 698 return null; 699 } 700 701 cursor.moveToPosition(position); 702 703 return cursor; 704 } 705 706 public Cursor getCursor() { 707 return (mAdapter == null) ? null : mAdapter.getCursor(); 708 } 709 710 @Override 711 public void onMenuVisibilityChanged(boolean isVisible) { 712 if (isVisible) { 713 cancelEnterFullScreenRunnable(); 714 } else { 715 postEnterFullScreenRunnableWithDelay(); 716 } 717 } 718 719 @Override 720 public void onNewPhotoLoaded(int position) { 721 // do nothing 722 } 723 724 protected void setPhotoIndex(int index) { 725 mCurrentPhotoIndex = index; 726 } 727 728 @Override 729 public void onFragmentPhotoLoadComplete(PhotoViewFragment fragment, boolean success) { 730 if (mTemporaryImage.getVisibility() != View.GONE && 731 TextUtils.equals(fragment.getPhotoUri(), mCurrentPhotoUri)) { 732 if (success) { 733 // The fragment for the current image is now ready for display. 734 mTemporaryImage.setVisibility(View.GONE); 735 mViewPager.setVisibility(View.VISIBLE); 736 } else { 737 // This means that we are unable to load the fragment's photo. 738 // I'm not sure what the best thing to do here is, but at least if 739 // we display the viewPager, the fragment itself can decide how to 740 // display the failure of its own image. 741 Log.w(TAG, "Failed to load fragment image"); 742 mTemporaryImage.setVisibility(View.GONE); 743 mViewPager.setVisibility(View.VISIBLE); 744 } 745 } 746 } 747 748 protected boolean isFullScreen() { 749 return mFullScreen; 750 } 751 752 @Override 753 public void onCursorChanged(PhotoViewFragment fragment, Cursor cursor) { 754 // do nothing 755 } 756 757 @Override 758 public PhotoPagerAdapter getAdapter() { 759 return mAdapter; 760 } 761 762 public void onEnterAnimationComplete() { 763 mEnterAnimationFinished = true; 764 mViewPager.setVisibility(View.VISIBLE); 765 setLightsOutMode(mFullScreen); 766 } 767 768 private void onExitAnimationComplete() { 769 finish(); 770 overridePendingTransition(0, 0); 771 } 772 773 private void runEnterAnimation() { 774 final int totalWidth = mRootView.getMeasuredWidth(); 775 final int totalHeight = mRootView.getMeasuredHeight(); 776 777 // FLAG: Need to handle the aspect ratio of the bitmap. If it's a portrait 778 // bitmap, then we need to position the view higher so that the middle 779 // pixels line up. 780 mTemporaryImage.setVisibility(View.VISIBLE); 781 // We need to take a full screen image, and scale/translate it so that 782 // it appears at exactly the same location onscreen as it is in the 783 // prior activity. 784 // The final image will take either the full screen width or height (or both). 785 786 final float scaleW = (float) mAnimationStartWidth / totalWidth; 787 final float scaleY = (float) mAnimationStartHeight / totalHeight; 788 final float scale = Math.max(scaleW, scaleY); 789 790 final int translateX = calculateTranslate(mAnimationStartX, mAnimationStartWidth, 791 totalWidth, scale); 792 final int translateY = calculateTranslate(mAnimationStartY, mAnimationStartHeight, 793 totalHeight, scale); 794 795 final int version = android.os.Build.VERSION.SDK_INT; 796 if (version >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { 797 mBackground.setAlpha(0f); 798 mBackground.animate().alpha(1f).setDuration(ENTER_ANIMATION_DURATION_MS).start(); 799 mBackground.setVisibility(View.VISIBLE); 800 801 mTemporaryImage.setScaleX(scale); 802 mTemporaryImage.setScaleY(scale); 803 mTemporaryImage.setTranslationX(translateX); 804 mTemporaryImage.setTranslationY(translateY); 805 806 Runnable endRunnable = new Runnable() { 807 @Override 808 public void run() { 809 PhotoViewActivity.this.onEnterAnimationComplete(); 810 } 811 }; 812 ViewPropertyAnimator animator = mTemporaryImage.animate().scaleX(1f).scaleY(1f) 813 .translationX(0).translationY(0).setDuration(ENTER_ANIMATION_DURATION_MS); 814 if (version >= Build.VERSION_CODES.JELLY_BEAN) { 815 animator.withEndAction(endRunnable); 816 } else { 817 mHandler.postDelayed(endRunnable, ENTER_ANIMATION_DURATION_MS); 818 } 819 animator.start(); 820 } else { 821 final Animation alphaAnimation = new AlphaAnimation(0f, 1f); 822 alphaAnimation.setDuration(ENTER_ANIMATION_DURATION_MS); 823 mBackground.startAnimation(alphaAnimation); 824 mBackground.setVisibility(View.VISIBLE); 825 826 final Animation translateAnimation = new TranslateAnimation(translateX, 827 translateY, 0, 0); 828 translateAnimation.setDuration(ENTER_ANIMATION_DURATION_MS); 829 Animation scaleAnimation = new ScaleAnimation(scale, scale, 0, 0); 830 scaleAnimation.setDuration(ENTER_ANIMATION_DURATION_MS); 831 832 AnimationSet animationSet = new AnimationSet(true); 833 animationSet.addAnimation(translateAnimation); 834 animationSet.addAnimation(scaleAnimation); 835 AnimationListener listener = new AnimationListener() { 836 @Override 837 public void onAnimationEnd(Animation arg0) { 838 PhotoViewActivity.this.onEnterAnimationComplete(); 839 } 840 841 @Override 842 public void onAnimationRepeat(Animation arg0) { 843 } 844 845 @Override 846 public void onAnimationStart(Animation arg0) { 847 } 848 }; 849 animationSet.setAnimationListener(listener); 850 mTemporaryImage.startAnimation(animationSet); 851 } 852 } 853 854 private void runExitAnimation() { 855 Intent intent = getIntent(); 856 // FLAG: should just fall back to a standard animation if either: 857 // 1. images have been added or removed since we've been here, or 858 // 2. we are currently looking at some image other than the one we 859 // started on. 860 861 final int totalWidth = mRootView.getMeasuredWidth(); 862 final int totalHeight = mRootView.getMeasuredHeight(); 863 864 // We need to take a full screen image, and scale/translate it so that 865 // it appears at exactly the same location onscreen as it is in the 866 // prior activity. 867 // The final image will take either the full screen width or height (or both). 868 final float scaleW = (float) mAnimationStartWidth / totalWidth; 869 final float scaleY = (float) mAnimationStartHeight / totalHeight; 870 final float scale = Math.max(scaleW, scaleY); 871 872 final int translateX = calculateTranslate(mAnimationStartX, mAnimationStartWidth, 873 totalWidth, scale); 874 final int translateY = calculateTranslate(mAnimationStartY, mAnimationStartHeight, 875 totalHeight, scale); 876 final int version = android.os.Build.VERSION.SDK_INT; 877 if (version >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { 878 mBackground.animate().alpha(0f).setDuration(EXIT_ANIMATION_DURATION_MS).start(); 879 mBackground.setVisibility(View.VISIBLE); 880 881 Runnable endRunnable = new Runnable() { 882 @Override 883 public void run() { 884 PhotoViewActivity.this.onExitAnimationComplete(); 885 } 886 }; 887 // If the temporary image is still visible it means that we have 888 // not yet loaded the fullres image, so we need to animate 889 // the temporary image out. 890 ViewPropertyAnimator animator = null; 891 if (mTemporaryImage.getVisibility() == View.VISIBLE) { 892 animator = mTemporaryImage.animate().scaleX(scale).scaleY(scale) 893 .translationX(translateX).translationY(translateY) 894 .setDuration(EXIT_ANIMATION_DURATION_MS); 895 } else { 896 animator = mViewPager.animate().scaleX(scale).scaleY(scale) 897 .translationX(translateX).translationY(translateY) 898 .setDuration(EXIT_ANIMATION_DURATION_MS); 899 } 900 if (version >= android.os.Build.VERSION_CODES.JELLY_BEAN) { 901 animator.withEndAction(endRunnable); 902 } else { 903 mHandler.postDelayed(endRunnable, EXIT_ANIMATION_DURATION_MS); 904 } 905 animator.start(); 906 } else { 907 final Animation alphaAnimation = new AlphaAnimation(1f, 0f); 908 alphaAnimation.setDuration(EXIT_ANIMATION_DURATION_MS); 909 mBackground.startAnimation(alphaAnimation); 910 mBackground.setVisibility(View.VISIBLE); 911 912 final Animation scaleAnimation = new ScaleAnimation(1f, 1f, scale, scale); 913 scaleAnimation.setDuration(EXIT_ANIMATION_DURATION_MS); 914 AnimationListener listener = new AnimationListener() { 915 @Override 916 public void onAnimationEnd(Animation arg0) { 917 PhotoViewActivity.this.onExitAnimationComplete(); 918 } 919 920 @Override 921 public void onAnimationRepeat(Animation arg0) { 922 } 923 924 @Override 925 public void onAnimationStart(Animation arg0) { 926 } 927 }; 928 scaleAnimation.setAnimationListener(listener); 929 // If the temporary image is still visible it means that we have 930 // not yet loaded the fullres image, so we need to animate 931 // the temporary image out. 932 if (mTemporaryImage.getVisibility() == View.VISIBLE) { 933 mTemporaryImage.startAnimation(scaleAnimation); 934 } else { 935 mViewPager.startAnimation(scaleAnimation); 936 } 937 } 938 } 939 940 private int calculateTranslate(int start, int startSize, int totalSize, float scale) { 941 // Translation takes precedence over scale. What this means is that if 942 // we want an view's upper left corner to be a particular spot on screen, 943 // but that view is scaled to something other than 1, we need to take into 944 // account the pixels lost to scaling. 945 // So if we have a view that is 200x300, and we want it's upper left corner 946 // to be at 50x50, but it's scaled by 50%, we can't just translate it to 50x50. 947 // If we were to do that, the view's *visible* upper left corner would be at 948 // 100x200. We need to take into account the difference between the outside 949 // size of the view (i.e. the size prior to scaling) and the scaled size. 950 // scaleFromEdge is the difference between the visible left edge and the 951 // actual left edge, due to scaling. 952 // scaleFromTop is the difference between the visible top edge, and the 953 // actual top edge, due to scaling. 954 int scaleFromEdge = Math.round((totalSize - totalSize * scale) / 2); 955 956 // The imageView is fullscreen, regardless of the aspect ratio of the actual image. 957 // This means that some portion of the imageView will be blank. We need to 958 // take into account the size of the blank area so that the actual image 959 // lines up with the starting image. 960 int blankSize = Math.round((totalSize * scale - startSize) / 2); 961 962 return start - scaleFromEdge - blankSize; 963 } 964 965 private void initTemporaryImage(Drawable drawable) { 966 if (mEnterAnimationFinished) { 967 // Forget this, we've already run the animation. 968 return; 969 } 970 mTemporaryImage.setImageDrawable(drawable); 971 if (drawable != null) { 972 // We have not yet run the enter animation. Start it now. 973 int totalWidth = mRootView.getMeasuredWidth(); 974 if (totalWidth == 0) { 975 // the measure pass has not yet finished. We can't properly 976 // run out animation until that is done. Listen for the layout 977 // to occur, then fire the animation. 978 final View base = mRootView; 979 base.getViewTreeObserver().addOnGlobalLayoutListener( 980 new OnGlobalLayoutListener() { 981 @Override 982 public void onGlobalLayout() { 983 int version = android.os.Build.VERSION.SDK_INT; 984 if (version >= android.os.Build.VERSION_CODES.JELLY_BEAN) { 985 base.getViewTreeObserver().removeOnGlobalLayoutListener(this); 986 } else { 987 base.getViewTreeObserver().removeGlobalOnLayoutListener(this); 988 } 989 runEnterAnimation(); 990 } 991 }); 992 } else { 993 // initiate the animation 994 runEnterAnimation(); 995 } 996 } 997 // Kick off the photo list loader 998 getSupportLoaderManager().initLoader(LOADER_PHOTO_LIST, null, this); 999 } 1000 1001 // START PhotoViewControllerCallbacks 1002 1003 @Override 1004 public void showActionBar() { 1005 getSupportActionBar().show(); 1006 } 1007 1008 @Override 1009 public void hideActionBar() { 1010 getSupportActionBar().hide(); 1011 } 1012 1013 @Override 1014 public boolean isScaleAnimationEnabled() { 1015 return mScaleAnimationEnabled; 1016 } 1017 1018 @Override 1019 public boolean isEnterAnimationFinished() { 1020 return mEnterAnimationFinished; 1021 } 1022 1023 @Override 1024 public View getRootView() { 1025 return mRootView; 1026 } 1027 1028 @Override 1029 public void setNotFullscreenCallbackDoNotUseThisFunction() { 1030 setFullScreen(false /* fullscreen */, true /* setDelayedRunnable */); 1031 } 1032 1033 // END PhotoViewControllerCallbacks 1034 1035 private class BitmapCallback implements LoaderManager.LoaderCallbacks<BitmapResult> { 1036 1037 @Override 1038 public Loader<BitmapResult> onCreateLoader(int id, Bundle args) { 1039 String uri = args.getString(ARG_IMAGE_URI); 1040 switch (id) { 1041 case PhotoViewCallbacks.BITMAP_LOADER_THUMBNAIL: 1042 return onCreateBitmapLoader(PhotoViewCallbacks.BITMAP_LOADER_THUMBNAIL, 1043 args, uri); 1044 case PhotoViewCallbacks.BITMAP_LOADER_AVATAR: 1045 return onCreateBitmapLoader(PhotoViewCallbacks.BITMAP_LOADER_AVATAR, 1046 args, uri); 1047 } 1048 return null; 1049 } 1050 1051 @Override 1052 public void onLoadFinished(Loader<BitmapResult> loader, BitmapResult result) { 1053 Drawable drawable = result.getDrawable(getResources()); 1054 final ActionBar actionBar = getSupportActionBar(); 1055 switch (loader.getId()) { 1056 case PhotoViewCallbacks.BITMAP_LOADER_THUMBNAIL: 1057 // We just loaded the initial thumbnail that we can display 1058 // while waiting for the full viewPager to get initialized. 1059 initTemporaryImage(drawable); 1060 // Destroy the loader so we don't attempt to load the thumbnail 1061 // again on screen rotations. 1062 getSupportLoaderManager().destroyLoader( 1063 PhotoViewCallbacks.BITMAP_LOADER_THUMBNAIL); 1064 break; 1065 case PhotoViewCallbacks.BITMAP_LOADER_AVATAR: 1066 if (drawable == null) { 1067 actionBar.setLogo(null); 1068 } else { 1069 actionBar.setLogo(drawable); 1070 } 1071 break; 1072 } 1073 } 1074 1075 @Override 1076 public void onLoaderReset(Loader<BitmapResult> loader) { 1077 // Do nothing 1078 } 1079 } 1080} 1081