• 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    offset: { dx: 0, dy: 0 - this.want.parameters['ohos.dialog.offset'] + 'px' }
96  });
97
98  @Builder
99  buildContent(): void {
100    Flex({ justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center }) {
101      Scroll() {
102        Column() {
103          Column() {
104            SymbolGlyph($r('sys.symbol.person_shield_fill'))
105              .width(Constants.SECURITY_ICON_WIDTH)
106              .height(Constants.SECURITY_ICON_HEIGHT)
107              .fontSize(Constants.FONT_SIZE_28)
108              .fontColor([$r('sys.color.brand')])
109              .border({
110                width: Constants.BORDER_WIDTH_1,
111                color: $r('app.color.icon_border'),
112                radius: $r('sys.float.ohos_id_corner_radius_default_m')
113              })
114              .padding(Constants.PADDING_10)
115            if (this.index === 1) {
116              Image(this.securityParams[this.index].icon)
117                .width(Constants.IMAGE_LENGTH_20)
118                .height(Constants.IMAGE_LENGTH_20)
119                .syncLoad(true)
120                .position({ x: Constants.IMAGE_POSITION_28, y: Constants.IMAGE_POSITION_28 })
121                .border({
122                  width: Constants.BORDER_WIDTH_1,
123                  color: $r('app.color.icon_border'),
124                  radius: Constants.IMAGE_LENGTH_20 * 14 / 54
125                })
126            } else {
127              SymbolGlyph($r('sys.symbol.local_fill'))
128                .width(Constants.IMAGE_LENGTH_20)
129                .height(Constants.IMAGE_LENGTH_20)
130                .fontSize(Constants.FONT_SIZE_12)
131                .fontColor([Color.White])
132                .backgroundColor($r('app.color.local_background_color'))
133                .padding(Constants.PADDING_4)
134                .position({ x: Constants.IMAGE_POSITION_28, y: Constants.IMAGE_POSITION_28 })
135                .border({
136                  width: Constants.BORDER_WIDTH_1,
137                  color: $r('app.color.icon_border'),
138                  radius: Constants.IMAGE_LENGTH_20 * 14 / 54
139                })
140            }
141          }
142          .pixelRound({ start: PixelRoundCalcPolicy.NO_FORCE_ROUND, end: PixelRoundCalcPolicy.NO_FORCE_ROUND })
143          .backgroundColor($r('app.color.icon_bg'))
144          .borderRadius($r('sys.float.ohos_id_corner_radius_default_m'))
145          Column() { // content
146            Column() {
147              Text(this.securityParams[this.index].label)
148                .textAlign(TextAlign.Center)
149                .fontColor($r('sys.color.font_primary'))
150                .fontSize($r('sys.float.Title_S'))
151                .fontWeight(FontWeight.Bold)
152                .textOverflow({ overflow: TextOverflow.Ellipsis })
153                .maxLines(Constants.SECURITY_HEADER_MAX_LINES)
154                .minFontSize(
155                  getLimitFontSize(getFontSizeScale(),
156                  Constants.SECURITY_HEADER_MAX_SCALE,
157                  $r('sys.float.Subtitle_M'),
158                  $r('sys.float.Title_S'))
159                )
160                .maxFontSize(
161                  getLimitFontSize(getFontSizeScale(),
162                  Constants.SECURITY_HEADER_MAX_SCALE,
163                  $r('sys.float.Title_S'),
164                  $r('sys.float.Title_S'))
165                )
166                .heightAdaptivePolicy(TextHeightAdaptivePolicy.MIN_FONT_SIZE_FIRST)
167            }
168            .constraintSize({ minHeight: Constants.HEADLINE_HEIGHT })
169            .justifyContent(FlexAlign.Center)
170            .padding({
171              top: Constants.DEFAULT_PADDING_TOP,
172              bottom: Constants.DEFAULT_PADDING_BOTTOM,
173            })
174
175            Text($r(this.securityParams[this.index].description, this.appName))
176              .textAlign(TextAlign.Start)
177              .fontColor($r('sys.color.font_primary'))
178              .fontSize($r('sys.float.Body_L'))
179              .lineHeight(Constants.TEXT_SMALL_LINE_HEIGHT)
180              .maxFontScale(Constants.DIALOG_TEXT_MAX_SCALE)
181          }
182        }
183        .clip(true)
184      }
185      .padding({ left: Constants.PADDING_24, right: Constants.PADDING_24 })
186      .margin({ top: Constants.MARGIN_24 })
187      .edgeEffect(EdgeEffect.Spring, { alwaysEnabled: false })
188      .scrollBarWidth(this.scrollBarWidth)
189      .onScrollStart(() => {
190        this.scrollBarWidth = Constants.SCROLL_BAR_WIDTH_ACTIVE;
191      })
192      .onScrollStop(() => {
193        this.scrollBarWidth = Constants.SCROLL_BAR_WIDTH_DEFAULT;
194      })
195    }
196  }
197
198  build() {}
199
200  aboutToAppear() {
201    Log.info('onAboutToAppear.');
202    this.GetAppName();
203    this.index = this.want.parameters['ohos.user.security.type'];
204    this.dialogController?.open();
205  }
206
207  aboutToDisappear() {
208    this.dialogController = null;
209  }
210
211  GetAppName() {
212    let bundleName: string = this.want.parameters['ohos.aafwk.param.callerBundleName'];
213    bundleManager.getApplicationInfo(bundleName, bundleManager.ApplicationFlag.GET_APPLICATION_INFO_DEFAULT)
214      .then(data => {
215        data.labelResource.params = [];
216        data.labelResource.type = RESOURCE_TYPE;
217        this.appName = data.labelResource;
218      })
219      .catch((error: BusinessError) => {
220        Log.error('getApplicationInfo failed. err is ' + JSON.stringify(error));
221      });
222  }
223
224  destruction() {
225    let option = new rpc.MessageOption();
226    let data = new rpc.MessageSequence();
227    let reply = new rpc.MessageSequence();
228    Promise.all([
229      data.writeInterfaceToken(Constants.SEC_COMP_DIALOG_CALLBACK),
230      data.writeInt(0)
231    ]).then(() => {
232      let proxy = this.want.parameters['ohos.ability.params.callback'].value as rpc.RemoteObject;
233      if (proxy != undefined) {
234        proxy.sendMessageRequest(Constants.RESULT_CODE, data, reply, option);
235      }
236    }).catch(() => {
237      Log.error('write result failed!');
238    }).finally(() => {
239      data.reclaim();
240      reply.reclaim();
241      this.win.destroyWindow();
242      let dialogSet: Set<number> = GlobalContext.load('dialogSet');
243      let callerToken: number = this.want.parameters['ohos.caller.uid'];
244      dialogSet.delete(callerToken);
245      GlobalContext.store('dialogSet', dialogSet);
246      if (dialogSet.size === 0) {
247        this.context.terminateSelf();
248      }
249    })
250  }
251}
252