Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 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 | |
| 17 | package com.android.gallery3d.app; |
| 18 | |
Owen Lin | fe49be4 | 2011-11-24 11:59:52 +0800 | [diff] [blame] | 19 | import android.app.Dialog; |
Pin Ting | 1d33c69 | 2012-02-07 18:11:25 +0800 | [diff] [blame] | 20 | import android.content.AsyncQueryHandler; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 21 | import android.content.ContentResolver; |
Owen Lin | fe49be4 | 2011-11-24 11:59:52 +0800 | [diff] [blame] | 22 | import android.content.DialogInterface; |
| 23 | import android.content.DialogInterface.OnCancelListener; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 24 | import android.content.Intent; |
Pin Ting | 1d33c69 | 2012-02-07 18:11:25 +0800 | [diff] [blame] | 25 | import android.database.Cursor; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 26 | import android.net.Uri; |
| 27 | import android.os.Bundle; |
Pin Ting | 1d33c69 | 2012-02-07 18:11:25 +0800 | [diff] [blame] | 28 | import android.provider.OpenableColumns; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 29 | import android.view.Menu; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 30 | import android.view.Window; |
| 31 | import android.widget.Toast; |
| 32 | |
Ray Chen | dbf4ba1 | 2011-10-04 15:19:44 +0800 | [diff] [blame] | 33 | import com.android.gallery3d.R; |
| 34 | import com.android.gallery3d.common.Utils; |
| 35 | import com.android.gallery3d.data.DataManager; |
| 36 | import com.android.gallery3d.data.MediaItem; |
| 37 | import com.android.gallery3d.data.MediaSet; |
| 38 | import com.android.gallery3d.data.Path; |
| 39 | import com.android.gallery3d.picasasource.PicasaSource; |
| 40 | import com.android.gallery3d.ui.GLRoot; |
| 41 | import com.android.gallery3d.util.GalleryUtils; |
| 42 | |
Owen Lin | fe49be4 | 2011-11-24 11:59:52 +0800 | [diff] [blame] | 43 | public final class Gallery extends AbstractGalleryActivity implements OnCancelListener { |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 44 | public static final String EXTRA_SLIDESHOW = "slideshow"; |
Yuli Huang | afb3d1d | 2012-05-28 23:39:27 +0800 | [diff] [blame^] | 45 | public static final String EXTRA_DREAM = "dream"; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 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 Lin | fe49be4 | 2011-11-24 11:59:52 +0800 | [diff] [blame] | 55 | private Dialog mVersionCheckDialog; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 56 | |
| 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); |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 62 | |
| 63 | setContentView(R.layout.main); |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 64 | |
| 65 | if (savedInstanceState != null) { |
| 66 | getStateManager().restoreFromState(savedInstanceState); |
| 67 | } else { |
| 68 | initializeByIntent(); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | private void initializeByIntent() { |
| 73 | Intent intent = getIntent(); |
| 74 | String action = intent.getAction(); |
| 75 | |
| 76 | if (Intent.ACTION_GET_CONTENT.equalsIgnoreCase(action)) { |
| 77 | startGetContent(intent); |
| 78 | } else if (Intent.ACTION_PICK.equalsIgnoreCase(action)) { |
| 79 | // We do NOT really support the PICK intent. Handle it as |
| 80 | // the GET_CONTENT. However, we need to translate the type |
| 81 | // in the intent here. |
| 82 | Log.w(TAG, "action PICK is not supported"); |
| 83 | String type = Utils.ensureNotNull(intent.getType()); |
| 84 | if (type.startsWith("vnd.android.cursor.dir/")) { |
| 85 | if (type.endsWith("/image")) intent.setType("image/*"); |
| 86 | if (type.endsWith("/video")) intent.setType("video/*"); |
| 87 | } |
| 88 | startGetContent(intent); |
| 89 | } else if (Intent.ACTION_VIEW.equalsIgnoreCase(action) |
| 90 | || ACTION_REVIEW.equalsIgnoreCase(action)){ |
| 91 | startViewAction(intent); |
| 92 | } else { |
| 93 | startDefaultPage(); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | public void startDefaultPage() { |
Ray Chen | dbf4ba1 | 2011-10-04 15:19:44 +0800 | [diff] [blame] | 98 | PicasaSource.showSignInReminder(this); |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 99 | Bundle data = new Bundle(); |
| 100 | data.putString(AlbumSetPage.KEY_MEDIA_PATH, |
| 101 | getDataManager().getTopSetPath(DataManager.INCLUDE_ALL)); |
| 102 | getStateManager().startState(AlbumSetPage.class, data); |
Owen Lin | fe49be4 | 2011-11-24 11:59:52 +0800 | [diff] [blame] | 103 | mVersionCheckDialog = PicasaSource.getVersionCheckDialog(this); |
| 104 | if (mVersionCheckDialog != null) { |
| 105 | mVersionCheckDialog.setOnCancelListener(this); |
| 106 | } |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | private void startGetContent(Intent intent) { |
| 110 | Bundle data = intent.getExtras() != null |
| 111 | ? new Bundle(intent.getExtras()) |
| 112 | : new Bundle(); |
| 113 | data.putBoolean(KEY_GET_CONTENT, true); |
| 114 | int typeBits = GalleryUtils.determineTypeBits(this, intent); |
| 115 | data.putInt(KEY_TYPE_BITS, typeBits); |
| 116 | data.putString(AlbumSetPage.KEY_MEDIA_PATH, |
| 117 | getDataManager().getTopSetPath(typeBits)); |
| 118 | getStateManager().startState(AlbumSetPage.class, data); |
| 119 | } |
| 120 | |
| 121 | private String getContentType(Intent intent) { |
| 122 | String type = intent.getType(); |
| 123 | if (type != null) return type; |
| 124 | |
| 125 | Uri uri = intent.getData(); |
| 126 | try { |
| 127 | return getContentResolver().getType(uri); |
| 128 | } catch (Throwable t) { |
| 129 | Log.w(TAG, "get type fail", t); |
| 130 | return null; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | private void startViewAction(Intent intent) { |
| 135 | Boolean slideshow = intent.getBooleanExtra(EXTRA_SLIDESHOW, false); |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 136 | if (slideshow) { |
| 137 | getActionBar().hide(); |
| 138 | DataManager manager = getDataManager(); |
Owen Lin | 21a412c | 2012-04-20 18:42:22 +0800 | [diff] [blame] | 139 | Path path = manager.findPathByUri(intent.getData(), intent.getType()); |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 140 | if (path == null || manager.getMediaObject(path) |
| 141 | instanceof MediaItem) { |
| 142 | path = Path.fromString( |
| 143 | manager.getTopSetPath(DataManager.INCLUDE_IMAGE)); |
| 144 | } |
| 145 | Bundle data = new Bundle(); |
| 146 | data.putString(SlideshowPage.KEY_SET_PATH, path.toString()); |
| 147 | data.putBoolean(SlideshowPage.KEY_RANDOM_ORDER, true); |
| 148 | data.putBoolean(SlideshowPage.KEY_REPEAT, true); |
Yuli Huang | afb3d1d | 2012-05-28 23:39:27 +0800 | [diff] [blame^] | 149 | if (intent.getBooleanExtra(EXTRA_DREAM, false)) { |
| 150 | data.putBoolean(SlideshowPage.KEY_DREAM, true); |
| 151 | } |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 152 | getStateManager().startState(SlideshowPage.class, data); |
| 153 | } else { |
| 154 | Bundle data = new Bundle(); |
| 155 | DataManager dm = getDataManager(); |
| 156 | Uri uri = intent.getData(); |
| 157 | String contentType = getContentType(intent); |
| 158 | if (contentType == null) { |
| 159 | Toast.makeText(this, |
| 160 | R.string.no_such_item, Toast.LENGTH_LONG).show(); |
| 161 | finish(); |
| 162 | return; |
| 163 | } |
Chih-Chung Chang | bd47a5c | 2011-10-18 19:54:31 +0800 | [diff] [blame] | 164 | if (uri == null) { |
| 165 | int typeBits = GalleryUtils.determineTypeBits(this, intent); |
| 166 | data.putInt(KEY_TYPE_BITS, typeBits); |
| 167 | data.putString(AlbumSetPage.KEY_MEDIA_PATH, |
| 168 | getDataManager().getTopSetPath(typeBits)); |
Chih-Chung Chang | bd47a5c | 2011-10-18 19:54:31 +0800 | [diff] [blame] | 169 | getStateManager().startState(AlbumSetPage.class, data); |
| 170 | } else if (contentType.startsWith( |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 171 | ContentResolver.CURSOR_DIR_BASE_TYPE)) { |
| 172 | int mediaType = intent.getIntExtra(KEY_MEDIA_TYPES, 0); |
| 173 | if (mediaType != 0) { |
| 174 | uri = uri.buildUpon().appendQueryParameter( |
| 175 | KEY_MEDIA_TYPES, String.valueOf(mediaType)) |
| 176 | .build(); |
| 177 | } |
Owen Lin | 21a412c | 2012-04-20 18:42:22 +0800 | [diff] [blame] | 178 | Path setPath = dm.findPathByUri(uri, null); |
Chih-Chung Chang | bd47a5c | 2011-10-18 19:54:31 +0800 | [diff] [blame] | 179 | MediaSet mediaSet = null; |
| 180 | if (setPath != null) { |
| 181 | mediaSet = (MediaSet) dm.getMediaObject(setPath); |
| 182 | } |
| 183 | if (mediaSet != null) { |
| 184 | if (mediaSet.isLeafAlbum()) { |
| 185 | data.putString(AlbumPage.KEY_MEDIA_PATH, setPath.toString()); |
Ray Chen | 84c220f | 2012-03-06 17:24:28 +0800 | [diff] [blame] | 186 | data.putString(AlbumPage.KEY_PARENT_MEDIA_PATH, |
| 187 | dm.getTopSetPath(DataManager.INCLUDE_ALL)); |
Chih-Chung Chang | bd47a5c | 2011-10-18 19:54:31 +0800 | [diff] [blame] | 188 | getStateManager().startState(AlbumPage.class, data); |
| 189 | } else { |
| 190 | data.putString(AlbumSetPage.KEY_MEDIA_PATH, setPath.toString()); |
| 191 | getStateManager().startState(AlbumSetPage.class, data); |
| 192 | } |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 193 | } else { |
| 194 | startDefaultPage(); |
| 195 | } |
| 196 | } else { |
Owen Lin | 21a412c | 2012-04-20 18:42:22 +0800 | [diff] [blame] | 197 | Path itemPath = dm.findPathByUri(uri, intent.getType()); |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 198 | Path albumPath = dm.getDefaultSetOf(itemPath); |
Ray Chen | c0ba093 | 2011-10-19 18:30:48 +0800 | [diff] [blame] | 199 | // TODO: Make this parameter public so other activities can reference it. |
| 200 | boolean singleItemOnly = intent.getBooleanExtra("SingleItemOnly", false); |
Pin Ting | 1d33c69 | 2012-02-07 18:11:25 +0800 | [diff] [blame] | 201 | if (!singleItemOnly && (albumPath != null)) { |
| 202 | data.putString(PhotoPage.KEY_MEDIA_SET_PATH, albumPath.toString()); |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 203 | } |
| 204 | data.putString(PhotoPage.KEY_MEDIA_ITEM_PATH, itemPath.toString()); |
Yuli Huang | 54fe620 | 2012-05-23 16:06:32 +0800 | [diff] [blame] | 205 | if (intent.getBooleanExtra(PhotoPage.KEY_TREAT_BACK_AS_UP, false)) { |
| 206 | data.putBoolean(PhotoPage.KEY_TREAT_BACK_AS_UP, true); |
| 207 | } |
Pin Ting | 186f8b2 | 2012-01-18 17:19:42 +0800 | [diff] [blame] | 208 | |
Pin Ting | 1d33c69 | 2012-02-07 18:11:25 +0800 | [diff] [blame] | 209 | // Displays the filename as title, reading the filename from the interface: |
| 210 | // {@link android.provider.OpenableColumns#DISPLAY_NAME}. |
| 211 | AsyncQueryHandler queryHandler = new AsyncQueryHandler(getContentResolver()) { |
| 212 | @Override |
| 213 | protected void onQueryComplete(int token, Object cookie, Cursor cursor) { |
| 214 | try { |
| 215 | if ((cursor != null) && cursor.moveToFirst()) { |
| 216 | String displayName = cursor.getString(0); |
| 217 | |
| 218 | // Just show empty title if other apps don't set DISPLAY_NAME |
| 219 | setTitle((displayName == null) ? "" : displayName); |
Pin Ting | 1d33c69 | 2012-02-07 18:11:25 +0800 | [diff] [blame] | 220 | } |
| 221 | } finally { |
| 222 | Utils.closeSilently(cursor); |
| 223 | } |
| 224 | } |
| 225 | }; |
| 226 | queryHandler.startQuery(0, null, uri, new String[] {OpenableColumns.DISPLAY_NAME}, |
| 227 | null, null, null); |
Pin Ting | 186f8b2 | 2012-01-18 17:19:42 +0800 | [diff] [blame] | 228 | |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 229 | getStateManager().startState(PhotoPage.class, data); |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | @Override |
| 235 | public boolean onCreateOptionsMenu(Menu menu) { |
| 236 | super.onCreateOptionsMenu(menu); |
| 237 | return getStateManager().createOptionsMenu(menu); |
| 238 | } |
| 239 | |
| 240 | @Override |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 241 | protected void onResume() { |
| 242 | Utils.assertTrue(getStateManager().getStateCount() > 0); |
| 243 | super.onResume(); |
Owen Lin | fe49be4 | 2011-11-24 11:59:52 +0800 | [diff] [blame] | 244 | if (mVersionCheckDialog != null) { |
| 245 | mVersionCheckDialog.show(); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | @Override |
| 250 | protected void onPause() { |
| 251 | super.onPause(); |
| 252 | if (mVersionCheckDialog != null) { |
| 253 | mVersionCheckDialog.dismiss(); |
| 254 | } |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | @Override |
Owen Lin | fe49be4 | 2011-11-24 11:59:52 +0800 | [diff] [blame] | 258 | public void onCancel(DialogInterface dialog) { |
| 259 | if (dialog == mVersionCheckDialog) { |
| 260 | mVersionCheckDialog = null; |
| 261 | } |
| 262 | } |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 263 | } |