1c5c2eed7Sopenharmony_ci/*
2c5c2eed7Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3c5c2eed7Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4c5c2eed7Sopenharmony_ci * you may not use this file except in compliance with the License.
5c5c2eed7Sopenharmony_ci * You may obtain a copy of the License at
6c5c2eed7Sopenharmony_ci *
7c5c2eed7Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8c5c2eed7Sopenharmony_ci *
9c5c2eed7Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10c5c2eed7Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11c5c2eed7Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12c5c2eed7Sopenharmony_ci * See the License for the specific language governing permissions and
13c5c2eed7Sopenharmony_ci * limitations under the License.
14c5c2eed7Sopenharmony_ci */
15c5c2eed7Sopenharmony_ci
16c5c2eed7Sopenharmony_ciimport extension from '@ohos.app.ability.ServiceExtensionAbility';
17c5c2eed7Sopenharmony_ciimport window from '@ohos.window';
18c5c2eed7Sopenharmony_ciimport display from '@ohos.display';
19c5c2eed7Sopenharmony_ciimport rpc from '@ohos.rpc';
20c5c2eed7Sopenharmony_ciimport { GlobalContext } from '../common/utils/globalContext';
21c5c2eed7Sopenharmony_ciimport dialogRequest from '@ohos.app.ability.dialogRequest';
22c5c2eed7Sopenharmony_ci
23c5c2eed7Sopenharmony_ciconst TAG = 'PermissionManager_Log: ';
24c5c2eed7Sopenharmony_ciconst BG_COLOR = '#00000000';
25c5c2eed7Sopenharmony_ciconst DEFAULT_CORNER_RADIUS_L = 16;
26c5c2eed7Sopenharmony_ciconst RESULT_CODE_1 = 1;
27c5c2eed7Sopenharmony_ciconst ACCESS_TOKEN = 'ohos.security.accesstoken.tokencallback';
28c5c2eed7Sopenharmony_ci
29c5c2eed7Sopenharmony_ciexport default class ServiceExtensionAbility extends extension {
30c5c2eed7Sopenharmony_ci  /**
31c5c2eed7Sopenharmony_ci  * Lifecycle function, called back when a service extension is started for initialization.
32c5c2eed7Sopenharmony_ci  */
33c5c2eed7Sopenharmony_ci  onCreate(want): void {
34c5c2eed7Sopenharmony_ci    console.info(TAG + 'ServiceExtensionAbility onCreate, ability name is ' + want.abilityName);
35c5c2eed7Sopenharmony_ci
36c5c2eed7Sopenharmony_ci    globalThis.windowNum = 0;
37c5c2eed7Sopenharmony_ci  }
38c5c2eed7Sopenharmony_ci
39c5c2eed7Sopenharmony_ci  /**
40c5c2eed7Sopenharmony_ci  * Lifecycle function, called back when a service extension is started or recall.
41c5c2eed7Sopenharmony_ci  */
42c5c2eed7Sopenharmony_ci  onRequest(want, startId): void {
43c5c2eed7Sopenharmony_ci    console.info(TAG + 'ServiceExtensionAbility onRequest. start id is ' + startId);
44c5c2eed7Sopenharmony_ci    console.info(TAG + 'want: ' + JSON.stringify(want));
45c5c2eed7Sopenharmony_ci
46c5c2eed7Sopenharmony_ci    try {
47c5c2eed7Sopenharmony_ci      let dis = display.getDefaultDisplaySync();
48c5c2eed7Sopenharmony_ci      let navigationBarRect = {
49c5c2eed7Sopenharmony_ci        left: 0,
50c5c2eed7Sopenharmony_ci        top: 0,
51c5c2eed7Sopenharmony_ci        width: dis.width,
52c5c2eed7Sopenharmony_ci        height: dis.height
53c5c2eed7Sopenharmony_ci      };
54c5c2eed7Sopenharmony_ci      this.createWindow('permissionDialog' + startId, window.WindowType.TYPE_DIALOG, navigationBarRect, want);
55c5c2eed7Sopenharmony_ci    } catch (exception) {
56c5c2eed7Sopenharmony_ci      console.error(TAG + 'Failed to obtain the default display object. Code: ' + JSON.stringify(exception));
57c5c2eed7Sopenharmony_ci    };
58c5c2eed7Sopenharmony_ci  }
59c5c2eed7Sopenharmony_ci
60c5c2eed7Sopenharmony_ci  /**
61c5c2eed7Sopenharmony_ci  * Lifecycle function, called back before a service extension is destroyed.
62c5c2eed7Sopenharmony_ci  */
63c5c2eed7Sopenharmony_ci  onDestroy(): void {
64c5c2eed7Sopenharmony_ci    console.info(TAG + 'ServiceExtensionAbility onDestroy.');
65c5c2eed7Sopenharmony_ci  }
66c5c2eed7Sopenharmony_ci
67c5c2eed7Sopenharmony_ci  private async createWindow(name: string, windowType, rect, want): Promise<void> {
68c5c2eed7Sopenharmony_ci    let requestInfo: dialogRequest.RequestInfo;
69c5c2eed7Sopenharmony_ci    try {
70c5c2eed7Sopenharmony_ci      requestInfo = dialogRequest.getRequestInfo(want);
71c5c2eed7Sopenharmony_ci    } catch (err) {
72c5c2eed7Sopenharmony_ci      console.error(`getRequestInfo err= ${JSON.stringify(err)}`);
73c5c2eed7Sopenharmony_ci    }
74c5c2eed7Sopenharmony_ci
75c5c2eed7Sopenharmony_ci    console.info(TAG + 'create window start, requestInfo: ' + JSON.stringify(requestInfo));
76c5c2eed7Sopenharmony_ci    let rectInfo = requestInfo ? requestInfo.windowRect : rect;
77c5c2eed7Sopenharmony_ci    rectInfo = rectInfo.width === 0 ? rect : rectInfo;
78c5c2eed7Sopenharmony_ci    try {
79c5c2eed7Sopenharmony_ci      const win = await window.createWindow({ ctx: this.context, name, windowType });
80c5c2eed7Sopenharmony_ci      console.info(TAG + 'createWindow end.');
81c5c2eed7Sopenharmony_ci      let storage: LocalStorage = new LocalStorage({ 'want': want, 'win': win });
82c5c2eed7Sopenharmony_ci      await this.BindDialogTarget(win, want);
83c5c2eed7Sopenharmony_ci      console.info(TAG + 'bindDialogTarget end.');
84c5c2eed7Sopenharmony_ci      await win.moveWindowTo(rectInfo.left, rectInfo.top);
85c5c2eed7Sopenharmony_ci      console.info(TAG + 'moveWindowTo end.');
86c5c2eed7Sopenharmony_ci      await win.resize(rectInfo.width, rectInfo.height);
87c5c2eed7Sopenharmony_ci      console.info(TAG + 'resize end.');
88c5c2eed7Sopenharmony_ci      await win.loadContent('pages/dialogPlus', storage);
89c5c2eed7Sopenharmony_ci      win.setWindowBackgroundColor(BG_COLOR);
90c5c2eed7Sopenharmony_ci      if (rectInfo.width < rect.width) {
91c5c2eed7Sopenharmony_ci        win.setCornerRadius(DEFAULT_CORNER_RADIUS_L);
92c5c2eed7Sopenharmony_ci      }
93c5c2eed7Sopenharmony_ci      await win.showWindow();
94c5c2eed7Sopenharmony_ci      console.info(TAG + 'showWindow end.');
95c5c2eed7Sopenharmony_ci      globalThis.windowNum ++;
96c5c2eed7Sopenharmony_ci      GlobalContext.store('windowNum', globalThis.windowNum);
97c5c2eed7Sopenharmony_ci    } catch (err) {
98c5c2eed7Sopenharmony_ci      console.error(TAG + `window create failed! err: ${JSON.stringify(err)}`);
99c5c2eed7Sopenharmony_ci    }
100c5c2eed7Sopenharmony_ci  }
101c5c2eed7Sopenharmony_ci
102c5c2eed7Sopenharmony_ci  private async BindDialogTarget(win, want): Promise<void> {
103c5c2eed7Sopenharmony_ci    let proxy = want.parameters['ohos.ability.params.callback'].value;
104c5c2eed7Sopenharmony_ci    win.bindDialogTarget(want.parameters['ohos.ability.params.token'].value, () => {
105c5c2eed7Sopenharmony_ci      let option = new rpc.MessageOption();
106c5c2eed7Sopenharmony_ci      let data = new rpc.MessageSequence();
107c5c2eed7Sopenharmony_ci      let reply = new rpc.MessageSequence();
108c5c2eed7Sopenharmony_ci      try {
109c5c2eed7Sopenharmony_ci        data.writeInterfaceToken(ACCESS_TOKEN);
110c5c2eed7Sopenharmony_ci        proxy.sendMessageRequest(RESULT_CODE_1, data, reply, option);
111c5c2eed7Sopenharmony_ci      } catch (err) {
112c5c2eed7Sopenharmony_ci        console.error(TAG + `write result failed: ${JSON.stringify(err)}`);
113c5c2eed7Sopenharmony_ci      } finally {
114c5c2eed7Sopenharmony_ci        data.reclaim();
115c5c2eed7Sopenharmony_ci        reply.reclaim();
116c5c2eed7Sopenharmony_ci      }
117c5c2eed7Sopenharmony_ci      let windowNum = GlobalContext.load('windowNum');
118c5c2eed7Sopenharmony_ci      windowNum --;
119c5c2eed7Sopenharmony_ci      GlobalContext.store('windowNum', windowNum);
120c5c2eed7Sopenharmony_ci      win.destroyWindow();
121c5c2eed7Sopenharmony_ci      if (windowNum === 0) {
122c5c2eed7Sopenharmony_ci        this.context.terminateSelf();
123c5c2eed7Sopenharmony_ci      }
124c5c2eed7Sopenharmony_ci    });
125c5c2eed7Sopenharmony_ci  }
126c5c2eed7Sopenharmony_ci};