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 { UIExtensionContentSession, Want } from '@kit.AbilityKit'; 17import { hilog } from '@kit.PerformanceAnalysisKit'; 18import { promptAction } from '@kit.ArkUI'; 19import uiExtension from '@ohos.arkui.uiExtension'; 20 21const TAG: string = 'UIExtensionContentSession'; 22const DOMAIN: number = 0xFF00; 23let storage: LocalStorage = LocalStorage.getShared(); 24 25@Entry 26@Component 27struct UIExtensionContentSessionPage { 28 private session: (UIExtensionContentSession | undefined) = storage.get<UIExtensionContentSession>('session'); 29 private want: (Want | undefined) = storage.get<Want>('want'); 30 private promptDuration: number = 2000; 31 32 build() { 33 Column() { 34 Row() { 35 Flex({ justifyContent: FlexAlign.Start, alignContent: FlexAlign.Center }) { 36 Text($r('app.string.UIExtensionAbilityContext')) 37 .fontSize(30) 38 .fontWeight(700) 39 .textAlign(TextAlign.Start) 40 .margin({ 41 top: '12%', 42 left: '2%' 43 }) 44 } 45 } 46 .width('100%') 47 .height('18%') 48 .justifyContent(FlexAlign.Start) 49 .backgroundColor($r('app.color.backGrounding')) 50 51 List({ initialIndex: 0 }) { 52 ListItem() { 53 Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) { 54 Text($r('app.string.UIExtensionContentSession_terminateSelfWithResultCallback')) 55 .textAlign(TextAlign.Start) 56 .fontWeight(500) 57 .margin({ 58 top: 17, 59 bottom: 17, 60 left: 12 61 }) 62 .fontSize(16) 63 .width('77.87%') 64 .height('39.29%') 65 .fontColor($r('app.color.text_color')) 66 67 } 68 .id('terminateSelfWithResultCallback') 69 .onClick(() => { 70 ((): void => { 71 this.session?.terminateSelfWithResult({ resultCode: 99, want: this.want }, () => { 72 hilog.info(DOMAIN, TAG, 'UIExtensionContentSession.terminateSelfWithResult(callback) called'); 73 }) 74 })() 75 }) 76 } 77 .height('8.45%') 78 .backgroundColor($r('app.color.start_window_background')) 79 .borderRadius(24) 80 .margin({ top: 12, right: 12, left: 12 }) 81 82 ListItem() { 83 Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) { 84 Text($r('app.string.UIExtensionContentSession_terminateSelfWithResultPromise')) 85 .textAlign(TextAlign.Start) 86 .fontWeight(500) 87 .margin({ 88 top: 17, 89 bottom: 17, 90 left: 12 91 }) 92 .fontSize(16) 93 .width('77.87%') 94 .height('39.29%') 95 .fontColor($r('app.color.text_color')) 96 } 97 .id('terminateSelfWithResultPromise') 98 .onClick(() => { 99 ((): void => { 100 this.session?.terminateSelfWithResult({ resultCode: 100, want: this.want }).then(() => { 101 hilog.info(DOMAIN, TAG, 'UIExtensionContentSession.terminateSelfWithResult(promise) called'); 102 }) 103 })() 104 }) 105 } 106 .height('8.45%') 107 .backgroundColor($r('app.color.start_window_background')) 108 .borderRadius(24) 109 .margin({ top: 12, right: 12, left: 12 }) 110 111 112 ListItem() { 113 Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) { 114 Text($r('app.string.UIExtensionContentSession_setWindowPrivacyModeCallback')) 115 .textAlign(TextAlign.Start) 116 .fontWeight(500) 117 .margin({ 118 top: 17, 119 bottom: 17, 120 left: 12 121 }) 122 .fontSize(16) 123 .width('77.87%') 124 .height('39.29%') 125 .fontColor($r('app.color.text_color')) 126 } 127 .id('setWindowPrivacyModeCallback') 128 .onClick(() => { 129 ((): void => { 130 this.session?.setWindowPrivacyMode(true, () => { 131 let msg = `UIExtensionContentSession.setWindowPrivacyMode(callback) called. isPrivacyMode: true`; 132 hilog.info(DOMAIN, TAG, msg); 133 promptAction.showToast({ message: msg, duration: this.promptDuration }); 134 }) 135 })() 136 }) 137 } 138 .height('8.45%') 139 .backgroundColor($r('app.color.start_window_background')) 140 .borderRadius(24) 141 .margin({ top: 12, right: 12, left: 12 }) 142 143 ListItem() { 144 Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) { 145 Text($r('app.string.UIExtensionContentSession_setWindowPrivacyModePromise')) 146 .textAlign(TextAlign.Start) 147 .fontWeight(500) 148 .margin({ 149 top: 17, 150 bottom: 17, 151 left: 12 152 }) 153 .fontSize(16) 154 .width('77.87%') 155 .height('39.29%') 156 .fontColor($r('app.color.text_color')) 157 } 158 .id('setWindowPrivacyModePromise') 159 .onClick(() => { 160 ((): void => { 161 this.session?.setWindowPrivacyMode(false).then(() => { 162 let msg = `UIExtensionContentSession.setWindowPrivacyMode(promise) called. isPrivacyMode: false`; 163 hilog.info(DOMAIN, TAG, msg); 164 promptAction.showToast({ message: msg, duration: this.promptDuration }); 165 }) 166 })() 167 }) 168 } 169 .height('8.45%') 170 .backgroundColor($r('app.color.start_window_background')) 171 .borderRadius(24) 172 .margin({ top: 12, right: 12, left: 12 }) 173 174 175 ListItem() { 176 Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) { 177 Text($r('app.string.UIExtensionContentSession_getUIExtensionWindowProxy')) 178 .textAlign(TextAlign.Start) 179 .fontWeight(500) 180 .margin({ 181 top: 17, 182 bottom: 17, 183 left: 12 184 }) 185 .fontSize(16) 186 .width('77.87%') 187 .height('39.29%') 188 .fontColor($r('app.color.text_color')) 189 } 190 .id('getUIExtensionWindowProxy') 191 .onClick(() => { 192 ((): void => { 193 let windowProxy: (uiExtension.WindowProxy | undefined) = this.session?.getUIExtensionWindowProxy(); 194 let msg = `UIExtensionContentSession.getUIExtensionWindowProxy called. proxy: ${typeof windowProxy}`; 195 hilog.info(DOMAIN, TAG, msg); 196 promptAction.showToast({ message: msg, duration: this.promptDuration }); 197 })() 198 }) 199 } 200 .height('8.45%') 201 .backgroundColor($r('app.color.start_window_background')) 202 .borderRadius(24) 203 .margin({ top: 12, right: 12, left: 12 }) 204 205 } 206 .height('86%') 207 .backgroundColor($r('app.color.backGrounding')) 208 } 209 .height('100%') 210 .width('100%') 211 .backgroundColor($r('app.color.backGrounding')) 212 } 213}