PageRenderTime 42ms CodeModel.GetById 20ms app.highlight 15ms RepoModel.GetById 2ms app.codeStats 0ms

/shell/src/com/google/marvin/shell/ShakeDetector.java

http://eyes-free.googlecode.com/
Java | 102 lines | 64 code | 18 blank | 20 comment | 8 complexity | 0af87401fc97c5729cc82496d3d2538c MD5 | raw file
  1/*
  2 * Copyright (C) 2010 Google Inc.
  3 *
  4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5 * use this file except in compliance with the License. You may obtain a copy of
  6 * the License at
  7 *
  8 * http://www.apache.org/licenses/LICENSE-2.0
  9 *
 10 * Unless required by applicable law or agreed to in writing, software
 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 13 * License for the specific language governing permissions and limitations under
 14 * the License.
 15 */
 16
 17package com.google.marvin.shell;
 18
 19import android.content.Context;
 20import android.hardware.Sensor;
 21import android.hardware.SensorEvent;
 22import android.hardware.SensorEventListener;
 23import android.hardware.SensorManager;
 24
 25/**
 26 * Uses the accelerometer to detect when the user is shaking the phone.
 27 *
 28 * @author clchen@google.com (Charles L. Chen), credo@google.com (Tim Credo)
 29 */
 30public class ShakeDetector {
 31
 32    public interface ShakeListener {
 33        public void onShakeDetected();
 34    }
 35
 36    private SensorEventListener mListener;
 37
 38    private ShakeListener cb;
 39
 40    private SensorManager sensorManager;
 41
 42    public ShakeDetector(Context context, ShakeListener callback) {
 43        cb = callback;
 44        mListener = new SensorEventListener() {
 45            private final double deletionForce = .8;
 46
 47            private final int deletionCount = 2;
 48
 49            int shakeCount = 0;
 50
 51            boolean lastShakePositive = false;
 52
 53            private int shakeCountTimeout = 500;
 54
 55            @Override
 56            public void onSensorChanged(SensorEvent event) {
 57                float[] values = event.values;
 58                if ((values[1] > deletionForce) && !lastShakePositive) {
 59                    (new Thread(new resetShakeCount())).start();
 60                    shakeCount++;
 61                    lastShakePositive = true;
 62                } else if ((values[1] < -deletionForce) && lastShakePositive) {
 63                    (new Thread(new resetShakeCount())).start();
 64                    shakeCount++;
 65                    lastShakePositive = false;
 66                }
 67                if (shakeCount > deletionCount) {
 68                    shakeCount = 0;
 69                    cb.onShakeDetected();
 70                }
 71            }
 72
 73            @Override
 74            public void onAccuracyChanged(Sensor arg0, int arg1) {
 75            }
 76
 77            class resetShakeCount implements Runnable {
 78                @Override
 79                public void run() {
 80                    try {
 81                        Thread.sleep(shakeCountTimeout);
 82                        shakeCount = 0;
 83                    } catch (InterruptedException e) {
 84                        e.printStackTrace();
 85                    }
 86                }
 87            }
 88        };
 89
 90        sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
 91        Sensor accelerometer = sensorManager.getDefaultSensor(SensorManager.SENSOR_ACCELEROMETER);
 92        sensorManager.registerListener(
 93                mListener, accelerometer, SensorManager.SENSOR_DELAY_FASTEST);
 94    }
 95
 96    public void shutdown() {
 97        if (sensorManager != null) {
 98            sensorManager.unregisterListener(mListener);
 99        }
100    }
101
102}