Intents.java revision 7732839c0cd9d87954a07e166b52b93cabea1374
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";
397732839c0cd9d87954a07e166b52b93cabea1374Adam Copp    public static final String EXTRA_WATCH_NETWORK = "watch_network";
40f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
41f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein    /**
42f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein     * Gets a photo view intent builder to display the photos from phone activity.
43f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein     *
44f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein     * @param context The context
45f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein     * @return The intent builder
46f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein     */
47f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein    public static PhotoViewIntentBuilder newPhotoViewActivityIntentBuilder(Context context) {
48f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        return new PhotoViewIntentBuilder(context, PhotoViewActivity.class);
49f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein    }
50f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
51f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein    /**
52f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein     * Gets a photo view intent builder to display the photo view fragment
53f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein     *
54f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein     * @param context The context
55f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein     * @return The intent builder
56f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein     */
57f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein    public static PhotoViewIntentBuilder newPhotoViewFragmentIntentBuilder(Context context) {
58f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        return new PhotoViewIntentBuilder(context, PhotoViewFragment.class);
59f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein    }
60f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
61f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein    /** Gets a new photo view intent builder */
62f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein    public static PhotoViewIntentBuilder newPhotoViewIntentBuilder(
635c16562ccda53e0f204490c2218f0f8127169dd8Martin Hibdon            Context context, Class<? extends Activity> cls) {
64f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        return new PhotoViewIntentBuilder(context, cls);
65f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein    }
66f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
67f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein    /** Builder to create a photo view intent */
68f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein    public static class PhotoViewIntentBuilder {
69f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        private final Intent mIntent;
70f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
71f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        /** The index of the photo to show */
72f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        private Integer mPhotoIndex;
7399f6f3ea0e43d5ac96344a987735fc39485f8f05Martin Hibdon        /** The URI of the initial photo to show */
7499f6f3ea0e43d5ac96344a987735fc39485f8f05Martin Hibdon        private String mInitialPhotoUri;
75f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        /** The URI of the group of photos to display */
76f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        private String mPhotosUri;
77f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        /** The URL of the photo to display */
78f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        private String mResolvedPhotoUri;
79f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        /** The projection for the query to use; optional */
80f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        private String[] mProjection;
81928a39fb533255b34145285fabbd4f51961df63aAndrew Sapperstein        /** The URI of a thumbnail of the photo to display */
82928a39fb533255b34145285fabbd4f51961df63aAndrew Sapperstein        private String mThumbnailUri;
8391dbfd25cc234de393ae22fc39a832a6335e1bc2Adam Copp        /** The maximum scale to display images at before  */
8491dbfd25cc234de393ae22fc39a832a6335e1bc2Adam Copp        private Float mMaxInitialScale;
857732839c0cd9d87954a07e166b52b93cabea1374Adam Copp        /**
867732839c0cd9d87954a07e166b52b93cabea1374Adam Copp         * True if the PhotoViewFragments should watch for network changes to restart their loaders
877732839c0cd9d87954a07e166b52b93cabea1374Adam Copp         */
887732839c0cd9d87954a07e166b52b93cabea1374Adam Copp        private boolean mWatchNetwork;
89f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
90f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        private PhotoViewIntentBuilder(Context context, Class<?> cls) {
91f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            mIntent = new Intent(context, cls);
92f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        }
93f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
94f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        /** Sets the photo index */
95f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        public PhotoViewIntentBuilder setPhotoIndex(Integer photoIndex) {
96f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            mPhotoIndex = photoIndex;
97f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            return this;
98f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        }
99f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
10099f6f3ea0e43d5ac96344a987735fc39485f8f05Martin Hibdon        /** Sets the initial photo URI */
10199f6f3ea0e43d5ac96344a987735fc39485f8f05Martin Hibdon        public PhotoViewIntentBuilder setInitialPhotoUri(String initialPhotoUri) {
10299f6f3ea0e43d5ac96344a987735fc39485f8f05Martin Hibdon            mInitialPhotoUri = initialPhotoUri;
10399f6f3ea0e43d5ac96344a987735fc39485f8f05Martin Hibdon            return this;
10499f6f3ea0e43d5ac96344a987735fc39485f8f05Martin Hibdon        }
10599f6f3ea0e43d5ac96344a987735fc39485f8f05Martin Hibdon
106f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        /** Sets the photos URI */
107f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        public PhotoViewIntentBuilder setPhotosUri(String photosUri) {
108f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            mPhotosUri = photosUri;
109f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            return this;
110f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        }
111f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
112f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        /** Sets the query projection */
113f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        public PhotoViewIntentBuilder setProjection(String[] projection) {
114f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            mProjection = projection;
115f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            return this;
116f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        }
117f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
118f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        /** Sets the resolved photo URI. This method is for the case
119f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein         *  where the URI given to {@link PhotoViewActivity} points directly
120f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein         *  to a single image and does not need to be resolved via a query
121f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein         *  to the {@link ContentProvider}. If this value is set, it supersedes
122f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein         *  {@link #setPhotosUri(String)}. */
123f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        public PhotoViewIntentBuilder setResolvedPhotoUri(String resolvedPhotoUri) {
124f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            mResolvedPhotoUri = resolvedPhotoUri;
125f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            return this;
126f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        }
127f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
128928a39fb533255b34145285fabbd4f51961df63aAndrew Sapperstein        /**
129928a39fb533255b34145285fabbd4f51961df63aAndrew Sapperstein         * Sets the URI for a thumbnail preview of the photo.
130928a39fb533255b34145285fabbd4f51961df63aAndrew Sapperstein         */
131928a39fb533255b34145285fabbd4f51961df63aAndrew Sapperstein        public PhotoViewIntentBuilder setThumbnailUri(String thumbnailUri) {
132928a39fb533255b34145285fabbd4f51961df63aAndrew Sapperstein            mThumbnailUri = thumbnailUri;
133928a39fb533255b34145285fabbd4f51961df63aAndrew Sapperstein            return this;
134928a39fb533255b34145285fabbd4f51961df63aAndrew Sapperstein        }
135928a39fb533255b34145285fabbd4f51961df63aAndrew Sapperstein
13691dbfd25cc234de393ae22fc39a832a6335e1bc2Adam Copp        /**
13791dbfd25cc234de393ae22fc39a832a6335e1bc2Adam Copp         * Sets the maximum scale which an image is initially displayed at
13891dbfd25cc234de393ae22fc39a832a6335e1bc2Adam Copp         */
13991dbfd25cc234de393ae22fc39a832a6335e1bc2Adam Copp        public PhotoViewIntentBuilder setMaxInitialScale(float maxScale) {
14091dbfd25cc234de393ae22fc39a832a6335e1bc2Adam Copp            mMaxInitialScale = maxScale;
14191dbfd25cc234de393ae22fc39a832a6335e1bc2Adam Copp            return this;
14291dbfd25cc234de393ae22fc39a832a6335e1bc2Adam Copp        }
14391dbfd25cc234de393ae22fc39a832a6335e1bc2Adam Copp
1447732839c0cd9d87954a07e166b52b93cabea1374Adam Copp        /**
1457732839c0cd9d87954a07e166b52b93cabea1374Adam Copp         * Enable watching the network for connectivity changes.
1467732839c0cd9d87954a07e166b52b93cabea1374Adam Copp         *
1477732839c0cd9d87954a07e166b52b93cabea1374Adam Copp         * When a change is detected, bitmap loaders will be restarted if required.
1487732839c0cd9d87954a07e166b52b93cabea1374Adam Copp         */
1497732839c0cd9d87954a07e166b52b93cabea1374Adam Copp        public PhotoViewIntentBuilder watchNetworkConnectivityChanges() {
1507732839c0cd9d87954a07e166b52b93cabea1374Adam Copp            mWatchNetwork = true;
1517732839c0cd9d87954a07e166b52b93cabea1374Adam Copp            return this;
1527732839c0cd9d87954a07e166b52b93cabea1374Adam Copp        }
1537732839c0cd9d87954a07e166b52b93cabea1374Adam Copp
154f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        /** Build the intent */
155f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        public Intent build() {
156f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            mIntent.setAction(Intent.ACTION_VIEW);
157f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
158f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            mIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
159f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
160f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            if (mPhotoIndex != null) {
161f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein                mIntent.putExtra(EXTRA_PHOTO_INDEX, (int) mPhotoIndex);
162f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            }
163f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
16499f6f3ea0e43d5ac96344a987735fc39485f8f05Martin Hibdon            if (mInitialPhotoUri != null) {
16599f6f3ea0e43d5ac96344a987735fc39485f8f05Martin Hibdon                mIntent.putExtra(EXTRA_INITIAL_PHOTO_URI, mInitialPhotoUri);
16699f6f3ea0e43d5ac96344a987735fc39485f8f05Martin Hibdon            }
16799f6f3ea0e43d5ac96344a987735fc39485f8f05Martin Hibdon
16899f6f3ea0e43d5ac96344a987735fc39485f8f05Martin Hibdon            if (mInitialPhotoUri != null && mPhotoIndex != null) {
16999f6f3ea0e43d5ac96344a987735fc39485f8f05Martin Hibdon                throw new IllegalStateException(
17099f6f3ea0e43d5ac96344a987735fc39485f8f05Martin Hibdon                        "specified both photo index and photo uri");
17199f6f3ea0e43d5ac96344a987735fc39485f8f05Martin Hibdon            }
17299f6f3ea0e43d5ac96344a987735fc39485f8f05Martin Hibdon
173f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            if (mPhotosUri != null) {
174f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein                mIntent.putExtra(EXTRA_PHOTOS_URI, mPhotosUri);
175f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            }
176f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
177f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            if (mResolvedPhotoUri != null) {
178f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein                mIntent.putExtra(EXTRA_RESOLVED_PHOTO_URI, mResolvedPhotoUri);
179f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            }
180f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
181f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            if (mProjection != null) {
182f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein                mIntent.putExtra(EXTRA_PROJECTION, mProjection);
183f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            }
184f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein
185928a39fb533255b34145285fabbd4f51961df63aAndrew Sapperstein            if (mThumbnailUri != null) {
186928a39fb533255b34145285fabbd4f51961df63aAndrew Sapperstein                mIntent.putExtra(EXTRA_THUMBNAIL_URI, mThumbnailUri);
187928a39fb533255b34145285fabbd4f51961df63aAndrew Sapperstein            }
188928a39fb533255b34145285fabbd4f51961df63aAndrew Sapperstein
18991dbfd25cc234de393ae22fc39a832a6335e1bc2Adam Copp            if (mMaxInitialScale != null) {
19091dbfd25cc234de393ae22fc39a832a6335e1bc2Adam Copp                mIntent.putExtra(EXTRA_MAX_INITIAL_SCALE, mMaxInitialScale);
19191dbfd25cc234de393ae22fc39a832a6335e1bc2Adam Copp            }
19291dbfd25cc234de393ae22fc39a832a6335e1bc2Adam Copp
1937732839c0cd9d87954a07e166b52b93cabea1374Adam Copp            if (mWatchNetwork == true) {
1947732839c0cd9d87954a07e166b52b93cabea1374Adam Copp                mIntent.putExtra(EXTRA_WATCH_NETWORK, true);
1957732839c0cd9d87954a07e166b52b93cabea1374Adam Copp            }
1967732839c0cd9d87954a07e166b52b93cabea1374Adam Copp
197f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein            return mIntent;
198f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein        }
199f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein    }
200f77a7eb196d16110c7b1087352b423913821ff61Andrew Sapperstein}
201