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

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

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

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

เริ่มต้น SDK

ในเวอร์ชันก่อนหน้า คุณจะต้องเริ่มต้น Navigation SDK จากนั้นจึงรับการอ้างอิงไปยังคลาส FleetEngine ใน Driver SDK v3 ให้เริ่มต้น 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

    RidesharingDriverApi ridesharingDriverApi = RidesharingDriverApi.createInstance(driverContext);
    
  4. รับ NavigationVehicleReporter จากออบเจ็กต์ API *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() ข้อผิดพลาดในการอัปเดตสถานะพาหนะจะเผยแพร่โดยใช้ชุด StatusListener ที่เป็นตัวเลือกซึ่งระบุไว้ใน DriverContext

การรายงานข้อผิดพลาดด้วย 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 เป็นพารามิเตอร์ ลูกค้าบริการร่วมเดินทางต้องตรวจสอบสิทธิ์สำหรับบริการประเภท 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);
    }
  }
}