diff --git a/README.md b/README.md index 1de218493770ec3a44c470d2e8d42fb8db7ea669..aae69dba611e07fc6f4fd9253068d80a2a7715f2 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,62 @@ 4. 本Sample仅提供在本地创建、更新与结束实况窗的能力(不能后台更新,若要后台更新,请使用Push Kit)。若要使用Push Kit更新,可参考[推送实况窗消息服务](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/push-update-liveview),用已有服务器调用对应的REST API。 +### 工程目录 + +``` +LiveViewLockScreen +├── liveviewlockscreenlibrary +│ ├── src/main +│ │ ├── ets +│ │ │ ├── common +│ │ │ │ └── Constants.ets // 公共常量 +│ │ │ ├── component +│ │ │ │ ├── IndexComponent.ets // 首页列表组件 +│ │ │ │ └── LockScreenComponent.ets // 锁屏实况窗组件 +│ │ │ ├── model +│ │ │ │ └── RouteDataModel.ets // 路线数据模型 +│ │ │ ├── utils +│ │ │ │ ├── BreakpointSystem.ets // 断点系统工具 +│ │ │ │ ├── BundleNameUtil.ets // 包名工具 +│ │ │ │ ├── LiveView.ets // 实况窗逻辑 +│ │ │ │ ├── LiveViewExtAbilityUtil.ets // 实况窗扩展 Ability 工具 +│ │ │ │ ├── LiveViewUtil.ets // 实况窗工具 +│ │ │ │ └── WindowUtil.ets // 窗口工具 +│ │ │ ├── view +│ │ │ │ ├── RoadView.ets // 道路显示 View +│ │ │ │ ├── TrafficInfoView.ets // 交通信息 View +│ │ │ │ └── TrafficView.ets // 实时路况 View +│ │ │ ├── viewmodel +│ │ │ │ └── RouteDataViewModel.ets // 路线数据 ViewModel +│ │ │ └── LiveViewLockScreenController.ets // 全局控制器 +│ │ ├── resources +│ │ └── module.json5 +│ ├── CHANGELOG.md +│ ├── Index.ets +│ ├── LICENSE +│ ├── README.md +│ ├── build-profile.json5 +│ ├── consumer-rules.txt +│ ├── hvigorfile.ts +│ ├── obfuscation-rules.txt +│ └── oh-package.json5 +└── liveviewlockscreensample +├── src/main +│ ├── ets +│ │ ├── entryability +│ │ │ └── EntryAbility.ets // 应用入口 Ability +│ │ ├── liveview +│ │ │ └── LiveViewExtAbility.ets // 实况窗扩展 Ability +│ │ └── pages +│ │ ├── Index.ets // 首页(调用库组件演示) +│ │ └── LockScreen.ets // 锁屏实况窗页 +│ └── resources +├── build-profile.json5 +├── hvigorfile.ts +├── obfuscation-rules.txt +└── oh-package.json5 +``` + ## 具体实现 1. HomePage主页面在创建后,创建实况窗和创建定时任务,每间隔固定时间调用liveViewManager.updateLiveView()接口更新实况窗,并通过commonEventManager.publish()接口传递新的参数给沉浸实况窗页面,实现实况窗和沉浸实况窗更新。 @@ -42,8 +98,8 @@ 1.本示例仅支持标准系统上运行,支持设备:华为手机; -2.HarmonyOS系统:HarmonyOS 5.0.0 Developer Release及以上; +2.HarmonyOS系统:HarmonyOS 5.0.0 Release及以上; -3.DevEco Studio版本:DevEco Studio 5.0.0 Developer Release及以上; +3.DevEco Studio版本:DevEco Studio 5.0.0 Release及以上; -4.HarmonyOS SDK版本:HarmonyOS 5.0.0 Developer Release SDK及以上。 \ No newline at end of file +4.HarmonyOS SDK版本:HarmonyOS 5.0.0 Release SDK及以上。 \ No newline at end of file diff --git a/build-profile.json5 b/build-profile.json5 index 246f563696a0a8ededa01b00995b75c8c71ae6ff..e000b4cc944bad767639211026fd3d587914d7d0 100644 --- a/build-profile.json5 +++ b/build-profile.json5 @@ -5,6 +5,7 @@ { "name": "default", "signingConfig": "default", + "targetSdkVersion": "5.0.0(12)", "compatibleSdkVersion": "5.0.0(12)", "runtimeOS": "HarmonyOS", } diff --git a/liveviewlockscreenlibrary/Index.ets b/liveviewlockscreenlibrary/Index.ets index a91d0a7d207c5d8fe9c7a6a70ee90a508dbd35e2..3b29927499e08bb61a88791482d30279eb9d7d8a 100644 --- a/liveviewlockscreenlibrary/Index.ets +++ b/liveviewlockscreenlibrary/Index.ets @@ -1,11 +1,5 @@ -export { LiveViewLockScreenPage } from './src/main/ets/pages/LiveViewLockScreenPage'; +export { IndexComponent } from './src/main/ets/component/IndexComponent'; -export { LockScreenPage } from './src/main/ets/liveview/LockScreenPage'; +export { LockScreenComponent } from './src/main/ets/component/LockScreenComponent'; -export { BreakpointSystem, BreakpointTypeEnum } from './src/main/ets/utils/BreakpointSystem'; - -export { BundleNameUtil } from './src/main/ets/utils/BundleNameUtil'; - -export { LiveViewExtAbilityUtil } from './src/main/ets/utils/LiveViewExtAbilityUtil'; - -export { WindowUtil } from './src/main/ets/utils/WindowUtil'; \ No newline at end of file +export { LiveViewLockScreenController } from './src/main/ets/LiveViewLockScreenController'; \ No newline at end of file diff --git a/liveviewlockscreenlibrary/README.md b/liveviewlockscreenlibrary/README.md index f52c07b0c8ab1436af25918ab6e6c32775f00aa3..d8b25d5a237fe77aaafc0768900c1a97a8eb9b0c 100644 --- a/liveviewlockscreenlibrary/README.md +++ b/liveviewlockscreenlibrary/README.md @@ -28,17 +28,49 @@ ohpm install @ohos_samples/liveviewlockscreenlibrary import { LiveViewLockScreenPage, BundleNameUtil, WindowUtil } from '@ohos_samples/liveviewlockscreenlibrary'; ``` -按需在文件中使用导出模块即可,其中LiveViewLockScreenPage是整个sample的入口页面,进入Sample后如果有实况窗和锁屏沉浸实况窗权限,会自动创建实况窗和锁屏沉浸实况窗;点击停止导航,会关闭实况窗和锁屏沉浸实况窗,点击继续导航,会重新创建实况窗和锁屏沉浸实况窗。BundleNameUtil封装了获取包名的功能;WindowUtil封装了沉浸式、设备断点判断、避让区域计算等窗口能力。示例如下: +按需在文件中使用导出模块即可,其中IndexComponent是整个sample的入口页面,进入Sample后如果有实况窗和锁屏沉浸实况窗权限,会自动创建实况窗和锁屏沉浸实况窗LockScreenComponent;点击停止导航,会关闭实况窗和锁屏沉浸实况窗,点击继续导航,会重新创建实况窗和锁屏沉浸实况窗。LiveViewLockScreenController封装了获取包名的功能,已经沉浸式、设备断点判断、避让区域计算等窗口能力。示例如下: ``` -// Page.ets -import { LiveViewLockScreenPage } from '@ohos_samples/liveviewlockscreenlibrary'; +// Index.ets +import { IndexComponent } from '@ohos_samples/liveviewlockscreenlibrary'; Stack() { - LiveViewLockScreenPage() + IndexComponent() +} + +// LockScreen.ets +import { LockScreenComponent } from '@ohos_samples/liveviewlockscreenlibrary'; +Stack() { + LockScreenComponent() } // EntryAbility.ets -import { BundleNameUtil, WindowUtil } from '@ohos_samples/liveviewlockscreenlibrary'; -BundleNameUtil.getBundleName(); -WindowUtil.requestFullScreen(windowStage); -WindowUtil.registerBreakPoint(windowStage); +import { LiveViewLockScreenController } from '@ohos_samples/liveviewlockscreenlibrary'; +onCreate(): void { + LiveViewLockScreenController.saveBundleName(); +} +onWindowStageCreate(windowStage: window.WindowStage): void { + 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; + } + LiveViewLockScreenController.initWindowConfig(windowStage); + hilog.info(0x0000, TAG, '%{public}s', 'Succeeded in loading the content.'); + }); +} + +// LiveViewExtAbility.ets +import { LiveViewLockScreenController } from 'liveviewlockscreenlibrary'; +onSessionCreate(_want: Want, session: UIExtensionContentSession): void { + LiveViewLockScreenController.initLiveView(session); + try { + session.loadContent('pages/LockScreen'); + } catch (error) { + hilog.error(0x0000, TAG, '%{public}s', + `Session load content fail. code is ${(error as BusinessError).code} message is ${(error as BusinessError).message}`); + } +} +onSessionDestroy(): void { + LiveViewLockScreenController.destroyLLiveView(); +} ``` \ No newline at end of file diff --git a/liveviewlockscreenlibrary/build-profile.json5 b/liveviewlockscreenlibrary/build-profile.json5 index 312d38eb08629793b3484c7373213f22840c8d82..cda3307123ec7c43181580a86ef7e82a18319a34 100644 --- a/liveviewlockscreenlibrary/build-profile.json5 +++ b/liveviewlockscreenlibrary/build-profile.json5 @@ -8,7 +8,7 @@ "arkOptions": { "obfuscation": { "ruleOptions": { - "enable": true, + "enable": false, "files": [ "./obfuscation-rules.txt" ] diff --git a/liveviewlockscreenlibrary/src/main/ets/LiveViewLockScreenController.ets b/liveviewlockscreenlibrary/src/main/ets/LiveViewLockScreenController.ets new file mode 100644 index 0000000000000000000000000000000000000000..fe6ef8a44deac0b111302ec311a68c1f477a6e8c --- /dev/null +++ b/liveviewlockscreenlibrary/src/main/ets/LiveViewLockScreenController.ets @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { UIExtensionContentSession } from '@kit.AbilityKit'; +import { window } from '@kit.ArkUI'; +import { BundleNameUtil } from './utils/BundleNameUtil'; +import { LiveViewExtAbilityUtil } from './utils/LiveViewExtAbilityUtil'; +import { WindowUtil } from './utils/WindowUtil'; + +export class LiveViewLockScreenController { + public static initWindowConfig(windowStage: window.WindowStage): void { + WindowUtil.initialize(windowStage); + } + + public static saveBundleName(): void { + BundleNameUtil.saveBundleName(); + } + + public static initLiveView(session: UIExtensionContentSession): void { + LiveViewExtAbilityUtil.getInstance().setSession(session); + } + + public static destroyLLiveView(): void { + LiveViewExtAbilityUtil.getInstance().destroySubscribe(); + } +} \ No newline at end of file diff --git a/liveviewlockscreenlibrary/src/main/ets/common/Constants.ets b/liveviewlockscreenlibrary/src/main/ets/common/Constants.ets new file mode 100644 index 0000000000000000000000000000000000000000..a7800f3fefee2670b126215339cbe91cf853bb0b --- /dev/null +++ b/liveviewlockscreenlibrary/src/main/ets/common/Constants.ets @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export class Constants { + /* + * AppStorage key prefix. + */ + public static readonly KEY_PREFIX: string = 'liveviewlockscreen'; +} \ No newline at end of file diff --git a/liveviewlockscreenlibrary/src/main/ets/pages/LiveViewLockScreenPage.ets b/liveviewlockscreenlibrary/src/main/ets/component/IndexComponent.ets similarity index 95% rename from liveviewlockscreenlibrary/src/main/ets/pages/LiveViewLockScreenPage.ets rename to liveviewlockscreenlibrary/src/main/ets/component/IndexComponent.ets index 07785c67bed6ecd77d990ffb6fbe42d30bfa376f..8b216d06fdf58caa02c056a9be348c60f5e67642 100644 --- a/liveviewlockscreenlibrary/src/main/ets/pages/LiveViewLockScreenPage.ets +++ b/liveviewlockscreenlibrary/src/main/ets/component/IndexComponent.ets @@ -77,7 +77,7 @@ struct TipDialog { } @Component -export struct LiveViewLockScreenPage { +export struct IndexComponent { private customDialogController: CustomDialogController = new CustomDialogController({ builder: TipDialog({ onConfirm: () => { @@ -97,7 +97,7 @@ export struct LiveViewLockScreenPage { }); aboutToAppear(): void { - LiveView.getInstance(this.getUIContext().getHostContext()).createLiveView(); + LiveView.getInstance(this.getUIContext().getHostContext() as Context).createLiveView(); this.customDialogController?.open(); } diff --git a/liveviewlockscreenlibrary/src/main/ets/liveview/LockScreenPage.ets b/liveviewlockscreenlibrary/src/main/ets/component/LockScreenComponent.ets similarity index 79% rename from liveviewlockscreenlibrary/src/main/ets/liveview/LockScreenPage.ets rename to liveviewlockscreenlibrary/src/main/ets/component/LockScreenComponent.ets index b28ab1c4e6c3026c3657d9f0d469dc8812ccffd0..2655cbc493046de6890598b0e0d00e95c8eb9fb3 100644 --- a/liveviewlockscreenlibrary/src/main/ets/liveview/LockScreenPage.ets +++ b/liveviewlockscreenlibrary/src/main/ets/component/LockScreenComponent.ets @@ -13,14 +13,16 @@ * limitations under the License. */ +import { Constants } from '../common/Constants'; import { LaneData } from '../model/RouteDataModel'; import { BreakpointType, BreakpointTypeEnum } from '../utils/BreakpointSystem'; import { RoadView } from '../view/RoadView'; -@Entry -export struct LockScreenPage { - @StorageLink('liveViewLockScreenCurrentHeightBreakpoint') currentHeightBreakpoint: string = BreakpointTypeEnum.SM; - @StorageProp('liveViewLockScreenLaneData') roadInfo: LaneData | undefined = undefined; +@Component +export struct LockScreenComponent { + @StorageLink(Constants.KEY_PREFIX + 'CurrentHeightBreakpoint') currentHeightBreakpoint: string = + BreakpointTypeEnum.SM; + @StorageLink(Constants.KEY_PREFIX + 'LaneData') 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 b4b7552a4287db7c1b5468eaba626e7d8c4b2274..a97859690a26027d9baf74cd6a48218e2c5e57eb 100644 --- a/liveviewlockscreenlibrary/src/main/ets/utils/BreakpointSystem.ets +++ b/liveviewlockscreenlibrary/src/main/ets/utils/BreakpointSystem.ets @@ -16,6 +16,7 @@ import { window } from '@kit.ArkUI'; import type { BusinessError } from '@kit.BasicServicesKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; +import { Constants } from '../common/Constants'; const TAG: string = '[BreakpointSystem]'; @@ -73,8 +74,8 @@ export class BreakpointSystem { private currentHeightBreakpoint: BreakpointTypeEnum = BreakpointTypeEnum.LG; private constructor() { - AppStorage.setOrCreate('liveViewLockScreenCurrentWidthBreakpoint', this.currentWidthBreakpoint); - AppStorage.setOrCreate('liveViewLockScreenCurrentHeightBreakpoint', this.currentHeightBreakpoint); + AppStorage.setOrCreate(Constants.KEY_PREFIX + 'CurrentWidthBreakpoint', this.currentWidthBreakpoint); + AppStorage.setOrCreate(Constants.KEY_PREFIX + 'CurrentHeightBreakpoint', this.currentHeightBreakpoint); } public static getInstance(): BreakpointSystem { @@ -87,28 +88,28 @@ export class BreakpointSystem { public updateCurrentWidthBreakpoint(widthBreakpoint: BreakpointTypeEnum): void { if (this.currentWidthBreakpoint !== widthBreakpoint) { this.currentWidthBreakpoint = widthBreakpoint; - AppStorage.setOrCreate('liveViewLockScreenCurrentWidthBreakpoint', this.currentWidthBreakpoint); + AppStorage.setOrCreate(Constants.KEY_PREFIX + 'CurrentWidthBreakpoint', this.currentWidthBreakpoint); } } public updateCurrentHeightBreakpoint(heightBreakpoint: BreakpointTypeEnum): void { if (this.currentHeightBreakpoint !== heightBreakpoint) { this.currentHeightBreakpoint = heightBreakpoint; - AppStorage.setOrCreate('liveViewLockScreenCurrentHeightBreakpoint', this.currentHeightBreakpoint); + AppStorage.setOrCreate(Constants.KEY_PREFIX + 'CurrentHeightBreakpoint', this.currentHeightBreakpoint); } } - public onWindowSizeChange(window: window.Window, uiContent: UIContext): void { - this.updateWidthHeightBp(window, uiContent); + public onWindowSizeChange(window: window.Window): void { + this.updateWidthHeightBp(window); } - public updateWidthHeightBp(window: window.Window, uiContent: UIContext): void { + public updateWidthHeightBp(window: window.Window): void { try { const mainWindow: window.WindowProperties = window.getWindowProperties(); const windowWidth: number = mainWindow.windowRect.width; const windowHeight: number = mainWindow.windowRect.height; - const windowWidthVp = uiContent.px2vp(windowWidth); - const windowHeightVp = uiContent.px2vp(windowHeight); + const windowWidthVp = window.getUIContext().px2vp(windowWidth); + const windowHeightVp = window.getUIContext().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 fb2b875e66b0b2aefec8e22ee754ec62805bcb49..ca7a838e8da77fe06e63aece9c716d3cf6936166 100644 --- a/liveviewlockscreenlibrary/src/main/ets/utils/BundleNameUtil.ets +++ b/liveviewlockscreenlibrary/src/main/ets/utils/BundleNameUtil.ets @@ -16,15 +16,16 @@ import { bundleManager } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; +import { Constants } from '../common/Constants'; const TAG: string = '[BundleNameUtil]'; export class BundleNameUtil { - public static getBundleName(): void { + public static saveBundleName(): void { try { bundleManager.getBundleInfoForSelf(bundleManager.BundleFlag.GET_BUNDLE_INFO_DEFAULT).then((data) => { hilog.info(0x0000, TAG, '%{public}s', 'getBundleInfoForSelf successfully.'); - AppStorage.setOrCreate('liveViewLockScreenBundleName', data.name); + AppStorage.setOrCreate(Constants.KEY_PREFIX + 'BundleName', 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 58162634924d11e4c5954bc1b982d3b6215e09c4..b35ba8e169fdf1af068333be3d7b805c042f9805 100644 --- a/liveviewlockscreenlibrary/src/main/ets/utils/LiveView.ets +++ b/liveviewlockscreenlibrary/src/main/ets/utils/LiveView.ets @@ -17,6 +17,7 @@ import { appManager, WantAgent, wantAgent } from '@kit.AbilityKit'; import { backgroundTaskManager } from '@kit.BackgroundTasksKit'; import { BusinessError, commonEventManager } from '@kit.BasicServicesKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; +import { Constants } from '../common/Constants'; import { RouteData } from '../model/RouteDataModel'; import { LiveViewUtil } from './LiveViewUtil'; import { getRouteData } from '../viewmodel/RouteDataViewModel'; @@ -26,18 +27,18 @@ const TAG: string = '[LiveView]'; export class LiveView { // Singleton pattern implementation private static instance: LiveView; - private context: Context | undefined; + private context: Context; private liveViewController: LiveViewUtil; private updateInterval: number | undefined; // Private constructor for singleton pattern - private constructor(context: Context | undefined) { + private constructor(context: Context) { this.context = context; - this.liveViewController = new LiveViewUtil(); + this.liveViewController = new LiveViewUtil(context); } // Singleton accessor method - public static getInstance(context: Context | undefined): LiveView { + public static getInstance(context: Context): LiveView { // Lazy initialization of the singleton instance if (!LiveView.instance) { LiveView.instance = new LiveView(context); @@ -62,7 +63,7 @@ export class LiveView { // Prepare common event data let options: commonEventManager.CommonEventPublishData = { data: 'data', - bundleName: AppStorage.get('liveViewLockScreenBundleName'), + bundleName: AppStorage.get(Constants.KEY_PREFIX + 'BundleName'), parameters: { 'roadData': routeInfo.roadInfo } @@ -114,10 +115,10 @@ export class LiveView { // Internal method to manage background tasks private startContinuousRunningTask() { // Configure WantAgent for background operation - let wantAgentInfo: wantAgent.WantAgentInfo = { + const wantAgentInfo: wantAgent.WantAgentInfo = { wants: [ { - bundleName: AppStorage.get('liveViewLockScreenBundleName'), + bundleName: AppStorage.get(Constants.KEY_PREFIX + 'BundleName'), abilityName: 'LiveviewlockscreensampleAbility' } ], diff --git a/liveviewlockscreenlibrary/src/main/ets/utils/LiveViewExtAbilityUtil.ets b/liveviewlockscreenlibrary/src/main/ets/utils/LiveViewExtAbilityUtil.ets index d0f441f64c82c7a59e000bd57e232165b423a9e4..487fd04e2c6f6b16f266145575322484e4ff1f5a 100644 --- a/liveviewlockscreenlibrary/src/main/ets/utils/LiveViewExtAbilityUtil.ets +++ b/liveviewlockscreenlibrary/src/main/ets/utils/LiveViewExtAbilityUtil.ets @@ -16,10 +16,11 @@ import { bundleManager, UIExtensionContentSession } from '@kit.AbilityKit'; import { display, window } from '@kit.ArkUI'; -import { BreakpointTypeEnum } from './BreakpointSystem'; -import { hilog } from '@kit.PerformanceAnalysisKit'; import { BusinessError, commonEventManager } from '@kit.BasicServicesKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { Constants } from '../common/Constants'; import { LaneData } from '../model/RouteDataModel'; +import { BreakpointTypeEnum } from './BreakpointSystem'; const TAG: string = '[LiveViewExtAbilityUtil]'; @@ -44,29 +45,30 @@ export class LiveViewExtAbilityUtil { public setSession(session: UIExtensionContentSession): void { // Window size listener. - const extensionWindow = session.getUIExtensionWindowProxy(); - extensionWindow.on('windowSizeChange', (windowSize: window.Size) => { - LiveViewExtAbilityUtil.updateBreakPoint(windowSize); - }); - - // Initialize event subscription. - let bundleName: string = ''; try { - bundleManager.getBundleInfoForSelf(bundleManager.BundleFlag.GET_BUNDLE_INFO_DEFAULT).then((data) => { - hilog.info(0x0000, TAG, '%{public}s', 'getBundleInfoForSelf successfully.'); - bundleName = data.name; - }).catch((error: BusinessError) => { - hilog.error(0x0000, TAG, '%{public}s', - `getBundleInfoForSelf failed. code is ${error.code} message is ${error.message}`); + session.getUIExtensionWindowProxy().on('windowSizeChange', (windowSize: window.Size) => { + LiveViewExtAbilityUtil.updateBreakPoint(windowSize); }); } catch (error) { hilog.error(0x0000, TAG, '%{public}s', - `getBundleInfoForSelf failed. code is ${(error as BusinessError).code} message is ${(error as BusinessError).message}`); + `Session update break point failed. code: ${(error as BusinessError).code} message is ${(error as BusinessError).message}`); } + LiveViewExtAbilityUtil.getInstance().createSubscriber(); + } + + public destroySubscribe(): void { + commonEventManager.unsubscribe(this.subscriber, () => { + this.subscriber = null; + }); + } + + private createSubscriber(): void { + // Initialize event subscription. + let bundleName: string = LiveViewExtAbilityUtil.getBundleName(); let subscribeInfo: commonEventManager.CommonEventSubscribeInfo = { events: ['live_view_lock_screen'], publisherBundleName: bundleName, - priority: 0 + priority: 0, }; commonEventManager.createSubscriber(subscribeInfo, (error: BusinessError, data: commonEventManager.CommonEventSubscriber) => { @@ -88,12 +90,11 @@ 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('liveViewLockScreenLaneData', laneData); + AppStorage.setOrCreate(Constants.KEY_PREFIX + 'LaneData', laneData); hilog.info(0x0000, TAG, '%{public}s', 'Succeeded in receive commonEvent.'); } }); }) - session.loadContent('pages/LockScreen'); } // Distinguish page layout using vertical breakpoints. @@ -111,7 +112,7 @@ export class LiveViewExtAbilityUtil { } else { currentHeightBreakpoint = BreakpointTypeEnum.MD; } - AppStorage.setOrCreate('liveViewLockScreenCurrentHeightBreakpoint', currentHeightBreakpoint); + AppStorage.setOrCreate(Constants.KEY_PREFIX + 'CurrentHeightBreakpoint', currentHeightBreakpoint); hilog.info(0x0000, TAG, '%{public}s', 'updateBreakpoint'); } catch (error) { hilog.error(0x0000, TAG, '%{public}s', @@ -120,9 +121,21 @@ export class LiveViewExtAbilityUtil { } } - public destroySubscribe(): void { - commonEventManager.unsubscribe(this.subscriber, () => { - this.subscriber = null; - }); + private static getBundleName(): string { + let bundleName: string = ''; + try { + bundleManager.getBundleInfoForSelf(bundleManager.BundleFlag.GET_BUNDLE_INFO_DEFAULT).then((data) => { + bundleName = data.name; + hilog.info(0x0000, TAG, '%{public}s', 'getBundleInfoForSelf successfully.'); + }).catch((error: BusinessError) => { + hilog.error(0x0000, TAG, '%{public}s', + `getBundleInfoForSelf failed. code is ${error.code} message is ${error.message}`); + }); + } catch (error) { + hilog.error(0x0000, TAG, '%{public}s', + `getBundleInfoForSelf failed. code is ${(error as BusinessError).code} message is ${(error as BusinessError).message}`); + } + hilog.info(0x0000, TAG, '%{public}s', 'Ability onCreate'); + return bundleName; } } \ No newline at end of file diff --git a/liveviewlockscreenlibrary/src/main/ets/utils/LiveViewUtil.ets b/liveviewlockscreenlibrary/src/main/ets/utils/LiveViewUtil.ets index bca604d42507fa55cf9ce3a7b128fb81217bdeb0..596c32aa09a80822da91ad48aac9300bdfc8bd44 100644 --- a/liveviewlockscreenlibrary/src/main/ets/utils/LiveViewUtil.ets +++ b/liveviewlockscreenlibrary/src/main/ets/utils/LiveViewUtil.ets @@ -13,26 +13,31 @@ * limitations under the License. */ -import { application, Want, wantAgent } from '@kit.AbilityKit'; +import { Want, wantAgent } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; import { liveViewManager } from '@kit.LiveViewKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; +import { Constants } from '../common/Constants'; import { RouteData } from '../model/RouteDataModel'; const TAG: string = '[LiveViewUtil]'; export class LiveViewUtil { - private context = application.getApplicationContext(); + private context: Context; private defaultLiveView?: liveViewManager.LiveView; private sequence: number = 0; + public constructor(context: Context) { + this.context = context; + } + public async startLiveView(params: RouteData) { // Verify that the live view switch is turned on if (!await LiveViewUtil.isLiveViewEnabled()) { hilog.error(0x0000, TAG, '%{public}s', 'Live view is disabled.'); return; } - this.defaultLiveView = await this.createPrimaryLiveVie(); + this.defaultLiveView = await this.createPrimaryLiveView(); this.updateLiveViewInfo(params); return await liveViewManager.startLiveView(this.defaultLiveView); @@ -62,7 +67,7 @@ export class LiveViewUtil { public async closeLiveView() { // Ensure that the sequence is greater than the current live window page. this.sequence++; - this.defaultLiveView = await this.createPrimaryLiveVie(); + this.defaultLiveView = await this.createPrimaryLiveView(); await liveViewManager.stopLiveView(this.defaultLiveView).then(() => { this.sequence = 0; this.defaultLiveView = undefined; @@ -87,13 +92,12 @@ export class LiveViewUtil { type: liveViewManager.CapsuleType.CAPSULE_TYPE_TEXT, status: 1, icon: 'turn_right_light_square.png', - backgroundColor: this.context.resourceManager.getStringSync($r('app.string.live_view_background').id), - title: `${params.distance}${this.context.resourceManager.getStringSync($r('app.string.meter').id)}`, + backgroundColor: this.getStringSync($r('app.string.live_view_background').id), + title: `${params.distance}${this.getStringSync($r('app.string.meter').id)}`, } liveView.liveViewData.primary.title = - `${this.context.resourceManager.getStringSync($r('app.string.live_view_primary_title_first') - .id)}${params.distance}${this.context.resourceManager.getStringSync($r('app.string.live_view_primary_title_second') - .id)}`; + `${this.getStringSync($r('app.string.live_view_primary_title_first') + .id)}${params.distance}${this.getStringSync($r('app.string.live_view_primary_title_second').id)}`; liveView.liveViewData.primary.content = [{ text: this.context.resourceManager.getStringSync(params.roadName) }]; liveView.liveViewData.primary.liveViewLockScreenAbilityName = 'LiveViewExtAbility'; liveView.liveViewData.primary.liveViewLockScreenAbilityParameters = { '': '' }; @@ -114,9 +118,9 @@ export class LiveViewUtil { this.defaultLiveView = liveView; } - private async createPrimaryLiveVie(): Promise { + private async createPrimaryLiveView(): Promise { // Construct live window request body. - let liveView: liveViewManager.LiveView = { + const liveView: liveViewManager.LiveView = { id: 0, sequence: this.sequence, // Application scenarios of the live window. NAVIGATION: Navigation. @@ -127,13 +131,13 @@ export class LiveViewUtil { type: liveViewManager.CapsuleType.CAPSULE_TYPE_TEXT, status: 1, icon: 'turn_right_light_square.png', - backgroundColor: this.context.resourceManager.getStringSync($r('app.string.live_view_background').id), - title: this.context.resourceManager.getStringSync($r('app.string.live_view_title').id), + backgroundColor: this.getStringSync($r('app.string.live_view_background').id), + title: this.getStringSync($r('app.string.live_view_title').id), }, // Live view card related parameters primary: { - title: this.context.resourceManager.getStringSync($r('app.string.live_view_title').id), - content: [{ text: this.context.resourceManager.getStringSync($r('app.string.live_view_content').id) }], + title: this.getStringSync($r('app.string.live_view_title').id), + content: [{ text: this.getStringSync($r('app.string.live_view_content').id) }], // Add LiveViewLockScreenExtensionAbility name to build lock screen live view liveViewLockScreenAbilityName: 'LiveViewExtAbility', liveViewLockScreenAbilityParameters: { liveViewParameters: '' }, @@ -148,7 +152,7 @@ export class LiveViewUtil { private static async buildWantAgent(): Promise { const wantAgentInfo: wantAgent.WantAgentInfo = { wants: [{ - bundleName: AppStorage.get('liveViewLockScreenBundleName'), + bundleName: AppStorage.get(Constants.KEY_PREFIX + 'BundleName'), abilityName: 'LiveviewlockscreensampleAbility' } as Want], actionType: wantAgent.OperationType.START_ABILITIES, @@ -158,4 +162,15 @@ export class LiveViewUtil { const agent: object = await wantAgent.getWantAgent(wantAgentInfo); return agent; } + + private getStringSync(resId: number): string { + let stringSyn: string = ''; + try { + stringSyn = this.context.resourceManager.getStringSync(resId); + } catch (error) { + hilog.error(0x0000, TAG, '%{public}s', + `Get string failed. code: ${(error as BusinessError).code} message is ${(error as BusinessError).message}`); + } + return stringSyn; + } } \ No newline at end of file diff --git a/liveviewlockscreenlibrary/src/main/ets/utils/WindowUtil.ets b/liveviewlockscreenlibrary/src/main/ets/utils/WindowUtil.ets index 5b756a6c5ddf983f39b1bf0250d0ad4d8097ee21..5483c97ac1fd3408b43571d7dc927ba16b82c279 100644 --- a/liveviewlockscreenlibrary/src/main/ets/utils/WindowUtil.ets +++ b/liveviewlockscreenlibrary/src/main/ets/utils/WindowUtil.ets @@ -16,56 +16,77 @@ import { window } from '@kit.ArkUI'; import type { BusinessError } from '@kit.BasicServicesKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; +import { Constants } from '../common/Constants'; import { BreakpointSystem } from './BreakpointSystem'; const TAG: string = '[WindowUtil]'; export class WindowUtil { - public static requestFullScreen(windowStage: window.WindowStage): void { - windowStage.getMainWindow((error: BusinessError, data: window.Window) => { - if (error.code) { - hilog.error(0x0000, TAG, '%{public}s', - `Failed to getMainWindow. Cause code: ${error.code}, message: ${error.message}`); - return; - } - const windowObj: window.Window = data; - try { - windowObj.setWindowSystemBarProperties({ - statusBarContentColor: '#ffffff', - }).then(() => { - hilog.info(0x0000, TAG, '%{public}s', 'Succeeded in setting the system bar properties.'); - }).catch((error: BusinessError) => { - hilog.error(0x0000, TAG, '%{public}s', - `Failed to set the window layout to full-screen mode. Cause code: ${error.code}, message: ${error.message}`); - }); + private static windowClass?: window.Window; - windowObj.setWindowLayoutFullScreen(true).then(() => { - hilog.info(0x0000, TAG, '%{public}s', 'Succeeded in setting the window layout to full-screen mode'); - }).catch((error: BusinessError) => { - hilog.error(0x0000, TAG, '%{public}s', - `Failed to set the window layout to full-screen mode. Cause code: ${error.code}, message: ${error.message}`); - }); - } catch (error) { - hilog.error(0x0000, TAG, '%{public}s', - `onWindowStageCreate failed. Cause code: ${error.code}, message: ${error.message}`); - } - }); + public static initialize(windowStage: window.WindowStage) { + try { + WindowUtil.windowClass = windowStage.getMainWindowSync(); + const uiContext: UIContext = WindowUtil.windowClass.getUIContext(); + AppStorage.setOrCreate(Constants.KEY_PREFIX + 'UIContext', uiContext); + WindowUtil.registerBreakPoint(WindowUtil.windowClass); + WindowUtil.requestFullScreen(WindowUtil.windowClass); + } catch (err) { + hilog.error(0x0000, TAG, `WindowUtil initialize failed. Cause: ${err.code} ${err.message}`); + } + } + + private static registerBreakPoint(windowClass: window.Window) { + try { + BreakpointSystem.getInstance().onWindowSizeChange(windowClass); + windowClass.on('windowSizeChange', () => BreakpointSystem.getInstance().onWindowSizeChange(windowClass)); + + const navIndicatorAvoidArea: window.AvoidArea = + windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR); + WindowUtil.setAvoidArea(window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR, navIndicatorAvoidArea); + windowClass.on('avoidAreaChange', (avoidAreaOption) => { + WindowUtil.setAvoidArea(avoidAreaOption.type, avoidAreaOption.area); + }); + } catch (e) { + const error = e as BusinessError; + hilog.error(0x0000, TAG, + `Get windowAvoidArea or Register windowSizeChange failed. code: ${error.code}, message: ${error.message}`); + } } - public static registerBreakPoint(windowStage: window.WindowStage) { - windowStage.getMainWindow((error: BusinessError, data: window.Window) => { - if (error.code) { - hilog.error(0x0000, TAG, `Failed to get main window. Cause code: ${error.code}, message: ${error.message}`); - return; + // Get status indicator height. + private static setAvoidArea(type: window.AvoidAreaType, area: window.AvoidArea) { + try { + if (type === window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR) { + AppStorage.setOrCreate(Constants.KEY_PREFIX + 'NaviIndicatorHeight', + WindowUtil.windowClass?.getUIContext().px2vp(area.bottomRect.height)); } - 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('liveViewLockScreenBottomRectHeight', bottomRectHeight); - }) + } catch (error) { + hilog.error(0x0000, TAG, '%{public}s', + `Get windowAvoidArea or Register windowSizeChange failed. code: ${(error as BusinessError).code} message is ${(error as BusinessError).message}`); + } + } + + private static requestFullScreen(windowClass: window.Window): void { + try { + windowClass.setWindowSystemBarProperties({ + statusBarContentColor: '#ffffff', + }).then(() => { + hilog.info(0x0000, TAG, '%{public}s', 'Succeeded in setting the system bar properties.'); + }).catch((error: BusinessError) => { + hilog.error(0x0000, TAG, '%{public}s', + `Failed to set the window layout to full-screen mode. Cause code: ${error.code}, message: ${error.message}`); + }); + + windowClass.setWindowLayoutFullScreen(true).then(() => { + hilog.info(0x0000, TAG, '%{public}s', 'Succeeded in setting the window layout to full-screen mode'); + }).catch((error: BusinessError) => { + hilog.error(0x0000, TAG, '%{public}s', + `Failed to set the window layout to full-screen mode. Cause code: ${error.code}, message: ${error.message}`); + }); + } catch (error) { + hilog.error(0x0000, TAG, '%{public}s', + `onWindowStageCreate failed. Cause code: ${error.code}, message: ${error.message}`); + } } } \ 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 c68869306caa34b08f7d4e6f09068f558d746e6a..06439234029c08de6ba6f38761e100e218b63804 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(this.getUIContext().getHostContext()).createLiveView(); + LiveView.getInstance(this.getUIContext().getHostContext() as Context).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(this.getUIContext().getHostContext()).finishLiveView(); + LiveView.getInstance(this.getUIContext().getHostContext() as Context).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 07b2be912fcddab03e02eeb0d87fda5dedbb6968..e7d0cd142d9ca66313704841d34453432627ca96 100644 --- a/liveviewlockscreenlibrary/src/main/ets/view/TrafficView.ets +++ b/liveviewlockscreenlibrary/src/main/ets/view/TrafficView.ets @@ -13,14 +13,16 @@ * limitations under the License. */ +import { Constants } from '../common/Constants'; import { BreakpointType, BreakpointTypeEnum } from '../utils/BreakpointSystem'; import { TrafficInfoView } from './TrafficInfoView'; @Component export struct TrafficView { - @StorageProp('liveViewLockScreenBottomRectHeight') bottomRectHeight: number = 0; - @StorageLink('liveViewLockScreenCurrentWidthBreakpoint') currentWidthBreakpoint: string = BreakpointTypeEnum.SM; - @StorageLink('liveViewLockScreenCurrentHeightBreakpoint') currentHeightBreakpoint: string = BreakpointTypeEnum.LG; + @StorageLink(Constants.KEY_PREFIX + 'NaviIndicatorHeight') naviIndicatorHeight: number = 0; + @StorageLink(Constants.KEY_PREFIX + 'CurrentWidthBreakpoint') currentWidthBreakpoint: string = BreakpointTypeEnum.SM; + @StorageLink(Constants.KEY_PREFIX + 'CurrentHeightBreakpoint') currentHeightBreakpoint: string = + BreakpointTypeEnum.LG; @Prop lane: number = 2; private uiContext: UIContext = this.getUIContext(); private updateInterval: number | undefined; @@ -226,7 +228,7 @@ export struct TrafficView { }) .padding({ bottom: new BreakpointType({ - sm: this.uiContext.px2vp(this.bottomRectHeight), + sm: this.uiContext.px2vp(this.naviIndicatorHeight), md: 0, lg: 0, }).getValue(this.currentWidthBreakpoint), @@ -234,8 +236,8 @@ export struct TrafficView { .margin({ bottom: new BreakpointType({ sm: 0, - md: this.uiContext.px2vp(this.bottomRectHeight), - lg: this.uiContext.px2vp(this.bottomRectHeight), + md: this.uiContext.px2vp(this.naviIndicatorHeight), + lg: this.uiContext.px2vp(this.naviIndicatorHeight), }).getValue(this.currentWidthBreakpoint), }) } diff --git a/liveviewlockscreenlibrary/src/main/module.json5 b/liveviewlockscreenlibrary/src/main/module.json5 index 177ddf3161339cb9d2a0c00d837a9679eeece759..25a6c5b4e0b15d5ec39434fcf10533380582cdf0 100644 --- a/liveviewlockscreenlibrary/src/main/module.json5 +++ b/liveviewlockscreenlibrary/src/main/module.json5 @@ -6,6 +6,15 @@ "default", "tablet", "2in1" + ], + "requestPermissions": [ + { + "name": "ohos.permission.KEEP_BACKGROUND_RUNNING", + "reason": "$string:reason_background", + "usedScene": { + "when": "always" + } + }, ] } -} +} \ No newline at end of file diff --git a/liveviewlockscreenlibrary/src/main/resources/base/element/color.json b/liveviewlockscreenlibrary/src/main/resources/base/element/color.json index 4ed1297740ee47753074ba509f9b1f40bddfb7d4..b8a7c85783be8eac1e10291461d94e8f257a435f 100644 --- a/liveviewlockscreenlibrary/src/main/resources/base/element/color.json +++ b/liveviewlockscreenlibrary/src/main/resources/base/element/color.json @@ -1,9 +1,5 @@ { "color": [ - { - "name": "start_window_background", - "value": "#FFFFFF" - }, { "name": "Index_background_first", "value": "#000000" diff --git a/liveviewlockscreenlibrary/src/main/resources/base/element/string.json b/liveviewlockscreenlibrary/src/main/resources/base/element/string.json index aa9d46ca0d04a5a5cdc85f6bbeabf3f4378e1010..b989469ef00ce107637426f14899990a14184db3 100644 --- a/liveviewlockscreenlibrary/src/main/resources/base/element/string.json +++ b/liveviewlockscreenlibrary/src/main/resources/base/element/string.json @@ -1,40 +1,28 @@ { "string": [ - { - "name": "module_desc", - "value": "module description" - }, - { - "name": "EntryAbility_desc", - "value": "description" - }, - { - "name": "EntryAbility_label", - "value": "锁屏沉浸实况窗" - }, { "name": "reason_background", - "value": "后台更新实况窗数据" + "value": "Background operation to update the live view" }, { "name": "traffic_info_meter", - "value": "800米" + "value": "800 meters" }, { "name": "traffic_info_smmd_first", - "value": "800米" + "value": "Turn right" }, { "name": "traffic_info_smmd_second", - "value": "后右转" + "value": "after 800m" }, { "name": "traffic_info_first", - "value": "1 小时 24 分钟" + "value": "1 hour 24 minutes" }, { "name": "traffic_info_second", - "value": "72.6公里·09:32·" + "value": "72.6km·09:32·" }, { "name": "traffic_info_third", @@ -42,7 +30,7 @@ }, { "name": "live_view_title", - "value": "导航中" + "value": "In navigation" }, { "name": "live_view_background", @@ -54,47 +42,47 @@ }, { "name": "meter", - "value": "米" + "value": " meters" }, { "name": "live_view_primary_title_first", - "value": "" + "value": "Turn right after " }, { "name": "live_view_primary_title_second", - "value": "米后右转" + "value": " m" }, { "name": "road_name", - "value": "翠湖东路" + "value": "Cuihu East Road" }, { "name": "then", - "value": "然后" + "value": "Then" }, { "name": "Continue", - "value": "继续导航" + "value": "Continue" }, { "name": "Finish", - "value": "停止导航" + "value": "Finish" }, { "name": "tip", - "value": "提示" + "value": "Tip" }, { "name": "tip_text", - "value": " 本案例已完成Pura X内外屏适配和实况窗功能开发,因涉及权限申请,仅做UI展示,具体开发请参考示例代码。" + "value": "Permission is required to experience the lock screen immersive live window" }, { "name": "tip_get", - "value": "知道了" + "value": "I get" }, { "name": "toast", - "value": "仅做功能原型展示,具体业务需自行实现。" + "value": "Just a functional prototype—you’ll need to implement the business logic yourself." } ] } \ No newline at end of file diff --git a/liveviewlockscreenlibrary/src/main/resources/en_US/element/string.json b/liveviewlockscreenlibrary/src/main/resources/en_US/element/string.json index 4d1556f1a8880625f4c04d873422a73eedf127c9..b989469ef00ce107637426f14899990a14184db3 100644 --- a/liveviewlockscreenlibrary/src/main/resources/en_US/element/string.json +++ b/liveviewlockscreenlibrary/src/main/resources/en_US/element/string.json @@ -1,17 +1,5 @@ { "string": [ - { - "name": "module_desc", - "value": "module description" - }, - { - "name": "EntryAbility_desc", - "value": "description" - }, - { - "name": "EntryAbility_label", - "value": "LiveViewLockScreen" - }, { "name": "reason_background", "value": "Background operation to update the live view" diff --git a/liveviewlockscreenlibrary/src/main/resources/zh_CN/element/string.json b/liveviewlockscreenlibrary/src/main/resources/zh_CN/element/string.json index aa9d46ca0d04a5a5cdc85f6bbeabf3f4378e1010..73631fae03c4a8a7eeb7bb3d4cc49f63de8fae23 100644 --- a/liveviewlockscreenlibrary/src/main/resources/zh_CN/element/string.json +++ b/liveviewlockscreenlibrary/src/main/resources/zh_CN/element/string.json @@ -1,20 +1,8 @@ { "string": [ - { - "name": "module_desc", - "value": "module description" - }, - { - "name": "EntryAbility_desc", - "value": "description" - }, - { - "name": "EntryAbility_label", - "value": "锁屏沉浸实况窗" - }, { "name": "reason_background", - "value": "后台更新实况窗数据" + "value": "后台运行更新实况窗" }, { "name": "traffic_info_meter", diff --git a/liveviewlockscreensample/src/main/ets/entryability/EntryAbility.ets b/liveviewlockscreensample/src/main/ets/entryability/EntryAbility.ets index 438ac2c685920cca7bef408b35c6979e5d8d693e..defc1761682c41cf4788908b6cd9bfe3380ff9df 100644 --- a/liveviewlockscreensample/src/main/ets/entryability/EntryAbility.ets +++ b/liveviewlockscreensample/src/main/ets/entryability/EntryAbility.ets @@ -13,17 +13,16 @@ * limitations under the License. */ -import { ConfigurationConstant, UIAbility } from '@kit.AbilityKit'; +import { UIAbility } from '@kit.AbilityKit'; import { window } from '@kit.ArkUI'; import { hilog } from '@kit.PerformanceAnalysisKit'; -import { BundleNameUtil, WindowUtil } from 'liveviewlockscreenlibrary' +import { LiveViewLockScreenController } from 'liveviewlockscreenlibrary'; const TAG: string = '[EntryAbility]'; export default class EntryAbility extends UIAbility { onCreate(): void { - this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); - BundleNameUtil.getBundleName(); + LiveViewLockScreenController.saveBundleName(); hilog.info(0x0000, TAG, '%{public}s', 'Ability onCreate'); } @@ -40,8 +39,7 @@ export default class EntryAbility extends UIAbility { `Failed to load the content. Cause code: ${error.code}, message: ${error.message}`); return; } - WindowUtil.requestFullScreen(windowStage); - WindowUtil.registerBreakPoint(windowStage); + LiveViewLockScreenController.initWindowConfig(windowStage); hilog.info(0x0000, TAG, '%{public}s', 'Succeeded in loading the content.'); }); } diff --git a/liveviewlockscreensample/src/main/ets/liveview/LiveViewExtAbility.ets b/liveviewlockscreensample/src/main/ets/liveview/LiveViewExtAbility.ets index 34340aff8f6b3a3c6ea9a707db009b4f5573e5b9..6b94c0294da5bba303d66de96a31f677a0ded04c 100644 --- a/liveviewlockscreensample/src/main/ets/liveview/LiveViewExtAbility.ets +++ b/liveviewlockscreensample/src/main/ets/liveview/LiveViewExtAbility.ets @@ -14,18 +14,15 @@ */ import { AbilityConstant, UIExtensionContentSession, Want } from '@kit.AbilityKit'; -import { commonEventManager } from '@kit.BasicServicesKit'; +import { BusinessError } from '@kit.BasicServicesKit'; import { LiveViewLockScreenExtensionAbility } from '@kit.LiveViewKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; -import { LiveViewExtAbilityUtil } from 'liveviewlockscreenlibrary'; +import { LiveViewLockScreenController } from 'liveviewlockscreenlibrary'; const TAG: string = '[LiveViewExtAbility]'; // Main class for lock screen live view extension, handles live view presentation on lock screen. export default class LiveViewExtAbility extends LiveViewLockScreenExtensionAbility { - // Common event subscriber for handling system events. - private subscriber: commonEventManager.CommonEventSubscriber | null = null; - onCreate(_launchParam: AbilityConstant.LaunchParam): void { hilog.info(0x0000, TAG, '%{public}s', 'LiveViewLockScreenExtAbility onCreate begin.'); } @@ -45,12 +42,18 @@ export default class LiveViewExtAbility extends LiveViewLockScreenExtensionAbili // Core logic when creating UI session. onSessionCreate(_want: Want, session: UIExtensionContentSession): void { hilog.info(0x0000, TAG, '%{public}s', 'LiveViewLockScreenExtAbility onSessionCreate begin.'); - LiveViewExtAbilityUtil.getInstance().setSession(session); + LiveViewLockScreenController.initLiveView(session); + try { + session.loadContent('pages/LockScreen'); + } catch (error) { + hilog.error(0x0000, TAG, '%{public}s', + `Session load content fail. code is ${(error as BusinessError).code} message is ${(error as BusinessError).message}`); + } } // Cleanup operations when destroying UI session onSessionDestroy(): void { - LiveViewExtAbilityUtil.getInstance().destroySubscribe(); + LiveViewLockScreenController.destroyLLiveView(); hilog.info(0x0000, TAG, '%{public}s', 'LiveViewLockScreenExtAbility onSessionDestroy begin.'); } diff --git a/liveviewlockscreensample/src/main/ets/pages/Index.ets b/liveviewlockscreensample/src/main/ets/pages/Index.ets index 99c28de999fd18a7a852b1d69395dd3b7e0dc6b7..8292b279b43ea08225d0d262e8fa461901db7c11 100644 --- a/liveviewlockscreensample/src/main/ets/pages/Index.ets +++ b/liveviewlockscreensample/src/main/ets/pages/Index.ets @@ -13,14 +13,14 @@ * limitations under the License. */ -import { LiveViewLockScreenPage } from 'liveviewlockscreenlibrary' +import { IndexComponent } from 'liveviewlockscreenlibrary' @Entry @Component struct Index { build() { Stack() { - LiveViewLockScreenPage() + IndexComponent() } } -} +} \ No newline at end of file diff --git a/liveviewlockscreensample/src/main/ets/pages/LockScreen.ets b/liveviewlockscreensample/src/main/ets/pages/LockScreen.ets index 9a31811ac4c78a9e50e2ecdebf54bec92bc88eba..03314719a745dae760f72138030b9c8dbbceec5c 100644 --- a/liveviewlockscreensample/src/main/ets/pages/LockScreen.ets +++ b/liveviewlockscreensample/src/main/ets/pages/LockScreen.ets @@ -13,13 +13,13 @@ * limitations under the License. */ -import { LockScreenPage } from 'liveviewlockscreenlibrary' +import { LockScreenComponent } from 'liveviewlockscreenlibrary' @Entry struct LockScreen { build() { Stack() { - LockScreenPage() + LockScreenComponent() } } -} +} \ No newline at end of file diff --git a/liveviewlockscreensample/src/main/module.json5 b/liveviewlockscreensample/src/main/module.json5 index fca9f251ef7a501e5c91610ee798793572429b50..0f83c861417ac8b06671ba5b4f20dea35bbe6916 100644 --- a/liveviewlockscreensample/src/main/module.json5 +++ b/liveviewlockscreensample/src/main/module.json5 @@ -9,8 +9,9 @@ "tablet", "2in1" ], - "deliveryWithInstall": true, + "deliveryWithInstall": false, "installationFree": false, + "pages": "$profile:main_pages", "abilities": [ { @@ -21,7 +22,7 @@ "label": "$string:EntryAbility_label", "startWindowIcon": "$media:startIcon", "startWindowBackground": "$color:start_window_background", - "exported": false, + "exported": true, "backgroundModes": [ 'location' ], @@ -47,17 +48,5 @@ "exported": false } ], - "requestPermissions": [ - { - "name": "ohos.permission.KEEP_BACKGROUND_RUNNING", - "reason": "$string:reason_background", - "usedScene": { - "abilities": [ - "EntryAbility" - ], - "when": "always" - } - }, - ], } } \ No newline at end of file diff --git a/liveviewlockscreensample/src/main/resources/base/element/color.json b/liveviewlockscreensample/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/liveviewlockscreensample/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/liveviewlockscreensample/src/main/resources/base/element/string.json b/liveviewlockscreensample/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..a218cd84a3cfdd16dc4edfe05ce90865ecd033c1 --- /dev/null +++ b/liveviewlockscreensample/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "LiveViewLockScreen" + } + ] +} \ No newline at end of file diff --git a/liveviewlockscreensample/src/main/resources/base/profile/main_pages.json b/liveviewlockscreensample/src/main/resources/base/profile/main_pages.json index 969d4a34698d8ead8cc85256c09ef4100b4a4f2a..576646da35cde5a8e7bed01b4717b3882b0de130 100644 --- a/liveviewlockscreensample/src/main/resources/base/profile/main_pages.json +++ b/liveviewlockscreensample/src/main/resources/base/profile/main_pages.json @@ -3,4 +3,4 @@ "pages/Index", "pages/LockScreen" ] -} +} \ No newline at end of file diff --git a/liveviewlockscreenlibrary/src/main/resources/dark/element/color.json b/liveviewlockscreensample/src/main/resources/dark/element/color.json similarity index 100% rename from liveviewlockscreenlibrary/src/main/resources/dark/element/color.json rename to liveviewlockscreensample/src/main/resources/dark/element/color.json