From 063d5610d26c38f58e99d9d44a6300bee539e44c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9F=B3=E4=B8=9C=E6=B5=B7?= Date: Thu, 15 May 2025 10:06:26 +0800 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E5=9D=97=E6=A0=87=E8=AF=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../entry/src/main/ets/entryability/EntryAbility.ets | 8 ++++++++ .../entry/src/main/ets/pages/HTTPReconnection.ets | 2 ++ .../entry/src/main/ets/pages/RCPReconnection.ets | 4 ++++ .../entry/src/main/ets/pages/SocketReconnection.ets | 10 ++++++++++ .../entry/src/main/ets/utils/ConnectionUtil.ets | 2 ++ 5 files changed, 26 insertions(+) diff --git a/NetworkReconnection/entry/src/main/ets/entryability/EntryAbility.ets b/NetworkReconnection/entry/src/main/ets/entryability/EntryAbility.ets index 176523e..1619714 100644 --- a/NetworkReconnection/entry/src/main/ets/entryability/EntryAbility.ets +++ b/NetworkReconnection/entry/src/main/ets/entryability/EntryAbility.ets @@ -3,7 +3,9 @@ import { hilog } from '@kit.PerformanceAnalysisKit'; import { window } from '@kit.ArkUI'; import ConnectionUtil from '../utils/ConnectionUtil'; +// [Start entry_ability] export default class EntryAbility extends UIAbility { + // DocsDot onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); } @@ -30,19 +32,25 @@ export default class EntryAbility extends UIAbility { // Main window is destroyed, release UI related resources hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); } + // DocsDot onForeground(): void { // Ability has brought to foreground + // DocsDot ConnectionUtil.register(); ConnectionUtil.netCapabilitiesChange(); AppStorage.setOrCreate('onForeground', true); + // DocsDot hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); } onBackground(): void { // Ability has back to background + // DocsDot ConnectionUtil.unregister(); + // DocsDot AppStorage.setOrCreate('onForeground', false); hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); } } +// [End entry_ability] diff --git a/NetworkReconnection/entry/src/main/ets/pages/HTTPReconnection.ets b/NetworkReconnection/entry/src/main/ets/pages/HTTPReconnection.ets index b06255f..73d9efe 100644 --- a/NetworkReconnection/entry/src/main/ets/pages/HTTPReconnection.ets +++ b/NetworkReconnection/entry/src/main/ets/pages/HTTPReconnection.ets @@ -117,6 +117,7 @@ export struct HTTPReconnection { } } + // [Start get_http_request] async getHttpRequest(url: string, retry: number): Promise { try { return await this.httpRequest?.requestInStream(url, @@ -138,4 +139,5 @@ export struct HTTPReconnection { return; } } + // [End get_http_request] } diff --git a/NetworkReconnection/entry/src/main/ets/pages/RCPReconnection.ets b/NetworkReconnection/entry/src/main/ets/pages/RCPReconnection.ets index 4d1adb6..dd08857 100644 --- a/NetworkReconnection/entry/src/main/ets/pages/RCPReconnection.ets +++ b/NetworkReconnection/entry/src/main/ets/pages/RCPReconnection.ets @@ -87,6 +87,7 @@ export struct RCPReconnection { .title(CommonConstants.RCP_DOWNLOAD) } + // [Start create_rcp_session] createRCPSession(): rcp.Session { const customHttpEventsHandler: rcp.HttpEventsHandler = { onDownloadProgress: (totalSize: number, transferredSize: number) => { @@ -114,6 +115,7 @@ export struct RCPReconnection { return rcp.createSession(sessionConfig); } + // DocsDot rcpDownloadFile() { this.process = Math.floor(this.downloadSize / this.contentLength * 100); const helper = photoAccessHelper.getPhotoAccessHelper(this.context); @@ -137,6 +139,7 @@ export struct RCPReconnection { }); }); } + // DocsDot async getRcpRequest(url: string, retry: number): Promise { try { @@ -158,4 +161,5 @@ export struct RCPReconnection { return; } } + // [End create_rcp_session] } diff --git a/NetworkReconnection/entry/src/main/ets/pages/SocketReconnection.ets b/NetworkReconnection/entry/src/main/ets/pages/SocketReconnection.ets index b53c5b2..8e8c484 100644 --- a/NetworkReconnection/entry/src/main/ets/pages/SocketReconnection.ets +++ b/NetworkReconnection/entry/src/main/ets/pages/SocketReconnection.ets @@ -27,8 +27,12 @@ import { photoAccessHelper } from '@kit.MediaLibraryKit'; export struct SocketReconnection { @State process: number = 0; @State isDownload: boolean = false; + // [Start storage_net_available] @StorageProp('netAvailable') @Watch('onSocketUpdated') netAvailable: boolean = true; + // [End storage_net_available] + // [Start storage_on_foreground] @StorageProp('onForeground') @Watch('onForegroundChange') onForeground: boolean = true; + // DocsDot private context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; private tcp?: socket.TCPSocket; private receivedDataBuf: buffer.Buffer = buffer.alloc(15 * 1024 * 1024); @@ -43,16 +47,20 @@ export struct SocketReconnection { aboutToAppear(): void { this.tcpSocketConnect(); } + // DocsDot onForegroundChange(): void { this.onForeground ? this.tcpSocketConnect() : this.tcpSocketDisconnect(); } + // [End storage_on_foreground] + // [Start on_socket_updated] onSocketUpdated() { this.netAvailable ? this.tcpSocketConnect() : this.tcpSocketDisconnect(); Logger.info('netAvailable:' + this.netAvailable); } + // DocsDot build() { NavDestination() { Column() { @@ -106,6 +114,7 @@ export struct SocketReconnection { } .title(CommonConstants.Socket_DOWNLOAD) } + // DocsDot tcpSocketConnect() { if (!this.netAvailable) { @@ -152,6 +161,7 @@ export struct SocketReconnection { }); }); } + // [End on_socket_updated] tcpSocketDisconnect() { this.tcp?.close(); diff --git a/NetworkReconnection/entry/src/main/ets/utils/ConnectionUtil.ets b/NetworkReconnection/entry/src/main/ets/utils/ConnectionUtil.ets index 6573898..f00f1c2 100644 --- a/NetworkReconnection/entry/src/main/ets/utils/ConnectionUtil.ets +++ b/NetworkReconnection/entry/src/main/ets/utils/ConnectionUtil.ets @@ -18,6 +18,7 @@ import { BusinessError } from '@kit.BasicServicesKit'; import Logger from '../utils/Logger'; export class ConnectionUtil { + // [Start net_capabilities_change] private netCon: connection.NetConnection = connection.createNetConnection(); register() { @@ -43,6 +44,7 @@ export class ConnectionUtil { Logger.info("WifiChangeListen-- Succeeded to get data: " + JSON.stringify(data)); }); } + // [End net_capabilities_change] unregister() { this.netCon.unregister((error: BusinessError) => { -- Gitee From 35693addf9294fdc9277e8a016df58e72fb6e8fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9F=B3=E4=B8=9C=E6=B5=B7?= Date: Thu, 15 May 2025 14:19:12 +0800 Subject: [PATCH 2/3] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E5=9D=97=E6=A0=87=E8=AF=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../entry/src/main/ets/entryability/EntryAbility.ets | 12 ++++++------ .../entry/src/main/ets/pages/RCPReconnection.ets | 6 +++--- .../entry/src/main/ets/pages/SocketReconnection.ets | 8 ++++---- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/NetworkReconnection/entry/src/main/ets/entryability/EntryAbility.ets b/NetworkReconnection/entry/src/main/ets/entryability/EntryAbility.ets index 1619714..e93758b 100644 --- a/NetworkReconnection/entry/src/main/ets/entryability/EntryAbility.ets +++ b/NetworkReconnection/entry/src/main/ets/entryability/EntryAbility.ets @@ -5,7 +5,7 @@ import ConnectionUtil from '../utils/ConnectionUtil'; // [Start entry_ability] export default class EntryAbility extends UIAbility { - // DocsDot + // [StartExclude entry_ability] onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); } @@ -32,23 +32,23 @@ export default class EntryAbility extends UIAbility { // Main window is destroyed, release UI related resources hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); } - // DocsDot + // [EndExclude entry_ability] onForeground(): void { // Ability has brought to foreground - // DocsDot + // [StartExclude entry_ability] ConnectionUtil.register(); ConnectionUtil.netCapabilitiesChange(); AppStorage.setOrCreate('onForeground', true); - // DocsDot + // [EndExclude entry_ability] hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); } onBackground(): void { // Ability has back to background - // DocsDot + // [StartExclude entry_ability] ConnectionUtil.unregister(); - // DocsDot + // [EndExclude entry_ability] AppStorage.setOrCreate('onForeground', false); hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); } diff --git a/NetworkReconnection/entry/src/main/ets/pages/RCPReconnection.ets b/NetworkReconnection/entry/src/main/ets/pages/RCPReconnection.ets index dd08857..9b02eae 100644 --- a/NetworkReconnection/entry/src/main/ets/pages/RCPReconnection.ets +++ b/NetworkReconnection/entry/src/main/ets/pages/RCPReconnection.ets @@ -87,7 +87,7 @@ export struct RCPReconnection { .title(CommonConstants.RCP_DOWNLOAD) } - // [Start create_rcp_session] + // [StartExclude create_rcp_session] createRCPSession(): rcp.Session { const customHttpEventsHandler: rcp.HttpEventsHandler = { onDownloadProgress: (totalSize: number, transferredSize: number) => { @@ -114,8 +114,8 @@ export struct RCPReconnection { } return rcp.createSession(sessionConfig); } + // [EndExclude create_rcp_session] - // DocsDot rcpDownloadFile() { this.process = Math.floor(this.downloadSize / this.contentLength * 100); const helper = photoAccessHelper.getPhotoAccessHelper(this.context); @@ -139,8 +139,8 @@ export struct RCPReconnection { }); }); } - // DocsDot + // [Start create_rcp_session] async getRcpRequest(url: string, retry: number): Promise { try { return await this.session.get(url) diff --git a/NetworkReconnection/entry/src/main/ets/pages/SocketReconnection.ets b/NetworkReconnection/entry/src/main/ets/pages/SocketReconnection.ets index 8e8c484..1dd4f6d 100644 --- a/NetworkReconnection/entry/src/main/ets/pages/SocketReconnection.ets +++ b/NetworkReconnection/entry/src/main/ets/pages/SocketReconnection.ets @@ -32,7 +32,7 @@ export struct SocketReconnection { // [End storage_net_available] // [Start storage_on_foreground] @StorageProp('onForeground') @Watch('onForegroundChange') onForeground: boolean = true; - // DocsDot + // [StartExclude storage_on_foreground] private context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; private tcp?: socket.TCPSocket; private receivedDataBuf: buffer.Buffer = buffer.alloc(15 * 1024 * 1024); @@ -47,7 +47,7 @@ export struct SocketReconnection { aboutToAppear(): void { this.tcpSocketConnect(); } - // DocsDot + // [EndExclude storage_on_foreground] onForegroundChange(): void { this.onForeground ? this.tcpSocketConnect() : this.tcpSocketDisconnect(); @@ -60,7 +60,7 @@ export struct SocketReconnection { Logger.info('netAvailable:' + this.netAvailable); } - // DocsDot + // [StartExclude on_socket_updated] build() { NavDestination() { Column() { @@ -114,7 +114,7 @@ export struct SocketReconnection { } .title(CommonConstants.Socket_DOWNLOAD) } - // DocsDot + // [EndExclude on_socket_updated] tcpSocketConnect() { if (!this.netAvailable) { -- Gitee From f37d483f62990688b1315c7cfce20266dbb1405f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9F=B3=E4=B8=9C=E6=B5=B7?= Date: Fri, 16 May 2025 15:56:22 +0800 Subject: [PATCH 3/3] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E5=9D=97=E6=A0=87=E8=AF=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../entry/src/main/ets/pages/HTTPReconnection.ets | 2 +- .../entry/src/main/ets/pages/RCPReconnection.ets | 8 ++++---- .../entry/src/main/ets/pages/SocketReconnection.ets | 6 +++--- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/NetworkReconnection/entry/src/main/ets/pages/HTTPReconnection.ets b/NetworkReconnection/entry/src/main/ets/pages/HTTPReconnection.ets index 73d9efe..99a1989 100644 --- a/NetworkReconnection/entry/src/main/ets/pages/HTTPReconnection.ets +++ b/NetworkReconnection/entry/src/main/ets/pages/HTTPReconnection.ets @@ -135,7 +135,7 @@ export struct HTTPReconnection { }); } catch (err) { this.isDownload = false; - promptAction.showToast({ message: $r('app.string.download_error') }); + promptAction.showToast({ message: "Download Error" }); return; } } diff --git a/NetworkReconnection/entry/src/main/ets/pages/RCPReconnection.ets b/NetworkReconnection/entry/src/main/ets/pages/RCPReconnection.ets index 9b02eae..f003321 100644 --- a/NetworkReconnection/entry/src/main/ets/pages/RCPReconnection.ets +++ b/NetworkReconnection/entry/src/main/ets/pages/RCPReconnection.ets @@ -87,7 +87,7 @@ export struct RCPReconnection { .title(CommonConstants.RCP_DOWNLOAD) } - // [StartExclude create_rcp_session] + // [Start create_rcp_session] createRCPSession(): rcp.Session { const customHttpEventsHandler: rcp.HttpEventsHandler = { onDownloadProgress: (totalSize: number, transferredSize: number) => { @@ -114,8 +114,8 @@ export struct RCPReconnection { } return rcp.createSession(sessionConfig); } - // [EndExclude create_rcp_session] + // [StartExclude create_rcp_session] rcpDownloadFile() { this.process = Math.floor(this.downloadSize / this.contentLength * 100); const helper = photoAccessHelper.getPhotoAccessHelper(this.context); @@ -139,8 +139,8 @@ export struct RCPReconnection { }); }); } + // [EndExclude create_rcp_session] - // [Start create_rcp_session] async getRcpRequest(url: string, retry: number): Promise { try { return await this.session.get(url) @@ -156,7 +156,7 @@ export struct RCPReconnection { } }) } catch (err) { - promptAction.showToast({ message: $r('app.string.download_error') }); + promptAction.showToast({ message: "Download Error" }); this.isDownload = false; return; } diff --git a/NetworkReconnection/entry/src/main/ets/pages/SocketReconnection.ets b/NetworkReconnection/entry/src/main/ets/pages/SocketReconnection.ets index 1dd4f6d..c5bf10d 100644 --- a/NetworkReconnection/entry/src/main/ets/pages/SocketReconnection.ets +++ b/NetworkReconnection/entry/src/main/ets/pages/SocketReconnection.ets @@ -118,7 +118,7 @@ export struct SocketReconnection { tcpSocketConnect() { if (!this.netAvailable) { - promptAction.showToast({ message: $r('app.string.connect_error') }); + promptAction.showToast({ message: "Connect error" }); return; } @@ -141,7 +141,7 @@ export struct SocketReconnection { this.tcp.bind(clientIpAddress, (err: BusinessError) => { if (err) { Logger.error('bind fail' + JSON.stringify(err)); - promptAction.showToast({ message: $r('app.string.connect_error') }); + promptAction.showToast({ message: "Connect error" }); return; } @@ -155,7 +155,7 @@ export struct SocketReconnection { this.tcp?.connect(tcpConnect, (err: BusinessError) => { if (err) { Logger.error('connect fail'); - promptAction.showToast({ message: $r('app.string.connect_error') }); + promptAction.showToast({ message: "Connect error" }); return; } }); -- Gitee