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

SDK trình điều khiển dành cho bản phát hành Android 3.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.

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 3, 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.

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

    RidesharingVehicleReporter vehicleReporter = ridesharingDriverApi.getRidesharingVehicleReporter();
    

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 3, hãy bật tính năng cập nhật vị trí như sau:

RidesharingVehicleReporter reporter = ...;

reporter.enableLocationTracking();

Để cập nhật khoảng thời gian báo cáo, hãy sử dụng RidesharingVehicleReporter.setLocationReportingInterval(long, TimeUnit) hoặc 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().

Đặt trạng thái xe trên máy chủ

Trong các phiên bản cũ, bạn sẽ sử dụng đối tượng FleetEngine để đặt trạng thái xe. Trong SDK trình điều khiển phiên bản 3, hãy dùng đối tượng RidesharingVehicleReporter để đặt trạng thái xe:

  RidesharingVehicleReporter reporter = ...;

  reporter.enableLocationTracking();
  reporter.setVehicleState(VehicleState.ONLINE);

Để đặt trạng thái xe thành OFFLINE, hãy gọi RidesharingVehicleReporter.disableLocationTracking(). Các lỗi cập nhật trạng thái xe được phổ biến bằng cách sử dụng StatusListener được cung cấp (không bắt buộc) được đặt trong DriverContext.

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ố. Ứng dụng đi chung xe phải xác thực loại dịch vụ VEHICLE để bật tính năng báo cáo vị trí và báo cáo trạng thái xe (trực tuyến/ngoại tuyến).

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