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