diff --git a/liveviewlockscreenlibrary/src/main/ets/liveview/LockScreenPage.ets b/liveviewlockscreenlibrary/src/main/ets/liveview/LockScreenPage.ets index a16002716cb73df652d4185d43b7ba764203cf52..b28ab1c4e6c3026c3657d9f0d469dc8812ccffd0 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; + @StorageLink('liveViewLockScreenCurrentHeightBreakpoint') currentHeightBreakpoint: string = BreakpointTypeEnum.SM; + @StorageProp('liveViewLockScreenLaneData') 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 32e35c65d503cab3d0d5d78b11e6b70e242329e9..07785c67bed6ecd77d990ffb6fbe42d30bfa376f 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/BreakpointSystem.ets b/liveviewlockscreenlibrary/src/main/ets/utils/BreakpointSystem.ets index 52db322e254d514730ac04a405d311804e0ac1bb..b4b7552a4287db7c1b5468eaba626e7d8c4b2274 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 b08a358dc314deda473e0bd413500f53678835d4..fb2b875e66b0b2aefec8e22ee754ec62805bcb49 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 cc8fc390d0abcadbd29f0d057e55445c98e3e41e..58162634924d11e4c5954bc1b982d3b6215e09c4 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; } @@ -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 8422aa2e550d83f41edf2495997e13cc4dc50c3a..d0f441f64c82c7a59e000bd57e232165b423a9e4 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('liveViewLockScreenLaneData', 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. @@ -117,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 e860c886ba4fc630377d133eda8db940ccab7111..bca604d42507fa55cf9ce3a7b128fb81217bdeb0 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 dd1a7c9e0a8bf252cafbc73b91358121b256c0b5..5b756a6c5ddf983f39b1bf0250d0ad4d8097ee21 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/TrafficInfoView.ets b/liveviewlockscreenlibrary/src/main/ets/view/TrafficInfoView.ets index 6cccda4a26ad342d4ef6559f4b29ca9d3d6dd01c..c68869306caa34b08f7d4e6f09068f558d746e6a 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 7b27441321b235593d56094851e52181ce6ddb51..07b2be912fcddab03e02eeb0d87fda5dedbb6968 100644 --- a/liveviewlockscreenlibrary/src/main/ets/view/TrafficView.ets +++ b/liveviewlockscreenlibrary/src/main/ets/view/TrafficView.ets @@ -18,10 +18,11 @@ 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; 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), }) } diff --git a/liveviewlockscreensample/src/main/ets/entryability/EntryAbility.ets b/liveviewlockscreensample/src/main/ets/entryability/EntryAbility.ets index ffee2663a804ca3402859f40d2a306af9a994657..438ac2c685920cca7bef408b35c6979e5d8d693e 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 510685c5ca77db3dd260a03d141ba5d59432e426..9a31811ac4c78a9e50e2ecdebf54bec92bc88eba 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() {