• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
19 import android.content.ContentResolver;
20 import android.content.Intent;
21 import android.net.Uri;
22 import android.os.Bundle;
23 import android.view.Menu;
24 import android.view.MenuItem;
25 import android.view.Window;
26 import android.widget.Toast;
27 
28 import com.android.gallery3d.R;
29 import com.android.gallery3d.common.Utils;
30 import com.android.gallery3d.data.DataManager;
31 import com.android.gallery3d.data.MediaItem;
32 import com.android.gallery3d.data.MediaSet;
33 import com.android.gallery3d.data.Path;
34 import com.android.gallery3d.picasasource.PicasaSource;
35 import com.android.gallery3d.ui.GLRoot;
36 import com.android.gallery3d.util.GalleryUtils;
37 
38 public final class Gallery extends AbstractGalleryActivity {
39     public static final String EXTRA_SLIDESHOW = "slideshow";
40     public static final String EXTRA_CROP = "crop";
41 
42     public static final String ACTION_REVIEW = "com.android.camera.action.REVIEW";
43     public static final String KEY_GET_CONTENT = "get-content";
44     public static final String KEY_GET_ALBUM = "get-album";
45     public static final String KEY_TYPE_BITS = "type-bits";
46     public static final String KEY_MEDIA_TYPES = "mediaTypes";
47 
48     private static final String TAG = "Gallery";
49     private GalleryActionBar mActionBar;
50 
51     @Override
onCreate(Bundle savedInstanceState)52     protected void onCreate(Bundle savedInstanceState) {
53         super.onCreate(savedInstanceState);
54         requestWindowFeature(Window.FEATURE_ACTION_BAR);
55         requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
56         requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
57 
58         setContentView(R.layout.main);
59         mActionBar = new GalleryActionBar(this);
60 
61         if (savedInstanceState != null) {
62             getStateManager().restoreFromState(savedInstanceState);
63         } else {
64             initializeByIntent();
65         }
66     }
67 
initializeByIntent()68     private void initializeByIntent() {
69         Intent intent = getIntent();
70         String action = intent.getAction();
71 
72         if (Intent.ACTION_GET_CONTENT.equalsIgnoreCase(action)) {
73             startGetContent(intent);
74         } else if (Intent.ACTION_PICK.equalsIgnoreCase(action)) {
75             // We do NOT really support the PICK intent. Handle it as
76             // the GET_CONTENT. However, we need to translate the type
77             // in the intent here.
78             Log.w(TAG, "action PICK is not supported");
79             String type = Utils.ensureNotNull(intent.getType());
80             if (type.startsWith("vnd.android.cursor.dir/")) {
81                 if (type.endsWith("/image")) intent.setType("image/*");
82                 if (type.endsWith("/video")) intent.setType("video/*");
83             }
84             startGetContent(intent);
85         } else if (Intent.ACTION_VIEW.equalsIgnoreCase(action)
86                 || ACTION_REVIEW.equalsIgnoreCase(action)){
87             startViewAction(intent);
88         } else {
89             startDefaultPage();
90         }
91     }
92 
startDefaultPage()93     public void startDefaultPage() {
94         PicasaSource.showSignInReminder(this);
95         Bundle data = new Bundle();
96         data.putString(AlbumSetPage.KEY_MEDIA_PATH,
97                 getDataManager().getTopSetPath(DataManager.INCLUDE_ALL));
98         getStateManager().startState(AlbumSetPage.class, data);
99     }
100 
startGetContent(Intent intent)101     private void startGetContent(Intent intent) {
102         Bundle data = intent.getExtras() != null
103                 ? new Bundle(intent.getExtras())
104                 : new Bundle();
105         data.putBoolean(KEY_GET_CONTENT, true);
106         int typeBits = GalleryUtils.determineTypeBits(this, intent);
107         data.putInt(KEY_TYPE_BITS, typeBits);
108         data.putString(AlbumSetPage.KEY_MEDIA_PATH,
109                 getDataManager().getTopSetPath(typeBits));
110         getStateManager().setLaunchGalleryOnTop(true);
111         getStateManager().startState(AlbumSetPage.class, data);
112     }
113 
getContentType(Intent intent)114     private String getContentType(Intent intent) {
115         String type = intent.getType();
116         if (type != null) return type;
117 
118         Uri uri = intent.getData();
119         try {
120             return getContentResolver().getType(uri);
121         } catch (Throwable t) {
122             Log.w(TAG, "get type fail", t);
123             return null;
124         }
125     }
126 
startViewAction(Intent intent)127     private void startViewAction(Intent intent) {
128         Boolean slideshow = intent.getBooleanExtra(EXTRA_SLIDESHOW, false);
129         getStateManager().setLaunchGalleryOnTop(true);
130         if (slideshow) {
131             getActionBar().hide();
132             DataManager manager = getDataManager();
133             Path path = manager.findPathByUri(intent.getData());
134             if (path == null || manager.getMediaObject(path)
135                     instanceof MediaItem) {
136                 path = Path.fromString(
137                         manager.getTopSetPath(DataManager.INCLUDE_IMAGE));
138             }
139             Bundle data = new Bundle();
140             data.putString(SlideshowPage.KEY_SET_PATH, path.toString());
141             data.putBoolean(SlideshowPage.KEY_RANDOM_ORDER, true);
142             data.putBoolean(SlideshowPage.KEY_REPEAT, true);
143             getStateManager().startState(SlideshowPage.class, data);
144         } else {
145             Bundle data = new Bundle();
146             DataManager dm = getDataManager();
147             Uri uri = intent.getData();
148             String contentType = getContentType(intent);
149             if (contentType == null) {
150                 Toast.makeText(this,
151                         R.string.no_such_item, Toast.LENGTH_LONG).show();
152                 finish();
153                 return;
154             }
155             if (uri == null) {
156                 int typeBits = GalleryUtils.determineTypeBits(this, intent);
157                 data.putInt(KEY_TYPE_BITS, typeBits);
158                 data.putString(AlbumSetPage.KEY_MEDIA_PATH,
159                         getDataManager().getTopSetPath(typeBits));
160                 getStateManager().setLaunchGalleryOnTop(true);
161                 getStateManager().startState(AlbumSetPage.class, data);
162             } else if (contentType.startsWith(
163                     ContentResolver.CURSOR_DIR_BASE_TYPE)) {
164                 int mediaType = intent.getIntExtra(KEY_MEDIA_TYPES, 0);
165                 if (mediaType != 0) {
166                     uri = uri.buildUpon().appendQueryParameter(
167                             KEY_MEDIA_TYPES, String.valueOf(mediaType))
168                             .build();
169                 }
170                 Path setPath = dm.findPathByUri(uri);
171                 MediaSet mediaSet = null;
172                 if (setPath != null) {
173                     mediaSet = (MediaSet) dm.getMediaObject(setPath);
174                 }
175                 if (mediaSet != null) {
176                     if (mediaSet.isLeafAlbum()) {
177                         data.putString(AlbumPage.KEY_MEDIA_PATH, setPath.toString());
178                         getStateManager().startState(AlbumPage.class, data);
179                     } else {
180                         data.putString(AlbumSetPage.KEY_MEDIA_PATH, setPath.toString());
181                         getStateManager().startState(AlbumSetPage.class, data);
182                     }
183                 } else {
184                     startDefaultPage();
185                 }
186             } else {
187                 Path itemPath = dm.findPathByUri(uri);
188                 Path albumPath = dm.getDefaultSetOf(itemPath);
189                 // TODO: Make this parameter public so other activities can reference it.
190                 boolean singleItemOnly = intent.getBooleanExtra("SingleItemOnly", false);
191                 if (!singleItemOnly && albumPath != null) {
192                     data.putString(PhotoPage.KEY_MEDIA_SET_PATH,
193                             albumPath.toString());
194                 }
195                 data.putString(PhotoPage.KEY_MEDIA_ITEM_PATH, itemPath.toString());
196                 getStateManager().startState(PhotoPage.class, data);
197             }
198         }
199     }
200 
201     @Override
onCreateOptionsMenu(Menu menu)202     public boolean onCreateOptionsMenu(Menu menu) {
203         super.onCreateOptionsMenu(menu);
204         return getStateManager().createOptionsMenu(menu);
205     }
206 
207     @Override
onOptionsItemSelected(MenuItem item)208     public boolean onOptionsItemSelected(MenuItem item) {
209         GLRoot root = getGLRoot();
210         root.lockRenderThread();
211         try {
212             return getStateManager().itemSelected(item);
213         } finally {
214             root.unlockRenderThread();
215         }
216     }
217 
218     @Override
onBackPressed()219     public void onBackPressed() {
220         // send the back event to the top sub-state
221         GLRoot root = getGLRoot();
222         root.lockRenderThread();
223         try {
224             getStateManager().onBackPressed();
225         } finally {
226             root.unlockRenderThread();
227         }
228     }
229 
230     @Override
onDestroy()231     public void onDestroy() {
232         super.onDestroy();
233         GLRoot root = getGLRoot();
234         root.lockRenderThread();
235         try {
236             getStateManager().destroy();
237         } finally {
238             root.unlockRenderThread();
239         }
240     }
241 
242     @Override
onResume()243     protected void onResume() {
244         Utils.assertTrue(getStateManager().getStateCount() > 0);
245         super.onResume();
246     }
247 
248     @Override
getGalleryActionBar()249     public GalleryActionBar getGalleryActionBar() {
250         return mActionBar;
251     }
252 }
253