Search in sources :

Example 1 with SensorEventListener

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

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 2 with SensorEventListener

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

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 3 with SensorEventListener

use of android.hardware.SensorEventListener in project HAR-Android by linw7.

the class StepActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_step);
    total_step = (TextView) findViewById(R.id.total_step);
    current_step = (TextView) findViewById(R.id.current_step);
    energy_step = (TextView) findViewById(R.id.energy_step);
    distance_step = (TextView) findViewById(R.id.distance_step);
    circular_button = (CircularProgressButton) findViewById(R.id.circular_button);
    final SensorEventListener step_listener = new SensorEventListener() {

        @Override
        public void onSensorChanged(SensorEvent sensorEvent) {
            if (sensorEvent.sensor.getType() == Sensor.TYPE_STEP_COUNTER) {
                if (new_start == false) {
                    new_start = true;
                    cur_step = (int) (sensorEvent.values[0]);
                }
                mCount = sensorEvent.values[0];
            }
            if (sensorEvent.sensor.getType() == Sensor.TYPE_STEP_DETECTOR) {
                if (sensorEvent.values[0] == 1.0) {
                    mDetector = mDetector + 1;
                }
            }
        }

        @Override
        public // 传感器精度改变时调用
        void onAccuracyChanged(Sensor sensor, int i) {
        }
    };
    mCount = 0;
    mDetector = 0;
    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    mStepCount = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
    mStepDetector = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR);
    mSensorManager.registerListener(step_listener, mStepCount, SensorManager.SENSOR_DELAY_FASTEST);
    mSensorManager.registerListener(step_listener, mStepDetector, SensorManager.SENSOR_DELAY_FASTEST);
    circular_button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (!runable) {
                runable = true;
                circular_button.setIndeterminateProgressMode(true);
                for (int i = 1; i < 100; i++) circular_button.setProgress(i);
                timer.schedule(display_task, 0, 100);
                circular_button.setProgress(100);
            } else {
                new AlertDialog.Builder(StepActivity.this).setTitle("请确认").setMessage("您正在处于步数监测状态,是否退出?").setPositiveButton(R.string.AlertDialog_yes, null).setNegativeButton(R.string.AlertDialog_no, null).show();
            }
        }
    });
}
Also used : SensorEventListener(android.hardware.SensorEventListener) TextView(android.widget.TextView) View(android.view.View) SensorEvent(android.hardware.SensorEvent) Sensor(android.hardware.Sensor)

Example 4 with SensorEventListener

use of android.hardware.SensorEventListener in project HAR-Android by linw7.

the class StepMainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_step_main);
    total_step = (TextView) findViewById(R.id.total_step);
    current_step = (TextView) findViewById(R.id.current_step);
    energy_step = (TextView) findViewById(R.id.energy_step);
    distance_step = (TextView) findViewById(R.id.distance_step);
    circular_button = (CircularProgressButton) findViewById(R.id.circular_button);
    progress_step = (ProgressBar) findViewById(R.id.progress_step);
    progress_energy = (ProgressBar) findViewById(R.id.progress_energy);
    progress_step_text = (TextView) findViewById(R.id.progress_step_text);
    progress_energy_text = (TextView) findViewById(R.id.progress_energy_text);
    progressView = (CircularProgressView) findViewById(R.id.progress_view);
    target_step = (TextView) findViewById(R.id.target_step);
    target_energy = (TextView) findViewById(R.id.target_energy);
    text_step = (TextView) findViewById(R.id.text_step);
    text_energy = (TextView) findViewById(R.id.text_energy);
    runable = false;
    circular_button.setIndeterminateProgressMode(true);
    // 设置目标
    target_step.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            AlertDialog dialog = new AlertDialog.Builder(StepMainActivity.this).setTitle("请设置步数目标").setIcon(R.drawable.logo_activity).setSingleChoiceItems(STEP, -1, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(StepMainActivity.this, STEP[which], Toast.LENGTH_SHORT).show();
                    if (which == 0)
                        set_step = 2000;
                    else if (which == 1)
                        set_step = 4000;
                    else if (which == 2)
                        set_step = 6000;
                    else if (which == 3)
                        set_step = 8000;
                    else if (which == 4)
                        set_step = 10000;
                    set_step = 100;
                    Log.i("step", "设置步数:" + set_step);
                    dialog.dismiss();
                }
            }).create();
            dialog.show();
        }
    });
    // 设置能量目标
    target_energy.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            AlertDialog dialog = new AlertDialog.Builder(StepMainActivity.this).setTitle("请设置卡路里目标").setIcon(R.drawable.logo_activity).setSingleChoiceItems(ENERGY, -1, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(StepMainActivity.this, ENERGY[which], Toast.LENGTH_SHORT).show();
                    if (which == 0)
                        set_energy = 300;
                    else if (which == 1)
                        set_energy = 600;
                    else if (which == 2)
                        set_energy = 900;
                    else if (which == 3)
                        set_energy = 1200;
                    else if (which == 4)
                        set_energy = 1500;
                    set_energy = 6;
                    dialog.dismiss();
                }
            }).create();
            dialog.show();
        }
    });
    // 传感器
    final SensorEventListener step_listener = new SensorEventListener() {

        @Override
        public void onSensorChanged(SensorEvent sensorEvent) {
            if (sensorEvent.sensor.getType() == Sensor.TYPE_STEP_COUNTER) {
                if (new_start == false) {
                    new_start = true;
                    cur_step = (int) (sensorEvent.values[0]);
                }
                mCount = sensorEvent.values[0];
            }
            if (sensorEvent.sensor.getType() == Sensor.TYPE_STEP_DETECTOR) {
                if (sensorEvent.values[0] == 1.0) {
                    mDetector = mDetector + 1;
                }
            }
        }

        @Override
        public // 传感器精度改变时调用
        void onAccuracyChanged(Sensor sensor, int i) {
        }
    };
    mCount = 0;
    mDetector = 0;
    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    mStepCount = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
    mStepDetector = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR);
    mSensorManager.registerListener(step_listener, mStepCount, SensorManager.SENSOR_DELAY_FASTEST);
    mSensorManager.registerListener(step_listener, mStepDetector, SensorManager.SENSOR_DELAY_FASTEST);
    timer.schedule(display_task, 0, 100);
    circular_button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (!runable) {
                run();
            } else {
                reset();
            }
        }
    });
}
Also used : AlertDialog(android.app.AlertDialog) DialogInterface(android.content.DialogInterface) SensorEventListener(android.hardware.SensorEventListener) CircularProgressView(com.github.rahatarmanahmed.cpv.CircularProgressView) TextView(android.widget.TextView) View(android.view.View) SensorEvent(android.hardware.SensorEvent) Sensor(android.hardware.Sensor)

Example 5 with SensorEventListener

use of android.hardware.SensorEventListener in project xDrip by NightscoutFoundation.

the class Reminders method onPause.

@Override
protected void onPause() {
    super.onPause();
    Log.d(TAG, "onPause");
    final SensorEventListener activity = this;
    JoH.runOnUiThreadDelayed(new Runnable() {

        @Override
        public void run() {
            Log.d(TAG, "Unregistering proximity sensor listener");
            try {
                mSensorManager.unregisterListener(activity);
            } catch (Exception e) {
                Log.d(TAG, "Error unregistering proximity listener: " + e);
            }
        }
    }, 10000);
}
Also used : SensorEventListener(android.hardware.SensorEventListener)

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