Skip to content

Commit f57e025

Browse files
committed
[RELEASE] 4.18.1
1 parent ea924cb commit f57e025

File tree

18 files changed

+395
-178
lines changed

18 files changed

+395
-178
lines changed

CHANGELOG.md

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,11 @@
11
# Change Log
22

3-
## Unreleased
3+
## 4.18.1 — 2024-12-07
44
* [Android] Re-factor HeadlessTask.java. Introduce HeadlessTaskManager class.
5+
* [Android] remove previous requirement in `4.18.1` to call `BackgroundGeolocation.finishHeadlessTask(taskId)`.
56

67
## 4.18.0 — 2024-12-02
78
* [Android] implement support for new "Bridgeless Architecture"
8-
* [Android] Introduce new android-only method for signalling completion of your headless-tasks registered with `BackgroundGeolocation.registerHeadlessTask(bgGeoHeadlessTask)`. This allows RN to quickly free-up resources when your task is complete, signalling good background behaviour with the OS.
9-
10-
```typescript
11-
const bgGeoHeadlessTask = async (event) => {
12-
const params = event.params; // <-- our event-data from the BG Geo SDK.
13-
const eventName = event.name;
14-
const taskId = event.taskId; // <-- [NEW] very important!
15-
16-
console.log('[BGGeoHeadlessTask] ', eventName, taskId, params);
17-
await doWork();
18-
19-
// [NEW] Signal completion of our RN HeadlessTask !!!
20-
BackgroundGeolocation.finishHeadlessTask(event.taskId);
21-
}
22-
// Example "work" function where you might perform a long-running task (such as an HTTP request).
23-
// Uses a simple JS setTimeout timer to simulate work.
24-
const doWork = async () => {
25-
return new Promise(async (resolve, reject) => {
26-
// Start a BGGeo backgroundTask for performing long-running task (such as HTTP request)
27-
const bgTaskId = await BackgroundGeolocation.startBackgroundTask();
28-
console.log('*** [doWork] START');
29-
setTimeout(() => {
30-
console.log('*** [doWork] FINISH');
31-
// Signal completion of our bg-task.
32-
BackgroundGeolocation.stopBackgroundTask(bgTaskId);
33-
resolve();
34-
}, 5000);
35-
});
36-
}
37-
38-
BackgroundGeolocation.registerHeadlessTask(bgGeoHeadlessTask);
39-
```
409

4110
## 4.17.6 &mdash; 2024-11=12
4211
* [Android] Remove enforcement of minimum Geofence radius `150`

android/src/main/AndroidManifest.xml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2-
package="com.transistorsoft.rnbackgroundgeolocation">
3-
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
42
<application android:label="@string/app_name"></application>
5-
63
</manifest>

android/src/main/java/com/transistorsoft/rnbackgroundgeolocation/HeadlessTask.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ public void onHeadlessEvent(HeadlessEvent event) {
109109
.build()
110110
);
111111
} catch (Exception e) {
112-
TSLog.logger.warn(TSLog.warn("Failed invoke HeadlessTask " + name + ". Task ignored." + e.getMessage()));
112+
TSLog.logger.warn(TSLog.warn("Failed invoke HeadlessTask " + name + ". Task ignored: " + e.getMessage()));
113+
e.printStackTrace();
113114
}
114115
}
115116

android/src/main/java/com/transistorsoft/rnbackgroundgeolocation/HeadlessTaskManager.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import android.content.Context;
44
import android.os.Handler;
55
import android.os.Looper;
6-
import android.util.Log;
76

87
import androidx.annotation.NonNull;
98
import androidx.annotation.Nullable;
@@ -19,6 +18,7 @@
1918
import com.facebook.react.jstasks.HeadlessJsTaskConfig;
2019
import com.facebook.react.jstasks.HeadlessJsTaskContext;
2120
import com.facebook.react.jstasks.HeadlessJsTaskEventListener;
21+
import com.transistorsoft.locationmanager.logger.TSLog;
2222

2323
import java.lang.reflect.Method;
2424

@@ -119,7 +119,10 @@ public void finishTask(Context context, int taskId) throws TaskNotFoundError, Co
119119
if (reactContext != null) {
120120
Task task = findTask(taskId);
121121
if (task != null) {
122-
HeadlessJsTaskContext.getInstance(reactContext).finishTask(task.getReactTaskId());
122+
HeadlessJsTaskContext headlessJsTaskContext = HeadlessJsTaskContext.getInstance(reactContext);
123+
if (headlessJsTaskContext.isTaskRunning(task.getReactTaskId())) {
124+
headlessJsTaskContext.finishTask(task.getReactTaskId());
125+
}
123126
} else {
124127
throw new TaskNotFoundError(taskId);
125128
}
@@ -157,7 +160,7 @@ public void onHeadlessJsTaskFinish(int taskId) {
157160
removeTask(task);
158161
task.onFinish();
159162

160-
Log.d(TAG, "[onHeadlessJsTaskFinish] taskId: " + taskId);
163+
TSLog.logger.debug("[onHeadlessJsTaskFinish] taskId: " + taskId);
161164
}
162165

163166
private ReactNativeHost getReactNativeHost(Context context) {
@@ -185,7 +188,7 @@ private ReactContext getReactContext(Context context) {
185188
Method getCurrentReactContext = reactHost.getClass().getMethod("getCurrentReactContext");
186189
return (ReactContext) getCurrentReactContext.invoke(reactHost);
187190
} catch (Exception e) {
188-
Log.e(TAG, "Reflection error getCurrentReactContext: " + e.getMessage());
191+
TSLog.logger.error(TSLog.error( "Reflection error getCurrentReactContext: " + e.getMessage()), e);
189192
}
190193
}
191194
final ReactInstanceManager reactInstanceManager = getReactNativeHost(context).getReactInstanceManager();
@@ -202,7 +205,7 @@ private void createReactContextAndScheduleTask(Context context) {
202205
return;
203206
}
204207
if (mIsInitializingReactContext.compareAndSet(false, true)) {
205-
Log.d(TAG, "[createReactContextAndScheduleTask] initialize ReactContext");
208+
TSLog.logger.debug( "[createReactContextAndScheduleTask] initialize ReactContext");
206209
final Object reactHost = getReactHost(context);
207210
if (isBridglessArchitectureEnabled()) { // NEW arch
208211
ReactInstanceEventListener callback = new ReactInstanceEventListener() {
@@ -214,7 +217,7 @@ public void onReactContextInitialized(@NonNull ReactContext reactContext) {
214217
Method removeReactInstanceEventListener = reactHost.getClass().getMethod("removeReactInstanceEventListener", ReactInstanceEventListener.class);
215218
removeReactInstanceEventListener.invoke(reactHost, this);
216219
} catch (Exception e) {
217-
Log.e(TAG, "HeadlessTask reflection error removeReactInstanceEventListener: " + e);
220+
TSLog.logger.error(TSLog.error("HeadlessTask reflection error removeReactInstanceEventListener: ") + e);
218221
}
219222
}
220223
};
@@ -224,7 +227,7 @@ public void onReactContextInitialized(@NonNull ReactContext reactContext) {
224227
Method startReactHost = reactHost.getClass().getMethod("start");
225228
startReactHost.invoke(reactHost);
226229
} catch (Exception e) {
227-
Log.e(TAG, "HeadlessTask reflection error ReactHost start: " + e.getMessage());
230+
TSLog.logger.error(TSLog.error("HeadlessTask reflection error ReactHost start: " + e.getMessage()), e);
228231
}
229232
} else { // OLD arch
230233
final ReactInstanceManager reactInstanceManager = getReactNativeHost(context).getReactInstanceManager();
@@ -349,9 +352,11 @@ synchronized static int getNextTaskId() {
349352
mOnErrorCallback = builder.onErrorCallback;
350353
mTimeout = builder.timeout;
351354
mParams = builder.params;
352-
mParams.putInt("taskId", mId);
355+
// append our custom headless taskId.
356+
mParams.putInt("_transistorHeadlessTaskId", mId);
353357
}
354358

359+
355360
boolean invoke(ReactContext reactContext) throws IllegalStateException {
356361
HeadlessJsTaskContext headlessJsTaskContext = HeadlessJsTaskContext.getInstance(reactContext);
357362
// Provide the RN taskId to our private TaskConfig instance, mapping the RN taskId to our TaskConfig's internal taskId.

android/src/main/java/com/transistorsoft/rnbackgroundgeolocation/RNBackgroundGeolocationModule.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,12 @@ public void finish(int taskId, Callback success, Callback failure) {
825825
success.invoke(taskId);
826826
}
827827

828+
/**
829+
* Called by user's registered HeadlessTask when they call BackgroundGeolocation.finishHeadlessTask(event.taskId)
830+
* @param taskId
831+
* @param success
832+
* @param failure
833+
*/
828834
@ReactMethod
829835
public void finishHeadlessTask(int taskId, Callback success, Callback failure) {
830836
EventBus eventBus = EventBus.getDefault();

docs/assets/js/search.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)