From d8049cb8c6be500a0d9307c569928596ce4008c4 Mon Sep 17 00:00:00 2001 From: Manojkumar Date: Sat, 10 Mar 2018 03:46:53 +0530 Subject: [PATCH 1/3] Fixed: Unable to access location in Airplane mode with GPS on --- src/geolocation.android.ts | 57 ++++++++++++++++++++++++++++---------- src/package.json | 2 +- 2 files changed, 43 insertions(+), 16 deletions(-) diff --git a/src/geolocation.android.ts b/src/geolocation.android.ts index 7c5f082..20da7c8 100644 --- a/src/geolocation.android.ts +++ b/src/geolocation.android.ts @@ -4,8 +4,11 @@ import { setTimeout, clearTimeout } from "timer"; import { LocationBase, defaultGetLocationTimeout, fastestTimeUpdate, minTimeUpdate } from "./geolocation.common"; import { Options, successCallbackType, errorCallbackType } from "./location-monitor"; import * as permissions from "nativescript-permissions"; +import System = android.provider.Settings.System; +import Context = android.content.Context; declare var com: any; +const LocationSettingsStatusCodes = com.google.android.gms.location.LocationSettingsStatusCodes; let REQUEST_ENABLE_LOCATION = 4269; // random number let _onEnableLocationSuccess = null; let _onEnableLocationFail = null; @@ -32,6 +35,20 @@ androidAppInstance.on(AndroidApplication.activityResultEvent, function (args: an } }); +function isAirplaneModeOn(): boolean { + return System.getInt(androidAppInstance.context.getContentResolver(), System.AIRPLANE_MODE_ON) !== 0; +} + +function isProviderEnabled(provider: string): boolean { + try { + const locationManager: android.location.LocationManager = (androidAppInstance.context) + .getSystemService(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 +193,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 === 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 === 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 +279,13 @@ export function isEnabled(options?: Options): Promise { _isLocationServiceEnabled(options).then( () => { resolve(true); - }, () => { + }, (ex) => { + if (typeof ex.getStatusCode === "function" + && ex.getStatusCode() === 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..28abddc 100644 --- a/src/package.json +++ b/src/package.json @@ -1,6 +1,6 @@ { "name": "nativescript-geolocation", - "version": "4.2.3", + "version": "4.3.0", "description": "Provides API for getting and monitoring location for NativeScript app.", "main": "geolocation", "nativescript": { From 2be892e84e8783933528e124b229ba8ac46ef526 Mon Sep 17 00:00:00 2001 From: Manojkumar Date: Wed, 14 Mar 2018 21:02:14 +0530 Subject: [PATCH 2/3] Adjusted as per review comments --- src/geolocation.android.ts | 9 ++++----- src/package.json | 2 +- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/geolocation.android.ts b/src/geolocation.android.ts index 20da7c8..48571d4 100644 --- a/src/geolocation.android.ts +++ b/src/geolocation.android.ts @@ -4,8 +4,6 @@ import { setTimeout, clearTimeout } from "timer"; import { LocationBase, defaultGetLocationTimeout, fastestTimeUpdate, minTimeUpdate } from "./geolocation.common"; import { Options, successCallbackType, errorCallbackType } from "./location-monitor"; import * as permissions from "nativescript-permissions"; -import System = android.provider.Settings.System; -import Context = android.content.Context; declare var com: any; const LocationSettingsStatusCodes = com.google.android.gms.location.LocationSettingsStatusCodes; @@ -36,13 +34,14 @@ androidAppInstance.on(AndroidApplication.activityResultEvent, function (args: an }); function isAirplaneModeOn(): boolean { - return System.getInt(androidAppInstance.context.getContentResolver(), System.AIRPLANE_MODE_ON) !== 0; + 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(Context.LOCATION_SERVICE); + const locationManager: android.location.LocationManager = (androidAppInstance.context) + .getSystemService(android.content.Context.LOCATION_SERVICE); return locationManager.isProviderEnabled(provider); } catch (ex) { return false; diff --git a/src/package.json b/src/package.json index 28abddc..f387cb4 100644 --- a/src/package.json +++ b/src/package.json @@ -1,6 +1,6 @@ { "name": "nativescript-geolocation", - "version": "4.3.0", + "version": "4.2.4", "description": "Provides API for getting and monitoring location for NativeScript app.", "main": "geolocation", "nativescript": { From 8ae911f0f6e4102b21991a168194b1c43247279e Mon Sep 17 00:00:00 2001 From: Manojkumar Date: Wed, 14 Mar 2018 21:12:21 +0530 Subject: [PATCH 3/3] Adjusted status code usage for snapshot build --- src/geolocation.android.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/geolocation.android.ts b/src/geolocation.android.ts index 48571d4..bd85406 100644 --- a/src/geolocation.android.ts +++ b/src/geolocation.android.ts @@ -6,7 +6,6 @@ import { Options, successCallbackType, errorCallbackType } from "./location-moni import * as permissions from "nativescript-permissions"; declare var com: any; -const LocationSettingsStatusCodes = com.google.android.gms.location.LocationSettingsStatusCodes; let REQUEST_ENABLE_LOCATION = 4269; // random number let _onEnableLocationSuccess = null; let _onEnableLocationFail = null; @@ -194,7 +193,7 @@ export function enableLocationRequest(always?: boolean): Promise { }, (ex) => { if (typeof ex.getStatusCode === "function") { const statusCode = ex.getStatusCode(); - if (statusCode === LocationSettingsStatusCodes.RESOLUTION_REQUIRED) { + 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 @@ -205,7 +204,7 @@ export function enableLocationRequest(always?: boolean): Promise { // Ignore the error. return resolve(); } - } else if (statusCode === LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE + } else if (statusCode === com.google.android.gms.location.LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE && isAirplaneModeOn() && isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) { return resolve(); @@ -280,7 +279,7 @@ export function isEnabled(options?: Options): Promise { resolve(true); }, (ex) => { if (typeof ex.getStatusCode === "function" - && ex.getStatusCode() === LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE + && ex.getStatusCode() === com.google.android.gms.location.LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE && isAirplaneModeOn() && isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) { return resolve(true);