Home | History | Annotate | Download | only in photo
      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 
     18 package com.android.ex.photo;
     19 
     20 import android.app.Activity;
     21 import android.content.ContentProvider;
     22 import android.content.Context;
     23 import android.content.Intent;
     24 
     25 import com.android.ex.photo.fragments.PhotoViewFragment;
     26 
     27 /**
     28  * Build intents to start app activities
     29  */
     30 
     31 public class Intents {
     32     // Intent extras
     33     public static final String EXTRA_PHOTO_INDEX = "photo_index";
     34     public static final String EXTRA_INITIAL_PHOTO_URI = "initial_photo_uri";
     35     public static final String EXTRA_PHOTOS_URI = "photos_uri";
     36     public static final String EXTRA_RESOLVED_PHOTO_URI = "resolved_photo_uri";
     37     public static final String EXTRA_PROJECTION = "projection";
     38     public static final String EXTRA_THUMBNAIL_URI = "thumbnail_uri";
     39     public static final String EXTRA_MAX_INITIAL_SCALE = "max_scale";
     40     public static final String EXTRA_WATCH_NETWORK = "watch_network";
     41 
     42 
     43     // Parameters affecting the intro/exit animation
     44     public static final String EXTRA_SCALE_UP_ANIMATION = "scale_up_animation";
     45     public static final String EXTRA_ANIMATION_START_X = "start_x_extra";
     46     public static final String EXTRA_ANIMATION_START_Y = "start_y_extra";
     47     public static final String EXTRA_ANIMATION_START_WIDTH = "start_width_extra";
     48     public static final String EXTRA_ANIMATION_START_HEIGHT = "start_height_extra";
     49 
     50     // Parameters affecting the display and features
     51     public static final String EXTRA_ACTION_BAR_HIDDEN_INITIALLY = "action_bar_hidden_initially";
     52     public static final String EXTRA_DISPLAY_THUMBS_FULLSCREEN = "display_thumbs_fullscreen";
     53 
     54     /**
     55      * Gets a photo view intent builder to display the photos from phone activity.
     56      *
     57      * @param context The context
     58      * @return The intent builder
     59      */
     60     public static PhotoViewIntentBuilder newPhotoViewActivityIntentBuilder(Context context) {
     61         return new PhotoViewIntentBuilder(context, PhotoViewActivity.class);
     62     }
     63 
     64     /**
     65      * Gets a photo view intent builder to display the photo view fragment
     66      *
     67      * @param context The context
     68      * @return The intent builder
     69      */
     70     public static PhotoViewIntentBuilder newPhotoViewFragmentIntentBuilder(Context context) {
     71         return new PhotoViewIntentBuilder(context, PhotoViewFragment.class);
     72     }
     73 
     74     /** Gets a new photo view intent builder */
     75     public static PhotoViewIntentBuilder newPhotoViewIntentBuilder(
     76             Context context, Class<? extends Activity> cls) {
     77         return new PhotoViewIntentBuilder(context, cls);
     78     }
     79 
     80     /** Builder to create a photo view intent */
     81     public static class PhotoViewIntentBuilder {
     82         private final Intent mIntent;
     83 
     84         /** The index of the photo to show */
     85         private Integer mPhotoIndex;
     86         /** The URI of the initial photo to show */
     87         private String mInitialPhotoUri;
     88         /** The URI of the initial thumbnail to show */
     89         private String mInitialThumbnailUri;
     90         /** The URI of the group of photos to display */
     91         private String mPhotosUri;
     92         /** The URL of the photo to display */
     93         private String mResolvedPhotoUri;
     94         /** The projection for the query to use; optional */
     95         private String[] mProjection;
     96         /** The URI of a thumbnail of the photo to display */
     97         private String mThumbnailUri;
     98         /** The maximum scale to display images at before  */
     99         private Float mMaxInitialScale;
    100         /**
    101          * True if the PhotoViewFragments should watch for network changes to restart their loaders
    102          */
    103         private boolean mWatchNetwork;
    104         /** true we want to run the image scale animation */
    105         private boolean mScaleAnimation;
    106         /** The parameters for performing the scale up/scale down animations
    107          * upon enter and exit. StartX and StartY represent the screen coordinates
    108          * of the upper left corner of the start rectangle, startWidth and startHeight
    109          * represent the width and height of the start rectangle.
    110          */
    111         private int mStartX;
    112         private int mStartY;
    113         private int mStartWidth;
    114         private int mStartHeight;
    115 
    116         private boolean mActionBarHiddenInitially;
    117         private boolean mDisplayFullScreenThumbs;
    118 
    119         private PhotoViewIntentBuilder(Context context, Class<?> cls) {
    120             mIntent = new Intent(context, cls);
    121             mScaleAnimation = false;
    122             mActionBarHiddenInitially = false;
    123             mDisplayFullScreenThumbs = false;
    124         }
    125 
    126         /** Sets the photo index */
    127         public PhotoViewIntentBuilder setPhotoIndex(Integer photoIndex) {
    128             mPhotoIndex = photoIndex;
    129             return this;
    130         }
    131 
    132         /** Sets the initial photo URI */
    133         public PhotoViewIntentBuilder setInitialPhotoUri(String initialPhotoUri) {
    134             mInitialPhotoUri = initialPhotoUri;
    135             return this;
    136         }
    137 
    138         /** Sets the photos URI */
    139         public PhotoViewIntentBuilder setPhotosUri(String photosUri) {
    140             mPhotosUri = photosUri;
    141             return this;
    142         }
    143 
    144         /** Sets the query projection */
    145         public PhotoViewIntentBuilder setProjection(String[] projection) {
    146             mProjection = projection;
    147             return this;
    148         }
    149 
    150         /** Sets the resolved photo URI. This method is for the case
    151          *  where the URI given to {@link PhotoViewActivity} points directly
    152          *  to a single image and does not need to be resolved via a query
    153          *  to the {@link ContentProvider}. If this value is set, it supersedes
    154          *  {@link #setPhotosUri(String)}. */
    155         public PhotoViewIntentBuilder setResolvedPhotoUri(String resolvedPhotoUri) {
    156             mResolvedPhotoUri = resolvedPhotoUri;
    157             return this;
    158         }
    159 
    160         /**
    161          * Sets the URI for a thumbnail preview of the photo.
    162          */
    163         public PhotoViewIntentBuilder setThumbnailUri(String thumbnailUri) {
    164             mThumbnailUri = thumbnailUri;
    165             return this;
    166         }
    167 
    168         /**
    169          * Sets the maximum scale which an image is initially displayed at
    170          */
    171         public PhotoViewIntentBuilder setMaxInitialScale(float maxScale) {
    172             mMaxInitialScale = maxScale;
    173             return this;
    174         }
    175 
    176         /**
    177          * Enable watching the network for connectivity changes.
    178          *
    179          * When a change is detected, bitmap loaders will be restarted if required.
    180          */
    181         public PhotoViewIntentBuilder watchNetworkConnectivityChanges() {
    182             mWatchNetwork = true;
    183             return this;
    184         }
    185 
    186         public PhotoViewIntentBuilder setScaleAnimation(int startX, int startY,
    187                 int startWidth, int startHeight) {
    188             mScaleAnimation = true;
    189             mStartX = startX;
    190             mStartY = startY;
    191             mStartWidth = startWidth;
    192             mStartHeight = startHeight;
    193             return this;
    194         }
    195 
    196         // If this option is turned on, then the photoViewer will be initially
    197         // displayed with the action bar hidden. This is as opposed to the default
    198         // behavior, where the actionBar is initially shown.
    199         public PhotoViewIntentBuilder setActionBarHiddenInitially(
    200                 boolean actionBarHiddenInitially) {
    201             mActionBarHiddenInitially = actionBarHiddenInitially;
    202             return this;
    203         }
    204 
    205         // If this option is turned on, then the small, lo-res thumbnail will
    206         // be scaled up to the maximum size to cover as much of the screen as
    207         // possible while still maintaining the correct aspect ratio. This means
    208         // that the image may appear blurry until the the full-res image is
    209         // loaded.
    210         // This is as opposed to the default behavior, where only part of the
    211         // thumbnail is displayed in a small view in the center of the screen,
    212         // and a loading spinner is displayed until the full-res image is loaded.
    213         public PhotoViewIntentBuilder setDisplayThumbsFullScreen(
    214                 boolean displayFullScreenThumbs) {
    215             mDisplayFullScreenThumbs = displayFullScreenThumbs;
    216             return this;
    217         }
    218 
    219         /** Build the intent */
    220         public Intent build() {
    221             mIntent.setAction(Intent.ACTION_VIEW);
    222 
    223             mIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
    224 
    225             if (mPhotoIndex != null) {
    226                 mIntent.putExtra(EXTRA_PHOTO_INDEX, (int) mPhotoIndex);
    227             }
    228 
    229             if (mInitialPhotoUri != null) {
    230                 mIntent.putExtra(EXTRA_INITIAL_PHOTO_URI, mInitialPhotoUri);
    231             }
    232 
    233             if (mInitialPhotoUri != null && mPhotoIndex != null) {
    234                 throw new IllegalStateException(
    235                         "specified both photo index and photo uri");
    236             }
    237 
    238             if (mPhotosUri != null) {
    239                 mIntent.putExtra(EXTRA_PHOTOS_URI, mPhotosUri);
    240             }
    241 
    242             if (mResolvedPhotoUri != null) {
    243                 mIntent.putExtra(EXTRA_RESOLVED_PHOTO_URI, mResolvedPhotoUri);
    244             }
    245 
    246             if (mProjection != null) {
    247                 mIntent.putExtra(EXTRA_PROJECTION, mProjection);
    248             }
    249 
    250             if (mThumbnailUri != null) {
    251                 mIntent.putExtra(EXTRA_THUMBNAIL_URI, mThumbnailUri);
    252             }
    253 
    254             if (mMaxInitialScale != null) {
    255                 mIntent.putExtra(EXTRA_MAX_INITIAL_SCALE, mMaxInitialScale);
    256             }
    257 
    258             if (mWatchNetwork == true) {
    259                 mIntent.putExtra(EXTRA_WATCH_NETWORK, true);
    260             }
    261 
    262             mIntent.putExtra(EXTRA_SCALE_UP_ANIMATION, mScaleAnimation);
    263             if (mScaleAnimation) {
    264                 mIntent.putExtra(EXTRA_ANIMATION_START_X, mStartX);
    265                 mIntent.putExtra(EXTRA_ANIMATION_START_Y, mStartY);
    266                 mIntent.putExtra(EXTRA_ANIMATION_START_WIDTH, mStartWidth);
    267                 mIntent.putExtra(EXTRA_ANIMATION_START_HEIGHT, mStartHeight);
    268             }
    269 
    270             mIntent.putExtra(EXTRA_ACTION_BAR_HIDDEN_INITIALLY, mActionBarHiddenInitially);
    271             mIntent.putExtra(EXTRA_DISPLAY_THUMBS_FULLSCREEN, mDisplayFullScreenThumbs);
    272 
    273             return mIntent;
    274         }
    275     }
    276 }
    277