From a63ef8570d667e5643db0893824090efc90295f1 Mon Sep 17 00:00:00 2001 From: zhangyuming27 Date: Tue, 16 Sep 2025 16:27:01 +0800 Subject: [PATCH 1/2] try-catch rectification --- .../main/ets/entryability/EntryAbility.ets | 12 +++++- entry/src/main/ets/pages/CommentPage.ets | 34 +++++++++------- entry/src/main/ets/utils/WindowUtil.ets | 40 +++++++++++++++---- 3 files changed, 63 insertions(+), 23 deletions(-) diff --git a/entry/src/main/ets/entryability/EntryAbility.ets b/entry/src/main/ets/entryability/EntryAbility.ets index fcbb37b..76e9a83 100644 --- a/entry/src/main/ets/entryability/EntryAbility.ets +++ b/entry/src/main/ets/entryability/EntryAbility.ets @@ -61,7 +61,11 @@ export default class EntryAbility extends UIAbility { // Explicit invocation of page restore this.contentStorage = new LocalStorage(); Logger.info('Ability onCreate restoreWindowStage'); - this.context.restoreWindowStage(this.contentStorage); + try { + this.context.restoreWindowStage(this.contentStorage); + } catch (error) { + Logger.error('Failed restore window stage. Cause: ' + JSON.stringify(error)); + } } } @@ -87,7 +91,11 @@ export default class EntryAbility extends UIAbility { // Explicit invocation of page restore this.contentStorage = new LocalStorage(); Logger.info('Ability onNewWant restoreWindowStage'); - this.context.restoreWindowStage(this.contentStorage); + try { + this.context.restoreWindowStage(this.contentStorage); + } catch (error) { + Logger.error('Failed to restore window stage. Cause: ' + JSON.stringify(error)); + } } } diff --git a/entry/src/main/ets/pages/CommentPage.ets b/entry/src/main/ets/pages/CommentPage.ets index fa4340f..41f68cb 100644 --- a/entry/src/main/ets/pages/CommentPage.ets +++ b/entry/src/main/ets/pages/CommentPage.ets @@ -25,22 +25,28 @@ import Logger from '../utils/Logger'; function getRemoteDeviceId(targetDevice: string): string | undefined { let dmClass = distributedDeviceManager.createDeviceManager('com.example.codelab'); if (typeof dmClass === 'object' && dmClass !== null) { - let list = dmClass.getAvailableDeviceListSync(); - if (typeof (list) === 'undefined' || typeof (list.length) === 'undefined') { - Logger.error('getRemoteDeviceId err: list is null'); - return; - } - if (list.length === 0) { - Logger.error('getRemoteDeviceId err: list is null'); - return; - } - for (const device of list) { - if (targetDevice !== '' && device.networkId === targetDevice) { - Logger.info(device.networkId as string); - return device.networkId; + try { + let list = dmClass.getAvailableDeviceListSync(); + if (typeof (list) === 'undefined' || typeof (list.length) === 'undefined') { + Logger.error('getRemoteDeviceId err: list is null'); + return; + } + if (list.length === 0) { + Logger.error('getRemoteDeviceId err: list is null'); + return; } + for (const device of list) { + if (targetDevice !== '' && device.networkId === targetDevice) { + Logger.info(device.networkId as string); + return device.networkId; + } + } + return list[0].networkId; + } catch (error) { + Logger.error('getRemoteDeviceId err: list is null. Cause: ' + JSON.stringify(error)); + return; } - return list[0].networkId; + } else { Logger.error('getRemoteDeviceId err: dmClass is null'); return; diff --git a/entry/src/main/ets/utils/WindowUtil.ets b/entry/src/main/ets/utils/WindowUtil.ets index 1b570d8..165aa4a 100644 --- a/entry/src/main/ets/utils/WindowUtil.ets +++ b/entry/src/main/ets/utils/WindowUtil.ets @@ -20,14 +20,22 @@ import Logger from './Logger'; const TAG: string = '[WindowUtil]'; + + export class WindowUtil { public static updateStatusBarColor(context: common.BaseContext, isDark: boolean): void { window.getLastWindow(context).then((windowClass: window.Window) => { try { - windowClass.setWindowSystemBarProperties({ statusBarContentColor: isDark ? '#FFFFFF' : '#000000' }); + windowClass.setWindowSystemBarProperties({ statusBarContentColor: isDark ? '#FFFFFF' : '#000000' }) + .catch((error: Error) => { + Logger.error(TAG, 'Failed to set the system bar properties. Cause: ' + JSON.stringify(error)); + }); } catch (exception) { Logger.error(TAG, 'Failed to set the system bar properties. Cause: ' + JSON.stringify(exception)); } + }) + .catch((error: Error) => { + Logger.error(TAG, 'Failed to get last window. Cause: ' + JSON.stringify(error)); }); } @@ -67,12 +75,30 @@ export class WindowUtil { static getDeviceSize(context: Context, area: window.AvoidArea, naviBarArea: window.AvoidArea): void { // Get device height window.getLastWindow(context).then((data: window.Window) => { - let properties = data.getWindowProperties(); - AppStorage.setOrCreate('statusBarHeight', data.getUIContext().px2vp(area.topRect.height)); - AppStorage.setOrCreate('naviIndicatorHeight', data.getUIContext().px2vp(naviBarArea.bottomRect.height)); - AppStorage.setOrCreate('deviceHeight', data.getUIContext().px2vp(properties.windowRect.height)); - AppStorage.setOrCreate('deviceWidth', data.getUIContext().px2vp(properties.windowRect.width)); - }); + let properties: window.WindowProperties; + try { + properties = data.getWindowProperties(); + AppStorage.setOrCreate('statusBarHeight', WindowUtil.getDataUIContext(data, area.topRect.height)); + AppStorage.setOrCreate('naviIndicatorHeight', WindowUtil.getDataUIContext(data, naviBarArea.bottomRect.height)); + AppStorage.setOrCreate('deviceHeight', WindowUtil.getDataUIContext(data, properties.windowRect.height)); + AppStorage.setOrCreate('deviceWidth', WindowUtil.getDataUIContext(data, properties.windowRect.width)); + } catch (exception) { + Logger.error(TAG, 'Failed to get Last Window. Cause: ' + JSON.stringify(exception)); + } + }).catch((exception: Error)=> { + Logger.error(TAG, 'Failed to get Last Window. Cause: ' + JSON.stringify(exception)); + }) + ; + } + + static getDataUIContext(data: window.Window, px2vp: number): number { + let newValue = 0; + try { + newValue = data.getUIContext().px2vp(px2vp) + } catch(exception) { + Logger.error(TAG, 'Failed to get UI context. Cause: ' + JSON.stringify(exception)); + } + return newValue; } static setMainWindowRestricted(context: Context): void { -- Gitee From 18f4aec5fa9c737da031cb1834fde67bb479abc0 Mon Sep 17 00:00:00 2001 From: zhangyuming27 Date: Wed, 17 Sep 2025 17:28:44 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20IDE=E6=8E=A5=E5=8F=A3=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entry/src/main/ets/entryability/EntryAbility.ets | 2 +- entry/src/main/ets/utils/WindowUtil.ets | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/entry/src/main/ets/entryability/EntryAbility.ets b/entry/src/main/ets/entryability/EntryAbility.ets index 76e9a83..b4e9791 100644 --- a/entry/src/main/ets/entryability/EntryAbility.ets +++ b/entry/src/main/ets/entryability/EntryAbility.ets @@ -64,7 +64,7 @@ export default class EntryAbility extends UIAbility { try { this.context.restoreWindowStage(this.contentStorage); } catch (error) { - Logger.error('Failed restore window stage. Cause: ' + JSON.stringify(error)); + Logger.error('Failed to restore window stage. Cause: ' + JSON.stringify(error)); } } } diff --git a/entry/src/main/ets/utils/WindowUtil.ets b/entry/src/main/ets/utils/WindowUtil.ets index 165aa4a..7b5f1c1 100644 --- a/entry/src/main/ets/utils/WindowUtil.ets +++ b/entry/src/main/ets/utils/WindowUtil.ets @@ -108,7 +108,11 @@ export class WindowUtil { return; } // Setting restricted display - windowClass.setPreferredOrientation(window.Orientation.AUTO_ROTATION_RESTRICTED); + windowClass.setPreferredOrientation(window.Orientation.AUTO_ROTATION_RESTRICTED).catch((exception: Error) => { + Logger.error(TAG, 'Failed to set orientation. Cause: ' + JSON.stringify(exception)); + }); + }).catch((exception: Error) => { + Logger.error(TAG, 'Failed to set main window. Cause: ' + JSON.stringify(exception)); }); } @@ -119,7 +123,11 @@ export class WindowUtil { return; } // Setting restricted display - windowClass.setPreferredOrientation(window.Orientation.PORTRAIT); + windowClass.setPreferredOrientation(window.Orientation.PORTRAIT).catch((exception: Error) => { + Logger.error(TAG, 'Failed to set orientation. Cause: ' + JSON.stringify(exception)); + }); + }).catch((exception: Error) => { + Logger.error(TAG, 'Failed to set main window. Cause: ' + JSON.stringify(exception)); }); } -- Gitee