Пример #1
0
  synchronized void start() {
    if (!serviceMgr.isTowerConnected())
      throw new IllegalStateException("Service manager must be connected.");

    IDroneApi droneApi = droneApiRef.get();
    if (isStarted(droneApi)) return;

    try {
      droneApi =
          serviceMgr
              .get3drServices()
              .registerDroneApi(this.apiListener, serviceMgr.getApplicationId());
      droneApi.asBinder().linkToDeath(binderDeathRecipient, 0);
    } catch (RemoteException e) {
      throw new IllegalStateException("Unable to retrieve a valid drone handle.");
    }

    if (asyncScheduler == null || asyncScheduler.isShutdown())
      asyncScheduler = Executors.newFixedThreadPool(1);

    addAttributesObserver(droneApi, this.droneObserver);
    resetFlightTimer();

    droneApiRef.set(droneApi);
  }
Пример #2
0
 private void handleRemoteException(RemoteException e) {
   final IDroneApi droneApi = droneApiRef.get();
   if (droneApi != null && !droneApi.asBinder().pingBinder()) {
     final String errorMsg = e.getMessage();
     Log.e(TAG, errorMsg, e);
     notifyDroneServiceInterrupted(errorMsg);
   }
 }
Пример #3
0
 public void removeMavlinkObserver(MavlinkObserver observer) {
   final IDroneApi droneApi = droneApiRef.get();
   if (isStarted(droneApi)) {
     try {
       droneApi.removeMavlinkObserver(observer);
     } catch (RemoteException e) {
       handleRemoteException(e);
     }
   }
 }
Пример #4
0
  public boolean performAsyncActionOnHandler(
      Action action, Handler handler, AbstractCommandListener listener) {
    final IDroneApi droneApi = droneApiRef.get();
    if (isStarted(droneApi)) {
      try {
        droneApi.executeAsyncAction(action, wrapListener(handler, listener));
        return true;
      } catch (RemoteException e) {
        handleRemoteException(e);
      }
    }

    return false;
  }
Пример #5
0
 private void removeAttributesObserver(IDroneApi droneApi, IObserver observer) {
   if (isStarted(droneApi)) {
     try {
       droneApi.removeAttributesObserver(observer);
     } catch (RemoteException e) {
       handleRemoteException(e);
     }
   }
 }
Пример #6
0
  synchronized void destroy() {
    IDroneApi droneApi = droneApiRef.get();

    removeAttributesObserver(droneApi, this.droneObserver);

    try {
      if (isStarted(droneApi)) {
        droneApi.asBinder().unlinkToDeath(binderDeathRecipient, 0);
        serviceMgr.get3drServices().releaseDroneApi(droneApi);
      }
    } catch (RemoteException | NoSuchElementException e) {
      Log.e(TAG, e.getMessage(), e);
    }

    if (asyncScheduler != null) {
      asyncScheduler.shutdownNow();
      asyncScheduler = null;
    }

    droneApiRef.set(null);
  }
Пример #7
0
  public <T extends Parcelable> T getAttribute(String type) {
    final IDroneApi droneApi = droneApiRef.get();
    if (!isStarted(droneApi) || type == null) return this.getAttributeDefaultValue(type);

    T attribute = null;
    Bundle carrier = null;
    try {
      carrier = droneApi.getAttribute(type);
    } catch (RemoteException e) {
      handleRemoteException(e);
    }

    if (carrier != null) {
      try {
        carrier.setClassLoader(contextClassLoader);
        attribute = carrier.getParcelable(type);
      } catch (Exception e) {
        Log.e(TAG, e.getMessage(), e);
      }
    }

    return attribute == null ? this.<T>getAttributeDefaultValue(type) : attribute;
  }
Пример #8
0
 private boolean isStarted(IDroneApi droneApi) {
   return droneApi != null && droneApi.asBinder().pingBinder();
 }