1df226684Sopenharmony_ci/*
2df226684Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
3df226684Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4df226684Sopenharmony_ci * you may not use this file except in compliance with the License.
5df226684Sopenharmony_ci * You may obtain a copy of the License at
6df226684Sopenharmony_ci *
7df226684Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8df226684Sopenharmony_ci *
9df226684Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10df226684Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11df226684Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12df226684Sopenharmony_ci * See the License for the specific language governing permissions and
13df226684Sopenharmony_ci * limitations under the License.
14df226684Sopenharmony_ci */
15df226684Sopenharmony_ci
16df226684Sopenharmony_ciimport ActionExtensionAbility from '@ohos.app.ability.ActionExtensionAbility';
17df226684Sopenharmony_ciimport UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession';
18df226684Sopenharmony_ciimport { Configuration } from '@ohos.app.ability.Configuration';
19df226684Sopenharmony_ciimport emitter from '@ohos.events.emitter';
20df226684Sopenharmony_ciimport Want from '@ohos.app.ability.Want';
21df226684Sopenharmony_ciimport { HiLog } from '../common/HiLog';
22df226684Sopenharmony_ciimport { getDLPInfo, DLPInfo } from '../common/utils';
23df226684Sopenharmony_ci
24df226684Sopenharmony_ciconst TAG = 'share';
25df226684Sopenharmony_ciconst BG_COLOR = '#00000000';
26df226684Sopenharmony_ci
27df226684Sopenharmony_ciexport default class EncryptedSharingAbility extends ActionExtensionAbility {
28df226684Sopenharmony_ci
29df226684Sopenharmony_ci  onForeground(): void {
30df226684Sopenharmony_ci    HiLog.info(TAG, `EncryptedSharingAbility onForeground.`);
31df226684Sopenharmony_ci  }
32df226684Sopenharmony_ci
33df226684Sopenharmony_ci  onBackground(): void {
34df226684Sopenharmony_ci    HiLog.info(TAG, `EncryptedSharingAbility onBackground.`);
35df226684Sopenharmony_ci  }
36df226684Sopenharmony_ci
37df226684Sopenharmony_ci  onConfigurationUpdate(newConfig: Configuration): void {
38df226684Sopenharmony_ci    HiLog.info(TAG, 'haringAbility onConfigurationUpdate new language: ' + newConfig.language);
39df226684Sopenharmony_ci    emitter.emit('onConfigurationUpdate');
40df226684Sopenharmony_ci  }
41df226684Sopenharmony_ci
42df226684Sopenharmony_ci  checkValidSharedRecords(want: Want): Promise<void> {
43df226684Sopenharmony_ci    return new Promise((resolve, reject) => {
44df226684Sopenharmony_ci      let parameters = want.parameters as Record<string, Object>;
45df226684Sopenharmony_ci      let callerToken = parameters['ohos.aafwk.param.callerToken'] as number;
46df226684Sopenharmony_ci      let callerBundleName: string = parameters['ohos.aafwk.param.callerBundleName'] as string;
47df226684Sopenharmony_ci      if (callerToken === undefined || callerBundleName === undefined) {
48df226684Sopenharmony_ci        HiLog.error(TAG, `need caller info in want.parameters`);
49df226684Sopenharmony_ci        reject(); return;
50df226684Sopenharmony_ci      }
51df226684Sopenharmony_ci      AppStorage.setOrCreate('hiPkgName', callerBundleName);
52df226684Sopenharmony_ci      resolve(); return;
53df226684Sopenharmony_ci    })
54df226684Sopenharmony_ci  }
55df226684Sopenharmony_ci
56df226684Sopenharmony_ci  async onSessionCreate(want: Want, session: UIExtensionContentSession): Promise<void> {
57df226684Sopenharmony_ci    HiLog.info(TAG, `EncryptedSharingAbility onSessionCreate.`);
58df226684Sopenharmony_ci    try {
59df226684Sopenharmony_ci      await this.checkValidSharedRecords(want);
60df226684Sopenharmony_ci    } catch {
61df226684Sopenharmony_ci      return;
62df226684Sopenharmony_ci    }
63df226684Sopenharmony_ci    let dlpInfo: DLPInfo = await getDLPInfo();
64df226684Sopenharmony_ci    AppStorage.setOrCreate('hiPNameId', dlpInfo.name);
65df226684Sopenharmony_ci    AppStorage.setOrCreate('hiPVersionId', dlpInfo.versionCode);
66df226684Sopenharmony_ci    let storage: LocalStorage = new LocalStorage({
67df226684Sopenharmony_ci      'session': session,
68df226684Sopenharmony_ci      'actionWant': want
69df226684Sopenharmony_ci    } as Record<string, UIExtensionContentSession | Want>);
70df226684Sopenharmony_ci    try {
71df226684Sopenharmony_ci      session.loadContent('pages/encryptedSharing', storage);
72df226684Sopenharmony_ci      session.setWindowBackgroundColor(BG_COLOR);
73df226684Sopenharmony_ci    } catch (exception) {
74df226684Sopenharmony_ci      HiLog.error(TAG, `Failed to set the background color. Cause: ${JSON.stringify(exception)}`);
75df226684Sopenharmony_ci    }
76df226684Sopenharmony_ci  }
77df226684Sopenharmony_ci
78df226684Sopenharmony_ci  onSessionDestroy(): void {
79df226684Sopenharmony_ci    HiLog.info(TAG, `EncryptedSharingAbility onSessionDestroy.`);
80df226684Sopenharmony_ci  }
81df226684Sopenharmony_ci
82df226684Sopenharmony_ci  onDestroy(): void {
83df226684Sopenharmony_ci    HiLog.info(TAG, `EncryptedSharingAbility onDestroy.`);
84df226684Sopenharmony_ci  }
85df226684Sopenharmony_ci};