001 | /* |
002 | * Copyright (C) 2011 The Rexsee Open Source Project |
003 | * |
004 | * Licensed under the Rexsee License, Version 1.0 (the "License"); |
005 | * you may not use this file except in compliance with the License. |
006 | * You may obtain a copy of the License at |
007 | * |
008 | * http://www.rexsee.com/CN/legal/license.html |
009 | * |
010 | * Unless required by applicable law or agreed to in writing, software |
011 | * distributed under the License is distributed on an "AS IS" BASIS, |
012 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
013 | * See the License for the specific language governing permissions and |
014 | * limitations under the License. |
015 | */ |
016 | |
017 | package rexsee.sensor; |
018 | |
019 | import rexsee.core.browser.JavascriptInterface; |
020 | import rexsee.core.browser.RexseeBrowser; |
021 | import android.content.Context; |
022 | import android.hardware.Sensor; |
023 | import android.hardware.SensorEvent; |
024 | import android.hardware.SensorEventListener; |
025 | import android.hardware.SensorManager; |
026 | |
027 | public class RexseeSensorOrientation implements JavascriptInterface { |
028 | |
029 | public static final String INTERFACE_NAME = "Orientation"; |
030 | @Override |
031 | public String getInterfaceName() { |
032 | return mBrowser.application.resources.prefix + INTERFACE_NAME; |
033 | } |
034 | @Override |
035 | public JavascriptInterface getInheritInterface(RexseeBrowser childBrowser) { |
036 | return this; |
037 | } |
038 | @Override |
039 | public JavascriptInterface getNewInterface(RexseeBrowser childBrowser) { |
040 | return new RexseeSensorOrientation(childBrowser); |
041 | } |
042 | |
043 | public static final String EVENT_ONORIENTATIONCHANGED = "onOrientationChanged"; |
044 | |
045 | private final Context mContext; |
046 | private final RexseeBrowser mBrowser; |
047 | private final SensorManager mSensorManager; |
048 | private SensorEventListener mSensorListener; |
049 | private final Sensor mSensor; |
050 | |
051 | private int mRate = SensorManager.SENSOR_DELAY_NORMAL; |
052 | private int mCycle = 100; //milliseconds |
053 | private int mEventCycle = 100; //milliseconds |
054 | |
055 | private float mAccuracyX = 0; |
056 | private float mAccuracyY = 0; |
057 | private float mAccuracyZ = 0; |
058 | |
059 | private long lastUpdate = -1; |
060 | private long lastEvent = -1; |
061 | |
062 | private float x = -999f; |
063 | private float y = -999f; |
064 | private float z = -999f; |
065 | |
066 | public RexseeSensorOrientation(RexseeBrowser browser) { |
067 | mContext = browser.getContext(); |
068 | mBrowser = browser; |
069 | browser.eventList.add(EVENT_ONORIENTATIONCHANGED); |
070 | |
071 | mSensorManager = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE); |
072 | |
073 | mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION); |
074 | mSensorListener = new SensorEventListener() { |
075 | @Override |
076 | public void onAccuracyChanged(Sensor sensor, int accuracy) { |
077 | } |
078 | @Override |
079 | public void onSensorChanged(SensorEvent event) { |
080 | if (event.sensor.getType() != Sensor.TYPE_ORIENTATION) return; |
081 | long curTime = System.currentTimeMillis(); |
082 | if (lastUpdate == -1 || (curTime - lastUpdate) > mCycle) { |
083 | lastUpdate = curTime; |
084 | float lastX = x; |
085 | float lastY = y; |
086 | float lastZ = z; |
087 | x = event.values[SensorManager.DATA_X]; |
088 | y = event.values[SensorManager.DATA_Y]; |
089 | z = event.values[SensorManager.DATA_Z]; |
090 | if (lastEvent == -1 || (curTime - lastEvent) > mEventCycle) { |
091 | if ( |
092 | (mAccuracyX >= 0 && Math.abs(x - lastX) > mAccuracyX) |
093 | || (mAccuracyY >= 0 && Math.abs(y - lastY) > mAccuracyY) |
094 | || (mAccuracyZ >= 0 && Math.abs(z - lastZ) > mAccuracyZ) |
095 | ) { |
096 | lastEvent = curTime; |
097 | mBrowser.eventList.run(EVENT_ONORIENTATIONCHANGED); |
098 | } |
099 | } |
100 | } |
101 | } |
102 | }; |
103 | |
104 | } |
105 | |
106 | public String getLastKnownX() { |
107 | return (x == -999) ? "null" : String.valueOf(x); |
108 | } |
109 | public String getLastKnownY() { |
110 | return (y == -999) ? "null" : String.valueOf(y); |
111 | } |
112 | public String getLastKnownZ() { |
113 | return (z == -999) ? "null" : String.valueOf(z); |
114 | } |
115 | |
116 | public void setRate(String rate) { |
117 | mRate = SensorRate.getInt(rate); |
118 | } |
119 | public String getRate() { |
120 | return SensorRate.getString(mRate); |
121 | } |
122 | public void setCycle(int milliseconds) { |
123 | mCycle = milliseconds; |
124 | } |
125 | public int getCycle() { |
126 | return mCycle; |
127 | } |
128 | public void setEventCycle(int milliseconds) { |
129 | mEventCycle = milliseconds; |
130 | } |
131 | public int getEventCycle() { |
132 | return mEventCycle; |
133 | } |
134 | public void setAccuracy(float x, float y, float z) { |
135 | mAccuracyX = x; |
136 | mAccuracyY = y; |
137 | mAccuracyZ = z; |
138 | } |
139 | public float getAccuracyX() { |
140 | return mAccuracyX; |
141 | } |
142 | public float getAccuracyY() { |
143 | return mAccuracyY; |
144 | } |
145 | public float getAccuracyZ() { |
146 | return mAccuracyZ; |
147 | } |
148 | |
149 | public boolean isReady() { |
150 | return (mSensor == null) ? false : true; |
151 | } |
152 | public void start() { |
153 | if (isReady()) { |
154 | mSensorManager.registerListener(mSensorListener, mSensor, mRate); |
155 | } else { |
156 | mBrowser.exception(getInterfaceName(), "Orientation sensor is not found."); |
157 | } |
158 | } |
159 | public void stop() { |
160 | if (isReady()) { |
161 | mSensorManager.unregisterListener(mSensorListener); |
162 | } |
163 | } |
164 | |
165 | } |
联系客服