Showing
7 changed files
with
242 additions
and
3 deletions
| @@ -43,3 +43,9 @@ export { ErrorToastUtils } from './src/main/ets/utils/ErrorToastUtils' | @@ -43,3 +43,9 @@ export { ErrorToastUtils } from './src/main/ets/utils/ErrorToastUtils' | ||
| 43 | export { EmitterUtils } from './src/main/ets/utils/EmitterUtils' | 43 | export { EmitterUtils } from './src/main/ets/utils/EmitterUtils' |
| 44 | 44 | ||
| 45 | export { EmitterEventId } from './src/main/ets/utils/EmitterEventId' | 45 | export { EmitterEventId } from './src/main/ets/utils/EmitterEventId' |
| 46 | + | ||
| 47 | +export { NetworkUtil } from './src/main/ets/utils/NetworkUtil' | ||
| 48 | + | ||
| 49 | +export { NetworkManager } from './src/main/ets/network/NetworkManager' | ||
| 50 | + | ||
| 51 | +export { NetworkType } from './src/main/ets/network/NetworkType' |
| 1 | +import connection from '@ohos.net.connection'; | ||
| 2 | +import { BusinessError } from '@ohos.base'; | ||
| 3 | +import { Logger } from '../utils/Logger'; | ||
| 4 | +import { EmitterUtils } from '../utils/EmitterUtils'; | ||
| 5 | +import { EmitterEventId } from '../utils/EmitterEventId'; | ||
| 6 | +import { NetworkType } from './NetworkType'; | ||
| 7 | + | ||
| 8 | +const TAG = 'NetworkManager' | ||
| 9 | + | ||
| 10 | +/** | ||
| 11 | + * 网络管理类 | ||
| 12 | + */ | ||
| 13 | +export class NetworkManager { | ||
| 14 | + private netCon: connection.NetConnection | null = null; | ||
| 15 | + private static instance: NetworkManager; | ||
| 16 | + private networkType: NetworkType = NetworkType.TYPE_UNKNOWN | ||
| 17 | + | ||
| 18 | + public static getInstance(): NetworkManager { | ||
| 19 | + if (!NetworkManager.instance) { | ||
| 20 | + NetworkManager.instance = new NetworkManager(); | ||
| 21 | + } | ||
| 22 | + return NetworkManager.instance; | ||
| 23 | + } | ||
| 24 | + | ||
| 25 | + public init() { | ||
| 26 | + // 初始化 | ||
| 27 | + if (this.netCon) { | ||
| 28 | + // 拦截重复初始化 | ||
| 29 | + return | ||
| 30 | + } | ||
| 31 | + this.networkMonitorRegister() | ||
| 32 | + } | ||
| 33 | + | ||
| 34 | + public release() { | ||
| 35 | + this.networkMonitorUnregister() | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + private constructor() { | ||
| 39 | + } | ||
| 40 | + | ||
| 41 | + private networkMonitorRegister() { | ||
| 42 | + this.netCon = connection.createNetConnection(); | ||
| 43 | + this.netCon.register((error) => { | ||
| 44 | + if (error) { | ||
| 45 | + Logger.error(TAG, 'register error:' + error.message); | ||
| 46 | + return; | ||
| 47 | + } | ||
| 48 | + Logger.info(TAG, 'register success'); | ||
| 49 | + }) | ||
| 50 | + this.netCon.on('netAvailable', (data: connection.NetHandle) => { | ||
| 51 | + Logger.info(TAG, 'netAvailable, data is: ' + JSON.stringify(data)) | ||
| 52 | + }) | ||
| 53 | + this.netCon.on('netBlockStatusChange', (data: connection.NetBlockStatusInfo) => { | ||
| 54 | + Logger.info(TAG, 'netBlockStatusChange, data is: ' + JSON.stringify(data)) | ||
| 55 | + // TODO 网络阻塞,是否创建新的网络、提示 | ||
| 56 | + }) | ||
| 57 | + this.netCon.on('netCapabilitiesChange', (data: connection.NetCapabilityInfo) => { | ||
| 58 | + Logger.info(TAG, 'netCapabilitiesChange, data is: ' + JSON.stringify(data)) | ||
| 59 | + this.parseData(data) | ||
| 60 | + // 可能多次通知 | ||
| 61 | + EmitterUtils.sendEvent(EmitterEventId.NETWORK_CONNECTED, JSON.stringify(this.networkType)) | ||
| 62 | + }) | ||
| 63 | + this.netCon.on('netConnectionPropertiesChange', (data: connection.NetConnectionPropertyInfo) => { | ||
| 64 | + Logger.info(TAG, 'netConnectionPropertiesChange, data is: ' + JSON.stringify(data)) | ||
| 65 | + }) | ||
| 66 | + | ||
| 67 | + this.netCon.on('netUnavailable', ((data: void) => { | ||
| 68 | + Logger.info(TAG, 'netUnavailable, data is: ' + JSON.stringify(data)) | ||
| 69 | + })); | ||
| 70 | + this.netCon.on('netLost', ((data: connection.NetHandle) => { | ||
| 71 | + Logger.info(TAG, 'netLost, data is: ' + JSON.stringify(data)) | ||
| 72 | + // TODO 断网 | ||
| 73 | + EmitterUtils.sendEvent(EmitterEventId.NETWORK_DISCONNECTED) | ||
| 74 | + this.networkType = NetworkType.TYPE_NONE | ||
| 75 | + })) | ||
| 76 | + } | ||
| 77 | + | ||
| 78 | + private networkMonitorUnregister() { | ||
| 79 | + if (this.netCon) { | ||
| 80 | + this.netCon.unregister((error: BusinessError) => { | ||
| 81 | + if (error) { | ||
| 82 | + Logger.error(TAG, 'unregister error:' + error.message); | ||
| 83 | + return; | ||
| 84 | + } | ||
| 85 | + Logger.info(TAG, 'unregister success'); | ||
| 86 | + }) | ||
| 87 | + } | ||
| 88 | + } | ||
| 89 | + | ||
| 90 | + /** | ||
| 91 | + * @deprecated | ||
| 92 | + */ | ||
| 93 | + private getNetworkMessage(netHandle: connection.NetHandle) { | ||
| 94 | + connection.getNetCapabilities(netHandle, (error, netCap) => { | ||
| 95 | + if (error) { | ||
| 96 | + Logger.error(TAG, 'getNetCapabilities error:' + error.message); | ||
| 97 | + return; | ||
| 98 | + } | ||
| 99 | + let netType = netCap.bearerTypes; | ||
| 100 | + this.reset(netType) | ||
| 101 | + }) | ||
| 102 | + } | ||
| 103 | + | ||
| 104 | + private reset(netType: Array<connection.NetBearType>) { | ||
| 105 | + if (netType == null) { | ||
| 106 | + return | ||
| 107 | + } | ||
| 108 | + for (let i = 0; i < netType.length; i++) { | ||
| 109 | + if (netType[i] === 0) { | ||
| 110 | + // 蜂窝网 | ||
| 111 | + this.networkType = NetworkType.TYPE_CELLULAR | ||
| 112 | + } else if (netType[i] === 1) { | ||
| 113 | + // Wi-Fi网络 | ||
| 114 | + this.networkType = NetworkType.TYPE_WIFI | ||
| 115 | + } else { | ||
| 116 | + // 以太网网络 | ||
| 117 | + this.networkType = NetworkType.TYPE_ETHERNET | ||
| 118 | + } | ||
| 119 | + } | ||
| 120 | + } | ||
| 121 | + | ||
| 122 | + /** | ||
| 123 | + * 获取网络类型,网络监听网络变化刷新类型,这里优先返回变量 | ||
| 124 | + */ | ||
| 125 | + public getNetType(): NetworkType { | ||
| 126 | + if (this.networkType != NetworkType.TYPE_UNKNOWN) { | ||
| 127 | + return this.networkType | ||
| 128 | + } | ||
| 129 | + return this.getNetTypeSync() | ||
| 130 | + } | ||
| 131 | + | ||
| 132 | + /** | ||
| 133 | + * 同步获取网络类型,耗时 | ||
| 134 | + */ | ||
| 135 | + public getNetTypeSync(): NetworkType { | ||
| 136 | + let netHandle = connection.getDefaultNetSync(); | ||
| 137 | + let netCapabilities = connection.getNetCapabilitiesSync(netHandle) | ||
| 138 | + this.reset(netCapabilities.bearerTypes) | ||
| 139 | + return this.networkType; | ||
| 140 | + } | ||
| 141 | + | ||
| 142 | + private parseData(data: connection.NetCapabilityInfo) { | ||
| 143 | + // 解析网络信息 | ||
| 144 | + this.reset(data.netCap.bearerTypes) | ||
| 145 | + } | ||
| 146 | +} |
| @@ -3,6 +3,10 @@ | @@ -3,6 +3,10 @@ | ||
| 3 | */ | 3 | */ |
| 4 | export enum EmitterEventId { | 4 | export enum EmitterEventId { |
| 5 | // 通知登出,事件id | 5 | // 通知登出,事件id |
| 6 | - FORCE_USER_LOGIN_OUT = 1 | 6 | + FORCE_USER_LOGIN_OUT = 1, |
| 7 | + // 网络连接成功,事件id | ||
| 8 | + NETWORK_CONNECTED = 2, | ||
| 9 | + // 网络断开,事件id | ||
| 10 | + NETWORK_DISCONNECTED = 3, | ||
| 7 | } | 11 | } |
| 8 | 12 |
| 1 | +import { NetworkManager } from '../network/NetworkManager' | ||
| 2 | +import { NetworkType } from '../network/NetworkType' | ||
| 3 | + | ||
| 4 | +/** | ||
| 5 | + * 网络相关工具类 | ||
| 6 | + * 要实时监听网络,需要添加注册函数{例:src/main/ets/entryability/EntryAbility.ets:32} | ||
| 7 | + */ | ||
| 8 | +export class NetworkUtil { | ||
| 9 | + /** | ||
| 10 | + * 网络环境:0:无网 1:WiFi 2:2G 3:3G 4:4G 5:5G,暂时只识别出蜂窝网 | ||
| 11 | + * 扩展6-以太网 | ||
| 12 | + */ | ||
| 13 | + static TYPE_NONE = '0' | ||
| 14 | + static TYPE_WIFI = '1' | ||
| 15 | + static TYPE_CELLULAR = '5' | ||
| 16 | + // TODO 以太网,手机不涉及 | ||
| 17 | + static TYPE_ETHERNET = '6' | ||
| 18 | + | ||
| 19 | + static getNetworkType(): string { | ||
| 20 | + let type = NetworkManager.getInstance().getNetType() | ||
| 21 | + return NetworkUtil.parseType(type) | ||
| 22 | + } | ||
| 23 | + | ||
| 24 | + static parseType(type: NetworkType): string { | ||
| 25 | + switch (type) { | ||
| 26 | + case NetworkType.TYPE_UNKNOWN: | ||
| 27 | + case NetworkType.TYPE_NONE: | ||
| 28 | + return NetworkUtil.TYPE_NONE; | ||
| 29 | + case NetworkType.TYPE_WIFI: | ||
| 30 | + return NetworkUtil.TYPE_WIFI; | ||
| 31 | + case NetworkType.TYPE_CELLULAR: | ||
| 32 | + return NetworkUtil.TYPE_CELLULAR; | ||
| 33 | + case NetworkType.TYPE_ETHERNET: | ||
| 34 | + return NetworkUtil.TYPE_ETHERNET; | ||
| 35 | + default: | ||
| 36 | + return NetworkUtil.TYPE_NONE; | ||
| 37 | + } | ||
| 38 | + } | ||
| 39 | +} |
| @@ -4,7 +4,16 @@ import UIAbility from '@ohos.app.ability.UIAbility'; | @@ -4,7 +4,16 @@ import UIAbility from '@ohos.app.ability.UIAbility'; | ||
| 4 | import Want from '@ohos.app.ability.Want'; | 4 | import Want from '@ohos.app.ability.Want'; |
| 5 | import window from '@ohos.window'; | 5 | import window from '@ohos.window'; |
| 6 | import { registerRouter } from 'wdRouter'; | 6 | import { registerRouter } from 'wdRouter'; |
| 7 | -import { SPHelper, StringUtils, WindowModel } from 'wdKit'; | 7 | +import { |
| 8 | + EmitterEventId, | ||
| 9 | + EmitterUtils, | ||
| 10 | + Logger, | ||
| 11 | + NetworkManager, | ||
| 12 | + NetworkType, | ||
| 13 | + SPHelper, | ||
| 14 | + StringUtils, | ||
| 15 | + WindowModel | ||
| 16 | +} from 'wdKit'; | ||
| 8 | import { HttpUrlUtils, WDHttp } from 'wdNetwork'; | 17 | import { HttpUrlUtils, WDHttp } from 'wdNetwork'; |
| 9 | 18 | ||
| 10 | export default class EntryAbility extends UIAbility { | 19 | export default class EntryAbility extends UIAbility { |
| @@ -12,15 +21,30 @@ export default class EntryAbility extends UIAbility { | @@ -12,15 +21,30 @@ export default class EntryAbility extends UIAbility { | ||
| 12 | SPHelper.init(this.context); | 21 | SPHelper.init(this.context); |
| 13 | hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); | 22 | hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); |
| 14 | registerRouter(); | 23 | registerRouter(); |
| 24 | + NetworkManager.getInstance().init() | ||
| 15 | WDHttp.initHttpHeader() | 25 | WDHttp.initHttpHeader() |
| 16 | const spHostUrl = SPHelper.default.getSync('hostUrl', '') as string | 26 | const spHostUrl = SPHelper.default.getSync('hostUrl', '') as string |
| 17 | if (StringUtils.isNotEmpty(spHostUrl)) { | 27 | if (StringUtils.isNotEmpty(spHostUrl)) { |
| 18 | HttpUrlUtils.hostUrl = spHostUrl | 28 | HttpUrlUtils.hostUrl = spHostUrl |
| 19 | } | 29 | } |
| 30 | + | ||
| 31 | + // 注册监听网络连接 | ||
| 32 | + EmitterUtils.receiveEvent(EmitterEventId.NETWORK_CONNECTED, ((str?: string) => { | ||
| 33 | + let type: NetworkType | null = null | ||
| 34 | + if (str) { | ||
| 35 | + type = JSON.parse(str) as NetworkType | ||
| 36 | + } | ||
| 37 | + Logger.info('network connected: ' + type?.toString()) | ||
| 38 | + })) | ||
| 39 | + // 注册监听网络断开 | ||
| 40 | + EmitterUtils.receiveEvent(EmitterEventId.NETWORK_DISCONNECTED, (() => { | ||
| 41 | + Logger.info('network disconnected') | ||
| 42 | + })) | ||
| 20 | } | 43 | } |
| 21 | 44 | ||
| 22 | onDestroy(): void { | 45 | onDestroy(): void { |
| 23 | hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); | 46 | hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); |
| 47 | + NetworkManager.getInstance().release() | ||
| 24 | } | 48 | } |
| 25 | 49 | ||
| 26 | onWindowStageCreate(windowStage: window.WindowStage): void { | 50 | onWindowStageCreate(windowStage: window.WindowStage): void { |
-
Please register or login to post a comment