/** Retrieve the OAuth Request Token and present a browser to the user to authorize the token. */
  @Override
  protected Uri doInBackground(Uri... params) {
    determineProgressGoal(null);

    Uri trackUri = null;
    String trackName =
        mAdapter.getBreadcrumbsTracks().getValueForItem(mTrack, BreadcrumbsTracks.NAME);
    HttpEntity responseEntity = null;
    try {
      HttpUriRequest request =
          new HttpGet(
              "http://api.gobreadcrumbs.com/v1/tracks/" + mTrack.second + "/placemarks.gpx");
      if (isCancelled()) {
        throw new IOException("Fail to execute request due to canceling");
      }
      mConsumer.sign(request);
      if (BreadcrumbsAdapter.DEBUG) {
        Log.d(TAG, "Execute request: " + request.getURI());
        for (Header header : request.getAllHeaders()) {
          Log.d(TAG, "   with header: " + header.toString());
        }
      }
      HttpResponse response = mHttpclient.execute(request);
      responseEntity = response.getEntity();
      InputStream is = responseEntity.getContent();
      InputStream stream = new BufferedInputStream(is, 8192);
      if (BreadcrumbsAdapter.DEBUG) {
        stream = XmlCreator.convertStreamToLoggedStream(TAG, stream);
      }
      trackUri = importTrack(stream, trackName);
    } catch (OAuthMessageSignerException e) {
      handleError(e, mContext.getString(R.string.error_importgpx_xml));
    } catch (OAuthExpectationFailedException e) {
      handleError(e, mContext.getString(R.string.error_importgpx_xml));
    } catch (OAuthCommunicationException e) {
      handleError(e, mContext.getString(R.string.error_importgpx_xml));
    } catch (IOException e) {
      handleError(e, mContext.getString(R.string.error_importgpx_xml));
    } finally {
      if (responseEntity != null) {
        try {
          EntityUtils.consume(responseEntity);
        } catch (IOException e) {
          Log.e(TAG, "Failed to close the content stream", e);
        }
      }
    }
    return trackUri;
  }
  /** Retrieve the OAuth Request Token and present a browser to the user to authorize the token. */
  @Override
  protected Void doInBackground(Void... params) {
    HttpEntity responseEntity = null;
    mBundleIds = new HashSet<Integer>();
    mBundles = new LinkedList<Object[]>();
    try {
      HttpUriRequest request = new HttpGet("http://api.gobreadcrumbs.com/v1/bundles.xml");
      if (isCancelled()) {
        throw new IOException("Fail to execute request due to canceling");
      }
      mConsumer.sign(request);
      if (BreadcrumbsAdapter.DEBUG) {
        Log.d(TAG, "Execute request: " + request.getURI());
        for (Header header : request.getAllHeaders()) {
          Log.d(TAG, "   with header: " + header.toString());
        }
      }
      HttpResponse response = mHttpclient.execute(request);
      responseEntity = response.getEntity();
      InputStream is = responseEntity.getContent();
      InputStream stream = new BufferedInputStream(is, 8192);
      if (BreadcrumbsAdapter.DEBUG) {
        stream = XmlCreator.convertStreamToLoggedStream(TAG, stream);
      }

      XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
      factory.setNamespaceAware(true);
      XmlPullParser xpp = factory.newPullParser();
      xpp.setInput(stream, "UTF-8");

      String tagName = null;
      int eventType = xpp.getEventType();

      String bundleName = null, bundleDescription = null;
      Integer bundleId = null;
      while (eventType != XmlPullParser.END_DOCUMENT) {
        if (eventType == XmlPullParser.START_TAG) {
          tagName = xpp.getName();
        } else if (eventType == XmlPullParser.END_TAG) {
          if ("bundle".equals(xpp.getName()) && bundleId != null) {
            mBundles.add(new Object[] {bundleId, bundleName, bundleDescription});
          }
          tagName = null;
        } else if (eventType == XmlPullParser.TEXT) {
          if ("description".equals(tagName)) {
            bundleDescription = xpp.getText();
          } else if ("id".equals(tagName)) {
            bundleId = Integer.parseInt(xpp.getText());
            mBundleIds.add(bundleId);
          } else if ("name".equals(tagName)) {
            bundleName = xpp.getText();
          }
        }
        eventType = xpp.next();
      }
    } catch (OAuthMessageSignerException e) {
      mAdapter.removeAuthentication();
      handleError(
          mContext.getString(R.string.taskerror_breadcrumbs_bundle),
          e,
          "Failed to sign the request with authentication signature");
    } catch (OAuthExpectationFailedException e) {
      mAdapter.removeAuthentication();
      handleError(
          mContext.getString(R.string.taskerror_breadcrumbs_bundle),
          e,
          "The request did not authenticate");
    } catch (OAuthCommunicationException e) {
      mAdapter.removeAuthentication();
      handleError(
          mContext.getString(R.string.taskerror_breadcrumbs_bundle),
          e,
          "The authentication communication failed");
    } catch (IOException e) {
      handleError(
          mContext.getString(R.string.taskerror_breadcrumbs_bundle),
          e,
          "A problem during communication");
    } catch (XmlPullParserException e) {
      handleError(
          mContext.getString(R.string.taskerror_breadcrumbs_bundle),
          e,
          "A problem while reading the XML data");
    } catch (IllegalStateException e) {
      handleError(
          mContext.getString(R.string.taskerror_breadcrumbs_bundle),
          e,
          "A problem during communication");
    } finally {
      if (responseEntity != null) {
        try {
          EntityUtils.consume(responseEntity);
        } catch (IOException e) {
          Log.w(TAG, "Failed closing inputstream");
        }
      }
    }
    return null;
  }