Hướng dẫn di chuyển SDK trình điều khiển Android 4.0

SDK trình điều khiển cho bản phát hành Android 4.0 yêu cầu bạn cập nhật mã cho một số hoạt động nhất định. Hướng dẫn này đề cập đến các thay đổi và những việc bạn cần làm để di chuyển mã của mình.

Đối với mô hình trình điều khiển untrusted, ứng dụng phân phối chỉ cần xác thực cho loại dịch vụ VEHICLE để bật tính năng báo cáo vị trí. Các ứng dụng có mô hình người lái xe đáng tin cậy phải cung cấp thông tin xác thực cho loại dịch vụ TASK để bật các phương thức báo cáo về điểm dừng xe trong DeliveryVehicleReporter.

Thay đổi tên gói

Tên gói đã thay đổi từ com.google.android.libraries.ridesharing.driver thành com.google.android.libraries.mapsplatform.transportation.driver. Vui lòng cập nhật các tệp tham chiếu trong mã của bạn.

Khởi chạy SDK

Trong các phiên bản trước, bạn sẽ khởi chạy SDK Điều hướng, sau đó lấy thông tin tham chiếu đến lớp FleetEngine. Trong SDK trình điều khiển phiên bản 4, hãy khởi chạy SDK như sau:

  1. Lấy đối tượng Navigator từ 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. Tạo đối tượng DriverContext, điền sẵn các trường bắt buộc.

    DriverContext driverContext = DriverContext.builder(application)
        .setProviderId(providerId)
        .setVehicleId(vehicleId)
        .setAuthTokenFactory(authTokenFactory)
        .setNavigator(navigator)
        .setRoadSnappedLocationProvider(
            NavigationApi.getRoadSnappedLocationProvider(application))
        .build();
    
  3. Sử dụng đối tượng DriverContext để khởi chạy *DriverApi.

    DeliveryDriverApi deliveryDriverApi = DeliveryDriverApi.createInstance(driverContext);
    
  4. Lấy NavigationVehicleReporter từ đối tượng API. *VehicleReporter mở rộng NavigationVehicleReporter.

    DeliveryVehicleReporter vehicleReporter = deliveryDriverApi.getDeliveryVehicleReporter();
    

Bật và tắt thông tin cập nhật về vị trí

Trong các phiên bản cũ, bạn cần bật tính năng cập nhật vị trí sau khi có được thông tin tham chiếu FleetEngine. Trong SDK Driver phiên bản 4, hãy bật tính năng cập nhật vị trí như sau:

DeliveryVehicleReporter reporter = ...;

reporter.enableLocationTracking();

Để cập nhật khoảng thời gian báo cáo, hãy sử dụng DeliveryVehicleReporter.setLocationReportingInterval(long, TimeUnit).

Khi người lái xe hoàn tất ca làm, hãy tắt tính năng cập nhật vị trí và đánh dấu xe là không có kết nối mạng bằng cách gọi NavigationVehicleReporter.disableLocationTracking().

Báo cáo lỗi bằng StatusListener

ErrorListener đã bị xoá và kết hợp với StatusListener, có thể được định nghĩa như sau:

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.
  }
}

Đang xác thực với AuthTokenFactory

AuthTokenFactory hiện chỉ có một phương thức là getToken(). Phương thức này lấy AuthTokenContext làm tham số.

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);
    }
  }
}

Danh sách Task trở thành danh sách TaskInfo

Danh sách Task đã được thay thế bằng danh sách TaskInfo trong VehicleStop. Ví dụ về mã sau đây minh hoạ cách tạo đối tượng VehicleStop.

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