sensor android JAVA_SensorService.java

/*

* Copyright (C) 2008 The Android Open Source Project

*

* Licensed under the Apache License, Version 2.0 (the "License");

* you may not use this file except in compliance with the License.

* You may obtain a copy of the License at

*

* http://www.apache.org/licenses/LICENSE-2.0

*

* Unless required by applicable law or agreed to in writing, software

* distributed under the License is distributed on an "AS IS" BASIS,

* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

* See the License for the specific language governing permissions and

* limitations under the License.

*/

package com.android.server;

import android.content.Context;

import android.hardware.ISensorService;

import android.os.Binder;

import android.os.Bundle;

import android.os.RemoteException;

import android.os.IBinder;

import android.util.Config;

import android.util.Log;

import java.util.ArrayList;

import com.android.internal.app.IBatteryStats;

import com.android.server.am.BatteryStatsService;

/**

* 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.

*/

class SensorService extends ISensorService.Stub {

static final String TAG = SensorService.class.getSimpleName();

private static final boolean DEBUG = false;

private static final boolean localLOGV = DEBUG ? Config.LOGD : Config.LOGV;

private static final int SENSOR_DISABLE = -1;

/**

* Battery statistics to be updated when sensors are enabled and disabled.

*/

final IBatteryStats mBatteryStats = BatteryStatsService.getService();

private final class Listener implements IBinder.DeathRecipient {

final IBinder mToken;

int mSensors = 0;

int mDelay = 0x7FFFFFFF;

Listener(IBinder token) {

mToken = token;

}

void addSensor(int sensor, int delay) {

mSensors |= (1

mDelay = delay;

}

void removeSensor(int sensor) {

mSensors &= ~(1

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;

}

void deactivateIfUnused(int sensor) throws RemoteException {

int size = mListeners.size();

for (int i=0 ; imListeners = new ArrayList();

private static native int _sensors_control_init();

private static native Bundle _sensors_control_open();

private static native boolean _sensors_control_activate(int sensor, boolean activate);

private static native int _sensors_control_set_delay(int ms);

private static native int _sensors_control_wake();

}

Java程序

|

198行

|

6.64 KB

/*

* Copyright (C) 2008 The Android Open Source Project

*

* Licensed under the Apache License, Version 2.0 (the "License");

* you may not use this file except in compliance with the License.

* You may obtain a copy of the License at

*

* http://www.apache.org/licenses/LICENSE-2.0

*

* Unless required by applicable law or agreed to in writing, software

* distributed under the License is distributed on an "AS IS" BASIS,

* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

* See the License for the specific language governing permissions and

* limitations under the License.

*/

package com.android.server;

import android.content.Context;

import android.hardware.ISensorService;

import android.os.Binder;

import android.os.Bundle;

import android.os.RemoteException;

import android.os.IBinder;

import android.util.Config;

import android.util.Log;

import java.util.ArrayList;

import com.android.internal.app.IBatteryStats;

import com.android.server.am.BatteryStatsService;

/**

* 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.

*/

class SensorService extends ISensorService.Stub {

static final String TAG = SensorService.class.getSimpleName();

private static final boolean DEBUG = false;

private static final boolean localLOGV = DEBUG ? Config.LOGD : Config.LOGV;

private static final int SENSOR_DISABLE = -1;

/**

* Battery statistics to be updated when sensors are enabled and disabled.

*/

final IBatteryStats mBatteryStats = BatteryStatsService.getService();

private final class Listener implements IBinder.DeathRecipient {

final IBinder mToken;

int mSensors = 0;

int mDelay = 0x7FFFFFFF;

Listener(IBinder token) {

mToken = token;

}

void addSensor(int sensor, int delay) {

mSensors |= (1<

if (mDelay > delay)

mDelay = delay;

}

void removeSensor(int sensor) {

mSensors &= ~(1<

}

boolean hasSensor(int sensor) {

return ((mSensors & (1<

}

public void binderDied() {

if (localLOGV) Log.d(TAG, "sensor listener died");

synchronized(mListeners) {

mListeners.remove(this);

mToken.unlinkToDeath(this, 0);

// go through the lists of sensors used by the listener that

// died and deactivate them.

for (int sensor=0 ; sensor<32 && mSensors!=0 ; sensor++) {

if (hasSensor(sensor)) {

removeSensor(sensor);

try {

deactivateIfUnused(sensor);

} catch (RemoteException e) {

Log.w(TAG, "RemoteException in binderDied");

}

}

}

mListeners.notify();

}

}

}

@SuppressWarnings("unused")

public SensorService(Context context) {

if (localLOGV) Log.d(TAG, "SensorService startup");

_sensors_control_init();

}

public Bundle getDataChannel() throws RemoteException {

return _sensors_control_open();

}

public boolean enableSensor(IBinder binder, String name, int sensor, int enable)

throws RemoteException {

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;

}

void deactivateIfUnused(int sensor) throws RemoteException {

int size = mListeners.size();

for (int i=0 ; i

if (mListeners.get(i).hasSensor(sensor))

return;

}

_sensors_control_activate(sensor, false);

}

ArrayList mListeners = new ArrayList();

private static native int _sensors_control_init();

private static native Bundle _sensors_control_open();

private static native boolean _sensors_control_activate(int sensor, boolean activate);

private static native int _sensors_control_set_delay(int ms);

private static native int _sensors_control_wake();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值