[size=1em][size=1em]/*
[size=1em]* Copyright (C) 2011 The Rexsee Open Source Project
[size=1em]*
[size=1em]* Licensed under the Rexsee License, Version 1.0 (the "License");
[size=1em]* you may not use this file except in compliance with the License.
[size=1em]* You may obtain a copy of the License at
[size=1em]*
[size=1em]*
http://www.rexsee.com/CN/legal/license.html
[size=1em]*
[size=1em]* Unless required by applicable law or agreed to in writing, software
[size=1em]* distributed under the License is distributed on an "AS IS" BASIS,
[size=1em]* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
[size=1em]* See the License for the specific language governing permissions and
[size=1em]* limitations under the License.
[size=1em]*/
[size=1em]
[size=1em]package rexsee.sensor;
[size=1em]
[size=1em]import rexsee.core.browser.JavascriptInterface;
[size=1em]import rexsee.core.browser.RexseeBrowser;
[size=1em]import android.content.Context;
[size=1em]import android.hardware.Sensor;
[size=1em]import android.hardware.SensorEvent;
[size=1em]import android.hardware.SensorEventListener;
[size=1em]import android.hardware.SensorManager;
[size=1em]
[size=1em]public class RexseeSensorTemperature implements JavascriptInterface {
[size=1em]
[size=1em] private static final String INTERFACE_NAME = "Temperature";
[size=1em] @Override
[size=1em] public String getInterfaceName() {
[size=1em] return mBrowser.application.resources.prefix + INTERFACE_NAME;
[size=1em] }
[size=1em] @Override
[size=1em] public JavascriptInterface getInheritInterface(RexseeBrowser childBrowser) {
[size=1em] return this;
[size=1em] }
[size=1em] @Override
[size=1em] public JavascriptInterface getNewInterface(RexseeBrowser childBrowser) {
[size=1em] return new RexseeSensorTemperature(childBrowser);
[size=1em] }
[size=1em]
[size=1em] public static final String EVENT_ONTEMPERATURECHANGED = "onTemperatureChanged";
[size=1em]
[size=1em] private final Context mContext;
[size=1em] private final RexseeBrowser mBrowser;
[size=1em] private final SensorManager mSensorManager;
[size=1em] private final SensorEventListener mSensorListener;
[size=1em] private final Sensor mSensor;
[size=1em]
[size=1em] private int mRate = SensorManager.SENSOR_DELAY_NORMAL;
[size=1em] private int mCycle = 100; //milliseconds
[size=1em] private int mEventCycle = 100; //milliseconds
[size=1em] private float mAccuracy = 0;
[size=1em]
[size=1em] private long lastUpdate = -1;
[size=1em] private long lastEvent = -1;
[size=1em]
[size=1em] private float value = -999f;
[size=1em]
[size=1em] public RexseeSensorTemperature(RexseeBrowser browser) {
[size=1em] mContext = browser.getContext();
[size=1em] mBrowser = browser;
[size=1em] browser.eventList.add(EVENT_ONTEMPERATURECHANGED);
[size=1em]
[size=1em] mSensorManager = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE);
[size=1em]
[size=1em] mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_TEMPERATURE);
[size=1em]
[size=1em] mSensorListener = new SensorEventListener() {
[size=1em] @Override
[size=1em] public void onAccuracyChanged(Sensor sensor, int accuracy) {
[size=1em] }
[size=1em] @Override
[size=1em] public void onSensorChanged(SensorEvent event) {
[size=1em] if (event.sensor.getType() != Sensor.TYPE_TEMPERATURE) return;
[size=1em] long curTime = System.currentTimeMillis();
[size=1em] if (lastUpdate == -1 || (curTime - lastUpdate) > mCycle) {
[size=1em] lastUpdate = curTime;
[size=1em] float lastValue = value;
[size=1em] value = event.values[SensorManager.DATA_X];
[size=1em] if (lastEvent == -1 || (curTime - lastEvent) > mEventCycle) {
[size=1em] if (Math.abs(value - lastValue) > mAccuracy) {
[size=1em] lastEvent = curTime;
[size=1em] mBrowser.eventList.run(EVENT_ONTEMPERATURECHANGED);
[size=1em] }
[size=1em] }
[size=1em] }
[size=1em] }
[size=1em] };
[size=1em]
[size=1em] }
[size=1em]
[size=1em] public String getLastKnownValue() {
[size=1em] return (value == -999) ? "null" : String.valueOf(value);
[size=1em] }
[size=1em]
[size=1em] public void setRate(String rate) {
[size=1em] mRate = SensorRate.getInt(rate);
[size=1em] }
[size=1em] public String getRate() {
[size=1em] return SensorRate.getString(mRate);
[size=1em] }
[size=1em] public void setCycle(int milliseconds) {
[size=1em] mCycle = milliseconds;
[size=1em] }
[size=1em] public int getCycle() {
[size=1em] return mCycle;
[size=1em] }
[size=1em] public void setEventCycle(int milliseconds) {
[size=1em] mEventCycle = milliseconds;
[size=1em] }
[size=1em] public int getEventCycle() {
[size=1em] return mEventCycle;
[size=1em] }
[size=1em] public void setAccuracy(float value) {
[size=1em] mAccuracy = Math.abs(value);
[size=1em] }
[size=1em] public float getAccuracy() {
[size=1em] return mAccuracy;
[size=1em] }
[size=1em]
[size=1em] public boolean isReady() {
[size=1em] return (mSensor == null) ? false : true;
[size=1em] }
[size=1em] public void start() {
[size=1em] if (isReady()) {
[size=1em] mSensorManager.registerListener(mSensorListener, mSensor, mRate);
[size=1em] } else {
[size=1em] mBrowser.exception(getInterfaceName(), "Temperature sensor is not found.");
[size=1em] }
[size=1em] }
[size=1em] public void stop() {
[size=1em] if (isReady()) {
[size=1em] mSensorManager.unregisterListener(mSensorListener);
[size=1em] }
[size=1em] }
[size=1em]
[size=1em]}