Android Driver SDK 3.0 遷移指南

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

套件名稱變更

套件名稱已從 com.google.android.libraries.ridesharing.driver 變更為 com.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();
    

啟用及停用位置更新功能

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

RidesharingVehicleReporter reporter = ...;

reporter.enableLocationTracking();

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

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

在伺服器上設定車輛狀態

在舊版中,您將使用 FleetEngine 物件設定車輛狀態。在 Driver 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);
    }
  }
}