From 2dc32d1402d6496d9c038cdb9db52bb28b145b7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9F=B3=E4=B8=9C=E6=B5=B7?= Date: Thu, 18 Sep 2025 18:38:10 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20=E5=87=86=E5=87=BA=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/ets/entryability/EntryAbility.ets | 67 ++++++++----------- entry/src/main/ets/pages/MailHomePage.ets | 10 +-- hvigor/hvigor-config.json5 | 2 +- oh-package.json5 | 2 +- 4 files changed, 36 insertions(+), 45 deletions(-) diff --git a/entry/src/main/ets/entryability/EntryAbility.ets b/entry/src/main/ets/entryability/EntryAbility.ets index aa7062a..30fd30b 100644 --- a/entry/src/main/ets/entryability/EntryAbility.ets +++ b/entry/src/main/ets/entryability/EntryAbility.ets @@ -13,7 +13,7 @@ * limitations under the License. */ -import { abilityAccessCtrl, AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; import { commonType, distributedDataObject } from '@kit.ArkData'; import { window } from '@kit.ArkUI'; @@ -31,20 +31,18 @@ export default class EntryAbility extends UIAbility { private distributedObject: distributedDataObject.DataObject | undefined = undefined; onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { - this.permissions(); try { this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); - hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onCreate'); + hilog.info(DOMAIN, 'EntryAbility', '%{public}s', 'Ability onCreate'); this.restoreDistributedObject(want, launchParam); } catch (error) { - hilog.error(0x0000, 'EntryAbility', `have error .Code:${error.code},message: ${error.message}`); + hilog.error(0x0000, 'EntryAbility', `getApplicationContext error. Code:${error.code},message: ${error.message}`); return error.code; } } onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam): void { - this.permissions(); - hilog.info(0x0000, 'hilog', `Ability onNewWant`); + hilog.info(0x0000, 'EntryAbility', `Ability onNewWant`); this.restoreDistributedObject(want, launchParam); } @@ -56,7 +54,7 @@ export default class EntryAbility extends UIAbility { */ async restoreDistributedObject(want: Want, launchParam: AbilityConstant.LaunchParam): Promise { this.context.setMissionContinueState(AbilityConstant.ContinueState.INACTIVE, (result) => { - hilog.info(0x0000, 'hilog', `setMissionContinueState: ${JSON.stringify(result)}`); + hilog.info(0x0000, 'EntryAbility', `setMissionContinueState: ${JSON.stringify(result)}`); }); if (launchParam.launchReason !== AbilityConstant.LaunchReason.CONTINUATION) { return; @@ -72,7 +70,7 @@ export default class EntryAbility extends UIAbility { try { this.distributedObject.on('status', (sessionId: string, networkId: string, status: 'online' | 'offline' | 'restored') => { - hilog.info(0x0000, 'hilog', 'EntryAbility', 'status changed ' + sessionId + ' ' + status + ' ' + networkId); + hilog.info(0x0000, 'EntryAbility', 'status changed ' + sessionId + ' ' + status + ' ' + networkId); if (status === 'restored') { if (!this.distributedObject) { return; @@ -83,7 +81,7 @@ export default class EntryAbility extends UIAbility { AppStorage.setOrCreate('emailContent', this.distributedObject['emailContent']); AppStorage.setOrCreate('attachments', this.distributedObject['attachments']); let attachments = this.distributedObject['attachments'] as commonType.Assets; - hilog.info(0x0000, 'hilog', + hilog.info(0x0000, 'EntryAbility', 'this.distributedObject[attachments] ' + JSON.stringify(this.distributedObject['attachments'])); for (const attachment of attachments) { this.fileCopy(attachment); @@ -91,21 +89,22 @@ export default class EntryAbility extends UIAbility { } }); let sessionId: string = want.parameters?.distributedSessionId as string; - hilog.info(0x0000, 'hilog', 'sessionId' + sessionId); - this.distributedObject.setSessionId(sessionId); + hilog.info(0x0000, 'EntryAbility', 'sessionId' + sessionId); + this.distributedObject.setSessionId(sessionId).catch((err: BusinessError) => { + hilog.error(0x0000, 'EntryAbility', `setSessionId error .Code:${err.code},message: ${err.message}`); + }); this.context.restoreWindowStage(new LocalStorage()); } catch (error) { - hilog.error(0x0000, 'EntryAbility', `have error .Code:${error.code},message: ${error.message}`); + hilog.error(0x0000, 'EntryAbility', `have error. Code:${error.code}, message: ${error.message}`); } } onWindowStageRestore(windowStage: window.WindowStage): void { - hilog.info(0x0000, 'hilog', 'EntryAbility', 'Ability onWindowStageRestore'); + hilog.info(0x0000, 'EntryAbility', 'Ability onWindowStageRestore'); windowStage.loadContent('pages/MailHomePage', (err, data) => { if (err.code) { - hilog.error(0x0000, 'EntryAbility', 'Failed to load the content, ', - `Catch err: ${err.code},msg:${err.message}`); + hilog.error(0x0000, 'EntryAbility', `ailed to load the content, Code:${err.code},message: ${err.message}`); return; } hilog.info(0x0000, 'EntryAbility', 'Succeeded in loading the content, ', `Data: ${data}`); @@ -133,14 +132,16 @@ export default class EntryAbility extends UIAbility { AppStorage.get('subject'), AppStorage.get('emailContent'), assets); let source = mailInfoManager.flatAssets(); this.distributedObject = distributedDataObject.create(this.context, source); - this.distributedObject.setSessionId(sessionId); + this.distributedObject.setSessionId(sessionId).catch((err: BusinessError) => { + hilog.error(0x0000, 'EntryAbility', `setSessionId error. Code:${err.code},message: ${err.message}`); + }); await this.distributedObject.save(wantParam.targetDevice as string).then(() => { - hilog.info(0x0000, 'hilog', 'onContinue distributedObject save success'); + hilog.info(0x0000, 'EntryAbility', 'onContinue distributedObject save success'); }).catch((err: BusinessError) => { - hilog.error(0x0000, 'hilog', `Failed to save. Code:${err.code},message:${err.message}`); + hilog.error(0x0000, 'EntryAbility', `Failed to save. Code:${err.code}, message:${err.message}`); }); - } catch (error) { - hilog.error(0x0000, 'hilog', 'distributedDataObject failed', `code ${(error as BusinessError).code}`); + } catch (err) { + hilog.error(0x0000, 'EntryAbility', `distributedDataObject failed, Code:${err.code}, message:${err.message}`); } return AbilityConstant.OnContinueResult.AGREE; } @@ -174,7 +175,7 @@ export default class EntryAbility extends UIAbility { hilog.info(0x0000, 'EntryAbility', attachment.name + 'synchronized successfully.'); } } catch (error) { - hilog.error(0x0000, 'EntryAbility', `have error .Code:${error.code},message: ${error.message}`); + hilog.error(0x0000, 'EntryAbility', `have error. Code:${error.code}, message: ${error.message}`); } } @@ -200,7 +201,7 @@ export default class EntryAbility extends UIAbility { }; return attachment; } catch (error) { - hilog.error(0x0000, 'statSync', `statSync error: ${error.code} msg:${error.message}`); + hilog.error(0x0000, 'EntryAbility', `statSync error, Code: ${error.code} message:${error.message}`); return null; } } @@ -211,11 +212,15 @@ export default class EntryAbility extends UIAbility { windowStage.loadContent('pages/MailHomePage', (err, data) => { if (err.code) { - hilog.error(0x0000, 'EntryAbility', 'Failed to load the content, ', `Catch err: ${err}`); + hilog.error(0x0000, 'EntryAbility', `Failed to load the content, Code: ${err.code} message:${err.message}`); return; } hilog.info(0x0000, 'EntryAbility', 'Succeeded in loading the content, ', `Data: ${data}`); - uiContext = windowStage.getMainWindowSync().getUIContext(); + try { + uiContext = windowStage.getMainWindowSync().getUIContext(); + } catch (error) { + hilog.error(0x0000, 'EntryAbility', `getMainWindowSync error .Code:${error.code}, message: ${error.message}`); + } }); } @@ -234,18 +239,4 @@ export default class EntryAbility extends UIAbility { onDestroy() { hilog.info(DOMAIN, 'EntryAbility', 'Ability onDestroy'); } - - /** - * Apply for the permission to exchange data between different devices. - */ - permissions(): void { - let atManager = abilityAccessCtrl.createAtManager(); - try { - atManager.requestPermissionsFromUser(this.context, ['ohos.permission.DISTRIBUTED_DATASYNC']).then((data) => { - hilog.info(0x0000, 'hilog', `Data permissions:${data.permissions}`); - }); - } catch (err) { - hilog.info(0x0000, 'hilog', `Catch err: ${err}`); - } - } } diff --git a/entry/src/main/ets/pages/MailHomePage.ets b/entry/src/main/ets/pages/MailHomePage.ets index 3ed5bee..11009e6 100644 --- a/entry/src/main/ets/pages/MailHomePage.ets +++ b/entry/src/main/ets/pages/MailHomePage.ets @@ -37,13 +37,13 @@ struct MailHomePage { onPageShow(): void { this.context.setMissionContinueState(AbilityConstant.ContinueState.ACTIVE, (result) => { - hilog.info(0x0000, 'hilog', 'setMissionContinueState ACTIVE result: ', JSON.stringify(result)); + hilog.info(0x0000, 'MailHomePage', 'setMissionContinueState ACTIVE result: ', JSON.stringify(result)); }); } onBackPress(): void { this.context.setMissionContinueState(AbilityConstant.ContinueState.INACTIVE, (result) => { - hilog.info(0x0000, 'hilog', 'setMissionContinueState INACTIVE result: ', JSON.stringify(result)); + hilog.info(0x0000, 'MailHomePage', 'setMissionContinueState INACTIVE result: ', JSON.stringify(result)); }); } @@ -290,13 +290,13 @@ struct MailHomePage { fileIo.closeSync(destinationDistribute); this.appendix.push({ iconIndex: fileType, fileName: fileName }); } - hilog.info(0x0000, 'hilog', + hilog.info(0x0000, 'MailHomePage', `DocumentViewPicker.select successfully, DocumentSelectResult uri: ${JSON.stringify(DocumentSelectResult)}`); }).catch((error: BusinessError) => { - hilog.error(0x0000, 'MailHomePage', `have error .Code:${error.code},message: ${error.message}`); + hilog.error(0x0000, 'MailHomePage', `have error. Code:${error.code},message: ${error.message}`); }); } catch (error) { - hilog.error(0x0000, 'MailHomePage', `have error .Code:${error.code},message: ${error.message}`); + hilog.error(0x0000, 'MailHomePage', `have error. Code:${error.code},message: ${error.message}`); } } } \ No newline at end of file diff --git a/hvigor/hvigor-config.json5 b/hvigor/hvigor-config.json5 index f70ecd4..15d67ae 100644 --- a/hvigor/hvigor-config.json5 +++ b/hvigor/hvigor-config.json5 @@ -1,5 +1,5 @@ { - "modelVersion": "5.0.0", + "modelVersion": "5.1.1", "dependencies": { } } \ No newline at end of file diff --git a/oh-package.json5 b/oh-package.json5 index 0cd9ae6..d478da1 100644 --- a/oh-package.json5 +++ b/oh-package.json5 @@ -1,5 +1,5 @@ { - "modelVersion": "5.0.0", + "modelVersion": "5.1.1", "license": "", "devDependencies": { }, -- Gitee From 3e849520b86f6f6b43f68fac47f898522008db3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9F=B3=E4=B8=9C=E6=B5=B7?= Date: Fri, 19 Sep 2025 09:47:50 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20=E5=87=86=E5=87=BA=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.en.md | 8 +- README.md | 8 +- entry/src/main/ets/common/CommonConstants.ets | 334 ------------------ .../main/ets/entryability/EntryAbility.ets | 100 +----- entry/src/main/ets/pages/MailHomePage.ets | 135 +------ entry/src/main/ets/utils/MailInfoManager.ets | 25 +- .../src/main/ets/viewmodel/MailViewModel.ets | 66 ---- 7 files changed, 14 insertions(+), 662 deletions(-) delete mode 100644 entry/src/main/ets/common/CommonConstants.ets delete mode 100644 entry/src/main/ets/viewmodel/MailViewModel.ets diff --git a/README.en.md b/README.en.md index 3646a2d..8853aa5 100644 --- a/README.en.md +++ b/README.en.md @@ -20,16 +20,12 @@ Based on application connection and distributed data objects, this Codelab imple ``` ├──entry/src/main/ets -│ ├──common -│ │ └──CommonConstants.ets // Constant set │ ├──entryability │ │ └──EntryAbility.ets // Entry file │ ├──pages │ │ └──MailHomePage.ets // Mail home page -│ ├──utils -│ │ └──MailInfoManager.ets // Mail information management class -│ └──viewmodel -│ └──MailViewModel.ets // Mail model +│ └──utils +│ └──MailInfoManager.ets // Mail information management class └──entry/src/main/resources // Resource file ``` diff --git a/README.md b/README.md index 5d0198b..ca1b4de 100644 --- a/README.md +++ b/README.md @@ -20,16 +20,12 @@ ``` ├──entry/src/main/ets -│ ├──common -│ │ └──CommonConstants.ets // 常量集合 │ ├──entryability │ │ └──EntryAbility.ets // 入口文件 │ ├──pages │ │ └──MailHomePage.ets // 邮件首页 -│ ├──utils -│ │ └──MailInfoManager.ets // 邮件信息管理类 -│ └──viewmodel -│ └──MailViewModel.ets // 邮件model +│ └──utils +│ └──MailInfoManager.ets // 邮件信息管理类 └──entry/src/main/resources // 资源文件 ``` diff --git a/entry/src/main/ets/common/CommonConstants.ets b/entry/src/main/ets/common/CommonConstants.ets deleted file mode 100644 index 9c76036..0000000 --- a/entry/src/main/ets/common/CommonConstants.ets +++ /dev/null @@ -1,334 +0,0 @@ -/* - * 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 { relationalStore } from '@kit.ArkData'; - -/** - * Common constants for all features. - */ -export default class CommonConstants { - /** - * Search font weight. - */ - static readonly SEARCH_FONT_WEIGHT: number = 10; - /** - * The font family of search component. - */ - static readonly SEARCH_FONT_FAMILY: string = 'serif'; - /** - * The layout weight of the list title. - */ - static readonly TITLE_LAYOUT_WEIGHT: number = 1; - /** - * The max lines of the list title. - */ - static readonly TITLE_MAX_LINES: number = 1; - /** - * Set component layout weight. - */ - static readonly WEIGHT: number = 1; - /** - * The contacts detail page url. - */ - static readonly PAGE_DETAIL_URL: string = 'pages/ContactDetailPage'; - /** - * The contacts editing page url. - */ - static readonly ADD_EDIT_URL: string = 'pages/ContactAddAndEditPage'; - /** - * The contacts list page url. - */ - static readonly LIST_PAGE_URL: string = 'pages/ContactHomePage'; - /** - * The contacts delete page url. - */ - static readonly DELETE_PAGE_URL: string = 'pages/ContactDeletePage'; - /** - * The key to transfer parameters when replace pages. - */ - static readonly KEY_PARAM_DATA: string = 'data'; - /** - * The maximum component width. - */ - static readonly PERCENTAGE_MAX: string = '100%'; - /** - * Eighty percent assembly width. - */ - static readonly PERCENTAGE_HEIGHT_MAX: string = '80%'; - /** - * Delete page title width. - */ - static readonly PAGE_TITLE_WIDTH: string = '90%'; - /** - * Edit the width of the input box on the page. - */ - static readonly EDIT_INPUT_WIDTH: string = '80%'; - /** - * The contacts details page components width. - */ - static readonly DETAIL_ITEM_WIDTH: string = '70%'; - /** - * Height of the address book list. - */ - static readonly CONTACTS_LIST_HEIGHT: string = '90%'; - /** - * The width of the bottom component. - */ - static readonly BOTTOM_COMPONENT_WIDTH: string = '50%'; - /** - * The contacts details page top height. - */ - static readonly DETAIL_TOP_HEIGHT: string = '30%'; - /** - * The percentage of list component. - */ - static readonly LIST_WIDTH_PERCENT: string = '93.3%'; - /** - * The contacts list divider left margin. - */ - static readonly CONTACTS_LIST_DIVIDER: string = '16%'; - /** - * The contacts list divider left margin. - */ - static readonly CONTACTS_EMPTY_TOP: string = '17.8%'; - /** - * Contact database key. - */ - static readonly CONTACTS_DATABASE_KEY: string = 'contactsKey'; - /** - * The message prompt component duration. - */ - static readonly PROMPT_DURATION: number = 2000; - /** - * Address book database unique identifier. - */ - static readonly DB_STORE_ID: string = 'DatabaseStoreId'; - /** - * Ability name. - */ - static readonly ABILITY_NAME: string = 'EntryAbility'; - /** - * Detail page id. - */ - static readonly DETAIL_PAGE_ID: string = 'detailId'; - /** - * Delete page id. - */ - static readonly DELETE_PAGE_ID: string = 'deleteId'; - /** - * Phone components id. - */ - static readonly PHONE_COMPONENTS_ID: number = 1; - /** - * Email components id. - */ - static readonly Email_COMPONENTS_ID: number = 2; - /** - * Note components id. - */ - static readonly Note_COMPONENTS_ID: number = 3; - /** - * Device list group. - */ - static readonly DEVICE_GROUP: string = 'deviceGroup'; - /** - * Even column. - */ - static readonly EVEN_COLUMN: number = 2; - /** - * Indicates whether to edit the page. - */ - static readonly IS_EDIT: string = 'isEdit'; - /** - * Key of details page information. - */ - static readonly DETAIL_INFO_KEY: string = 'key'; - /** - * Monitors distributed database changes. - */ - static readonly DATA_CHANGE: string = 'dataChange'; - /** - * Start ability callback. - */ - static readonly EXIT: string = 'exit'; - /** - * Start ability callback id. - */ - static readonly SUBSCRIBE_ID: number = 100; - /** - * The mode of start device discovery info. - */ - static readonly INFO_MODE: number = 0xAA; - /** - * The frequency of start device discovery info. - */ - static readonly INFO_FREQ: number = 2; - /** - * Discover device list. - */ - static readonly DISCOVER_DEVICE_LIST: string = 'discoverDeviceList'; - /** - * Zero. - */ - static readonly ZERO: string = '0'; - /** - * One. - */ - static readonly ONE: number = 1; - /** - * Trusted device list. - */ - static readonly TRUSTED_DEVICE_LIST: string = 'trustedDeviceList'; - /** - * Subscribe ID range. - */ - static readonly SUBSCRIBE_ID_RANGE: number = 65536; - /** - * Auth type. - */ - static readonly AUTH_TYPE: number = 1; - /** - * Subscribe mode. - */ - static readonly SUBSCRIBE_MODE: number = 0xAA; - /** - * Subscribe medium. - */ - static readonly SUBSCRIBE_MEDIUM: number = 0; - /** - * Subscribe freq. - */ - static readonly SUBSCRIBE_FREQ: number = 2; - /** - * Subscribe capability. - */ - static readonly SUBSCRIBE_CAPABILITY: number = 0; - /** - * Font weight. - */ - static readonly FONT_WEIGHT_500: number = 500; - /** - * Invalid Index. - */ - static readonly INVALID_INDEX: number = -1; - /** - * One hundred percent. - */ - static readonly FULL_PERCENT: string = '100%'; - /** - * Number one. - */ - static readonly NUMBER_ONE: number = 1; - /** - * Device name width. - */ - static readonly DEVICE_NAME_WIDTH: string = '78%'; - /** - * Select icon width. - */ - static readonly SELECT_ICON_WIDTH: string = '8%'; - /** - * Localhost name. - */ - static readonly LOCALHOST_NAME: string = 'Local Host'; - /** - * Entry ability. - */ - static readonly ENTRY_ABILITY: string = 'EntryAbility'; - /** - * Ability name. - */ - static readonly BUNDLE_NAME: string = 'com.example.crossDeviceDataSynchronization'; - /** - * App name. - */ - static readonly APP_NAME: string = 'Distributed Contacts'; - /** - * Contacts name max length. - */ - static readonly CONTACTS_NAME_MAX_LENGTH: number = 10; - /** - * Contacts tel max length. - */ - static readonly CONTACTS_TEL_MAX_LENGTH: number = 11; - /** - * Contacts detail input box max length. - */ - static readonly CONTACTS_DETAIL_MAX: number = 20; - /** - * Distributed mail image storage location. - */ - static readonly MAIL_DISTRIBUTED_PATH: string = '/data/storage/el2/distributedfiles/'; - /** - * File name for storing cross-end photos. - */ - static readonly PICTURE_NAME: string = 'pictureName'; - /** - * Supports transfer. - */ - static readonly CAN_CONTINUATION: string = 'true'; - /** - * Forwarding is not supported. - */ - static readonly NO_CONTINUATION: string = 'false'; - /** - * With picture streaming. - */ - static readonly CAN_PHOTO: string = 'true'; - /** - * Transfer without picture. - */ - static readonly NO_PHOTO: string = 'false'; - /** - * Image stream size. - */ - static readonly IMAGE_STREAM_SIZE: number = 10240000; - /** - * Bottom toolbar picture spacing. - */ - static readonly BOTTOM_IMAGE_SPACE: number = 24; - static readonly FILE_BUFFER_SIZE: number = 4096; - static readonly APPENDIX_LIST_SPACE: number = 5; - static readonly APPENDIX_LIST_START_MARGIN: number = 15; - static readonly APPENDIX_LIST_END_MARGIN: number = 15; - /** - * Rdb database config. - */ - static readonly STORE_CONFIG: relationalStore.StoreConfig = { - name: 'database.db', - securityLevel: relationalStore.SecurityLevel.S1, - }; - /** - * Component size. - */ - static readonly DIALOG_HEIGHT = '55%'; - static readonly TABS_HEIGHT = '45%'; - static readonly MINIMUM_SIZE = 0; - static readonly FULL_SIZE = 1; - static readonly PROMPT_BOTTOM = '70vp'; - /** - * Component location. - */ - static readonly EDIT_POSITION_X = '80%'; - static readonly EDIT_POSITION_Y = '90%'; - static readonly DELETE_POSITION_X = '50%'; - static readonly DELETE_POSITION_Y = '90%'; - /** - * Log tag. - */ - static readonly RDB_TAG = '[Debug.Rdb]'; - static readonly TABLE_TAG = '[Debug.AccountTable]'; - static readonly INDEX_TAG = '[Debug.Index]'; -} \ No newline at end of file diff --git a/entry/src/main/ets/entryability/EntryAbility.ets b/entry/src/main/ets/entryability/EntryAbility.ets index 30fd30b..cb4634f 100644 --- a/entry/src/main/ets/entryability/EntryAbility.ets +++ b/entry/src/main/ets/entryability/EntryAbility.ets @@ -15,17 +15,13 @@ import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; -import { commonType, distributedDataObject } from '@kit.ArkData'; +import { distributedDataObject } from '@kit.ArkData'; import { window } from '@kit.ArkUI'; -import { AppendixBean } from '../viewmodel/MailViewModel'; -import { fileIo, fileUri } from '@kit.CoreFileKit'; import { JSON } from '@kit.ArkTS'; import { hilog } from '@kit.PerformanceAnalysisKit'; import { MailInfoManager } from '../utils/MailInfoManager'; -import CommonConstants from '../common/CommonConstants'; const DOMAIN = 0x0000; -let uiContext: UIContext | undefined = undefined; export default class EntryAbility extends UIAbility { private distributedObject: distributedDataObject.DataObject | undefined = undefined; @@ -59,12 +55,8 @@ export default class EntryAbility extends UIAbility { if (launchParam.launchReason !== AbilityConstant.LaunchReason.CONTINUATION) { return; } - AppStorage.setOrCreate('isContinuation', CommonConstants.CAN_CONTINUATION); - AppStorage.setOrCreate>('appendix', - JSON.parse(want.parameters?.appendix as string) as Array); // Obtain the session ID of a distributed data object. - - let mailInfoManager: MailInfoManager = new MailInfoManager(undefined, undefined, undefined, undefined, undefined); + let mailInfoManager: MailInfoManager = new MailInfoManager(undefined, undefined, undefined, undefined); this.distributedObject = distributedDataObject.create(this.context, mailInfoManager); // Add a data restored listener. try { @@ -79,13 +71,6 @@ export default class EntryAbility extends UIAbility { AppStorage.setOrCreate('sender', this.distributedObject['sender']); AppStorage.setOrCreate('subject', this.distributedObject['subject']); AppStorage.setOrCreate('emailContent', this.distributedObject['emailContent']); - AppStorage.setOrCreate('attachments', this.distributedObject['attachments']); - let attachments = this.distributedObject['attachments'] as commonType.Assets; - hilog.info(0x0000, 'EntryAbility', - 'this.distributedObject[attachments] ' + JSON.stringify(this.distributedObject['attachments'])); - for (const attachment of attachments) { - this.fileCopy(attachment); - } } }); let sessionId: string = want.parameters?.distributedSessionId as string; @@ -112,26 +97,14 @@ export default class EntryAbility extends UIAbility { } async onContinue(wantParam: Record): Promise { - wantParam.appendix = JSON.stringify(AppStorage.get>('appendix')); try { // Generate the session ID of the distributed data object. let sessionId: string = distributedDataObject.genSessionId(); wantParam.distributedSessionId = sessionId; - let appendix = AppStorage.get>('appendix'); - let assets: commonType.Assets = []; - if (appendix) { - for (let i = 0; i < appendix.length; i++) { - let append = appendix[i]; - let attachment: commonType.Asset = this.getAssetInfo(append)!; - assets.push(attachment); - } - } - let mailInfoManager: MailInfoManager = new MailInfoManager(AppStorage.get('recipient'), AppStorage.get('sender'), - AppStorage.get('subject'), AppStorage.get('emailContent'), assets); - let source = mailInfoManager.flatAssets(); - this.distributedObject = distributedDataObject.create(this.context, source); + AppStorage.get('subject'), AppStorage.get('emailContent')); + this.distributedObject = distributedDataObject.create(this.context, mailInfoManager); this.distributedObject.setSessionId(sessionId).catch((err: BusinessError) => { hilog.error(0x0000, 'EntryAbility', `setSessionId error. Code:${err.code},message: ${err.message}`); }); @@ -146,66 +119,6 @@ export default class EntryAbility extends UIAbility { return AbilityConstant.OnContinueResult.AGREE; } - /** - * Copy distributed files. - * @param attachmentRecord - * @param key - */ - private fileCopy(attachment: commonType.Asset) { - let filePath: string = this.context.distributedFilesDir + '/' + attachment.name; - let savePath: string = this.context.filesDir + '/' + attachment.name; - try { - if (fileIo.accessSync(filePath)) { - let saveFile = fileIo.openSync(savePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); - let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE); - let buf: ArrayBuffer = new ArrayBuffer(CommonConstants.FILE_BUFFER_SIZE); - let readSize = 0; - let readLen = fileIo.readSync(file.fd, buf, { - offset: readSize - }); - while (readLen > 0) { - readSize += readLen; - fileIo.writeSync(saveFile.fd, buf); - readLen = fileIo.readSync(file.fd, buf, { - offset: readSize - }); - } - fileIo.closeSync(file); - fileIo.closeSync(saveFile); - hilog.info(0x0000, 'EntryAbility', attachment.name + 'synchronized successfully.'); - } - } catch (error) { - hilog.error(0x0000, 'EntryAbility', `have error. Code:${error.code}, message: ${error.message}`); - } - } - - /** - * Obtain distributed file asset information. - * @param append - * @returns - */ - private getAssetInfo(append: AppendixBean) { - try { - let filePath = uiContext!.getHostContext()!.distributedFilesDir + '/' + append.fileName; - fileIo.statSync(filePath); - let uri: string = fileUri.getUriFromPath(filePath); - let stat = fileIo.statSync(filePath); - - let attachment: commonType.Asset = { - name: append.fileName, - uri: uri, - path: filePath, - createTime: stat.ctime.toString(), - modifyTime: stat.ctime.toString(), - size: stat.size.toString() - }; - return attachment; - } catch (error) { - hilog.error(0x0000, 'EntryAbility', `statSync error, Code: ${error.code} message:${error.message}`); - return null; - } - } - onWindowStageCreate(windowStage: window.WindowStage) { // Main window is created, set main page for this ability. hilog.info(0x0000, 'EntryAbility', 'Ability onWindowStageCreate'); @@ -216,11 +129,6 @@ export default class EntryAbility extends UIAbility { return; } hilog.info(0x0000, 'EntryAbility', 'Succeeded in loading the content, ', `Data: ${data}`); - try { - uiContext = windowStage.getMainWindowSync().getUIContext(); - } catch (error) { - hilog.error(0x0000, 'EntryAbility', `getMainWindowSync error .Code:${error.code}, message: ${error.message}`); - } }); } diff --git a/entry/src/main/ets/pages/MailHomePage.ets b/entry/src/main/ets/pages/MailHomePage.ets index 11009e6..359bc7f 100644 --- a/entry/src/main/ets/pages/MailHomePage.ets +++ b/entry/src/main/ets/pages/MailHomePage.ets @@ -13,26 +13,16 @@ * limitations under the License. */ -import { BusinessError } from '@kit.BasicServicesKit'; import { AbilityConstant, common } from '@kit.AbilityKit'; -import { fileIo, picker } from '@kit.CoreFileKit'; -import { image } from '@kit.ImageKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; -import CommonConstants from '../common/CommonConstants'; -import { AppendixBean, imageIndex } from '../viewmodel/MailViewModel'; @Entry @Component struct MailHomePage { - @State crossEndPicture: PixelMap | undefined = undefined; - @State uiContext: UIContext | undefined = AppStorage.get('uiContext'); @StorageLink('recipient') recipient: string = ''; @StorageLink('sender') sender: string = ''; @StorageLink('subject') subject: string = ''; @StorageLink('emailContent') emailContent: string = ''; - @StorageLink('isContinuation') isContinuation: string = CommonConstants.NO_CONTINUATION; - @StorageLink('appendix') appendix: Array = []; - @StorageLink('attachments') attachments: Object = []; private context = this.getUIContext().getHostContext() as common.UIAbilityContext; onPageShow(): void { @@ -101,7 +91,7 @@ struct MailHomePage { .focusable(true) TextInput({ text: this.recipient }) .type(InputType.Email) - .width(CommonConstants.PERCENTAGE_MAX) + .width('100%') .backgroundColor($r('app.color.input_textarea_background')) .caretColor($r('app.color.text_cursor_color')) .onChange((recipientName: string) => { @@ -123,7 +113,7 @@ struct MailHomePage { .fontSize($r('app.float.text_font_size')) TextInput({ text: this.sender }) .type(InputType.Email) - .width(CommonConstants.PERCENTAGE_MAX) + .width('100%') .backgroundColor($r('app.color.input_textarea_background')) .caretColor($r('app.color.text_cursor_color')) .onChange((senderName: string) => { @@ -144,7 +134,7 @@ struct MailHomePage { .fontColor($r('app.color.content_text_color')) .fontSize($r('app.float.text_font_size')) TextInput({ text: this.subject }) - .width(CommonConstants.PERCENTAGE_MAX) + .width('100%') .backgroundColor($r('app.color.input_textarea_background')) .caretColor($r('app.color.text_cursor_color')) .onChange((subjectInfo: string) => { @@ -159,48 +149,10 @@ struct MailHomePage { color: 'rgba(0, 0, 0, 0.2)' }) - // Appendix - if (this.appendix.length > 0) { - Row() { - Text($r('app.string.appendix_text')) - .fontColor($r('app.color.content_text_color')) - .fontSize($r('app.float.text_font_size')) - List({ space: CommonConstants.APPENDIX_LIST_SPACE }) { - ForEach(this.appendix, (item: AppendixBean) => { - ListItem() { - Row() { - Image(imageIndex[item.iconIndex].icon) - .width($r('app.float.appendix_list_item_width')) - .aspectRatio(1) - .margin({ right: $r('app.float.appendix_list_item_margin_right') }) - Text(item.fileName) - .fontSize($r('app.float.appendix_file_name_font_size')) - } - } - }, (_item: AppendixBean, index: number) => index.toString()) - } - .listDirection(Axis.Horizontal) - .alignListItem(ListItemAlign.Center) - .divider({ - strokeWidth: 1, - color: Color.Grey, - startMargin: CommonConstants.APPENDIX_LIST_START_MARGIN, - endMargin: CommonConstants.APPENDIX_LIST_END_MARGIN - }) - .scrollBar(BarState.Off) - } - .width('100%') - .height(56) - .border({ - width: { bottom: 1 }, - color: 'rgba(0, 0, 0, 0.2)' - }) - } - // Message content area. Row() { TextArea({ text: this.emailContent }) - .height(CommonConstants.PERCENTAGE_MAX) + .height('100%') .padding(0) .backgroundColor($r('app.color.input_textarea_background')) .borderRadius(0) @@ -212,91 +164,14 @@ struct MailHomePage { } .layoutWeight(1) .alignItems(VerticalAlign.Top) - .width(CommonConstants.PERCENTAGE_MAX) + .width('100%') .margin({ top: $r('app.float.text_image_margin'), bottom: $r('app.float.navigation_height') }) - - // Image display area. - Row() { - if (this.crossEndPicture) { - Image(this.crossEndPicture) - .height($r('app.float.photo_picture_height')) - .objectFit(ImageFit.ScaleDown) - .borderRadius($r('app.float.photo_picture_radius')) - } - } - .width(CommonConstants.PERCENTAGE_MAX) - .justifyContent(FlexAlign.Start) } .width('100%') .layoutWeight(1) .padding({ left: 8, right: 8 }) } - - /** - * Rendering pictures. - * - * @param buffer Returned image stream of cross-end photographing. - */ - createPicture(buffer: ArrayBuffer): void { - let imageSource = image.createImageSource(buffer); - imageSource.createPixelMap().then((pixelMap) => { - this.crossEndPicture = pixelMap; - }); - } - - aboutToDisappear(): void { - this.crossEndPicture?.release(); - } - - /** - * Add appendix from file manager. - * - * @param fileType - */ - documentSelect(fileType: number): void { - try { - let DocumentSelectOptions = new picker.DocumentSelectOptions(); - let documentPicker = new picker.DocumentViewPicker(); - documentPicker.select(DocumentSelectOptions).then((DocumentSelectResult: Array) => { - for (let documentSelectResultElement of DocumentSelectResult) { - let buf = new ArrayBuffer(CommonConstants.FILE_BUFFER_SIZE); - let readSize = 0; - let file = fileIo.openSync(documentSelectResultElement, fileIo.OpenMode.READ_ONLY); - let readLen = fileIo.readSync(file.fd, buf, { offset: readSize }); - // File name is not supported chinese name. - let fileName = file.name; - if (!fileName.endsWith(imageIndex[fileType].fileType) || - new RegExp("\[\\u4E00-\\u9FA5]|[\\uFE30-\\uFFA0]", "gi").test(fileName)) { - this.getUIContext().getPromptAction().showToast({ - message: $r('app.string.alert_message_chinese') - }); - return; - } - let destination = fileIo.openSync(this.getUIContext().getHostContext()! - .filesDir + '/' + fileName, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); - let destinationDistribute = fileIo.openSync(this.getUIContext().getHostContext()! - .distributedFilesDir + '/' + fileName, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); - while (readLen > 0) { - readSize += readLen; - fileIo.writeSync(destination.fd, buf); - fileIo.writeSync(destinationDistribute.fd, buf); - readLen = fileIo.readSync(file.fd, buf, { offset: readSize }); - } - fileIo.closeSync(file); - fileIo.closeSync(destination); - fileIo.closeSync(destinationDistribute); - this.appendix.push({ iconIndex: fileType, fileName: fileName }); - } - hilog.info(0x0000, 'MailHomePage', - `DocumentViewPicker.select successfully, DocumentSelectResult uri: ${JSON.stringify(DocumentSelectResult)}`); - }).catch((error: BusinessError) => { - hilog.error(0x0000, 'MailHomePage', `have error. Code:${error.code},message: ${error.message}`); - }); - } catch (error) { - hilog.error(0x0000, 'MailHomePage', `have error. Code:${error.code},message: ${error.message}`); - } - } } \ No newline at end of file diff --git a/entry/src/main/ets/utils/MailInfoManager.ets b/entry/src/main/ets/utils/MailInfoManager.ets index d89f6b9..7dab966 100644 --- a/entry/src/main/ets/utils/MailInfoManager.ets +++ b/entry/src/main/ets/utils/MailInfoManager.ets @@ -13,33 +13,18 @@ * limitations under the License. */ -import { commonType } from '@kit.ArkData'; - export class MailInfoManager { private recipient: string | undefined; private sender: string | undefined; private subject: string | undefined; private emailContent: string | undefined; - private attachments: commonType.Assets | undefined; constructor(recipient: string | undefined, sender: string | undefined, subject: string | undefined, - emailContent: string | undefined, attachments: commonType.Assets | undefined) { + emailContent: string | undefined) { this.recipient = recipient; this.sender = sender; this.subject = subject; this.emailContent = emailContent; - this.attachments = attachments; - } - - flatAssets(): object { - let obj: object = this; - if (!this.attachments) { - return obj; - } - for (let i = 0; i < this.attachments.length; i++) { - obj[`attachments${i}`] = this.attachments[i]; - } - return obj; } getRecipient(): string | undefined { @@ -73,12 +58,4 @@ export class MailInfoManager { setEmailContent(value: string | undefined) { this.emailContent = value; } - - getAttachments(): commonType.Assets | undefined { - return this.attachments; - } - - setAttachments(value: commonType.Assets) { - this.attachments = value; - } } \ No newline at end of file diff --git a/entry/src/main/ets/viewmodel/MailViewModel.ets b/entry/src/main/ets/viewmodel/MailViewModel.ets deleted file mode 100644 index 305c210..0000000 --- a/entry/src/main/ets/viewmodel/MailViewModel.ets +++ /dev/null @@ -1,66 +0,0 @@ -/* - * 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. - */ - -export interface AppendixBean { - /** - * Icon index. - */ - iconIndex: number; - - /** - * File name. - */ - fileName: string; -} - -export interface AppendixFile { - /** - * Index. - */ - index: number; - - /** - * File type. - */ - fileType: string; - - /** - * File type icon. - */ - icon: Resource; -} - -export enum FileType { - /** - * Txt file. - */ - TXT, - - /** - * Doc file. - */ - DOC, - - /** - * Pdf file. - */ - PDF -} - -export const imageIndex: Array = [ - { index: FileType.TXT, fileType: 'txt', icon: $r('app.media.ic_files_grid_txt') }, - { index: FileType.DOC, fileType: 'doc', icon: $r('app.media.ic_files_documents') }, - { index: FileType.PDF, fileType: 'pdf', icon: $r('app.media.ic_files_pdf') } -]; \ No newline at end of file -- Gitee