diff --git a/NetworkReconnection/entry/src/main/ets/entryability/EntryAbility.ets b/NetworkReconnection/entry/src/main/ets/entryability/EntryAbility.ets index 176523e8ec52fddc8ab0bf746505f0b135afbfe5..e93758ba827778546a5e6278562ccad87245d74b 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 { + // [StartExclude entry_ability] 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'); } + // [EndExclude entry_ability] onForeground(): void { // Ability has brought to foreground + // [StartExclude entry_ability] ConnectionUtil.register(); ConnectionUtil.netCapabilitiesChange(); AppStorage.setOrCreate('onForeground', true); + // [EndExclude entry_ability] hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); } onBackground(): void { // Ability has back to background + // [StartExclude entry_ability] ConnectionUtil.unregister(); + // [EndExclude entry_ability] 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 b06255fb395554704b770cf689890be49508402f..99a1989ce7d40c5c7823eb69de9313589a8153e8 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, @@ -134,8 +135,9 @@ export struct HTTPReconnection { }); } catch (err) { this.isDownload = false; - promptAction.showToast({ message: $r('app.string.download_error') }); + promptAction.showToast({ message: "Download Error" }); 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 4d1adb63e6c7bc5fdc117ce32f661b30d64f6012..f003321e73940f215c0157c80f51f90bf80e6603 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); } + // [StartExclude create_rcp_session] rcpDownloadFile() { this.process = Math.floor(this.downloadSize / this.contentLength * 100); const helper = photoAccessHelper.getPhotoAccessHelper(this.context); @@ -137,6 +139,7 @@ export struct RCPReconnection { }); }); } + // [EndExclude create_rcp_session] async getRcpRequest(url: string, retry: number): Promise { try { @@ -153,9 +156,10 @@ export struct RCPReconnection { } }) } catch (err) { - promptAction.showToast({ message: $r('app.string.download_error') }); + promptAction.showToast({ message: "Download Error" }); this.isDownload = false; 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 b53c5b25adf5d6965a546ede3988bbe5c78eaa72..c5bf10dcd83f900f17b50600ff80a8a7976bc84b 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; + // [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); @@ -43,16 +47,20 @@ export struct SocketReconnection { aboutToAppear(): void { this.tcpSocketConnect(); } + // [EndExclude storage_on_foreground] 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); } + // [StartExclude on_socket_updated] build() { NavDestination() { Column() { @@ -106,10 +114,11 @@ export struct SocketReconnection { } .title(CommonConstants.Socket_DOWNLOAD) } + // [EndExclude on_socket_updated] tcpSocketConnect() { if (!this.netAvailable) { - promptAction.showToast({ message: $r('app.string.connect_error') }); + promptAction.showToast({ message: "Connect error" }); return; } @@ -132,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; } @@ -146,12 +155,13 @@ 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; } }); }); } + // [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 6573898ce1c2bf2a47f9c73cd974b77b36764d58..f00f1c2e17ac7e5dd3537dd0e892fdf61b194bd1 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) => {