diff --git a/FoldableGuilde/entry/src/main/ets/modules/AsideColum.ets b/FoldableGuilde/entry/src/main/ets/modules/AsideColum.ets index 1b9587e3a3d24be72e78af0b7c2651689e048afa..a0d1f62525c7b542c244f6501911d7191d60b14a 100644 --- a/FoldableGuilde/entry/src/main/ets/modules/AsideColum.ets +++ b/FoldableGuilde/entry/src/main/ets/modules/AsideColum.ets @@ -1,4 +1,3 @@ -@Entry @Component export struct AsideColumn { @State l_width: string = '100%' diff --git a/FoldableGuilde/entry/src/main/ets/modules/PinchText.ets b/FoldableGuilde/entry/src/main/ets/modules/PinchText.ets index 6894b274026b9deef20504ada07725452751f6fe..efdc8293604724af62bdfd172fb58f01042ef802 100644 --- a/FoldableGuilde/entry/src/main/ets/modules/PinchText.ets +++ b/FoldableGuilde/entry/src/main/ets/modules/PinchText.ets @@ -1,4 +1,3 @@ -@Entry @Component export struct PinchText { text: string = '亲近大海,领悟海,体会到了海的心声,海自然与你共鸣,无需可以强求,也无需费力搜寻,海,始终都会以潮声与你相亲,已其海色伴你舒适。' diff --git a/FoldableGuilde/entry/src/main/ets/modules/Screen.ets b/FoldableGuilde/entry/src/main/ets/modules/Screen.ets index f773a5f3cefeb791eb8946588e493024cb9a5009..3f8eb3b693a28fae2eb94288ba71edfc7972eebf 100644 --- a/FoldableGuilde/entry/src/main/ets/modules/Screen.ets +++ b/FoldableGuilde/entry/src/main/ets/modules/Screen.ets @@ -2,7 +2,6 @@ import { display, window } from '@kit.ArkUI' import { common } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; -@Entry @Component export struct Movie { @State isFoldable: Boolean = false @@ -22,27 +21,38 @@ export struct Movie { // 如果使用Navigation路由跳转,需要在NavDestination的onShown()和onHidden()事件中注册监听 onPageShow(): void { // 获取应用主窗口 - let promise = window.getLastWindow(this.context); - promise.then((data: window.Window) => { + window.getLastWindow(this.context).then((data: window.Window) => { this.mainWindow = data; console.info('Succeeded in obtaining the top window. Data: ' + JSON.stringify(data)); - let foldStatus: display.FoldStatus = display.getFoldStatus(); - if (foldStatus === display.FoldStatus.FOLD_STATUS_FOLDED) { - // 当前折叠状态为折叠态,设置窗口横屏显示 - this.mainWindow?.setPreferredOrientation(window.Orientation.AUTO_ROTATION_LANDSCAPE); - } else if (foldStatus === display.FoldStatus.FOLD_STATUS_EXPANDED) { - // 如果折叠状态为展开态,设置窗口跟随传感器自动旋转,且受控制中心的旋转开关控制 - this.mainWindow?.setPreferredOrientation(window.Orientation.AUTO_ROTATION_RESTRICTED); + try { + let foldStatus: display.FoldStatus = display.getFoldStatus(); + if (foldStatus === display.FoldStatus.FOLD_STATUS_FOLDED) { + // 当前折叠状态为折叠态,设置窗口横屏显示 + this.mainWindow?.setPreferredOrientation(window.Orientation.AUTO_ROTATION_LANDSCAPE).catch((err: BusinessError) => { + console.error(`Failed to set AUTO_ROTATION_LANDSCAPE. Cause code: ${err.code}, message: ${err.message}`); + }); + } else if (foldStatus === display.FoldStatus.FOLD_STATUS_EXPANDED) { + // 如果折叠状态为展开态,设置窗口跟随传感器自动旋转,且受控制中心的旋转开关控制 + this.mainWindow?.setPreferredOrientation(window.Orientation.AUTO_ROTATION_RESTRICTED).catch((err: BusinessError) => { + console.error(`Failed to set AUTO_ROTATION_RESTRICTED. Cause code: ${err.code}, message: ${err.message}`); + }); + } + // 监听折叠屏折叠状态 + display.on('foldStatusChange', this.onDetailFoldStatusChange); + } catch (err) { + console.error(`Failed to obtain the fold status. Cause code: ${err.code}, message: ${err.message}`); } - // 监听折叠屏折叠状态 - display.on('foldStatusChange', this.onDetailFoldStatusChange); }).catch((err: BusinessError) => { console.error(`Failed to obtain the top window. Cause code: ${err.code}, message: ${err.message}`); }); } onPageHide(): void { - display.off('foldStatusChange'); + try { + display.off('foldStatusChange'); + } catch (err) { + console.error(`Failed to cancel monitoring the fold status. Cause code: ${err.code}, message: ${err.message}`); + } } build() { diff --git a/FoldableGuilde/entry/src/main/ets/modules/Screen2.ets b/FoldableGuilde/entry/src/main/ets/modules/Screen2.ets index 4868f48c00c5b303d78223e27ead5b04771bd178..eae30280e876e114c7e0ad1dc11b183dd286e621 100644 --- a/FoldableGuilde/entry/src/main/ets/modules/Screen2.ets +++ b/FoldableGuilde/entry/src/main/ets/modules/Screen2.ets @@ -2,7 +2,6 @@ import { display } from '@kit.ArkUI' import { WindowUtil } from '../utils/WindowUtil'; -@Entry @Component export struct FoldableScreen { @State isFoldable: Boolean = false @@ -14,14 +13,18 @@ export struct FoldableScreen { this.windowUtil?.setPhoneAutoRotation(); // 实时监听设备折叠状态 - display.on('foldStatusChange', (data: display.FoldStatus) => { - console.info('实时监听设备折叠状态: ' + JSON.stringify(data)); - if (data === display.FoldStatus.FOLD_STATUS_HALF_FOLDED) { - this.windowUtil?.setPhoneLandscape() - } else if (data === display.FoldStatus.FOLD_STATUS_EXPANDED) { - this.windowUtil?.setPhoneAutoRotation(); - } - }); + try { + display.on('foldStatusChange', (data: display.FoldStatus) => { + console.info('Monitor the folding status of the device in real-time: ' + JSON.stringify(data)); + if (data === display.FoldStatus.FOLD_STATUS_HALF_FOLDED) { + this.windowUtil?.setPhoneLandscape() + } else if (data === display.FoldStatus.FOLD_STATUS_EXPANDED) { + this.windowUtil?.setPhoneAutoRotation(); + } + }); + } catch (err) { + console.error(`Failed to monitor the folding status of the device. Cause code: ${err.code}, message: ${err.message}`); + } } build() { diff --git a/FoldableGuilde/entry/src/main/ets/modules/Scroll.ets b/FoldableGuilde/entry/src/main/ets/modules/Scroll.ets index 3bcb9181795efc2c45d92d8fd43b8e0eaca6154a..0e2c442e776a197622338b7e22fd8fb21bab7b62 100644 --- a/FoldableGuilde/entry/src/main/ets/modules/Scroll.ets +++ b/FoldableGuilde/entry/src/main/ets/modules/Scroll.ets @@ -1,4 +1,3 @@ -@Entry @Component export struct ScrollComponent { @State list: string[] = new Array(100).fill('') diff --git a/FoldableGuilde/entry/src/main/ets/modules/ScrollParent.ets b/FoldableGuilde/entry/src/main/ets/modules/ScrollParent.ets index a776cd27be7d7902133812cd4ae24fda32373676..62bb8ef7f29a2461780e4fc4cbcf44e88e8b3169 100644 --- a/FoldableGuilde/entry/src/main/ets/modules/ScrollParent.ets +++ b/FoldableGuilde/entry/src/main/ets/modules/ScrollParent.ets @@ -1,6 +1,5 @@ import web_webview from '@ohos.web.webview'; -@Entry @Component export struct NestedScroll { private scrollerForScroll: Scroller = new Scroller() diff --git a/FoldableGuilde/entry/src/main/ets/pages/Index.ets b/FoldableGuilde/entry/src/main/ets/pages/Index.ets index e85cfc24d15260e31e9c6a5572664a910507cd15..6c46c287cf8dbfb129ebd35af9a0a5544230f460 100644 --- a/FoldableGuilde/entry/src/main/ets/pages/Index.ets +++ b/FoldableGuilde/entry/src/main/ets/pages/Index.ets @@ -18,33 +18,51 @@ import { deviceInfo } from '@kit.BasicServicesKit' struct Index { // Monitor the change of folding state to obtain the current width and height of the screen onPageShow(): void { - // [Start foldDisplayModeChange] - display.on('foldDisplayModeChange', (data: display.FoldDisplayMode) => { - let displayInfo: display.Display = display.getDefaultDisplaySync(); - if (data === display.FoldDisplayMode.FOLD_DISPLAY_MODE_FULL) { - console.info('当前屏幕状态:全屏显示'); - console.info('屏幕宽度: ' + displayInfo.width); - console.info('屏幕高度: ' + displayInfo.height); - } else if (data === display.FoldDisplayMode.FOLD_DISPLAY_MODE_MAIN) { - console.info('当前屏幕状态:主屏幕显示'); - console.info('屏幕宽度: ' + displayInfo.width); - console.info('屏幕高度: ' + displayInfo.height); - } - }); - // [End foldDisplayModeChange] + try { + // [Start foldDisplayModeChange] + display.on('foldDisplayModeChange', (data: display.FoldDisplayMode) => { + let displayInfo: display.Display = display.getDefaultDisplaySync(); + if (data === display.FoldDisplayMode.FOLD_DISPLAY_MODE_FULL) { + console.info('当前屏幕状态:全屏显示'); + console.info('屏幕宽度: ' + displayInfo.width); + console.info('屏幕高度: ' + displayInfo.height); + } else if (data === display.FoldDisplayMode.FOLD_DISPLAY_MODE_MAIN) { + console.info('当前屏幕状态:主屏幕显示'); + console.info('屏幕宽度: ' + displayInfo.width); + console.info('屏幕高度: ' + displayInfo.height); + } + }); + // [End foldDisplayModeChange] + } catch (err) { + console.error(`Failed to monitor the fold display mode. Cause code: ${err.code}, message: ${err.message}`); + } } judgeDevice() { // [Start judgeDevice] // 1. The device type is phone, and it supports folding - if (deviceInfo.deviceType === 'phone' && display.isFoldable()) {} + try { + if (deviceInfo.deviceType === 'phone' && display.isFoldable()) { + } + } catch (err) { + console.error(`Failed to obtain the fold status of phone. Cause code: ${err.code}, message: ${err.message}`); + } // 2. Judging whether the current folding state is an expanded state/folded state/semi-folded state - if (display.getFoldStatus() === display.FoldStatus.FOLD_STATUS_EXPANDED) {} + try { + if (display.getFoldStatus() === display.FoldStatus.FOLD_STATUS_EXPANDED) { + } + } catch (err) { + console.error(`Failed to obtain the fold status of phone. Cause code: ${err.code}, message: ${err.message}`); + } // [End judgeDevice] } onPageHide(): void { - display.off('foldDisplayModeChange'); + try { + display.off('foldDisplayModeChange'); + } catch (err) { + console.error(`Failed to cancel monitoring the fold display mode. Cause code: ${err.code}, message: ${err.message}`); + } } build() { diff --git a/FoldableGuilde/entry/src/main/ets/utils/WindowUtil.ets b/FoldableGuilde/entry/src/main/ets/utils/WindowUtil.ets index 2d0c5c28312d13fd3fce78df3d44354534f11543..4e38be8ddac0495033aeb3221d71e3aa46615521 100644 --- a/FoldableGuilde/entry/src/main/ets/utils/WindowUtil.ets +++ b/FoldableGuilde/entry/src/main/ets/utils/WindowUtil.ets @@ -1,5 +1,6 @@ // utils/WindowsUtil.ets import { window } from '@kit.ArkUI'; +import { BusinessError } from '@kit.BasicServicesKit'; @Observed export class WindowUtil { @@ -19,6 +20,9 @@ export class WindowUtil { return; } this.windowStage.getMainWindow((err, windowClass: window.Window) => { + if (err) { + console.error(`Failed to get the main window. Cause code: ${err.code}, message: ${err.message}`); + } this.mainWindowClass = windowClass; }); } @@ -28,7 +32,9 @@ export class WindowUtil { return; } // 设置传感器自动旋转模式 - this.mainWindowClass.setPreferredOrientation(window.Orientation.AUTO_ROTATION); + this.mainWindowClass.setPreferredOrientation(window.Orientation.AUTO_ROTATION).catch((err: BusinessError) => { + console.error(`Failed to set phone's screen auto rotation. Cause code: ${err.code}, message: ${err.message}`); + }); } setPhoneLandscape(): void { @@ -36,7 +42,9 @@ export class WindowUtil { return; } // 设置横屏显示模式 - this.mainWindowClass.setPreferredOrientation(window.Orientation.LANDSCAPE); + this.mainWindowClass.setPreferredOrientation(window.Orientation.LANDSCAPE).catch((err: BusinessError) => { + console.error(`Failed to set phone's screen landscape. Cause code: ${err.code}, message: ${err.message}`); + }); } setWindowStage(windowStage: window.WindowStage): void { diff --git a/ImageEditTaskPool/entry/src/main/ets/entryability/EntryAbility.ets b/ImageEditTaskPool/entry/src/main/ets/entryability/EntryAbility.ets index a910cbde1b62e24cac154651d7e996c5a8152ef0..f38a1417a9cc7517b57d2cdb7071ac9618b52725 100644 --- a/ImageEditTaskPool/entry/src/main/ets/entryability/EntryAbility.ets +++ b/ImageEditTaskPool/entry/src/main/ets/entryability/EntryAbility.ets @@ -48,20 +48,26 @@ export default class EntryAbility extends UIAbility { } }); - let windowClass: window.Window = windowStage.getMainWindowSync(); - windowClass.setWindowLayoutFullScreen(true); - - let SystemBarProperties: window.SystemBarProperties = { - statusBarContentColor: '#FFFFFF' - }; - let promise = windowClass.setWindowSystemBarProperties(SystemBarProperties); - promise.then(() => { - hilog.info(0x0000, TAG, 'Succeeded in setting the system bar properties.'); - }).catch((err: BusinessError) => { - hilog.error(0x0000, TAG, - `Failed to set the system bar properties. Cause code: ${err.code}, message: ${err.message}`); - }); - + let windowClass: window.Window; + try { + windowClass = windowStage.getMainWindowSync(); + windowClass.setWindowLayoutFullScreen(true).catch((err: BusinessError) => { + hilog.error(0x0000, TAG, 'Failed to set window layout full screen. Cause: %{public}s', + JSON.stringify(err) ?? ''); + }); + let SystemBarProperties: window.SystemBarProperties = { + statusBarContentColor: '#FFFFFF' + }; + windowClass.setWindowSystemBarProperties(SystemBarProperties).then(() => { + hilog.info(0x0000, TAG, 'Succeeded in setting the system bar properties.'); + }).catch((err: BusinessError) => { + hilog.error(0x0000, TAG, + `Failed to set the system bar properties. Cause code: ${err.code}, message: ${err.message}`); + }); + } catch (err) { + hilog.error(0x0000, TAG, 'Failed to get main window. Cause: %{public}s', + JSON.stringify(err) ?? ''); + } windowStage.loadContent('pages/HomePage', (err, data) => { if (err.code) { @@ -69,15 +75,18 @@ export default class EntryAbility extends UIAbility { JSON.stringify(err) ?? ''); return; } - const uiContext = windowClass.getUIContext(); - AppStorage.setOrCreate('uiContext', uiContext); - let navigationBarArea: window.AvoidArea = - windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR); - let area: window.AvoidArea = windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM); - AppStorage.setOrCreate('naviIndicatorHeight', uiContext.px2vp(navigationBarArea.bottomRect.height)); - AppStorage.setOrCreate('statusBarHeight', uiContext.px2vp(area.topRect.height)); - hilog.info(0x0000, TAG, 'Succeeded in loading the content. Data: %{public}s', - JSON.stringify(data) ?? ''); + try { + const uiContext = windowClass.getUIContext(); + AppStorage.setOrCreate('uiContext', uiContext); + let navigationBarArea: window.AvoidArea = + windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR); + let area: window.AvoidArea = windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM); + AppStorage.setOrCreate('naviIndicatorHeight', uiContext.px2vp(navigationBarArea.bottomRect.height)); + AppStorage.setOrCreate('statusBarHeight', uiContext.px2vp(area.topRect.height)); + } catch (err) { + hilog.error(0x0000, TAG, 'Failed to load the content. Cause: %{public}s', + JSON.stringify(err) ?? ''); + } }); } diff --git a/ImageEditTaskPool/entry/src/main/ets/utils/DecodeUtil.ets b/ImageEditTaskPool/entry/src/main/ets/utils/DecodeUtil.ets index ee53b915c6aa8be725d933ab2f11608f715aa84d..75aa9af3f48d97c312a1279479602a95c9837d0d 100644 --- a/ImageEditTaskPool/entry/src/main/ets/utils/DecodeUtil.ets +++ b/ImageEditTaskPool/entry/src/main/ets/utils/DecodeUtil.ets @@ -27,11 +27,17 @@ const TAG: string = '[DecodeUtil]'; async function getResourceFd(component: UIContext): Promise { const context = component.getHostContext()!; const resourceMgr = context.resourceManager; - let imageBuffer = await resourceMgr.getMediaContent($r('app.media.ic_low')); - let filePath = context.cacheDir + '/low.jpg'; - let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); - fileIo.writeSync(file.fd, imageBuffer.buffer); - return file.fd; + let res = -1; + try { + let imageBuffer = await resourceMgr.getMediaContent($r('app.media.ic_low')); + let filePath = context.cacheDir + '/low.jpg'; + let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); + fileIo.writeSync(file.fd, imageBuffer.buffer); + res = file.fd; + } catch (err) { + hilog.error(0x0000, 'DecodeUtil', 'Failed to get resource fd: ', JSON.stringify(err) ?? ''); + } + return res; } /** diff --git a/ImageEditTaskPool/entry/src/main/ets/utils/EncodeUtil.ets b/ImageEditTaskPool/entry/src/main/ets/utils/EncodeUtil.ets index bfd292066f2478c03b3001cbd8db205f566dfd15..1c6c330c2517ca3764828a0bb2c9e50eeaacaeed 100644 --- a/ImageEditTaskPool/entry/src/main/ets/utils/EncodeUtil.ets +++ b/ImageEditTaskPool/entry/src/main/ets/utils/EncodeUtil.ets @@ -15,9 +15,9 @@ import { photoAccessHelper } from '@kit.MediaLibraryKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; -import { promptAction } from '@kit.ArkUI'; import { fileIo } from '@kit.CoreFileKit'; import { image } from '@kit.ImageKit'; +import { BusinessError } from '@kit.BasicServicesKit'; const TAG: string = '[EncodeUtil]'; const context = AppStorage.get('uiContext') as UIContext; @@ -37,20 +37,26 @@ export async function encode(component: Object, pixelMap: PixelMap) { format: 'image/jpeg', quality: 100 } - const imageData = await imagePackerApi.packToData(newPixelMap, packOptions); - hilog.info(0x0000, TAG, '%{public}s', `imageData's length is ${imageData.byteLength}`); - // Create image asset. - let photoType: photoAccessHelper.PhotoType = photoAccessHelper.PhotoType.IMAGE; - let extension: string = 'jpg'; - phAccessHelper.createAsset(photoType, extension, (err, uri) => { - if (err) { - hilog.error(0x0000, TAG, 'createAsset ', JSON.stringify(err) ?? ''); - } - if (uri != undefined) { - let file = fileIo.openSync(uri, fileIo.OpenMode.READ_WRITE); - fileIo.writeSync(file.fd, imageData); - fileIo.close(file.fd); - context.getPromptAction().showToast({ message: $r('app.string.image_save') }); - } - }); + try { + const imageData = await imagePackerApi.packToData(newPixelMap, packOptions); + hilog.info(0x0000, TAG, '%{public}s', `imageData's length is ${imageData.byteLength}`); + // Create image asset. + let photoType: photoAccessHelper.PhotoType = photoAccessHelper.PhotoType.IMAGE; + let extension: string = 'jpg'; + phAccessHelper.createAsset(photoType, extension, (err, uri) => { + if (err) { + hilog.error(0x0000, TAG, 'createAsset ', JSON.stringify(err) ?? ''); + } + if (uri !== undefined) { + let file = fileIo.openSync(uri, fileIo.OpenMode.READ_WRITE); + fileIo.writeSync(file.fd, imageData); + fileIo.close(file.fd).catch((err: BusinessError) => { + hilog.error(0x0000, TAG, 'Failed to close: ', JSON.stringify(err) ?? ''); + }); + context.getPromptAction().showToast({ message: $r('app.string.image_save') }); + } + }); + } catch (err) { + hilog.error(0x0000, TAG, 'Failed to packToData: ', JSON.stringify(err) ?? ''); + } } \ No newline at end of file diff --git a/ImageEditTaskPool/entry/src/main/ets/view/AdjustContentView.ets b/ImageEditTaskPool/entry/src/main/ets/view/AdjustContentView.ets index 61efe46ed42de612299348d7bac93341d26a4c51..f4c312a9435cceeea56a07a8d16131c8b332d829 100644 --- a/ImageEditTaskPool/entry/src/main/ets/view/AdjustContentView.ets +++ b/ImageEditTaskPool/entry/src/main/ets/view/AdjustContentView.ets @@ -12,15 +12,16 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { MessageEvents, worker } from '@kit.ArkTS'; +import { worker } from '@kit.ArkTS'; import { taskpool } from '@kit.ArkTS'; import { image } from '@kit.ImageKit'; import { adjustIconList, IconStatus } from '../viewModel/IconListViewModel'; import { adjustImageValue, adjustSaturation } from '../utils/AdjustUtil'; -import { AdjustId, HSVIndex, RGBIndex } from '../viewModel/OptionViewModel'; +import { AdjustId } from '../viewModel/OptionViewModel'; import { adjustOpacity } from '../utils/OpacityUtil'; import { MessageItem } from '../viewModel/MessageItem'; -import { CommonConstants } from '../common/constant/CommonConstants'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { BusinessError } from '@kit.BasicServicesKit'; @Component export default struct AdjustContentView { @@ -206,9 +207,11 @@ struct SliderCustom { const group = splitTask(buffers, type, sliderValue, value); // [StartExclude postProcess_start] // [Start execute_start] - taskpool.execute(group, taskpool.Priority.HIGH).then((ret) => { + taskpool.execute(group, taskpool.Priority.HIGH).catch((err: BusinessError) => { + hilog.error(0x0000, 'AdjustContentView', 'Failed to execute taskpool: ', JSON.stringify(err) ?? ''); + }).then((ret) => { // Combine the results of each task execution - const entireArrayBuffer = mergeArrayBuffers(ret); + const entireArrayBuffer = mergeArrayBuffers(ret as Object[]); // Update the UI based on the calculation results this.updatePixelMap(entireArrayBuffer); }); @@ -263,13 +266,17 @@ function splitTask(buffers: ArrayBuffer[], type: AdjustId, sliderValue: number, // Creating a Task Group let group: taskpool.TaskGroup = new taskpool.TaskGroup(); for (const buffer of buffers) { - group.addTask(imageProcessing, { - // Add a task to a task group - type, - bufferArray: buffer, - sliderValue, - value - }); + try { + group.addTask(imageProcessing, { + // Add a task to a task group + type, + bufferArray: buffer, + sliderValue, + value + }); + } catch (err) { + hilog.error(0x0000, 'AdjustContentView', 'Failed to add the task: ', JSON.stringify(err) ?? ''); + } } return group; }