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

Commit de6d7f6

Browse files
author
Dimitar Tachev
authored
Merge pull request #116 from manojdcoder/master
Allowing access to location in Airplane mode with GPS on
2 parents 50a3fcd + decb8f8 commit de6d7f6

File tree

2 files changed

+41
-16
lines changed

2 files changed

+41
-16
lines changed

src/geolocation.android.ts

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,21 @@ androidAppInstance.on(AndroidApplication.activityResultEvent, function (args: an
3232
}
3333
});
3434

35+
function isAirplaneModeOn(): boolean {
36+
return android.provider.Settings.System.getInt(androidAppInstance.context.getContentResolver(),
37+
android.provider.Settings.System.AIRPLANE_MODE_ON) !== 0;
38+
}
39+
40+
function isProviderEnabled(provider: string): boolean {
41+
try {
42+
const locationManager: android.location.LocationManager = (<android.content.Context>androidAppInstance.context)
43+
.getSystemService(android.content.Context.LOCATION_SERVICE);
44+
return locationManager.isProviderEnabled(provider);
45+
} catch (ex) {
46+
return false;
47+
}
48+
}
49+
3550
export function getCurrentLocation(options: Options): Promise<Location> {
3651
return new Promise(function (resolve, reject) {
3752
enableLocationRequest().then(() => {
@@ -176,22 +191,26 @@ export function enableLocationRequest(always?: boolean): Promise<void> {
176191
_isLocationServiceEnabled().then(() => {
177192
resolve();
178193
}, (ex) => {
179-
if (typeof ex.getStatusCode === "function" &&
180-
ex.getStatusCode() === com.google.android.gms.common.api.CommonStatusCodes.RESOLUTION_REQUIRED) {
181-
182-
try {
183-
// cache resolve and reject callbacks in order to call them
184-
// on REQUEST_ENABLE_LOCATION Activity Result
185-
_onEnableLocationSuccess = resolve;
186-
_onEnableLocationFail = reject;
187-
ex.startResolutionForResult(androidAppInstance.foregroundActivity, REQUEST_ENABLE_LOCATION);
188-
} catch (sendEx) {
189-
// Ignore the error.
190-
resolve();
194+
if (typeof ex.getStatusCode === "function") {
195+
const statusCode = ex.getStatusCode();
196+
if (statusCode === com.google.android.gms.location.LocationSettingsStatusCodes.RESOLUTION_REQUIRED) {
197+
try {
198+
// cache resolve and reject callbacks in order to call them
199+
// on REQUEST_ENABLE_LOCATION Activity Result
200+
_onEnableLocationSuccess = resolve;
201+
_onEnableLocationFail = reject;
202+
return ex.startResolutionForResult(androidAppInstance.foregroundActivity, REQUEST_ENABLE_LOCATION);
203+
} catch (sendEx) {
204+
// Ignore the error.
205+
return resolve();
206+
}
207+
} else if (statusCode === com.google.android.gms.location.LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE
208+
&& isAirplaneModeOn()
209+
&& isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
210+
return resolve();
191211
}
192-
} else {
193-
reject(new Error("Cannot enable the location service"));
194212
}
213+
reject(new Error("Cannot enable the location service"));
195214
});
196215
}, reject);
197216
}, reject);
@@ -258,7 +277,13 @@ export function isEnabled(options?: Options): Promise<boolean> {
258277
_isLocationServiceEnabled(options).then(
259278
() => {
260279
resolve(true);
261-
}, () => {
280+
}, (ex) => {
281+
if (typeof ex.getStatusCode === "function"
282+
&& ex.getStatusCode() === com.google.android.gms.location.LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE
283+
&& isAirplaneModeOn()
284+
&& isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
285+
return resolve(true);
286+
}
262287
resolve(false);
263288
});
264289
}

src/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nativescript-geolocation",
3-
"version": "4.2.3",
3+
"version": "4.2.4",
44
"description": "Provides API for getting and monitoring location for NativeScript app.",
55
"main": "geolocation",
66
"nativescript": {

0 commit comments

Comments
 (0)