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 public class Intents {
     31     // Intent extras
     32     public static final String EXTRA_PHOTO_INDEX = "photo_index";
     33     public static final String EXTRA_INITIAL_PHOTO_URI = "initial_photo_uri";
     34     public static final String EXTRA_PHOTOS_URI = "photos_uri";
     35     public static final String EXTRA_RESOLVED_PHOTO_URI = "resolved_photo_uri";
     36     public static final String EXTRA_PROJECTION = "projection";
     37     public static final String EXTRA_THUMBNAIL_URI = "thumbnail_uri";
     38     public static final String EXTRA_MAX_INITIAL_SCALE = "max_scale";
     39 
     40     /**
     41      * Gets a photo view intent builder to display the photos from phone activity.
     42      *
     43      * @param context The context
     44      * @return The intent builder
     45      */
     46     public static PhotoViewIntentBuilder newPhotoViewActivityIntentBuilder(Context context) {
     47         return new PhotoViewIntentBuilder(context, PhotoViewActivity.class);
     48     }
     49 
     50     /**
     51      * Gets a photo view intent builder to display the photo view fragment
     52      *
     53      * @param context The context
     54      * @return The intent builder
     55      */
     56     public static PhotoViewIntentBuilder newPhotoViewFragmentIntentBuilder(Context context) {
     57         return new PhotoViewIntentBuilder(context, PhotoViewFragment.class);
     58     }
     59 
     60     /** Gets a new photo view intent builder */
     61     public static PhotoViewIntentBuilder newPhotoViewIntentBuilder(
     62             Context context, Class<? extends Activity> cls) {
     63         return new PhotoViewIntentBuilder(context, cls);
     64     }
     65 
     66     /** Builder to create a photo view intent */
     67     public static class PhotoViewIntentBuilder {
     68         private final Intent mIntent;
     69 
     70         /** The index of the photo to show */
     71         private Integer mPhotoIndex;
     72         /** The URI of the initial photo to show */
     73         private String mInitialPhotoUri;
     74         /** The URI of the group of photos to display */
     75         private String mPhotosUri;
     76         /** The URL of the photo to display */
     77         private String mResolvedPhotoUri;
     78         /** The projection for the query to use; optional */
     79         private String[] mProjection;
     80         /** The URI of a thumbnail of the photo to display */
     81         private String mThumbnailUri;
     82         /** The maximum scale to display images at before  */
     83         private Float mMaxInitialScale;
     84 
     85         private PhotoViewIntentBuilder(Context context, Class<?> cls) {
     86             mIntent = new Intent(context, cls);
     87         }
     88 
     89         /** Sets the photo index */
     90         public PhotoViewIntentBuilder setPhotoIndex(Integer photoIndex) {
     91             mPhotoIndex = photoIndex;
     92             return this;
     93         }
     94 
     95         /** Sets the initial photo URI */
     96         public PhotoViewIntentBuilder setInitialPhotoUri(String initialPhotoUri) {
     97             mInitialPhotoUri = initialPhotoUri;
     98             return this;
     99         }
    100 
    101         /** Sets the photos URI */
    102         public PhotoViewIntentBuilder setPhotosUri(String photosUri) {
    103             mPhotosUri = photosUri;
    104             return this;
    105         }
    106 
    107         /** Sets the query projection */
    108         public PhotoViewIntentBuilder setProjection(String[] projection) {
    109             mProjection = projection;
    110             return this;
    111         }
    112 
    113         /** Sets the resolved photo URI. This method is for the case
    114          *  where the URI given to {@link PhotoViewActivity} points directly
    115          *  to a single image and does not need to be resolved via a query
    116          *  to the {@link ContentProvider}. If this value is set, it supersedes
    117          *  {@link #setPhotosUri(String)}. */
    118         public PhotoViewIntentBuilder setResolvedPhotoUri(String resolvedPhotoUri) {
    119             mResolvedPhotoUri = resolvedPhotoUri;
    120             return this;
    121         }
    122 
    123         /**
    124          * Sets the URI for a thumbnail preview of the photo.
    125          */
    126         public PhotoViewIntentBuilder setThumbnailUri(String thumbnailUri) {
    127             mThumbnailUri = thumbnailUri;
    128             return this;
    129         }
    130 
    131         /**
    132          * Sets the maximum scale which an image is initially displayed at
    133          */
    134         public PhotoViewIntentBuilder setMaxInitialScale(float maxScale) {
    135             mMaxInitialScale = maxScale;
    136             return this;
    137         }
    138 
    139         /** Build the intent */
    140         public Intent build() {
    141             mIntent.setAction(Intent.ACTION_VIEW);
    142 
    143             mIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
    144 
    145             if (mPhotoIndex != null) {
    146                 mIntent.putExtra(EXTRA_PHOTO_INDEX, (int) mPhotoIndex);
    147             }
    148 
    149             if (mInitialPhotoUri != null) {
    150                 mIntent.putExtra(EXTRA_INITIAL_PHOTO_URI, mInitialPhotoUri);
    151             }
    152 
    153             if (mInitialPhotoUri != null && mPhotoIndex != null) {
    154                 throw new IllegalStateException(
    155                         "specified both photo index and photo uri");
    156             }
    157 
    158             if (mPhotosUri != null) {
    159                 mIntent.putExtra(EXTRA_PHOTOS_URI, mPhotosUri);
    160             }
    161 
    162             if (mResolvedPhotoUri != null) {
    163                 mIntent.putExtra(EXTRA_RESOLVED_PHOTO_URI, mResolvedPhotoUri);
    164             }
    165 
    166             if (mProjection != null) {
    167                 mIntent.putExtra(EXTRA_PROJECTION, mProjection);
    168             }
    169 
    170             if (mThumbnailUri != null) {
    171                 mIntent.putExtra(EXTRA_THUMBNAIL_URI, mThumbnailUri);
    172             }
    173 
    174             if (mMaxInitialScale != null) {
    175                 mIntent.putExtra(EXTRA_MAX_INITIAL_SCALE, mMaxInitialScale);
    176             }
    177 
    178             return mIntent;
    179         }
    180     }
    181 }
    182