Intents.java revision 99f6f3ea0e43d5ac96344a987735fc39485f8f05
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
20f77a7eb196d16110c7b1087352b423913821ff61Andrew Sappersteinimport android.content.ContentProvider;
21f77a7eb196d16110c7b1087352b423913821ff61Andrew Sappersteinimport android.content.Context;
22f77a7eb196d16110c7b1087352b423913821ff61Andrew Sappersteinimport android.content.Intent;
23f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
24f77a7eb196d16110c7b1087352b423913821ff61Andrew Sappersteinimport com.android.ex.photo.fragments.PhotoViewFragment;
25f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
26f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein/**
27f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein * Build intents to start app activities
28f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein */
29f77a7eb196d16110c7b1087352b423913821ff61Andrew Sappersteinpublic class Intents {
30f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein    // Intent extras
31f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein    public static final String EXTRA_PHOTO_INDEX = "photo_index";
3299f6f3ea0e43d5ac96344a987735fc39485f8f05Martin Hibdon    public static final String EXTRA_INITIAL_PHOTO_URI = "initial_photo_uri";
33f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein    public static final String EXTRA_PHOTOS_URI = "photos_uri";
34f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein    public static final String EXTRA_RESOLVED_PHOTO_URI = "resolved_photo_uri";
35f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein    public static final String EXTRA_PROJECTION = "projection";
36928a39fb533255b34145285fabbd4f51961df63aAndrew Sapperstein    public static final String EXTRA_THUMBNAIL_URI = "thumbnail_uri";
3791dbfd25cc234de393ae22fc39a832a6335e1bc2Adam Copp    public static final String EXTRA_MAX_INITIAL_SCALE = "max_scale";
38f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
39f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein    /**
40f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein     * Gets a photo view intent builder to display the photos from phone activity.
41f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein     *
42f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein     * @param context The context
43f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein     * @return The intent builder
44f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein     */
45f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein    public static PhotoViewIntentBuilder newPhotoViewActivityIntentBuilder(Context context) {
46f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        return new PhotoViewIntentBuilder(context, PhotoViewActivity.class);
47f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein    }
48f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
49f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein    /**
50f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein     * Gets a photo view intent builder to display the photo view fragment
51f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein     *
52f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein     * @param context The context
53f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein     * @return The intent builder
54f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein     */
55f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein    public static PhotoViewIntentBuilder newPhotoViewFragmentIntentBuilder(Context context) {
56f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        return new PhotoViewIntentBuilder(context, PhotoViewFragment.class);
57f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein    }
58f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
59f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein    /** Gets a new photo view intent builder */
60f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein    public static PhotoViewIntentBuilder newPhotoViewIntentBuilder(
61f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            Context context, Class<? extends PhotoViewActivity> cls) {
62f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        return new PhotoViewIntentBuilder(context, cls);
63f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein    }
64f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
65f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein    /** Builder to create a photo view intent */
66f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein    public static class PhotoViewIntentBuilder {
67f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        private final Intent mIntent;
68f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
69f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        /** The index of the photo to show */
70f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        private Integer mPhotoIndex;
7199f6f3ea0e43d5ac96344a987735fc39485f8f05Martin Hibdon        /** The URI of the initial photo to show */
7299f6f3ea0e43d5ac96344a987735fc39485f8f05Martin Hibdon        private String mInitialPhotoUri;
73f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        /** The URI of the group of photos to display */
74f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        private String mPhotosUri;
75f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        /** The URL of the photo to display */
76f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        private String mResolvedPhotoUri;
77f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        /** The projection for the query to use; optional */
78f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        private String[] mProjection;
79928a39fb533255b34145285fabbd4f51961df63aAndrew Sapperstein        /** The URI of a thumbnail of the photo to display */
80928a39fb533255b34145285fabbd4f51961df63aAndrew Sapperstein        private String mThumbnailUri;
8191dbfd25cc234de393ae22fc39a832a6335e1bc2Adam Copp        /** The maximum scale to display images at before  */
8291dbfd25cc234de393ae22fc39a832a6335e1bc2Adam Copp        private Float mMaxInitialScale;
83f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
84f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        private PhotoViewIntentBuilder(Context context, Class<?> cls) {
85f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            mIntent = new Intent(context, cls);
86f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        }
87f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
88f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        /** Sets the photo index */
89f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        public PhotoViewIntentBuilder setPhotoIndex(Integer photoIndex) {
90f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            mPhotoIndex = photoIndex;
91f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            return this;
92f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        }
93f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
9499f6f3ea0e43d5ac96344a987735fc39485f8f05Martin Hibdon        /** Sets the initial photo URI */
9599f6f3ea0e43d5ac96344a987735fc39485f8f05Martin Hibdon        public PhotoViewIntentBuilder setInitialPhotoUri(String initialPhotoUri) {
9699f6f3ea0e43d5ac96344a987735fc39485f8f05Martin Hibdon            mInitialPhotoUri = initialPhotoUri;
9799f6f3ea0e43d5ac96344a987735fc39485f8f05Martin Hibdon            return this;
9899f6f3ea0e43d5ac96344a987735fc39485f8f05Martin Hibdon        }
9999f6f3ea0e43d5ac96344a987735fc39485f8f05Martin Hibdon
100f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        /** Sets the photos URI */
101f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        public PhotoViewIntentBuilder setPhotosUri(String photosUri) {
102f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            mPhotosUri = photosUri;
103f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            return this;
104f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        }
105f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
106f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        /** Sets the query projection */
107f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        public PhotoViewIntentBuilder setProjection(String[] projection) {
108f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            mProjection = projection;
109f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            return this;
110f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        }
111f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
112f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        /** Sets the resolved photo URI. This method is for the case
113f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein         *  where the URI given to {@link PhotoViewActivity} points directly
114f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein         *  to a single image and does not need to be resolved via a query
115f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein         *  to the {@link ContentProvider}. If this value is set, it supersedes
116f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein         *  {@link #setPhotosUri(String)}. */
117f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        public PhotoViewIntentBuilder setResolvedPhotoUri(String resolvedPhotoUri) {
118f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            mResolvedPhotoUri = resolvedPhotoUri;
119f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            return this;
120f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        }
121f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
122928a39fb533255b34145285fabbd4f51961df63aAndrew Sapperstein        /**
123928a39fb533255b34145285fabbd4f51961df63aAndrew Sapperstein         * Sets the URI for a thumbnail preview of the photo.
124928a39fb533255b34145285fabbd4f51961df63aAndrew Sapperstein         */
125928a39fb533255b34145285fabbd4f51961df63aAndrew Sapperstein        public PhotoViewIntentBuilder setThumbnailUri(String thumbnailUri) {
126928a39fb533255b34145285fabbd4f51961df63aAndrew Sapperstein            mThumbnailUri = thumbnailUri;
127928a39fb533255b34145285fabbd4f51961df63aAndrew Sapperstein            return this;
128928a39fb533255b34145285fabbd4f51961df63aAndrew Sapperstein        }
129928a39fb533255b34145285fabbd4f51961df63aAndrew Sapperstein
13091dbfd25cc234de393ae22fc39a832a6335e1bc2Adam Copp        /**
13191dbfd25cc234de393ae22fc39a832a6335e1bc2Adam Copp         * Sets the maximum scale which an image is initially displayed at
13291dbfd25cc234de393ae22fc39a832a6335e1bc2Adam Copp         */
13391dbfd25cc234de393ae22fc39a832a6335e1bc2Adam Copp        public PhotoViewIntentBuilder setMaxInitialScale(float maxScale) {
13491dbfd25cc234de393ae22fc39a832a6335e1bc2Adam Copp            mMaxInitialScale = maxScale;
13591dbfd25cc234de393ae22fc39a832a6335e1bc2Adam Copp            return this;
13691dbfd25cc234de393ae22fc39a832a6335e1bc2Adam Copp        }
13791dbfd25cc234de393ae22fc39a832a6335e1bc2Adam Copp
138f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        /** Build the intent */
139f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        public Intent build() {
140f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            mIntent.setAction(Intent.ACTION_VIEW);
141f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
142f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            mIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
143f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
144f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            if (mPhotoIndex != null) {
145f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein                mIntent.putExtra(EXTRA_PHOTO_INDEX, (int) mPhotoIndex);
146f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            }
147f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
14899f6f3ea0e43d5ac96344a987735fc39485f8f05Martin Hibdon            if (mInitialPhotoUri != null) {
14999f6f3ea0e43d5ac96344a987735fc39485f8f05Martin Hibdon                mIntent.putExtra(EXTRA_INITIAL_PHOTO_URI, mInitialPhotoUri);
15099f6f3ea0e43d5ac96344a987735fc39485f8f05Martin Hibdon            }
15199f6f3ea0e43d5ac96344a987735fc39485f8f05Martin Hibdon
15299f6f3ea0e43d5ac96344a987735fc39485f8f05Martin Hibdon            if (mInitialPhotoUri != null && mPhotoIndex != null) {
15399f6f3ea0e43d5ac96344a987735fc39485f8f05Martin Hibdon                throw new IllegalStateException(
15499f6f3ea0e43d5ac96344a987735fc39485f8f05Martin Hibdon                        "specified both photo index and photo uri");
15599f6f3ea0e43d5ac96344a987735fc39485f8f05Martin Hibdon            }
15699f6f3ea0e43d5ac96344a987735fc39485f8f05Martin Hibdon
157f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            if (mPhotosUri != null) {
158f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein                mIntent.putExtra(EXTRA_PHOTOS_URI, mPhotosUri);
159f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            }
160f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
161f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            if (mResolvedPhotoUri != null) {
162f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein                mIntent.putExtra(EXTRA_RESOLVED_PHOTO_URI, mResolvedPhotoUri);
163f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            }
164f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
165f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            if (mProjection != null) {
166f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein                mIntent.putExtra(EXTRA_PROJECTION, mProjection);
167f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            }
168f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
169928a39fb533255b34145285fabbd4f51961df63aAndrew Sapperstein            if (mThumbnailUri != null) {
170928a39fb533255b34145285fabbd4f51961df63aAndrew Sapperstein                mIntent.putExtra(EXTRA_THUMBNAIL_URI, mThumbnailUri);
171928a39fb533255b34145285fabbd4f51961df63aAndrew Sapperstein            }
172928a39fb533255b34145285fabbd4f51961df63aAndrew Sapperstein
17391dbfd25cc234de393ae22fc39a832a6335e1bc2Adam Copp            if (mMaxInitialScale != null) {
17491dbfd25cc234de393ae22fc39a832a6335e1bc2Adam Copp                mIntent.putExtra(EXTRA_MAX_INITIAL_SCALE, mMaxInitialScale);
17591dbfd25cc234de393ae22fc39a832a6335e1bc2Adam Copp            }
17691dbfd25cc234de393ae22fc39a832a6335e1bc2Adam Copp
177f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            return mIntent;
178f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        }
179f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein    }
180f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein}
181