คู่มือการย้ายข้อมูล 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

ในเวอร์ชันก่อนหน้า คุณจะต้องเริ่มต้น Navigation 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();