@Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    EventService service =
        new EventServiceImpl(
            getString(R.string.thing_broker_server),
            getString(R.string.thing_broker_port),
            null,
            false);
    Event event = new Event();
    event.setThingId(getString(R.string.thing_broker_thing_name));
    Map<String, String> gameInfo = new HashMap<String, String>();

    if (resultCode == RESULT_OK) {
      if (data.getStringExtra("method").equals("nfc")) {
        nfc_upload = data.getStringExtra("uploadResult");
        System.out.println("NFC upload result: " + nfc_upload);
        if (nfc_upload != null && !nfc_upload.equals("")) {
          webView.loadUrl("javascript:gotNFC('" + nfc_upload + "');");
        }

      } else if (data.getStringExtra("method").equals("qr")) {
        System.out.println("QR value");
        qr_upload = data.getStringExtra("qr_result");
        player = data.getStringExtra("playernumber");
        if (qr_upload != null && !qr_upload.equals("")) {
          webView.loadUrl("javascript:gotQR('" + qr_upload + "');");

          gameInfo.put("type", "qrResult");
          gameInfo.put("player", player);
          gameInfo.put("value", qr_upload);
          event.setInfo(gameInfo);
          service.postEvent(STANDARD_MESSAGE, (ThingBrokerRequestListener) this, event, true);
        }

      } else if (data.getStringExtra("method").equals("accel")) {
        System.out.println("ACCEL");
        accelerometer_rate = data.getDoubleExtra("accel_rate", 5.0);
        accelerometer_enabled = true;
        player = data.getStringExtra("playernumber");
        startAccelUpload();
        System.out.println("start accel Upload");
      } else if (data.getStringExtra("method").equals("upload")) {
        System.out.println("Upload this shit now");
        uploadDimensionsTest = data.getStringExtra("uploadResult");
        System.out.println("Upload Dimension Test" + uploadDimensionsTest);
        if (uploadDimensionsTest != null && !uploadDimensionsTest.equals("")) {
          webView.loadUrl("javascript:gotImage('" + uploadDimensionsTest + "');");
        }
        System.out.println("uploadDimesnions passed over");
      }
    }
  }
    @Override
    public void run() {
      String accelerometer_value = null;
      accelerometer_value =
          "{ \"x\": \""
              + linear_accel[0]
              + "\",\"y\": \""
              + linear_accel[1]
              + "\", \"z\": \""
              + linear_accel[2]
              + "\", \"player\": \""
              + player
              + "\" }";
      // Sending Events

      EventService service =
          new EventServiceImpl(
              getString(R.string.thing_broker_server),
              getString(R.string.thing_broker_port),
              null,
              false);
      Event event = new Event();
      event.setThingId(getString(R.string.thing_broker_thing_name));
      Map<String, String> gameInfo = new HashMap<String, String>();

      System.out.println("Accelerometer values are: " + accelerometer_value);

      if (linear_accel == null) {
        System.out.println("No Accelerometer Data found.");
        uploadAccel.cancel();
      } else {
        webView.loadUrl("javascript:updateAccelData('" + accelerometer_value + "');");

        gameInfo.put("type", "accelData");
        gameInfo.put("player", player);
        gameInfo.put("x", Float.toString(linear_accel[0]));
        gameInfo.put("y", Float.toString(linear_accel[1]));
        gameInfo.put("z", Float.toString(linear_accel[2]));
        event.setInfo(gameInfo);
        service.postEvent(STANDARD_MESSAGE, this, event, true);
      }
    }
  @Override
  public void onResume() {
    super.onResume();
    if (justPlayedMedia) {
      justPlayedMedia = false;
      return;
    }

    sm.registerListener(this, accelerometers, SensorManager.SENSOR_DELAY_NORMAL);

    // load callback URL from preferences, if it exists, and whether the user has requested a quit.
    // These preferences will only exist if, while we were suspended, our sub-launched self wrote
    // the preferences
    // because it wants us to refresh the page or quit.
    SharedPreferences settings = getSharedPreferences("container_prefs", 0);
    callbackUrl = settings.getString("callbackUrl", "");

    nfc_upload = settings.getString("nfc_retrieved_message", "");
    System.out.println("nfc_upload : " + nfc_upload);

    System.out.println("callbackURL is: " + callbackUrl);
    boolean shouldQuit = settings.getBoolean("quitChallenge", false);

    // now clear the preferences again so that we don't refresh ourselves/quit if suspended by some
    // other
    // activity
    SharedPreferences.Editor editor = settings.edit();
    editor.putString("callbackUrl", "");
    editor.putString("nfc_retrieved_message", "");
    editor.putBoolean("quitChallenge", false);
    editor.commit();

    EventService service =
        new EventServiceImpl(
            getString(R.string.thing_broker_server),
            getString(R.string.thing_broker_port),
            null,
            false);
    Event event = new Event();
    event.setThingId(getString(R.string.thing_broker_thing_name));
    Map<String, String> gameInfo = new HashMap<String, String>();

    if (shouldQuit) {
      doLeaveChallengeConfirmation();
    } else if (callbackUrl != null && !callbackUrl.equals("")) {
      if (callbackUrl.equals(HtmlCallbackActivity.ABORT_CODE)) {
        if (currentUrl == null || currentUrl.startsWith(getString(R.string.default_url))) finish();
      } else {
        System.out.println("Webview loading: " + callbackUrl);
        webView.loadUrl(callbackUrl);
        currentUrl = callbackUrl;
      }
    } else if (nfc_upload != null && !nfc_upload.equals("")) {
      webView.loadUrl("javascript:gotNFC('" + nfc_upload + "');");

      gameInfo.put("type", "nfcResult");
      gameInfo.put("player", player);
      gameInfo.put("value", nfc_upload);
      event.setInfo(gameInfo);
      service.postEvent(STANDARD_MESSAGE, this, event, true);
    }
  }