desc:4G问题

main
xiaowusky 1 year ago
parent 798787d70a
commit ef34bfe79e

@ -85,9 +85,9 @@ class HeartbeatService : IntentService("HeartbeatService") {
}
}
fun isOnline(): Boolean {
private fun isOnline(): Boolean {
return try {
val timeoutMs = 1500
val timeoutMs = 2500
val sock = Socket()
val sockaddr: SocketAddress = InetSocketAddress("8.8.8.8", 53)
sock.connect(sockaddr, timeoutMs)

@ -61,8 +61,11 @@ class CommonTopBar : LinearLayout {
private val batteryCallback = object : BatteryHelper.OnBatteryLevelCallback {
override fun onLevel(level: Int) {
Log.i(this@CommonTopBar.javaClass.name, "batteryCallback onLevel = $level")
mBinding?.battery?.setImageLevel(level)
mBinding?.batteryTxt?.text = "${level.toString()}%"
mBinding?.root?.post {
mBinding?.battery?.setImageLevel(level)
mBinding?.batteryTxt?.text = "${level.toString()}%"
dealForNet4()
}
}
}
@ -73,18 +76,24 @@ class CommonTopBar : LinearLayout {
private val wifiCallback = object : WifiHelper.OnWifiLevelCallback {
override fun onEnable(enable: Boolean) {
Log.i(this@CommonTopBar.javaClass.name, "wifiCallback onEnable = $enable")
if (!enable) {
mBinding?.wifi?.visibility = GONE
} else {
mBinding?.root?.post {
if (!enable || isVirtuallyNet4()) {
mBinding?.wifi?.visibility = GONE
} else {
// mBinding?.wifi?.visibility = VISIBLE
}
dealForNet4()
}
}
override fun onLevel(level: Int) {
Log.i(this@CommonTopBar.javaClass.name, "wifiCallback onLevel = $level")
mBinding?.wifi?.visibility = if (level > 0) VISIBLE else GONE
val showLevel = (level + 1) * 20
mBinding?.wifi?.setImageLevel(showLevel)
mBinding?.root?.post {
mBinding?.wifi?.visibility = if (level > 0 && !isVirtuallyNet4()) VISIBLE else GONE
val showLevel = (level + 1) * 20
mBinding?.wifi?.setImageLevel(showLevel)
dealForNet4()
}
}
}
@ -106,6 +115,7 @@ class CommonTopBar : LinearLayout {
mBinding?.fourG?.visibility = GONE
}
}
dealForNet4()
}
}
@ -118,10 +128,19 @@ class CommonTopBar : LinearLayout {
showLevel = 100
}
mBinding?.fourG?.setImageLevel(showLevel)
dealForNet4()
}
}
}
private fun dealForNet4() {
if (!isVirtuallyNet4()) {
mBinding?.fourG?.visibility = GONE
} else {
mBinding?.fourG?.visibility = VISIBLE
}
}
private fun watchSim() {
SimHelper.addCallBack(simCallback)
}
@ -193,4 +212,8 @@ class CommonTopBar : LinearLayout {
fun getBackArea(): View? {
return mBinding?.backArea
}
fun isVirtuallyNet4(): Boolean {
return WifiHelper.getConnectedWifiSSID()?.contains("4G-UFI") ?: false;
}
}

@ -110,24 +110,36 @@ object SimHelper {
}
private var isNetOK = false
private var counter = 0
fun setNetStatus(status: Boolean) {
LogUtils.w("setNetStatus $status")
isNetOK = status
val netSpeed = getNetSpeed()
callbacks.forEach {
it.onEnable(status)
it.onLevel(netSpeed)
// UI刷新要慢
if (counter % 6 == 0) {
counter = 0
val netSpeed = getNetSpeed()
callbacks.forEach {
it.onEnable(status)
mSimState = if (status) {
TelephonyManager.SIM_STATE_READY
} else {
-1
}
it.onLevel(netSpeed)
mRssi = netSpeed
}
}
counter++
}
fun isNetOk(): Boolean {
return isNetOK
}
var lastTime = 0L
var lastTxBytes = 0L;
var lastRxBytes = 0L;
private var lastTime = 0L
private var lastTxBytes = 0L
private var lastRxBytes = 0L
private fun getNetSpeed(): Int {
val totalTxBytes = TrafficStats.getTotalTxBytes()
val totalRxBytes = TrafficStats.getTotalRxBytes()
@ -137,13 +149,16 @@ object SimHelper {
val rxOffset = totalRxBytes - lastRxBytes
val txOffset = totalTxBytes - lastTxBytes
Log.i(
"setNetStatus",
"txbyte = $txOffset rxbyte = $rxOffset timeOffset = $timeOffset"
"setNetStatus", "txbyte = $txOffset rxbyte = $rxOffset timeOffset = $timeOffset"
)
var ret = ((rxOffset + txOffset) / timeOffset).toInt()
if (lastTime == 0L) {
ret = 2
}
lastTime = currentTimeMillis
lastRxBytes = totalRxBytes
lastTxBytes = totalTxBytes
return ((rxOffset + txOffset) / timeOffset).toInt()
return ret
}
}

@ -9,6 +9,7 @@ import android.net.ConnectivityManager
import android.net.NetworkInfo
import android.net.wifi.WifiManager
import android.util.Log
import com.common.commonlib.CommonApplication
object WifiHelper {
@ -30,13 +31,14 @@ object WifiHelper {
callbacks.forEach {
it.onLevel(mRssi)
}
}else if (intent?.action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)){
val networkInfo = intent?.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO) as NetworkInfo?
} else if (intent?.action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
val networkInfo =
intent?.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO) as NetworkInfo?
networkInfo?.let {
if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
if (networkInfo.detailedState == NetworkInfo.DetailedState.DISCONNECTED){
if (networkInfo.detailedState == NetworkInfo.DetailedState.DISCONNECTED) {
mWifiState = -1
}else if (networkInfo.detailedState == NetworkInfo.DetailedState.CONNECTED){
} else if (networkInfo.detailedState == NetworkInfo.DetailedState.CONNECTED) {
mWifiState = WifiManager.WIFI_STATE_ENABLED
}
callbacks.forEach {
@ -92,4 +94,10 @@ object WifiHelper {
fun onEnable(enable: Boolean)
fun onLevel(level: Int)
}
fun getConnectedWifiSSID(): String? {
val wifiManager =
CommonApplication.getContext()?.getSystemService(WIFI_SERVICE) as WifiManager?
return wifiManager?.connectionInfo?.ssid
}
}

@ -2,7 +2,6 @@ package org.easydarwin
import android.graphics.Bitmap
import android.graphics.Color
import android.text.TextUtils
import android.util.Log
import com.common.commonlib.db.entity.Gas
import com.common.commonlib.utils.forShowStr
@ -22,15 +21,6 @@ object TxtOverlay {
// 提示变化的监听
private var mTipChangeListener: (() -> Unit)? = null;
// 上一次展示的提示文字
private var mLastShowTip = ""
// 待展示的提示文字
private var mToDoShowTip = ""
// 上一次展示的时间
private var lastTipUpdateTime: Long = 0
private const val bmpWidth: Int = 600
private const val bmpHeight: Int = 330
@ -42,40 +32,26 @@ object TxtOverlay {
// 时间格式化字符串
private val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
private fun buildOverlayBitmap(currentTimeMillis: Long) {
if (TextUtils.isEmpty(mToDoShowTip)) {
return
}
// 限制获取bitmap的频率保证性能
if (TextUtils.isEmpty(mLastShowTip) || mToDoShowTip != mLastShowTip || currentTimeMillis - lastTipUpdateTime > 1000) {
// 记录更新时间和上一次的文字
lastTipUpdateTime = currentTimeMillis
mLastShowTip = mToDoShowTip
val timeFormat = dateFormat.format(lastTipUpdateTime)
// 文字转bitmap
YUVUtils.generateBitmap(
bmp,
"$timeFormat@$mToDoShowTip",
42,
Color.WHITE
)
yuv = YUVUtils.getYUVByBitmap(bmp)
mTipChangeListener?.invoke()
}
private fun buildOverlayBitmap(currentTimeMillis: Long, tipStr: String) {
// 记录更新时间和上一次的文字
val timeFormat = dateFormat.format(currentTimeMillis)
// 文字转bitmap
YUVUtils.generateBitmap(
bmp,
"$timeFormat@$tipStr",
42,
Color.WHITE
)
yuv = YUVUtils.getYUVByBitmap(bmp)
mTipChangeListener?.invoke()
}
fun getOverlayBitmap(): Bitmap? {
if (!TextUtils.isEmpty(mToDoShowTip)) {
return bmp
}
return null
return bmp
}
fun getOverlayYuv(): ByteArray? {
if (!TextUtils.isEmpty(mToDoShowTip)) {
return yuv
}
return null
return yuv
}
fun setTipChangeListener(onChange: () -> Unit) {
@ -86,6 +62,7 @@ object TxtOverlay {
private var looping = false
private val gasMap = ConcurrentHashMap<String, Gas>()
// 外部调用,设置待显示水印文字
fun startShow(gas: Gas) {
gasMap[gas.gasName] = gas
@ -105,9 +82,12 @@ object TxtOverlay {
.append("@")
}
}
mToDoShowTip = overlayBuilder.toString()
Log.i("cyy", "time = ${dateFormat.format(currentTimeMillis)} mToDoShowTip = $mToDoShowTip")
buildOverlayBitmap(currentTimeMillis)
val tipStr = overlayBuilder.toString()
Log.i(
"cyy",
"time = ${dateFormat.format(currentTimeMillis)} mToDoShowTip = $tipStr"
)
buildOverlayBitmap(currentTimeMillis, tipStr)
delay(1000)
}
}

Loading…
Cancel
Save