1# Updating Widget Content Through a Proxy 2 3A widget can be updated through a proxy – a system application that has data sharing enabled – when the widget provider is not running. 4 5## Implementation Principles 6 7**Figure 1** Updating widget content through a proxy 8 9 10Compared with the [implementation of the ArkTS widget](../application-models/arkts-ui-widget-working-principles.md#implementation-principles) alone, updating through a proxy involves the data management service and data provider. 11 12- Data management service: provides a mechanism for data sharing among multiple applications. 13- Data provider: must be a system application that has data sharing enabled. The shared data is identified through the defined **key** + **subscriberId** combination. 14 15> **NOTE** 16> 17> This feature can be used when the system provides applications as data providers and publicly available shared data identifiers. 18 19Processing flow of the widget provider (indicated by the blue arrows in the figure): 20 211. The widget provider sets the **dataProxyEnabled** field to **true** in the **form_config.json** file to enable the update-through-proxy feature. 22> **NOTE** 23> 24> After the update-through-proxy feature is enabled, the settings for [updating periodically](../application-models/arkts-ui-widget-update-by-time.md) do not work. 25 262. In the [onAddForm](../reference/apis/js-apis-app-form-formExtensionAbility.md#onaddform) callback, the widget provider returns the **key** + **subscriberId** combination defined by the data provider to the Widget Manager. 27 283. The Widget Manager parses the subscription information of the widget provider and registers a subscription instance with the data management service. 29 30Processing flow of the widget update proxy (indicated by the red arrows in the figure): 31 321. The data provider uses the **key** + **subscriberId** combination as the data ID to store data to the database. 332. The data management service detects the change in the database and publishes the new data to all currently registered subscription instances. 343. The Widget Manager parses data from the subscription instance and sends the data to the widget rendering service. 354. The widget rendering service runs the widget page code **widgets.abc**, which implements rendering based on the new data and sends the rendered data to the [FormComponent](../reference/arkui-ts/ts-basic-components-formcomponent.md) corresponding to the widget host. 36 37There are two types of shared data provided by the data provider: 38 39- Ephemeral data: data that exists only for a specific period of time and can be subscribed to by system and non-system applications alike. 40 41- Persistent data: data that persists over time and can only be subscribed to by system applications. 42 43The update-through-proxy configuration varies by the type of shared data. 44 45 46## Widget Provider Development (Ephemeral Data) 47 48- Set the **dataProxyEnabled** field to **true** in the **form_config.json** file to enable the update-through-proxy feature. 49 ```json 50 { 51 "forms": [ 52 { 53 "name": "widget", 54 "description": "This is a service widget.", 55 "src": "./ets/widget/pages/WidgetCard.ets", 56 "uiSyntax": "arkts", 57 "window": { 58 "designWidth": 720, 59 "autoDesignWidth": true 60 }, 61 "colorMode": "auto", 62 "isDefault": true, 63 "updateEnabled": true, 64 "scheduledUpdateTime": "10:30", 65 "defaultDimension": "2*2", 66 "supportDimensions": ["2*2"], 67 "dataProxyEnabled": true // Enable the update-through-proxy feature. 68 } 69 ] 70 } 71 ``` 72 73- Configure the subscription information [proxyData](../reference/apis/js-apis-app-form-formBindingData.md#proxydata10) in the [onAddForm](../reference/apis/js-apis-app-form-formExtensionAbility.md#onaddform) callback and return the information to the Widget Manager through [formBinding](../reference/apis/js-apis-app-form-formBindingData.md#formbindingdata). In this example, **key** is set to **detail** and **subscriberId** is set to **11**. 74 > **NOTE** 75 > 76 > The value of **key** can be a URI or a simple string. The default value of **subscriberId** is the value of **formId**. The actual value depends on the definition of the data provider. 77 ```ts 78 import formBindingData from '@ohos.app.form.formBindingData'; 79 import Want from '@ohos.app.ability.Want'; 80 import FormExtensionAbility from '@ohos.app.form.FormExtensionAbility'; 81 82 export default class EntryFormAbility extends FormExtensionAbility { 83 onAddForm(want: Want) { 84 let formData: Record<string, Object> = {}; 85 let proxies: formBindingData.ProxyData[] = [ 86 { 87 "key": "detail", 88 "subscriberId": "11" 89 } 90 ] 91 let formBinding = formBindingData.createFormBindingData(formData); 92 formBinding["proxies"] = proxies; 93 return formBinding; 94 } 95 } 96 ``` 97 98- In the [widget page code file](arkts-ui-widget-creation.md), use the variable in LocalStorage to obtain the subscribed data. The variable in LocalStorage is bound to a string and updates the subscribed data in the key:value pair format. The key must be the same as that subscribed to by the widget provider. In this example, the subscribed data is obtained through **'detail'** and displayed in the **\<Text>** component. 99 ```ts 100 let storage = new LocalStorage(); 101 @Entry(storage) 102 @Component 103 struct Index { 104 @LocalStorageProp('detail') detail: string = 'Loading...'; 105 106 build() { 107 Row() { 108 Column() { 109 Text(this.detail) 110 .fontSize('12vp') 111 .textAlign(TextAlign.Center) 112 .width('100%') 113 .height('15%') 114 } 115 .width('100%') 116 } 117 .height('100%') 118 } 119 } 120 ``` 121 122## Widget Provider Development (Persistent Data; for System Applications Only) 123- Set the **dataProxyEnabled** field to **true** in the **form_config.json** file to enable the update-through-proxy feature. 124 ```json 125 { 126 "forms": [ 127 { 128 "name": "widget", 129 "description": "This is a service widget.", 130 "src": "./ets/widget/pages/WidgetCard.ets", 131 "uiSyntax": "arkts", 132 "window": { 133 "designWidth": 720, 134 "autoDesignWidth": true 135 }, 136 "colorMode": "auto", 137 "isDefault": true, 138 "updateEnabled": true, 139 "scheduledUpdateTime": "10:30", 140 "defaultDimension": "2*2", 141 "supportDimensions": ["2*2"], 142 "dataProxyEnabled": true // Enable the update-through-proxy feature. 143 } 144 ] 145 } 146 ``` 147 148- Add a subscription template ([addTemplate](../reference/apis/js-apis-data-dataShare.md#addtemplate10)) to the [onAddForm](../reference/apis/js-apis-app-form-formExtensionAbility.md#onaddform) callback and use the template predicates to notify the database of the subscribed data conditions. Then, configure the subscription information [proxyData](../reference/apis/js-apis-app-form-formBindingData.md#proxydata10) and return it to the Widget Manager through [formBinding](../reference/apis/js-apis-app-form-formBindingData.md#formbindingdata). In the example, the predicate is set to **"list": "select type from TBL00 limit 0,1"**, indicating that the first data record in the **type** column is obtained from the **TBL00** database. The data is returned to the widget page code file **widgets.abc** in {"list":[{"type":"value0"}]} format. 149 150 > **NOTE** 151 > 152 > - The value of **key** is a URI, which depends on the definition of the data release party. 153 > - The value of **subscriberId** can be customized. Ensure that the value of **subscriberId** in **addTemplate** is the same as that of **proxies.subscriberId**. 154 ```ts 155 import formBindingData from '@ohos.app.form.formBindingData'; 156 import FormExtensionAbility from '@ohos.app.form.FormExtensionAbility'; 157 import dataShare from '@ohos.data.dataShare'; 158 import Want from '@ohos.app.ability.Want'; 159 160 let dataShareHelper: dataShare.DataShareHelper; 161 export default class EntryFormAbility extends FormExtensionAbility { 162 onAddForm(want: Want) { 163 let template: dataShare.Template = { 164 predicates: { 165 "list": "select type from TBL00 limit 0,1" 166 }, 167 scheduler: "" 168 } 169 let subscriberId: string = "111"; 170 dataShare.createDataShareHelper(this.context, "datashareproxy://com.example.myapplication", { 171 isProxy: true 172 }).then((data: dataShare.DataShareHelper) => { 173 dataShareHelper = data; 174 dataShareHelper.addTemplate("datashareproxy://com.example.myapplication/test", subscriberId, template); 175 }) 176 177 let formData: Record<string, Object> = {}; 178 let proxies: formBindingData.ProxyData[] = [ 179 { 180 "key": "datashareproxy://com.example.myapplication/test", 181 "subscriberId": subscriberId 182 } 183 ] 184 let formBinding = formBindingData.createFormBindingData(formData); 185 formBinding["proxies"] = proxies; 186 187 return formBinding; 188 } 189 } 190 ``` 191 192- In the [widget page code file](arkts-ui-widget-creation.md), use the variable in LocalStorage to obtain the subscribed data. The variable in LocalStorage is bound to a string and updates the subscribed data in the key:value pair format. The key must be the same as that subscribed to by the widget provider. In the example, the subscribed data is obtained through **'list'**, and the value of the first element is displayed on the **\<Text>** component. 193 ```ts 194 let storage = new LocalStorage(); 195 @Entry(storage) 196 @Component 197 struct WidgetCard { 198 readonly ACTION_TYPE: string = 'router'; 199 readonly ABILITY_NAME: string = 'EntryAbility'; 200 readonly MESSAGE: string = 'add detail'; 201 readonly FULL_WIDTH_PERCENT: string = '100%'; 202 readonly FULL_HEIGHT_PERCENT: string = '100%'; 203 @LocalStorageProp('list') list: Record<string, string>[] = [{"type": "a"}]; 204 205 build() { 206 Row() { 207 Column() { 208 Text((this.list[0]["type"])) 209 .fontSize($r('app.float.font_size')) 210 } 211 .width(this.FULL_WIDTH_PERCENT) 212 } 213 .height(this.FULL_HEIGHT_PERCENT) 214 .onClick(() => { 215 postCardAction(this, { 216 "action": this.ACTION_TYPE, 217 "abilityName": this.ABILITY_NAME, 218 "params": { 219 "message": this.MESSAGE 220 } 221 }) 222 }) 223 } 224 } 225 ``` 226 227## Data Provider Development 228 229For details, see [Data Management](../database/share-data-by-silent-access.md). 230