@Override public void onError(final Throwable error) { throw new GoroException( "Uncaught error thrown by a task scheduled with startService()", error ); } }
@Test public void shouldNotEatErrors() throws Exception { Task task = mock(Task.class); Exception e = new Exception(); doThrow(e).when(task).call(); service.onStartCommand(GoroService.taskIntent(application, task), 0, 1); try { queues.executeAll(); fail("Error was not rethrown"); } catch (GoroException ge) { assertThat(ge.getCause()).isSameAs(e); } }
@Override public void onError(final Throwable error) { throw new GoroException( "Uncaught error thrown by a task scheduled with startService()", error ); } }
@Override public void onServiceDisconnected(final ComponentName name) { synchronized (lock) { if (service != null) { throw new GoroException("GoroService disconnected while we are using it"); } } }
public void addTaskListener(final GoroListener listener) { if (listener == null) { throw new IllegalArgumentException("Listener cannot be null"); } checkThread(); if (taskListeners.contains(listener)) { throw new GoroException("Listener " + listener + " is already registered"); } taskListeners.add(listener); }
/** * Bind to Goro service. This method will start the service and then bind to it. * @param context context that is binding to the service * @param connection service connection callbacks * @throws GoroException when service is not declared in the application manifest */ public static void bind(final Context context, final ServiceConnection connection) { Intent serviceIntent = new Intent(context, GoroService.class); if (context.startService(serviceIntent) == null) { throw new GoroException("Service " + GoroService.class + " does not seem to be included to your manifest file"); } boolean bound = context.bindService(serviceIntent, connection, 0); if (!bound) { throw new GoroException("Cannot bind to GoroService though this component seems to " + "be registered in the app manifest"); } }
private static void checkThread() { if (!checkMainThread()) { throw new GoroException("Listeners cannot be modified outside the main thread"); } }
public void addTaskListener(final GoroListener listener) { if (listener == null) { throw new IllegalArgumentException("Listener cannot be null"); } checkThread(); if (taskListeners.contains(listener)) { throw new GoroException("Listener " + listener + " is already registered"); } taskListeners.add(listener); }
private static void checkThread() { if (!checkMainThread()) { throw new GoroException("Listeners cannot be modified outside the main thread"); } }
public void removeTaskListener(final GoroListener listener) { if (listener == null) { throw new IllegalArgumentException("Listener cannot be null"); } checkThread(); if (!taskListeners.remove(listener)) { throw new GoroException("Listener " + listener + " is not registered"); } }
public void removeTaskListenerOrThrow(final GoroListener listener) { if (!removeTaskListener(listener)) { throw new GoroException("Listener " + listener + " is not registered"); } }
/** * Bind to Goro service. This method will start the service and then bind to it. * @param context context that is binding to the service * @param connection service connection callbacks * @throws GoroException when service is not declared in the application manifest */ public static void bind(final Context context, final ServiceConnection connection) { Intent serviceIntent = new Intent(context, GoroService.class); if (context.startService(serviceIntent) == null) { throw new GoroException("Service " + GoroService.class + " does not seem to be included to your manifest file"); } boolean bound = context.bindService(serviceIntent, connection, 0); if (!bound) { throw new GoroException("Cannot bind to GoroService though this component seems to " + "be registered in the app manifest"); } }
throw new GoroException("Got a new delegate " + delegate + " while being attached to another " + this.delegate);
@Override public synchronized T get(final long timeout, @SuppressWarnings("NullableProblems") final TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { // delegate if (goroFuture != null) { return goroFuture.get(timeout, unit); } if (checkMainThread()) { throw new GoroException("Blocking main thread here will lead to a deadlock"); } if (canceled) { throw new CancellationException("Task was canceled"); } // wait for act() or cancel() wait(unit.toMillis(timeout)); // either we got a delegate if (goroFuture != null) { return goroFuture.get(); } // or we are canceled if (canceled) { throw new CancellationException("Task was canceled"); } // otherwise it's a timeout throw new TimeoutException(); }
@Override public synchronized T get(final long timeout, @SuppressWarnings("NullableProblems") final TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { // delegate if (goroFuture != null) { return goroFuture.get(timeout, unit); } if (checkMainThread()) { throw new GoroException("Blocking main thread here will lead to a deadlock"); } if (canceled) { throw new CancellationException("Task was canceled"); } // wait for act() or cancel() wait(unit.toMillis(timeout)); // either we got a delegate if (goroFuture != null) { return goroFuture.get(); } // or we are canceled if (canceled) { throw new CancellationException("Task was canceled"); } // otherwise it's a timeout throw new TimeoutException(); }
@Override public synchronized T get() throws InterruptedException, ExecutionException { // delegate if (goroFuture != null) { return goroFuture.get(); } if (checkMainThread()) { throw new GoroException("Blocking main thread here will lead to a deadlock"); } if (canceled) { throw new CancellationException("Task was canceled"); } // wait for act() or cancel() wait(); // either we got a delegate if (goroFuture != null) { return goroFuture.get(); } // or we are canceled if (canceled) { throw new CancellationException("Task was canceled"); } // wtf? throw new IllegalStateException("get() is unblocked but there is neither result" + " nor cancellation"); }
@Override public synchronized T get() throws InterruptedException, ExecutionException { // delegate if (goroFuture != null) { return goroFuture.get(); } if (checkMainThread()) { throw new GoroException("Blocking main thread here will lead to a deadlock"); } if (canceled) { throw new CancellationException("Task was canceled"); } // wait for act() or cancel() wait(); // either we got a delegate if (goroFuture != null) { return goroFuture.get(); } // or we are canceled if (canceled) { throw new CancellationException("Task was canceled"); } // wtf? throw new IllegalStateException("get() is unblocked but there is neither result" + " nor cancellation"); }
@Override public void onServiceConnected(final ComponentName name, final IBinder binder) { synchronized (lock) { if (service != null) { throw new GoroException("Already bound and got onServiceConnected from " + name); } service = Goro.from(binder); // delegate listeners if (!scheduledListeners.taskListeners.isEmpty()) { for (GoroListener listener : scheduledListeners.taskListeners) { service.addTaskListener(listener); } scheduledListeners.taskListeners.clear(); } // delegate tasks if (!postponed.isEmpty()) { for (Postponed p : postponed) { p.act(service); } postponed.clear(); } if (oneshot) { doUnbindLocked(); } } }