public boolean getHasCompass() { boolean compass = false; SensorManager sensorManager = TiSensorHelper.getSensorManager(); if (sensorManager != null) { compass = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION) != null; } else { compass = TiSensorHelper.hasDefaultSensor(geolocationModule.getActivity(), Sensor.TYPE_ORIENTATION); } return compass; }
public void getCurrentHeading(final KrollFunction listener) { if (listener != null) { final SensorEventListener oneShotHeadingListener = new SensorEventListener() { public void onAccuracyChanged(Sensor sensor, int accuracy) {} public void onSensorChanged(SensorEvent event) { if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) { long eventTimestamp = event.timestamp / 1000000; long actualTimestamp = baseTime.getTimeInMillis() + (eventTimestamp - sensorTimerStart); listener.callAsync( geolocationModule.getKrollObject(), new Object[] {eventToHashMap(event, actualTimestamp)}); TiSensorHelper.unregisterListener(Sensor.TYPE_ORIENTATION, this); } } }; updateDeclination(); TiSensorHelper.registerListener( Sensor.TYPE_ORIENTATION, oneShotHeadingListener, SensorManager.SENSOR_DELAY_UI); } }
public void onPause() { if (sensorAttached) { manageUpdateListener(false, updateListener); sensorHelper.detach(); sensorAttached = false; } }
public boolean hasCompass() { boolean compass = false; SensorManager sm = sensorHelper.getSensorManager(); if (sm != null) { compass = sm.getDefaultSensor(Sensor.TYPE_ORIENTATION) != null; } return compass; }
protected void manageUpdateListener(boolean register, SensorEventListener listener) { if (register) { if (!listeningForUpdate) { sensorAttached = sensorHelper.attach(proxy.getTiContext().getActivity()); if (sensorAttached) { LocationManager locationManager = (LocationManager) proxy.getTiContext().getActivity().getSystemService(Context.LOCATION_SERVICE); Criteria criteria = new Criteria(); String provider = locationManager.getBestProvider(criteria, true); if (provider != null) { Location location = locationManager.getLastKnownLocation(provider); if (location != null) { geomagneticField = new GeomagneticField( (float) location.getLatitude(), (float) location.getLongitude(), (float) location.getAltitude(), System.currentTimeMillis()); } } sensorHelper.registerListener(SENSORS, listener, SensorManager.SENSOR_DELAY_UI); listeningForUpdate = true; } } } else { if (listeningForUpdate) { sensorHelper.unregisterListener(SENSORS, listener); if (sensorHelper.isEmpty()) { listeningForUpdate = false; sensorHelper.detach(); } } } }
public void getCurrentHeading(final KrollCallback listener) { final SensorEventListener oneShotListener = new SensorEventListener() { public void onAccuracyChanged(Sensor sensor, int accuracy) {} public void onSensorChanged(SensorEvent event) { int type = event.sensor.getType(); if (type == Sensor.TYPE_ORIENTATION) { long ts = event.timestamp / 1000000; // nanos to millis listener.callWithProperties(eventToTiDict(event, ts)); sensorHelper.unregisterListener(SENSORS, this); manageUpdateListener(false, this); } } }; manageUpdateListener(true, oneShotListener); sensorHelper.registerListener(SENSORS, oneShotListener, 5000); }
@Override public void eventListenerRemoved(String type, int count, KrollProxy proxy) { if (accelerometerRegistered) { if (EVENT_UPDATE.equals(type)) { TiSensorHelper.unregisterListener(Sensor.TYPE_ACCELEROMETER, this); accelerometerRegistered = false; } } if (magneticFieldRegistered) { if (EVENT_UPDATE.equals(type)) { TiSensorHelper.unregisterListener(Sensor.TYPE_MAGNETIC_FIELD, this); magneticFieldRegistered = false; } } if (ambiantTemperatureRegistered) { if (EVENT_UPDATE.equals(type)) { TiSensorHelper.unregisterListener(Sensor.TYPE_AMBIENT_TEMPERATURE, this); ambiantTemperatureRegistered = false; } } if (gameRotationVectorRegistered) { if (EVENT_UPDATE.equals(type)) { TiSensorHelper.unregisterListener(Sensor.TYPE_GAME_ROTATION_VECTOR, this); gameRotationVectorRegistered = false; } } if (geomagneticRotationVectorRegistered) { if (EVENT_UPDATE.equals(type)) { TiSensorHelper.unregisterListener(Sensor.TYPE_GEOMAGNETIC_ROTATION_VECTOR, this); geomagneticRotationVectorRegistered = false; } } if (gravityRegistered) { if (EVENT_UPDATE.equals(type)) { TiSensorHelper.unregisterListener(Sensor.TYPE_GRAVITY, this); gravityRegistered = false; } } if (gyroscopeRegistered) { if (EVENT_UPDATE.equals(type)) { TiSensorHelper.unregisterListener(Sensor.TYPE_GYROSCOPE, this); gyroscopeRegistered = false; } } if (gyroscopeUncalibratedRegistered) { if (EVENT_UPDATE.equals(type)) { TiSensorHelper.unregisterListener(Sensor.TYPE_GYROSCOPE_UNCALIBRATED, this); gyroscopeUncalibratedRegistered = false; } } /*if (heartRateRegistered) { if (EVENT_UPDATE.equals(type)) { TiSensorHelper.unregisterListener(Sensor.TYPE_HEART_RATE, this); heartRateRegistered = false; } }*/ if (lightRegistered) { if (EVENT_UPDATE.equals(type)) { TiSensorHelper.unregisterListener(Sensor.TYPE_LIGHT, this); lightRegistered = false; } } if (linearAccelerationRegistered) { if (EVENT_UPDATE.equals(type)) { TiSensorHelper.unregisterListener(Sensor.TYPE_LINEAR_ACCELERATION, this); linearAccelerationRegistered = false; } } if (magneticFieldUncalibratedRegistered) { if (EVENT_UPDATE.equals(type)) { TiSensorHelper.unregisterListener(Sensor.TYPE_MAGNETIC_FIELD_UNCALIBRATED, this); magneticFieldUncalibratedRegistered = false; } } if (orientationRegistered) { if (EVENT_UPDATE.equals(type)) { TiSensorHelper.unregisterListener(Sensor.TYPE_ORIENTATION, this); orientationRegistered = false; } } if (pressureRegistered) { if (EVENT_UPDATE.equals(type)) { TiSensorHelper.unregisterListener(Sensor.TYPE_PRESSURE, this); pressureRegistered = false; } } if (proximityRegistered) { if (EVENT_UPDATE.equals(type)) { TiSensorHelper.unregisterListener(Sensor.TYPE_PROXIMITY, this); proximityRegistered = false; } } if (relativeHumidityRegistered) { if (EVENT_UPDATE.equals(type)) { TiSensorHelper.unregisterListener(Sensor.TYPE_RELATIVE_HUMIDITY, this); relativeHumidityRegistered = false; } } if (rotationVectorRegistered) { if (EVENT_UPDATE.equals(type)) { TiSensorHelper.unregisterListener(Sensor.TYPE_ROTATION_VECTOR, this); rotationVectorRegistered = false; } } if (significantMotionRegistered) { if (EVENT_UPDATE.equals(type)) { TiSensorHelper.unregisterListener(Sensor.TYPE_SIGNIFICANT_MOTION, this); significantMotionRegistered = false; } } if (stepCounterRegistered) { if (EVENT_UPDATE.equals(type)) { TiSensorHelper.unregisterListener(Sensor.TYPE_STEP_COUNTER, this); stepCounterRegistered = false; } } if (stepDetectorRegistered) { if (EVENT_UPDATE.equals(type)) { TiSensorHelper.unregisterListener(Sensor.TYPE_STEP_DETECTOR, this); stepDetectorRegistered = false; } } setflashLightOff(); super.eventListenerRemoved(type, count, proxy); }
@Override public void eventListenerAdded(String type, int count, final KrollProxy proxy) { if (accelerometerEnabled && !accelerometerRegistered) { if (EVENT_UPDATE.equals(type)) { Log.i(LCAT, "registerListener for ACCELEROMETER:"); accelerationFilter = new MeanFilter(); // Used for gyro cals accelerationFilter.setWindowSize(10); // Used for gyro cals TiSensorHelper.registerListener( Sensor.TYPE_ACCELEROMETER, this, SensorManager.SENSOR_DELAY_UI); // TiSensorHelper.registerListener(Sensor.TYPE_MAGNETIC_FIELD, this, // SensorManager.SENSOR_DELAY_UI); accelerometerRegistered = true; } } if (magneticFieldEnabled && !magneticFieldRegistered) { if (EVENT_UPDATE.equals(type)) { Log.i(LCAT, "registerListener for MAGNETIC_FIELD:"); magneticFilter = new MeanFilter(); // Used for gyro cals magneticFilter.setWindowSize(10); // Used for gyro cals TiSensorHelper.registerListener( Sensor.TYPE_MAGNETIC_FIELD, this, SensorManager.SENSOR_DELAY_UI); magneticFieldRegistered = true; } } if (ambiantTemperatureEnabled && !ambiantTemperatureRegistered) { if (EVENT_UPDATE.equals(type)) { Log.i(LCAT, "registerListener for TYPE_AMBIENT_TEMPERATURE:"); TiSensorHelper.registerListener( Sensor.TYPE_AMBIENT_TEMPERATURE, this, SensorManager.SENSOR_DELAY_UI); ambiantTemperatureRegistered = true; } } if (gameRotationVectorEnabled && !gameRotationVectorRegistered) { if (EVENT_UPDATE.equals(type)) { Log.i(LCAT, "registerListener for TYPE_GAME_ROTATION_VECTOR:"); TiSensorHelper.registerListener( Sensor.TYPE_GAME_ROTATION_VECTOR, this, SensorManager.SENSOR_DELAY_UI); gameRotationVectorRegistered = true; } } if (geomagneticRotationVectorEnabled && !geomagneticRotationVectorRegistered) { if (EVENT_UPDATE.equals(type)) { Log.i(LCAT, "registerListener for TYPE_GEOMAGNETIC_ROTATION_VECTOR:"); TiSensorHelper.registerListener( Sensor.TYPE_GEOMAGNETIC_ROTATION_VECTOR, this, SensorManager.SENSOR_DELAY_UI); geomagneticRotationVectorRegistered = true; } } if (gravityEnabled && !gravityRegistered) { if (EVENT_UPDATE.equals(type)) { Log.i(LCAT, "registerListener for TYPE_GRAVITY:"); TiSensorHelper.registerListener(Sensor.TYPE_GRAVITY, this, SensorManager.SENSOR_DELAY_UI); gravityRegistered = true; } } if (gyroscopeEnabled && !gyroscopeRegistered) { if (EVENT_UPDATE.equals(type)) { Log.i(LCAT, "registerListener for TYPE_GYROSCOPE:"); initGyroMaths(); TiSensorHelper.registerListener(Sensor.TYPE_GYROSCOPE, this, SensorManager.SENSOR_DELAY_UI); gyroscopeRegistered = true; } } if (gyroscopeUncalibratedEnabled && !gyroscopeUncalibratedRegistered) { if (EVENT_UPDATE.equals(type)) { Log.i(LCAT, "registerListener for TYPE_GYROSCOPE_UNCALIBRATED:"); TiSensorHelper.registerListener( Sensor.TYPE_GYROSCOPE_UNCALIBRATED, this, SensorManager.SENSOR_DELAY_UI); gyroscopeUncalibratedRegistered = true; } } /*if (heartRateEnabled && !heartRateRegistered) { if (EVENT_UPDATE.equals(type)) { Log.i(LCAT, "registerListener for TYPE_HEART_RATE:"); TiSensorHelper.registerListener(Sensor.TYPE_HEART_RATE, this, SensorManager.SENSOR_DELAY_UI); heartRateRegistered = true; } }*/ if (lightEnabled && !lightRegistered) { if (EVENT_UPDATE.equals(type)) { Log.i(LCAT, "registerListener for TYPE_LIGHT:"); TiSensorHelper.registerListener(Sensor.TYPE_LIGHT, this, SensorManager.SENSOR_DELAY_UI); lightRegistered = true; } } if (linearAccelerationEnabled && !linearAccelerationRegistered) { if (EVENT_UPDATE.equals(type)) { Log.i(LCAT, "registerListener for TYPE_LINEAR_ACCELERATION:"); TiSensorHelper.registerListener( Sensor.TYPE_LINEAR_ACCELERATION, this, SensorManager.SENSOR_DELAY_UI); linearAccelerationRegistered = true; } } if (magneticFieldUncalibratedEnabled && !magneticFieldUncalibratedRegistered) { if (EVENT_UPDATE.equals(type)) { Log.i(LCAT, "registerListener for TYPE_MAGNETIC_FIELD_UNCALIBRATED:"); TiSensorHelper.registerListener( Sensor.TYPE_MAGNETIC_FIELD_UNCALIBRATED, this, SensorManager.SENSOR_DELAY_UI); magneticFieldUncalibratedRegistered = true; } } if (orientationEnabled && !orientationRegistered) { if (EVENT_UPDATE.equals(type)) { Log.i(LCAT, "registerListener for TYPE_ORIENTATION:"); TiSensorHelper.registerListener( Sensor.TYPE_ORIENTATION, this, SensorManager.SENSOR_DELAY_UI); orientationRegistered = true; } } if (pressureEnabled && !pressureRegistered) { if (EVENT_UPDATE.equals(type)) { Log.i(LCAT, "registerListener for TYPE_PRESSURE:"); TiSensorHelper.registerListener(Sensor.TYPE_PRESSURE, this, SensorManager.SENSOR_DELAY_UI); pressureRegistered = true; } } if (proximityEnabled && !proximityRegistered) { if (EVENT_UPDATE.equals(type)) { Log.i(LCAT, "registerListener for TYPE_PROXIMITY:"); TiSensorHelper.registerListener(Sensor.TYPE_PROXIMITY, this, SensorManager.SENSOR_DELAY_UI); proximityRegistered = true; } } if (relativeHumidityEnabled && !relativeHumidityRegistered) { if (EVENT_UPDATE.equals(type)) { Log.i(LCAT, "registerListener for TYPE_RELATIVE_HUMIDITY:"); TiSensorHelper.registerListener( Sensor.TYPE_RELATIVE_HUMIDITY, this, SensorManager.SENSOR_DELAY_UI); relativeHumidityRegistered = true; } } if (rotationVectorEnabled && !rotationVectorRegistered) { if (EVENT_UPDATE.equals(type)) { Log.i(LCAT, "registerListener for TYPE_ROTATION_VECTOR:"); TiSensorHelper.registerListener( Sensor.TYPE_ROTATION_VECTOR, this, SensorManager.SENSOR_DELAY_UI); rotationVectorRegistered = true; } } if (significantMotionEnabled && !significantMotionRegistered) { if (EVENT_UPDATE.equals(type)) { Log.i(LCAT, "registerListener for TYPE_SIGNIFICANT_MOTION:"); mSensor = sensorManager.getDefaultSensor(Sensor.TYPE_SIGNIFICANT_MOTION); mTriggerEventListener = new TriggerEventListener() { @Override public void onTrigger(TriggerEvent event) { KrollDict data = new KrollDict(); data.put("sType", Sensor.TYPE_SIGNIFICANT_MOTION); significantMotionValues = event.values.clone(); data.put("motion", significantMotionValues[0]); fireEvent(EVENT_UPDATE, data); sensorManager.requestTriggerSensor(mTriggerEventListener, mSensor); } }; sensorManager.requestTriggerSensor(mTriggerEventListener, mSensor); significantMotionRegistered = true; } } if (stepCounterEnabled && !stepCounterRegistered) { if (EVENT_UPDATE.equals(type)) { Log.i(LCAT, "registerListener for TYPE_STEP_COUNTER:"); TiSensorHelper.registerListener( Sensor.TYPE_STEP_COUNTER, this, SensorManager.SENSOR_DELAY_FASTEST); stepCounterRegistered = true; } } if (stepDetectorEnabled && !stepDetectorRegistered) { if (EVENT_UPDATE.equals(type)) { Log.i(LCAT, "registerListener for TYPE_STEP_DETECTOR:"); TiSensorHelper.registerListener( Sensor.TYPE_STEP_DETECTOR, this, SensorManager.SENSOR_DELAY_FASTEST); stepDetectorRegistered = true; } } super.eventListenerAdded(type, count, proxy); }
public void unregisterListener() { TiSensorHelper.unregisterListener(Sensor.TYPE_ORIENTATION, this); }
public void registerListener() { updateDeclination(); TiSensorHelper.registerListener(Sensor.TYPE_ORIENTATION, this, SensorManager.SENSOR_DELAY_UI); }