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