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 { common } from '@kit.AbilityKit'; 17import { hilog } from '@kit.PerformanceAnalysisKit'; 18import { font, promptAction } from '@kit.ArkUI'; 19 20const TAG: string = 'ApplicationContext'; 21const DOMAIN: number = 0xFF00; 22 23@Entry 24@Component 25struct ApplicationContext { 26 private promptDuration: number = 2000; 27 28 build() { 29 Column() { 30 Row() { 31 Flex({ justifyContent: FlexAlign.Start, alignContent: FlexAlign.Center }) { 32 Text($r('app.string.ApplicationContext')) 33 .fontSize(30) 34 .fontWeight(700) 35 .textAlign(TextAlign.Start) 36 .margin({ 37 top: 8, 38 bottom: 8, 39 left: 12 40 }) 41 } 42 } 43 .width('100%') 44 .height('14.36%') 45 .justifyContent(FlexAlign.Start) 46 .backgroundColor($r('app.color.backGrounding')) 47 48 List({ initialIndex: 0 }) { 49 ListItem() { 50 Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) { 51 Text($r('app.string.ApplicationContext_getCurrentAppCloneIndex')) 52 .textAlign(TextAlign.Start) 53 .fontWeight(500) 54 .margin({ 55 top: 17, 56 bottom: 17, 57 left: 12 58 }) 59 .fontSize(16) 60 .width('100%') 61 .height('39.29%') 62 .fontColor($r('app.color.text_color')) 63 } 64 .id('getCurrentAppCloneIndex') 65 .onClick(() => { 66 hilog.info(DOMAIN, TAG, 'ApplicationContext.getCurrentAppCloneIndex begin.'); 67 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; 68 let applicationContext = context.getApplicationContext(); 69 try { 70 let index = applicationContext.getCurrentAppCloneIndex(); 71 let msg = `getCurrentAppCloneIndex successfully, index: ${index}`; 72 hilog.info(DOMAIN, TAG, msg); 73 promptAction.showToast({ 74 message: msg, 75 duration: this.promptDuration 76 }) 77 } catch (error) { 78 hilog.info(DOMAIN, TAG, 79 `ApplicationContext.getCurrentAppCloneIndex fail, error: ${JSON.stringify(error)}`); 80 } 81 hilog.info(DOMAIN, TAG, 'ApplicationContext.getCurrentAppCloneIndex succeed.'); 82 }) 83 } 84 .height('8.45%') 85 .backgroundColor($r('app.color.start_window_background')) 86 .borderRadius(24) 87 .margin({ top: 12, right: 12, left: 12 }) 88 89 ListItem() { 90 Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) { 91 Text($r('app.string.ApplicationContext_getRunningProcessInformation_callback')) 92 .textAlign(TextAlign.Start) 93 .fontWeight(500) 94 .margin({ 95 top: 17, 96 bottom: 17, 97 left: 12 98 }) 99 .fontSize(16) 100 .width('100%') 101 .height('39.29%') 102 .fontColor($r('app.color.text_color')) 103 } 104 .id('getRunningProcessInformationCallback') 105 .onClick(() => { 106 hilog.info(DOMAIN, TAG, 'ApplicationContext.getRunningProcessInformation callback begin.'); 107 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; 108 let applicationContext = context.getApplicationContext(); 109 110 try { 111 applicationContext.getRunningProcessInformation((err, data) => { 112 let msg = `getRunningProcessInformation callback successfully, data: ${JSON.stringify(data)}`; 113 hilog.info(DOMAIN, TAG, msg); 114 promptAction.showToast({ 115 message: msg, 116 duration: this.promptDuration 117 }) 118 }) 119 } catch (err) { 120 hilog.info(DOMAIN, TAG, 121 `ApplicationContext.getRunningProcessInformation callback fail, error: ${JSON.stringify(err)}`); 122 } 123 hilog.info(DOMAIN, TAG, 'ApplicationContext.getRunningProcessInformation callback succeed.'); 124 }) 125 } 126 .height('8.45%') 127 .backgroundColor($r('app.color.start_window_background')) 128 .borderRadius(24) 129 .margin({ top: 12, right: 12, left: 12 }) 130 131 ListItem() { 132 Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) { 133 Text($r('app.string.ApplicationContext_getRunningProcessInformation_promise')) 134 .textAlign(TextAlign.Start) 135 .fontWeight(500) 136 .margin({ 137 top: 17, 138 bottom: 17, 139 left: 12 140 }) 141 .fontSize(16) 142 .width('100%') 143 .height('39.29%') 144 .fontColor($r('app.color.text_color')) 145 } 146 .id('getRunningProcessInformationPromise') 147 .onClick(() => { 148 hilog.info(DOMAIN, TAG, 'ApplicationContext.getRunningProcessInformation promise begin.'); 149 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; 150 let applicationContext = context.getApplicationContext(); 151 152 try { 153 applicationContext.getRunningProcessInformation().then((data) => { 154 let msg = `getRunningProcessInformation promise successfully, data: ${JSON.stringify(data)}`; 155 hilog.info(DOMAIN, TAG, msg); 156 promptAction.showToast({ 157 message: msg, 158 duration: this.promptDuration 159 }) 160 }) 161 } catch (err) { 162 hilog.info(DOMAIN, TAG, 163 `ApplicationContext.getRunningProcessInformation promise fail, error: ${JSON.stringify(err)}`); 164 } 165 hilog.info(DOMAIN, TAG, 'ApplicationContext.getRunningProcessInformation promise succeed.'); 166 }) 167 } 168 .height('8.45%') 169 .backgroundColor($r('app.color.start_window_background')) 170 .borderRadius(24) 171 .margin({ top: 12, right: 12, left: 12 }) 172 173 ListItem() { 174 Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) { 175 Text($r('app.string.ApplicationContext_setFont')) 176 .textAlign(TextAlign.Start) 177 .fontWeight(500) 178 .margin({ 179 top: 17, 180 bottom: 17, 181 left: 12 182 }) 183 .fontSize(16) 184 .width('100%') 185 .height('39.29%') 186 .fontColor($r('app.color.text_color')) 187 } 188 .id('setFont') 189 .onClick(() => { 190 font.registerFont({ 191 familyName: 'fontName', 192 familySrc: $rawfile('font/Roboto-Regular.ttf') 193 }) 194 195 hilog.info(DOMAIN, TAG, 'ApplicationContext.setFont begin.'); 196 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; 197 let applicationContext = context.getApplicationContext(); 198 199 try { 200 applicationContext.setFont('fontName'); 201 let msg = `setFont successfully`; 202 hilog.info(DOMAIN, TAG, msg); 203 promptAction.showToast({ 204 message: msg, 205 duration: this.promptDuration 206 }) 207 } catch (err) { 208 hilog.info(DOMAIN, TAG, 209 `ApplicationContext.setFont fail, error: ${JSON.stringify(err)}`); 210 } 211 hilog.info(DOMAIN, TAG, 'ApplicationContext.setFont succeed.'); 212 }) 213 } 214 .height('8.45%') 215 .backgroundColor($r('app.color.start_window_background')) 216 .borderRadius(24) 217 .margin({ top: 12, right: 12, left: 12 }) 218 } 219 .height('86%') 220 .backgroundColor($r('app.color.backGrounding')) 221 } 222 .height('100%') 223 .width('100%') 224 } 225}