1/* 2 * Copyright (c) 2023 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 featureAbility from '@ohos.ability.featureAbility'; 17import Want from '@ohos.app.ability.Want'; 18import { BusinessError } from '@ohos.base'; 19import fs from '@ohos.file.fs'; 20import promptAction from '@ohos.promptAction'; 21import worker from '@ohos.worker'; 22import hilog from '@ohos.hilog'; 23 24const TAG: string = 'PagePageAbilityFirst'; 25const domain: number = 0xFF00; 26 27@Entry 28@Component 29struct PagePageAbilityFirst { 30 build() { 31 Column() { 32 Row() { 33 Flex({ justifyContent: FlexAlign.Start, alignContent: FlexAlign.Center }) { 34 Text($r('app.string.pageAbility_first_button')) 35 .fontSize(24) 36 .fontWeight(FontWeight.Bold) 37 .textAlign(TextAlign.Start) 38 .margin({ top: 12, bottom: 11, right: 24, left: 24 }) 39 } 40 } 41 .width('100%') 42 .height(56) 43 .justifyContent(FlexAlign.Start) 44 .backgroundColor($r('app.color.backGrounding')) 45 46 List({ initialIndex: 0 }) { 47 ListItem() { 48 Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) { 49 Text($r('app.string.getDirectory_button')) 50 .textAlign(TextAlign.Start) 51 .fontWeight(FontWeight.Medium) 52 .margin({ top: 17, bottom: 17, left: 12, right: 92 }) 53 .fontSize(16) 54 .width(232) 55 .height(22) 56 .fontColor($r('app.color.text_color')) 57 } 58 .onClick(() => { 59 (async (): Promise<void> => { 60 let dir: string; 61 try { 62 hilog.info(domain, TAG, 'Begin to getOrCreateDistributedDir'); 63 dir = await featureAbility.getContext().getOrCreateDistributedDir(); 64 promptAction.showToast({ 65 message: dir 66 }); 67 hilog.info(domain, TAG, 'distribute dir is ' + dir); 68 let fd: number; 69 let path = dir + '/a.txt'; 70 fd = fs.openSync(path, fs.OpenMode.READ_WRITE).fd; 71 fs.close(fd); 72 } catch (error) { 73 hilog.error(domain, TAG, 'getOrCreateDistributedDir failed with : ' + error); 74 } 75 })() 76 }) 77 } 78 .height(56) 79 .backgroundColor($r('app.color.start_window_background')) 80 .borderRadius(24) 81 .margin({ top: 8, right: 12, left: 12 }) 82 83 ListItem() { 84 Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) { 85 Text($r('app.string.start_singleton_button')) 86 .textAlign(TextAlign.Start) 87 .fontWeight(FontWeight.Medium) 88 .margin({ top: 17, bottom: 17, left: 12, right: 92 }) 89 .fontSize(16) 90 .width(232) 91 .height(22) 92 .fontColor($r('app.color.text_color')) 93 } 94 .onClick(() => { 95 (async (): Promise<void> => { 96 try { 97 hilog.info(domain, TAG, 'Begin to start ability'); 98 let want: Want = { 99 bundleName: 'com.samples.famodelabilitydevelop', 100 moduleName: 'entry', 101 abilityName: 'com.samples.famodelabilitydevelop.PageAbilitySingleton' 102 }; 103 await featureAbility.startAbility({ want: want }); 104 hilog.info(domain, TAG, `Start ability succeed`); 105 } 106 catch (error) { 107 hilog.error(domain, TAG, 'Start ability failed with ' + error); 108 } 109 })() 110 }) 111 } 112 .height(56) 113 .backgroundColor($r('app.color.start_window_background')) 114 .borderRadius(24) 115 .margin({ top: 12, right: 12, left: 12 }) 116 117 ListItem() { 118 Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) { 119 Text($r('app.string.restart_singleton_button')) 120 .textAlign(TextAlign.Start) 121 .fontWeight(FontWeight.Medium) 122 .margin({ top: 17, bottom: 17, left: 12, right: 92 }) 123 .fontSize(16) 124 .width(232) 125 .height(22) 126 .fontColor($r('app.color.text_color')) 127 } 128 .onClick(() => { 129 (async (): Promise<void> => { 130 let wantInfo: Want = { 131 bundleName: 'com.samples.famodelabilitydevelop', 132 abilityName: 'com.samples.famodelabilitydevelop.PageAbilitySingleton', 133 parameters: { page: 'pages/second' } 134 }; 135 featureAbility.startAbility({ want: wantInfo }).then((data) => { 136 hilog.debug(domain, TAG, `restartAbility success : ${data}`); 137 }); 138 })() 139 }) 140 } 141 .height(56) 142 .backgroundColor($r('app.color.start_window_background')) 143 .borderRadius(24) 144 .margin({ top: 12, right: 12, left: 12 }) 145 146 ListItem() { 147 Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) { 148 Text($r('app.string.start_standard_first_button')) 149 .textAlign(TextAlign.Start) 150 .fontWeight(FontWeight.Medium) 151 .margin({ top: 17, bottom: 17, left: 12, right: 92 }) 152 .fontSize(16) 153 .width(232) 154 .height(22) 155 .fontColor($r('app.color.text_color')) 156 } 157 .onClick(() => { 158 let want: Want = { 159 bundleName: 'com.samples.famodelabilitydevelop', 160 abilityName: 'com.samples.famodelabilitydevelop.PageAbilityStandard', 161 parameters: { page: 'pages/first' } 162 }; 163 featureAbility.startAbility({ want: want }).then((data) => { 164 hilog.info(domain, TAG, `startAbility finish:${data}`); 165 }).catch((err: BusinessError) => { 166 hilog.info(domain, TAG, `startAbility failed errcode:${err.code}`); 167 }) 168 }) 169 } 170 .height(56) 171 .backgroundColor($r('app.color.start_window_background')) 172 .borderRadius(24) 173 .margin({ top: 12, right: 12, left: 12 }) 174 175 ListItem() { 176 Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) { 177 Text($r('app.string.start_standard_second_button')) 178 .textAlign(TextAlign.Start) 179 .fontWeight(FontWeight.Medium) 180 .margin({ top: 17, bottom: 17, left: 12, right: 92 }) 181 .fontSize(16) 182 .width(232) 183 .height(22) 184 .fontColor($r('app.color.text_color')) 185 } 186 .onClick(() => { 187 let want: Want = { 188 bundleName: 'com.samples.famodelabilitydevelop', 189 abilityName: 'com.samples.famodelabilitydevelop.PageAbilityStandard', 190 parameters: { page: 'pages/second' } 191 }; 192 featureAbility.startAbility({ want: want }).then((data) => { 193 hilog.info(domain, TAG, `startAbility finish:${data}`); 194 }).catch((err: BusinessError) => { 195 hilog.info(domain, TAG, `startAbility failed errcode:${err.code}`); 196 }) 197 }) 198 } 199 .height(56) 200 .backgroundColor($r('app.color.start_window_background')) 201 .borderRadius(24) 202 .margin({ top: 12, right: 12, left: 12 }) 203 204 ListItem() { 205 Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) { 206 Text($r('app.string.stop_pageAbility_button')) 207 .textAlign(TextAlign.Start) 208 .fontWeight(FontWeight.Medium) 209 .margin({ top: 17, bottom: 17, left: 12, right: 92 }) 210 .fontSize(16) 211 .width(232) 212 .height(22) 213 .fontColor($r('app.color.text_color')) 214 } 215 .onClick(() => { 216 (async (): Promise<void> => { 217 try { 218 hilog.info(domain, TAG, 'Begin to terminateSelf'); 219 await featureAbility.terminateSelf(); 220 hilog.info(domain, TAG, 'terminateSelf succeed'); 221 } catch (error) { 222 hilog.error(domain, TAG, 'terminateSelf failed with ' + error); 223 } 224 })() 225 }) 226 } 227 .height(56) 228 .backgroundColor($r('app.color.start_window_background')) 229 .borderRadius(24) 230 .margin({ top: 12, right: 12, left: 12 }) 231 232 ListItem() { 233 Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) { 234 Text($r('app.string.worker_button')) 235 .textAlign(TextAlign.Start) 236 .fontWeight(FontWeight.Medium) 237 .margin({ top: 17, bottom: 17, left: 12, right: 92 }) 238 .fontSize(16) 239 .width(232) 240 .height(22) 241 .fontColor($r('app.color.text_color')) 242 } 243 .onClick(() => { 244 let wk = new worker.ThreadWorker('../workers/worker.ts'); 245 246 // 发送消息到worker线程 247 wk.postMessage('message from main thread.') 248 249 // 处理来自worker线程的消息 250 wk.onmessage = (message): void => { 251 promptAction.showToast({ 252 message: JSON.stringify(message) 253 }); 254 hilog.info(domain, TAG, 'message from worker: ' + JSON.stringify(message)); 255 256 // 根据业务按需停止worker线程 257 wk.terminate(); 258 } 259 }) 260 } 261 .height(56) 262 .backgroundColor($r('app.color.start_window_background')) 263 .borderRadius(24) 264 .margin({ top: 12, right: 12, left: 12 }) 265 } 266 .height('100%') 267 .backgroundColor($r('app.color.backGrounding')) 268 } 269 .width('100%') 270 .margin({ top: 8 }) 271 .backgroundColor($r('app.color.backGrounding')) 272 } 273}