public void attachListeners() {
//开启监听
final SensorManager sm = getSensorManager();
if(Settings.System.getInt(getContentResolver(),Settings.System.KEY_MAKECALL_EAR,0) == 1){
//检测是否功能打开
mAnswerListener.reset();
sm.registerListener(mAnswerListener,
sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);
sm.registerListener(mPrSensorListener,
sm.getDefaultSensor(Sensor.TYPE_PROXIMITY),
SensorManager.SENSOR_DELAY_NORMAL);
}
}
private SensorManager getSensorManager() {
//获取sensor
return (SensorManager) getSystemService(Context.SENSOR_SERVICE);
}
public void detachListeners() {
//关闭监听
final SensorManager sm = getSensorManager();
sm.unregisterListener(mAnswerListener);
sm.unregisterListener(mPrSensorListener);
}
private interface ResettableSensorEventListener extends SensorEventListener {
public void reset();
}
private final ResettableSensorEventListener mAnswerListener = new ResettableSensorEventListener() {
// Our accelerometers are not quite accurate.
private static final int XY_GRAVITY_THRESHOLD = 10;
private static final int Z_GRAVITY_THRESHOLD = 3;
private boolean stopped;
private boolean isInitialHorizontal = false;
private boolean[] samples = new boolean[SENSOR_SAMPLES];
private int index = 0;
@Override
public void onAccuracyChanged(Sensor sensor, int acc) {
}
@Override
public void reset() {
stopped = false;
isInitialHorizontal = false;
for (int i = 0; i < SENSOR_SAMPLES; i++) {
samples[i] = false;
}
}
private boolean filterSamples() {
int trues = 0;
for (boolean sample : samples) {
if(sample) {
++trues;
}
}
return trues >= MIN_ACCEPT_COUNT;
}
@Override
public void onSensorChanged(SensorEvent event) {
float x = event.values[SensorManager.DATA_X];
float y = event.values[SensorManager.DATA_Y];
float z = event.values[SensorManager.DATA_Z];
if (stopped) {
return;
}
if(!isInitialHorizontal){
isInitialHorizontal = (Math.abs(x) + Math.abs(y)) < 3;
}
samples[index] = ((Math.abs(x) + y) > XY_GRAVITY_THRESHOLD)
&& (Math.abs(z) < Z_GRAVITY_THRESHOLD);
Log.d(TAG, "mAnswerListener-samples[" +index + "]" + samples[index]);
index = ((index + 1) % SENSOR_SAMPLES);
if (filterSamples() && mIsPositive && (isInitialHorizontal && Math.abs(x) > 4)) {
stopped = true;
for (int i = 0; i < SENSOR_SAMPLES; i++) {
samples[i] = false;
}
handleAction();
}
}
};
private void handleAction(){
//拨打电话,优先使用卡1拨打
final boolean hasCard1 = ContactPhoneUtils.hasEnabledIccCard(SimContactsConstants.SUB_1);
final boolean hasCard2 = ContactPhoneUtils.hasEnabledIccCard(SimContactsConstants.SUB_2);
if(hasCard1){
dialButtonPressed(SimContactsConstants.SUB_1);
} else if(hasCard2){
dialButtonPressed(SimContactsConstants.SUB_2);
} else {
dialButtonPressed(SimContactsConstants.SUB_1);
}
}
private final SensorEventListener mPrSensorListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
final float distance = event.values[0];
mIsPositive = distance >= 0.0f && distance < 5.0f;
Log.d(TAG, "mProximitySensorListener-mIsPositive" + mIsPositive);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// Not used.
}
};