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 */
15import UIExtensionAbility from '@ohos.app.ability.UIExtensionAbility';
16import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession';
17import Want from '@ohos.app.ability.Want';
18import { Configuration } from '@ohos.app.ability.Configuration';
19
20const TAG: string = '[ContinueSwitchAbility]'
21
22export default class ContinueSwitchAbility extends UIExtensionAbility {
23  onCreate() {
24    console.log(TAG, `UIExtAbility onCreate`);
25    AppStorage.setOrCreate('currentColorMode', this.context.config.colorMode);
26    AppStorage.setOrCreate('currentFontSizeScale', this.context.config.fontSizeScale);
27  }
28
29  onConfigurationUpdate(newConfig: Configuration) {
30    AppStorage.setOrCreate('currentColorMode', this.context.config.colorMode);
31    AppStorage.setOrCreate('currentFontSizeScale', this.context.config.fontSizeScale);
32  }
33
34  onForeground() {
35    console.log(TAG, `UIExtAbility onForeground`);
36  }
37
38  onBackground() {
39    console.log(TAG, `UIExtAbility onBackground`);
40  }
41
42  onDestroy() {
43    console.log(TAG, `UIExtAbility onDestroy`);
44  }
45
46  onSessionCreate(want: Want, session: UIExtensionContentSession) {
47    console.log(TAG, `UIExtAbility onSessionCreate.`);
48    let parameters = want.parameters;
49    let pushParams = want.parameters?.pushParams as string | undefined;
50    let startReason = '';
51    let isShowBack =
52      pushParams?.includes('isShowBack') ? (pushParams?.includes('isShowBack:false') ? false : true) : true;
53    if (parameters) {
54      startReason = parameters['startReason'] as string;
55    }
56    AppStorage.setOrCreate('continueSession', session);
57    AppStorage.setOrCreate('startReason', startReason);
58    AppStorage.setOrCreate('isShowBack', isShowBack);
59    let param: Record<string, UIExtensionContentSession> = {
60      'session': session
61    };
62    let storage: LocalStorage = new LocalStorage(param);
63    session.loadContent('pages/ContinueSwitch', storage);
64    console.log(TAG,
65      `onSessionCreate end. AppStorage.get<string>('startReason')= ${AppStorage.get<string>('startReason')}`);
66  }
67
68  onSessionDestroy(session: UIExtensionContentSession) {
69    console.log(TAG, `UIExtAbility onSessionDestroy`);
70  }
71}