PhotoViewActivity.java revision 1b641422773c48c8fe4f3b6bc6570f7e098124f2
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        mRootView.setOnSystemUiVisibilityChangeListener(mController);
251        mBackground = findViewById(R.id.photo_activity_background);
252        mTemporaryImage = (ImageView) findViewById(R.id.photo_activity_temporary_image);
253        mViewPager = (PhotoViewPager) findViewById(R.id.photo_view_pager);
254        mViewPager.setAdapter(mAdapter);
255        mViewPager.setOnPageChangeListener(this);
256        mViewPager.setOnInterceptTouchListener(this);
257        mViewPager.setPageMargin(resources.getDimensionPixelSize(R.dimen.photo_page_margin));
258
259        mBitmapCallback = new BitmapCallback();
260        if (!mScaleAnimationEnabled || mEnterAnimationFinished) {
261            // We are not running the scale up animation. Just let the fragments
262            // display and handle the animation.
263            getSupportLoaderManager().initLoader(LOADER_PHOTO_LIST, null, this);
264            // Make the background opaque immediately so that we don't see the activity
265            // behind this one.
266            mBackground.setVisibility(View.VISIBLE);
267        } else {
268            // Attempt to load the initial image thumbnail. Once we have the
269            // image, animate it up. Once the animation is complete, we can kick off
270            // loading the ViewPager. After the primary fullres image is loaded, we will
271            // make our temporary image invisible and display the ViewPager.
272            mViewPager.setVisibility(View.GONE);
273            Bundle args = new Bundle();
274            args.putString(ARG_IMAGE_URI, mCurrentPhotoUri);
275            getSupportLoaderManager().initLoader(BITMAP_LOADER_THUMBNAIL, args, mBitmapCallback);
276        }
277
278        mEnterFullScreenDelayTime =
279                resources.getInteger(R.integer.reenter_fullscreen_delay_time_in_millis);
280
281        final ActionBar actionBar = getSupportActionBar();
282        if (actionBar != null) {
283            actionBar.setDisplayHomeAsUpEnabled(true);
284            actionBar.addOnMenuVisibilityListener(this);
285            final int showTitle = ActionBar.DISPLAY_SHOW_TITLE;
286            actionBar.setDisplayOptions(showTitle, showTitle);
287            // Set the title and subtitle immediately here, rather than waiting
288            // for the fragment to be initialized.
289            setActionBarTitles(actionBar);
290        }
291
292        if (!mScaleAnimationEnabled) {
293            setLightsOutMode(mFullScreen);
294        } else {
295            // Keep lights out mode as false. This is to prevent jank cause by concurrent
296            // animations during the enter animation.
297            setLightsOutMode(false);
298        }
299    }
300
301    @Override
302    protected void onResume() {
303        super.onResume();
304        setFullScreen(mFullScreen, false);
305
306        mIsPaused = false;
307        if (mRestartLoader) {
308            mRestartLoader = false;
309            getSupportLoaderManager().restartLoader(LOADER_PHOTO_LIST, null, this);
310        }
311    }
312
313    @Override
314    protected void onPause() {
315        mIsPaused = true;
316        super.onPause();
317    }
318
319    @Override
320    public void onBackPressed() {
321        // If we are in fullscreen mode, and the default is not full screen, then
322        // switch back to actionBar display mode.
323        if (mFullScreen && !mActionBarHiddenInitially) {
324            toggleFullScreen();
325        } else {
326            if (mScaleAnimationEnabled) {
327                runExitAnimation();
328            } else {
329                super.onBackPressed();
330            }
331        }
332    }
333
334    @Override
335    public void onSaveInstanceState(Bundle outState) {
336        super.onSaveInstanceState(outState);
337
338        outState.putString(STATE_CURRENT_URI_KEY, mCurrentPhotoUri);
339        outState.putInt(STATE_CURRENT_INDEX_KEY, mCurrentPhotoIndex);
340        outState.putBoolean(STATE_FULLSCREEN_KEY, mFullScreen);
341        outState.putString(STATE_ACTIONBARTITLE_KEY, mActionBarTitle);
342        outState.putString(STATE_ACTIONBARSUBTITLE_KEY, mActionBarSubtitle);
343        outState.putBoolean(STATE_ENTERANIMATIONFINISHED_KEY, mEnterAnimationFinished);
344    }
345
346    @Override
347    public boolean onOptionsItemSelected(MenuItem item) {
348       switch (item.getItemId()) {
349          case android.R.id.home:
350             finish();
351             return true;
352          default:
353             return super.onOptionsItemSelected(item);
354       }
355    }
356
357    @Override
358    public void addScreenListener(int position, OnScreenListener listener) {
359        mScreenListeners.put(position, listener);
360    }
361
362    @Override
363    public void removeScreenListener(int position) {
364        mScreenListeners.remove(position);
365    }
366
367    @Override
368    public synchronized void addCursorListener(CursorChangedListener listener) {
369        mCursorListeners.add(listener);
370    }
371
372    @Override
373    public synchronized void removeCursorListener(CursorChangedListener listener) {
374        mCursorListeners.remove(listener);
375    }
376
377    @Override
378    public boolean isFragmentFullScreen(Fragment fragment) {
379        if (mViewPager == null || mAdapter == null || mAdapter.getCount() == 0) {
380            return mFullScreen;
381        }
382        return mFullScreen || (mViewPager.getCurrentItem() != mAdapter.getItemPosition(fragment));
383    }
384
385    @Override
386    public void toggleFullScreen() {
387        setFullScreen(!mFullScreen, true);
388    }
389
390    public void onPhotoRemoved(long photoId) {
391        final Cursor data = mAdapter.getCursor();
392        if (data == null) {
393            // Huh?! How would this happen?
394            return;
395        }
396
397        final int dataCount = data.getCount();
398        if (dataCount <= 1) {
399            finish();
400            return;
401        }
402
403        getSupportLoaderManager().restartLoader(LOADER_PHOTO_LIST, null, this);
404    }
405
406    @Override
407    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
408        if (id == LOADER_PHOTO_LIST) {
409            return new PhotoPagerLoader(this, Uri.parse(mPhotosUri), mProjection);
410        }
411        return null;
412    }
413
414    @Override
415    public Loader<BitmapResult> onCreateBitmapLoader(int id, Bundle args, String uri) {
416        switch (id) {
417            case BITMAP_LOADER_AVATAR:
418            case BITMAP_LOADER_THUMBNAIL:
419            case BITMAP_LOADER_PHOTO:
420                return new PhotoBitmapLoader(this, uri);
421            default:
422                return null;
423        }
424    }
425
426    @Override
427    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
428
429        final int id = loader.getId();
430        if (id == LOADER_PHOTO_LIST) {
431            if (data == null || data.getCount() == 0) {
432                mIsEmpty = true;
433            } else {
434                mAlbumCount = data.getCount();
435                if (mCurrentPhotoUri != null) {
436                    int index = 0;
437                    // Clear query params. Compare only the path.
438                    final int uriIndex = data.getColumnIndex(PhotoContract.PhotoViewColumns.URI);
439                    final Uri currentPhotoUri;
440                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
441                        currentPhotoUri = Uri.parse(mCurrentPhotoUri).buildUpon()
442                            .clearQuery().build();
443                    } else {
444                        currentPhotoUri = Uri.parse(mCurrentPhotoUri).buildUpon()
445                            .query(null).build();
446                    }
447                    while (data.moveToNext()) {
448                        final String uriString = data.getString(uriIndex);
449                        final Uri uri;
450                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
451                            uri = Uri.parse(uriString).buildUpon().clearQuery().build();
452                        } else {
453                            uri = Uri.parse(uriString).buildUpon().query(null).build();
454                        }
455                        if (currentPhotoUri != null && currentPhotoUri.equals(uri)) {
456                            mCurrentPhotoIndex = index;
457                            break;
458                        }
459                        index++;
460                    }
461                }
462
463                // We're paused; don't do anything now, we'll get re-invoked
464                // when the activity becomes active again
465                // TODO(pwestbro): This shouldn't be necessary, as the loader manager should
466                // restart the loader
467                if (mIsPaused) {
468                    mRestartLoader = true;
469                    return;
470                }
471                boolean wasEmpty = mIsEmpty;
472                mIsEmpty = false;
473
474                mAdapter.swapCursor(data);
475                if (mViewPager.getAdapter() == null) {
476                    mViewPager.setAdapter(mAdapter);
477                }
478                notifyCursorListeners(data);
479
480                // Use an index of 0 if the index wasn't specified or couldn't be found
481                if (mCurrentPhotoIndex < 0) {
482                    mCurrentPhotoIndex = 0;
483                }
484
485                mViewPager.setCurrentItem(mCurrentPhotoIndex, false);
486                if (wasEmpty) {
487                    setViewActivated(mCurrentPhotoIndex);
488                }
489            }
490            // Update the any action items
491            updateActionItems();
492        }
493    }
494
495    @Override
496    public void onLoaderReset(android.support.v4.content.Loader<Cursor> loader) {
497        // If the loader is reset, remove the reference in the adapter to this cursor
498        // TODO(pwestbro): reenable this when b/7075236 is fixed
499        // mAdapter.swapCursor(null);
500    }
501
502    protected void updateActionItems() {
503        // Do nothing, but allow extending classes to do work
504    }
505
506    private synchronized void notifyCursorListeners(Cursor data) {
507        // tell all of the objects listening for cursor changes
508        // that the cursor has changed
509        for (CursorChangedListener listener : mCursorListeners) {
510            listener.onCursorChanged(data);
511        }
512    }
513
514    @Override
515    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
516    }
517
518    @Override
519    public void onPageSelected(int position) {
520        mCurrentPhotoIndex = position;
521        setViewActivated(position);
522    }
523
524    @Override
525    public void onPageScrollStateChanged(int state) {
526    }
527
528    @Override
529    public boolean isFragmentActive(Fragment fragment) {
530        if (mViewPager == null || mAdapter == null) {
531            return false;
532        }
533        return mViewPager.getCurrentItem() == mAdapter.getItemPosition(fragment);
534    }
535
536    @Override
537    public void onFragmentVisible(PhotoViewFragment fragment) {
538        // Do nothing, we handle this in setViewActivated
539    }
540
541    @Override
542    public InterceptType onTouchIntercept(float origX, float origY) {
543        boolean interceptLeft = false;
544        boolean interceptRight = false;
545
546        for (OnScreenListener listener : mScreenListeners.values()) {
547            if (!interceptLeft) {
548                interceptLeft = listener.onInterceptMoveLeft(origX, origY);
549            }
550            if (!interceptRight) {
551                interceptRight = listener.onInterceptMoveRight(origX, origY);
552            }
553        }
554
555        if (interceptLeft) {
556            if (interceptRight) {
557                return InterceptType.BOTH;
558            }
559            return InterceptType.LEFT;
560        } else if (interceptRight) {
561            return InterceptType.RIGHT;
562        }
563        return InterceptType.NONE;
564    }
565
566    /**
567     * Updates the title bar according to the value of {@link #mFullScreen}.
568     */
569    protected void setFullScreen(boolean fullScreen, boolean setDelayedRunnable) {
570        final boolean fullScreenChanged = (fullScreen != mFullScreen);
571        mFullScreen = fullScreen;
572
573        if (mFullScreen) {
574            setLightsOutMode(true);
575            cancelEnterFullScreenRunnable();
576        } else {
577            setLightsOutMode(false);
578            if (setDelayedRunnable) {
579                postEnterFullScreenRunnableWithDelay();
580            }
581        }
582
583        if (fullScreenChanged) {
584            for (OnScreenListener listener : mScreenListeners.values()) {
585                listener.onFullScreenChanged(mFullScreen);
586            }
587        }
588    }
589
590    private void postEnterFullScreenRunnableWithDelay() {
591        mHandler.postDelayed(mEnterFullScreenRunnable, mEnterFullScreenDelayTime);
592    }
593
594    private void cancelEnterFullScreenRunnable() {
595        mHandler.removeCallbacks(mEnterFullScreenRunnable);
596    }
597
598    protected void setLightsOutMode(boolean enabled) {
599        mController.setImmersiveMode(enabled);
600    }
601
602    private final Runnable mEnterFullScreenRunnable = new Runnable() {
603        @Override
604        public void run() {
605            setFullScreen(true, true);
606        }
607    };
608
609    @Override
610    public void setViewActivated(int position) {
611        OnScreenListener listener = mScreenListeners.get(position);
612        if (listener != null) {
613            listener.onViewActivated();
614        }
615        final Cursor cursor = getCursorAtProperPosition();
616        mCurrentPhotoIndex = position;
617        // FLAG: get the column indexes once in onLoadFinished().
618        // That would make this more efficient, instead of looking these up
619        // repeatedly whenever we want them.
620        int uriIndex = cursor.getColumnIndex(PhotoContract.PhotoViewColumns.URI);
621        mCurrentPhotoUri = cursor.getString(uriIndex);
622        updateActionBar();
623
624        // Restart the timer to return to fullscreen.
625        cancelEnterFullScreenRunnable();
626        postEnterFullScreenRunnableWithDelay();
627    }
628
629    /**
630     * Adjusts the activity title and subtitle to reflect the photo name and count.
631     */
632    protected void updateActionBar() {
633        final int position = mViewPager.getCurrentItem() + 1;
634        final boolean hasAlbumCount = mAlbumCount >= 0;
635
636        final Cursor cursor = getCursorAtProperPosition();
637        if (cursor != null) {
638            // FLAG: We should grab the indexes when we first get the cursor
639            // and store them so we don't need to do it each time.
640            final int photoNameIndex = cursor.getColumnIndex(PhotoContract.PhotoViewColumns.NAME);
641            mActionBarTitle = cursor.getString(photoNameIndex);
642        } else {
643            mActionBarTitle = null;
644        }
645
646        if (mIsEmpty || !hasAlbumCount || position <= 0) {
647            mActionBarSubtitle = null;
648        } else {
649            mActionBarSubtitle =
650                    getResources().getString(R.string.photo_view_count, position, mAlbumCount);
651        }
652
653        setActionBarTitles(getSupportActionBar());
654    }
655
656    /**
657     * Sets the Action Bar title to {@link #mActionBarTitle} and the subtitle to
658     * {@link #mActionBarSubtitle}
659     */
660    protected final void setActionBarTitles(ActionBar actionBar) {
661        if (actionBar == null) {
662            return;
663        }
664        actionBar.setTitle(getInputOrEmpty(mActionBarTitle));
665        actionBar.setSubtitle(getInputOrEmpty(mActionBarSubtitle));
666    }
667
668    /**
669     * If the input string is non-null, it is returned, otherwise an empty string is returned;
670     * @param in
671     * @return
672     */
673    private static final String getInputOrEmpty(String in) {
674        if (in == null) {
675            return "";
676        }
677        return in;
678    }
679
680    /**
681     * Utility method that will return the cursor that contains the data
682     * at the current position so that it refers to the current image on screen.
683     * @return the cursor at the current position or
684     * null if no cursor exists or if the {@link PhotoViewPager} is null.
685     */
686    public Cursor getCursorAtProperPosition() {
687        if (mViewPager == null) {
688            return null;
689        }
690
691        final int position = mViewPager.getCurrentItem();
692        final Cursor cursor = mAdapter.getCursor();
693
694        if (cursor == null) {
695            return null;
696        }
697
698        cursor.moveToPosition(position);
699
700        return cursor;
701    }
702
703    public Cursor getCursor() {
704        return (mAdapter == null) ? null : mAdapter.getCursor();
705    }
706
707    @Override
708    public void onMenuVisibilityChanged(boolean isVisible) {
709        if (isVisible) {
710            cancelEnterFullScreenRunnable();
711        } else {
712            postEnterFullScreenRunnableWithDelay();
713        }
714    }
715
716    @Override
717    public void onNewPhotoLoaded(int position) {
718        // do nothing
719    }
720
721    protected void setPhotoIndex(int index) {
722        mCurrentPhotoIndex = index;
723    }
724
725    @Override
726    public void onFragmentPhotoLoadComplete(PhotoViewFragment fragment, boolean success) {
727        if (mTemporaryImage.getVisibility() != View.GONE &&
728                TextUtils.equals(fragment.getPhotoUri(), mCurrentPhotoUri)) {
729            if (success) {
730                // The fragment for the current image is now ready for display.
731                mTemporaryImage.setVisibility(View.GONE);
732                mViewPager.setVisibility(View.VISIBLE);
733            } else {
734                // This means that we are unable to load the fragment's photo.
735                // I'm not sure what the best thing to do here is, but at least if
736                // we display the viewPager, the fragment itself can decide how to
737                // display the failure of its own image.
738                Log.w(TAG, "Failed to load fragment image");
739                mTemporaryImage.setVisibility(View.GONE);
740                mViewPager.setVisibility(View.VISIBLE);
741            }
742        }
743    }
744
745    protected boolean isFullScreen() {
746        return mFullScreen;
747    }
748
749    @Override
750    public void onCursorChanged(PhotoViewFragment fragment, Cursor cursor) {
751        // do nothing
752    }
753
754    @Override
755    public PhotoPagerAdapter getAdapter() {
756        return mAdapter;
757    }
758
759    public void onEnterAnimationComplete() {
760        mEnterAnimationFinished = true;
761        mViewPager.setVisibility(View.VISIBLE);
762        setLightsOutMode(mFullScreen);
763    }
764
765    private void onExitAnimationComplete() {
766        finish();
767        overridePendingTransition(0, 0);
768    }
769
770    private void runEnterAnimation() {
771        final int totalWidth = mRootView.getMeasuredWidth();
772        final int totalHeight = mRootView.getMeasuredHeight();
773
774        // FLAG: Need to handle the aspect ratio of the bitmap.  If it's a portrait
775        // bitmap, then we need to position the view higher so that the middle
776        // pixels line up.
777        mTemporaryImage.setVisibility(View.VISIBLE);
778        // We need to take a full screen image, and scale/translate it so that
779        // it appears at exactly the same location onscreen as it is in the
780        // prior activity.
781        // The final image will take either the full screen width or height (or both).
782
783        final float scaleW = (float) mAnimationStartWidth / totalWidth;
784        final float scaleY = (float) mAnimationStartHeight / totalHeight;
785        final float scale = Math.max(scaleW, scaleY);
786
787        final int translateX = calculateTranslate(mAnimationStartX, mAnimationStartWidth,
788                totalWidth, scale);
789        final int translateY = calculateTranslate(mAnimationStartY, mAnimationStartHeight,
790                totalHeight, scale);
791
792        final int version = android.os.Build.VERSION.SDK_INT;
793        if (version >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
794            mBackground.setAlpha(0f);
795            mBackground.animate().alpha(1f).setDuration(ENTER_ANIMATION_DURATION_MS).start();
796            mBackground.setVisibility(View.VISIBLE);
797
798            mTemporaryImage.setScaleX(scale);
799            mTemporaryImage.setScaleY(scale);
800            mTemporaryImage.setTranslationX(translateX);
801            mTemporaryImage.setTranslationY(translateY);
802
803            Runnable endRunnable = new Runnable() {
804                @Override
805                public void run() {
806                    PhotoViewActivity.this.onEnterAnimationComplete();
807                }
808            };
809            ViewPropertyAnimator animator = mTemporaryImage.animate().scaleX(1f).scaleY(1f)
810                .translationX(0).translationY(0).setDuration(ENTER_ANIMATION_DURATION_MS);
811            if (version >= Build.VERSION_CODES.JELLY_BEAN) {
812                animator.withEndAction(endRunnable);
813            } else {
814                mHandler.postDelayed(endRunnable, ENTER_ANIMATION_DURATION_MS);
815            }
816            animator.start();
817        } else {
818            final Animation alphaAnimation = new AlphaAnimation(0f, 1f);
819            alphaAnimation.setDuration(ENTER_ANIMATION_DURATION_MS);
820            mBackground.startAnimation(alphaAnimation);
821            mBackground.setVisibility(View.VISIBLE);
822
823            final Animation translateAnimation = new TranslateAnimation(translateX,
824                    translateY, 0, 0);
825            translateAnimation.setDuration(ENTER_ANIMATION_DURATION_MS);
826            Animation scaleAnimation = new ScaleAnimation(scale, scale, 0, 0);
827            scaleAnimation.setDuration(ENTER_ANIMATION_DURATION_MS);
828
829            AnimationSet animationSet = new AnimationSet(true);
830            animationSet.addAnimation(translateAnimation);
831            animationSet.addAnimation(scaleAnimation);
832            AnimationListener listener = new AnimationListener() {
833                @Override
834                public void onAnimationEnd(Animation arg0) {
835                    PhotoViewActivity.this.onEnterAnimationComplete();
836                }
837
838                @Override
839                public void onAnimationRepeat(Animation arg0) {
840                }
841
842                @Override
843                public void onAnimationStart(Animation arg0) {
844                }
845            };
846            animationSet.setAnimationListener(listener);
847            mTemporaryImage.startAnimation(animationSet);
848        }
849    }
850
851    private void runExitAnimation() {
852        Intent intent = getIntent();
853        // FLAG: should just fall back to a standard animation if either:
854        // 1. images have been added or removed since we've been here, or
855        // 2. we are currently looking at some image other than the one we
856        // started on.
857
858        final int totalWidth = mRootView.getMeasuredWidth();
859        final int totalHeight = mRootView.getMeasuredHeight();
860
861        // We need to take a full screen image, and scale/translate it so that
862        // it appears at exactly the same location onscreen as it is in the
863        // prior activity.
864        // The final image will take either the full screen width or height (or both).
865        final float scaleW = (float) mAnimationStartWidth / totalWidth;
866        final float scaleY = (float) mAnimationStartHeight / totalHeight;
867        final float scale = Math.max(scaleW, scaleY);
868
869        final int translateX = calculateTranslate(mAnimationStartX, mAnimationStartWidth,
870                totalWidth, scale);
871        final int translateY = calculateTranslate(mAnimationStartY, mAnimationStartHeight,
872                totalHeight, scale);
873        final int version = android.os.Build.VERSION.SDK_INT;
874        if (version >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
875            mBackground.animate().alpha(0f).setDuration(EXIT_ANIMATION_DURATION_MS).start();
876            mBackground.setVisibility(View.VISIBLE);
877
878            Runnable endRunnable = new Runnable() {
879                @Override
880                public void run() {
881                    PhotoViewActivity.this.onExitAnimationComplete();
882                }
883            };
884            // If the temporary image is still visible it means that we have
885            // not yet loaded the fullres image, so we need to animate
886            // the temporary image out.
887            ViewPropertyAnimator animator = null;
888            if (mTemporaryImage.getVisibility() == View.VISIBLE) {
889                animator = mTemporaryImage.animate().scaleX(scale).scaleY(scale)
890                    .translationX(translateX).translationY(translateY)
891                    .setDuration(EXIT_ANIMATION_DURATION_MS);
892            } else {
893                animator = mViewPager.animate().scaleX(scale).scaleY(scale)
894                    .translationX(translateX).translationY(translateY)
895                    .setDuration(EXIT_ANIMATION_DURATION_MS);
896            }
897            if (version >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
898                animator.withEndAction(endRunnable);
899            } else {
900                mHandler.postDelayed(endRunnable, EXIT_ANIMATION_DURATION_MS);
901            }
902            animator.start();
903        } else {
904            final Animation alphaAnimation = new AlphaAnimation(1f, 0f);
905            alphaAnimation.setDuration(EXIT_ANIMATION_DURATION_MS);
906            mBackground.startAnimation(alphaAnimation);
907            mBackground.setVisibility(View.VISIBLE);
908
909            final Animation scaleAnimation = new ScaleAnimation(1f, 1f, scale, scale);
910            scaleAnimation.setDuration(EXIT_ANIMATION_DURATION_MS);
911            AnimationListener listener = new AnimationListener() {
912                @Override
913                public void onAnimationEnd(Animation arg0) {
914                    PhotoViewActivity.this.onExitAnimationComplete();
915                }
916
917                @Override
918                public void onAnimationRepeat(Animation arg0) {
919                }
920
921                @Override
922                public void onAnimationStart(Animation arg0) {
923                }
924            };
925            scaleAnimation.setAnimationListener(listener);
926            // If the temporary image is still visible it means that we have
927            // not yet loaded the fullres image, so we need to animate
928            // the temporary image out.
929            if (mTemporaryImage.getVisibility() == View.VISIBLE) {
930                mTemporaryImage.startAnimation(scaleAnimation);
931            } else {
932                mViewPager.startAnimation(scaleAnimation);
933            }
934        }
935    }
936
937    private int calculateTranslate(int start, int startSize, int totalSize, float scale) {
938        // Translation takes precedence over scale.  What this means is that if
939        // we want an view's upper left corner to be a particular spot on screen,
940        // but that view is scaled to something other than 1, we need to take into
941        // account the pixels lost to scaling.
942        // So if we have a view that is 200x300, and we want it's upper left corner
943        // to be at 50x50, but it's scaled by 50%, we can't just translate it to 50x50.
944        // If we were to do that, the view's *visible* upper left corner would be at
945        // 100x200.  We need to take into account the difference between the outside
946        // size of the view (i.e. the size prior to scaling) and the scaled size.
947        // scaleFromEdge is the difference between the visible left edge and the
948        // actual left edge, due to scaling.
949        // scaleFromTop is the difference between the visible top edge, and the
950        // actual top edge, due to scaling.
951        int scaleFromEdge = Math.round((totalSize - totalSize * scale) / 2);
952
953        // The imageView is fullscreen, regardless of the aspect ratio of the actual image.
954        // This means that some portion of the imageView will be blank.  We need to
955        // take into account the size of the blank area so that the actual image
956        // lines up with the starting image.
957        int blankSize = Math.round((totalSize * scale - startSize) / 2);
958
959        return start - scaleFromEdge - blankSize;
960    }
961
962    private void initTemporaryImage(Drawable drawable) {
963        if (mEnterAnimationFinished) {
964            // Forget this, we've already run the animation.
965            return;
966        }
967        mTemporaryImage.setImageDrawable(drawable);
968        if (drawable != null) {
969            // We have not yet run the enter animation. Start it now.
970            int totalWidth = mRootView.getMeasuredWidth();
971            if (totalWidth == 0) {
972                // the measure pass has not yet finished.  We can't properly
973                // run out animation until that is done. Listen for the layout
974                // to occur, then fire the animation.
975                final View base = mRootView;
976                base.getViewTreeObserver().addOnGlobalLayoutListener(
977                        new OnGlobalLayoutListener() {
978                    @Override
979                    public void onGlobalLayout() {
980                        int version = android.os.Build.VERSION.SDK_INT;
981                        if (version >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
982                            base.getViewTreeObserver().removeOnGlobalLayoutListener(this);
983                        } else {
984                            base.getViewTreeObserver().removeGlobalOnLayoutListener(this);
985                        }
986                        runEnterAnimation();
987                    }
988                });
989            } else {
990                // initiate the animation
991                runEnterAnimation();
992            }
993        }
994        // Kick off the photo list loader
995        getSupportLoaderManager().initLoader(LOADER_PHOTO_LIST, null, this);
996    }
997
998    // START PhotoViewControllerCallbacks
999
1000    @Override
1001    public void showActionBar() {
1002        getActionBar().show();
1003    }
1004
1005    @Override
1006    public void hideActionBar() {
1007        getActionBar().hide();
1008    }
1009
1010    @Override
1011    public boolean isScaleAnimationEnabled() {
1012        return mScaleAnimationEnabled;
1013    }
1014
1015    @Override
1016    public boolean isEnterAnimationFinished() {
1017        return mEnterAnimationFinished;
1018    }
1019
1020    @Override
1021    public View getRootView() {
1022        return mRootView;
1023    }
1024
1025    @Override
1026    public void setNotFullscreenCallbackDoNotUseThisFunction() {
1027        setFullScreen(false /* fullscreen */, true /* setDelayedRunnable */);
1028    }
1029
1030    // END PhotoViewControllerCallbacks
1031
1032    private class BitmapCallback implements LoaderManager.LoaderCallbacks<BitmapResult> {
1033
1034        @Override
1035        public Loader<BitmapResult> onCreateLoader(int id, Bundle args) {
1036            String uri = args.getString(ARG_IMAGE_URI);
1037            switch (id) {
1038                case PhotoViewCallbacks.BITMAP_LOADER_THUMBNAIL:
1039                    return onCreateBitmapLoader(PhotoViewCallbacks.BITMAP_LOADER_THUMBNAIL,
1040                            args, uri);
1041                case PhotoViewCallbacks.BITMAP_LOADER_AVATAR:
1042                    return onCreateBitmapLoader(PhotoViewCallbacks.BITMAP_LOADER_AVATAR,
1043                            args, uri);
1044            }
1045            return null;
1046        }
1047
1048        @Override
1049        public void onLoadFinished(Loader<BitmapResult> loader, BitmapResult result) {
1050            Drawable drawable = result.getDrawable(getResources());
1051            final ActionBar actionBar = getSupportActionBar();
1052            switch (loader.getId()) {
1053                case PhotoViewCallbacks.BITMAP_LOADER_THUMBNAIL:
1054                    // We just loaded the initial thumbnail that we can display
1055                    // while waiting for the full viewPager to get initialized.
1056                    initTemporaryImage(drawable);
1057                    // Destroy the loader so we don't attempt to load the thumbnail
1058                    // again on screen rotations.
1059                    getSupportLoaderManager().destroyLoader(
1060                            PhotoViewCallbacks.BITMAP_LOADER_THUMBNAIL);
1061                    break;
1062                case PhotoViewCallbacks.BITMAP_LOADER_AVATAR:
1063                    if (drawable == null) {
1064                        actionBar.setLogo(null);
1065                    } else {
1066                        actionBar.setLogo(drawable);
1067                    }
1068                    break;
1069            }
1070        }
1071
1072        @Override
1073        public void onLoaderReset(Loader<BitmapResult> loader) {
1074            // Do nothing
1075        }
1076    }
1077}
1078