Follow a trip in Android

Select platform: Android iOS JavaScript

When you follow a trip, your consumer app displays the location of the appropriate vehicle to the consumer. To do this, your app needs to start following a trip, update trip progress, and stop following a trip when it completes.

This document covers how that process works.

Before you begin

Make sure you have set up the following things:

  • The backend services for your consumer app are in place and your services for matching consumers with vehicles is operational.

  • You have set up a map for your app.

Start following a trip

When your backend server matches a consumer with a vehicle, use JourneySharingSession to start following the trip.

The following sample code demonstrates how to start following a trip after the view loads.

Java

public class MainActivity extends AppCompatActivity
    implements ConsumerViewModel.JourneySharingListener  {

  // Class implementation

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Create a TripModel instance to listen for updates to the trip specified by this trip name.
    String tripName = ...;
    TripModelManager tripModelManager = consumerApi.getTripModelManager();
    TripModel tripModel = tripModelManager.getTripModel(tripName);

    // Create a JourneySharingSession instance based on the TripModel.
    JourneySharingSession session = JourneySharingSession.createInstance(tripModel);

    // Add the JourneySharingSession instance on the map for updating the UI.
    consumerController.showSession(session);

    // Register for trip update events.
    tripModel.registerTripCallback(new TripModelCallback() {
      @Override
      public void onTripETAToNextWaypointUpdated(
          TripInfo tripInfo, @Nullable Long timestampMillis) {
        // ...
      }

      @Override
      public void onTripActiveRouteRemainingDistanceUpdated(
          TripInfo tripInfo, @Nullable Integer distanceMeters) {
        // ...
      }

      // ...
    });
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();

    if (journeySharingSession != null) {
      journeySharingSession.stop();
    }
  }
}

Kotlin

class SampleAppActivity : AppCompatActivity(), ConsumerViewModel.JourneySharingListener {

  // Class implementation

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    // Create a TripModel instance to listen for updates to the trip specified by this trip name.
    val tripName = "tripName"
    val tripModelManager = consumerApi.getTripModelManager()
    val tripModel = tripModelManager.getTripModel(tripName)

    // Create a JourneySharingSession instance based on the TripModel.
    val session = JourneySharingSession.createInstance(tripModel)

    // Add the JourneySharingSession instance on the map for updating the UI.
    consumerController.showSession(session)

    // Register for trip update events.
    tripModel.registerTripCallback(
      object : TripModelCallback() {
        override fun onTripETAToNextWaypointUpdated(
          tripInfo: TripInfo,
          timestampMillis: Long?,
        ) {
          // ...
        }

        override fun onTripActiveRouteRemainingDistanceUpdated(
          tripInfo: TripInfo,
          distanceMeters: Int?,
        ) {
          // ...
        }

      // ...
    })
  }

  override fun onDestroy() {
    super.onDestroy()

    journeySharingSession?.stop()
  }
}

Update trip progress

To update trip progress details, such as the distance that the vehicle needs to travel before arrival and the estimated time of arrival, your app needs to register and configure a listener as shown in the following examples.

  1. Register a listener on a TripModel object.

    Java

    // Create a TripModel instance for listening to updates to the trip specified by this trip name.
    String tripName = ...;
    TripModelManager tripModelManager = consumerApi.getTripModelManager();
    TripModel tripModel = tripModelManager.getTripModel(tripName);
    
    // Create a JourneySharingSession instance based on the TripModel.
    JourneySharingSession session = JourneySharingSession.createInstance(tripModel);
    
    // Add the JourneySharingSession instance on the map for updating the UI.
    consumerController.showSession(session);
    
    // Register for trip update events.
    tripModel.registerTripCallback(new TripModelCallback() {
    @Override
    public void onTripETAToNextWaypointUpdated(
            TripInfo tripInfo, @Nullable Long timestampMillis) {
          // ...
    }
    
    @Override
    public void onTripActiveRouteRemainingDistanceUpdated(
            TripInfo tripInfo, @Nullable Integer distanceMeters) {
          // ...
    }
    
    // ...
    });
    

    Kotlin

    // Create a TripModel instance for listening to updates to the trip specified by this trip name.
    val tripName = "tripName"
    val tripModelManager = consumerApi.getTripModelManager()
    val tripModel = tripModelManager.getTripModel(tripName)
    
    // Create a JourneySharingSession instance based on the TripModel.
    val session = JourneySharingSession.createInstance(tripModel)
    
    // Add the JourneySharingSession instance on the map for updating the UI.
    consumerController.showSession(session)
    
    // Register for trip update events.
    tripModel.registerTripCallback(
      object : TripModelCallback() {
        override fun onTripETAToNextWaypointUpdated(
          tripInfo: TripInfo,
          timestampMillis: Long?,
        ) {
          // ...
        }
    
        override fun onTripActiveRouteRemainingDistanceUpdated(
          tripInfo: TripInfo,
          distanceMeters: Int?,
        ) {
          // ...
        }
    
      // ...
    })
    
  2. Configure the listener for your trip using TripModelOptions.

    Java

    // Set refresh interval to 2 seconds.
    TripModelOptions tripOptions =
          TripModelOptions.builder().setRefreshIntervalMillis(2000).build();
    tripModel.setTripModelOptions(tripOptions);
    

    Kotlin

    // Set refresh interval to 2 seconds.
    val tripOptions = TripModelOptions.builder().setRefreshIntervalMillis(2000).build()
    tripModel.setTripModelOptions(tripOptions)
    

Stop following a trip

Make sure that your app stops following a trip when it is no longer needed, such as when trip is marked COMPLETE at the backend by the driver. Stopping journey sharing avoids unnecessary network requests to Fleet Engine and prevents memory leaks.

Use JourneySharingSession to stop following the trip as shown in the following sample code.

Java

public class MainActivity extends AppCompatActivity
    implements ConsumerViewModel.JourneySharingListener  {

  // Class implementation

  @Override
  protected void onDestroy() {
    super.onDestroy();

    if (journeySharingSession != null) {
      journeySharingSession.stop();
    }
  }
}

Kotlin

class SampleAppActivity : AppCompatActivity(), ConsumerViewModel.JourneySharingListener {

  // Class implementation

  override fun onDestroy() {
    super.onDestroy()

    journeySharingSession?.stop()
  }
}

Handle trip errors

The onTripRefreshError method surfaces errors that occur during trip monitoring. Error messages follow the Google Cloud Error standard. For detailed error message definitions and all the error codes, refer to Google Cloud Errors documentation.

Here are some common errors that can occur during trip monitoring:

HTTP RPC Description
400 INVALID_ARGUMENT Client specified an invalid trip name. The trip name must follow the format providers/{provider_id}/trips/{trip_id}. The provider_id must be the ID of the Cloud Project owned by the service provider.
401 UNAUTHENTICATED You receive this error if there are no valid authentication credentials. For example, if the JWT token is signed without a trip ID or the JWT token has expired.
403 PERMISSION_DENIED You receive this error if the client does not have sufficient permission (for example, a user with the consumer role tries to call updateTrip), if the JWT token is invalid, or the API is not enabled for the client project. The JWT token might be missing or the token is signed with a trip ID that does not match requested trip ID.
429 RESOURCE_EXHAUSTED The resource quota is at zero or the rate of traffic exceeds the limit.
503 UNAVAILABLE Service unavailable. Typically the server is down.
504 DEADLINE_EXCEEDED Request deadline exceeded. This error only occurs if the caller sets a deadline that is shorter than the method's default deadline (that is, the requested deadline is not enough for the server to process the request) and the request did not finish within the deadline.

Handle Consumer SDK Errors

The Consumer SDK sends trip update errors to the consumer app using a callback mechanism. The callback parameter is a platform-specific return type ( TripUpdateError on Android, and NSError on iOS).

Extract status codes

The errors passed to the callback are typically gRPC errors, and you can also extract additional information from them in the form of a status code. For the complete list of status codes, see Status codes and their use in gRPC.

Java

You can extract a gRPC status code that provides details about the error from the TripUpdateError returned from onTripUpdateError().

// Called when there is a trip update error.
@Override
public void onTripUpdateError(TripInfo tripInfo, TripUpdateError error) {
  Status.Code code = error.getStatusCode();
}

Kotlin

You can extract a gRPC status code that provides details about the error from the TripUpdateError returned from onTripUpdateError().

// Called when there is a trip update error.
override fun onTripUpdateError(tripInfo: TripInfo, error: TripUpdateError) {
  val code = error.getStatusCode()
}

Interpret status codes

Status codes cover two kinds of errors: server and network-related errors, and client-side errors.

Server and network errors

The following status codes are for either network or server errors, and you don't need to any take action to resolve them. The Consumer SDK automatically recovers from them.

Status CodeDescription
ABORTED The server stopped sending the response. This is normally caused by a server problem.
CANCELLED The server terminated the outgoing response. This normally happens when
the app is sent to the background, or when there is a state change in the
Consumer app.
INTERRUPTED
DEADLINE_EXCEEDED The server took too long to respond.
UNAVAILABLE The server was unavailable. This is normally caused by a network problem.

Client errors

The following status codes are for client errors, and you must take action to resolve them. The Consumer SDK continues retrying to refresh the trip until you end journey sharing, but it won't recover until you take action.

Status CodeDescription
INVALID_ARGUMENT The Consumer app specified an invalid trip name; The trip name must follow the format providers/{provider_id}/trips/{trip_id}.
NOT_FOUND The trip was never created.
PERMISSION_DENIED The Consumer app has insufficient permissions. This error occurs when:
  • The Consumer app doesn't have permissions
  • The Consumer SDK isn't enabled for the project in the Google Cloud Console.
  • The JWT token is either missing or is invalid.
  • The JWT token is signed with a trip ID that doesn't match the requested trip.
RESOURCE_EXHAUSTED The resource quota is at zero, or the rate of traffic flow exceeds the speed limit.
UNAUTHENTICATED The request failed authentication due to an invalid JWT token. This error occurs either when the JWT token is signed without a trip ID, or when the JWT token has expired.