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
38    /**
39     * Gets a photo view intent builder to display the photos from phone activity.
40     *
41     * @param context The context
42     * @return The intent builder
43     */
44    public static PhotoViewIntentBuilder newPhotoViewActivityIntentBuilder(Context context) {
45        return new PhotoViewIntentBuilder(context, PhotoViewActivity.class);
46    }
47
48    /**
49     * Gets a photo view intent builder to display the photo view fragment
50     *
51     * @param context The context
52     * @return The intent builder
53     */
54    public static PhotoViewIntentBuilder newPhotoViewFragmentIntentBuilder(Context context) {
55        return new PhotoViewIntentBuilder(context, PhotoViewFragment.class);
56    }
57
58    /** Gets a new photo view intent builder */
59    public static PhotoViewIntentBuilder newPhotoViewIntentBuilder(
60            Context context, Class<? extends PhotoViewActivity> cls) {
61        return new PhotoViewIntentBuilder(context, cls);
62    }
63
64    /** Builder to create a photo view intent */
65    public static class PhotoViewIntentBuilder {
66        private final Intent mIntent;
67
68        /** The index of the photo to show */
69        private Integer mPhotoIndex;
70        /** The URI of the group of photos to display */
71        private String mPhotosUri;
72        /** The URL of the photo to display */
73        private String mResolvedPhotoUri;
74        /** The projection for the query to use; optional */
75        private String[] mProjection;
76        /** The URI of a thumbnail of the photo to display */
77        private String mThumbnailUri;
78
79        private PhotoViewIntentBuilder(Context context, Class<?> cls) {
80            mIntent = new Intent(context, cls);
81        }
82
83        /** Sets the photo index */
84        public PhotoViewIntentBuilder setPhotoIndex(Integer photoIndex) {
85            mPhotoIndex = photoIndex;
86            return this;
87        }
88
89        /** Sets the photos URI */
90        public PhotoViewIntentBuilder setPhotosUri(String photosUri) {
91            mPhotosUri = photosUri;
92            return this;
93        }
94
95        /** Sets the query projection */
96        public PhotoViewIntentBuilder setProjection(String[] projection) {
97            mProjection = projection;
98            return this;
99        }
100
101        /** Sets the resolved photo URI. This method is for the case
102         *  where the URI given to {@link PhotoViewActivity} points directly
103         *  to a single image and does not need to be resolved via a query
104         *  to the {@link ContentProvider}. If this value is set, it supersedes
105         *  {@link #setPhotosUri(String)}. */
106        public PhotoViewIntentBuilder setResolvedPhotoUri(String resolvedPhotoUri) {
107            mResolvedPhotoUri = resolvedPhotoUri;
108            return this;
109        }
110
111        /**
112         * Sets the URI for a thumbnail preview of the photo.
113         */
114        public PhotoViewIntentBuilder setThumbnailUri(String thumbnailUri) {
115            mThumbnailUri = thumbnailUri;
116            return this;
117        }
118
119        /** Build the intent */
120        public Intent build() {
121            mIntent.setAction(Intent.ACTION_VIEW);
122
123            mIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
124
125            if (mPhotoIndex != null) {
126                mIntent.putExtra(EXTRA_PHOTO_INDEX, (int) mPhotoIndex);
127            }
128
129            if (mPhotosUri != null) {
130                mIntent.putExtra(EXTRA_PHOTOS_URI, mPhotosUri);
131            }
132
133            if (mResolvedPhotoUri != null) {
134                mIntent.putExtra(EXTRA_RESOLVED_PHOTO_URI, mResolvedPhotoUri);
135            }
136
137            if (mProjection != null) {
138                mIntent.putExtra(EXTRA_PROJECTION, mProjection);
139            }
140
141            if (mThumbnailUri != null) {
142                mIntent.putExtra(EXTRA_THUMBNAIL_URI, mThumbnailUri);
143            }
144
145            return mIntent;
146        }
147    }
148}
149