Owen Lin | a2fba68 | 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 | c74fa8c | 2011-11-24 11:59:52 +0800 | [diff] [blame] | 19 | import android.app.Dialog; |
Pin Ting | 0a5e164 | 2012-02-07 18:11:25 +0800 | [diff] [blame] | 20 | import android.content.AsyncQueryHandler; |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 21 | import android.content.ContentResolver; |
Owen Lin | c74fa8c | 2011-11-24 11:59:52 +0800 | [diff] [blame] | 22 | import android.content.DialogInterface; |
| 23 | import android.content.DialogInterface.OnCancelListener; |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 24 | import android.content.Intent; |
Pin Ting | 0a5e164 | 2012-02-07 18:11:25 +0800 | [diff] [blame] | 25 | import android.database.Cursor; |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 26 | import android.net.Uri; |
| 27 | import android.os.Bundle; |
Pin Ting | 0a5e164 | 2012-02-07 18:11:25 +0800 | [diff] [blame] | 28 | import android.provider.OpenableColumns; |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 29 | import android.view.Menu; |
| 30 | import android.view.MenuItem; |
| 31 | import android.view.Window; |
| 32 | import android.widget.Toast; |
| 33 | |
Ray Chen | 73e791c | 2011-10-04 15:19:44 +0800 | [diff] [blame] | 34 | import com.android.gallery3d.R; |
| 35 | import com.android.gallery3d.common.Utils; |
| 36 | import com.android.gallery3d.data.DataManager; |
| 37 | import com.android.gallery3d.data.MediaItem; |
| 38 | import com.android.gallery3d.data.MediaSet; |
| 39 | import com.android.gallery3d.data.Path; |
| 40 | import com.android.gallery3d.picasasource.PicasaSource; |
| 41 | import com.android.gallery3d.ui.GLRoot; |
| 42 | import com.android.gallery3d.util.GalleryUtils; |
| 43 | |
Owen Lin | c74fa8c | 2011-11-24 11:59:52 +0800 | [diff] [blame] | 44 | public final class Gallery extends AbstractGalleryActivity implements OnCancelListener { |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 45 | 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 Lin | c74fa8c | 2011-11-24 11:59:52 +0800 | [diff] [blame] | 55 | private Dialog mVersionCheckDialog; |
Owen Lin | a2fba68 | 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); |
| 62 | requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); |
| 63 | |
| 64 | setContentView(R.layout.main); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 65 | |
| 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 Chen | 73e791c | 2011-10-04 15:19:44 +0800 | [diff] [blame] | 99 | PicasaSource.showSignInReminder(this); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 100 | Bundle data = new Bundle(); |
| 101 | data.putString(AlbumSetPage.KEY_MEDIA_PATH, |
| 102 | getDataManager().getTopSetPath(DataManager.INCLUDE_ALL)); |
| 103 | getStateManager().startState(AlbumSetPage.class, data); |
Owen Lin | c74fa8c | 2011-11-24 11:59:52 +0800 | [diff] [blame] | 104 | mVersionCheckDialog = PicasaSource.getVersionCheckDialog(this); |
| 105 | if (mVersionCheckDialog != null) { |
| 106 | mVersionCheckDialog.setOnCancelListener(this); |
| 107 | } |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 108 | } |
| 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 Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 137 | 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 Chang | fff46c0 | 2011-10-18 19:54:31 +0800 | [diff] [blame] | 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)); |
Chih-Chung Chang | fff46c0 | 2011-10-18 19:54:31 +0800 | [diff] [blame] | 167 | getStateManager().startState(AlbumSetPage.class, data); |
| 168 | } else if (contentType.startsWith( |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 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 | } |
Chih-Chung Chang | fff46c0 | 2011-10-18 19:54:31 +0800 | [diff] [blame] | 176 | 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 Chen | f3f7f56 | 2012-03-06 17:24:28 +0800 | [diff] [blame] | 184 | data.putString(AlbumPage.KEY_PARENT_MEDIA_PATH, |
| 185 | dm.getTopSetPath(DataManager.INCLUDE_ALL)); |
Chih-Chung Chang | fff46c0 | 2011-10-18 19:54:31 +0800 | [diff] [blame] | 186 | getStateManager().startState(AlbumPage.class, data); |
| 187 | } else { |
| 188 | data.putString(AlbumSetPage.KEY_MEDIA_PATH, setPath.toString()); |
| 189 | getStateManager().startState(AlbumSetPage.class, data); |
| 190 | } |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 191 | } else { |
| 192 | startDefaultPage(); |
| 193 | } |
| 194 | } else { |
| 195 | Path itemPath = dm.findPathByUri(uri); |
| 196 | Path albumPath = dm.getDefaultSetOf(itemPath); |
Ray Chen | 1ce57c4 | 2011-10-19 18:30:48 +0800 | [diff] [blame] | 197 | // TODO: Make this parameter public so other activities can reference it. |
| 198 | boolean singleItemOnly = intent.getBooleanExtra("SingleItemOnly", false); |
Pin Ting | 0a5e164 | 2012-02-07 18:11:25 +0800 | [diff] [blame] | 199 | if (!singleItemOnly && (albumPath != null)) { |
| 200 | data.putString(PhotoPage.KEY_MEDIA_SET_PATH, albumPath.toString()); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 201 | } |
| 202 | data.putString(PhotoPage.KEY_MEDIA_ITEM_PATH, itemPath.toString()); |
Pin Ting | 22c0aec | 2012-01-18 17:19:42 +0800 | [diff] [blame] | 203 | |
Pin Ting | 0a5e164 | 2012-02-07 18:11:25 +0800 | [diff] [blame] | 204 | // 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 Ting | 0a5e164 | 2012-02-07 18:11:25 +0800 | [diff] [blame] | 215 | } |
| 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 Ting | 22c0aec | 2012-01-18 17:19:42 +0800 | [diff] [blame] | 223 | |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 224 | 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 Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 236 | 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 Lin | c74fa8c | 2011-11-24 11:59:52 +0800 | [diff] [blame] | 263 | 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 Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | @Override |
Owen Lin | c74fa8c | 2011-11-24 11:59:52 +0800 | [diff] [blame] | 277 | public void onCancel(DialogInterface dialog) { |
| 278 | if (dialog == mVersionCheckDialog) { |
| 279 | mVersionCheckDialog = null; |
| 280 | } |
| 281 | } |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 282 | } |