/** Initializes managers. */
  private void initManagers() {

    // Waffles
    w = new Waffle(mContext);

    // iSENSE API
    api = API.getInstance(mContext);
    api.useDev(true);

    // Upload Queue
    uq = new UploadQueue("data_walk", mContext, api);
    uq.buildQueueFromFile();

    // Vibrator for Long Click
    vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

    // GPS
    mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    initLocationManager();
    waitingForGPS();

    // Sensors
    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

    // Beep sound
    mMediaPlayer = MediaPlayer.create(this, R.raw.beep);
  }
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    saved = savedInstanceState;
    mContext = this;

    api = API.getInstance(mContext);
    api.useDev(useDev);
    if (useDev) {
      baseSessionUrl = VIS_URL_DEV;
    } else {
      baseSessionUrl = VIS_URL_PROD;
    }

    f = new Fields();
    uq = new UploadQueue("carrampphysics", mContext, api);
    uq.buildQueueFromFile();

    w = new Waffle(mContext);

    // Save the default login info
    final SharedPreferences mPrefs =
        new ObscuredSharedPreferences(
            CarRampPhysicsV2.mContext,
            CarRampPhysicsV2.mContext.getSharedPreferences("USER_INFO", Context.MODE_PRIVATE));
    if (mPrefs.getString("username", "").equals("")) {
      SharedPreferences.Editor mEdit = mPrefs.edit();
      mEdit.putString("username", DEFAULT_USER).commit();
      mEdit.putString("password", DEFAULT_USER).commit();
    }

    dateString = "";

    mHandler = new Handler();

    startStop = (Button) findViewById(R.id.startStop);

    values = (TextView) findViewById(R.id.values);

    SharedPreferences prefs = getSharedPreferences("RECORD_LENGTH", 0);
    length = countdown = prefs.getInt("length", 10);

    new OnCreateLoginTask().execute();

    loggedInAs = (TextView) findViewById(R.id.loginStatus);
    loggedInAs.setText(
        getResources().getString(R.string.logged_in_as)
            + " "
            + mPrefs.getString("username", "")
            + ", Name: "
            + firstName
            + " "
            + lastInitial);
    SharedPreferences prefs2 = getSharedPreferences("PROJID", 0);
    experimentNumber = prefs2.getString("project_id", null);
    if (experimentNumber == null) {
      if (useDev) {
        experimentNumber = DEFAULT_PROJ_DEV;
      } else {
        experimentNumber = DEFAULT_PROJ_PROD;
      }
    }
    dfm = new DataFieldManager(Integer.parseInt(experimentNumber), api, mContext, f);
    dfm.getOrder();

    startStop.setOnLongClickListener(
        new OnLongClickListener() {

          @Override
          public boolean onLongClick(View arg0) {

            mMediaPlayer.setLooping(false);
            mMediaPlayer.start();

            if (!api.hasConnectivity() && !saveMode) {
              startActivityForResult(
                  new Intent(mContext, SaveModeDialog.class), SAVE_MODE_REQUESTED);
              return false;
            }

            if (running) {

              if (timeHasElapsed) {

                getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

                setupDone = false;
                timeHasElapsed = false;
                useMenu = true;
                countdown = length;

                running = false;
                startStop.setText("Hold to Start");

                timeTimer.cancel();
                choiceViaMenu = false;

                startStop.setEnabled(true);
                startStop.setBackgroundResource(R.drawable.button_rsense);

                Intent dataIntent = new Intent(mContext, DataActivity.class);
                startActivityForResult(dataIntent, UPLOAD_OK_REQUESTED);

              } else if (usedHomeButton) {
                setupDone = false;
                timeHasElapsed = false;
                useMenu = true;
                countdown = length;

                running = false;
                startStop.setText("Hold to Start");

                timeTimer.cancel();
                choiceViaMenu = false;

                startStop.setEnabled(true);
                startStop.setBackgroundResource(R.drawable.button_rsense);
              }

            } else {

              OrientationManager.disableRotation(CarRampPhysicsV2.this);
              getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

              startStop.setEnabled(false);
              startStop.setBackgroundResource(R.drawable.button_rsense_green);

              dataSet = new JSONArray();
              elapsedMillis = 0;
              len = 0;
              len2 = 0;
              i = 0;
              currentTime = getUploadTime(0);

              SharedPreferences prefs = getSharedPreferences(RecordSettings.RECORD_SETTINGS, 0);

              x = prefs.getBoolean("X", false);
              y = prefs.getBoolean("Y", true);
              z = prefs.getBoolean("Z", false);
              mag = prefs.getBoolean("Magnitude", false);

              setEnabledFields();

              if (saveMode) {
                dfm.getOrder();
                System.out.println("Honk frogs@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
              }

              try {
                Thread.sleep(100);
              } catch (InterruptedException e) {
                w.make(
                    "Data recording has offset 100 milliseconds due to an error.",
                    Waffle.LENGTH_SHORT);
                e.printStackTrace();
              }

              useMenu = true;

              if (mSensorManager != null) {
                mSensorManager.registerListener(
                    CarRampPhysicsV2.this,
                    mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                    SensorManager.SENSOR_DELAY_FASTEST);
              }

              running = true;
              startStop.setText("" + countdown);

              timeTimer = new Timer();
              timeTimer.scheduleAtFixedRate(
                  new TimerTask() {

                    public void run() {

                      elapsedMillis += INTERVAL;

                      if (i >= (length * (1000 / INTERVAL))) {

                        timeTimer.cancel();
                        timeHasElapsed = true;

                        mHandler.post(
                            new Runnable() {
                              @Override
                              public void run() {
                                startStop.performLongClick();
                              }
                            });

                      } else {

                        i++;
                        len++;
                        len2++;

                        if (i % (1000 / INTERVAL) == 0) {
                          mHandler.post(
                              new Runnable() {
                                @Override
                                public void run() {
                                  startStop.setText("" + countdown);
                                }
                              });
                          countdown--;
                        }

                        f.timeMillis = currentTime + elapsedMillis;
                        Log.d("fantastag", "time added");

                        if (x) {
                          f.accel_x = toThou.format(accel[0]);
                          Log.d("fantastag", "X added");
                        }
                        if (y) {
                          f.accel_y = toThou.format(accel[1]);
                          Log.d("fantastag", "Y added");
                        }
                        if (z) {
                          f.accel_z = toThou.format(accel[2]);
                          Log.d("fantastag", "Z added");
                        }
                        if (mag) {
                          f.accel_total = toThou.format(accel[3]);
                          Log.d("fantastag", "Magnitude added");
                        }

                        if (!saveMode) {
                          dataSet.put(dfm.putData());
                          Log.d("tag", "NULLFROG");
                        } else {
                          dataSet.put(dfm.putDataForNoProjectID());
                          Log.d("tag", "NULLTOAD");
                        }
                      }
                    }
                  },
                  0,
                  INTERVAL);
            }

            return running;
          }
        });

    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

    if (mSensorManager != null) {
      mSensorManager.registerListener(
          CarRampPhysicsV2.this,
          mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
          SensorManager.SENSOR_DELAY_FASTEST);
      mSensorManager.registerListener(
          CarRampPhysicsV2.this,
          mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
          SensorManager.SENSOR_DELAY_FASTEST);
    }

    Criteria c = new Criteria();
    c.setAccuracy(Criteria.ACCURACY_FINE);

    accel = new float[4];
    orientation = new float[3];
    rawAccel = new float[3];
    rawMag = new float[3];

    mMediaPlayer = MediaPlayer.create(this, R.raw.beep);

    if (savedInstanceState == null) {
      if (firstName.equals("") || lastInitial.equals("")) {
        if (!dontPromptMeTwice) {
          startActivityForResult(new Intent(mContext, EnterNameActivity.class), resultGotName);
        }
      }
    }

    if (!api.hasConnectivity() && !saveMode) {
      startActivityForResult(new Intent(mContext, SaveModeDialog.class), SAVE_MODE_REQUESTED);
    }
  }