From 8ed0f906c87cb796ac1c06d7de99fca98688ede5 Mon Sep 17 00:00:00 2001 From: EasyGuohf <163991322+EasyGuohf@users.noreply.github.com> Date: Thu, 18 Sep 2025 16:25:13 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=97=A5=E5=BF=97Logger?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 3 +- smartwatchmaplibrary/Index.ets | 2 + .../src/main/ets/common/utils/Logger.ets | 45 +++++++++++++++++++ .../main/ets/common/utils/PermissionUtil.ets | 10 ++--- .../ets/component/SmartWatchMapComponent.ets | 2 +- .../main/resources/base/element/float.json | 0 .../main/resources/base/element/string.json | 12 ----- .../main/resources/en_US/element/string.json | 12 ----- .../main/resources/zh_CN/element/string.json | 12 ----- .../main/ets/entryability/EntryAbility.ets | 20 +++++---- .../entrybackupability/EntryBackupAbility.ets | 8 ++-- .../src/main/ets/pages/Index.ets | 1 - .../main/resources/zh_CN/element/string.json | 2 +- 13 files changed, 72 insertions(+), 57 deletions(-) create mode 100644 smartwatchmaplibrary/src/main/ets/common/utils/Logger.ets rename {smartwatchmapsample => smartwatchmaplibrary}/src/main/resources/base/element/float.json (100%) diff --git a/README.md b/README.md index 851547a..7083e6a 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,8 @@ ``` ├──entry/src/main/ets // 代码区 │ ├──common -│ │ └──util +│ │ └──util +│ │ ├──Logger.ets │ │ └──PermissionUtil.ets // 权限工具类 │ ├──entryability │ │ └──EntryAbility.ets // 程序入口类 diff --git a/smartwatchmaplibrary/Index.ets b/smartwatchmaplibrary/Index.ets index 4505e53..d66767f 100644 --- a/smartwatchmaplibrary/Index.ets +++ b/smartwatchmaplibrary/Index.ets @@ -1 +1,3 @@ export { SmartWatchMapComponent } from './src/main/ets/component/SmartWatchMapComponent'; + +export { default as Logger } from './src/main/ets/common/utils/Logger'; \ No newline at end of file diff --git a/smartwatchmaplibrary/src/main/ets/common/utils/Logger.ets b/smartwatchmaplibrary/src/main/ets/common/utils/Logger.ets new file mode 100644 index 0000000..db81d56 --- /dev/null +++ b/smartwatchmaplibrary/src/main/ets/common/utils/Logger.ets @@ -0,0 +1,45 @@ +/* + * 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. + */ + +import { hilog } from '@kit.PerformanceAnalysisKit'; + +class Logger { + private domain: number; + private prefix: string; + private format: string = '%{public}s, %{public}s'; + + public constructor(prefix: string) { + this.prefix = prefix; + this.domain = 0x0000; + } + + public debug(...args: Object[]): void { + hilog.debug(this.domain, this.prefix, this.format, args); + } + + public info(...args: Object[]): void { + hilog.info(this.domain, this.prefix, this.format, args); + } + + public warn(...args: Object[]): void { + hilog.warn(this.domain, this.prefix, this.format, args); + } + + public error(...args: Object[]): void { + hilog.error(this.domain, this.prefix, this.format, args); + } +} + +export default new Logger('[SmartWatchMap]'); \ No newline at end of file diff --git a/smartwatchmaplibrary/src/main/ets/common/utils/PermissionUtil.ets b/smartwatchmaplibrary/src/main/ets/common/utils/PermissionUtil.ets index ea4e77a..cd4d5e6 100644 --- a/smartwatchmaplibrary/src/main/ets/common/utils/PermissionUtil.ets +++ b/smartwatchmaplibrary/src/main/ets/common/utils/PermissionUtil.ets @@ -16,7 +16,7 @@ import { abilityAccessCtrl, bundleManager, common, Permissions } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; import { map } from '@kit.MapKit'; -import { hilog } from '@kit.PerformanceAnalysisKit'; +import Logger from './Logger'; const TAG: string = 'PermissionUtil'; @@ -45,7 +45,7 @@ export class PermissionUtil { ['ohos.permission.LOCATION', 'ohos.permission.APPROXIMATELY_LOCATION']).then(() => { this.mapController?.setMyLocationEnabled(true); }).catch((err: BusinessError) => { - hilog.error(0x0000, TAG, + Logger.error(TAG, `Failed to request permissions from user. Code is ${err.code}, message is ${err.message}`); }); } @@ -57,14 +57,14 @@ export class PermissionUtil { try { let bundleInfo: bundleManager.BundleInfo = await bundleManager.getBundleInfoForSelf(bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION); - hilog.info(0x0000, TAG, 'Succeeded in getting Bundle.'); + Logger.info(TAG, 'Succeeded in getting Bundle.'); let appInfo: bundleManager.ApplicationInfo = bundleInfo.appInfo; tokenId = appInfo.accessTokenId; grantStatus = await atManager.checkAccessToken(tokenId, permission); - hilog.info(0x0000, TAG, 'Succeeded in checking access token.'); + Logger.info(TAG, 'Succeeded in checking access token.'); } catch (error) { const message = (error as BusinessError).message; - hilog.error(0x0000, TAG, `Failed in checking access token, message is ${message}`); + Logger.error(TAG, `Failed in checking access token, message is ${message}`); } return grantStatus; diff --git a/smartwatchmaplibrary/src/main/ets/component/SmartWatchMapComponent.ets b/smartwatchmaplibrary/src/main/ets/component/SmartWatchMapComponent.ets index f056e0b..c73b662 100644 --- a/smartwatchmaplibrary/src/main/ets/component/SmartWatchMapComponent.ets +++ b/smartwatchmaplibrary/src/main/ets/component/SmartWatchMapComponent.ets @@ -15,7 +15,7 @@ import { common } from '@kit.AbilityKit'; import { AsyncCallback } from '@kit.BasicServicesKit'; -import { MapComponent, mapCommon, map } from '@kit.MapKit'; +import { map, mapCommon, MapComponent } from '@kit.MapKit'; import { PermissionUtil } from '../common/utils/PermissionUtil'; @Component diff --git a/smartwatchmapsample/src/main/resources/base/element/float.json b/smartwatchmaplibrary/src/main/resources/base/element/float.json similarity index 100% rename from smartwatchmapsample/src/main/resources/base/element/float.json rename to smartwatchmaplibrary/src/main/resources/base/element/float.json diff --git a/smartwatchmaplibrary/src/main/resources/base/element/string.json b/smartwatchmaplibrary/src/main/resources/base/element/string.json index de07eb2..1a2baac 100644 --- a/smartwatchmaplibrary/src/main/resources/base/element/string.json +++ b/smartwatchmaplibrary/src/main/resources/base/element/string.json @@ -1,17 +1,5 @@ { "string": [ - { - "name": "module_desc", - "value": "module description" - }, - { - "name": "EntryAbility_desc", - "value": "description" - }, - { - "name": "EntryAbility_label", - "value": "label" - }, { "name": "request_permission_reason", "value": "Using location permissions in location scenarios." diff --git a/smartwatchmaplibrary/src/main/resources/en_US/element/string.json b/smartwatchmaplibrary/src/main/resources/en_US/element/string.json index 32ca6d5..1a2baac 100644 --- a/smartwatchmaplibrary/src/main/resources/en_US/element/string.json +++ b/smartwatchmaplibrary/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": "SmartWatchStopCar" - }, { "name": "request_permission_reason", "value": "Using location permissions in location scenarios." diff --git a/smartwatchmaplibrary/src/main/resources/zh_CN/element/string.json b/smartwatchmaplibrary/src/main/resources/zh_CN/element/string.json index 925b0a4..10131af 100644 --- a/smartwatchmaplibrary/src/main/resources/zh_CN/element/string.json +++ b/smartwatchmaplibrary/src/main/resources/zh_CN/element/string.json @@ -1,17 +1,5 @@ { "string": [ - { - "name": "module_desc", - "value": "模块描述" - }, - { - "name": "EntryAbility_desc", - "value": "description" - }, - { - "name": "EntryAbility_label", - "value": "智能穿戴-地图应用" - }, { "name": "request_permission_reason", "value": "在定位场景中获取位置权限" diff --git a/smartwatchmapsample/src/main/ets/entryability/EntryAbility.ets b/smartwatchmapsample/src/main/ets/entryability/EntryAbility.ets index 964b655..477d360 100644 --- a/smartwatchmapsample/src/main/ets/entryability/EntryAbility.ets +++ b/smartwatchmapsample/src/main/ets/entryability/EntryAbility.ets @@ -14,44 +14,46 @@ */ import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; -import { hilog } from '@kit.PerformanceAnalysisKit'; import { window } from '@kit.ArkUI'; +import { Logger } from '@ohos_samples/smartwatchmaplibrary'; + +const TAG = '[EntryAbility]'; export default class EntryAbility extends UIAbility { onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { - hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + Logger.info(TAG, 'Ability onCreate'); } onDestroy(): void { - hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + Logger.info(TAG, 'Ability onDestroy'); } onWindowStageCreate(windowStage: window.WindowStage): void { // Main window is created, set main page for this ability - hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + Logger.info(TAG, 'Ability onWindowStageCreate'); windowStage.loadContent('pages/Index', (err) => { if (err.code) { - hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', + Logger.error(TAG, 'Failed to load the content. Cause: %{public}s', JSON.stringify(err.message) ?? ''); return; } - hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + Logger.info(TAG, 'Succeeded in loading the content.'); }); } onWindowStageDestroy(): void { // Main window is destroyed, release UI related resources - hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + Logger.info(TAG, 'Ability onWindowStageDestroy'); } onForeground(): void { // Ability has brought to foreground - hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + Logger.info(TAG, 'Ability onForeground'); } onBackground(): void { // Ability has back to background - hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + Logger.info(TAG, 'Ability onBackground'); } } \ No newline at end of file diff --git a/smartwatchmapsample/src/main/ets/entrybackupability/EntryBackupAbility.ets b/smartwatchmapsample/src/main/ets/entrybackupability/EntryBackupAbility.ets index 406ff12..de8067a 100644 --- a/smartwatchmapsample/src/main/ets/entrybackupability/EntryBackupAbility.ets +++ b/smartwatchmapsample/src/main/ets/entrybackupability/EntryBackupAbility.ets @@ -13,15 +13,17 @@ * limitations under the License. */ -import { hilog } from '@kit.PerformanceAnalysisKit'; import { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit'; +import { Logger } from '@ohos_samples/smartwatchmaplibrary'; + +const TAG = '[EntryBackupAbility]'; export default class EntryBackupAbility extends BackupExtensionAbility { async onBackup() { - hilog.info(0x0000, 'testTag', 'onBackup ok'); + Logger.info(TAG, 'onBackup ok'); } async onRestore(bundleVersion: BundleVersion) { - hilog.info(0x0000, 'testTag', 'onRestore ok'); + Logger.info(TAG, 'onRestore ok'); } } \ No newline at end of file diff --git a/smartwatchmapsample/src/main/ets/pages/Index.ets b/smartwatchmapsample/src/main/ets/pages/Index.ets index a18b29f..916feaf 100644 --- a/smartwatchmapsample/src/main/ets/pages/Index.ets +++ b/smartwatchmapsample/src/main/ets/pages/Index.ets @@ -18,7 +18,6 @@ import { SmartWatchMapComponent } from '@ohos_samples/smartwatchmaplibrary'; @Entry @Component struct Index { - build() { Column() { SmartWatchMapComponent() diff --git a/smartwatchmapsample/src/main/resources/zh_CN/element/string.json b/smartwatchmapsample/src/main/resources/zh_CN/element/string.json index 6699e42..620bd3a 100644 --- a/smartwatchmapsample/src/main/resources/zh_CN/element/string.json +++ b/smartwatchmapsample/src/main/resources/zh_CN/element/string.json @@ -6,7 +6,7 @@ }, { "name": "EntryAbility_desc", - "value": "description" + "value": "描述" }, { "name": "EntryAbility_label", -- Gitee From 47f00829d97df11064dd4a3370c8e9d181cfb1eb Mon Sep 17 00:00:00 2001 From: EasyGuohf <163991322+EasyGuohf@users.noreply.github.com> Date: Thu, 18 Sep 2025 16:40:06 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=97=A5=E5=BF=97Logger-?= =?UTF-8?q?readMe=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 7083e6a..6490f80 100644 --- a/README.md +++ b/README.md @@ -15,18 +15,24 @@ ### 工程目录 ``` -├──entry/src/main/ets // 代码区 +smartwatchmaplibrary +├──src/main/ets // 代码区 │ ├──common -│ │ └──util +│ │ └──utils │ │ ├──Logger.ets -│ │ └──PermissionUtil.ets // 权限工具类 -│ ├──entryability -│ │ └──EntryAbility.ets // 程序入口类 -│ ├──entrybackupability -│ │ └──EntryBackupAbility.ets -│ ├──pages -│ │ └──Index.ets // 首页 -└──entry/src/main/resources // 应用静态资源目录 +│ │ └──PermissionUtil.ets // 权限工具类 +│ ├──component +│ │ └──SmartWatchMapComponent.ets // 首页 +└──src/main/resources // 应用静态资源目录 +smartwatchmapsample +├──src/main/ets +│ ├──entryability +│ │ └──EntryAbility.ets +│ ├──entrybackupability +│ │ └──EntryBackupAbility.ets +│ └──pages +│ └──Index.ets // 入口页面 +└──src/main/resources // 应用静态资源目录 ``` ### 约束与限制 -- Gitee