• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022 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 */
15
16import extension from '@ohos.app.ability.ServiceExtensionAbility';
17import display from '@ohos.display';
18import deviceInfo from '@ohos.deviceInfo';
19import { Want } from '@kit.AbilityKit';
20import { Log } from '../common/utils/utils';
21import { GrantDialogModel } from './GrantDialogModel';
22import { GlobalContext } from '../common/utils/globalContext';
23
24let grantDialogModel: GrantDialogModel = new GrantDialogModel();
25
26export default class ServiceExtensionAbility extends extension {
27  /**
28  * Lifecycle function, called back when a service extension is started for initialization.
29  */
30  onCreate(want: Want): void {
31    Log.info('ServiceExtensionAbility onCreate, ability name is ' + want.abilityName);
32    GlobalContext.getContext().setAndGetWindowNum(0);
33  }
34
35  /**
36  * Lifecycle function, called back when a service extension is started or recall.
37  */
38  onRequest(want: Want, startId: number): void {
39    Log.info('ServiceExtensionAbility onRequest. start id is ' + startId);
40    if (deviceInfo.deviceType === 'wearable') {
41      this.context.terminateSelf();
42      Log.info('ServiceExtensionAbility terminateSelf');
43      return;
44    }
45
46    try {
47      let dis = display.getDefaultDisplaySync();
48      let navigationBarRect: display.Rect = {
49        left: 0,
50        top: 0,
51        width: dis.width,
52        height: dis.height
53      };
54      Log.info('want: ' + JSON.stringify(want));
55      grantDialogModel.createWindow(this.context, 'permissionDialog' + startId, navigationBarRect, want);
56    } catch (exception) {
57      Log.error('Failed to obtain the default display object. Code: ' + JSON.stringify(exception));
58    };
59  }
60
61  /**
62  * Lifecycle function, called back before a service extension is destroyed.
63  */
64  onDestroy(): void {
65    Log.info('ServiceExtensionAbility onDestroy.');
66  }
67
68};