Android Driver SDK 3.0 이전 가이드

Android용 Driver SDK 3.0 릴리스에서는 코드를 업데이트해야 합니다. 사용할 수 있습니다 이 가이드에서는 변경사항과 코드를 마이그레이션해야 합니다

패키지 이름 변경

패키지 이름이 com.google.android.libraries.ridesharing.driver부터 com.google.android.libraries.mapsplatform.transportation.driver입니다. 제발 코드에서 참조를 업데이트합니다.

SDK 초기화

이전 버전에서는 Navigation SDK를 초기화한 다음 FleetEngine 클래스 참조 Driver SDK 내 v3를 사용하는 경우 다음과 같이 SDK를 초기화합니다.

  1. NavigationApi에서 Navigator 객체를 가져옵니다.

    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. API 객체에서 NavigationVehicleReporter를 가져옵니다. *VehicleReporterNavigationVehicleReporter를 확장합니다.

    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()입니다. 오류 차량 상태 업데이트는 선택적으로 제공된 DriverContext에서 설정된 StatusListener

StatusListener를 사용한 Error Reporting

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