From badaf7c4f5f3a9d45990e32362a1883fdb7232e1 Mon Sep 17 00:00:00 2001 From: 26thE <962961353@qq.com> Date: Wed, 25 Jun 2025 15:15:17 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=BA=9F=E5=BC=83?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/ets/liveview/LockScreenPage.ets | 7 ++----- .../src/main/ets/pages/LiveViewLockScreenPage.ets | 2 +- .../src/main/ets/utils/LiveView.ets | 10 +++++----- .../src/main/ets/utils/LiveViewExtAbilityUtil.ets | 10 ++-------- .../src/main/ets/view/TrafficInfoView.ets | 4 ++-- .../src/main/ets/view/TrafficView.ets | 7 ++++--- 6 files changed, 16 insertions(+), 24 deletions(-) diff --git a/liveviewlockscreenlibrary/src/main/ets/liveview/LockScreenPage.ets b/liveviewlockscreenlibrary/src/main/ets/liveview/LockScreenPage.ets index a160027..9bbd648 100644 --- a/liveviewlockscreenlibrary/src/main/ets/liveview/LockScreenPage.ets +++ b/liveviewlockscreenlibrary/src/main/ets/liveview/LockScreenPage.ets @@ -17,13 +17,10 @@ import { LaneData } from '../model/RouteDataModel'; import { BreakpointType, BreakpointTypeEnum } from '../utils/BreakpointSystem'; import { RoadView } from '../view/RoadView'; -let storage: LocalStorage = LocalStorage.getShared(); - -@Entry(storage) -@Component +@Entry export struct LockScreenPage { @StorageLink('currentHeightBreakpoint') currentHeightBreakpoint: string = BreakpointTypeEnum.SM; - @LocalStorageProp('laneData') roadInfo: LaneData | undefined = undefined; + @StorageProp('laneData') roadInfo: LaneData | undefined = undefined; build() { Stack({ alignContent: Alignment.Top }) { diff --git a/liveviewlockscreenlibrary/src/main/ets/pages/LiveViewLockScreenPage.ets b/liveviewlockscreenlibrary/src/main/ets/pages/LiveViewLockScreenPage.ets index 32e35c6..07785c6 100644 --- a/liveviewlockscreenlibrary/src/main/ets/pages/LiveViewLockScreenPage.ets +++ b/liveviewlockscreenlibrary/src/main/ets/pages/LiveViewLockScreenPage.ets @@ -97,7 +97,7 @@ export struct LiveViewLockScreenPage { }); aboutToAppear(): void { - LiveView.getInstance().createLiveView(); + LiveView.getInstance(this.getUIContext().getHostContext()).createLiveView(); this.customDialogController?.open(); } diff --git a/liveviewlockscreenlibrary/src/main/ets/utils/LiveView.ets b/liveviewlockscreenlibrary/src/main/ets/utils/LiveView.ets index cc8fc39..0fe188b 100644 --- a/liveviewlockscreenlibrary/src/main/ets/utils/LiveView.ets +++ b/liveviewlockscreenlibrary/src/main/ets/utils/LiveView.ets @@ -26,21 +26,21 @@ const TAG: string = '[LiveView]'; export class LiveView { // Singleton pattern implementation private static instance: LiveView; - private context: Context; + private context: Context | undefined; private liveViewController: LiveViewUtil; private updateInterval: number | undefined; // Private constructor for singleton pattern - private constructor() { - this.context = getContext(this); + private constructor(context: Context | undefined) { + this.context = context; this.liveViewController = new LiveViewUtil(); } // Singleton accessor method - public static getInstance(): LiveView { + public static getInstance(context: Context | undefined): LiveView { // Lazy initialization of the singleton instance if (!LiveView.instance) { - LiveView.instance = new LiveView(); + LiveView.instance = new LiveView(context); } return LiveView.instance; } diff --git a/liveviewlockscreenlibrary/src/main/ets/utils/LiveViewExtAbilityUtil.ets b/liveviewlockscreenlibrary/src/main/ets/utils/LiveViewExtAbilityUtil.ets index 8422aa2..6873e4a 100644 --- a/liveviewlockscreenlibrary/src/main/ets/utils/LiveViewExtAbilityUtil.ets +++ b/liveviewlockscreenlibrary/src/main/ets/utils/LiveViewExtAbilityUtil.ets @@ -43,18 +43,12 @@ export class LiveViewExtAbilityUtil { } public setSession(session: UIExtensionContentSession): void { - let param: Record = { - 'session': session - }; - // Window size listener. const extensionWindow = session.getUIExtensionWindowProxy(); extensionWindow.on('windowSizeChange', (windowSize: window.Size) => { LiveViewExtAbilityUtil.updateBreakPoint(windowSize); }); - // Create state storage (for cross-page data sharing). - let storage: LocalStorage = new LocalStorage(param); // Initialize event subscription. let bundleName: string = ''; try { @@ -94,12 +88,12 @@ export class LiveViewExtAbilityUtil { hilog.info(0x0000, TAG, '%{public}s', 'Succeeded in subscribe commonEvent success.'); if (data.parameters) { let laneData = data.parameters['roadData'] as LaneData; - storage.setOrCreate('laneData', laneData); + AppStorage.setOrCreate('laneData', laneData); hilog.info(0x0000, TAG, '%{public}s', 'Succeeded in receive commonEvent.'); } }); }) - session.loadContent('pages/LockScreen', storage); + session.loadContent('pages/LockScreen'); } // Distinguish page layout using vertical breakpoints. diff --git a/liveviewlockscreenlibrary/src/main/ets/view/TrafficInfoView.ets b/liveviewlockscreenlibrary/src/main/ets/view/TrafficInfoView.ets index 6cccda4..c688693 100644 --- a/liveviewlockscreenlibrary/src/main/ets/view/TrafficInfoView.ets +++ b/liveviewlockscreenlibrary/src/main/ets/view/TrafficInfoView.ets @@ -78,7 +78,7 @@ export struct TrafficInfoView { .width('40%') .onClick(() => { this.isShowButton = false; - LiveView.getInstance().createLiveView(); + LiveView.getInstance(this.getUIContext().getHostContext()).createLiveView(); this.getUIContext().getPromptAction().showToast({ message: $r('app.string.toast'), duration: 2000, @@ -94,7 +94,7 @@ export struct TrafficInfoView { .width('40%') .onClick(() => { this.isShowButton = false; - LiveView.getInstance().finishLiveView(); + LiveView.getInstance(this.getUIContext().getHostContext()).finishLiveView(); this.getUIContext().getPromptAction().showToast({ message: $r('app.string.toast'), duration: 2000, diff --git a/liveviewlockscreenlibrary/src/main/ets/view/TrafficView.ets b/liveviewlockscreenlibrary/src/main/ets/view/TrafficView.ets index 7b27441..d76c80b 100644 --- a/liveviewlockscreenlibrary/src/main/ets/view/TrafficView.ets +++ b/liveviewlockscreenlibrary/src/main/ets/view/TrafficView.ets @@ -22,6 +22,7 @@ export struct TrafficView { @StorageLink('currentWidthBreakpoint') currentWidthBreakpoint: string = BreakpointTypeEnum.SM; @StorageLink('currentHeightBreakpoint') currentHeightBreakpoint: string = BreakpointTypeEnum.LG; @Prop lane: number = 2; + private uiContext: UIContext = this.getUIContext(); private updateInterval: number | undefined; aboutToAppear(): void { @@ -225,7 +226,7 @@ export struct TrafficView { }) .padding({ bottom: new BreakpointType({ - sm: px2vp(this.bottomRectHeight), + sm: this.uiContext.px2vp(this.bottomRectHeight), md: 0, lg: 0, }).getValue(this.currentWidthBreakpoint), @@ -233,8 +234,8 @@ export struct TrafficView { .margin({ bottom: new BreakpointType({ sm: 0, - md: px2vp(this.bottomRectHeight), - lg: px2vp(this.bottomRectHeight), + md: this.uiContext.px2vp(this.bottomRectHeight), + lg: this.uiContext.px2vp(this.bottomRectHeight), }).getValue(this.currentWidthBreakpoint), }) } -- Gitee From 65511dba8a65d94b8868bd52cde86c44ad1cd4b3 Mon Sep 17 00:00:00 2001 From: 26thE <962961353@qq.com> Date: Sat, 28 Jun 2025 14:55:14 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=BA=9F=E5=BC=83?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3&AppStorage?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/ets/liveview/LockScreenPage.ets | 4 ++-- .../src/main/ets/utils/BreakpointSystem.ets | 18 +++++++++--------- .../src/main/ets/utils/BundleNameUtil.ets | 2 +- .../src/main/ets/utils/LiveView.ets | 4 ++-- .../main/ets/utils/LiveViewExtAbilityUtil.ets | 4 ++-- .../src/main/ets/utils/LiveViewUtil.ets | 2 +- .../src/main/ets/utils/WindowUtil.ets | 7 ++++--- .../src/main/ets/view/TrafficView.ets | 6 +++--- .../src/main/ets/entryability/EntryAbility.ets | 4 ++-- .../src/main/ets/pages/LockScreen.ets | 5 +---- 10 files changed, 27 insertions(+), 29 deletions(-) diff --git a/liveviewlockscreenlibrary/src/main/ets/liveview/LockScreenPage.ets b/liveviewlockscreenlibrary/src/main/ets/liveview/LockScreenPage.ets index 9bbd648..b28ab1c 100644 --- a/liveviewlockscreenlibrary/src/main/ets/liveview/LockScreenPage.ets +++ b/liveviewlockscreenlibrary/src/main/ets/liveview/LockScreenPage.ets @@ -19,8 +19,8 @@ import { RoadView } from '../view/RoadView'; @Entry export struct LockScreenPage { - @StorageLink('currentHeightBreakpoint') currentHeightBreakpoint: string = BreakpointTypeEnum.SM; - @StorageProp('laneData') roadInfo: LaneData | undefined = undefined; + @StorageLink('liveViewLockScreenCurrentHeightBreakpoint') currentHeightBreakpoint: string = BreakpointTypeEnum.SM; + @StorageProp('liveViewLockScreenLaneData') roadInfo: LaneData | undefined = undefined; build() { Stack({ alignContent: Alignment.Top }) { diff --git a/liveviewlockscreenlibrary/src/main/ets/utils/BreakpointSystem.ets b/liveviewlockscreenlibrary/src/main/ets/utils/BreakpointSystem.ets index 52db322..b4b7552 100644 --- a/liveviewlockscreenlibrary/src/main/ets/utils/BreakpointSystem.ets +++ b/liveviewlockscreenlibrary/src/main/ets/utils/BreakpointSystem.ets @@ -73,8 +73,8 @@ export class BreakpointSystem { private currentHeightBreakpoint: BreakpointTypeEnum = BreakpointTypeEnum.LG; private constructor() { - AppStorage.setOrCreate('currentWidthBreakpoint', this.currentWidthBreakpoint); - AppStorage.setOrCreate('currentHeightBreakpoint', this.currentHeightBreakpoint); + AppStorage.setOrCreate('liveViewLockScreenCurrentWidthBreakpoint', this.currentWidthBreakpoint); + AppStorage.setOrCreate('liveViewLockScreenCurrentHeightBreakpoint', this.currentHeightBreakpoint); } public static getInstance(): BreakpointSystem { @@ -87,28 +87,28 @@ export class BreakpointSystem { public updateCurrentWidthBreakpoint(widthBreakpoint: BreakpointTypeEnum): void { if (this.currentWidthBreakpoint !== widthBreakpoint) { this.currentWidthBreakpoint = widthBreakpoint; - AppStorage.setOrCreate('currentWidthBreakpoint', this.currentWidthBreakpoint); + AppStorage.setOrCreate('liveViewLockScreenCurrentWidthBreakpoint', this.currentWidthBreakpoint); } } public updateCurrentHeightBreakpoint(heightBreakpoint: BreakpointTypeEnum): void { if (this.currentHeightBreakpoint !== heightBreakpoint) { this.currentHeightBreakpoint = heightBreakpoint; - AppStorage.setOrCreate('currentHeightBreakpoint', this.currentHeightBreakpoint); + AppStorage.setOrCreate('liveViewLockScreenCurrentHeightBreakpoint', this.currentHeightBreakpoint); } } - public onWindowSizeChange(window: window.Window): void { - this.updateWidthHeightBp(window); + public onWindowSizeChange(window: window.Window, uiContent: UIContext): void { + this.updateWidthHeightBp(window, uiContent); } - public updateWidthHeightBp(window: window.Window): void { + public updateWidthHeightBp(window: window.Window, uiContent: UIContext): void { try { const mainWindow: window.WindowProperties = window.getWindowProperties(); const windowWidth: number = mainWindow.windowRect.width; const windowHeight: number = mainWindow.windowRect.height; - const windowWidthVp = px2vp(windowWidth); - const windowHeightVp = px2vp(windowHeight); + const windowWidthVp = uiContent.px2vp(windowWidth); + const windowHeightVp = uiContent.px2vp(windowHeight); const windowRatio: number = windowHeightVp / windowWidthVp; let widthBp: BreakpointTypeEnum = BreakpointTypeEnum.SM; let heightBp: BreakpointTypeEnum = BreakpointTypeEnum.LG; diff --git a/liveviewlockscreenlibrary/src/main/ets/utils/BundleNameUtil.ets b/liveviewlockscreenlibrary/src/main/ets/utils/BundleNameUtil.ets index b08a358..fb2b875 100644 --- a/liveviewlockscreenlibrary/src/main/ets/utils/BundleNameUtil.ets +++ b/liveviewlockscreenlibrary/src/main/ets/utils/BundleNameUtil.ets @@ -24,7 +24,7 @@ export class BundleNameUtil { try { bundleManager.getBundleInfoForSelf(bundleManager.BundleFlag.GET_BUNDLE_INFO_DEFAULT).then((data) => { hilog.info(0x0000, TAG, '%{public}s', 'getBundleInfoForSelf successfully.'); - AppStorage.setOrCreate('bundleName', data.name); + AppStorage.setOrCreate('liveViewLockScreenBundleName', data.name); }).catch((error: BusinessError) => { hilog.error(0x0000, TAG, '%{public}s', `getBundleInfoForSelf failed. code is ${error.code} message is ${error.message}`); diff --git a/liveviewlockscreenlibrary/src/main/ets/utils/LiveView.ets b/liveviewlockscreenlibrary/src/main/ets/utils/LiveView.ets index 0fe188b..5816263 100644 --- a/liveviewlockscreenlibrary/src/main/ets/utils/LiveView.ets +++ b/liveviewlockscreenlibrary/src/main/ets/utils/LiveView.ets @@ -62,7 +62,7 @@ export class LiveView { // Prepare common event data let options: commonEventManager.CommonEventPublishData = { data: 'data', - bundleName: AppStorage.get('bundleName'), + bundleName: AppStorage.get('liveViewLockScreenBundleName'), parameters: { 'roadData': routeInfo.roadInfo } @@ -117,7 +117,7 @@ export class LiveView { let wantAgentInfo: wantAgent.WantAgentInfo = { wants: [ { - bundleName: AppStorage.get('bundleName'), + bundleName: AppStorage.get('liveViewLockScreenBundleName'), abilityName: 'LiveviewlockscreensampleAbility' } ], diff --git a/liveviewlockscreenlibrary/src/main/ets/utils/LiveViewExtAbilityUtil.ets b/liveviewlockscreenlibrary/src/main/ets/utils/LiveViewExtAbilityUtil.ets index 6873e4a..d0f441f 100644 --- a/liveviewlockscreenlibrary/src/main/ets/utils/LiveViewExtAbilityUtil.ets +++ b/liveviewlockscreenlibrary/src/main/ets/utils/LiveViewExtAbilityUtil.ets @@ -88,7 +88,7 @@ export class LiveViewExtAbilityUtil { hilog.info(0x0000, TAG, '%{public}s', 'Succeeded in subscribe commonEvent success.'); if (data.parameters) { let laneData = data.parameters['roadData'] as LaneData; - AppStorage.setOrCreate('laneData', laneData); + AppStorage.setOrCreate('liveViewLockScreenLaneData', laneData); hilog.info(0x0000, TAG, '%{public}s', 'Succeeded in receive commonEvent.'); } }); @@ -111,7 +111,7 @@ export class LiveViewExtAbilityUtil { } else { currentHeightBreakpoint = BreakpointTypeEnum.MD; } - AppStorage.setOrCreate('currentHeightBreakpoint', currentHeightBreakpoint); + AppStorage.setOrCreate('liveViewLockScreenCurrentHeightBreakpoint', currentHeightBreakpoint); hilog.info(0x0000, TAG, '%{public}s', 'updateBreakpoint'); } catch (error) { hilog.error(0x0000, TAG, '%{public}s', diff --git a/liveviewlockscreenlibrary/src/main/ets/utils/LiveViewUtil.ets b/liveviewlockscreenlibrary/src/main/ets/utils/LiveViewUtil.ets index e860c88..bca604d 100644 --- a/liveviewlockscreenlibrary/src/main/ets/utils/LiveViewUtil.ets +++ b/liveviewlockscreenlibrary/src/main/ets/utils/LiveViewUtil.ets @@ -148,7 +148,7 @@ export class LiveViewUtil { private static async buildWantAgent(): Promise { const wantAgentInfo: wantAgent.WantAgentInfo = { wants: [{ - bundleName: AppStorage.get('bundleName'), + bundleName: AppStorage.get('liveViewLockScreenBundleName'), abilityName: 'LiveviewlockscreensampleAbility' } as Want], actionType: wantAgent.OperationType.START_ABILITIES, diff --git a/liveviewlockscreenlibrary/src/main/ets/utils/WindowUtil.ets b/liveviewlockscreenlibrary/src/main/ets/utils/WindowUtil.ets index dd1a7c9..5b756a6 100644 --- a/liveviewlockscreenlibrary/src/main/ets/utils/WindowUtil.ets +++ b/liveviewlockscreenlibrary/src/main/ets/utils/WindowUtil.ets @@ -58,13 +58,14 @@ export class WindowUtil { hilog.error(0x0000, TAG, `Failed to get main window. Cause code: ${error.code}, message: ${error.message}`); return; } - BreakpointSystem.getInstance().updateWidthHeightBp(data); - data.on('windowSizeChange', () => BreakpointSystem.getInstance().onWindowSizeChange(data)); + let uiContext: UIContext = data.getUIContext(); + BreakpointSystem.getInstance().updateWidthHeightBp(data, uiContext); + data.on('windowSizeChange', () => BreakpointSystem.getInstance().onWindowSizeChange(data, uiContext)); const windowObj: window.Window = data; const type: window.AvoidAreaType = window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR; const avoidArea: window.AvoidArea = windowObj.getWindowAvoidArea(type); const bottomRectHeight: number = avoidArea.bottomRect.height; - AppStorage.setOrCreate('bottomRectHeight', bottomRectHeight); + AppStorage.setOrCreate('liveViewLockScreenBottomRectHeight', bottomRectHeight); }) } } \ No newline at end of file diff --git a/liveviewlockscreenlibrary/src/main/ets/view/TrafficView.ets b/liveviewlockscreenlibrary/src/main/ets/view/TrafficView.ets index d76c80b..07b2be9 100644 --- a/liveviewlockscreenlibrary/src/main/ets/view/TrafficView.ets +++ b/liveviewlockscreenlibrary/src/main/ets/view/TrafficView.ets @@ -18,9 +18,9 @@ import { TrafficInfoView } from './TrafficInfoView'; @Component export struct TrafficView { - @StorageProp('bottomRectHeight') bottomRectHeight: number = 0; - @StorageLink('currentWidthBreakpoint') currentWidthBreakpoint: string = BreakpointTypeEnum.SM; - @StorageLink('currentHeightBreakpoint') currentHeightBreakpoint: string = BreakpointTypeEnum.LG; + @StorageProp('liveViewLockScreenBottomRectHeight') bottomRectHeight: number = 0; + @StorageLink('liveViewLockScreenCurrentWidthBreakpoint') currentWidthBreakpoint: string = BreakpointTypeEnum.SM; + @StorageLink('liveViewLockScreenCurrentHeightBreakpoint') currentHeightBreakpoint: string = BreakpointTypeEnum.LG; @Prop lane: number = 2; private uiContext: UIContext = this.getUIContext(); private updateInterval: number | undefined; diff --git a/liveviewlockscreensample/src/main/ets/entryability/EntryAbility.ets b/liveviewlockscreensample/src/main/ets/entryability/EntryAbility.ets index ffee266..438ac2c 100644 --- a/liveviewlockscreensample/src/main/ets/entryability/EntryAbility.ets +++ b/liveviewlockscreensample/src/main/ets/entryability/EntryAbility.ets @@ -34,14 +34,14 @@ export default class EntryAbility extends UIAbility { onWindowStageCreate(windowStage: window.WindowStage): void { // Main window is created, set main page for this ability hilog.info(0x0000, TAG, '%{public}s', 'Ability onWindowStageCreate'); - WindowUtil.requestFullScreen(windowStage); - WindowUtil.registerBreakPoint(windowStage); windowStage.loadContent('pages/Index', (error) => { if (error.code) { hilog.error(0x0000, TAG, '%{public}s', `Failed to load the content. Cause code: ${error.code}, message: ${error.message}`); return; } + WindowUtil.requestFullScreen(windowStage); + WindowUtil.registerBreakPoint(windowStage); hilog.info(0x0000, TAG, '%{public}s', 'Succeeded in loading the content.'); }); } diff --git a/liveviewlockscreensample/src/main/ets/pages/LockScreen.ets b/liveviewlockscreensample/src/main/ets/pages/LockScreen.ets index 510685c..9a31811 100644 --- a/liveviewlockscreensample/src/main/ets/pages/LockScreen.ets +++ b/liveviewlockscreensample/src/main/ets/pages/LockScreen.ets @@ -15,10 +15,7 @@ import { LockScreenPage } from 'liveviewlockscreenlibrary' -let storage: LocalStorage = LocalStorage.getShared(); - -@Entry(storage) -@Component +@Entry struct LockScreen { build() { Stack() { -- Gitee