คู่มือการย้ายข้อมูล Android Driver SDK 4.0

ในรุ่น Driver SDK สำหรับ Android 4.0 คุณต้องอัปเดตโค้ดสำหรับการทำงานบางอย่าง คู่มือนี้จะอธิบายถึงการเปลี่ยนแปลงและสิ่งที่คุณต้องทำในการย้ายข้อมูลโค้ด

สำหรับโมเดลไดรเวอร์ที่untrusted ไคลเอ็นต์การนำส่งจะต้องตรวจสอบสิทธิ์สำหรับบริการประเภท VEHICLE เท่านั้น ซึ่งจะเปิดใช้การรายงานตำแหน่ง ไคลเอ็นต์ที่มีโมเดลไดรเวอร์ที่เชื่อถือได้ต้องตรวจสอบสิทธิ์สำหรับประเภทบริการ TASK เพื่อเปิดใช้วิธีการรายงานการหยุดรถใน DeliveryVehicleReporter

การเปลี่ยนชื่อแพ็กเกจ

ชื่อแพ็กเกจเปลี่ยนจาก com.google.android.libraries.ridesharing.driver เป็น com.google.android.libraries.mapsplatform.transportation.driver โปรดอัปเดตการอ้างอิงในโค้ดของคุณ

การเริ่มต้น SDK

ในเวอร์ชันก่อนหน้านี้ คุณจะเริ่มต้น SDK การนำทาง จากนั้นรับการอ้างอิงคลาส FleetEngine ใน Driver SDK v4 ให้เริ่มต้น SDK ดังนี้

  1. รับออบเจ็กต์ Navigator จาก NavigationApi

    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. รับ NavigationVehicleReporter จากออบเจ็กต์ API *VehicleReporter จะขยายเวลา NavigationVehicleReporter

    DeliveryVehicleReporter vehicleReporter = deliveryDriverApi.getDeliveryVehicleReporter();
    

การเปิดและปิดใช้การอัปเดตตำแหน่ง

ในเวอร์ชันก่อนหน้านี้ คุณจะเปิดใช้การอัปเดตตำแหน่งหลังจากได้รับข้อมูลอ้างอิง FleetEngine ใน Driver SDK v4 ให้เปิดใช้การอัปเดตตำแหน่งดังนี้

DeliveryVehicleReporter reporter = ...;

reporter.enableLocationTracking();

หากต้องการอัปเดตช่วงเวลาการรายงาน ให้ใช้ DeliveryVehicleReporter.setLocationReportingInterval(long, TimeUnit)

เมื่อเปลี่ยนคนขับเสร็จแล้ว ให้ปิดการอัปเดตตำแหน่ง และทำเครื่องหมายว่ารถออฟไลน์อยู่โดยโทรหา NavigationVehicleReporter.disableLocationTracking()

การรายงานข้อผิดพลาดด้วย StatusListener

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 ด้วยรายการ TaskInfo ใน VehicleStop แล้ว ตัวอย่างโค้ดต่อไปนี้แสดงวิธีสร้างออบเจ็กต์ VehicleStop

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