Merge pull request #3561 from dpalou/MOBILE-4239

MOBILE-4239 geolocation: Add some logging in geolocation service
main
Pau Ferrer Ocaña 2023-02-23 12:21:00 +01:00 committed by GitHub
commit 15a9c74b28
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 0 deletions

View File

@ -22,10 +22,17 @@ import { CoreUtils } from './utils/utils';
import { CorePlatform } from './platform';
import { CoreSilentError } from '@classes/errors/silenterror';
import { CoreSubscriptions } from '@singletons/subscriptions';
import { CoreLogger } from '@singletons/logger';
@Injectable({ providedIn: 'root' })
export class CoreGeolocationProvider {
protected logger: CoreLogger;
constructor() {
this.logger = CoreLogger.getInstance('CoreGeolocationProvider');
}
/**
* Get current user coordinates.
*
@ -34,16 +41,21 @@ export class CoreGeolocationProvider {
*/
async getCoordinates(): Promise<Coordinates> {
try {
this.logger.log('Getting coordinates.');
await this.authorizeLocation();
await this.enableLocation();
this.logger.log('Getting coordinates: authorized and enabled.');
const result = await Geolocation.getCurrentPosition({
enableHighAccuracy: true,
timeout: 30000,
});
this.logger.log('Coordinates retrieved');
return result.coords;
} catch (error) {
this.logger.log('Error getting coordinates.', error);
if (this.isCordovaPermissionDeniedError(error)) {
throw new CoreGeolocationError(CoreGeolocationErrorReason.PERMISSION_DENIED);
}
@ -94,6 +106,8 @@ export class CoreGeolocationProvider {
*/
protected async doAuthorizeLocation(failOnDeniedOnce: boolean = false): Promise<void> {
const authorizationStatus = await Diagnostic.getLocationAuthorizationStatus();
this.logger.log(`Authorize location: status ${authorizationStatus}`);
switch (authorizationStatus) {
case Diagnostic.permissionStatus.DENIED_ONCE:
if (failOnDeniedOnce) {
@ -101,7 +115,9 @@ export class CoreGeolocationProvider {
}
// Fall through.
case Diagnostic.permissionStatus.NOT_REQUESTED:
this.logger.log('Request location authorization.');
await this.requestLocationAuthorization();
this.logger.log('Location authorization granted.');
await CoreApp.waitForResume(500);
await this.doAuthorizeLocation(true);