Back to project page rcdroid.
The source code is released under:
Copyright (c) 2013, Jamie Furness (jamie@jamierf.co.uk) All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following...
If you think the Android project rcdroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.jamierf.rcdroid.input.sensor; /* w w w .j a va 2 s. c o m*/ import android.hardware.SensorManager; import com.google.common.base.Optional; import com.google.common.collect.Lists; import com.jamierf.rcdroid.input.sensor.listener.SensorListener; import java.util.Collection; public abstract class AbstractSensor<T> { private final Collection<SensorListener<? extends AbstractSensor<T>>> listeners; protected long timestamp; protected int accuracy; protected AbstractSensor() { listeners = Lists.newLinkedList(); timestamp = 0; accuracy = 0; } public void addListener(SensorListener<? extends AbstractSensor<T>> listener) { synchronized (listeners) { listeners.add(listener); } } public void removeListener(SensorListener<? extends AbstractSensor<T>> listener) { synchronized (listeners) { listeners.remove(listener); } } protected void setValueChanged(long timestamp) { synchronized (listeners) { this.timestamp = timestamp; for (SensorListener listener : listeners) { listener.onSensorChanged(this); } } } public void forceUpdate() { this.setValueChanged(System.currentTimeMillis()); } public abstract Optional<T> getValue(); public synchronized long getTimestamp() { return timestamp; } public synchronized int getAccuracy() { return accuracy; } public synchronized boolean isHighAccuracy() { return accuracy == SensorManager.SENSOR_STATUS_ACCURACY_HIGH; } public synchronized boolean isMediumAccuracy() { return accuracy == SensorManager.SENSOR_STATUS_ACCURACY_MEDIUM; } public synchronized boolean isLowAccuracy() { return accuracy == SensorManager.SENSOR_STATUS_ACCURACY_LOW; } public synchronized boolean isNoAccuracy() { return accuracy == 0; } public void close() { // No-op } }