Android 驱动程序 SDK 3.0 迁移指南

适用于 Android 3.0 版本的驱动程序 SDK 需要您更新代码 执行某些操作本指南概述了具体变化以及 迁移代码所需的操作

软件包名称更改

软件包名称已更改,原为 com.google.android.libraries.ridesharing.drivercom.google.android.libraries.mapsplatform.transportation.driver。请 更新代码中的引用

初始化 SDK

在早期版本中,您需要初始化 Navigation SDK,然后获取 对 FleetEngine 类的引用。在驱动程序 SDK 中 v3,请按如下方式初始化 SDK:

  1. NavigationApi 获取 Navigator 对象。

    NavigationApi.getNavigator(
          this, // Activity
          new NavigationApi.NavigatorListener() {
            @Override
            public void onNavigatorReady(Navigator navigator) {
              // Keep a reference to the Navigator (used to configure and start nav)
              this.navigator = navigator;
            }
          }
    );
    
  2. 创建一个用于填充必填字段的 DriverContext 对象。

    DriverContext driverContext = DriverContext.builder(application)
                 .setProviderId(providerId)
                 .setVehicleId(vehicleId)
                 .setAuthTokenFactory(authTokenFactory)
                 .setNavigator(navigator)
                 .setRoadSnappedLocationProvider(
                     NavigationApi.getRoadSnappedLocationProvider(application))
                 .build()
    
  3. 使用 DriverContext 对象初始化 *DriverApi

    RidesharingDriverApi ridesharingDriverApi = RidesharingDriverApi.createInstance(driverContext);
    
  4. 从 API 对象获取 NavigationVehicleReporter*VehicleReporter 扩展了 NavigationVehicleReporter

    RidesharingVehicleReporter vehicleReporter = ridesharingDriverApi.getRidesharingVehicleReporter();
    

启用和停用位置更新

在早期版本中,您可以在 Google Cloud 控制台 FleetEngine 引用。在驱动程序 SDK v3 中,启用 位置更新如下:

RidesharingVehicleReporter reporter = ...;

reporter.enableLocationTracking();

要更新报告间隔,请使用 RidesharingVehicleReporter.setLocationReportingInterval(long, TimeUnit)DeliveryVehicleReporter.setLocationReportingInterval(long, TimeUnit)

换档完成后,停用位置信息更新 并通过调用 NavigationVehicleReporter.disableLocationTracking() 将车辆标记为离线。

在服务器上设置车辆状态

在早期版本中,您可以使用 FleetEngine 对象来设置 车辆状态。在驱动程序 SDK v3 中,请使用 RidesharingVehicleReporter 对象来设置车辆状态:

  RidesharingVehicleReporter reporter = ...;

  reporter.enableLocationTracking();
  reporter.setVehicleState(VehicleState.ONLINE);

如需将车辆状态设置为 OFFLINE,请调用 RidesharingVehicleReporter.disableLocationTracking()。错误 更新车辆状态的操作是使用可选的 在 DriverContext 中设置的 StatusListener

使用 StatusListener 进行 Error Reporting

ErrorListener已被移除,并与 StatusListener 合并, 其定义可如下所示:

class MyStatusListener implements StatusListener {
  /** Called when background status is updated, during actions such as location reporting. */
  @Override
  public void updateStatus(
    StatusLevel statusLevel,
    StatusCode statusCode,
    String statusMsg) {
    // Status handling stuff goes here.
    // StatusLevel may be DEBUG, INFO, WARNING, or ERROR.
    // StatusCode may be DEFAULT, UNKNOWN_ERROR, VEHICLE_NOT_FOUND, 
    // BACKEND_CONNECTIVITY_ERROR, or PERMISSION_DENIED.
  }
}

使用 AuthTokenFactory 进行身份验证

AuthTokenFactory 现在只有一个方法,即 getToken()AuthTokenContext 作为参数。拼车客户必须 针对 VEHICLE 服务类型进行身份验证,以便启用位置信息功能 和车辆状态(线上/离线)报告。

class JsonAuthTokenFactory implements AuthTokenFactory {
  String vehicleServiceToken;  // initially null
  long expiryTimeMs = 0;

  // This method is called on a thread whose only responsibility is to send
  // location updates. Blocking is OK, but just know that no location updates
  // can occur until this method returns.
  @Override public String getToken(AuthTokenContext authTokenContext) {
    if (System.currentTimeMillis() > expiryTimeMs) {
      // The token has expired, go get a new one.
      fetchNewToken(vehicleId);
    }
    if (ServiceType.VEHICLE.equals(authTokenContext.getServiceType)) {
      return vehicleServiceToken;
    } else {
      throw new RuntimeException("Unsupported ServiceType: " + authTokenContext.getServiceType());
    }
  }

  private void fetchNewToken(String vehicleId) {
    String url = "https://yourauthserver.example/token/" + vehicleId;

    try (Reader r = new URL(url).openStream()) {
      com.google.gson.JsonObject obj
          = new com.google.gson.JsonParser().parse(r);
      vehicleServiceToken = obj.get("VehicleServiceToken").getAsString();
      expiryTimeMs = obj.getAsLong("TokenExpiryMs");

      // The expiry time could be an hour from now, but just to try and avoid
      // passing expired tokens, we subtract 10 minutes from that time.
      expiryTimeMs -= 10 * 60 * 1000;
    } catch (IOException e) {
      // It's OK to throw exceptions here, the StatusListener you passed to
      // create the FleetEngine class will be notified and pass along the failed
      // update warning.
      throw new RuntimeException("Could not get auth token", e);
    }
  }
}