2020-10-07 08:53:19 +00:00
|
|
|
// (C) Copyright 2015 Moodle Pty Ltd.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import { Coordinates } from '@ionic-native/geolocation';
|
|
|
|
|
|
|
|
import { CoreApp } from '@services/app';
|
2020-10-14 06:27:02 +00:00
|
|
|
import { CoreError } from '@classes/errors/error';
|
2020-11-24 08:31:11 +00:00
|
|
|
import { Geolocation, Diagnostic, makeSingleton } from '@singletons';
|
2020-10-07 08:53:19 +00:00
|
|
|
|
2020-11-19 15:35:17 +00:00
|
|
|
@Injectable({ providedIn: 'root' })
|
2020-10-07 08:53:19 +00:00
|
|
|
export class CoreGeolocationProvider {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get current user coordinates.
|
|
|
|
*
|
|
|
|
* @throws {CoreGeolocationError}
|
|
|
|
*/
|
|
|
|
async getCoordinates(): Promise<Coordinates> {
|
|
|
|
try {
|
|
|
|
await this.authorizeLocation();
|
|
|
|
await this.enableLocation();
|
|
|
|
|
2021-03-02 10:41:04 +00:00
|
|
|
const result = await Geolocation.getCurrentPosition({
|
2020-10-07 08:53:19 +00:00
|
|
|
enableHighAccuracy: true,
|
|
|
|
timeout: 30000,
|
|
|
|
});
|
|
|
|
|
|
|
|
return result.coords;
|
|
|
|
} catch (error) {
|
|
|
|
if (this.isCordovaPermissionDeniedError(error)) {
|
|
|
|
throw new CoreGeolocationError(CoreGeolocationErrorReason.PermissionDenied);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make sure that using device location has been authorized and ask for permission if it hasn't.
|
|
|
|
*
|
|
|
|
* @throws {CoreGeolocationError}
|
|
|
|
*/
|
|
|
|
async authorizeLocation(): Promise<void> {
|
|
|
|
await this.doAuthorizeLocation();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make sure that location is enabled and open settings to enable it if necessary.
|
|
|
|
*
|
|
|
|
* @throws {CoreGeolocationError}
|
|
|
|
*/
|
|
|
|
async enableLocation(): Promise<void> {
|
2021-03-02 10:41:04 +00:00
|
|
|
let locationEnabled = await Diagnostic.isLocationEnabled();
|
2020-10-07 08:53:19 +00:00
|
|
|
|
|
|
|
if (locationEnabled) {
|
|
|
|
// Location is enabled.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-02 10:41:04 +00:00
|
|
|
if (!CoreApp.isIOS()) {
|
|
|
|
Diagnostic.switchToLocationSettings();
|
|
|
|
await CoreApp.waitForResume(30000);
|
2020-10-07 08:53:19 +00:00
|
|
|
|
2021-03-02 10:41:04 +00:00
|
|
|
locationEnabled = await Diagnostic.isLocationEnabled();
|
2020-10-07 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!locationEnabled) {
|
|
|
|
throw new CoreGeolocationError(CoreGeolocationErrorReason.LocationNotEnabled);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Recursive implementation of authorizeLocation method, protected to avoid exposing the failOnDeniedOnce parameter.
|
|
|
|
*
|
|
|
|
* @param failOnDeniedOnce Throw an exception if the permission has been denied once.
|
|
|
|
* @throws {CoreGeolocationError}
|
|
|
|
*/
|
|
|
|
protected async doAuthorizeLocation(failOnDeniedOnce: boolean = false): Promise<void> {
|
2021-03-02 10:41:04 +00:00
|
|
|
const authorizationStatus = await Diagnostic.getLocationAuthorizationStatus();
|
2020-10-07 08:53:19 +00:00
|
|
|
|
|
|
|
switch (authorizationStatus) {
|
2021-03-02 10:41:04 +00:00
|
|
|
case Diagnostic.permissionStatus.DENIED_ONCE:
|
2020-10-07 08:53:19 +00:00
|
|
|
if (failOnDeniedOnce) {
|
|
|
|
throw new CoreGeolocationError(CoreGeolocationErrorReason.PermissionDenied);
|
|
|
|
}
|
|
|
|
// Fall through.
|
2021-03-02 10:41:04 +00:00
|
|
|
case Diagnostic.permissionStatus.NOT_REQUESTED:
|
|
|
|
await Diagnostic.requestLocationAuthorization();
|
|
|
|
await CoreApp.waitForResume(500);
|
2020-10-07 08:53:19 +00:00
|
|
|
await this.doAuthorizeLocation(true);
|
|
|
|
|
|
|
|
return;
|
2021-03-02 10:41:04 +00:00
|
|
|
case Diagnostic.permissionStatus.GRANTED:
|
|
|
|
case Diagnostic.permissionStatus.GRANTED_WHEN_IN_USE:
|
2020-10-07 08:53:19 +00:00
|
|
|
// Location is authorized.
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
throw new CoreGeolocationError(CoreGeolocationErrorReason.PermissionDenied);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether an error was caused by a PERMISSION_DENIED from the cordova plugin.
|
|
|
|
*
|
|
|
|
* @param error Error.
|
|
|
|
*/
|
2020-10-20 12:20:51 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2020-10-07 08:53:19 +00:00
|
|
|
protected isCordovaPermissionDeniedError(error?: any): boolean {
|
|
|
|
return error && 'code' in error && 'PERMISSION_DENIED' in error && error.code === error.PERMISSION_DENIED;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-03-02 10:41:04 +00:00
|
|
|
export const CoreGeolocation = makeSingleton(CoreGeolocationProvider);
|
2020-10-07 08:53:19 +00:00
|
|
|
|
|
|
|
export enum CoreGeolocationErrorReason {
|
|
|
|
PermissionDenied = 'permission-denied',
|
|
|
|
LocationNotEnabled = 'location-not-enabled',
|
|
|
|
}
|
|
|
|
|
|
|
|
export class CoreGeolocationError extends CoreError {
|
|
|
|
|
2020-10-08 12:04:38 +00:00
|
|
|
reason: CoreGeolocationErrorReason;
|
2020-10-07 08:53:19 +00:00
|
|
|
|
|
|
|
constructor(reason: CoreGeolocationErrorReason) {
|
|
|
|
super(`GeolocationError: ${reason}`);
|
|
|
|
|
|
|
|
this.reason = reason;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|