Switch over to use new camera activity

Change-Id: Ib907b5ab5d0e818261e95edd182f2e20c3bbebe0
diff --git a/res/layout/camera.xml b/res/layout/camera.xml
index d917990..9a3a01a 100644
--- a/res/layout/camera.xml
+++ b/res/layout/camera.xml
@@ -13,9 +13,9 @@
      See the License for the specific language governing permissions and
      limitations under the License.
 -->
-<com.android.camera.ui.NewCameraRootView
+<com.android.camera.ui.CameraRootView
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/camera_app_root"
     android:layout_width="match_parent"
     android:layout_height="match_parent" >
-</com.android.camera.ui.NewCameraRootView>
+</com.android.camera.ui.CameraRootView>
diff --git a/res/layout/camera_main.xml b/res/layout/camera_main.xml
deleted file mode 100644
index 99befc0..0000000
--- a/res/layout/camera_main.xml
+++ /dev/null
@@ -1,31 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!-- Copyright (C) 2012 The Android Open Source Project
-
-     Licensed under the Apache License, Version 2.0 (the "License");
-     you may not use this file except in compliance with the License.
-     You may obtain a copy of the License at
-
-          http://www.apache.org/licenses/LICENSE-2.0
-
-     Unless required by applicable law or agreed to in writing, software
-     distributed under the License is distributed on an "AS IS" BASIS,
-     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-     See the License for the specific language governing permissions and
-     limitations under the License.
--->
-<com.android.camera.ui.CameraRootView
-    xmlns:android="http://schemas.android.com/apk/res/android"
-    android:id="@+id/content"
-    android:layout_width="match_parent"
-    android:layout_height="match_parent" >
-
-    <include layout="@layout/gl_root_group" />
-
-    <FrameLayout
-        android:id="@+id/camera_app_root"
-        android:layout_width="match_parent"
-        android:layout_height="match_parent" />
-
-    <include layout="@layout/camera_controls" />
-
-</com.android.camera.ui.CameraRootView>
\ No newline at end of file
diff --git a/res/layout/new_photo_module.xml b/res/layout/new_photo_module.xml
deleted file mode 100644
index 70a7579..0000000
--- a/res/layout/new_photo_module.xml
+++ /dev/null
@@ -1,44 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!-- Copyright (C) 2013 The Android Open Source Project
-
-     Licensed under the Apache License, Version 2.0 (the "License");
-     you may not use this file except in compliance with the License.
-     You may obtain a copy of the License at
-
-          http://www.apache.org/licenses/LICENSE-2.0
-
-     Unless required by applicable law or agreed to in writing, software
-     distributed under the License is distributed on an "AS IS" BASIS,
-     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-     See the License for the specific language governing permissions and
-     limitations under the License.
--->
-
-<!-- This layout is shared by phone and tablet in both landscape and portrait
- orientation. The purpose of having this layout is to eventually not manually
- recreate views when the orientation changes, by migrating the views that do not
- need to be recreated in onConfigurationChanged from old photo_module to this
- layout. -->
-
-<merge xmlns:android="http://schemas.android.com/apk/res/android"
-    android:layout_width="match_parent"
-    android:layout_height="match_parent"
-    android:layout_gravity="center">
-    <TextureView
-        android:id="@+id/preview_content"
-        android:layout_width="match_parent"
-        android:layout_height="match_parent" />
-    <ViewStub android:id="@+id/face_view_stub"
-        android:inflatedId="@+id/face_view"
-        android:layout="@layout/face_view"
-        android:layout_width="match_parent"
-        android:layout_height="match_parent"
-        android:visibility="gone"/>
-    <com.android.camera.ui.RenderOverlay
-        android:id="@+id/render_overlay"
-        android:layout_width="match_parent"
-        android:layout_height="match_parent" />
-    <include layout="@layout/camera_controls"
-        android:layout_gravity="center"
-        style="@style/CameraControls"/>
-</merge>
\ No newline at end of file
diff --git a/res/layout/new_video_module.xml b/res/layout/new_video_module.xml
deleted file mode 100644
index 9eb3e84..0000000
--- a/res/layout/new_video_module.xml
+++ /dev/null
@@ -1,53 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!-- Copyright (C) 2013 The Android Open Source Project
-
-     Licensed under the Apache License, Version 2.0 (the "License");
-     you may not use this file except in compliance with the License.
-     You may obtain a copy of the License at
-
-          http://www.apache.org/licenses/LICENSE-2.0
-
-     Unless required by applicable law or agreed to in writing, software
-     distributed under the License is distributed on an "AS IS" BASIS,
-     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-     See the License for the specific language governing permissions and
-     limitations under the License.
--->
-<!-- This layout is shared by phone and tablet in landscape orientation. -->
-<merge xmlns:android="http://schemas.android.com/apk/res/android"
-    android:layout_height="match_parent"
-    android:layout_width="match_parent">
-    <TextureView
-        android:id="@+id/preview_content"
-        android:layout_width="match_parent"
-        android:layout_height="match_parent" />
-    <FrameLayout android:id="@+id/preview_border"
-            android:layout_width="match_parent"
-            android:layout_height="match_parent"
-            android:visibility="gone"
-            android:background="@drawable/ic_snapshot_border" />
-    <com.android.camera.ui.RenderOverlay
-        android:id="@+id/render_overlay"
-        android:layout_width="match_parent"
-        android:layout_height="match_parent" />
-    <com.android.camera.ui.RotateLayout android:id="@+id/recording_time_rect"
-            style="@style/ViewfinderLabelLayout">
-        <include layout="@layout/viewfinder_labels_video" android:id="@+id/labels" />
-    </com.android.camera.ui.RotateLayout>
-    <ImageView android:id="@+id/review_image"
-            android:layout_height="match_parent"
-            android:layout_width="match_parent"
-            android:visibility="gone"
-            android:background="@android:color/black"/>
-    <ImageView
-            android:id="@+id/btn_play"
-            style="@style/ReviewControlIcon"
-            android:layout_centerInParent="true"
-            android:src="@drawable/ic_gallery_play_big"
-            android:visibility="gone"
-            android:onClick="onReviewPlayClicked"/>
-
-    <include layout="@layout/camera_controls"
-        android:layout_gravity="center"
-        style="@style/CameraControls"/>
-</merge>
diff --git a/res/layout/photo_module.xml b/res/layout/photo_module.xml
index abf094e..70a7579 100644
--- a/res/layout/photo_module.xml
+++ b/res/layout/photo_module.xml
@@ -24,8 +24,10 @@
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:layout_gravity="center">
-    <include layout="@layout/count_down_to_capture"/>
-
+    <TextureView
+        android:id="@+id/preview_content"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent" />
     <ViewStub android:id="@+id/face_view_stub"
         android:inflatedId="@+id/face_view"
         android:layout="@layout/face_view"
@@ -36,4 +38,7 @@
         android:id="@+id/render_overlay"
         android:layout_width="match_parent"
         android:layout_height="match_parent" />
+    <include layout="@layout/camera_controls"
+        android:layout_gravity="center"
+        style="@style/CameraControls"/>
 </merge>
\ No newline at end of file
diff --git a/res/layout/video_module.xml b/res/layout/video_module.xml
index 790f3eb..9eb3e84 100644
--- a/res/layout/video_module.xml
+++ b/res/layout/video_module.xml
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="utf-8"?>
-<!-- Copyright (C) 2012 The Android Open Source Project
+<!-- Copyright (C) 2013 The Android Open Source Project
 
      Licensed under the Apache License, Version 2.0 (the "License");
      you may not use this file except in compliance with the License.
@@ -15,43 +15,39 @@
 -->
 <!-- This layout is shared by phone and tablet in landscape orientation. -->
 <merge xmlns:android="http://schemas.android.com/apk/res/android"
-    android:id="@+id/camera_app_root"
     android:layout_height="match_parent"
     android:layout_width="match_parent">
-    <com.android.camera.PreviewFrameLayout android:id="@+id/frame"
+    <TextureView
+        android:id="@+id/preview_content"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent" />
+    <FrameLayout android:id="@+id/preview_border"
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"
+            android:visibility="gone"
+            android:background="@drawable/ic_snapshot_border" />
+    <com.android.camera.ui.RenderOverlay
+        android:id="@+id/render_overlay"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent" />
+    <com.android.camera.ui.RotateLayout android:id="@+id/recording_time_rect"
+            style="@style/ViewfinderLabelLayout">
+        <include layout="@layout/viewfinder_labels_video" android:id="@+id/labels" />
+    </com.android.camera.ui.RotateLayout>
+    <ImageView android:id="@+id/review_image"
             android:layout_height="match_parent"
             android:layout_width="match_parent"
-            android:layout_gravity="center">
-        <com.android.camera.ui.PreviewSurfaceView
-                android:id="@+id/preview_surface_view"
-                android:layout_width="match_parent"
-                android:layout_height="match_parent"
-                android:visibility="gone"/>
-        <FrameLayout android:id="@+id/preview_border"
-                android:layout_width="match_parent"
-                android:layout_height="match_parent"
-                android:visibility="gone"
-                android:background="@drawable/ic_snapshot_border" />
-        <com.android.camera.ui.RenderOverlay
-            android:id="@+id/render_overlay"
-            android:layout_width="match_parent"
-            android:layout_height="match_parent" />
-        <com.android.camera.ui.RotateLayout android:id="@+id/recording_time_rect"
-                style="@style/ViewfinderLabelLayout">
-            <include layout="@layout/viewfinder_labels_video" android:id="@+id/labels" />
-        </com.android.camera.ui.RotateLayout>
-        <ImageView android:id="@+id/review_image"
-                android:layout_height="match_parent"
-                android:layout_width="match_parent"
-                android:visibility="gone"
-                android:background="@android:color/black"/>
-        <ImageView
-                android:id="@+id/btn_play"
-                style="@style/ReviewControlIcon"
-                android:layout_centerInParent="true"
-                android:src="@drawable/ic_gallery_play_big"
-                android:visibility="gone"
-                android:onClick="onReviewPlayClicked"/>
-    </com.android.camera.PreviewFrameLayout>
+            android:visibility="gone"
+            android:background="@android:color/black"/>
+    <ImageView
+            android:id="@+id/btn_play"
+            style="@style/ReviewControlIcon"
+            android:layout_centerInParent="true"
+            android:src="@drawable/ic_gallery_play_big"
+            android:visibility="gone"
+            android:onClick="onReviewPlayClicked"/>
 
+    <include layout="@layout/camera_controls"
+        android:layout_gravity="center"
+        style="@style/CameraControls"/>
 </merge>
diff --git a/src/com/android/camera/NewPreviewGestures.java b/src/com/android/camera/NewPreviewGestures.java
deleted file mode 100644
index 339c4b3..0000000
--- a/src/com/android/camera/NewPreviewGestures.java
+++ /dev/null
@@ -1,263 +0,0 @@
-package com.android.camera;
-
-/*
- * Copyright (C) 2013 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-import android.os.Handler;
-import android.os.Message;
-import android.util.Log;
-import android.view.GestureDetector;
-import android.view.MotionEvent;
-import android.view.ScaleGestureDetector;
-import android.view.View;
-import android.view.ViewConfiguration;
-
-import com.android.camera.PreviewGestures.SingleTapListener;
-import com.android.camera.PreviewGestures.SwipeListener;
-import com.android.camera.ui.PieRenderer;
-import com.android.camera.ui.RenderOverlay;
-import com.android.camera.ui.ZoomRenderer;
-import com.android.gallery3d.R;
-
-import java.util.ArrayList;
-import java.util.List;
-
-/* NewPreviewGestures disambiguates touch events received on RenderOverlay
- * and dispatch them to the proper recipient (i.e. zoom renderer or pie renderer).
- * Touch events on CameraControls will be handled by framework.
- * */
-public class NewPreviewGestures
-        implements ScaleGestureDetector.OnScaleGestureListener {
-
-    private static final String TAG = "CAM_gestures";
-
-    private static final long TIMEOUT_PIE = 200;
-    private static final int MSG_PIE = 1;
-    private static final int MODE_NONE = 0;
-    private static final int MODE_PIE = 1;
-    private static final int MODE_ZOOM = 2;
-    private static final int MODE_MODULE = 3;
-    private static final int MODE_ALL = 4;
-    private static final int MODE_SWIPE = 5;
-
-    public static final int DIR_UP = 0;
-    public static final int DIR_DOWN = 1;
-    public static final int DIR_LEFT = 2;
-    public static final int DIR_RIGHT = 3;
-
-    private NewCameraActivity mActivity;
-    private SingleTapListener mTapListener;
-    private RenderOverlay mOverlay;
-    private PieRenderer mPie;
-    private ZoomRenderer mZoom;
-    private MotionEvent mDown;
-    private MotionEvent mCurrent;
-    private ScaleGestureDetector mScale;
-    private int mMode;
-    private int mSlop;
-    private int mTapTimeout;
-    private boolean mZoomEnabled;
-    private boolean mEnabled;
-    private boolean mZoomOnly;
-    private int mOrientation;
-    private GestureDetector mGestureDetector;
-
-    private GestureDetector.SimpleOnGestureListener mGestureListener = new GestureDetector.SimpleOnGestureListener() {
-        @Override
-        public void onLongPress (MotionEvent e) {
-            // Open pie
-            if (mPie != null && !mPie.showsItems()) {
-                openPie();
-            }
-        }
-
-        @Override
-        public boolean onSingleTapUp (MotionEvent e) {
-            // Tap to focus when pie is not open
-            if (mPie == null || !mPie.showsItems()) {
-                mTapListener.onSingleTapUp(null, (int) e.getX(), (int) e.getY());
-                return true;
-            }
-            return false;
-        }
-
-        @Override
-        public boolean onScroll (MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
-            if (mMode == MODE_ZOOM) return false;
-            int deltaX = (int) (e1.getX() - e2.getX());
-            int deltaY = (int) (e1.getY() - e2.getY());
-            if (deltaY > 2 * deltaX && deltaY > -2 * deltaX) {
-                // Open pie on swipe up
-                if (mPie != null && !mPie.showsItems()) {
-                    openPie();
-                    return true;
-                }
-            }
-            return false;
-        }
-    };
-
-    private Handler mHandler = new Handler() {
-        public void handleMessage(Message msg) {
-            if (msg.what == MSG_PIE) {
-                mMode = MODE_PIE;
-                openPie();
-            }
-        }
-    };
-
-    public interface SingleTapListener {
-        public void onSingleTapUp(View v, int x, int y);
-    }
-
-    public NewPreviewGestures(NewCameraActivity ctx, SingleTapListener tapListener,
-            ZoomRenderer zoom, PieRenderer pie) {
-        mActivity = ctx;
-        mTapListener = tapListener;
-        mPie = pie;
-        mZoom = zoom;
-        mMode = MODE_ALL;
-        mScale = new ScaleGestureDetector(ctx, this);
-        mSlop = (int) ctx.getResources().getDimension(R.dimen.pie_touch_slop);
-        mTapTimeout = ViewConfiguration.getTapTimeout();
-        mEnabled = true;
-        mGestureDetector = new GestureDetector(mGestureListener);
-    }
-
-    public void setRenderOverlay(RenderOverlay overlay) {
-        mOverlay = overlay;
-    }
-
-    public void setOrientation(int orientation) {
-        mOrientation = orientation;
-    }
-
-    public void setEnabled(boolean enabled) {
-        mEnabled = enabled;
-    }
-
-    public void setZoomEnabled(boolean enable) {
-        mZoomEnabled = enable;
-    }
-
-    public void setZoomOnly(boolean zoom) {
-        mZoomOnly = zoom;
-    }
-
-    public boolean isEnabled() {
-        return mEnabled;
-    }
-
-    public boolean dispatchTouch(MotionEvent m) {
-        if (!mEnabled) {
-            return false;
-        }
-        mCurrent = m;
-        if (MotionEvent.ACTION_DOWN == m.getActionMasked()) {
-            mMode = MODE_NONE;
-            mDown = MotionEvent.obtain(m);
-        }
-
-        // If pie is open, redirects all the touch events to pie.
-        if (mPie != null && mPie.isOpen()) {
-            return sendToPie(m);
-        }
-
-        // If pie is not open, send touch events to gesture detector and scale
-        // listener to recognize the gesture.
-        mGestureDetector.onTouchEvent(m);
-        if (mZoom != null) {
-            mScale.onTouchEvent(m);
-            if (MotionEvent.ACTION_POINTER_DOWN == m.getActionMasked()) {
-                mMode = MODE_ZOOM;
-                if (mZoomEnabled) {
-                    // Start showing zoom UI as soon as there is a second finger down
-                    mZoom.onScaleBegin(mScale);
-                }
-            } else if (MotionEvent.ACTION_POINTER_UP == m.getActionMasked()) {
-                mZoom.onScaleEnd(mScale);
-            }
-        }
-        return true;
-    }
-
-    // left tests for finger moving right to left
-    private int getSwipeDirection(MotionEvent m) {
-        float dx = 0;
-        float dy = 0;
-        switch (mOrientation) {
-        case 0:
-            dx = m.getX() - mDown.getX();
-            dy = m.getY() - mDown.getY();
-            break;
-        case 90:
-            dx = - (m.getY() - mDown.getY());
-            dy = m.getX() - mDown.getX();
-            break;
-        case 180:
-            dx = -(m.getX() - mDown.getX());
-            dy = m.getY() - mDown.getY();
-            break;
-        case 270:
-            dx = m.getY() - mDown.getY();
-            dy = m.getX() - mDown.getX();
-            break;
-        }
-        if (dx < 0 && (Math.abs(dy) / -dx < 2)) return DIR_LEFT;
-        if (dx > 0 && (Math.abs(dy) / dx < 2)) return DIR_RIGHT;
-        if (dy > 0) return DIR_DOWN;
-        return DIR_UP;
-    }
-
-    private MotionEvent makeCancelEvent(MotionEvent m) {
-        MotionEvent c = MotionEvent.obtain(m);
-        c.setAction(MotionEvent.ACTION_CANCEL);
-        return c;
-    }
-
-    private void openPie() {
-        mGestureDetector.onTouchEvent(makeCancelEvent(mDown));
-        mScale.onTouchEvent(makeCancelEvent(mDown));
-        mOverlay.directDispatchTouch(mDown, mPie);
-    }
-
-    private boolean sendToPie(MotionEvent m) {
-        return mOverlay.directDispatchTouch(m, mPie);
-    }
-
-    // OnScaleGestureListener implementation
-    @Override
-    public boolean onScale(ScaleGestureDetector detector) {
-        return mZoom.onScale(detector);
-    }
-
-    @Override
-    public boolean onScaleBegin(ScaleGestureDetector detector) {
-        if (mPie == null || !mPie.isOpen()) {
-            mMode = MODE_ZOOM;
-            mGestureDetector.onTouchEvent(makeCancelEvent(mCurrent));
-            if (!mZoomEnabled) return false;
-            return mZoom.onScaleBegin(detector);
-        }
-        return false;
-    }
-
-    @Override
-    public void onScaleEnd(ScaleGestureDetector detector) {
-        mZoom.onScaleEnd(detector);
-    }
-}
-
diff --git a/src/com/android/camera/PreviewGestures.java b/src/com/android/camera/PreviewGestures.java
index 0b80ff6..925607e 100644
--- a/src/com/android/camera/PreviewGestures.java
+++ b/src/com/android/camera/PreviewGestures.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2012 The Android Open Source Project
+ * Copyright (C) 2013 The Android Open Source Project
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
 
 import android.os.Handler;
 import android.os.Message;
+import android.view.GestureDetector;
 import android.view.MotionEvent;
 import android.view.ScaleGestureDetector;
 import android.view.View;
@@ -28,9 +29,10 @@
 import com.android.camera.ui.ZoomRenderer;
 import com.android.gallery3d.R;
 
-import java.util.ArrayList;
-import java.util.List;
-
+/* PreviewGestures disambiguates touch events received on RenderOverlay
+ * and dispatch them to the proper recipient (i.e. zoom renderer or pie renderer).
+ * Touch events on CameraControls will be handled by framework.
+ * */
 public class PreviewGestures
         implements ScaleGestureDetector.OnScaleGestureListener {
 
@@ -58,23 +60,55 @@
     private MotionEvent mDown;
     private MotionEvent mCurrent;
     private ScaleGestureDetector mScale;
-    private List<View> mReceivers;
-    private List<View> mUnclickableAreas;
     private int mMode;
     private int mSlop;
     private int mTapTimeout;
+    private boolean mZoomEnabled;
     private boolean mEnabled;
     private boolean mZoomOnly;
     private int mOrientation;
-    private int[] mLocation;
-    private SwipeListener mSwipeListener;
+    private GestureDetector mGestureDetector;
+
+    private GestureDetector.SimpleOnGestureListener mGestureListener = new GestureDetector.SimpleOnGestureListener() {
+        @Override
+        public void onLongPress (MotionEvent e) {
+            // Open pie
+            if (mPie != null && !mPie.showsItems()) {
+                openPie();
+            }
+        }
+
+        @Override
+        public boolean onSingleTapUp (MotionEvent e) {
+            // Tap to focus when pie is not open
+            if (mPie == null || !mPie.showsItems()) {
+                mTapListener.onSingleTapUp(null, (int) e.getX(), (int) e.getY());
+                return true;
+            }
+            return false;
+        }
+
+        @Override
+        public boolean onScroll (MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
+            if (mMode == MODE_ZOOM) return false;
+            int deltaX = (int) (e1.getX() - e2.getX());
+            int deltaY = (int) (e1.getY() - e2.getY());
+            if (deltaY > 2 * deltaX && deltaY > -2 * deltaX) {
+                // Open pie on swipe up
+                if (mPie != null && !mPie.showsItems()) {
+                    openPie();
+                    return true;
+                }
+            }
+            return false;
+        }
+    };
 
     private Handler mHandler = new Handler() {
         public void handleMessage(Message msg) {
             if (msg.what == MSG_PIE) {
                 mMode = MODE_PIE;
                 openPie();
-                cancelActivityTouchHandling(mDown);
             }
         }
     };
@@ -83,12 +117,8 @@
         public void onSingleTapUp(View v, int x, int y);
     }
 
-    interface SwipeListener {
-        public void onSwipe(int direction);
-    }
-
     public PreviewGestures(CameraActivity ctx, SingleTapListener tapListener,
-            ZoomRenderer zoom, PieRenderer pie, SwipeListener swipe) {
+            ZoomRenderer zoom, PieRenderer pie) {
         mActivity = ctx;
         mTapListener = tapListener;
         mPie = pie;
@@ -98,8 +128,7 @@
         mSlop = (int) ctx.getResources().getDimension(R.dimen.pie_touch_slop);
         mTapTimeout = ViewConfiguration.getTapTimeout();
         mEnabled = true;
-        mLocation = new int[2];
-        mSwipeListener = swipe;
+        mGestureDetector = new GestureDetector(mGestureListener);
     }
 
     public void setRenderOverlay(RenderOverlay overlay) {
@@ -112,184 +141,51 @@
 
     public void setEnabled(boolean enabled) {
         mEnabled = enabled;
-        if (!enabled) {
-            cancelPie();
-        }
+    }
+
+    public void setZoomEnabled(boolean enable) {
+        mZoomEnabled = enable;
     }
 
     public void setZoomOnly(boolean zoom) {
         mZoomOnly = zoom;
     }
 
-    public void addTouchReceiver(View v) {
-        if (mReceivers == null) {
-            mReceivers = new ArrayList<View>();
-        }
-        mReceivers.add(v);
-    }
-
-    public void removeTouchReceiver(View v) {
-        if (mReceivers == null || v == null) return;
-        mReceivers.remove(v);
-    }
-
-    public void addUnclickableArea(View v) {
-        if (mUnclickableAreas == null) {
-            mUnclickableAreas = new ArrayList<View>();
-        }
-        mUnclickableAreas.add(v);
-    }
-
-    public void clearTouchReceivers() {
-        if (mReceivers != null) {
-            mReceivers.clear();
-        }
-    }
-
-    public void clearUnclickableAreas() {
-        if (mUnclickableAreas != null) {
-            mUnclickableAreas.clear();
-        }
-    }
-
-    private boolean checkClickable(MotionEvent m) {
-        if (mUnclickableAreas != null) {
-            for (View v : mUnclickableAreas) {
-                if (isInside(m, v)) {
-                    return false;
-                }
-            }
-        }
-        return true;
-    }
-
-    public void reset() {
-        clearTouchReceivers();
-        clearUnclickableAreas();
+    public boolean isEnabled() {
+        return mEnabled;
     }
 
     public boolean dispatchTouch(MotionEvent m) {
         if (!mEnabled) {
-            return mActivity.superDispatchTouchEvent(m);
+            return false;
         }
         mCurrent = m;
         if (MotionEvent.ACTION_DOWN == m.getActionMasked()) {
-            if (checkReceivers(m)) {
-                mMode = MODE_MODULE;
-                return mActivity.superDispatchTouchEvent(m);
-            } else {
-                mMode = MODE_ALL;
-                mDown = MotionEvent.obtain(m);
-                if (mPie != null && mPie.showsItems()) {
-                    mMode = MODE_PIE;
-                    return sendToPie(m);
-                }
-                if (mPie != null && !mZoomOnly && checkClickable(m)) {
-                    mHandler.sendEmptyMessageDelayed(MSG_PIE, TIMEOUT_PIE);
-                }
-                if (mZoom != null) {
-                    mScale.onTouchEvent(m);
-                }
-                // make sure this is ok
-                return mActivity.superDispatchTouchEvent(m);
-            }
-        } else if (mMode == MODE_NONE) {
-            return false;
-        } else if (mMode == MODE_SWIPE) {
-            if (MotionEvent.ACTION_UP == m.getActionMasked()) {
-                mSwipeListener.onSwipe(getSwipeDirection(m));
-            }
-            return true;
-        } else if (mMode == MODE_PIE) {
-            if (MotionEvent.ACTION_POINTER_DOWN == m.getActionMasked()) {
-                sendToPie(makeCancelEvent(m));
-                if (mZoom != null) {
-                    onScaleBegin(mScale);
-                }
-            } else {
-                return sendToPie(m);
-            }
-            return true;
-        } else if (mMode == MODE_ZOOM) {
-            mScale.onTouchEvent(m);
-            if (!mScale.isInProgress() && MotionEvent.ACTION_POINTER_UP == m.getActionMasked()) {
-                mMode = MODE_NONE;
-                onScaleEnd(mScale);
-            }
-            return true;
-        } else if (mMode == MODE_MODULE) {
-            return mActivity.superDispatchTouchEvent(m);
-        } else {
-            // didn't receive down event previously;
-            // assume module wasn't initialzed and ignore this event.
-            if (mDown == null) {
-                return true;
-            }
-            if (MotionEvent.ACTION_POINTER_DOWN == m.getActionMasked()) {
-                if (!mZoomOnly) {
-                    cancelPie();
-                    sendToPie(makeCancelEvent(m));
-                }
-                if (mZoom != null) {
-                    mScale.onTouchEvent(m);
-                    onScaleBegin(mScale);
-                }
-            } else if ((mMode == MODE_ZOOM) && !mScale.isInProgress()
-                    && MotionEvent.ACTION_POINTER_UP == m.getActionMasked()) {
-                // user initiated and stopped zoom gesture without zooming
-                mScale.onTouchEvent(m);
-                onScaleEnd(mScale);
-            }
-            // not zoom or pie mode and no timeout yet
-            if (mZoom != null) {
-                boolean res = mScale.onTouchEvent(m);
-                if (mScale.isInProgress()) {
-                    cancelPie();
-                    cancelActivityTouchHandling(m);
-                    return res;
-                }
-            }
-            if (MotionEvent.ACTION_UP == m.getActionMasked()) {
-                cancelPie();
-                // must have been tap
-                if (m.getEventTime() - mDown.getEventTime() < mTapTimeout
-                        && checkClickable(m)) {
-                    cancelActivityTouchHandling(m);
-                    mTapListener.onSingleTapUp(null,
-                            (int) mDown.getX() - mOverlay.getWindowPositionX(),
-                            (int) mDown.getY() - mOverlay.getWindowPositionY());
-                    return true;
-                } else {
-                    return mActivity.superDispatchTouchEvent(m);
-                }
-            } else if (MotionEvent.ACTION_MOVE == m.getActionMasked()) {
-                if ((Math.abs(m.getX() - mDown.getX()) > mSlop)
-                        || Math.abs(m.getY() - mDown.getY()) > mSlop) {
-                    // moved too far and no timeout yet, no focus or pie
-                    cancelPie();
-                    int dir = getSwipeDirection(m);
-                    if (dir == DIR_LEFT) {
-                        mMode = MODE_MODULE;
-                        return mActivity.superDispatchTouchEvent(m);
-                    } else {
-                        cancelActivityTouchHandling(m);
-                        mMode = MODE_NONE;
-                    }
-                }
-            }
-            return false;
+            mMode = MODE_NONE;
+            mDown = MotionEvent.obtain(m);
         }
-    }
 
-    private boolean checkReceivers(MotionEvent m) {
-        if (mReceivers != null) {
-            for (View receiver : mReceivers) {
-                if (isInside(m, receiver)) {
-                    return true;
+        // If pie is open, redirects all the touch events to pie.
+        if (mPie != null && mPie.isOpen()) {
+            return sendToPie(m);
+        }
+
+        // If pie is not open, send touch events to gesture detector and scale
+        // listener to recognize the gesture.
+        mGestureDetector.onTouchEvent(m);
+        if (mZoom != null) {
+            mScale.onTouchEvent(m);
+            if (MotionEvent.ACTION_POINTER_DOWN == m.getActionMasked()) {
+                mMode = MODE_ZOOM;
+                if (mZoomEnabled) {
+                    // Start showing zoom UI as soon as there is a second finger down
+                    mZoom.onScaleBegin(mScale);
                 }
+            } else if (MotionEvent.ACTION_POINTER_UP == m.getActionMasked()) {
+                mZoom.onScaleEnd(mScale);
             }
         }
-        return false;
+        return true;
     }
 
     // left tests for finger moving right to left
@@ -320,25 +216,6 @@
         return DIR_UP;
     }
 
-    private boolean isInside(MotionEvent evt, View v) {
-        v.getLocationInWindow(mLocation);
-        // when view is flipped horizontally
-        if ((int) v.getRotationY() == 180) {
-            mLocation[0] -= v.getWidth();
-        }
-        // when view is flipped vertically
-        if ((int) v.getRotationX() == 180) {
-            mLocation[1] -= v.getHeight();
-        }
-        return (v.getVisibility() == View.VISIBLE
-                && evt.getX() >= mLocation[0] && evt.getX() < mLocation[0] + v.getWidth()
-                && evt.getY() >= mLocation[1] && evt.getY() < mLocation[1] + v.getHeight());
-    }
-
-    public void cancelActivityTouchHandling(MotionEvent m) {
-        mActivity.superDispatchTouchEvent(makeCancelEvent(m));
-    }
-
     private MotionEvent makeCancelEvent(MotionEvent m) {
         MotionEvent c = MotionEvent.obtain(m);
         c.setAction(MotionEvent.ACTION_CANCEL);
@@ -346,21 +223,16 @@
     }
 
     private void openPie() {
-        mDown.offsetLocation(-mOverlay.getWindowPositionX(),
-                -mOverlay.getWindowPositionY());
+        mGestureDetector.onTouchEvent(makeCancelEvent(mDown));
+        mScale.onTouchEvent(makeCancelEvent(mDown));
         mOverlay.directDispatchTouch(mDown, mPie);
     }
 
-    private void cancelPie() {
-        mHandler.removeMessages(MSG_PIE);
-    }
-
     private boolean sendToPie(MotionEvent m) {
-        m.offsetLocation(-mOverlay.getWindowPositionX(),
-                -mOverlay.getWindowPositionY());
         return mOverlay.directDispatchTouch(m, mPie);
     }
 
+    // OnScaleGestureListener implementation
     @Override
     public boolean onScale(ScaleGestureDetector detector) {
         return mZoom.onScale(detector);
@@ -368,21 +240,18 @@
 
     @Override
     public boolean onScaleBegin(ScaleGestureDetector detector) {
-        if (mMode != MODE_ZOOM) {
+        if (mPie == null || !mPie.isOpen()) {
             mMode = MODE_ZOOM;
-            cancelActivityTouchHandling(mCurrent);
-        }
-        if (mCurrent.getActionMasked() != MotionEvent.ACTION_MOVE) {
+            mGestureDetector.onTouchEvent(makeCancelEvent(mCurrent));
+            if (!mZoomEnabled) return false;
             return mZoom.onScaleBegin(detector);
-        } else {
-            return true;
         }
+        return false;
     }
 
     @Override
     public void onScaleEnd(ScaleGestureDetector detector) {
-        if (mCurrent.getActionMasked() != MotionEvent.ACTION_MOVE) {
-            mZoom.onScaleEnd(detector);
-        }
+        mZoom.onScaleEnd(detector);
     }
 }
+