1/* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import { BasePermissionStrategy } from '../base/BasePermissionStrategy'; 17import { Permission, PermissionGroup, ButtonStatus, PermissionOption } from '../model/definition'; 18import { CallerAppInfo, PermissionGroupConfig } from '../model/typedef'; 19import Constants from '../utils/constant'; 20 21const fuzzyMarks = [Constants.LOCATION_FUZZY, Constants.LOCATION_BOTH_FUZZY, Constants.LOCATION_BOTH_PRECISE]; 22const preciseMarks = [Constants.LOCATION_UPGRADE, Constants.LOCATION_BOTH_PRECISE]; 23const groupConfig: PermissionGroupConfig = new PermissionGroupConfig({ 24 groupName: PermissionGroup.LOCATION, 25 permissions: [Permission.LOCATION_IN_BACKGROUND, Permission.APPROXIMATELY_LOCATION, Permission.LOCATION], 26 icon: $r('app.media.ic_public_gps'), 27 title: '', 28 reason: '', 29 buttonList: [] 30}) 31 32export class LocationStrategy extends BasePermissionStrategy { 33 public override getPermissionGroupConfig(): PermissionGroupConfig { 34 return groupConfig; 35 } 36 37 public override getGroupTitle(appName: string, locationFlag: number): ResourceStr { 38 if (locationFlag == Constants.LOCATION_FUZZY) { 39 return $r('app.string.access_general_location', appName); 40 } 41 if (locationFlag == Constants.LOCATION_UPGRADE) { 42 return $r('app.string.fuzzy_to_exact', appName); 43 } 44 return $r('app.string.group_label_location', appName); 45 } 46 47 public override getButtonList(): ButtonStatus[] { 48 return [ButtonStatus.ALLOW_ONLY_DURING_USE, ButtonStatus.ALLOW_THIS_TIME, ButtonStatus.CANCEL]; 49 } 50 51 public override grantHandle( 52 permission: Permission, callerAppInfo: CallerAppInfo, locationFlag: number, buttonStatus: ButtonStatus 53 ): PermissionOption { 54 let opt: PermissionOption = PermissionOption.SKIP; 55 if (permission === Permission.APPROXIMATELY_LOCATION) { 56 opt = this.handleFuzzyLocationGrant(callerAppInfo, locationFlag, buttonStatus); 57 } else if (permission === Permission.LOCATION) { 58 opt = this.handlePreciseLocationGrant(callerAppInfo, locationFlag, buttonStatus); 59 } else { 60 opt = PermissionOption.SKIP; 61 } 62 return opt; 63 } 64 65 public override denyHandle( 66 permission: Permission, callerAppInfo: CallerAppInfo, locationFlag: number, buttonStatus: ButtonStatus 67 ): PermissionOption { 68 let opt: PermissionOption = PermissionOption.REVOKE; 69 if ((locationFlag === Constants.LOCATION_UPGRADE) && (permission !== Permission.LOCATION)) { 70 opt = PermissionOption.SKIP; 71 } 72 return opt; 73 } 74 75 private handleFuzzyLocationGrant( 76 callerAppInfo: CallerAppInfo, locationFlag: number, buttonStatus: ButtonStatus 77 ): PermissionOption { 78 if (fuzzyMarks.includes(locationFlag)) { 79 return PermissionOption.GRANT; 80 } else { 81 return PermissionOption.SKIP; 82 } 83 } 84 85 private handlePreciseLocationGrant( 86 callerAppInfo: CallerAppInfo, locationFlag: number, buttonStatus: ButtonStatus 87 ): PermissionOption { 88 if (preciseMarks.includes(locationFlag)) { 89 return PermissionOption.GRANT; 90 } else { 91 return PermissionOption.SKIP; 92 } 93 } 94 95}