• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 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 */
15import bundleManager from '@ohos.bundle.bundleManager';
16import Constants from '../common/utils/constant';
17import rpc from '@ohos.rpc';
18import window from '@ohos.window';
19import common from '@ohos.app.ability.common';
20import { BusinessError } from '@ohos.base';
21import { CustomContentDialog } from '@ohos.arkui.advanced.Dialog';
22import {
23  Log,
24  getFontSizeScale,
25  getLimitFontSize
26} from '../common/utils/utils';
27import { Param, WantInfo } from '../common/utils/typedef';
28import { GlobalContext } from '../common/utils/globalContext';
29
30let storage = LocalStorage.getShared();
31const RESOURCE_TYPE: number = 10003;
32
33@Entry(storage)
34@Component
35struct SecurityDialog {
36  private context = getContext(this) as common.ServiceExtensionContext;
37  @LocalStorageLink('want') want: WantInfo = new WantInfo([]);
38  @LocalStorageLink('win') win: window.Window = {} as window.Window;
39  @State appName: ResourceStr = 'ToBeInstead';
40  @State index: number = 0;
41  @State scrollBarWidth: number = Constants.SCROLL_BAR_WIDTH_DEFAULT;
42
43  securityParams : Array<Param> = [
44    new Param(
45      $r('app.media.ic_location'), $r('app.string.SecurityTitle_location'), 'app.string.SecurityDescription_location'
46    ),
47    new Param(
48      $r('app.media.rawfile'), $r('app.string.SecurityTitle_mediaFiles'), 'app.string.SecurityDescription_mediaFiles'
49    )
50  ]
51
52  dialogController: CustomDialogController | null = new CustomDialogController({
53    builder: CustomContentDialog({
54      contentBuilder: () => {
55        this.buildContent();
56      },
57      contentAreaPadding: { right: 0 },
58      buttons: [
59        {
60          value: $r('app.string.cancel'),
61          buttonStyle: ButtonStyleMode.TEXTUAL,
62          action: () => {
63            this.dialogController?.close();
64            this.win.destroyWindow();
65            let dialogSet: Set<number> = GlobalContext.load('dialogSet');
66            let callerToken: number = this.want.parameters['ohos.caller.uid'];
67            dialogSet.delete(callerToken);
68            GlobalContext.store('dialogSet', dialogSet);
69            if (dialogSet.size === 0) {
70              this.context.terminateSelf();
71            }
72          }
73        },
74        {
75          value: $r('app.string.allow'),
76          buttonStyle: ButtonStyleMode.TEXTUAL,
77          action: () => {
78            this.dialogController?.close();
79            this.destruction();
80          }
81        }
82      ],
83    }),
84    autoCancel: false,
85    cancel: () => {
86      this.win.destroyWindow();
87      let dialogSet: Set<number> = GlobalContext.load('dialogSet');
88      let callerToken: number = this.want.parameters['ohos.caller.uid'];
89      dialogSet.delete(callerToken);
90      GlobalContext.store('dialogSet', dialogSet);
91      if (dialogSet.size === 0) {
92        this.context.terminateSelf();
93      }
94    }
95  });
96
97  @Builder
98  buildContent(): void {
99    Flex({ justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center }) {
100      Scroll() {
101        Column() {
102          Column() {
103            SymbolGlyph($r('sys.symbol.person_shield_fill'))
104              .width(Constants.SECURITY_ICON_WIDTH)
105              .height(Constants.SECURITY_ICON_HEIGHT)
106              .fontSize(Constants.FONT_SIZE_28)
107              .fontColor([$r('sys.color.brand')])
108              .border({
109                width: Constants.BORDER_WIDTH_1,
110                color: $r('app.color.icon_border'),
111                radius: $r('sys.float.ohos_id_corner_radius_default_m')
112              })
113              .padding(Constants.PADDING_10)
114            if (this.index === 1) {
115              Image(this.securityParams[this.index].icon)
116                .width(Constants.IMAGE_LENGTH_20)
117                .height(Constants.IMAGE_LENGTH_20)
118                .syncLoad(true)
119                .position({ x: Constants.IMAGE_POSITION_28, y: Constants.IMAGE_POSITION_28 })
120                .border({
121                  width: Constants.BORDER_WIDTH_1,
122                  color: $r('app.color.icon_border'),
123                  radius: Constants.IMAGE_LENGTH_20 * 14 / 54
124                })
125            } else {
126              SymbolGlyph($r('sys.symbol.local_fill'))
127                .width(Constants.IMAGE_LENGTH_20)
128                .height(Constants.IMAGE_LENGTH_20)
129                .fontSize(Constants.FONT_SIZE_12)
130                .fontColor([Color.White])
131                .backgroundColor($r('app.color.local_background_color'))
132                .padding(Constants.PADDING_4)
133                .position({ x: Constants.IMAGE_POSITION_28, y: Constants.IMAGE_POSITION_28 })
134                .border({
135                  width: Constants.BORDER_WIDTH_1,
136                  color: $r('app.color.icon_border'),
137                  radius: Constants.IMAGE_LENGTH_20 * 14 / 54
138                })
139            }
140          }
141          .backgroundColor($r('app.color.icon_bg'))
142          .borderRadius($r('sys.float.ohos_id_corner_radius_default_m'))
143          Column() { // content
144            Column() {
145              Text(this.securityParams[this.index].label)
146                .textAlign(TextAlign.Center)
147                .fontColor($r('sys.color.font_primary'))
148                .fontSize($r('sys.float.Title_S'))
149                .fontWeight(FontWeight.Bold)
150                .textOverflow({ overflow: TextOverflow.Ellipsis })
151                .maxLines(Constants.SECURITY_HEADER_MAX_LINES)
152                .minFontSize(
153                  getLimitFontSize(getFontSizeScale(),
154                  Constants.SECURITY_HEADER_MAX_SCALE,
155                  $r('sys.float.Subtitle_M'),
156                  $r('sys.float.Title_S'))
157                )
158                .maxFontSize(
159                  getLimitFontSize(getFontSizeScale(),
160                  Constants.SECURITY_HEADER_MAX_SCALE,
161                  $r('sys.float.Title_S'),
162                  $r('sys.float.Title_S'))
163                )
164                .heightAdaptivePolicy(TextHeightAdaptivePolicy.MIN_FONT_SIZE_FIRST)
165            }
166            .constraintSize({ minHeight: Constants.HEADLINE_HEIGHT })
167            .justifyContent(FlexAlign.Center)
168            .padding({
169              top: Constants.DEFAULT_PADDING_TOP,
170              bottom: Constants.DEFAULT_PADDING_BOTTOM,
171            })
172
173            Text($r(this.securityParams[this.index].description, this.appName))
174              .textAlign(TextAlign.Start)
175              .fontColor($r('sys.color.font_primary'))
176              .fontSize($r('sys.float.Body_L'))
177              .lineHeight(Constants.TEXT_SMALL_LINE_HEIGHT)
178              .maxFontScale(Constants.DIALOG_TEXT_MAX_SCALE)
179          }
180        }
181        .clip(true)
182      }
183      .padding({ left: Constants.PADDING_24, right: Constants.PADDING_24 })
184      .margin({ top: Constants.MARGIN_24 })
185      .edgeEffect(EdgeEffect.Spring, { alwaysEnabled: false })
186      .scrollBarWidth(this.scrollBarWidth)
187      .onScrollStart(() => {
188        this.scrollBarWidth = Constants.SCROLL_BAR_WIDTH_ACTIVE;
189      })
190      .onScrollStop(() => {
191        this.scrollBarWidth = Constants.SCROLL_BAR_WIDTH_DEFAULT;
192      })
193    }
194  }
195
196  build() {}
197
198  aboutToAppear() {
199    Log.info('onAboutToAppear.');
200    this.GetAppName();
201    this.index = this.want.parameters['ohos.user.security.type'];
202    this.dialogController?.open();
203  }
204
205  aboutToDisappear() {
206    this.dialogController = null;
207  }
208
209  GetAppName() {
210    let bundleName: string = this.want.parameters['ohos.aafwk.param.callerBundleName'];
211    bundleManager.getApplicationInfo(bundleName, bundleManager.ApplicationFlag.GET_APPLICATION_INFO_DEFAULT)
212      .then(data => {
213        data.labelResource.params = [];
214        data.labelResource.type = RESOURCE_TYPE;
215        this.appName = data.labelResource;
216      })
217      .catch((error: BusinessError) => {
218        Log.error('getApplicationInfo failed. err is ' + JSON.stringify(error));
219      });
220  }
221
222  destruction() {
223    let option = new rpc.MessageOption();
224    let data = new rpc.MessageSequence();
225    let reply = new rpc.MessageSequence();
226    Promise.all([
227      data.writeInterfaceToken(Constants.SEC_COMP_DIALOG_CALLBACK),
228      data.writeInt(0)
229    ]).then(() => {
230      let proxy = this.want.parameters['ohos.ability.params.callback'].value as rpc.RemoteObject;
231      if (proxy != undefined) {
232        proxy.sendMessageRequest(Constants.RESULT_CODE, data, reply, option);
233      }
234    }).catch(() => {
235      Log.error('write result failed!');
236    }).finally(() => {
237      data.reclaim();
238      reply.reclaim();
239      this.win.destroyWindow();
240      let dialogSet: Set<number> = GlobalContext.load('dialogSet');
241      let callerToken: number = this.want.parameters['ohos.caller.uid'];
242      dialogSet.delete(callerToken);
243      GlobalContext.store('dialogSet', dialogSet);
244      if (dialogSet.size === 0) {
245        this.context.terminateSelf();
246      }
247    })
248  }
249}
250