@Override
  public void onAttach(Activity activity) {
    super.onAttach(activity);

    this.context = activity;
    session = Session.getCurrentSession(context);

    // Check the onPositionRequestedListener
    try {
      onPositionRequestedListener = (OnPositionRequestedListener) activity;
      myActualPosition = onPositionRequestedListener.requestPosition();
      updateRestaurants();
    } catch (ClassCastException classCastException) {
      Log.e(
          LOG_TAG,
          "The attached activity must implements the OnPositionRequestedListener",
          classCastException);
    }

    // Check the onProgressBarShowRequestListener
    try {
      onProgressBarShowRequestListener = (OnProgressBarShowRequestListener) activity;
    } catch (ClassCastException classCastException) {
      Log.e(
          LOG_TAG,
          "The attached activity must implements the OnProgressBarShowRequestListener",
          classCastException);
    }
  }
  @Override
  public void onScroll(
      AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

    boolean loadMoreRestaurants = firstVisibleItem + visibleItemCount >= totalItemCount;

    boolean moreDataAvailable = (nextPageToken != null && !nextPageToken.equalsIgnoreCase(""));

    if (loadMoreRestaurants && moreDataAvailable && !isLoadingMoreRestaurants) {
      Log.v(
          LOG_TAG,
          "Load more restaurants request send and more restaurants are available. Loading.");
      // Show the progress bar
      onProgressBarShowRequestListener.showProgressBar();

      isLoadingMoreRestaurants = true;
      // The app is requesting for more data and there is more data available.
      // Requesting them
      Toast.makeText(context, R.string.loading_new_restaurants, Toast.LENGTH_LONG).show();
      session.getRestaurantsNearbyNextPage(
          nextPageToken,
          new RequestRestaurantsCallback() {

            @Override
            public void done(
                List<Restaurant> newRestaurants,
                String newNextPageToken,
                String errorMessage,
                RequestStatus requestStatus) {
              isLoadingMoreRestaurants = false;
              // Disable the progress bar
              onProgressBarShowRequestListener.hidePorgressBar();

              if (!ErrorHandler.isError(requestStatus)) {
                nextPageToken = newNextPageToken;
                restaurantListAdapter.addMoreRestaurants(newRestaurants);
              } else {
                Toast.makeText(context, errorMessage, Toast.LENGTH_LONG).show();

                // Remove the next page token saved
                nextPageToken = null;

                // If there is any error about Internet connection but the list of
                // restaurants has been retrieved offLine, reset the list
                if (requestStatus == RequestStatus.ERROR_REQUEST_NOK_HTTP_NO_CONNECTION
                    && newRestaurants != null) {
                  showRestaurantList(newRestaurants);
                }
              }
            }
          });
    }
  }
  /** Update the list of the restaurants based on the position of the user */
  private void updateRestaurants() {
    if (myActualPosition == null) {
      Log.e(
          LOG_TAG,
          "Trying to update the list of the restaurants when the position of the user is unknown.");
      return;
    }

    if (session == null) {
      Log.w(LOG_TAG, "Trying to upate the list of the restaurants when the session is not ready");
      return;
    }

    session.getRestaurantsNearby(
        myActualPosition,
        new RequestRestaurantsCallback() {

          @Override
          public void done(
              List<Restaurant> restaurants,
              String newNextPageToken,
              String errorMessage,
              RequestStatus requestStatus) {
            if (!ErrorHandler.isError(requestStatus)) {
              Log.v(LOG_TAG, "List of the restaurants returned correctly");
              nextPageToken = newNextPageToken;
              showRestaurantList(restaurants);
            } else {
              Toast.makeText(context, errorMessage, Toast.LENGTH_LONG).show();

              // Remove the next page token saved
              nextPageToken = null;
              // If there is any error about Internet connection but the list of
              // restaurants has been retrieved offline, draw them on the map
              if (requestStatus == RequestStatus.ERROR_REQUEST_NOK_HTTP_NO_CONNECTION
                  && restaurants != null) {
                showRestaurantList(restaurants);
              }
            }
          }
        });
  }