Gallery.java revision ba65c373f244f6de0b45afa6b7cd711866f1bfe4
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.view.Window;
30import android.widget.Toast;
31
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) return type;
122
123        Uri uri = intent.getData();
124        try {
125            return getContentResolver().getType(uri);
126        } catch (Throwable t) {
127            Log.w(TAG, "get type fail", t);
128            return null;
129        }
130    }
131
132    private void startViewAction(Intent intent) {
133        Boolean slideshow = intent.getBooleanExtra(EXTRA_SLIDESHOW, false);
134        if (slideshow) {
135            getActionBar().hide();
136            DataManager manager = getDataManager();
137            Path path = manager.findPathByUri(intent.getData(), intent.getType());
138            if (path == null || manager.getMediaObject(path)
139                    instanceof MediaItem) {
140                path = Path.fromString(
141                        manager.getTopSetPath(DataManager.INCLUDE_IMAGE));
142            }
143            Bundle data = new Bundle();
144            data.putString(SlideshowPage.KEY_SET_PATH, path.toString());
145            data.putBoolean(SlideshowPage.KEY_RANDOM_ORDER, true);
146            data.putBoolean(SlideshowPage.KEY_REPEAT, true);
147            if (intent.getBooleanExtra(EXTRA_DREAM, false)) {
148                data.putBoolean(SlideshowPage.KEY_DREAM, true);
149            }
150            getStateManager().startState(SlideshowPage.class, data);
151        } else {
152            Bundle data = new Bundle();
153            DataManager dm = getDataManager();
154            Uri uri = intent.getData();
155            String contentType = getContentType(intent);
156            if (contentType == null) {
157                Toast.makeText(this,
158                        R.string.no_such_item, Toast.LENGTH_LONG).show();
159                finish();
160                return;
161            }
162            if (uri == null) {
163                int typeBits = GalleryUtils.determineTypeBits(this, intent);
164                data.putInt(KEY_TYPE_BITS, typeBits);
165                data.putString(AlbumSetPage.KEY_MEDIA_PATH,
166                        getDataManager().getTopSetPath(typeBits));
167                getStateManager().startState(AlbumSetPage.class, data);
168            } else if (contentType.startsWith(
169                    ContentResolver.CURSOR_DIR_BASE_TYPE)) {
170                int mediaType = intent.getIntExtra(KEY_MEDIA_TYPES, 0);
171                if (mediaType != 0) {
172                    uri = uri.buildUpon().appendQueryParameter(
173                            KEY_MEDIA_TYPES, String.valueOf(mediaType))
174                            .build();
175                }
176                Path setPath = dm.findPathByUri(uri, null);
177                MediaSet mediaSet = null;
178                if (setPath != null) {
179                    mediaSet = (MediaSet) dm.getMediaObject(setPath);
180                }
181                if (mediaSet != null) {
182                    if (mediaSet.isLeafAlbum()) {
183                        data.putString(AlbumPage.KEY_MEDIA_PATH, setPath.toString());
184                        data.putString(AlbumPage.KEY_PARENT_MEDIA_PATH,
185                                dm.getTopSetPath(DataManager.INCLUDE_ALL));
186                        getStateManager().startState(AlbumPage.class, data);
187                    } else {
188                        data.putString(AlbumSetPage.KEY_MEDIA_PATH, setPath.toString());
189                        getStateManager().startState(AlbumSetPage.class, data);
190                    }
191                } else {
192                    startDefaultPage();
193                }
194            } else {
195                Path itemPath = dm.findPathByUri(uri, intent.getType());
196                Path albumPath = dm.getDefaultSetOf(itemPath);
197
198                data.putString(PhotoPage.KEY_MEDIA_ITEM_PATH, itemPath.toString());
199
200                // TODO: Make the parameter "SingleItemOnly" public so other
201                //       activities can reference it.
202                boolean singleItemOnly = (albumPath == null)
203                        || intent.getBooleanExtra("SingleItemOnly", false);
204                if (!singleItemOnly) {
205                    data.putString(PhotoPage.KEY_MEDIA_SET_PATH, albumPath.toString());
206                    // when FLAG_ACTIVITY_NEW_TASK is set, (e.g. when intent is fired
207                    // from notification), back button should behave the same as up button
208                    // rather than taking users back to the home screen
209                    if (intent.getBooleanExtra(PhotoPage.KEY_TREAT_BACK_AS_UP, false)
210                            || ((intent.getFlags() & Intent.FLAG_ACTIVITY_NEW_TASK) != 0)) {
211                        data.putBoolean(PhotoPage.KEY_TREAT_BACK_AS_UP, true);
212                    }
213                }
214
215                getStateManager().startState(PhotoPage.class, data);
216            }
217        }
218    }
219
220    @Override
221    protected void onResume() {
222        Utils.assertTrue(getStateManager().getStateCount() > 0);
223        super.onResume();
224        if (mVersionCheckDialog != null) {
225            mVersionCheckDialog.show();
226        }
227    }
228
229    @Override
230    protected void onPause() {
231        super.onPause();
232        if (mVersionCheckDialog != null) {
233            mVersionCheckDialog.dismiss();
234        }
235    }
236
237    @Override
238    public void onCancel(DialogInterface dialog) {
239        if (dialog == mVersionCheckDialog) {
240            mVersionCheckDialog = null;
241        }
242    }
243}
244