FileDocCategorySizeDatePackage
SensorService.javaAPI DocAndroid 1.5 API6837Wed May 06 22:42:00 BST 2009com.android.server

SensorService

public class SensorService extends ISensorService.Stub
Class that manages the device's sensors. It register clients and activate the needed sensors. The sensor events themselves are not broadcasted from this service, instead, a file descriptor is provided to each client they can read events from.

Fields Summary
static final String
TAG
private static final boolean
DEBUG
private static final boolean
localLOGV
private static final int
SENSOR_DISABLE
final com.android.internal.app.IBatteryStats
mBatteryStats
Battery statistics to be updated when sensors are enabled and disabled.
ArrayList
mListeners
Constructors Summary
public SensorService(android.content.Context context)

        if (localLOGV) Log.d(TAG, "SensorService startup");
        _sensors_control_init();
    
Methods Summary
private static native boolean_sensors_control_activate(int sensor, boolean activate)

private static native int_sensors_control_init()

private static native android.os.ParcelFileDescriptor_sensors_control_open()

private static native int_sensors_control_set_delay(int ms)

private static native int_sensors_control_wake()

voiddeactivateIfUnused(int sensor)

        int size = mListeners.size();
        for (int i=0 ; i<size ; i++) {
            if (mListeners.get(i).hasSensor(sensor))
                return;
        }
        _sensors_control_activate(sensor, false);
    
public booleanenableSensor(android.os.IBinder binder, java.lang.String name, int sensor, int enable)

        if (localLOGV) Log.d(TAG, "enableSensor " + name + "(#" + sensor + ") " + enable);
        
        // Inform battery statistics service of status change
        int uid = Binder.getCallingUid();
        long identity = Binder.clearCallingIdentity();
        if (enable == SENSOR_DISABLE) {
            mBatteryStats.noteStopSensor(uid, sensor);
        } else {
            mBatteryStats.noteStartSensor(uid, sensor);
        }
        Binder.restoreCallingIdentity(identity);

        if (binder == null) {
            Log.w(TAG, "listener is null (sensor=" + name + ", id=" + sensor + ")");
            return false;
        }

        synchronized(mListeners) {
            if (enable!=SENSOR_DISABLE && !_sensors_control_activate(sensor, true)) {
                Log.w(TAG, "could not enable sensor " + sensor);
                return false;
            }
                    
            Listener l = null;
            int minDelay = enable;
            for (Listener listener : mListeners) {
                if (binder == listener.mToken) {
                    l = listener;
                }
                if (minDelay > listener.mDelay)
                    minDelay = listener.mDelay;
            }
            
            if (l == null && enable!=SENSOR_DISABLE) {
                l = new Listener(binder);
                binder.linkToDeath(l, 0);
                mListeners.add(l);
                mListeners.notify();
            }
            
            if (l == null) {
                // by construction, this means we're disabling a listener we
                // don't know about...
                Log.w(TAG, "listener with binder " + binder + 
                        ", doesn't exist (sensor=" + name + ", id=" + sensor + ")");
                return false;
            }
            
            if (minDelay >= 0) {
                _sensors_control_set_delay(minDelay);
            }
            
            if (enable != SENSOR_DISABLE) {
                l.addSensor(sensor, enable);
            } else {
                l.removeSensor(sensor);
                deactivateIfUnused(sensor);
                if (l.mSensors == 0) {
                    mListeners.remove(l);
                    binder.unlinkToDeath(l, 0);
                    mListeners.notify();
                }
            }
            
            if (mListeners.size() == 0) {
                _sensors_control_wake();
            }
        }        
        return true;
    
public android.os.ParcelFileDescriptorgetDataChanel()

        return _sensors_control_open();