1# Accessing the Security & Privacy Framework 2 3The Security & Privacy framework allows access via a UIAbility or an ExtensionAbility. 4 5In the Security & Privacy Center, the accessed applications are displayed by **bundleName** in alphabetical order. 6 7The corresponding **action** and **metadata** fields need to be configured in the **module.json5** file of the application module for the Security & Privacy Center menu access framework to scan and parse. 8 9This topic describes how to implement the access to the Security & Privacy framework. 10 11## Adding a Metadata Configuration File 12 13Add a metadata configuration file (.json) containing the following parameters for the service. 14 15| Key (Attribute Name)| Value | Mandatory| Description | 16| ----------------- | ------------------------------------------------------------ | -------- | ------------------------------------------------- | 17| displayedMode | - **list**: indicates the list view.<br>- **card**: indicates the widget view, which is not supported currently.| Yes | Currently, only the list view is available no matter whether you set this parameter to **list** or **card**.| 18| mainTitleResource | Name of the main title string displayed on the entry menu. | Yes | - | 19| dstAbilityMode | - **0**: A UIAbility is to be started.<br>- **1**: A UIExtensionAbility is to be started. | Yes | - | 20| dstAbilityName | Name of the target ability to start.<br>- If **dstAbilityMode** is **0**, a UIAbility will be started.<br>- if **dstAbilityMode** is **1**, the ability inherits from a UIExtensionAbility and the caller loads its own page in this ability. | Yes | - | 21| dstBundleName | Bundle name of the application. | Yes | - | 22| displayUserConfig | - **ONLY_PRIMARY_USER**: display the content only to the primary user.<br>- **ONLY_SUB_USER**: display the content only to sub-users.| No | If this parameter is left blank, the content is displayed to all users. | 23 24For example, create a **security_privacy.json** file in **resource/rawfile/** directory of the module. You can customize the file name. 25 26The following shows the configuration of the **security_privacy.json** file. 27 28```json 29{ 30 "displayedMode": "list", 31 "mainTitleResource": "$string:main_title", 32 "dstAbilityMode": 0, 33 "dstAbilityName": "EntryAbility", 34 "dstBundleName": "com.example.test" 35} 36``` 37 38## Modifying the Application Configuration File 39 40The **module.json5** file of each module contains the configuration of the UIAbility and ExtensionAbility components of the module and the permissions required for application running. To access the Security & Privacy framework, you need to configure the following fields in **module.json5**. 41 42### Setting actions 43 44In the **module.json5** file, set the **actions** field under **skills** to **action.access.privacy.center**. 45 46```typescript 47"skills": [ 48 { 49 "actions": [ 50 "action.access.privacy.center" 51 ] 52 } 53] 54``` 55 56### Setting Metadata 57 58In the **module.json5** file, add an entry with **name** and **value** under **metadata**, and set **name** to **metadata.access.privacy.center** and **value** to the metadata configuration file name. 59 60The metadata configuration file is the **resource/rawfile/*xxx*.json** file (**security_privacy.json** in this example) added above. 61 62> **NOTE** 63> 64> Set **actions** and **metadata** based on the **dstAbilityMode** value set in the metadata configuration file. 65> 66> - If **dstAbilityMode** is **0** (UIAbility), configure **metadata** under **abilities**. 67> 68> - If **dstAbilityMode** is **1** (UIExtensionAbility), configure **metadata** under **extensionAbilities**. When configuring **extensionAbilities**, set **type** to **sys/commonUI**. 69> 70 71The following example provides only the settings for accessing the Security & Privacy framework. You need to set the other fields in the **module.json5** file to match your case. 72 73```typescript 74// Set this field if dstAbilityMode is 0 (UIAbility). 75"abilities": [ 76 { 77 "skills": [ 78 { 79 "actions": [ 80 "action.access.privacy.center" 81 ] 82 } 83 ], 84 "metadata": [ 85 { 86 "name": 'metadata.access.privacy.center', 87 "value": 'security_privacy.json' 88 } 89 ] 90 } 91] 92``` 93 94```typescript 95// Set this field if dstAbilityMode is 1 (UIExtensionAbility). 96"extensionAbilities": [ 97 { 98 "type": "sys/commonUI", 99 "skills": [ 100 { 101 "actions": [ 102 "action.access.privacy.center" 103 ] 104 } 105 ], 106 "metadata": [ 107 { 108 "name": 'metadata.access.privacy.center', 109 "value": 'security_privacy.json' 110 } 111 ] 112 } 113 ] 114``` 115 116### Requesting Permissions 117 118The caller must have the [ohos.permission.ACCESS_SECURITY_PRIVACY_CENTER](../AccessToken/permissions-for-system-apps.md#ohospermissionaccess_security_privacy_center) permission. 119 120For details about how to request the permission, see [Workflow for Requesting Permissions](../AccessToken/determine-application-mode.md). 121 122## Accessing the Security & Privacy Framework via a UIAbility 123 124To access the Security & Privacy framework via a UIAbility, use the default access mode generated. 125 126Example: 127 128```typescript 129import AbilityConstant from '@ohos.app.ability.AbilityConstant'; 130import hilog from '@ohos.hilog'; 131import UIAbility from '@ohos.app.ability.UIAbility'; 132import Want from '@ohos.app.ability.Want'; 133import window from '@ohos.window'; 134 135export default class EntryAbility extends UIAbility { 136 onWindowStageCreate(windowStage: window.WindowStage): void { 137 // Main window is created, set main page for this ability 138 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); 139 windowStage.loadContent('pages/Index', (err, data) => { 140 if (err.code) { 141 hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); 142 return; 143 } 144 hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? ''); 145 }); 146 } 147} 148``` 149 150## Accessing the Security & Privacy Framework via an ExtensionAbility 151 152To access the Security & Privacy Framework via an ExtensionAbility, the Ability page needs to inherit from the ExtensionAbility. You need to modify the configuration and create a session to start the main page of the application. 153 154Example: 155 156```typescript 157import hilog from '@ohos.hilog'; 158import Want from '@ohos.app.ability.Want'; 159import ExtensionAbility from '@ohos.app.ability.ExtensionAbility'; 160import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession'; 161 162export default class EntryAbility extends ExtensionAbility { 163 onSessionCreate(want: Want, session: UIExtensionContentSession) { 164 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onSessionCreate'); 165 let param: Record<string, Object> = { 166 'session': session 167 } 168 let storage: LocalStorage = new LocalStorage(param) 169 session.loadContent('pages/Index', storage) 170 } 171} 172``` 173 174## Exiting the Security & Privacy Framework (UIAbility) 175 176If the caller (which accessed the Security & Privacy Framework via a UIAbility) needs to exit actively, for example, there is a return button on the page, call **router.back()** or **terminateSelf()** to destroy the page. 177 178Example: 179 180```typescript 181import router from '@ohos.router'; 182 183@Entry() 184@Component 185struct Index { 186 187 build() { 188 Row() { 189 Column() { 190 Button("click to back") 191 .onClick(() => { 192 router.back() 193 }) 194 } 195 .width('100%') 196 } 197 .height('100%') 198 } 199} 200``` 201 202## Exiting the Security & Privacy Framework (ExtensionAbility) 203 204If the caller (which accessed the Security & Privacy Framework via a UIExtensionAbility) needs to exit actively, for example, there is a return button on the page, call **sendData** of UIExtensionContentSession to send {"action": "exit"}. Upon receiving the data, the receiver exits the page. 205 206Example: 207 208```typescript 209import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession' 210 211let storage = LocalStorage.getShared() 212 213@Entry(storage) 214@Component 215struct Index { 216 private session: UIExtensionContentSession = storage.get<UIExtensionContentSession>('session') as UIExtensionContentSession 217 build() { 218 Row() { 219 Column() { 220 Button("click to back") 221 .onClick(() => { 222 this.session.sendData({ 'action': "exit" }) 223 }) 224 } 225 .width('100%') 226 } 227 .height('100%') 228 } 229} 230``` 231