From 07ba00c2eb038906d81905c3d7a24821d2b4302f Mon Sep 17 00:00:00 2001 From: wzp Date: Sun, 17 Dec 2023 21:59:11 +0800 Subject: [PATCH] fix:api update Signed-off-by: wzp --- .../main/ets/default/manager/WindowManager.ts | 165 ++++++++++-------- .../src/main/ets/default/utils/FileUtils.ts | 32 ++-- .../common/uicomponents/RecentMissionCard.ets | 8 +- .../src/main/ets/MainAbility/MainAbility.ts | 11 +- .../phone/src/main/ets/pages/EntryView.ets | 2 - .../phone/src/main/ets/pages/RecentView.ets | 2 +- 6 files changed, 120 insertions(+), 100 deletions(-) diff --git a/common/src/main/ets/default/manager/WindowManager.ts b/common/src/main/ets/default/manager/WindowManager.ts index a719a3f9..c42e5c7b 100644 --- a/common/src/main/ets/default/manager/WindowManager.ts +++ b/common/src/main/ets/default/manager/WindowManager.ts @@ -13,7 +13,7 @@ * limitations under the License. */ -import Window from '@ohos.window'; +import window from '@ohos.window'; import display from '@ohos.display'; import commonEventMgr from '@ohos.commonEventManager'; import common from '@ohos.app.ability.common'; @@ -45,11 +45,11 @@ class WindowManager { FORM_SERVICE_WINDOW_NAME = 'FormServiceView'; - DESKTOP_RANK = Window.WindowType.TYPE_DESKTOP; + DESKTOP_RANK = window.WindowType.TYPE_DESKTOP; - RECENT_RANK = Window.WindowType.TYPE_LAUNCHER_RECENT; + RECENT_RANK = window.WindowType.TYPE_LAUNCHER_RECENT; - DOCK_RANK = Window.WindowType.TYPE_LAUNCHER_DOCK; + DOCK_RANK = window.WindowType.TYPE_LAUNCHER_DOCK; recentMode?: number; @@ -116,7 +116,7 @@ class WindowManager { * @param height window height */ async setWindowSize(width: number, height: number): Promise { - const abilityWindow = await Window.getLastWindow(globalThis.desktopContext as common.BaseContext); + const abilityWindow = await window.getLastWindow(globalThis.desktopContext as common.BaseContext); void abilityWindow.resize(width, height); } @@ -127,7 +127,7 @@ class WindowManager { * @param y coordinate y */ async setWindowPosition(x: number, y: number): Promise { - const abilityWindow = await Window.getLastWindow(globalThis.desktopContext as common.BaseContext); + const abilityWindow = await window.getLastWindow(globalThis.desktopContext as common.BaseContext); void abilityWindow.moveWindowTo(x, y); } @@ -169,45 +169,61 @@ class WindowManager { createWindow(context: common.ServiceExtensionContext, name: string, windowType: number, loadContent: string, isShow: boolean, callback?: Function) { - Window.create(context, name, windowType).then((win) => { - void win.setPreferredOrientation(Window.Orientation.AUTO_ROTATION_RESTRICTED); - void win.loadContent(loadContent).then(() => { - void win.setSystemBarProperties({ - navigationBarColor: StyleConstants.DEFAULT_SYSTEM_UI_COLOR, - statusBarColor: StyleConstants.DEFAULT_SYSTEM_UI_COLOR - }).then(() => { - win.setBackgroundColor(StyleConstants.DEFAULT_SYSTEM_UI_COLOR, () => { - Log.showDebug(TAG, `then begin ${name} window loadContent in then!`); - if (name !== this.RECENT_WINDOW_NAME) { - void win.setLayoutFullScreen(true).then(() => { - Log.showDebug(TAG, `${name} setLayoutFullScreen`); + let cfg: window.Configuration = { + name: name, + windowType: windowType, + ctx: context + }; + try { + window.createWindow(cfg) + .then((win: window.Window) => { + win.setPreferredOrientation(window.Orientation.AUTO_ROTATION_RESTRICTED); + win.setUIContent(loadContent) + .then(() => { + win.setWindowSystemBarProperties({ + navigationBarColor: StyleConstants.DEFAULT_SYSTEM_UI_COLOR, + statusBarColor: StyleConstants.DEFAULT_SYSTEM_UI_COLOR + }).then(() => { + win.setWindowBackgroundColor(StyleConstants.DEFAULT_SYSTEM_UI_COLOR); + Log.showDebug(TAG, `then begin ${name} window loadContent in then!`); + if (name !== this.RECENT_WINDOW_NAME) { + win.setWindowLayoutFullScreen(true).then(() => { + Log.showDebug(TAG, `${name} setLayoutFullScreen`); + }); + } + if (callback) { + callback(win); + } + // there is a low probability that white flashes when no delay because of setBackgroundColor is asynchronous + setTimeout(() => { + isShow && this.showWindow(name); + }, StyleConstants.WINDOW_SHOW_DELAY) }); - } - if (callback) { - callback(win); - } - // there is a low probability that white flashes when no delay because of setBackgroundColor is asynchronous - setTimeout(() => { - isShow && this.showWindow(name); - }, StyleConstants.WINDOW_SHOW_DELAY) - }) - }); - }, (error) => { - Log.showError(TAG, `createWindow, create error: ${JSON.stringify(error)}`); - }); - }); + }, (err: BusinessError) => { + Log.showError(TAG, `createWindow, setUIContent error: ${JSON.stringify(err)}`); + }); + }) + .catch((err: BusinessError) => { + Log.showError(TAG, `createWindow, createWindow error: ${JSON.stringify(err)}`); + }) + } catch (err) { + let _err = err as BusinessError; + Log.showError(TAG, `createWindow, error: ${JSON.stringify(_err)}`); + } } createWindowIfAbsent(context: common.ServiceExtensionContext, name: string, windowType: number, loadContent: string): void { Log.showDebug(TAG, `create, name ${name}`); - Window.find(name).then(win => { - void win.show().then(() => { + try { + let win: window.Window = window.findWindow(name); + win.showWindow().then(() => { Log.showDebug(TAG, `show launcher ${name}`); }); - }).catch(error => { - Log.showError(TAG, `${name} ability is not created, because ${error}`); + } catch (err) { + let _err = err as BusinessError; + Log.showError(TAG, `${name} ability is not created, because ${_err.message}`); this.createWindow(context, name, windowType, loadContent, true); - }); + } } resetSizeWindow(name: string, rect: { @@ -255,7 +271,7 @@ class WindowManager { minimizeAllApps(): void { try { let dis: display.Display = display.getDefaultDisplaySync(); - Window.minimizeAll(dis.id).then(() => { + window.minimizeAll(dis.id).then(() => { Log.showDebug(TAG, 'Launcher minimizeAll'); }); } catch (err) { @@ -282,13 +298,16 @@ class WindowManager { findWindow(name: string, callback?: Function): void { Log.showDebug(TAG, `findWindow, name ${name}`); - void Window.find(name) - .then((win) => { - Log.showDebug(TAG, `findWindow, find then name: ${name}`); - if (callback) { - callback(win); - } - }); + try { + let win: window.Window = window.findWindow(name); + Log.showDebug(TAG, `findWindow, find then name: ${name}`); + if (callback) { + callback(win); + } + } catch (err) { + let _err = err as BusinessError; + Log.showError(TAG, `findWindow errCode: ${_err.code}, errMsg: ${_err.message}`); + } } createRecentWindow(mode?: number) { @@ -303,26 +322,33 @@ class WindowManager { Log.showDebug(TAG, `${this.RECENT_WINDOW_NAME} setFullScreen`); }); }; - let registerWinEvent = (win) => { + let registerWinEvent = (win: window.Window) => { Log.showDebug(TAG, 'registerWinEvent Begin'); - win.on('lifeCycleEvent', (stageEventType) => { + win.on('windowEvent', (stageEventType) => { Log.showDebug(TAG,`Recent lifeCycleEvent callback stageEventType=${stageEventType}`); - if (stageEventType == Window.WindowStageEventType.INACTIVE) { + if (stageEventType === window.WindowEventType.WINDOW_INACTIVE) { + Log.showDebug(TAG,'Recent MainAbility onWindowStageInactive'); - Window.find(windowManager.RECENT_WINDOW_NAME).then((win) => { + try { + let wins: window.Window = window.findWindow(windowManager.RECENT_WINDOW_NAME); Log.showDebug(TAG,'Hide recent on inactive'); - win.hide(); - }) + wins.hide(); + } catch (err) { + let _err = err as BusinessError; + Log.showError(TAG, `Recent lifeCycleEvent findWindow errCode: ${_err.code}, errMsg: ${_err.message}`); + } } }) }; - Window.find(windowManager.RECENT_WINDOW_NAME).then(win => { + try { + let win: window.Window = window.findWindow(windowManager.RECENT_WINDOW_NAME); setWinMode(win); - void win.show().then(() => { - Log.showDebug(TAG, 'show launcher recent ability'); - }); - }).catch(error => { - Log.showDebug(TAG, `recent window is not created, because ${error}`); + win.showWindow() + .then(() => { + Log.showDebug(TAG, 'show launcher recent ability'); + }); + } catch (err) { + Log.showDebug(TAG, `recent window is not created, because ${JSON.stringify(err)}`); let callback = (win) => { Log.showDebug(TAG, 'Post recent window created'); registerWinEvent(win); @@ -330,12 +356,12 @@ class WindowManager { } this.createWindow(globalThis.desktopContext, windowManager.RECENT_WINDOW_NAME, windowManager.RECENT_RANK, 'pages/' + windowManager.RECENT_WINDOW_NAME, false, callback); - }); + } } destroyRecentWindow() { this.findWindow(windowManager.RECENT_WINDOW_NAME, win => { - win.off('lifeCycleEvent', (win) => { + win.off('windowEvent', (win) => { win.destroy().then(() => { Log.showDebug(TAG, 'destroyRecentWindow'); }); @@ -417,18 +443,15 @@ class WindowManager { Log.showInfo(TAG, 'screen change to portrait'); AppStorage.setOrCreate('isPortrait', true); } - display.getDefaultDisplay() - .then((dis: { - id: number, - width: number, - height: number, - refreshRate: number - }) => { - Log.showInfo(TAG, `change to display: ${JSON.stringify(dis)}`); - AppStorage.setOrCreate('screenWidth', px2vp(dis.width)); - AppStorage.setOrCreate('screenHeight', px2vp(dis.height)); - Log.showDebug(TAG, `screenWidth and screenHeight: ${AppStorage.get('screenWidth')},${AppStorage.get('screenHeight')}`); - }); + try { + let dis: display.Display = display.getDefaultDisplaySync(); + Log.showInfo(TAG, `change to display: ${JSON.stringify(dis)}`); + AppStorage.setOrCreate('screenWidth', px2vp(dis.width)); + AppStorage.setOrCreate('screenHeight', px2vp(dis.height)); + Log.showDebug(TAG, `screenWidth and screenHeight: ${AppStorage.get('screenWidth')},${AppStorage.get('screenHeight')}`); + } catch (err) { + Log.showError(TAG, `onPortrait error: ${JSON.stringify(err)}`); + } } createWindowWithName = ((windowName: string, windowRank: number): void => { diff --git a/common/src/main/ets/default/utils/FileUtils.ts b/common/src/main/ets/default/utils/FileUtils.ts index 807a0e59..d6403513 100644 --- a/common/src/main/ets/default/utils/FileUtils.ts +++ b/common/src/main/ets/default/utils/FileUtils.ts @@ -16,7 +16,8 @@ /** * An util that provides io functionality between file and JSON object. */ -import Fileio from '@ohos.fileio'; + +import fs from '@ohos.file.fs'; import util from '@ohos.util'; import { Log } from './Log'; @@ -36,11 +37,11 @@ export default class FileUtils { Log.showDebug(TAG, 'readJsonFile start execution'); let readStreamSync = null; try { - readStreamSync = Fileio.createStreamSync(filePath, 'r'); + readStreamSync = fs.createStreamSync(filePath, 'r'); const content = this.getContent(readStreamSync); return JSON.parse(content); } catch (e) { - Log.showError(TAG, `readJsonFile error: ${e.toString()}`); + Log.showError(TAG, `readJsonFile error: ${JSON.stringify(e)}`); } finally { readStreamSync.closeSync(); } @@ -56,11 +57,11 @@ export default class FileUtils { Log.showDebug(TAG, 'readStringFromFile start execution'); let readStreamSync = null; try { - readStreamSync = Fileio.createStreamSync(filePath, 'r'); + readStreamSync = fs.createStreamSync(filePath, 'r'); const content = this.getContent(readStreamSync); return content; } catch (e) { - Log.showError(TAG, `readStringFromFile error: ${e.toString()}, filePath: ${filePath}`); + Log.showError(TAG, `readStringFromFile error: ${JSON.stringify(e)}, filePath: ${filePath}`); } finally { if (readStreamSync) { readStreamSync.closeSync(); @@ -78,11 +79,11 @@ export default class FileUtils { Log.showDebug(TAG, 'writeStringToFile start execution'); let writeStreamSync = null; try { - writeStreamSync = Fileio.createStreamSync(filePath, 'w+'); + writeStreamSync = fs.createStreamSync(filePath, 'w+'); let number = writeStreamSync.writeSync(str); Log.showDebug(TAG, 'writeStringToFile number: ' + number); } catch (e) { - Log.showError(TAG, `writeStringToFile error: ${e.toString()}`); + Log.showError(TAG, `writeStringToFile error: ${JSON.stringify(e)}`); } finally { writeStreamSync.closeSync(); Log.showDebug(TAG, 'writeStringToFile close sync'); @@ -122,8 +123,8 @@ export default class FileUtils { contentBuf.set(uInt8Arr, offset); offset += uInt8Arr.byteLength; } - let textDecoder = new util.TextDecoder('utf-8', {ignoreBOM: true}); - const content = textDecoder.decode(contentBuf, {stream: false}); + let textDecoder = util.TextDecoder.create('utf-8', {ignoreBOM: true}); + const content = textDecoder.decodeWithStream(contentBuf, {stream: false}); return content; } @@ -134,14 +135,15 @@ export default class FileUtils { * @return {boolean} - boolean true(Exist) */ static isExist(filePath: string): boolean { + let result: boolean = false; try { - Fileio.accessSync(filePath); + result = fs.accessSync(filePath); Log.showDebug(TAG, 'accessSync success.'); } catch(e) { - Log.showError(TAG, `isExit error: ${e.toString()}`); - return false; + Log.showError(TAG, `isExit error: ${JSON.stringify(e)}`); + result = false; } - return true; + return result; } /** @@ -151,9 +153,9 @@ export default class FileUtils { */ static deleteConfigFile(filePath: string): void { try { - Fileio.unlinkSync(filePath); + fs.unlinkSync(filePath); } catch(e) { - Log.showError(TAG, `deleteFile error: ${e.toString()}`); + Log.showError(TAG, `deleteFile error: ${JSON.stringify(e)}`); } } } \ No newline at end of file diff --git a/feature/recents/src/main/ets/default/common/uicomponents/RecentMissionCard.ets b/feature/recents/src/main/ets/default/common/uicomponents/RecentMissionCard.ets index 90d1d4e1..0c9bfd16 100644 --- a/feature/recents/src/main/ets/default/common/uicomponents/RecentMissionCard.ets +++ b/feature/recents/src/main/ets/default/common/uicomponents/RecentMissionCard.ets @@ -162,7 +162,7 @@ export default struct RecentMissionCard { .onClick((event) => { this.isClickSubComponent = true; Log.showDebug(TAG, `click set recent mission: ${this.missionId} Locked status: ${!this.lockedState}`); - this.mRecentMissionsViewModel?.setRecentMissionLock(this.missionId,!this.lockedState); + RecentMissionsViewModel.getInstance().setRecentMissionLock(this.missionId,!this.lockedState); this.setStartAppInfo(event?.target?.area?.globalPosition); this.isClickSubComponent = false; }) @@ -194,7 +194,7 @@ export default struct RecentMissionCard { } Trace.start(Trace.CORE_METHOD_START_APP_ANIMATION); this.setStartAppInfo(event?.target?.area?.globalPosition); - this.mRecentMissionsViewModel?.moveMissionToFront(this.missionId); + RecentMissionsViewModel.getInstance().moveMissionToFront(this.missionId); }) } .width(this.mIsSingleLayout ? @@ -211,10 +211,10 @@ export default struct RecentMissionCard { RecentsStyleConstants.SINGLE_LIST_APP_IMAGE_WIDTH * RecentsStyleConstants.DPI_RATIO : RecentsStyleConstants.DOUBLE_LIST_APP_IMAGE_WIDTH) / 2; if (e.offsetY < -50 && e.offsetX <= offsetWidth && -offsetWidth <= e.offsetX) { - this.mRecentMissionsViewModel?.deleteRecentMission(false, this.missionId); + RecentMissionsViewModel.getInstance().deleteRecentMission(false, this.missionId); } else if (e.offsetY > 50 && e.offsetX <= offsetWidth && -offsetWidth <= e.offsetX) { Log.showDebug(TAG, `gesture set recent mission: ${this.missionId} Locked status: ${!this.lockedState}`); - this.mRecentMissionsViewModel?.setRecentMissionLock(this.missionId,!this.lockedState); + RecentMissionsViewModel.getInstance().setRecentMissionLock(this.missionId,!this.lockedState); } })) } diff --git a/product/phone/src/main/ets/MainAbility/MainAbility.ts b/product/phone/src/main/ets/MainAbility/MainAbility.ts index 70144cfa..f96c4584 100644 --- a/product/phone/src/main/ets/MainAbility/MainAbility.ts +++ b/product/phone/src/main/ets/MainAbility/MainAbility.ts @@ -35,6 +35,7 @@ import { PageDesktopViewModel } from '@ohos/pagedesktop'; import Window from '@ohos.window'; import inputConsumer from '@ohos.multimodalInput.inputConsumer'; import { KeyCode } from '@ohos.multimodalInput.keyCode'; +import window from '@ohos.window'; const TAG = 'LauncherMainAbility'; @@ -60,17 +61,13 @@ export default class MainAbility extends ServiceExtension { await dbStore.initRdbConfig(); await dbStore.createTable(); - let registerWinEvent = (win) => { - win.on('lifeCycleEvent', (stageEventType) => { + let registerWinEvent = (win: window.Window) => { + win.on('windowEvent', (stageEventType) => { // 桌面获焦或失焦时,通知桌面的卡片变为可见状态 - if (stageEventType === Window.WindowStageEventType.ACTIVE) { - AppStorage.setOrCreate('entryViewFocus', true); + if (stageEventType === window.WindowEventType.WINDOW_ACTIVE) { localEventManager.sendLocalEventSticky(EventConstants.EVENT_REQUEST_FORM_ITEM_VISIBLE, null); Log.showInfo(TAG, `lifeCycleEvent change: ${stageEventType}`); } - if (stageEventType === Window.WindowStageEventType.INACTIVE) { - AppStorage.setOrCreate('entryViewFocus', false); - } }) }; diff --git a/product/phone/src/main/ets/pages/EntryView.ets b/product/phone/src/main/ets/pages/EntryView.ets index 30cc5422..c166dcd1 100644 --- a/product/phone/src/main/ets/pages/EntryView.ets +++ b/product/phone/src/main/ets/pages/EntryView.ets @@ -42,7 +42,6 @@ struct EntryView { @StorageLink('screenWidth') screenWidth: number = 0; @StorageLink('screenHeight') @Watch('updateScreenInfo') screenHeight: number = 0; @StorageLink('deviceType') deviceType: string = CommonConstants.DEFAULT_DEVICE_TYPE; - @StorageLink('entryViewFocus') entryViewFocus: boolean = true; @State workSpaceWidth: number = 0; @State workSpaceHeight: number = 0; @State dockHeight: number = 0; @@ -157,7 +156,6 @@ struct EntryView { .backgroundImage(StyleConstants.DEFAULT_BACKGROUND_IMAGE) .backgroundImageSize(ImageSize.Cover) .backgroundImagePosition(Alignment.Center) - .foregroundBlurStyle(this.entryViewFocus ? BlurStyle.NONE : BlurStyle.Thick) .width('100%') .height('100%') } diff --git a/product/phone/src/main/ets/pages/RecentView.ets b/product/phone/src/main/ets/pages/RecentView.ets index d42b2a84..ce5c6f66 100644 --- a/product/phone/src/main/ets/pages/RecentView.ets +++ b/product/phone/src/main/ets/pages/RecentView.ets @@ -89,7 +89,7 @@ struct RecentView { .onClick(() => { if (!this.isClickSubComponent) { Log.showInfo(TAG, 'click recent missions area'); - this.mRecentMissionsViewModel?.backView && this.mRecentMissionsViewModel.backView(); + RecentMissionsViewModel.getInstance().backView(); } }) }