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