Intents.java revision f5a30690749a354cbbf8c8d9ad4fd5ed84e7d41e
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.content.ContentProvider;
22import android.content.Context;
23import android.content.Intent;
24
25import com.android.ex.photo.fragments.PhotoViewFragment;
26
27/**
28 * Build intents to start app activities
29 */
30
31public 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    /** Gets a new photo view intent builder */
81    public static PhotoViewIntentBuilder newPhotoViewIntentBuilder(
82            Context context, String activityName) {
83        return new PhotoViewIntentBuilder(context, activityName);
84    }
85
86    /** Builder to create a photo view intent */
87    public static class PhotoViewIntentBuilder {
88        private final Intent mIntent;
89
90        /** The index of the photo to show */
91        private Integer mPhotoIndex;
92        /** The URI of the initial photo to show */
93        private String mInitialPhotoUri;
94        /** The URI of the initial thumbnail to show */
95        private String mInitialThumbnailUri;
96        /** The URI of the group of photos to display */
97        private String mPhotosUri;
98        /** The URL of the photo to display */
99        private String mResolvedPhotoUri;
100        /** The projection for the query to use; optional */
101        private String[] mProjection;
102        /** The URI of a thumbnail of the photo to display */
103        private String mThumbnailUri;
104        /** The maximum scale to display images at before  */
105        private Float mMaxInitialScale;
106        /**
107         * True if the PhotoViewFragments should watch for network changes to restart their loaders
108         */
109        private boolean mWatchNetwork;
110        /** true we want to run the image scale animation */
111        private boolean mScaleAnimation;
112        /** The parameters for performing the scale up/scale down animations
113         * upon enter and exit. StartX and StartY represent the screen coordinates
114         * of the upper left corner of the start rectangle, startWidth and startHeight
115         * represent the width and height of the start rectangle.
116         */
117        private int mStartX;
118        private int mStartY;
119        private int mStartWidth;
120        private int mStartHeight;
121
122        private boolean mActionBarHiddenInitially;
123        private boolean mDisplayFullScreenThumbs;
124
125        private PhotoViewIntentBuilder(Context context, Class<?> cls) {
126            mIntent = new Intent(context, cls);
127            initialize();
128        }
129
130        private PhotoViewIntentBuilder(Context context, String activityName) {
131            mIntent = new Intent();
132            mIntent.setClassName(context, activityName);
133            initialize();
134        }
135
136        private void initialize() {
137            mScaleAnimation = false;
138            mActionBarHiddenInitially = false;
139            mDisplayFullScreenThumbs = false;
140        }
141
142        /** Sets the photo index */
143        public PhotoViewIntentBuilder setPhotoIndex(Integer photoIndex) {
144            mPhotoIndex = photoIndex;
145            return this;
146        }
147
148        /** Sets the initial photo URI */
149        public PhotoViewIntentBuilder setInitialPhotoUri(String initialPhotoUri) {
150            mInitialPhotoUri = initialPhotoUri;
151            return this;
152        }
153
154        /** Sets the photos URI */
155        public PhotoViewIntentBuilder setPhotosUri(String photosUri) {
156            mPhotosUri = photosUri;
157            return this;
158        }
159
160        /** Sets the query projection */
161        public PhotoViewIntentBuilder setProjection(String[] projection) {
162            mProjection = projection;
163            return this;
164        }
165
166        /** Sets the resolved photo URI. This method is for the case
167         *  where the URI given to {@link PhotoViewActivity} points directly
168         *  to a single image and does not need to be resolved via a query
169         *  to the {@link ContentProvider}. If this value is set, it supersedes
170         *  {@link #setPhotosUri(String)}. */
171        public PhotoViewIntentBuilder setResolvedPhotoUri(String resolvedPhotoUri) {
172            mResolvedPhotoUri = resolvedPhotoUri;
173            return this;
174        }
175
176        /**
177         * Sets the URI for a thumbnail preview of the photo.
178         */
179        public PhotoViewIntentBuilder setThumbnailUri(String thumbnailUri) {
180            mThumbnailUri = thumbnailUri;
181            return this;
182        }
183
184        /**
185         * Sets the maximum scale which an image is initially displayed at
186         */
187        public PhotoViewIntentBuilder setMaxInitialScale(float maxScale) {
188            mMaxInitialScale = maxScale;
189            return this;
190        }
191
192        /**
193         * Enable watching the network for connectivity changes.
194         *
195         * When a change is detected, bitmap loaders will be restarted if required.
196         */
197        public PhotoViewIntentBuilder watchNetworkConnectivityChanges() {
198            mWatchNetwork = true;
199            return this;
200        }
201
202        /**
203         * Enable a scale animation that animates the initial photo URI passed in using
204         * {@link #setInitialPhotoUri}.
205         *
206         * Note: To avoid janky transitions, particularly when exiting the photoviewer, ensure the
207         * following system UI flags are set on the root view of the relying app's activity
208         * (via @{link View.setSystemUiVisibility(int)}):
209         *     {@code View.SYSTEM_UI_FLAG_VISIBLE | View.SYSTEM_UI_FLAG_LAYOUT_STABLE}
210         * As well, client should ensure {@code android:fitsSystemWindows} is set on the root
211         * content view.
212         */
213        public PhotoViewIntentBuilder setScaleAnimation(int startX, int startY,
214                int startWidth, int startHeight) {
215            mScaleAnimation = true;
216            mStartX = startX;
217            mStartY = startY;
218            mStartWidth = startWidth;
219            mStartHeight = startHeight;
220            return this;
221        }
222
223        // If this option is turned on, then the photoViewer will be initially
224        // displayed with the action bar hidden. This is as opposed to the default
225        // behavior, where the actionBar is initially shown.
226        public PhotoViewIntentBuilder setActionBarHiddenInitially(
227                boolean actionBarHiddenInitially) {
228            mActionBarHiddenInitially = actionBarHiddenInitially;
229            return this;
230        }
231
232        // If this option is turned on, then the small, lo-res thumbnail will
233        // be scaled up to the maximum size to cover as much of the screen as
234        // possible while still maintaining the correct aspect ratio. This means
235        // that the image may appear blurry until the the full-res image is
236        // loaded.
237        // This is as opposed to the default behavior, where only part of the
238        // thumbnail is displayed in a small view in the center of the screen,
239        // and a loading spinner is displayed until the full-res image is loaded.
240        public PhotoViewIntentBuilder setDisplayThumbsFullScreen(
241                boolean displayFullScreenThumbs) {
242            mDisplayFullScreenThumbs = displayFullScreenThumbs;
243            return this;
244        }
245
246        /** Build the intent */
247        public Intent build() {
248            mIntent.setAction(Intent.ACTION_VIEW);
249
250            mIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
251
252            if (mPhotoIndex != null) {
253                mIntent.putExtra(EXTRA_PHOTO_INDEX, (int) mPhotoIndex);
254            }
255
256            if (mInitialPhotoUri != null) {
257                mIntent.putExtra(EXTRA_INITIAL_PHOTO_URI, mInitialPhotoUri);
258            }
259
260            if (mInitialPhotoUri != null && mPhotoIndex != null) {
261                throw new IllegalStateException(
262                        "specified both photo index and photo uri");
263            }
264
265            if (mPhotosUri != null) {
266                mIntent.putExtra(EXTRA_PHOTOS_URI, mPhotosUri);
267            }
268
269            if (mResolvedPhotoUri != null) {
270                mIntent.putExtra(EXTRA_RESOLVED_PHOTO_URI, mResolvedPhotoUri);
271            }
272
273            if (mProjection != null) {
274                mIntent.putExtra(EXTRA_PROJECTION, mProjection);
275            }
276
277            if (mThumbnailUri != null) {
278                mIntent.putExtra(EXTRA_THUMBNAIL_URI, mThumbnailUri);
279            }
280
281            if (mMaxInitialScale != null) {
282                mIntent.putExtra(EXTRA_MAX_INITIAL_SCALE, mMaxInitialScale);
283            }
284
285            if (mWatchNetwork == true) {
286                mIntent.putExtra(EXTRA_WATCH_NETWORK, true);
287            }
288
289            mIntent.putExtra(EXTRA_SCALE_UP_ANIMATION, mScaleAnimation);
290            if (mScaleAnimation) {
291                mIntent.putExtra(EXTRA_ANIMATION_START_X, mStartX);
292                mIntent.putExtra(EXTRA_ANIMATION_START_Y, mStartY);
293                mIntent.putExtra(EXTRA_ANIMATION_START_WIDTH, mStartWidth);
294                mIntent.putExtra(EXTRA_ANIMATION_START_HEIGHT, mStartHeight);
295            }
296
297            mIntent.putExtra(EXTRA_ACTION_BAR_HIDDEN_INITIALLY, mActionBarHiddenInitially);
298            mIntent.putExtra(EXTRA_DISPLAY_THUMBS_FULLSCREEN, mDisplayFullScreenThumbs);
299
300            return mIntent;
301        }
302    }
303}
304