• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2024 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 ActionExtensionAbility from '@ohos.app.ability.ActionExtensionAbility';
17import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession';
18import { Configuration } from '@ohos.app.ability.Configuration';
19import emitter from '@ohos.events.emitter';
20import Want from '@ohos.app.ability.Want';
21import { HiLog } from '../common/HiLog';
22import { getDLPInfo, DLPInfo } from '../common/utils';
23
24const TAG = 'share';
25const BG_COLOR = '#00000000';
26
27export default class EncryptedSharingAbility extends ActionExtensionAbility {
28
29  onForeground(): void {
30    HiLog.info(TAG, `EncryptedSharingAbility onForeground.`);
31  }
32
33  onBackground(): void {
34    HiLog.info(TAG, `EncryptedSharingAbility onBackground.`);
35  }
36
37  onConfigurationUpdate(newConfig: Configuration): void {
38    HiLog.info(TAG, 'haringAbility onConfigurationUpdate new language: ' + newConfig.language);
39    emitter.emit('onConfigurationUpdate');
40  }
41
42  checkValidSharedRecords(want: Want): Promise<void> {
43    return new Promise((resolve, reject) => {
44      let parameters = want.parameters as Record<string, Object>;
45      let callerToken = parameters['ohos.aafwk.param.callerToken'] as number;
46      let callerBundleName: string = parameters['ohos.aafwk.param.callerBundleName'] as string;
47      if (callerToken === undefined || callerBundleName === undefined) {
48        HiLog.error(TAG, `need caller info in want.parameters`);
49        reject(); return;
50      }
51      AppStorage.setOrCreate('hiPkgName', callerBundleName);
52      resolve(); return;
53    })
54  }
55
56  async onSessionCreate(want: Want, session: UIExtensionContentSession): Promise<void> {
57    HiLog.info(TAG, `EncryptedSharingAbility onSessionCreate.`);
58    try {
59      await this.checkValidSharedRecords(want);
60    } catch {
61      return;
62    }
63    let dlpInfo: DLPInfo = await getDLPInfo();
64    AppStorage.setOrCreate('hiPNameId', dlpInfo.name);
65    AppStorage.setOrCreate('hiPVersionId', dlpInfo.versionCode);
66    let storage: LocalStorage = new LocalStorage({
67      'session': session,
68      'actionWant': want
69    } as Record<string, UIExtensionContentSession | Want>);
70    try {
71      session.loadContent('pages/encryptedSharing', storage);
72      session.setWindowBackgroundColor(BG_COLOR);
73    } catch (exception) {
74      HiLog.error(TAG, `Failed to set the background color. Cause: ${JSON.stringify(exception)}`);
75    }
76  }
77
78  onSessionDestroy(): void {
79    HiLog.info(TAG, `EncryptedSharingAbility onSessionDestroy.`);
80  }
81
82  onDestroy(): void {
83    HiLog.info(TAG, `EncryptedSharingAbility onDestroy.`);
84  }
85};