diff --git a/demo/app/background-service.ts b/demo/app/background-service.ts index 7545300..ead35b0 100644 --- a/demo/app/background-service.ts +++ b/demo/app/background-service.ts @@ -5,11 +5,40 @@ import { device } from "tns-core-modules/platform"; import * as Toast from "nativescript-toast"; let watchId; -application.on(application.exitEvent, function (args: any) { + +function _clearWatch() { if (watchId) { geolocation.clearWatch(watchId); + watchId = null; } -}); +} + +function _startWatch() { + geolocation.enableLocationRequest().then(function () { + _clearWatch(); + watchId = geolocation.watchLocation( + function (loc) { + if (loc) { + let toast = Toast.makeText('Background Location: \n' + loc.latitude + ', ' + loc.longitude); + toast.show(); + console.log('Background Location: ' + loc.latitude + ' ' + loc.longitude); + } + }, + function (e) { + console.log("Background watchLocation error: " + (e.message || e)); + }, + { + desiredAccuracy: Accuracy.high, + updateDistance: 1.0, + updateTime: 3000, + minimumUpdateTime: 100 + }); + }, function (e) { + console.log("Background enableLocationRequest error: " + (e.message || e)); + }); +} + +application.on(application.exitEvent, _clearWatch); if (application.android) { if (device.sdkVersion < "26") { @@ -19,28 +48,7 @@ if (application.android) { return android.app.Service.START_STICKY; }, onCreate: function () { - let that = this; - geolocation.enableLocationRequest().then(function () { - that.id = geolocation.watchLocation( - function (loc) { - if (loc) { - let toast = Toast.makeText('Background Location: ' + loc.latitude + ' ' + loc.longitude); - toast.show(); - console.log('Background Location: ' + loc.latitude + ' ' + loc.longitude); - } - }, - function (e) { - console.log("Background watchLocation error: " + (e.message || e)); - }, - { - desiredAccuracy: Accuracy.high, - updateDistance: 0.1, - updateTime: 3000, - minimumUpdateTime: 100 - }); - }, function (e) { - console.log("Background enableLocationRequest error: " + (e.message || e)); - }); + _startWatch(); }, onBind: function (intent) { console.log("on Bind Services"); @@ -50,45 +58,22 @@ if (application.android) { }, onDestroy: function () { console.log('service onDestroy'); - geolocation.clearWatch(this.id); + _clearWatch(); } }); } else { (android.app).job.JobService.extend("com.nativescript.location.BackgroundService26", { - onStartJob(params) { - let executed = false; - geolocation.enableLocationRequest().then(function () { - watchId = geolocation.watchLocation( - function (loc) { - if (loc) { - let toast = Toast.makeText('Background Location: ' + loc.latitude + ' ' + loc.longitude); - toast.show(); - console.log('Background Location: ' + loc.latitude + ' ' + loc.longitude); - } - executed = true; - }, - function (e) { - console.log("Background watchLocation error: " + (e.message || e)); - executed = true; - }, - { - desiredAccuracy: Accuracy.high, - updateDistance: 0.1, - updateTime: 3000, - minimumUpdateTime: 100 - }); - }, function (e) { - console.log("Background enableLocationRequest error: " + (e.message || e)); - }); - - return executed; + onStartJob() { + console.log('service onStartJob'); + _startWatch(); + return true; }, - - onStopJob() { + onStopJob(jobParameters: any) { console.log('service onStopJob'); - geolocation.clearWatch(watchId); - return true; + this.jobFinished(jobParameters, false); + _clearWatch(); + return false; }, }); } diff --git a/demo/app/main-page.ts b/demo/app/main-page.ts index 2863cae..23554a7 100644 --- a/demo/app/main-page.ts +++ b/demo/app/main-page.ts @@ -7,23 +7,23 @@ const utils = require("tns-core-modules/utils/utils"); import * as application from "tns-core-modules/application"; import { device } from "tns-core-modules/platform"; -let locationService = require('./background-service'); - let page: Page; let model = new MainViewModel(); let watchIds = []; -let backgroundIds = []; +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. declare var com: any; -application.on(application.exitEvent, function (args: any) { - if (application.android && backgroundIds.length > 0) { +function _stopBackgroundJob() { + if (application.android) { let context = utils.ad.getApplicationContext(); const jobScheduler = context.getSystemService((android.content.Context).JOB_SCHEDULER_SERVICE); - const service = backgroundIds.pop(); - jobScheduler.cancel(service); - console.log(`Job Canceled: ${service}`); + if (jobScheduler.getPendingJob(jobId) !== null) { + jobScheduler.cancel(jobId); + console.log(`Job Canceled: ${jobId}`); + } } -}); +} +application.on(application.exitEvent, _stopBackgroundJob); export function pageLoaded(args: EventData) { page = args.object; @@ -33,16 +33,14 @@ export function pageLoaded(args: EventData) { export function startBackgroundTap() { if (application.android) { let context = utils.ad.getApplicationContext(); - let intent = new android.content.Intent(context, com.nativescript.location.BackgroundService.class); if (device.sdkVersion >= "26") { - const component = new android.content.ComponentName(context, com.nativescript.location.BackgroundService26.class); - const builder = new (android.app).job.JobInfo.Builder(1, component); - builder.setRequiredNetworkType((android.app).job.JobInfo.NETWORK_TYPE_ANY); - builder.setPeriodic(15 * 60 * 1000); const jobScheduler = context.getSystemService((android.content.Context).JOB_SCHEDULER_SERVICE); - const service = jobScheduler.schedule(builder.build()); - backgroundIds.push(service); + const component = new android.content.ComponentName(context, com.nativescript.location.BackgroundService26.class); + const builder = new (android.app).job.JobInfo.Builder(jobId, component); + builder.setOverrideDeadline(0); + return jobScheduler.schedule(builder.build()); } else { + let intent = new android.content.Intent(context, com.nativescript.location.BackgroundService.class); context.startService(intent); } } @@ -50,16 +48,11 @@ export function startBackgroundTap() { export function stopBackgroundTap() { if (application.android) { - let context = utils.ad.getApplicationContext(); - let intent = new android.content.Intent(context, com.nativescript.location.BackgroundService.class); if (device.sdkVersion >= "26") { - if (backgroundIds.length > 0) { - const jobScheduler = context.getSystemService((android.content.Context).JOB_SCHEDULER_SERVICE); - const service = backgroundIds.pop(); - jobScheduler.cancel(service); - console.log(`Job Canceled: ${service}`); - } + _stopBackgroundJob(); } else { + let context = utils.ad.getApplicationContext(); + let intent = new android.content.Intent(context, com.nativescript.location.BackgroundService.class); context.stopService(intent); } } @@ -79,18 +72,17 @@ export function enableLocationTap() { } export function buttonGetLocationTap() { - let location = geolocation.getCurrentLocation({ + geolocation.getCurrentLocation({ desiredAccuracy: Accuracy.high, maximumAge: 5000, timeout: 10000 - }) - .then(function (loc) { - if (loc) { - model.locations.push(loc); - } - }, function (e) { - console.log("Error: " + (e.message || e)); - }); + }).then(function (loc) { + if (loc) { + model.locations.push(loc); + } + }, function (e) { + console.log("Error: " + (e.message || e)); + }); } export function buttonStartTap() { @@ -106,7 +98,7 @@ export function buttonStartTap() { }, { desiredAccuracy: Accuracy.high, - updateDistance: 0.1, + updateDistance: 1, updateTime: 3000, minimumUpdateTime: 100 })); diff --git a/demo/app/package.json b/demo/app/package.json index 5b7c0da..5c5204e 100644 --- a/demo/app/package.json +++ b/demo/app/package.json @@ -2,33 +2,9 @@ "name": "tns-template-hello-world-ts", "main": "app.js", "version": "1.6.0", - "author": { - "name": "Telerik", - "email": "support@telerik.com" - }, - "description": "Nativescript hello-world-ts project template", - "license": "Apache-2.0", - "keywords": [ - "telerik", - "mobile", - "nativescript", - "{N}", - "tns", - "appbuilder", - "template" - ], - "repository": { - "type": "git", - "url": "git+ssh://git@github.com/NativeScript/template-hello-world-ts.git" - }, - "bugs": { - "url": "https://github.com/NativeScript/template-hello-world-ts/issues" - }, - "homepage": "https://github.com/NativeScript/template-hello-world-ts", "android": { "v8Flags": "--expose_gc", + "markingMode": "none", "requireModules": ["nativescript-geolocation"] - }, - "directories": {}, - "readme": "ERROR: No README data found!" + } } diff --git a/src/geolocation.android.ts b/src/geolocation.android.ts index 41cb5de..2da2278 100644 --- a/src/geolocation.android.ts +++ b/src/geolocation.android.ts @@ -275,12 +275,8 @@ function _isLocationServiceEnabled(options?: Options): Promise { locationSettingsBuilder.setAlwaysShow(true); let locationSettingsClient = com.google.android.gms.location.LocationServices.getSettingsClient(androidAppInstance.context); locationSettingsClient.checkLocationSettings(locationSettingsBuilder.build()) - .addOnSuccessListener(_getTaskSuccessListener((a) => { - resolve(); - })) - .addOnFailureListener(_getTaskFailListener((ex) => { - reject(ex); - })); + .addOnSuccessListener(_getTaskSuccessListener(resolve)) + .addOnFailureListener(_getTaskFailListener(reject)); }); } @@ -342,7 +338,7 @@ function androidLocationFromLocation(location: Location): android.location.Locat return androidLocation; } -// absctaction for unit testing +// abstraction for unit testing export class LocationManager { static getLastLocation(maximumAge, resolve, reject): Promise { _ensureLocationClient();