blob: e3937bcd025f5f2cf638b7509ab501370c241a19 [file] [log] [blame]
Owen Lina2fba682011-08-17 22:07:43 +08001/*
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
Owen Linc74fa8c2011-11-24 11:59:52 +080019import android.app.Dialog;
Pin Ting0a5e1642012-02-07 18:11:25 +080020import android.content.AsyncQueryHandler;
Owen Lina2fba682011-08-17 22:07:43 +080021import android.content.ContentResolver;
Owen Linc74fa8c2011-11-24 11:59:52 +080022import android.content.DialogInterface;
23import android.content.DialogInterface.OnCancelListener;
Owen Lina2fba682011-08-17 22:07:43 +080024import android.content.Intent;
Pin Ting0a5e1642012-02-07 18:11:25 +080025import android.database.Cursor;
Owen Lina2fba682011-08-17 22:07:43 +080026import android.net.Uri;
27import android.os.Bundle;
Pin Ting0a5e1642012-02-07 18:11:25 +080028import android.provider.OpenableColumns;
Owen Lina2fba682011-08-17 22:07:43 +080029import android.view.Menu;
30import android.view.MenuItem;
31import android.view.Window;
32import android.widget.Toast;
33
Ray Chen73e791c2011-10-04 15:19:44 +080034import com.android.gallery3d.R;
35import com.android.gallery3d.common.Utils;
36import com.android.gallery3d.data.DataManager;
37import com.android.gallery3d.data.MediaItem;
38import com.android.gallery3d.data.MediaSet;
39import com.android.gallery3d.data.Path;
40import com.android.gallery3d.picasasource.PicasaSource;
41import com.android.gallery3d.ui.GLRoot;
42import com.android.gallery3d.util.GalleryUtils;
43
Owen Linc74fa8c2011-11-24 11:59:52 +080044public final class Gallery extends AbstractGalleryActivity implements OnCancelListener {
Owen Lina2fba682011-08-17 22:07:43 +080045 public static final String EXTRA_SLIDESHOW = "slideshow";
46 public static final String EXTRA_CROP = "crop";
47
48 public static final String ACTION_REVIEW = "com.android.camera.action.REVIEW";
49 public static final String KEY_GET_CONTENT = "get-content";
50 public static final String KEY_GET_ALBUM = "get-album";
51 public static final String KEY_TYPE_BITS = "type-bits";
52 public static final String KEY_MEDIA_TYPES = "mediaTypes";
53
54 private static final String TAG = "Gallery";
Owen Linc74fa8c2011-11-24 11:59:52 +080055 private Dialog mVersionCheckDialog;
Owen Lina2fba682011-08-17 22:07:43 +080056
57 @Override
58 protected void onCreate(Bundle savedInstanceState) {
59 super.onCreate(savedInstanceState);
60 requestWindowFeature(Window.FEATURE_ACTION_BAR);
61 requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
62 requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
63
64 setContentView(R.layout.main);
Owen Lina2fba682011-08-17 22:07:43 +080065
66 if (savedInstanceState != null) {
67 getStateManager().restoreFromState(savedInstanceState);
68 } else {
69 initializeByIntent();
70 }
71 }
72
73 private void initializeByIntent() {
74 Intent intent = getIntent();
75 String action = intent.getAction();
76
77 if (Intent.ACTION_GET_CONTENT.equalsIgnoreCase(action)) {
78 startGetContent(intent);
79 } else if (Intent.ACTION_PICK.equalsIgnoreCase(action)) {
80 // We do NOT really support the PICK intent. Handle it as
81 // the GET_CONTENT. However, we need to translate the type
82 // in the intent here.
83 Log.w(TAG, "action PICK is not supported");
84 String type = Utils.ensureNotNull(intent.getType());
85 if (type.startsWith("vnd.android.cursor.dir/")) {
86 if (type.endsWith("/image")) intent.setType("image/*");
87 if (type.endsWith("/video")) intent.setType("video/*");
88 }
89 startGetContent(intent);
90 } else if (Intent.ACTION_VIEW.equalsIgnoreCase(action)
91 || ACTION_REVIEW.equalsIgnoreCase(action)){
92 startViewAction(intent);
93 } else {
94 startDefaultPage();
95 }
96 }
97
98 public void startDefaultPage() {
Ray Chen73e791c2011-10-04 15:19:44 +080099 PicasaSource.showSignInReminder(this);
Owen Lina2fba682011-08-17 22:07:43 +0800100 Bundle data = new Bundle();
101 data.putString(AlbumSetPage.KEY_MEDIA_PATH,
102 getDataManager().getTopSetPath(DataManager.INCLUDE_ALL));
103 getStateManager().startState(AlbumSetPage.class, data);
Owen Linc74fa8c2011-11-24 11:59:52 +0800104 mVersionCheckDialog = PicasaSource.getVersionCheckDialog(this);
105 if (mVersionCheckDialog != null) {
106 mVersionCheckDialog.setOnCancelListener(this);
107 }
Owen Lina2fba682011-08-17 22:07:43 +0800108 }
109
110 private void startGetContent(Intent intent) {
111 Bundle data = intent.getExtras() != null
112 ? new Bundle(intent.getExtras())
113 : new Bundle();
114 data.putBoolean(KEY_GET_CONTENT, true);
115 int typeBits = GalleryUtils.determineTypeBits(this, intent);
116 data.putInt(KEY_TYPE_BITS, typeBits);
117 data.putString(AlbumSetPage.KEY_MEDIA_PATH,
118 getDataManager().getTopSetPath(typeBits));
119 getStateManager().startState(AlbumSetPage.class, data);
120 }
121
122 private String getContentType(Intent intent) {
123 String type = intent.getType();
124 if (type != null) return type;
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);
Owen Lina2fba682011-08-17 22:07:43 +0800137 if (slideshow) {
138 getActionBar().hide();
139 DataManager manager = getDataManager();
140 Path path = manager.findPathByUri(intent.getData());
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 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 }
Chih-Chung Changfff46c02011-10-18 19:54:31 +0800162 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));
Chih-Chung Changfff46c02011-10-18 19:54:31 +0800167 getStateManager().startState(AlbumSetPage.class, data);
168 } else if (contentType.startsWith(
Owen Lina2fba682011-08-17 22:07:43 +0800169 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 }
Chih-Chung Changfff46c02011-10-18 19:54:31 +0800176 Path setPath = dm.findPathByUri(uri);
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());
Ray Chenf3f7f562012-03-06 17:24:28 +0800184 data.putString(AlbumPage.KEY_PARENT_MEDIA_PATH,
185 dm.getTopSetPath(DataManager.INCLUDE_ALL));
Chih-Chung Changfff46c02011-10-18 19:54:31 +0800186 getStateManager().startState(AlbumPage.class, data);
187 } else {
188 data.putString(AlbumSetPage.KEY_MEDIA_PATH, setPath.toString());
189 getStateManager().startState(AlbumSetPage.class, data);
190 }
Owen Lina2fba682011-08-17 22:07:43 +0800191 } else {
192 startDefaultPage();
193 }
194 } else {
195 Path itemPath = dm.findPathByUri(uri);
196 Path albumPath = dm.getDefaultSetOf(itemPath);
Ray Chen1ce57c42011-10-19 18:30:48 +0800197 // TODO: Make this parameter public so other activities can reference it.
198 boolean singleItemOnly = intent.getBooleanExtra("SingleItemOnly", false);
Pin Ting0a5e1642012-02-07 18:11:25 +0800199 if (!singleItemOnly && (albumPath != null)) {
200 data.putString(PhotoPage.KEY_MEDIA_SET_PATH, albumPath.toString());
Owen Lina2fba682011-08-17 22:07:43 +0800201 }
202 data.putString(PhotoPage.KEY_MEDIA_ITEM_PATH, itemPath.toString());
Pin Ting22c0aec2012-01-18 17:19:42 +0800203
Pin Ting0a5e1642012-02-07 18:11:25 +0800204 // Displays the filename as title, reading the filename from the interface:
205 // {@link android.provider.OpenableColumns#DISPLAY_NAME}.
206 AsyncQueryHandler queryHandler = new AsyncQueryHandler(getContentResolver()) {
207 @Override
208 protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
209 try {
210 if ((cursor != null) && cursor.moveToFirst()) {
211 String displayName = cursor.getString(0);
212
213 // Just show empty title if other apps don't set DISPLAY_NAME
214 setTitle((displayName == null) ? "" : displayName);
Pin Ting0a5e1642012-02-07 18:11:25 +0800215 }
216 } finally {
217 Utils.closeSilently(cursor);
218 }
219 }
220 };
221 queryHandler.startQuery(0, null, uri, new String[] {OpenableColumns.DISPLAY_NAME},
222 null, null, null);
Pin Ting22c0aec2012-01-18 17:19:42 +0800223
Owen Lina2fba682011-08-17 22:07:43 +0800224 getStateManager().startState(PhotoPage.class, data);
225 }
226 }
227 }
228
229 @Override
230 public boolean onCreateOptionsMenu(Menu menu) {
231 super.onCreateOptionsMenu(menu);
232 return getStateManager().createOptionsMenu(menu);
233 }
234
235 @Override
Owen Lina2fba682011-08-17 22:07:43 +0800236 public void onBackPressed() {
237 // send the back event to the top sub-state
238 GLRoot root = getGLRoot();
239 root.lockRenderThread();
240 try {
241 getStateManager().onBackPressed();
242 } finally {
243 root.unlockRenderThread();
244 }
245 }
246
247 @Override
248 public void onDestroy() {
249 super.onDestroy();
250 GLRoot root = getGLRoot();
251 root.lockRenderThread();
252 try {
253 getStateManager().destroy();
254 } finally {
255 root.unlockRenderThread();
256 }
257 }
258
259 @Override
260 protected void onResume() {
261 Utils.assertTrue(getStateManager().getStateCount() > 0);
262 super.onResume();
Owen Linc74fa8c2011-11-24 11:59:52 +0800263 if (mVersionCheckDialog != null) {
264 mVersionCheckDialog.show();
265 }
266 }
267
268 @Override
269 protected void onPause() {
270 super.onPause();
271 if (mVersionCheckDialog != null) {
272 mVersionCheckDialog.dismiss();
273 }
Owen Lina2fba682011-08-17 22:07:43 +0800274 }
275
276 @Override
Owen Linc74fa8c2011-11-24 11:59:52 +0800277 public void onCancel(DialogInterface dialog) {
278 if (dialog == mVersionCheckDialog) {
279 mVersionCheckDialog = null;
280 }
281 }
Owen Lina2fba682011-08-17 22:07:43 +0800282}