Skip to content

Commit

Permalink
Merge pull request #4098 from dpalou/MOBILE-4470
Browse files Browse the repository at this point in the history
MOBILE-4470 location: Improve handling approximate location
  • Loading branch information
dpalou authored Jun 17, 2024
2 parents 68bd095 + 1e28512 commit 38a2470
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 46 deletions.
56 changes: 11 additions & 45 deletions cordova-plugin-moodleapp/src/ts/plugins/Diagnostic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,20 @@ export class Diagnostic {
permission.accessFineLocation,
permission.accessBackgroundLocation,
]).then(statuses => {
const coarseStatus = statuses[permission.accessCoarseLocation];
const fineStatus = statuses[permission.accessFineLocation];
const backgroundStatus = typeof statuses[permission.accessBackgroundLocation] !== 'undefined' ?
statuses[permission.accessBackgroundLocation] : true;

let status = this.combinePermissionStatuses({
[permission.accessCoarseLocation]: statuses[permission.accessCoarseLocation],
[permission.accessFineLocation]: statuses[permission.accessFineLocation],
});
if (status === permissionStatus.granted && backgroundStatus !== permissionStatus.granted) {
status = permissionStatus.grantedWhenInUse;
let status: string = permissionStatus.notRequested;

if (coarseStatus === permissionStatus.granted || fineStatus === permissionStatus.granted) {
status = backgroundStatus === permissionStatus.granted ?
permissionStatus.granted : permissionStatus.grantedWhenInUse;
} else if (coarseStatus === permissionStatus.deniedOnce || fineStatus === permissionStatus.deniedOnce) {
status = permissionStatus.deniedOnce;
} else if (coarseStatus === permissionStatus.deniedAlways || fineStatus === permissionStatus.deniedAlways) {
status = permissionStatus.deniedAlways;
}

resolve(status);
Expand Down Expand Up @@ -204,45 +209,6 @@ export class Diagnostic {
});
}

/**
* Given a list of permissions and their statuses, returns the combined status.
*
* @param statuses Permissions with statuses.
* @returns Combined status.
*/
protected combinePermissionStatuses(statuses: Record<string, string>): string {
if (this.anyStatusIs(statuses, permissionStatus.deniedAlways)) {
return permissionStatus.deniedAlways;
}

if (this.anyStatusIs(statuses, permissionStatus.deniedOnce)) {
return permissionStatus.deniedOnce;
}

if (this.anyStatusIs(statuses, permissionStatus.granted)) {
return permissionStatus.granted;
}

return permissionStatus.notRequested;
}

/**
* Check if any of the permissions in the statuses object has the specified status.
*
* @param statuses Permissions with status for each permission.
* @param status Status to check.
* @returns True if any permission has the specified status.
*/
protected anyStatusIs(statuses: Record<string, string>, status: string): boolean {
for (const permission in statuses) {
if (statuses[permission] === status) {
return true;
}
}

return false;
}

/**
* Android only. Requests app to be granted authorisation for a runtime permission.
*
Expand Down
2 changes: 1 addition & 1 deletion src/core/services/geolocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export class CoreGeolocationProvider {
case diagnostic.permissionStatus.granted:
case diagnostic.permissionStatus.grantedWhenInUse:
// Location is authorized.
return;
return;

case diagnostic.permissionStatus.deniedOnce:
if (failOnDeniedOnce) {
Expand Down

0 comments on commit 38a2470

Please sign in to comment.