Search in sources :

Example 61 with SensorManager

use of android.hardware.SensorManager in project CalendarTrigger by rparkins999.

the class MuteService method checkOrientationWait.

// return true if still waiting for correct orientation
private boolean checkOrientationWait(int classNum, boolean before) {
    int wanted;
    if (before) {
        wanted = PrefsManager.getBeforeOrientation(this, classNum);
    } else {
        wanted = PrefsManager.getAfterOrientation(this, classNum);
    }
    if ((wanted == 0) || (wanted == PrefsManager.BEFORE_ANY_POSITION)) {
        return false;
    }
    switch(PrefsManager.getOrientationState(this)) {
        case // sensor currently not active
        PrefsManager.ORIENTATION_IDLE:
            SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE);
            Sensor ams = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
            if (ams == null) {
                return false;
            }
            lock();
            PrefsManager.setOrientationState(this, PrefsManager.ORIENTATION_WAITING);
            sm.registerListener(this, ams, SensorManager.SENSOR_DELAY_FASTEST);
            new MyLog(this, "Requested accelerometer value for class ".concat(PrefsManager.getClassName(this, classNum)));
        // FALLTHRU
        case // waiting for value
        PrefsManager.ORIENTATION_WAITING:
            return true;
        case // just got a value
        PrefsManager.ORIENTATION_DONE:
            nextAccelTime = System.currentTimeMillis() + FIVE_MINUTES;
            new MyLog(this, "accelerometerX = " + String.valueOf(accelerometerX) + ", accelerometerY = " + String.valueOf(accelerometerY) + ", accelerometerZ = " + String.valueOf(accelerometerZ));
            if ((accelerometerX >= -0.3) && (accelerometerX <= 0.3) && (accelerometerY >= -0.3) && (accelerometerY <= 0.3) && (accelerometerZ >= 9.6) && (accelerometerZ <= 10.0)) {
                if ((wanted & PrefsManager.BEFORE_FACE_UP) != 0) {
                    return false;
                }
            } else if ((accelerometerX >= -0.3) && (accelerometerX <= 0.3) && (accelerometerY >= -0.3) && (accelerometerY <= 0.3) && (accelerometerZ >= -10.0) && (accelerometerZ <= -9.6)) {
                if ((wanted & PrefsManager.BEFORE_FACE_DOWN) != 0) {
                    return false;
                }
            } else if ((wanted & PrefsManager.BEFORE_OTHER_POSITION) != 0) {
                return false;
            }
            nextAccelTime = System.currentTimeMillis() + FIVE_MINUTES;
            PrefsManager.setOrientationState(this, PrefsManager.ORIENTATION_IDLE);
            new MyLog(this, "Still waiting for orientation for class ".concat(PrefsManager.getClassName(this, classNum)));
            return true;
        default:
            return false;
    }
}
Also used : SensorManager(android.hardware.SensorManager) MyLog(uk.co.yahoo.p1rpp.calendartrigger.MyLog) Sensor(android.hardware.Sensor)

Example 62 with SensorManager

use of android.hardware.SensorManager in project CalendarTrigger by rparkins999.

the class MuteService method onSensorChanged.

@Override
public void onSensorChanged(SensorEvent sensorEvent) {
    switch(sensorEvent.sensor.getType()) {
        case Sensor.TYPE_STEP_COUNTER:
            {
                int newCounterSteps = (int) sensorEvent.values[0];
                if (newCounterSteps != PrefsManager.getStepCount(this)) {
                    PrefsManager.setStepCount(this, newCounterSteps);
                    startIfNecessary(this, "Step counter changed");
                }
            }
            break;
        case Sensor.TYPE_ACCELEROMETER:
            SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE);
            sm.unregisterListener(this);
            accelerometerX = sensorEvent.values[0];
            accelerometerY = sensorEvent.values[1];
            accelerometerZ = sensorEvent.values[2];
            PrefsManager.setOrientationState(this, PrefsManager.ORIENTATION_DONE);
            startIfNecessary(this, "Accelerometer event");
            break;
        default:
    }
}
Also used : SensorManager(android.hardware.SensorManager)

Example 63 with SensorManager

use of android.hardware.SensorManager in project CalendarTrigger by rparkins999.

the class MuteService method StartStepCounter.

// FIXME can we use a similar power saving trick as accelerometer?
// return true if step counter is now running
private boolean StartStepCounter(int classNum) {
    if (PrefsManager.getStepCount(this) == PrefsManager.STEP_COUNTER_IDLE) {
        SensorManager sensorManager = (SensorManager) getSystemService(Activity.SENSOR_SERVICE);
        Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER, true);
        if (sensor == null) {
            // if we can't get a wakeup step counter, try without
            sensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
            if (sensor == null) {
                // no step counter at all
                return false;
            }
        }
        if (sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL)) {
            new MyLog(this, "Step counter activated for class ".concat(PrefsManager.getClassName(this, classNum)));
            if (sensor.isWakeUpSensor()) {
                PrefsManager.setStepCount(this, PrefsManager.STEP_COUNTER_WAKEUP);
            } else {
                PrefsManager.setStepCount(this, PrefsManager.STEP_COUNTER_WAKE_LOCK);
                lock();
            }
            return true;
        } else {
            // could not activate step counter
            return false;
        }
    } else {
        // already starting it for another class
        return true;
    }
}
Also used : SensorManager(android.hardware.SensorManager) MyLog(uk.co.yahoo.p1rpp.calendartrigger.MyLog) Sensor(android.hardware.Sensor)

Example 64 with SensorManager

use of android.hardware.SensorManager in project android_packages_apps_Settings by crdroidandroid.

the class DoubleTwistPreferenceController method isGestureAvailable.

public static boolean isGestureAvailable(Context context) {
    final Resources resources = context.getResources();
    final String name = resources.getString(R.string.gesture_double_twist_sensor_name);
    final String vendor = resources.getString(R.string.gesture_double_twist_sensor_vendor);
    if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(vendor)) {
        final 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 65 with SensorManager

use of android.hardware.SensorManager in project QRCode by 5peak2me.

the class AmbientLightManager method stop.

void stop() {
    if (lightSensor != null) {
        SensorManager sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
        sensorManager.unregisterListener(this);
        cameraManager = null;
        lightSensor = null;
    }
}
Also used : SensorManager(android.hardware.SensorManager)

Aggregations

SensorManager (android.hardware.SensorManager)89 Sensor (android.hardware.Sensor)29 SharedPreferences (android.content.SharedPreferences)12 Resources (android.content.res.Resources)8 PowerManager (android.os.PowerManager)6 ContentResolver (android.content.ContentResolver)5 IntentFilter (android.content.IntentFilter)5 SystemSensorManager (android.hardware.SystemSensorManager)5 IPowerManager (android.os.IPowerManager)5 RemoteException (android.os.RemoteException)5 WindowManager (android.view.WindowManager)5 DisplayManagerInternal (android.hardware.display.DisplayManagerInternal)4 BatteryManagerInternal (android.os.BatteryManagerInternal)4 DreamManagerInternal (android.service.dreams.DreamManagerInternal)4 IVrManager (android.service.vr.IVrManager)4 Display (android.view.Display)4 WindowManagerPolicy (android.view.WindowManagerPolicy)4 LightsManager (com.android.server.lights.LightsManager)4 SuppressLint (android.annotation.SuppressLint)3 Intent (android.content.Intent)3