Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.

Commit 5af489c

Browse files
authored
Merge pull request #175 from NativeScript/lini/marking-mode-none
feat: add markingMode: none to geolocation demo
2 parents 8d84e63 + 37966e6 commit 5af489c

File tree

4 files changed

+72
-123
lines changed

4 files changed

+72
-123
lines changed

demo/app/background-service.ts

Lines changed: 41 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,40 @@ import { device } from "tns-core-modules/platform";
55
import * as Toast from "nativescript-toast";
66

77
let watchId;
8-
application.on(application.exitEvent, function (args: any) {
8+
9+
function _clearWatch() {
910
if (watchId) {
1011
geolocation.clearWatch(watchId);
12+
watchId = null;
1113
}
12-
});
14+
}
15+
16+
function _startWatch() {
17+
geolocation.enableLocationRequest().then(function () {
18+
_clearWatch();
19+
watchId = geolocation.watchLocation(
20+
function (loc) {
21+
if (loc) {
22+
let toast = Toast.makeText('Background Location: \n' + loc.latitude + ', ' + loc.longitude);
23+
toast.show();
24+
console.log('Background Location: ' + loc.latitude + ' ' + loc.longitude);
25+
}
26+
},
27+
function (e) {
28+
console.log("Background watchLocation error: " + (e.message || e));
29+
},
30+
{
31+
desiredAccuracy: Accuracy.high,
32+
updateDistance: 1.0,
33+
updateTime: 3000,
34+
minimumUpdateTime: 100
35+
});
36+
}, function (e) {
37+
console.log("Background enableLocationRequest error: " + (e.message || e));
38+
});
39+
}
40+
41+
application.on(application.exitEvent, _clearWatch);
1342

1443
if (application.android) {
1544
if (device.sdkVersion < "26") {
@@ -19,28 +48,7 @@ if (application.android) {
1948
return android.app.Service.START_STICKY;
2049
},
2150
onCreate: function () {
22-
let that = this;
23-
geolocation.enableLocationRequest().then(function () {
24-
that.id = geolocation.watchLocation(
25-
function (loc) {
26-
if (loc) {
27-
let toast = Toast.makeText('Background Location: ' + loc.latitude + ' ' + loc.longitude);
28-
toast.show();
29-
console.log('Background Location: ' + loc.latitude + ' ' + loc.longitude);
30-
}
31-
},
32-
function (e) {
33-
console.log("Background watchLocation error: " + (e.message || e));
34-
},
35-
{
36-
desiredAccuracy: Accuracy.high,
37-
updateDistance: 0.1,
38-
updateTime: 3000,
39-
minimumUpdateTime: 100
40-
});
41-
}, function (e) {
42-
console.log("Background enableLocationRequest error: " + (e.message || e));
43-
});
51+
_startWatch();
4452
},
4553
onBind: function (intent) {
4654
console.log("on Bind Services");
@@ -50,45 +58,22 @@ if (application.android) {
5058
},
5159
onDestroy: function () {
5260
console.log('service onDestroy');
53-
geolocation.clearWatch(this.id);
61+
_clearWatch();
5462
}
5563
});
5664
}
5765
else {
5866
(<any>android.app).job.JobService.extend("com.nativescript.location.BackgroundService26", {
59-
onStartJob(params) {
60-
let executed = false;
61-
geolocation.enableLocationRequest().then(function () {
62-
watchId = geolocation.watchLocation(
63-
function (loc) {
64-
if (loc) {
65-
let toast = Toast.makeText('Background Location: ' + loc.latitude + ' ' + loc.longitude);
66-
toast.show();
67-
console.log('Background Location: ' + loc.latitude + ' ' + loc.longitude);
68-
}
69-
executed = true;
70-
},
71-
function (e) {
72-
console.log("Background watchLocation error: " + (e.message || e));
73-
executed = true;
74-
},
75-
{
76-
desiredAccuracy: Accuracy.high,
77-
updateDistance: 0.1,
78-
updateTime: 3000,
79-
minimumUpdateTime: 100
80-
});
81-
}, function (e) {
82-
console.log("Background enableLocationRequest error: " + (e.message || e));
83-
});
84-
85-
return executed;
67+
onStartJob() {
68+
console.log('service onStartJob');
69+
_startWatch();
70+
return true;
8671
},
87-
88-
onStopJob() {
72+
onStopJob(jobParameters: any) {
8973
console.log('service onStopJob');
90-
geolocation.clearWatch(watchId);
91-
return true;
74+
this.jobFinished(jobParameters, false);
75+
_clearWatch();
76+
return false;
9277
},
9378
});
9479
}

demo/app/main-page.ts

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,23 @@ const utils = require("tns-core-modules/utils/utils");
77
import * as application from "tns-core-modules/application";
88
import { device } from "tns-core-modules/platform";
99

10-
let locationService = require('./background-service');
11-
1210
let page: Page;
1311
let model = new MainViewModel();
1412
let watchIds = [];
15-
let backgroundIds = [];
13+
const jobId = 308; // the id should be unique for each background job. We only use one, so we set the id to be the same each time.
1614
declare var com: any;
1715

18-
application.on(application.exitEvent, function (args: any) {
19-
if (application.android && backgroundIds.length > 0) {
16+
function _stopBackgroundJob() {
17+
if (application.android) {
2018
let context = utils.ad.getApplicationContext();
2119
const jobScheduler = context.getSystemService((<any>android.content.Context).JOB_SCHEDULER_SERVICE);
22-
const service = backgroundIds.pop();
23-
jobScheduler.cancel(service);
24-
console.log(`Job Canceled: ${service}`);
20+
if (jobScheduler.getPendingJob(jobId) !== null) {
21+
jobScheduler.cancel(jobId);
22+
console.log(`Job Canceled: ${jobId}`);
23+
}
2524
}
26-
});
25+
}
26+
application.on(application.exitEvent, _stopBackgroundJob);
2727

2828
export function pageLoaded(args: EventData) {
2929
page = <Page>args.object;
@@ -33,33 +33,26 @@ export function pageLoaded(args: EventData) {
3333
export function startBackgroundTap() {
3434
if (application.android) {
3535
let context = utils.ad.getApplicationContext();
36-
let intent = new android.content.Intent(context, com.nativescript.location.BackgroundService.class);
3736
if (device.sdkVersion >= "26") {
38-
const component = new android.content.ComponentName(context, com.nativescript.location.BackgroundService26.class);
39-
const builder = new (<any>android.app).job.JobInfo.Builder(1, component);
40-
builder.setRequiredNetworkType((<any>android.app).job.JobInfo.NETWORK_TYPE_ANY);
41-
builder.setPeriodic(15 * 60 * 1000);
4237
const jobScheduler = context.getSystemService((<any>android.content.Context).JOB_SCHEDULER_SERVICE);
43-
const service = jobScheduler.schedule(builder.build());
44-
backgroundIds.push(service);
38+
const component = new android.content.ComponentName(context, com.nativescript.location.BackgroundService26.class);
39+
const builder = new (<any>android.app).job.JobInfo.Builder(jobId, component);
40+
builder.setOverrideDeadline(0);
41+
return jobScheduler.schedule(builder.build());
4542
} else {
43+
let intent = new android.content.Intent(context, com.nativescript.location.BackgroundService.class);
4644
context.startService(intent);
4745
}
4846
}
4947
}
5048

5149
export function stopBackgroundTap() {
5250
if (application.android) {
53-
let context = utils.ad.getApplicationContext();
54-
let intent = new android.content.Intent(context, com.nativescript.location.BackgroundService.class);
5551
if (device.sdkVersion >= "26") {
56-
if (backgroundIds.length > 0) {
57-
const jobScheduler = context.getSystemService((<any>android.content.Context).JOB_SCHEDULER_SERVICE);
58-
const service = backgroundIds.pop();
59-
jobScheduler.cancel(service);
60-
console.log(`Job Canceled: ${service}`);
61-
}
52+
_stopBackgroundJob();
6253
} else {
54+
let context = utils.ad.getApplicationContext();
55+
let intent = new android.content.Intent(context, com.nativescript.location.BackgroundService.class);
6356
context.stopService(intent);
6457
}
6558
}
@@ -79,18 +72,17 @@ export function enableLocationTap() {
7972
}
8073

8174
export function buttonGetLocationTap() {
82-
let location = geolocation.getCurrentLocation({
75+
geolocation.getCurrentLocation({
8376
desiredAccuracy: Accuracy.high,
8477
maximumAge: 5000,
8578
timeout: 10000
86-
})
87-
.then(function (loc) {
88-
if (loc) {
89-
model.locations.push(loc);
90-
}
91-
}, function (e) {
92-
console.log("Error: " + (e.message || e));
93-
});
79+
}).then(function (loc) {
80+
if (loc) {
81+
model.locations.push(loc);
82+
}
83+
}, function (e) {
84+
console.log("Error: " + (e.message || e));
85+
});
9486
}
9587

9688
export function buttonStartTap() {
@@ -106,7 +98,7 @@ export function buttonStartTap() {
10698
},
10799
{
108100
desiredAccuracy: Accuracy.high,
109-
updateDistance: 0.1,
101+
updateDistance: 1,
110102
updateTime: 3000,
111103
minimumUpdateTime: 100
112104
}));

demo/app/package.json

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,9 @@
22
"name": "tns-template-hello-world-ts",
33
"main": "app.js",
44
"version": "1.6.0",
5-
"author": {
6-
"name": "Telerik",
7-
"email": "support@telerik.com"
8-
},
9-
"description": "Nativescript hello-world-ts project template",
10-
"license": "Apache-2.0",
11-
"keywords": [
12-
"telerik",
13-
"mobile",
14-
"nativescript",
15-
"{N}",
16-
"tns",
17-
"appbuilder",
18-
"template"
19-
],
20-
"repository": {
21-
"type": "git",
22-
"url": "git+ssh://git@github.com/NativeScript/template-hello-world-ts.git"
23-
},
24-
"bugs": {
25-
"url": "https://github.com/NativeScript/template-hello-world-ts/issues"
26-
},
27-
"homepage": "https://github.com/NativeScript/template-hello-world-ts",
285
"android": {
296
"v8Flags": "--expose_gc",
7+
"markingMode": "none",
308
"requireModules": ["nativescript-geolocation"]
31-
},
32-
"directories": {},
33-
"readme": "ERROR: No README data found!"
9+
}
3410
}

src/geolocation.android.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -275,12 +275,8 @@ function _isLocationServiceEnabled(options?: Options): Promise<boolean> {
275275
locationSettingsBuilder.setAlwaysShow(true);
276276
let locationSettingsClient = com.google.android.gms.location.LocationServices.getSettingsClient(androidAppInstance.context);
277277
locationSettingsClient.checkLocationSettings(locationSettingsBuilder.build())
278-
.addOnSuccessListener(_getTaskSuccessListener((a) => {
279-
resolve();
280-
}))
281-
.addOnFailureListener(_getTaskFailListener((ex) => {
282-
reject(ex);
283-
}));
278+
.addOnSuccessListener(_getTaskSuccessListener(resolve))
279+
.addOnFailureListener(_getTaskFailListener(reject));
284280
});
285281
}
286282

@@ -342,7 +338,7 @@ function androidLocationFromLocation(location: Location): android.location.Locat
342338
return androidLocation;
343339
}
344340

345-
// absctaction for unit testing
341+
// abstraction for unit testing
346342
export class LocationManager {
347343
static getLastLocation(maximumAge, resolve, reject): Promise<Location> {
348344
_ensureLocationClient();

0 commit comments

Comments
 (0)