diff --git a/src/geolocation.android.ts b/src/geolocation.android.ts index 7c5f082..bd85406 100644 --- a/src/geolocation.android.ts +++ b/src/geolocation.android.ts @@ -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 = (androidAppInstance.context) + .getSystemService(android.content.Context.LOCATION_SERVICE); + return locationManager.isProviderEnabled(provider); + } catch (ex) { + return false; + } +} + export function getCurrentLocation(options: Options): Promise { return new Promise(function (resolve, reject) { enableLocationRequest().then(() => { @@ -176,22 +191,26 @@ export function enableLocationRequest(always?: boolean): Promise { _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); @@ -258,7 +277,13 @@ export function isEnabled(options?: Options): Promise { _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); }); } diff --git a/src/package.json b/src/package.json index e2310bc..f387cb4 100644 --- a/src/package.json +++ b/src/package.json @@ -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": {