Merge "Use calculated frame offset instead of gyroscope." into ics-mr0
diff --git a/src/com/android/camera/panorama/MosaicFrameProcessor.java b/src/com/android/camera/panorama/MosaicFrameProcessor.java
index 7660f5e..300e4e3 100644
--- a/src/com/android/camera/panorama/MosaicFrameProcessor.java
+++ b/src/com/android/camera/panorama/MosaicFrameProcessor.java
@@ -65,7 +65,8 @@
     private int mPreviewBufferSize;
 
     public interface ProgressListener {
-        public void onProgress(boolean isFinished, float panningRateX, float panningRateY);
+        public void onProgress(boolean isFinished, float panningRateX, float panningRateY,
+                float progressX, float progressY);
     }
 
     public MosaicFrameProcessor(int previewWidth, int previewHeight, int bufSize) {
@@ -173,11 +174,15 @@
 
                 // Publish progress of the ongoing processing
                 if (mProgressListener != null) {
-                    mProgressListener.onProgress(false, mPanningRateX, mPanningRateY);
+                    mProgressListener.onProgress(false, mPanningRateX, mPanningRateY,
+                            mTranslationLastX * HR_TO_LR_DOWNSAMPLE_FACTOR / mPreviewWidth,
+                            mTranslationLastY * HR_TO_LR_DOWNSAMPLE_FACTOR / mPreviewHeight);
                 }
             } else {
                 if (mProgressListener != null) {
-                    mProgressListener.onProgress(true, mPanningRateX, mPanningRateY);
+                    mProgressListener.onProgress(true, mPanningRateX, mPanningRateY,
+                            mTranslationLastX * HR_TO_LR_DOWNSAMPLE_FACTOR / mPreviewWidth,
+                            mTranslationLastY * HR_TO_LR_DOWNSAMPLE_FACTOR / mPreviewHeight);
                 }
             }
         }
@@ -211,20 +216,20 @@
         mTotalTranslationY += mDeltaY[idx];
         mTotalDeltaTime += mDeltaTime[idx];
 
-        mTranslationLastX = translationCurrX;
-        mTranslationLastY = translationCurrY;
-        mLastProcessedFrameTimestamp = now;
-        mOldestIdx = (mOldestIdx + 1) % WINDOW_SIZE;
-
         // The panning rate is measured as the rate of the translation percentage in
         // image width/height. Take the horizontal panning rate for example, the image width
         // used in finding the translation is (PreviewWidth / HR_TO_LR_DOWNSAMPLE_FACTOR).
         // To get the horizontal translation percentage, the horizontal translation,
         // (translationCurrX - mTranslationLastX), is divided by the
         // image width. We then get the rate by dividing the translation percentage with deltaTime.
-        mPanningRateX = mTotalTranslationX / (mPreviewWidth / HR_TO_LR_DOWNSAMPLE_FACTOR)
-                / mTotalDeltaTime;
-        mPanningRateY = mTotalTranslationY / (mPreviewHeight / HR_TO_LR_DOWNSAMPLE_FACTOR)
-                / mTotalDeltaTime;
+        mPanningRateX = mTotalTranslationX /
+                (mPreviewWidth / HR_TO_LR_DOWNSAMPLE_FACTOR) / mTotalDeltaTime;
+        mPanningRateY = mTotalTranslationY /
+                (mPreviewHeight / HR_TO_LR_DOWNSAMPLE_FACTOR) / mTotalDeltaTime;
+
+        mTranslationLastX = translationCurrX;
+        mTranslationLastY = translationCurrY;
+        mLastProcessedFrameTimestamp = now;
+        mOldestIdx = (mOldestIdx + 1) % WINDOW_SIZE;
     }
 }
diff --git a/src/com/android/camera/panorama/PanoramaActivity.java b/src/com/android/camera/panorama/PanoramaActivity.java
index f11bc3a..bbd45b8 100755
--- a/src/com/android/camera/panorama/PanoramaActivity.java
+++ b/src/com/android/camera/panorama/PanoramaActivity.java
@@ -49,8 +49,6 @@
 import android.hardware.Camera.Parameters;
 import android.hardware.Camera.Size;
 import android.hardware.Sensor;
-import android.hardware.SensorEvent;
-import android.hardware.SensorEventListener;
 import android.hardware.SensorManager;
 import android.net.Uri;
 import android.os.Bundle;
@@ -554,13 +552,14 @@
 
         mMosaicFrameProcessor.setProgressListener(new MosaicFrameProcessor.ProgressListener() {
             @Override
-            public void onProgress(boolean isFinished, float panningRateX, float panningRateY) {
+            public void onProgress(boolean isFinished, float panningRateX, float panningRateY,
+                    float progressX, float progressY) {
                 if (isFinished
                         || (mMaxAngleX - mMinAngleX >= DEFAULT_SWEEP_ANGLE)
                         || (mMaxAngleY - mMinAngleY >= DEFAULT_SWEEP_ANGLE)) {
                     stopCapture(false);
                 } else {
-                    updateProgress(panningRateX);
+                    updateProgress(panningRateX, progressX, progressY);
                 }
             }
         });
@@ -625,7 +624,7 @@
         mRightIndicator.setEnabled(false);
     }
 
-    private void updateProgress(float panningRate) {
+    private void updateProgress(float panningRate, float progressX, float progressY) {
         mMosaicView.setReady();
         mMosaicView.requestRender();
 
@@ -637,6 +636,7 @@
         } else {
             hideTooFastIndication();
         }
+        mPanoProgressBar.setProgress((int) (progressX * mHorizontalViewAngle));
     }
 
     private void createContentView() {
@@ -909,7 +909,6 @@
         releaseCamera();
         mMosaicView.onPause();
         clearMosaicFrameProcessorIfNeeded();
-        mSensorManager.unregisterListener(mListener);
         mOrientationEventListener.disable();
         System.gc();
     }
@@ -920,14 +919,6 @@
 
         mPausing = false;
         mOrientationEventListener.enable();
-        /*
-         * It is not necessary to get accelerometer events at a very high rate,
-         * by using a game rate (SENSOR_DELAY_UI), we get an automatic
-         * low-pass filter, which "extracts" the gravity component of the
-         * acceleration. As an added benefit, we use less power and CPU
-         * resources.
-         */
-        mSensorManager.registerListener(mListener, mSensor, SensorManager.SENSOR_DELAY_UI);
 
         mCaptureState = CAPTURE_STATE_VIEWFINDER;
         setupCamera();
@@ -938,55 +929,6 @@
         mMosaicView.onResume();
     }
 
-    private void updateCompassValue() {
-        if (mCaptureState == CAPTURE_STATE_VIEWFINDER) return;
-        // By what angle has the camera moved since start of capture?
-        mTraversedAngleX = (int) (mCompassValueX - mCompassValueXStart);
-        mTraversedAngleY = (int) (mCompassValueY - mCompassValueYStart);
-        mMinAngleX = Math.min(mMinAngleX, mTraversedAngleX);
-        mMaxAngleX = Math.max(mMaxAngleX, mTraversedAngleX);
-        mMinAngleY = Math.min(mMinAngleY, mTraversedAngleY);
-        mMaxAngleY = Math.max(mMaxAngleY, mTraversedAngleY);
-
-        // Use orientation to identify if the user is panning to the right or the left.
-        switch (mDeviceOrientation) {
-            case 0:
-                mPanoProgressBar.setProgress(-mTraversedAngleX);
-                break;
-            case 90:
-                mPanoProgressBar.setProgress(mTraversedAngleY);
-                break;
-            case 180:
-                mPanoProgressBar.setProgress(mTraversedAngleX);
-                break;
-            case 270:
-                mPanoProgressBar.setProgress(-mTraversedAngleY);
-                break;
-        }
-        mPanoProgressBar.invalidate();
-    }
-
-    private final SensorEventListener mListener = new SensorEventListener() {
-        public void onSensorChanged(SensorEvent event) {
-            if (event.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
-                if (mTimestamp != 0) {
-                    final float dT = (event.timestamp - mTimestamp) * NS2S;
-                    mCompassValueX += event.values[1] * dT * 180.0f / Math.PI;
-                    mCompassValueY += event.values[0] * dT * 180.0f / Math.PI;
-                    mCompassValueXStartBuffer = mCompassValueX;
-                    mCompassValueYStartBuffer = mCompassValueY;
-                    updateCompassValue();
-                }
-                mTimestamp = event.timestamp;
-
-            }
-        }
-
-        @Override
-        public void onAccuracyChanged(Sensor sensor, int accuracy) {
-        }
-    };
-
     public MosaicJpeg generateFinalMosaic(boolean highRes) {
         if (mMosaicFrameProcessor.createMosaic(highRes) == Mosaic.MOSAIC_RET_CANCELLED) {
             return null;