OSDN Git Service

Add camera icon for camera roll.
[android-x86/packages-apps-Gallery2.git] / src / com / android / gallery3d / app / AlbumPage.java
index efffe40..3f42aee 100644 (file)
@@ -22,6 +22,8 @@ import android.content.Intent;
 import android.graphics.Rect;
 import android.net.Uri;
 import android.os.Bundle;
+import android.os.Handler;
+import android.os.Message;
 import android.os.Vibrator;
 import android.provider.MediaStore;
 import android.view.ActionMode;
@@ -44,21 +46,25 @@ import com.android.gallery3d.ui.ActionModeHandler.ActionModeListener;
 import com.android.gallery3d.ui.AlbumSlotRenderer;
 import com.android.gallery3d.ui.DetailsHelper;
 import com.android.gallery3d.ui.DetailsHelper.CloseListener;
+import com.android.gallery3d.ui.FadeTexture;
 import com.android.gallery3d.ui.GLCanvas;
 import com.android.gallery3d.ui.GLRoot;
 import com.android.gallery3d.ui.GLView;
 import com.android.gallery3d.ui.RelativePosition;
-import com.android.gallery3d.ui.ScreenNailHolder;
 import com.android.gallery3d.ui.SelectionManager;
 import com.android.gallery3d.ui.SlotView;
+import com.android.gallery3d.ui.SynchronizedHandler;
 import com.android.gallery3d.util.Future;
 import com.android.gallery3d.util.GalleryUtils;
+import com.android.gallery3d.util.MediaSetUtils;
 
 public class AlbumPage extends ActivityState implements GalleryActionBar.ClusterRunner,
         SelectionManager.SelectionListener, MediaSet.SyncListener {
     @SuppressWarnings("unused")
     private static final String TAG = "AlbumPage";
 
+    private static final int MSG_PICK_PHOTO = 1;
+
     public static final String KEY_MEDIA_PATH = "media-path";
     public static final String KEY_PARENT_MEDIA_PATH = "parent-media-path";
     public static final String KEY_SET_CENTER = "set-center";
@@ -73,7 +79,6 @@ public class AlbumPage extends ActivityState implements GalleryActionBar.Cluster
     private static final int BIT_LOADING_SYNC = 2;
 
     private static final float USER_DISTANCE_METER = 0.3f;
-    private static final boolean TEST_CAMERA_PREVIEW = false;
 
     private boolean mIsActive = false;
     private AlbumSlotRenderer mAlbumView;
@@ -97,6 +102,7 @@ public class AlbumPage extends ActivityState implements GalleryActionBar.Cluster
     private MediaSet mMediaSet;
     private boolean mShowDetails;
     private float mUserDistance; // in pixel
+    private Handler mHandler;
 
     private Future<Integer> mSyncTask = null;
 
@@ -142,6 +148,16 @@ public class AlbumPage extends ActivityState implements GalleryActionBar.Cluster
         }
     };
 
+    // This are the transitions we want:
+    //
+    // +--------+           +------------+    +-------+    +----------+
+    // | Camera |---------->| Fullscreen |--->| Album |--->| AlbumSet |
+    // |  View  | thumbnail |   Photo    | up | Page  | up |   Page   |
+    // +--------+           +------------+    +-------+    +----------+
+    //     ^                      |               |            ^  |
+    //     |                      |               |            |  |         close
+    //     +----------back--------+               +----back----+  +--back->  app
+    //
     @Override
     protected void onBackPressed() {
         if (mShowDetails) {
@@ -151,7 +167,18 @@ public class AlbumPage extends ActivityState implements GalleryActionBar.Cluster
         } else {
             // TODO: fix this regression
             // mAlbumView.savePositions(PositionRepository.getInstance(mActivity));
+            onUpPressed();
+        }
+    }
+
+    private void onUpPressed() {
+        if (mActivity.getStateManager().getStateCount() > 1) {
             super.onBackPressed();
+        } else if (mParentMediaSetString != null) {
+            Bundle data = new Bundle(getData());
+            data.putString(AlbumSetPage.KEY_MEDIA_PATH, mParentMediaSetString);
+            mActivity.getStateManager().switchState(
+                    this, AlbumSetPage.class, data);
         }
     }
 
@@ -159,45 +186,52 @@ public class AlbumPage extends ActivityState implements GalleryActionBar.Cluster
         mAlbumView.setPressedIndex(index);
     }
 
-    private void onUp() {
-        mAlbumView.setPressedIndex(-1);
+    private void onUp(boolean followedByLongPress) {
+        if (followedByLongPress) {
+            // Avoid showing press-up animations for long-press.
+            mAlbumView.setPressedIndex(-1);
+        } else {
+            mAlbumView.setPressedUp();
+        }
     }
 
     private void onSingleTapUp(int slotIndex) {
-        MediaItem item = mAlbumDataAdapter.get(slotIndex);
-        if (item == null) {
-            Log.w(TAG, "item not ready yet, ignore the click");
-            return;
-        }
-        if (mShowDetails) {
-            mAlbumView.setHighlightItemPath(item.getPath());
-            mDetailsHelper.reloadDetails(slotIndex);
-        } else if (!mSelectionManager.inSelectionMode()) {
-            if (mGetContent) {
-                onGetContent(item);
-            } else {
-                // Get into the PhotoPage.
-                Bundle data = new Bundle();
+        if (!mIsActive) return;
 
-                // mAlbumView.savePositions(PositionRepository.getInstance(mActivity));
-                data.putInt(PhotoPage.KEY_INDEX_HINT, slotIndex);
-                data.putParcelable(PhotoPage.KEY_OPEN_ANIMATION_RECT,
-                        getSlotRect(slotIndex));
-                data.putString(PhotoPage.KEY_MEDIA_SET_PATH,
-                        mMediaSetPath.toString());
-                data.putString(PhotoPage.KEY_MEDIA_ITEM_PATH,
-                        item.getPath().toString());
-                if (TEST_CAMERA_PREVIEW) {
-                    ScreenNailHolder holder = new CameraScreenNailHolder(mActivity);
-                    data.putParcelable(PhotoPage.KEY_SCREENNAIL_HOLDER, holder);
-                }
-                mActivity.getStateManager().startStateForResult(
-                        PhotoPage.class, REQUEST_PHOTO, data);
-            }
-        } else {
+        if (mSelectionManager.inSelectionMode()) {
+            MediaItem item = mAlbumDataAdapter.get(slotIndex);
+            if (item == null) return; // Item not ready yet, ignore the click
             mSelectionManager.toggle(item.getPath());
-            mDetailsSource.findIndex(slotIndex);
             mSlotView.invalidate();
+        } else {
+            // Show pressed-up animation for the single-tap.
+            mAlbumView.setPressedIndex(slotIndex);
+            mAlbumView.setPressedUp();
+            mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_PICK_PHOTO, slotIndex, 0),
+                    FadeTexture.DURATION);
+        }
+    }
+
+    private void pickPhoto(int slotIndex) {
+        if (!mIsActive) return;
+
+        MediaItem item = mAlbumDataAdapter.get(slotIndex);
+        if (item == null) return; // Item not ready yet, ignore the click
+        if (mGetContent) {
+            onGetContent(item);
+        } else {
+            // Get into the PhotoPage.
+            // mAlbumView.savePositions(PositionRepository.getInstance(mActivity));
+            Bundle data = new Bundle();
+            data.putInt(PhotoPage.KEY_INDEX_HINT, slotIndex);
+            data.putParcelable(PhotoPage.KEY_OPEN_ANIMATION_RECT,
+                    getSlotRect(slotIndex));
+            data.putString(PhotoPage.KEY_MEDIA_SET_PATH,
+                    mMediaSetPath.toString());
+            data.putString(PhotoPage.KEY_MEDIA_ITEM_PATH,
+                    item.getPath().toString());
+            mActivity.getStateManager().startStateForResult(
+                    PhotoPage.class, REQUEST_PHOTO, data);
         }
     }
 
@@ -234,16 +268,11 @@ public class AlbumPage extends ActivityState implements GalleryActionBar.Cluster
 
     public void onLongTap(int slotIndex) {
         if (mGetContent) return;
-        if (mShowDetails) {
-            onSingleTapUp(slotIndex);
-        } else {
-            MediaItem item = mAlbumDataAdapter.get(slotIndex);
-            if (item == null) return;
-            mSelectionManager.setAutoLeaveSelectionMode(true);
-            mSelectionManager.toggle(item.getPath());
-            mDetailsSource.findIndex(slotIndex);
-            mSlotView.invalidate();
-        }
+        MediaItem item = mAlbumDataAdapter.get(slotIndex);
+        if (item == null) return;
+        mSelectionManager.setAutoLeaveSelectionMode(true);
+        mSelectionManager.toggle(item.getPath());
+        mSlotView.invalidate();
     }
 
     @Override
@@ -288,6 +317,19 @@ public class AlbumPage extends ActivityState implements GalleryActionBar.Cluster
                 mSlotView.startScatteringAnimation(mOpenCenter);
             }
         }
+
+        mHandler = new SynchronizedHandler(mActivity.getGLRoot()) {
+            @Override
+            public void handleMessage(Message message) {
+                switch (message.what) {
+                    case MSG_PICK_PHOTO: {
+                        pickPhoto(message.arg1);
+                        break;
+                    }
+                    default: throw new AssertionError(message.what);
+                }
+            }
+        };
     }
 
     @Override
@@ -342,7 +384,7 @@ public class AlbumPage extends ActivityState implements GalleryActionBar.Cluster
         mSelectionManager = new SelectionManager(mActivity, false);
         mSelectionManager.setSelectionListener(this);
         Config.AlbumPage config = Config.AlbumPage.get((Context) mActivity);
-        mSlotView = new SlotView((Context) mActivity, config.slotViewSpec);
+        mSlotView = new SlotView(mActivity, config.slotViewSpec);
         mAlbumView = new AlbumSlotRenderer(mActivity, mSlotView, mSelectionManager);
         mSlotView.setSlotRenderer(mAlbumView);
         mRootPane.addComponent(mSlotView);
@@ -353,8 +395,8 @@ public class AlbumPage extends ActivityState implements GalleryActionBar.Cluster
             }
 
             @Override
-            public void onUp() {
-                AlbumPage.this.onUp();
+            public void onUp(boolean followedByLongPress) {
+                AlbumPage.this.onUp(followedByLongPress);
             }
 
             @Override
@@ -429,13 +471,20 @@ public class AlbumPage extends ActivityState implements GalleryActionBar.Cluster
                 menu.findItem(R.id.action_slideshow).setVisible(true);
             }
 
-            MenuItem groupBy = menu.findItem(R.id.action_group_by);
             FilterUtils.setupMenuItems(actionBar, mMediaSetPath, true);
 
+            MenuItem groupBy = menu.findItem(R.id.action_group_by);
             if (groupBy != null) {
                 groupBy.setVisible(mShowClusterMenu);
             }
 
+            MenuItem switchCamera = menu.findItem(R.id.action_camera);
+            if (switchCamera != null) {
+                switchCamera.setVisible(
+                        MediaSetUtils.isCameraSource(mMediaSetPath)
+                        && GalleryUtils.isCameraAvailable(activity));
+            }
+
             actionBar.setTitle(mMediaSet.getName());
         }
         actionBar.setSubtitle(null);
@@ -447,13 +496,7 @@ public class AlbumPage extends ActivityState implements GalleryActionBar.Cluster
     protected boolean onItemSelected(MenuItem item) {
         switch (item.getItemId()) {
             case android.R.id.home: {
-                if (mActivity.getStateManager().getStateCount() > 1) {
-                    onBackPressed();
-                } else if (mParentMediaSetString != null) {
-                    Bundle data = new Bundle(getData());
-                    data.putString(AlbumSetPage.KEY_MEDIA_PATH, mParentMediaSetString);
-                    mActivity.getStateManager().switchState(this, AlbumSetPage.class, data);
-                }
+                onUpPressed();
                 return true;
             }
             case R.id.action_cancel:
@@ -484,6 +527,10 @@ public class AlbumPage extends ActivityState implements GalleryActionBar.Cluster
                 }
                 return true;
             }
+            case R.id.action_camera: {
+                GalleryUtils.startCameraActivity((Activity) mActivity);
+                return true;
+            }
             default:
                 return false;
         }
@@ -501,8 +548,8 @@ public class AlbumPage extends ActivityState implements GalleryActionBar.Cluster
             }
             case REQUEST_PHOTO: {
                 if (data == null) return;
-                mFocusIndex = data.getIntExtra(PhotoPage.KEY_INDEX_HINT, 0);
-                mSlotView.setCenterIndex(mFocusIndex);
+                mFocusIndex = data.getIntExtra(PhotoPage.KEY_RETURN_INDEX_HINT, 0);
+                mSlotView.makeSlotVisible(mFocusIndex);
                 mSlotView.startRestoringAnimation(mFocusIndex);
                 break;
             }