diff --git a/ArkTS/entry/src/main/ets/pages/MyDescriptorCom.ets b/ArkTS/entry/src/main/ets/pages/MyDescriptorCom.ets index a8ee90f478b15d4c5b60501d443442c5f87e5b74..7b3e203676784b3c69ad7814d276953cfef633aa 100644 --- a/ArkTS/entry/src/main/ets/pages/MyDescriptorCom.ets +++ b/ArkTS/entry/src/main/ets/pages/MyDescriptorCom.ets @@ -22,7 +22,7 @@ function MyDescriptor(target: Object, key: string, descriptor: PropertyDescripto const originalMethod: Function = descriptor.value descriptor.value = (...args: Object[]) => { // Get the name, input parameters, and return value of the decorated method - console.log(`Calling ${target.constructor.name} method ${key} with argument: ${args}`) + console.log(`Calling ${target.constructor?.name} method ${key} with argument: ${args}`) const result: Object = originalMethod(...args) console.log(`Method ${key} returned: ${result}`) return result diff --git a/ArkUI/entry/src/main/ets/entryability/EntryAbilityScreenBrightness.ets b/ArkUI/entry/src/main/ets/entryability/EntryAbilityScreenBrightness.ets new file mode 100644 index 0000000000000000000000000000000000000000..1338cae95e4c4dca7d881fefea9014aa0bf82026 --- /dev/null +++ b/ArkUI/entry/src/main/ets/entryability/EntryAbilityScreenBrightness.ets @@ -0,0 +1,66 @@ +/* +* Copyright (c) 2024 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* +* FAQ:如何获取与设置屏幕亮度 +*/ + +import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +const DOMAIN = 0x0000; + +export default class EntryAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy'); + } + // [Start windowClass] + onWindowStageCreate(windowStage: window.WindowStage): void { + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + let windowClass = windowStage.getMainWindowSync(); + AppStorage.setOrCreate('windowClass',windowClass); + // [StartExclude windowClass] + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err)); + return; + } + hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.'); + }); + // [EndExclude windowClass] + } + // [End windowClass] + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground'); + } +} \ No newline at end of file diff --git a/ArkUI/entry/src/main/ets/pages/MockOnclick.ets b/ArkUI/entry/src/main/ets/pages/MockOnclick.ets index bebb319e5068690b9b53aaffb85cce529dfd6898..3a955870ab5334ee1f9a948e6dd7bd908861734f 100644 --- a/ArkUI/entry/src/main/ets/pages/MockOnclick.ets +++ b/ArkUI/entry/src/main/ets/pages/MockOnclick.ets @@ -63,9 +63,11 @@ class MyNodeController extends NodeController { return false; } let node: FrameNode | null = this.rootNode.getFrameNode(); + if (node == null) { + return false; + } let offsetX: number | null | undefined = node?.getPositionToParent().x; let offsetY: number | null | undefined = node?.getPositionToParent().y; - let changedTouchLen = event.changedTouches.length; for (let i = 0; i < changedTouchLen; i++) { if (offsetX != null && offsetY != null && offsetX != undefined && offsetY != undefined) { diff --git a/ArkUI/entry/src/main/ets/pages/ScreenBrightness.ets b/ArkUI/entry/src/main/ets/pages/ScreenBrightness.ets new file mode 100644 index 0000000000000000000000000000000000000000..61e013b542c6a58fca855589f72f0371515c47c8 --- /dev/null +++ b/ArkUI/entry/src/main/ets/pages/ScreenBrightness.ets @@ -0,0 +1,119 @@ +/* +* Copyright (c) 2024 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* +* FAQ:如何获取与设置屏幕亮度 +*/ + +import { promptAction, window } from '@kit.ArkUI'; +import { BusinessError, settings } from '@kit.BasicServicesKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; + +const TAG: string = 'brightness'; + +@Entry +@Component +struct Index { + @State settingsBrightness: string = ''; + @State windowBrightness: number = 0; + private context: Context = this.getUIContext().getHostContext() as Context; + private windowClass: window.Window = AppStorage.get('windowClass') as window.Window; + + build() { + Column() { + Column() { + Text('通过settings模块实现') + Column() { + Text('当前屏幕亮度' + this.settingsBrightness + '(范围0-255)') + // [Start getBrightnessOne] + Button('获取屏幕亮度') + .width(328) + .margin({ + top: 16, + bottom:16 + }) + .onClick(() => { + // Get screen brightness through the getValueSync() method. + this.settingsBrightness = settings.getValueSync(this.context, settings.display.SCREEN_BRIGHTNESS_STATUS, 10); + }) + // [End getBrightnessOne] + Button('设置屏幕亮度') + .width(328) + .onClick(() => { + // settings.setValue() is only available for system applications. + promptAction.showToast({ + message: 'settings.setValue方法仅系统应用使用' + }); + }) + } + } + .margin({ + bottom: 40 + }) + + Column() { + Text('通过window模块实现') + Column() { + Text('当前屏幕亮度' + this.windowBrightness + '(范围0-1)') + // [Start getBrightnessTwo] + Button('获取屏幕亮度') + .width(328) + .margin({ + top: 16, + bottom:16 + }) + .onClick(() => { + try { + // By retrieving window properties using getWindowProperties(), the screen brightness can be obtained. + let properties = this.windowClass?.getWindowProperties(); + this.windowBrightness = properties?.brightness ?? -1; + } catch (exception) { + hilog.error(0x0000, TAG, + `Failed to obtain the window properties. Cause code: ${exception.code}, message: ${exception.message}`); + } + }) + // [End getBrightnessTwo] + // [Start setBrightnessTwo] + Button('设置屏幕亮度') + .width(328) + .onClick(() => { + try { + this.windowBrightness = Math.random() * (1.0 - 0.0) + 0.0; + if (this.windowBrightness < 0 || this.windowBrightness > 1) { + hilog.error(0x0000, TAG, `WindowBrightness is not within the valid range`); + return; + } + this.windowClass?.setWindowBrightness(this.windowBrightness, (err: BusinessError) => { + const errCode: number = err.code; + if (errCode) { + hilog.error(0x0000, TAG, + `Failed to set the brightness. Cause code: ${err.code}, message: ${err.message}`); + return; + } + hilog.info(0x0000, TAG, 'Succeeded in setting the brightness.'); + }); + } catch (exception) { + hilog.error(0x0000, TAG, + `Failed to set the brightness. Cause code: ${exception.code}, message: ${exception.message}`); + } + }) + // [End setBrightnessTwo] + } + } + } + .height('100%') + .width('100%') + } +} \ No newline at end of file