@Override protected @Nullable HeadlessJsTaskConfig getTaskConfig(Intent intent) { Bundle extras = intent.getExtras(); // If extras have been passed to the intent, pass them on into the JS as taskData // which can be accessed as the first param. WritableMap data = /* extras != null ? Arguments.fromBundle(extras) : */ Arguments.createMap(); int timeout = extras.getInt("timeout"); Log.d(TAG, String.format("Returning HeadlessJsTaskConfig, timeout=%s ms", timeout)); return new HeadlessJsTaskConfig( // The the task was registered with in JS - must match "BackgroundTask", data, TimeUnit.SECONDS.toMillis(timeout) ); }
@Override protected HeadlessJsTaskConfig getTaskConfig(Intent intent) { boolean allowForeground = Boolean.valueOf(getString(R.string.rnsb_allow_foreground)); if(allowForeground || !isAppOnForeground(this)) { return new HeadlessJsTaskConfig( TASK_ID, null, Long.valueOf(getString(R.string.rnsb_default_timeout)), allowForeground); } stopSelf(); return null; }
@Override public int onStartCommand(Intent intent, int flags, int startId) { HeadlessJsTaskConfig taskConfig = getTaskConfig(intent); if (taskConfig != null) { startTask(taskConfig); return START_REDELIVER_INTENT; } return START_NOT_STICKY; }
@Nullable @Override protected HeadlessJsTaskConfig getTaskConfig(Intent intent) { Log.d(LOG_TAG, "getTaskConfig() called with: intent = [" + intent + "]"); Bundle extras = intent.getExtras(); boolean allowExecutionInForeground = extras.getBoolean("allowExecutionInForeground", false); long timeout = extras.getLong("timeout", 2000); // For task with quick execution period additional check is required ReactNativeHost reactNativeHost = ((ReactApplication) getApplicationContext()).getReactNativeHost(); boolean appInForeground = Utils.isReactNativeAppInForeground(reactNativeHost); if (appInForeground && !allowExecutionInForeground) { return null; } return new HeadlessJsTaskConfig(intent.getStringExtra("jobKey"), Arguments.fromBundle(extras), timeout, allowExecutionInForeground); }
@Override @Nullable protected HeadlessJsTaskConfig getTaskConfig(Intent intent) { if (intent.getExtras() == null) { return null; } GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent); if (geofencingEvent.hasError()) { // Suppress geofencing event with error Log.d(TAG, "Suppress geocoding event with error"); return null; } WritableMap location = Arguments.createMap(); location.putDouble("latitude", geofencingEvent.getTriggeringLocation().getLatitude()); location.putDouble("longitude", geofencingEvent.getTriggeringLocation().getLongitude()); WritableMap region = Arguments.createMap(); region.putString("identifier", geofencingEvent.getTriggeringGeofences().get(0).getRequestId()); WritableArray regionIdentifiers = Arguments.createArray(); for (Geofence triggered: geofencingEvent.getTriggeringGeofences()) { regionIdentifiers.pushString(triggered.getRequestId()); } region.putArray("identifiers", regionIdentifiers); WritableMap jsArgs = Arguments.createMap(); jsArgs.putMap("location", location); jsArgs.putMap("region", region); jsArgs.putBoolean("didEnter", geofencingEvent.getGeofenceTransition() == Geofence.GEOFENCE_TRANSITION_ENTER); jsArgs.putBoolean("didExit", geofencingEvent.getGeofenceTransition() == Geofence.GEOFENCE_TRANSITION_EXIT); //jsArgs.putBoolean("didDwell", geofencingEvent.getGeofenceTransition() == Geofence.GEOFENCE_TRANSITION_DWELL); Log.d(TAG, "Report geofencing event to JS: " + jsArgs); return new HeadlessJsTaskConfig(RNRegionMonitorModule.TRANSITION_TASK_NAME, jsArgs, 0, true); }
private void invokeStartTask(ReactContext reactContext, HeadlessJsTaskConfig taskConfig) { HeadlessJsTaskContext headlessJsTaskContext = HeadlessJsTaskContext.getInstance(reactContext); headlessJsTaskContext.addTaskEventListener(this); int taskId = headlessJsTaskContext.startTask(taskConfig); mActiveTasks.add(taskId); }
@Override protected @Nullable HeadlessJsTaskConfig getTaskConfig(Intent intent) { String event = intent.getStringExtra(BleService.EXTRA_EVENT); Log.w("BleEventListener", event); WritableMap writableMap = Arguments.createMap(); switch (event) { case BleService.ACTION_SERVICES_DISCOVERED: Log.w("BleEventListener", "ACTION_SERVICES_DISCOVERED"); Peripheral peripheral = intent.getParcelableExtra(BleService.EXTRA_VALUE); writableMap.putMap("peripheral", BleNative.constructPeripheralMap(peripheral)); writableMap.putString("event", "serviceDiscovered"); return new HeadlessJsTaskConfig( "bleEvent", writableMap, 10000); case BleService.ACTION_STATE_CHANGED: writableMap.putString("state", intent.getStringExtra(BleService.EXTRA_STATE)); writableMap.putString("id", intent.getStringExtra(BleService.EXTRA_ADDRESS)); writableMap.putString("event", "stateChanged"); return new HeadlessJsTaskConfig( "bleEvent", writableMap, 10000); case BleService.ACTION_BOND_STATE_CHANGED: writableMap.putString("state", intent.getStringExtra(BleService.EXTRA_STATE)); writableMap.putString("id", intent.getStringExtra(BleService.EXTRA_ADDRESS)); writableMap.putString("event", "bondStateChanged"); return new HeadlessJsTaskConfig( "bleEvent", writableMap, 10000); default: break; } return null; }
private void invokeStartTask(ReactContext reactContext, final HeadlessJsTaskConfig taskConfig) { final HeadlessJsTaskContext headlessJsTaskContext = HeadlessJsTaskContext.getInstance(reactContext); headlessJsTaskContext.addTaskEventListener(this); mActiveTaskContext = headlessJsTaskContext; UiThreadUtil.runOnUiThread(new Runnable() { @Override public void run() { int taskId = headlessJsTaskContext.startTask(taskConfig); } }); }
/** * Called from {@link #onStartCommand} to create a {@link HeadlessJsTaskConfig} for this intent. * @param intent the {@link Intent} received in {@link #onStartCommand}. * @return a {@link HeadlessJsTaskConfig} to be used with {@link #startTask}, or * {@code null} to ignore this command. */ protected @Nullable HeadlessJsTaskConfig getTaskConfig(Intent intent) { return null; }