Android Driver SDK 4.0 遷移指南

Android 4.0 適用的驅動程式 SDK 需要針對特定作業更新程式碼。本指南將概述異動內容,以及遷移程式碼時需要採取的行動。

如果是untrusted驅動程式模型,傳送用戶端只需要驗證 VEHICLE 服務類型,即可啟用位置回報功能。具有「可信任」驅動程式模型的用戶端必須為 TASK 服務類型提供驗證,才能在 DeliveryVehicleReporter 中啟用車輛停靠回報方法。

套件名稱變更

套件名稱已從 com.google.android.libraries.ridesharing.driver 變更為 com.google.android.libraries.mapsplatform.transportation.driver。請更新程式碼中的參照。

初始化 SDK

在先前版本中,您需要初始化 Navigation SDK,然後取得 FleetEngine 類別的參照。在驅動程式 SDK v4 中,依照下列方式初始化 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

    DeliveryDriverApi deliveryDriverApi = DeliveryDriverApi.createInstance(driverContext);
    
  4. 從 API 物件取得 NavigationVehicleReporter*VehicleReporter 擴充 NavigationVehicleReporter

    DeliveryVehicleReporter vehicleReporter = deliveryDriverApi.getDeliveryVehicleReporter();
    

啟用及停用位置更新功能

在先前版本中,您必須在取得 FleetEngine 參照後啟用位置更新功能。在 Driver SDK v4 中,啟用位置更新功能,如下所示:

DeliveryVehicleReporter reporter = ...;

reporter.enableLocationTracking();

如要更新報表間隔,請使用 DeliveryVehicleReporter.setLocationReportingInterval(long, TimeUnit)

駕駛班完成後,請停用位置更新功能,並呼叫 NavigationVehicleReporter.disableLocationTracking() 將車輛標示為離線。

搭配 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 做為參數。

class JsonAuthTokenFactory implements AuthTokenFactory {
  // Initially null.
  private String vehicleServiceToken;
  // Initially null. Only used in the trusted driver model to authenticate
  // vehicle-stop reporting.
  private String taskServiceToken;
  private long expiryTimeMs = 0;

  // This method is called on a thread that only sends location updates (and
  // vehicle stop updates if you choose to report them). Blocking is OK, but just
  // know that no 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 if (ServiceType.TASK.equals(authTokenContext.getServiceType())) {
      // Only used for the trusted driver model to access vehicle-stop reporting
      // methods in DeliveryVehicleReporter.
      return taskServiceToken;
    } else {
      throw new RuntimeException("Unsupported ServiceType: " + authTokenContext.getServiceType());
    }
  }

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

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

      // 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 DriverContext class will be notified and passed along the failed
      // update warning.
      throw new RuntimeException("Could not get auth token", e);
    }
  }
}

Task 清單會變成 TaskInfo 清單

Task 清單已替換為 VehicleStop 中的 TaskInfo 清單。以下程式碼範例示範如何建立 VehicleStop 物件。

VehicleStop vehicleStop = VehicleStop.builder()
    .setTaskInfoList(taskInfoList)
    .setWaypoint(waypoint)
    .setVehicleStopState(vehicleStopState)
    .build();