Home | History | Annotate | Download | only in app
      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.app.Dialog;
     20 import android.content.ContentResolver;
     21 import android.content.DialogInterface;
     22 import android.content.DialogInterface.OnCancelListener;
     23 import android.content.Intent;
     24 import android.net.Uri;
     25 import android.os.Bundle;
     26 import android.view.Menu;
     27 import android.view.MenuItem;
     28 import android.view.Window;
     29 import android.widget.Toast;
     30 
     31 import com.android.gallery3d.R;
     32 import com.android.gallery3d.common.Utils;
     33 import com.android.gallery3d.data.DataManager;
     34 import com.android.gallery3d.data.MediaItem;
     35 import com.android.gallery3d.data.MediaSet;
     36 import com.android.gallery3d.data.Path;
     37 import com.android.gallery3d.picasasource.PicasaSource;
     38 import com.android.gallery3d.ui.GLRoot;
     39 import com.android.gallery3d.util.GalleryUtils;
     40 
     41 public final class Gallery extends AbstractGalleryActivity implements OnCancelListener {
     42     public static final String EXTRA_SLIDESHOW = "slideshow";
     43     public static final String EXTRA_CROP = "crop";
     44 
     45     public static final String ACTION_REVIEW = "com.android.camera.action.REVIEW";
     46     public static final String KEY_GET_CONTENT = "get-content";
     47     public static final String KEY_GET_ALBUM = "get-album";
     48     public static final String KEY_TYPE_BITS = "type-bits";
     49     public static final String KEY_MEDIA_TYPES = "mediaTypes";
     50 
     51     private static final String TAG = "Gallery";
     52     private GalleryActionBar mActionBar;
     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         requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
     61 
     62         setContentView(R.layout.main);
     63         mActionBar = new GalleryActionBar(this);
     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() {
     98         PicasaSource.showSignInReminder(this);
     99         Bundle data = new Bundle();
    100         data.putString(AlbumSetPage.KEY_MEDIA_PATH,
    101                 getDataManager().getTopSetPath(DataManager.INCLUDE_ALL));
    102         getStateManager().startState(AlbumSetPage.class, data);
    103         mVersionCheckDialog = PicasaSource.getVersionCheckDialog(this);
    104         if (mVersionCheckDialog != null) {
    105             mVersionCheckDialog.setOnCancelListener(this);
    106         }
    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().setLaunchGalleryOnTop(true);
    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);
    137         getStateManager().setLaunchGalleryOnTop(true);
    138         if (slideshow) {
    139             getActionBar().hide();
    140             DataManager manager = getDataManager();
    141             Path path = manager.findPathByUri(intent.getData());
    142             if (path == null || manager.getMediaObject(path)
    143                     instanceof MediaItem) {
    144                 path = Path.fromString(
    145                         manager.getTopSetPath(DataManager.INCLUDE_IMAGE));
    146             }
    147             Bundle data = new Bundle();
    148             data.putString(SlideshowPage.KEY_SET_PATH, path.toString());
    149             data.putBoolean(SlideshowPage.KEY_RANDOM_ORDER, true);
    150             data.putBoolean(SlideshowPage.KEY_REPEAT, true);
    151             getStateManager().startState(SlideshowPage.class, data);
    152         } else {
    153             Bundle data = new Bundle();
    154             DataManager dm = getDataManager();
    155             Uri uri = intent.getData();
    156             String contentType = getContentType(intent);
    157             if (contentType == null) {
    158                 Toast.makeText(this,
    159                         R.string.no_such_item, Toast.LENGTH_LONG).show();
    160                 finish();
    161                 return;
    162             }
    163             if (uri == null) {
    164                 int typeBits = GalleryUtils.determineTypeBits(this, intent);
    165                 data.putInt(KEY_TYPE_BITS, typeBits);
    166                 data.putString(AlbumSetPage.KEY_MEDIA_PATH,
    167                         getDataManager().getTopSetPath(typeBits));
    168                 getStateManager().setLaunchGalleryOnTop(true);
    169                 getStateManager().startState(AlbumSetPage.class, data);
    170             } else if (contentType.startsWith(
    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                 }
    178                 Path setPath = dm.findPathByUri(uri);
    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());
    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);
    196                 Path albumPath = dm.getDefaultSetOf(itemPath);
    197                 // TODO: Make this parameter public so other activities can reference it.
    198                 boolean singleItemOnly = intent.getBooleanExtra("SingleItemOnly", false);
    199                 if (!singleItemOnly && albumPath != null) {
    200                     data.putString(PhotoPage.KEY_MEDIA_SET_PATH,
    201                             albumPath.toString());
    202                 }
    203                 data.putString(PhotoPage.KEY_MEDIA_ITEM_PATH, itemPath.toString());
    204                 getStateManager().startState(PhotoPage.class, data);
    205             }
    206         }
    207     }
    208 
    209     @Override
    210     public boolean onCreateOptionsMenu(Menu menu) {
    211         super.onCreateOptionsMenu(menu);
    212         return getStateManager().createOptionsMenu(menu);
    213     }
    214 
    215     @Override
    216     public boolean onOptionsItemSelected(MenuItem item) {
    217         GLRoot root = getGLRoot();
    218         root.lockRenderThread();
    219         try {
    220             return getStateManager().itemSelected(item);
    221         } finally {
    222             root.unlockRenderThread();
    223         }
    224     }
    225 
    226     @Override
    227     public void onBackPressed() {
    228         // send the back event to the top sub-state
    229         GLRoot root = getGLRoot();
    230         root.lockRenderThread();
    231         try {
    232             getStateManager().onBackPressed();
    233         } finally {
    234             root.unlockRenderThread();
    235         }
    236     }
    237 
    238     @Override
    239     public void onDestroy() {
    240         super.onDestroy();
    241         GLRoot root = getGLRoot();
    242         root.lockRenderThread();
    243         try {
    244             getStateManager().destroy();
    245         } finally {
    246             root.unlockRenderThread();
    247         }
    248     }
    249 
    250     @Override
    251     protected void onResume() {
    252         Utils.assertTrue(getStateManager().getStateCount() > 0);
    253         super.onResume();
    254         if (mVersionCheckDialog != null) {
    255             mVersionCheckDialog.show();
    256         }
    257     }
    258 
    259     @Override
    260     protected void onPause() {
    261         super.onPause();
    262         if (mVersionCheckDialog != null) {
    263             mVersionCheckDialog.dismiss();
    264         }
    265     }
    266 
    267     @Override
    268     public GalleryActionBar getGalleryActionBar() {
    269         return mActionBar;
    270     }
    271 
    272     @Override
    273     public void onCancel(DialogInterface dialog) {
    274         if (dialog == mVersionCheckDialog) {
    275             mVersionCheckDialog = null;
    276         }
    277     }
    278 }
    279