@@ -32,6 +32,21 @@ androidAppInstance.on(AndroidApplication.activityResultEvent, function (args: an
32
32
}
33
33
} ) ;
34
34
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
+
35
50
export function getCurrentLocation ( options : Options ) : Promise < Location > {
36
51
return new Promise ( function ( resolve , reject ) {
37
52
enableLocationRequest ( ) . then ( ( ) => {
@@ -176,22 +191,26 @@ export function enableLocationRequest(always?: boolean): Promise<void> {
176
191
_isLocationServiceEnabled ( ) . then ( ( ) => {
177
192
resolve ( ) ;
178
193
} , ( 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 ( ) ;
191
211
}
192
- } else {
193
- reject ( new Error ( "Cannot enable the location service" ) ) ;
194
212
}
213
+ reject ( new Error ( "Cannot enable the location service" ) ) ;
195
214
} ) ;
196
215
} , reject ) ;
197
216
} , reject ) ;
@@ -258,7 +277,13 @@ export function isEnabled(options?: Options): Promise<boolean> {
258
277
_isLocationServiceEnabled ( options ) . then (
259
278
( ) => {
260
279
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
+ }
262
287
resolve ( false ) ;
263
288
} ) ;
264
289
}
0 commit comments