Android Driver SDK 4.0 遷移指南

適用於 Android 4.0 版的 Driver SDK 要求你更新程式碼 對某些作業執行特定作業本指南將概述變更內容以及 您不需要遷移程式碼

變更套件名稱

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

初始化 SDK

在先前的版本中,您會初始化 Navigation SDK,然後取得 FleetEngine 類別的參照。在驅動程式 SDK 中 第 4 版是否已初始化 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 參照。在驅動程式 SDK v4 中,啟用 位置更新如下:

RidesharingVehicleReporter reporter = ...;

reporter.enableLocationTracking();

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

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

在伺服器上設定車輛狀態

在先前的版本中,您會使用 FleetEngine 物件來設定 例如車輛狀態在驅動程式 SDK v4 中,使用 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 做為參數。ServiceType 已淘汰你現在 只需取得所含車輛 ID 理賠,而非依賴 ServiceType

class JsonAuthTokenFactory implements AuthTokenFactory {
  private String token;  // initially null
  private 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(authTokenContext.getVehicleId());
    }
    return token;
  }

  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();
      token = obj.get("Token").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 is notified, and passes along the failed
      // update warning.
      throw new RuntimeException("Could not get auth token", e);
    }
  }
}