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