1/*
2 * Copyright (C) 2010 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
17package com.android.gallery3d.app;
18
19import android.content.Context;
20import android.hardware.Sensor;
21import android.hardware.SensorEvent;
22import android.hardware.SensorEventListener;
23import android.hardware.SensorManager;
24import android.os.SystemClock;
25import android.util.FloatMath;
26import android.view.Display;
27import android.view.Surface;
28import android.view.WindowManager;
29
30import com.android.gallery3d.common.Utils;
31import com.android.gallery3d.util.GalleryUtils;
32
33public class EyePosition {
34    private static final String TAG = "EyePosition";
35
36    public interface EyePositionListener {
37        public void onEyePositionChanged(float x, float y, float z);
38    }
39
40    private static final float GYROSCOPE_THRESHOLD = 0.15f;
41    private static final float GYROSCOPE_LIMIT = 10f;
42    private static final int GYROSCOPE_SETTLE_DOWN = 15;
43    private static final float GYROSCOPE_RESTORE_FACTOR = 0.995f;
44
45    private static final float USER_ANGEL = (float) Math.toRadians(10);
46    private static final float USER_ANGEL_COS = FloatMath.cos(USER_ANGEL);
47    private static final float USER_ANGEL_SIN = FloatMath.sin(USER_ANGEL);
48    private static final float MAX_VIEW_RANGE = (float) 0.5;
49    private static final int NOT_STARTED = -1;
50
51    private static final float USER_DISTANCE_METER = 0.3f;
52
53    private Context mContext;
54    private EyePositionListener mListener;
55    private Display mDisplay;
56    // The eyes' position of the user, the origin is at the center of the
57    // device and the unit is in pixels.
58    private float mX;
59    private float mY;
60    private float mZ;
61
62    private final float mUserDistance; // in pixel
63    private final float mLimit;
64    private long mStartTime = NOT_STARTED;
65    private Sensor mSensor;
66    private PositionListener mPositionListener = new PositionListener();
67
68    private int mGyroscopeCountdown = 0;
69
70    public EyePosition(Context context, EyePositionListener listener) {
71        mContext = context;
72        mListener = listener;
73        mUserDistance = GalleryUtils.meterToPixel(USER_DISTANCE_METER);
74        mLimit = mUserDistance * MAX_VIEW_RANGE;
75
76        WindowManager wManager = (WindowManager) mContext
77                .getSystemService(Context.WINDOW_SERVICE);
78        mDisplay = wManager.getDefaultDisplay();
79
80        // The 3D effect where the photo albums fan out in 3D based on angle
81        // of device tilt is currently disabled.
82/*
83        SensorManager sManager = (SensorManager) mContext
84                .getSystemService(Context.SENSOR_SERVICE);
85        mSensor = sManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
86        if (mSensor == null) {
87            Log.w(TAG, "no gyroscope, use accelerometer instead");
88            mSensor = sManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
89        }
90        if (mSensor == null) {
91            Log.w(TAG, "no sensor available");
92        }
93*/
94    }
95
96    public void resetPosition() {
97        mStartTime = NOT_STARTED;
98        mX = mY = 0;
99        mZ = -mUserDistance;
100        mListener.onEyePositionChanged(mX, mY, mZ);
101    }
102
103    /*
104     * We assume the user is at the following position
105     *
106     *              /|\  user's eye
107     *               |   /
108     *   -G(gravity) |  /
109     *               |_/
110     *             / |/_____\ -Y (-y direction of device)
111     *     user angel
112     */
113    private void onAccelerometerChanged(float gx, float gy, float gz) {
114
115        float x = gx, y = gy, z = gz;
116
117        switch (mDisplay.getRotation()) {
118            case Surface.ROTATION_90: x = -gy; y= gx; break;
119            case Surface.ROTATION_180: x = -gx; y = -gy; break;
120            case Surface.ROTATION_270: x = gy; y = -gx; break;
121        }
122
123        float temp = x * x + y * y + z * z;
124        float t = -y /temp;
125
126        float tx = t * x;
127        float ty = -1 + t * y;
128        float tz = t * z;
129
130        float length = FloatMath.sqrt(tx * tx + ty * ty + tz * tz);
131        float glength = FloatMath.sqrt(temp);
132
133        mX = Utils.clamp((x * USER_ANGEL_COS / glength
134                + tx * USER_ANGEL_SIN / length) * mUserDistance,
135                -mLimit, mLimit);
136        mY = -Utils.clamp((y * USER_ANGEL_COS / glength
137                + ty * USER_ANGEL_SIN / length) * mUserDistance,
138                -mLimit, mLimit);
139        mZ = -FloatMath.sqrt(
140                mUserDistance * mUserDistance - mX * mX - mY * mY);
141        mListener.onEyePositionChanged(mX, mY, mZ);
142    }
143
144    private void onGyroscopeChanged(float gx, float gy, float gz) {
145        long now = SystemClock.elapsedRealtime();
146        float distance = (gx > 0 ? gx : -gx) + (gy > 0 ? gy : - gy);
147        if (distance < GYROSCOPE_THRESHOLD
148                || distance > GYROSCOPE_LIMIT || mGyroscopeCountdown > 0) {
149            --mGyroscopeCountdown;
150            mStartTime = now;
151            float limit = mUserDistance / 20f;
152            if (mX > limit || mX < -limit || mY > limit || mY < -limit) {
153                mX *= GYROSCOPE_RESTORE_FACTOR;
154                mY *= GYROSCOPE_RESTORE_FACTOR;
155                mZ = (float) -Math.sqrt(
156                        mUserDistance * mUserDistance - mX * mX - mY * mY);
157                mListener.onEyePositionChanged(mX, mY, mZ);
158            }
159            return;
160        }
161
162        float t = (now - mStartTime) / 1000f * mUserDistance * (-mZ);
163        mStartTime = now;
164
165        float x = -gy, y = -gx;
166        switch (mDisplay.getRotation()) {
167            case Surface.ROTATION_90: x = -gx; y= gy; break;
168            case Surface.ROTATION_180: x = gy; y = gx; break;
169            case Surface.ROTATION_270: x = gx; y = -gy; break;
170        }
171
172        mX = Utils.clamp((float) (mX + x * t / Math.hypot(mZ, mX)),
173                -mLimit, mLimit) * GYROSCOPE_RESTORE_FACTOR;
174        mY = Utils.clamp((float) (mY + y * t / Math.hypot(mZ, mY)),
175                -mLimit, mLimit) * GYROSCOPE_RESTORE_FACTOR;
176
177        mZ = -FloatMath.sqrt(
178                mUserDistance * mUserDistance - mX * mX - mY * mY);
179        mListener.onEyePositionChanged(mX, mY, mZ);
180    }
181
182    private class PositionListener implements SensorEventListener {
183        public void onAccuracyChanged(Sensor sensor, int accuracy) {
184        }
185
186        public void onSensorChanged(SensorEvent event) {
187            switch (event.sensor.getType()) {
188                case Sensor.TYPE_GYROSCOPE: {
189                    onGyroscopeChanged(
190                            event.values[0], event.values[1], event.values[2]);
191                    break;
192                }
193                case Sensor.TYPE_ACCELEROMETER: {
194                    onAccelerometerChanged(
195                            event.values[0], event.values[1], event.values[2]);
196                }
197            }
198        }
199    }
200
201    public void pause() {
202        if (mSensor != null) {
203            SensorManager sManager = (SensorManager) mContext
204                    .getSystemService(Context.SENSOR_SERVICE);
205            sManager.unregisterListener(mPositionListener);
206        }
207    }
208
209    public void resume() {
210        if (mSensor != null) {
211            SensorManager sManager = (SensorManager) mContext
212                    .getSystemService(Context.SENSOR_SERVICE);
213            sManager.registerListener(mPositionListener,
214                    mSensor, SensorManager.SENSOR_DELAY_GAME);
215        }
216
217        mStartTime = NOT_STARTED;
218        mGyroscopeCountdown = GYROSCOPE_SETTLE_DOWN;
219        mX = mY = 0;
220        mZ = -mUserDistance;
221        mListener.onEyePositionChanged(mX, mY, mZ);
222    }
223}
224