Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 41 additions & 56 deletions demo/app/background-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand All @@ -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");
Expand All @@ -50,45 +58,22 @@ if (application.android) {
},
onDestroy: function () {
console.log('service onDestroy');
geolocation.clearWatch(this.id);
_clearWatch();
}
});
}
else {
(<any>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;
},
});
}
Expand Down
60 changes: 26 additions & 34 deletions demo/app/main-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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((<any>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 = <Page>args.object;
Expand All @@ -33,33 +33,26 @@ 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 (<any>android.app).job.JobInfo.Builder(1, component);
builder.setRequiredNetworkType((<any>android.app).job.JobInfo.NETWORK_TYPE_ANY);
builder.setPeriodic(15 * 60 * 1000);
const jobScheduler = context.getSystemService((<any>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 (<any>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);
}
}
}

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((<any>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);
}
}
Expand All @@ -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() {
Expand All @@ -106,7 +98,7 @@ export function buttonStartTap() {
},
{
desiredAccuracy: Accuracy.high,
updateDistance: 0.1,
updateDistance: 1,
updateTime: 3000,
minimumUpdateTime: 100
}));
Expand Down
28 changes: 2 additions & 26 deletions demo/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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!"
}
}
10 changes: 3 additions & 7 deletions src/geolocation.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,12 +275,8 @@ function _isLocationServiceEnabled(options?: Options): Promise<boolean> {
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));
});
}

Expand Down Expand Up @@ -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<Location> {
_ensureLocationClient();
Expand Down