/** * Create an intent that contains a task that should be scheduled * on a default queue. * @param context context instance * @param task task instance * @param <T> task type * @see #taskIntent(android.content.Context, String, java.util.concurrent.Callable) */ public static <T extends Callable<?> & Parcelable> Intent taskIntent(final Context context, final T task) { return taskIntent(context, Goro.DEFAULT_QUEUE, task); }
private synchronized void bindIfRequired() { if (!bindRequested) { bindRequested = true; GoroService.bind(context, this); } }
@Override public int onStartCommand(final Intent intent, final int flags, final int startId) { if (intent != null) { Callable<?> task = getTaskFromExtras(intent); if (task != null) { injectContext(task); String queueName = intent.hasExtra(EXTRA_QUEUE_NAME) ? intent.getStringExtra(EXTRA_QUEUE_NAME) : Goro.DEFAULT_QUEUE; ObservableFuture<?> future = getBinder().goro.schedule(queueName, task); if (!intent.getBooleanExtra(EXTRA_IGNORE_ERROR, false)) { ensureErrorWillBeThrown(future); } } } return START_STICKY; }
@Test public void shouldScheduleTasksCreatedWithFactoryMethod() { service.onStartCommand(GoroService.taskIntent(application, task), 0, 1); queues.executeAll(); assertThat(executed).isTrue(); }
@Before public void init() { queues = new TestingQueues(); GoroService.setup(application, new Goro.GoroImpl(queues)); service = new GoroService(); service.onCreate(); task = new Task(new Callable<String>() { @Override public String call() throws Exception { executed = true; return "ok"; } }); executed = false; }
@Override public void onCreate() { super.onCreate(); objectGraph = ObjectGraph.create(new AppModule(this)); objectGraph.inject(this); GoroService.setup(this, goro); }
private void doUnbindLocked() { service = null; GoroService.unbind(context, this); }
@Override public IBinder onBind(final Intent intent) { if (DEBUG) { Log.w("Goro", "bind"); } hasBoundUsers = true; stopHandler.doNotStop(); return getBinder(); }
@Override public void handleMessage(final Message msg) { final GoroService service = serviceRef.get(); if (service == null) { return; } switch (msg.what) { case MSG_CHECK_FOR_STOP: if (!isServiceActive(service)) { if (DEBUG) { Log.w(TAG, "send stop"); } sendEmptyMessage(MSG_STOP); } break; case MSG_STOP: if (DEBUG) { Log.w(TAG, "do stop"); } service.stopSelf(); break; default: throw new IllegalArgumentException("Unexpected message " + msg); } }
@Test public void serviceContextShouldBeInjected() { Task task = mock(Task.class); service.onStartCommand(GoroService.taskIntent(application, task), 0, 1); verify(task).injectServiceContext(service); }
@Override protected void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.goro); GoroService.setup(this, GORO); if (savedInstanceState != null) { counter = savedInstanceState.getInt("counter"); } GoroView goroView = (GoroView) findViewById(R.id.goro); observer = new GoroStateObserver(goroView, savedInstanceState); View restButton = findViewById(R.id.button_post_rest); restButton.setOnClickListener(new Clicker(QUEUE_REST)); View dbButton = findViewById(R.id.button_post_db); dbButton.setOnClickListener(new Clicker(QUEUE_DB)); }
private synchronized void unbindIfRequired() { if (bindRequested && updateDelegate(null)) { bindRequested = false; GoroService.unbind(context, this); } }
@Override public IBinder onBind(final Intent intent) { if (DEBUG) { Log.i(TAG, "bind"); } hasBoundUsers = true; stopHandler.doNotStop(); return getBinder(); }
@Override public void handleMessage(final Message msg) { final GoroService service = serviceRef.get(); if (service == null) { return; } switch (msg.what) { case MSG_CHECK_FOR_STOP: if (!isServiceActive(service)) { if (DEBUG) { Log.w("Goro", "send stop"); } sendEmptyMessage(MSG_STOP); } break; case MSG_STOP: if (DEBUG) { Log.w("Goro", "do stop"); } service.stopSelf(); break; default: throw new IllegalArgumentException("Unexpected message " + msg); } }
@Override public int onStartCommand(final Intent intent, final int flags, final int startId) { if (intent != null) { Callable<?> task = getTaskFromExtras(intent); if (task != null) { injectContext(task); String queueName = intent.hasExtra(EXTRA_QUEUE_NAME) ? intent.getStringExtra(EXTRA_QUEUE_NAME) : Goro.DEFAULT_QUEUE; ObservableFuture<?> future = getBinder().goro.schedule(queueName, task); if (!intent.getBooleanExtra(EXTRA_IGNORE_ERROR, false)) { ensureErrorWillBeThrown(future); } } if (intent.hasExtra(EXTRA_NOTIFICATION_BUNDLE)) { Bundle bundle = intent.getBundleExtra(EXTRA_NOTIFICATION_BUNDLE); if (bundle != null) { Notification notification = bundle.getParcelable(EXTRA_NOTIFICATION); int notificationId = intent.getIntExtra(EXTRA_NOTIFICATION_ID, 0); if (notification != null) { startForeground(notificationId, notification); } } } } return START_STICKY; }
@Test public void canEatErrorsWhenAsked() throws Exception { Task task = mock(Task.class); Exception e = new Exception(); doThrow(e).when(task).call(); Intent intent = GoroService.taskIntent(application, task) .putExtra(GoroService.EXTRA_IGNORE_ERROR, true); service.onStartCommand(intent, 0, 1); queues.executeAll(); // and nothing happens }
/** * Create an intent that contains a task that should be scheduled * on a default queue. * @param context context instance * @param task task instance * @param <T> task type * @see #taskIntent(android.content.Context, String, java.util.concurrent.Callable) */ public static <T extends Callable<?> & Parcelable> Intent taskIntent(final Context context, final T task) { return taskIntent(context, Goro.DEFAULT_QUEUE, task); }
@Override public void run() { GoroService.setup(getContext(), Goro.createWithDelegate(executor)); Log.i("GoroTest", "BINDING..."); goro = Goro.from(bindService(new Intent())); assertThat(goro).isNotNull(); bindSyc.countDown(); } });
@Override public void bind() { synchronized (lock) { oneshot = false; } GoroService.bind(context, this); }
@Override public void unbind() { if (updateDelegate(null)) { GoroService.unbind(context, this); unbindRequested = false; } else { unbindRequested = true; } }