/** 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;
  }
  @Override
  protected void onPostExecute(Uri result) {
    super.onPostExecute(result);

    long ogtTrackId = Long.parseLong(result.getLastPathSegment());
    Uri metadataUri =
        Uri.withAppendedPath(
            ContentUris.withAppendedId(Tracks.CONTENT_URI, ogtTrackId), "metadata");

    BreadcrumbsTracks tracks = mAdapter.getBreadcrumbsTracks();
    Integer bcTrackId = mTrack.second;
    Integer bcBundleId = tracks.getBundleIdForTrackId(bcTrackId);
    // TODO Integer bcActivityId = tracks.getActivityIdForBundleId(bcBundleId);
    String bcDifficulty = tracks.getValueForItem(mTrack, BreadcrumbsTracks.DIFFICULTY);
    String bcRating = tracks.getValueForItem(mTrack, BreadcrumbsTracks.RATING);
    String bcPublic = tracks.getValueForItem(mTrack, BreadcrumbsTracks.ISPUBLIC);
    String bcDescription = tracks.getValueForItem(mTrack, BreadcrumbsTracks.DESCRIPTION);

    ArrayList<ContentValues> metaValues = new ArrayList<ContentValues>();
    if (bcTrackId != null) {
      metaValues.add(buildContentValues(BreadcrumbsTracks.TRACK_ID, Long.toString(bcTrackId)));
    }
    if (bcDescription != null) {
      metaValues.add(buildContentValues(BreadcrumbsTracks.DESCRIPTION, bcDescription));
    }
    if (bcDifficulty != null) {
      metaValues.add(buildContentValues(BreadcrumbsTracks.DIFFICULTY, bcDifficulty));
    }
    if (bcRating != null) {
      metaValues.add(buildContentValues(BreadcrumbsTracks.RATING, bcRating));
    }
    if (bcPublic != null) {
      metaValues.add(buildContentValues(BreadcrumbsTracks.ISPUBLIC, bcPublic));
    }
    if (bcBundleId != null) {
      metaValues.add(buildContentValues(BreadcrumbsTracks.BUNDLE_ID, Integer.toString(bcBundleId)));
    }
    //      if (bcActivityId != null)
    //      {
    //         metaValues.add(buildContentValues(BreadcrumbsTracks.ACTIVITY_ID,
    // Integer.toString(bcActivityId)));
    //      }
    ContentResolver resolver = mContext.getContentResolver();
    resolver.bulkInsert(metadataUri, metaValues.toArray(new ContentValues[1]));

    tracks.addSyncedTrack(ogtTrackId, mTrack.second);
  }