Intents.java revision 5c16562ccda53e0f204490c2218f0f8127169dd8
1f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein/*
2f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein * Copyright (C) 2011 Google Inc.
3f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein * Licensed to The Android Open Source Project.
4f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein *
5f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein * Licensed under the Apache License, Version 2.0 (the "License");
6f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein * you may not use this file except in compliance with the License.
7f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein * You may obtain a copy of the License at
8f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein *
9f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein *      http://www.apache.org/licenses/LICENSE-2.0
10f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein *
11f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein * Unless required by applicable law or agreed to in writing, software
12f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein * distributed under the License is distributed on an "AS IS" BASIS,
13f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein * See the License for the specific language governing permissions and
15f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein * limitations under the License.
16f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein */
17f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
18f77a7eb196d16110c7b1087352b423913821ff61Andrew Sappersteinpackage com.android.ex.photo;
19f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
205c16562ccda53e0f204490c2218f0f8127169dd8Martin Hibdonimport android.app.Activity;
21f77a7eb196d16110c7b1087352b423913821ff61Andrew Sappersteinimport android.content.ContentProvider;
22f77a7eb196d16110c7b1087352b423913821ff61Andrew Sappersteinimport android.content.Context;
23f77a7eb196d16110c7b1087352b423913821ff61Andrew Sappersteinimport android.content.Intent;
24f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
25f77a7eb196d16110c7b1087352b423913821ff61Andrew Sappersteinimport com.android.ex.photo.fragments.PhotoViewFragment;
26f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
27f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein/**
28f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein * Build intents to start app activities
29f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein */
30f77a7eb196d16110c7b1087352b423913821ff61Andrew Sappersteinpublic class Intents {
31f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein    // Intent extras
32f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein    public static final String EXTRA_PHOTO_INDEX = "photo_index";
3399f6f3ea0e43d5ac96344a987735fc39485f8f05Martin Hibdon    public static final String EXTRA_INITIAL_PHOTO_URI = "initial_photo_uri";
34f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein    public static final String EXTRA_PHOTOS_URI = "photos_uri";
35f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein    public static final String EXTRA_RESOLVED_PHOTO_URI = "resolved_photo_uri";
36f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein    public static final String EXTRA_PROJECTION = "projection";
37928a39fb533255b34145285fabbd4f51961df63aAndrew Sapperstein    public static final String EXTRA_THUMBNAIL_URI = "thumbnail_uri";
3891dbfd25cc234de393ae22fc39a832a6335e1bc2Adam Copp    public static final String EXTRA_MAX_INITIAL_SCALE = "max_scale";
39f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
40f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein    /**
41f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein     * Gets a photo view intent builder to display the photos from phone activity.
42f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein     *
43f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein     * @param context The context
44f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein     * @return The intent builder
45f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein     */
46f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein    public static PhotoViewIntentBuilder newPhotoViewActivityIntentBuilder(Context context) {
47f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        return new PhotoViewIntentBuilder(context, PhotoViewActivity.class);
48f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein    }
49f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
50f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein    /**
51f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein     * Gets a photo view intent builder to display the photo view fragment
52f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein     *
53f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein     * @param context The context
54f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein     * @return The intent builder
55f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein     */
56f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein    public static PhotoViewIntentBuilder newPhotoViewFragmentIntentBuilder(Context context) {
57f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        return new PhotoViewIntentBuilder(context, PhotoViewFragment.class);
58f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein    }
59f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
60f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein    /** Gets a new photo view intent builder */
61f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein    public static PhotoViewIntentBuilder newPhotoViewIntentBuilder(
625c16562ccda53e0f204490c2218f0f8127169dd8Martin Hibdon            Context context, Class<? extends Activity> cls) {
63f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        return new PhotoViewIntentBuilder(context, cls);
64f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein    }
65f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
66f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein    /** Builder to create a photo view intent */
67f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein    public static class PhotoViewIntentBuilder {
68f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        private final Intent mIntent;
69f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
70f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        /** The index of the photo to show */
71f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        private Integer mPhotoIndex;
7299f6f3ea0e43d5ac96344a987735fc39485f8f05Martin Hibdon        /** The URI of the initial photo to show */
7399f6f3ea0e43d5ac96344a987735fc39485f8f05Martin Hibdon        private String mInitialPhotoUri;
74f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        /** The URI of the group of photos to display */
75f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        private String mPhotosUri;
76f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        /** The URL of the photo to display */
77f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        private String mResolvedPhotoUri;
78f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        /** The projection for the query to use; optional */
79f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        private String[] mProjection;
80928a39fb533255b34145285fabbd4f51961df63aAndrew Sapperstein        /** The URI of a thumbnail of the photo to display */
81928a39fb533255b34145285fabbd4f51961df63aAndrew Sapperstein        private String mThumbnailUri;
8291dbfd25cc234de393ae22fc39a832a6335e1bc2Adam Copp        /** The maximum scale to display images at before  */
8391dbfd25cc234de393ae22fc39a832a6335e1bc2Adam Copp        private Float mMaxInitialScale;
84f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
85f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        private PhotoViewIntentBuilder(Context context, Class<?> cls) {
86f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            mIntent = new Intent(context, cls);
87f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        }
88f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
89f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        /** Sets the photo index */
90f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        public PhotoViewIntentBuilder setPhotoIndex(Integer photoIndex) {
91f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            mPhotoIndex = photoIndex;
92f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            return this;
93f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        }
94f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
9599f6f3ea0e43d5ac96344a987735fc39485f8f05Martin Hibdon        /** Sets the initial photo URI */
9699f6f3ea0e43d5ac96344a987735fc39485f8f05Martin Hibdon        public PhotoViewIntentBuilder setInitialPhotoUri(String initialPhotoUri) {
9799f6f3ea0e43d5ac96344a987735fc39485f8f05Martin Hibdon            mInitialPhotoUri = initialPhotoUri;
9899f6f3ea0e43d5ac96344a987735fc39485f8f05Martin Hibdon            return this;
9999f6f3ea0e43d5ac96344a987735fc39485f8f05Martin Hibdon        }
10099f6f3ea0e43d5ac96344a987735fc39485f8f05Martin Hibdon
101f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        /** Sets the photos URI */
102f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        public PhotoViewIntentBuilder setPhotosUri(String photosUri) {
103f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            mPhotosUri = photosUri;
104f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            return this;
105f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        }
106f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
107f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        /** Sets the query projection */
108f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        public PhotoViewIntentBuilder setProjection(String[] projection) {
109f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            mProjection = projection;
110f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            return this;
111f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        }
112f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
113f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        /** Sets the resolved photo URI. This method is for the case
114f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein         *  where the URI given to {@link PhotoViewActivity} points directly
115f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein         *  to a single image and does not need to be resolved via a query
116f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein         *  to the {@link ContentProvider}. If this value is set, it supersedes
117f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein         *  {@link #setPhotosUri(String)}. */
118f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        public PhotoViewIntentBuilder setResolvedPhotoUri(String resolvedPhotoUri) {
119f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            mResolvedPhotoUri = resolvedPhotoUri;
120f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            return this;
121f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        }
122f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
123928a39fb533255b34145285fabbd4f51961df63aAndrew Sapperstein        /**
124928a39fb533255b34145285fabbd4f51961df63aAndrew Sapperstein         * Sets the URI for a thumbnail preview of the photo.
125928a39fb533255b34145285fabbd4f51961df63aAndrew Sapperstein         */
126928a39fb533255b34145285fabbd4f51961df63aAndrew Sapperstein        public PhotoViewIntentBuilder setThumbnailUri(String thumbnailUri) {
127928a39fb533255b34145285fabbd4f51961df63aAndrew Sapperstein            mThumbnailUri = thumbnailUri;
128928a39fb533255b34145285fabbd4f51961df63aAndrew Sapperstein            return this;
129928a39fb533255b34145285fabbd4f51961df63aAndrew Sapperstein        }
130928a39fb533255b34145285fabbd4f51961df63aAndrew Sapperstein
13191dbfd25cc234de393ae22fc39a832a6335e1bc2Adam Copp        /**
13291dbfd25cc234de393ae22fc39a832a6335e1bc2Adam Copp         * Sets the maximum scale which an image is initially displayed at
13391dbfd25cc234de393ae22fc39a832a6335e1bc2Adam Copp         */
13491dbfd25cc234de393ae22fc39a832a6335e1bc2Adam Copp        public PhotoViewIntentBuilder setMaxInitialScale(float maxScale) {
13591dbfd25cc234de393ae22fc39a832a6335e1bc2Adam Copp            mMaxInitialScale = maxScale;
13691dbfd25cc234de393ae22fc39a832a6335e1bc2Adam Copp            return this;
13791dbfd25cc234de393ae22fc39a832a6335e1bc2Adam Copp        }
13891dbfd25cc234de393ae22fc39a832a6335e1bc2Adam Copp
139f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        /** Build the intent */
140f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        public Intent build() {
141f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            mIntent.setAction(Intent.ACTION_VIEW);
142f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
143f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            mIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
144f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
145f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            if (mPhotoIndex != null) {
146f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein                mIntent.putExtra(EXTRA_PHOTO_INDEX, (int) mPhotoIndex);
147f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            }
148f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
14999f6f3ea0e43d5ac96344a987735fc39485f8f05Martin Hibdon            if (mInitialPhotoUri != null) {
15099f6f3ea0e43d5ac96344a987735fc39485f8f05Martin Hibdon                mIntent.putExtra(EXTRA_INITIAL_PHOTO_URI, mInitialPhotoUri);
15199f6f3ea0e43d5ac96344a987735fc39485f8f05Martin Hibdon            }
15299f6f3ea0e43d5ac96344a987735fc39485f8f05Martin Hibdon
15399f6f3ea0e43d5ac96344a987735fc39485f8f05Martin Hibdon            if (mInitialPhotoUri != null && mPhotoIndex != null) {
15499f6f3ea0e43d5ac96344a987735fc39485f8f05Martin Hibdon                throw new IllegalStateException(
15599f6f3ea0e43d5ac96344a987735fc39485f8f05Martin Hibdon                        "specified both photo index and photo uri");
15699f6f3ea0e43d5ac96344a987735fc39485f8f05Martin Hibdon            }
15799f6f3ea0e43d5ac96344a987735fc39485f8f05Martin Hibdon
158f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            if (mPhotosUri != null) {
159f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein                mIntent.putExtra(EXTRA_PHOTOS_URI, mPhotosUri);
160f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            }
161f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
162f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            if (mResolvedPhotoUri != null) {
163f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein                mIntent.putExtra(EXTRA_RESOLVED_PHOTO_URI, mResolvedPhotoUri);
164f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            }
165f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
166f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            if (mProjection != null) {
167f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein                mIntent.putExtra(EXTRA_PROJECTION, mProjection);
168f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            }
169f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
170928a39fb533255b34145285fabbd4f51961df63aAndrew Sapperstein            if (mThumbnailUri != null) {
171928a39fb533255b34145285fabbd4f51961df63aAndrew Sapperstein                mIntent.putExtra(EXTRA_THUMBNAIL_URI, mThumbnailUri);
172928a39fb533255b34145285fabbd4f51961df63aAndrew Sapperstein            }
173928a39fb533255b34145285fabbd4f51961df63aAndrew Sapperstein
17491dbfd25cc234de393ae22fc39a832a6335e1bc2Adam Copp            if (mMaxInitialScale != null) {
17591dbfd25cc234de393ae22fc39a832a6335e1bc2Adam Copp                mIntent.putExtra(EXTRA_MAX_INITIAL_SCALE, mMaxInitialScale);
17691dbfd25cc234de393ae22fc39a832a6335e1bc2Adam Copp            }
17791dbfd25cc234de393ae22fc39a832a6335e1bc2Adam Copp
178f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            return mIntent;
179f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        }
180f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein    }
181f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein}
182