@@ -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+
3550export 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 }
0 commit comments