@Override
  public void onSensorChanged(SensorEvent event) {
    // TODO Auto-generated method stub
    float x = event.values[0];
    float y = event.values[1];
    float z = event.values[2];
    float value = Math.abs(x) + Math.abs(y) + Math.abs(z);
    double cal = Math.sqrt(x * x + y * y + z * z);
    double currentTime = System.currentTimeMillis();

    // first time
    if (lastCal == 0 && lastMag == 0) {
      magStartTime = currentTime;
      lastValue = value;
      lastMag = currentTime; // last calibration time
      lastBumpRecord = currentTime; // last bump recording time
      lastCal = cal; // calibration
    }

    // first 5 seconds calibrate
    if (currentTime - magStartTime < 5000) {
      lastCal = (lastCal + cal) / 2;
      lastMag = currentTime;
      lastValue = value;
      return;
    }

    // it's bump
    if (currentTime - lastBumpRecord > 999) {
      if (cal > 2 * lastCal) {
        if (!isBump) {
          isBump = true;
          // if(currentTime - lastBumpRecord > 2100){
          lastBumpRecord = currentTime;
          numBumps++;
          vibe.vibrate(500);
          mp.start();

          boolean rtn = true;
          if (lastLocation != null) {
            rtn = trip.addFlagNow(lastLocation, currentTime);
          } else {
            // TO BE REMOVED
            lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            rtn = trip.addFlagNow(location, currentTime);
            // System.out.println("#########"+location.getLatitude()+"
            // "+location.getLongitude());
          }
          if (!rtn) {
            System.out.println("could not save flag!!####!!!###");
          }

          // Update the status page every time, if we can.
          notifyListeners();
          // }
        }
      } else if (cal < 1.5 * lastCal) {
        isBump = false;
      }
    }

    // calibrate each second
    if (currentTime - lastMag > 999) {
      if (!isBump) {
        lastCal = (lastCal + cal) / 2;
        lastMag = currentTime;
      }
    }

    // Location lastLocation;
    // TripData trip;
    // double currentTime = System.currentTimeMillis();

  }