Search in sources :

Example 31 with Sensor

use of android.hardware.Sensor in project cordova-android-chromeview by thedracle.

the class AccelListener method start.

//--------------------------------------------------------------------------
// LOCAL METHODS
//--------------------------------------------------------------------------
//
/**
     * Start listening for acceleration sensor.
     * 
     * @return          status of listener
    */
private int start() {
    // If already starting or running, then just return
    if ((this.status == AccelListener.RUNNING) || (this.status == AccelListener.STARTING)) {
        return this.status;
    }
    this.setStatus(AccelListener.STARTING);
    // Get accelerometer from sensor manager
    List<Sensor> list = this.sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER);
    // If found, then register as listener
    if ((list != null) && (list.size() > 0)) {
        this.mSensor = list.get(0);
        this.sensorManager.registerListener(this, this.mSensor, SensorManager.SENSOR_DELAY_UI);
        this.setStatus(AccelListener.STARTING);
    } else {
        this.setStatus(AccelListener.ERROR_FAILED_TO_START);
        this.fail(AccelListener.ERROR_FAILED_TO_START, "No sensors found to register accelerometer listening to.");
        return this.status;
    }
    // Set a timeout callback on the main thread.
    Handler handler = new Handler(Looper.getMainLooper());
    handler.postDelayed(new Runnable() {

        public void run() {
            AccelListener.this.timeout();
        }
    }, 2000);
    return this.status;
}
Also used : Handler(android.os.Handler) Sensor(android.hardware.Sensor)

Example 32 with Sensor

use of android.hardware.Sensor 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 33 with Sensor

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

the class ShadowSensorManagerTest method getSensor_shouldBeConfigurable.

@Test
public void getSensor_shouldBeConfigurable() {
    Sensor sensor = Shadow.newInstanceOf(Sensor.class);
    shadowOf(sensorManager).addSensor(Sensor.TYPE_ACCELEROMETER, sensor);
    assertThat(sensor).isSameAs(sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER));
}
Also used : Sensor(android.hardware.Sensor) Test(org.junit.Test)

Example 34 with Sensor

use of android.hardware.Sensor in project Resurrection_packages_apps_Settings by ResurrectionRemix.

the class GestureSettings method hasSensor.

private static boolean hasSensor(Context context, int nameResId, int vendorResId) {
    Resources resources = context.getResources();
    String name = resources.getString(nameResId);
    String vendor = resources.getString(vendorResId);
    if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(vendor)) {
        SensorManager sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
        for (Sensor s : sensorManager.getSensorList(Sensor.TYPE_ALL)) {
            if (name.equals(s.getName()) && vendor.equals(s.getVendor())) {
                return true;
            }
        }
    }
    return false;
}
Also used : SensorManager(android.hardware.SensorManager) Resources(android.content.res.Resources) Sensor(android.hardware.Sensor)

Example 35 with Sensor

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

the class SensorPowerCalculator method calculateApp.

@Override
public void calculateApp(BatterySipper app, BatteryStats.Uid u, long rawRealtimeUs, long rawUptimeUs, int statsType) {
    // Process Sensor usage
    final SparseArray<? extends BatteryStats.Uid.Sensor> sensorStats = u.getSensorStats();
    final int NSE = sensorStats.size();
    for (int ise = 0; ise < NSE; ise++) {
        final BatteryStats.Uid.Sensor sensor = sensorStats.valueAt(ise);
        final int sensorHandle = sensorStats.keyAt(ise);
        final BatteryStats.Timer timer = sensor.getSensorTime();
        final long sensorTime = timer.getTotalTimeLocked(rawRealtimeUs, statsType) / 1000;
        switch(sensorHandle) {
            case BatteryStats.Uid.Sensor.GPS:
                app.gpsTimeMs = sensorTime;
                app.gpsPowerMah = (app.gpsTimeMs * mGpsPowerOn) / (1000 * 60 * 60);
                break;
            default:
                final int sensorsCount = mSensors.size();
                for (int i = 0; i < sensorsCount; i++) {
                    final Sensor s = mSensors.get(i);
                    if (s.getHandle() == sensorHandle) {
                        app.sensorPowerMah += (sensorTime * s.getPower()) / (1000 * 60 * 60);
                        break;
                    }
                }
                break;
        }
    }
}
Also used : BatteryStats(android.os.BatteryStats) Sensor(android.hardware.Sensor)

Aggregations

Sensor (android.hardware.Sensor)36 SensorManager (android.hardware.SensorManager)7 SensorEventListener (android.hardware.SensorEventListener)5 BatteryStats (android.os.BatteryStats)5 SensorEvent (android.hardware.SensorEvent)4 Intent (android.content.Intent)2 Handler (android.os.Handler)2 ArrayList (java.util.ArrayList)2 NotificationChannel (android.app.NotificationChannel)1 NotificationManager (android.app.NotificationManager)1 SharedPreferences (android.content.SharedPreferences)1 Resources (android.content.res.Resources)1 ViewPager (android.support.v4.view.ViewPager)1 View (android.view.View)1 ViewTreeObserver (android.view.ViewTreeObserver)1 Window (android.view.Window)1 AdapterView (android.widget.AdapterView)1 TextView (android.widget.TextView)1 InjectView (butterknife.InjectView)1 BmobUser (cn.bmob.v3.BmobUser)1