Search in sources :

Example 11 with SensorEventListener

use of android.hardware.SensorEventListener in project android_frameworks_base by DirtyUnicorns.

the class PowerManagerService method runPostProximityCheck.

private void runPostProximityCheck(final Runnable r) {
    if (mSensorManager == null) {
        r.run();
        return;
    }
    synchronized (mProximityWakeLock) {
        mProximityWakeLock.acquire();
        mProximityListener = new SensorEventListener() {

            @Override
            public void onSensorChanged(SensorEvent event) {
                cleanupProximityLocked();
                if (!mHandler.hasMessages(MSG_WAKE_UP)) {
                    Slog.w(TAG, "Proximity sensor took too long, wake event already triggered!");
                    return;
                }
                mHandler.removeMessages(MSG_WAKE_UP);
                final float distance = event.values[0];
                if (distance >= PROXIMITY_NEAR_THRESHOLD || distance >= mProximitySensor.getMaximumRange()) {
                    r.run();
                } else {
                    Slog.w(TAG, "Not waking up. Proximity sensor is blocked.");
                }
            }

            @Override
            public void onAccuracyChanged(Sensor sensor, int accuracy) {
            // Do nothing
            }
        };
        mSensorManager.registerListener(mProximityListener, mProximitySensor, SensorManager.SENSOR_DELAY_FASTEST);
    }
}
Also used : SensorEventListener(android.hardware.SensorEventListener) SensorEvent(android.hardware.SensorEvent) Sensor(android.hardware.Sensor)

Example 12 with SensorEventListener

use of android.hardware.SensorEventListener in project AndroidLife by CaMnter.

the class SensorManagerActivity method initData.

/**
 * Initialize the Activity data
 */
@Override
protected void initData() {
    // 获取传感器管理器
    SensorManager sensorManager = (SensorManager) this.getSystemService(Context.SENSOR_SERVICE);
    // 获取默认加速度传感器
    Sensor linear = sensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);
    /**
     * 传感器监听
     * SensorEvent.values[0] = x
     * SensorEvent.values[1] = y
     * SensorEvent.values[2] = z
     */
    SensorEventListener listener = new SensorEventListener() {

        @Override
        public void onSensorChanged(SensorEvent event) {
            String info = "";
            for (int i = 0; i < event.values.length; i++) {
                info += "event.values[" + i + "]:" + event.values[i] + "\t";
            }
            Log.i("SensorManagerActivity", info);
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
        }
    };
    // 给对应传感器添加监听
    sensorManager.registerListener(listener, linear, SensorManager.SENSOR_DELAY_NORMAL);
    // 获得全部的传感器列表
    List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ALL);
    this.sensorManagerTV.setText(sensors.size() + "");
    for (Sensor sensor : sensors) {
        TextView title = new TextView(this);
        title.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        title.setTextSize(15);
        title.setTextColor(this.getResources().getColor(R.color.themeColor));
        switch(sensor.getType()) {
            case Sensor.TYPE_ACCELEROMETER:
                {
                    title.setText("1.加速度传感器" + "(" + sensor.getName() + ")");
                    this.addInfoView(sensor, title);
                    break;
                }
            case Sensor.TYPE_MAGNETIC_FIELD:
                {
                    title.setText("2.磁力传感器" + "(" + sensor.getName() + ")");
                    this.addInfoView(sensor, title);
                    break;
                }
            case Sensor.TYPE_ORIENTATION:
                {
                    title.setText("3.方向传感器" + "(" + sensor.getName() + ")");
                    this.addInfoView(sensor, title);
                    break;
                }
            case Sensor.TYPE_GYROSCOPE:
                {
                    title.setText("4.陀螺仪传感器" + "(" + sensor.getName() + ")");
                    this.addInfoView(sensor, title);
                    break;
                }
            case Sensor.TYPE_LIGHT:
                {
                    title.setText("5.光线感应传感器" + "(" + sensor.getName() + ")");
                    this.addInfoView(sensor, title);
                    break;
                }
            case Sensor.TYPE_PRESSURE:
                {
                    title.setText("6.压力传感器" + "(" + sensor.getName() + ")");
                    this.addInfoView(sensor, title);
                    break;
                }
            case Sensor.TYPE_TEMPERATURE:
                {
                    title.setText("7.温度传感器" + "(" + sensor.getName() + ")");
                    this.addInfoView(sensor, title);
                    break;
                }
            case Sensor.TYPE_PROXIMITY:
                {
                    title.setText("8.距离传感器" + "(" + sensor.getName() + ")");
                    this.addInfoView(sensor, title);
                    break;
                }
            case Sensor.TYPE_GRAVITY:
                {
                    title.setText("9.重力传感器" + "(" + sensor.getName() + ")");
                    this.addInfoView(sensor, title);
                    break;
                }
            case Sensor.TYPE_LINEAR_ACCELERATION:
                {
                    title.setText("10.线性加速度传感器" + "(" + sensor.getName() + ")");
                    this.addInfoView(sensor, title);
                    break;
                }
            case Sensor.TYPE_ROTATION_VECTOR:
                {
                    title.setText("11.旋转矢量传感器" + "(" + sensor.getName() + ")");
                    this.addInfoView(sensor, title);
                    break;
                }
        }
    }
}
Also used : SensorManager(android.hardware.SensorManager) ViewGroup(android.view.ViewGroup) SensorEventListener(android.hardware.SensorEventListener) TextView(android.widget.TextView) SensorEvent(android.hardware.SensorEvent) Sensor(android.hardware.Sensor)

Example 13 with SensorEventListener

use of android.hardware.SensorEventListener in project robolectric by robolectric.

the class ShadowSensorManagerTest method shouldReturnHasNoListenerAfterUnregisterListener.

@Test
public void shouldReturnHasNoListenerAfterUnregisterListener() {
    SensorEventListener listener = registerListener();
    sensorManager.unregisterListener(listener, sensorManager.getDefaultSensor(SensorManager.SENSOR_ACCELEROMETER));
    assertThat(shadow.hasListener(listener)).isFalse();
}
Also used : SensorEventListener(android.hardware.SensorEventListener) Test(org.junit.Test)

Example 14 with SensorEventListener

use of android.hardware.SensorEventListener in project robolectric by robolectric.

the class ShadowSensorManagerTest method shouldReturnHasNoListenerAfterUnregisterListenerWithoutSpecificSensor.

@Test
public void shouldReturnHasNoListenerAfterUnregisterListenerWithoutSpecificSensor() {
    SensorEventListener listener = registerListener();
    sensorManager.unregisterListener(listener);
    assertThat(shadow.hasListener(listener)).isFalse();
}
Also used : SensorEventListener(android.hardware.SensorEventListener) Test(org.junit.Test)

Example 15 with SensorEventListener

use of android.hardware.SensorEventListener in project android_packages_apps_CMParts by LineageOS.

the class KeyHandler method processEvent.

private void processEvent(final int action) {
    mProximityWakeLock.acquire();
    mSensorManager.registerListener(new SensorEventListener() {

        @Override
        public void onSensorChanged(SensorEvent event) {
            mProximityWakeLock.release();
            mSensorManager.unregisterListener(this);
            if (!mEventHandler.hasMessages(GESTURE_REQUEST)) {
                // The sensor took too long; ignoring
                return;
            }
            mEventHandler.removeMessages(GESTURE_REQUEST);
            if (event.values[0] == mProximitySensor.getMaximumRange()) {
                Message msg = getMessageForAction(action);
                mEventHandler.sendMessage(msg);
            }
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // Ignore
        }
    }, mProximitySensor, SensorManager.SENSOR_DELAY_FASTEST);
}
Also used : Message(android.os.Message) SensorEventListener(android.hardware.SensorEventListener) SensorEvent(android.hardware.SensorEvent) Sensor(android.hardware.Sensor)

Aggregations

SensorEventListener (android.hardware.SensorEventListener)16 Sensor (android.hardware.Sensor)10 SensorEvent (android.hardware.SensorEvent)8 Test (org.junit.Test)5 TextView (android.widget.TextView)3 View (android.view.View)2 AlertDialog (android.app.AlertDialog)1 DialogInterface (android.content.DialogInterface)1 SensorManager (android.hardware.SensorManager)1 Message (android.os.Message)1 ViewGroup (android.view.ViewGroup)1 CircularProgressView (com.github.rahatarmanahmed.cpv.CircularProgressView)1