diff --git a/entry/src/main/ets/entryability/EntryAbility.ets b/entry/src/main/ets/entryability/EntryAbility.ets index fcbb37b1da010d9ea6700392d527327391454871..b4e9791db7617af4d0679feadd2849048bedd250 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 to 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 fa4340fd0e2fdc8c21051888805c2a3423d24464..41f68cb02de9487e84f6cc23f619632458da3d71 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 1b570d8553d088e38a900ae782b544201393f29b..7b5f1c1dfe9f3199a46fcdba313c52e1115302d2 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 { @@ -82,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)); }); } @@ -93,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)); }); }