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

Allowing access to location in Airplane mode with GPS on #116

Merged
merged 4 commits into from
Mar 15, 2018
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
55 changes: 40 additions & 15 deletions src/geolocation.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@ androidAppInstance.on(AndroidApplication.activityResultEvent, function (args: an
}
});

function isAirplaneModeOn(): boolean {
return android.provider.Settings.System.getInt(androidAppInstance.context.getContentResolver(),
android.provider.Settings.System.AIRPLANE_MODE_ON) !== 0;
}

function isProviderEnabled(provider: string): boolean {
try {
const locationManager: android.location.LocationManager = (<android.content.Context>androidAppInstance.context)
.getSystemService(android.content.Context.LOCATION_SERVICE);
return locationManager.isProviderEnabled(provider);
} catch (ex) {
return false;
}
}

export function getCurrentLocation(options: Options): Promise<Location> {
return new Promise(function (resolve, reject) {
enableLocationRequest().then(() => {
Expand Down Expand Up @@ -176,22 +191,26 @@ export function enableLocationRequest(always?: boolean): Promise<void> {
_isLocationServiceEnabled().then(() => {
resolve();
}, (ex) => {
if (typeof ex.getStatusCode === "function" &&
ex.getStatusCode() === com.google.android.gms.common.api.CommonStatusCodes.RESOLUTION_REQUIRED) {

try {
// cache resolve and reject callbacks in order to call them
// on REQUEST_ENABLE_LOCATION Activity Result
_onEnableLocationSuccess = resolve;
_onEnableLocationFail = reject;
ex.startResolutionForResult(androidAppInstance.foregroundActivity, REQUEST_ENABLE_LOCATION);
} catch (sendEx) {
// Ignore the error.
resolve();
if (typeof ex.getStatusCode === "function") {
const statusCode = ex.getStatusCode();
if (statusCode === com.google.android.gms.location.LocationSettingsStatusCodes.RESOLUTION_REQUIRED) {
try {
// cache resolve and reject callbacks in order to call them
// on REQUEST_ENABLE_LOCATION Activity Result
_onEnableLocationSuccess = resolve;
_onEnableLocationFail = reject;
return ex.startResolutionForResult(androidAppInstance.foregroundActivity, REQUEST_ENABLE_LOCATION);
} catch (sendEx) {
// Ignore the error.
return resolve();
}
} else if (statusCode === com.google.android.gms.location.LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE
&& isAirplaneModeOn()
&& isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
return resolve();
}
} else {
reject(new Error("Cannot enable the location service"));
}
reject(new Error("Cannot enable the location service"));
});
}, reject);
}, reject);
Expand Down Expand Up @@ -258,7 +277,13 @@ export function isEnabled(options?: Options): Promise<boolean> {
_isLocationServiceEnabled(options).then(
() => {
resolve(true);
}, () => {
}, (ex) => {
if (typeof ex.getStatusCode === "function"
&& ex.getStatusCode() === com.google.android.gms.location.LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE
&& isAirplaneModeOn()
&& isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
return resolve(true);
}
resolve(false);
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nativescript-geolocation",
"version": "4.2.3",
"version": "4.2.4",
"description": "Provides API for getting and monitoring location for NativeScript app.",
"main": "geolocation",
"nativescript": {
Expand Down