Gallery.java revision cfa7fde1a336a3200b2ad9091b27ba7bfd8f5c9a
1/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.gallery3d.app;
18
19import android.app.Dialog;
20import android.content.AsyncQueryHandler;
21import android.content.ContentResolver;
22import android.content.DialogInterface;
23import android.content.DialogInterface.OnCancelListener;
24import android.content.Intent;
25import android.database.Cursor;
26import android.net.Uri;
27import android.os.Bundle;
28import android.provider.OpenableColumns;
29import android.widget.Toast;
30
31import com.actionbarsherlock.view.Window;
32import com.android.gallery3d.R;
33import com.android.gallery3d.common.Utils;
34import com.android.gallery3d.data.DataManager;
35import com.android.gallery3d.data.MediaItem;
36import com.android.gallery3d.data.MediaSet;
37import com.android.gallery3d.data.Path;
38import com.android.gallery3d.picasasource.PicasaSource;
39import com.android.gallery3d.util.GalleryUtils;
40
41public final class Gallery extends AbstractGalleryActivity implements OnCancelListener {
42    public static final String EXTRA_SLIDESHOW = "slideshow";
43    public static final String EXTRA_DREAM = "dream";
44    public static final String EXTRA_CROP = "crop";
45
46    public static final String ACTION_REVIEW = "com.android.camera.action.REVIEW";
47    public static final String KEY_GET_CONTENT = "get-content";
48    public static final String KEY_GET_ALBUM = "get-album";
49    public static final String KEY_TYPE_BITS = "type-bits";
50    public static final String KEY_MEDIA_TYPES = "mediaTypes";
51
52    private static final String TAG = "Gallery";
53    private Dialog mVersionCheckDialog;
54
55    @Override
56    protected void onCreate(Bundle savedInstanceState) {
57        super.onCreate(savedInstanceState);
58        requestWindowFeature(Window.FEATURE_ACTION_BAR);
59        requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
60
61        setContentView(R.layout.main);
62
63        if (savedInstanceState != null) {
64            getStateManager().restoreFromState(savedInstanceState);
65        } else {
66            initializeByIntent();
67        }
68    }
69
70    private void initializeByIntent() {
71        Intent intent = getIntent();
72        String action = intent.getAction();
73
74        if (Intent.ACTION_GET_CONTENT.equalsIgnoreCase(action)) {
75            startGetContent(intent);
76        } else if (Intent.ACTION_PICK.equalsIgnoreCase(action)) {
77            // We do NOT really support the PICK intent. Handle it as
78            // the GET_CONTENT. However, we need to translate the type
79            // in the intent here.
80            Log.w(TAG, "action PICK is not supported");
81            String type = Utils.ensureNotNull(intent.getType());
82            if (type.startsWith("vnd.android.cursor.dir/")) {
83                if (type.endsWith("/image")) intent.setType("image/*");
84                if (type.endsWith("/video")) intent.setType("video/*");
85            }
86            startGetContent(intent);
87        } else if (Intent.ACTION_VIEW.equalsIgnoreCase(action)
88                || ACTION_REVIEW.equalsIgnoreCase(action)){
89            startViewAction(intent);
90        } else {
91            startDefaultPage();
92        }
93    }
94
95    public void startDefaultPage() {
96        PicasaSource.showSignInReminder(this);
97        Bundle data = new Bundle();
98        data.putString(AlbumSetPage.KEY_MEDIA_PATH,
99                getDataManager().getTopSetPath(DataManager.INCLUDE_ALL));
100        getStateManager().startState(AlbumSetPage.class, data);
101        mVersionCheckDialog = PicasaSource.getVersionCheckDialog(this);
102        if (mVersionCheckDialog != null) {
103            mVersionCheckDialog.setOnCancelListener(this);
104        }
105    }
106
107    private void startGetContent(Intent intent) {
108        Bundle data = intent.getExtras() != null
109                ? new Bundle(intent.getExtras())
110                : new Bundle();
111        data.putBoolean(KEY_GET_CONTENT, true);
112        int typeBits = GalleryUtils.determineTypeBits(this, intent);
113        data.putInt(KEY_TYPE_BITS, typeBits);
114        data.putString(AlbumSetPage.KEY_MEDIA_PATH,
115                getDataManager().getTopSetPath(typeBits));
116        getStateManager().startState(AlbumSetPage.class, data);
117    }
118
119    private String getContentType(Intent intent) {
120        String type = intent.getType();
121        if (type != null) {
122            return GalleryUtils.MIME_TYPE_PANORAMA360.equals(type)
123                ? MediaItem.MIME_TYPE_JPEG : type;
124        }
125
126        Uri uri = intent.getData();
127        try {
128            return getContentResolver().getType(uri);
129        } catch (Throwable t) {
130            Log.w(TAG, "get type fail", t);
131            return null;
132        }
133    }
134
135    private void startViewAction(Intent intent) {
136        Boolean slideshow = intent.getBooleanExtra(EXTRA_SLIDESHOW, false);
137        if (slideshow) {
138            getActionBar().hide();
139            DataManager manager = getDataManager();
140            Path path = manager.findPathByUri(intent.getData(), intent.getType());
141            if (path == null || manager.getMediaObject(path)
142                    instanceof MediaItem) {
143                path = Path.fromString(
144                        manager.getTopSetPath(DataManager.INCLUDE_IMAGE));
145            }
146            Bundle data = new Bundle();
147            data.putString(SlideshowPage.KEY_SET_PATH, path.toString());
148            data.putBoolean(SlideshowPage.KEY_RANDOM_ORDER, true);
149            data.putBoolean(SlideshowPage.KEY_REPEAT, true);
150            if (intent.getBooleanExtra(EXTRA_DREAM, false)) {
151                data.putBoolean(SlideshowPage.KEY_DREAM, true);
152            }
153            getStateManager().startState(SlideshowPage.class, data);
154        } else {
155            Bundle data = new Bundle();
156            DataManager dm = getDataManager();
157            Uri uri = intent.getData();
158            String contentType = getContentType(intent);
159            if (contentType == null) {
160                Toast.makeText(this,
161                        R.string.no_such_item, Toast.LENGTH_LONG).show();
162                finish();
163                return;
164            }
165            if (uri == null) {
166                int typeBits = GalleryUtils.determineTypeBits(this, intent);
167                data.putInt(KEY_TYPE_BITS, typeBits);
168                data.putString(AlbumSetPage.KEY_MEDIA_PATH,
169                        getDataManager().getTopSetPath(typeBits));
170                getStateManager().startState(AlbumSetPage.class, data);
171            } else if (contentType.startsWith(
172                    ContentResolver.CURSOR_DIR_BASE_TYPE)) {
173                int mediaType = intent.getIntExtra(KEY_MEDIA_TYPES, 0);
174                if (mediaType != 0) {
175                    uri = uri.buildUpon().appendQueryParameter(
176                            KEY_MEDIA_TYPES, String.valueOf(mediaType))
177                            .build();
178                }
179                Path setPath = dm.findPathByUri(uri, null);
180                MediaSet mediaSet = null;
181                if (setPath != null) {
182                    mediaSet = (MediaSet) dm.getMediaObject(setPath);
183                }
184                if (mediaSet != null) {
185                    if (mediaSet.isLeafAlbum()) {
186                        data.putString(AlbumPage.KEY_MEDIA_PATH, setPath.toString());
187                        data.putString(AlbumPage.KEY_PARENT_MEDIA_PATH,
188                                dm.getTopSetPath(DataManager.INCLUDE_ALL));
189                        getStateManager().startState(AlbumPage.class, data);
190                    } else {
191                        data.putString(AlbumSetPage.KEY_MEDIA_PATH, setPath.toString());
192                        getStateManager().startState(AlbumSetPage.class, data);
193                    }
194                } else {
195                    startDefaultPage();
196                }
197            } else {
198                Path itemPath = dm.findPathByUri(uri, contentType);
199                Path albumPath = dm.getDefaultSetOf(itemPath);
200
201                data.putString(PhotoPage.KEY_MEDIA_ITEM_PATH, itemPath.toString());
202
203                // TODO: Make the parameter "SingleItemOnly" public so other
204                //       activities can reference it.
205                boolean singleItemOnly = (albumPath == null)
206                        || intent.getBooleanExtra("SingleItemOnly", false);
207                if (!singleItemOnly) {
208                    data.putString(PhotoPage.KEY_MEDIA_SET_PATH, albumPath.toString());
209                    // when FLAG_ACTIVITY_NEW_TASK is set, (e.g. when intent is fired
210                    // from notification), back button should behave the same as up button
211                    // rather than taking users back to the home screen
212                    if (intent.getBooleanExtra(PhotoPage.KEY_TREAT_BACK_AS_UP, false)
213                            || ((intent.getFlags() & Intent.FLAG_ACTIVITY_NEW_TASK) != 0)) {
214                        data.putBoolean(PhotoPage.KEY_TREAT_BACK_AS_UP, true);
215                    }
216                }
217
218                getStateManager().startState(SinglePhotoPage.class, data);
219            }
220        }
221    }
222
223    @Override
224    protected void onResume() {
225        Utils.assertTrue(getStateManager().getStateCount() > 0);
226        super.onResume();
227        if (mVersionCheckDialog != null) {
228            mVersionCheckDialog.show();
229        }
230    }
231
232    @Override
233    protected void onPause() {
234        super.onPause();
235        if (mVersionCheckDialog != null) {
236            mVersionCheckDialog.dismiss();
237        }
238    }
239
240    @Override
241    public void onCancel(DialogInterface dialog) {
242        if (dialog == mVersionCheckDialog) {
243            mVersionCheckDialog = null;
244        }
245    }
246}
247