Skip to content

Commit

Permalink
网络环境检测并发优化,一卡通部分页面暗色主题修复,生物验证解锁微调。
Browse files Browse the repository at this point in the history
  • Loading branch information
bjutapp committed Dec 15, 2024
1 parent 0f6ecdf commit b2d1b3c
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 60 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* [x] 桌面APP长按快捷菜单
* [x] 图书馆相关功能
* [x] 校内WiFi一键登录
* [x] 生物验证解锁
* [ ] 校内邮箱浏览
* [ ] 办事大厅
* [ ] 重要消息提示
Expand All @@ -30,7 +31,7 @@

## 应用兼容性说明

本app完全支持armeabi-v7a, arm64-v8a, x86和x86_64安卓架构,理论上较好支持Android 7.1+版本 (Android API level 25)。
本app完全支持armeabi-v7a, arm64-v8a, x86和x86_64安卓架构,理论上较好支持Android 7.1+版本 (Android API level 25),生物验证仅支持Android 9.0+版本

## 应用内部分截图

Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ android {
applicationId = "com.hlwdy.bjut"
minSdk = 24
targetSdk = 34
versionCode = 2
versionName = "1.1.0"
versionCode = 3
versionName = "1.1.1"
/*
ndk {
abiFilters += listOf("armeabi-v7a", "arm64-v8a","x86","x86_64")
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/hlwdy/bjut/BiometricHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class BiometricHelper(private val activity: AppCompatActivity) {

override fun onAuthenticationFailed() {
super.onAuthenticationFailed()
Toast.makeText(activity, "认证尝试失败", Toast.LENGTH_SHORT).show()
Toast.makeText(activity, "尝试验证失败", Toast.LENGTH_SHORT).show()
//pendingAction = null
//允许继续尝试
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/hlwdy/bjut/RouterActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class RouterActivity : AppCompatActivity() {
.commit()

}
})
},{finish()})

}
}
2 changes: 1 addition & 1 deletion app/src/main/java/com/hlwdy/bjut/ui/LoginActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class LoginActivity : BaseActivity() {
if(account_session_util(context).isLoggedIn()){
BiometricHelper(context).authenticate({//跳转生物验证
jumpToMain()
})
},{finish()})
}
else {
rootLayout.removeView(overlayView)
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/hlwdy/bjut/ui/card/CardFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class EnhancedCachingWebViewClient(private val context: Context,private val frag
isNeedNewID=false
}
// 注入CSS
val css = "@media (prefers-color-scheme: dark) { body,uni-page-body,.bdbg1,.code-bg,.news-w-bg,.bdbg,.ticket-nav,.tap2{ background: #121212 !important; }uni-navigator,.menu-list,.news-w,.payment-method,.condition, .condition-checkbox,.amtbutton,.v-tabs__container,.v-tabs__container-item,.tr,.nowcode,.tap-box,.uni-picker-select,.uni-picker-header,.newsCol,.text-w,.txt,.itemArea_li,#popup_content{background:#313131 !important;color:white !important;}uni-text{color:white !important}.code-w{box-shadow:none !important;}.news-w,.newsli{border-bottom:none !important;}.bdbg1,uni-view{color:white !important}.bdt{border-top: 1px solid #555555 !important;}.per-details,.bor-bottom{border-bottom: 1px solid #555555 !important;}}"
val css = "@media (prefers-color-scheme: dark) { body,uni-page-body,.bdbg1,.code-bg,.news-w-bg,.bdbg,.ticket-nav,.tap2,.alert-page>uni-view,.pages_water-water-virtualcard-mywaterorder-mywaterorder uni-view{ background: #121212 !important; }uni-navigator,.menu-list,.news-w,.payment-method,.condition, .condition-checkbox,.amtbutton,.v-tabs__container,.v-tabs__container-item,.tr,.nowcode,.tap-box,.uni-picker-select,.uni-picker-header,.newsCol,.text-w,.txt,.itemArea_li,#popup_content{background:#313131 !important;color:white !important;}uni-text{color:white !important}.code-w{box-shadow:none !important;}.news-w,.newsli{border-bottom:none !important;}.bdbg1,uni-view{color:white !important}.bdt{border-top: 1px solid #555555 !important;}.per-details,.bor-bottom{border-bottom: 1px solid #555555 !important;}}"
view.evaluateJavascript(
"""
(function() {
Expand Down
131 changes: 78 additions & 53 deletions app/src/main/java/com/hlwdy/bjut/ui/network/NetworkFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ import com.hlwdy.bjut.databinding.FragmentNetworkBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.async
import kotlinx.coroutines.selects.select
import java.net.InetAddress
import org.json.JSONObject
import com.hlwdy.bjut.network_account_util
Expand All @@ -34,13 +37,13 @@ import java.util.Locale
import kotlin.math.ln
import kotlin.math.pow

fun isInternalIp(ip: String): Boolean {
fun isInternalIp(ip: String?): Boolean {
val privateIpPatterns = listOf(
"^10\\..*".toRegex(),
"^172\\.(1[6-9]|2[0-9]|3[0-1])\\..*".toRegex(),
"^192\\.168\\..*".toRegex()
)
return privateIpPatterns.any { it.matches(ip) }
return ip!=null&&privateIpPatterns.any { it.matches(ip) }
}
fun checkWebsiteAccessibility(urlString: String): Boolean {
return try {
Expand Down Expand Up @@ -116,23 +119,42 @@ fun postUrl(urlString: String, params: Set<String>): String? {
return null
}
}
private fun checkBjutLocation():String{
//测试是否为宿舍光猫
val bres= checkWebsiteAccessibility("http://10.21.221.98")
if(bres){
return "dorm"
}
//测试是否为bjut_wifi
val wres= checkWebsiteAccessibility("http://wlgn.bjut.edu.cn")
if(wres){
return "wifi"
}
//测试是否为其他情况(例如有线网)
val res= checkWebsiteAccessibility("http://172.30.201.2")
if(res){
return "bjut"
suspend fun checkBjutLocation():String{
return coroutineScope {
//测试是否为宿舍光猫
val bPingDeferred = async(Dispatchers.IO){checkWebsiteAccessibility("http://10.21.221.98")}
//测试是否为bjut_wifi
val wPingDeferred = async(Dispatchers.IO){checkWebsiteAccessibility("http://wlgn.bjut.edu.cn")}
//测试是否为其他情况(例如有线网)
val lPingDeferred = async(Dispatchers.IO){checkWebsiteAccessibility("http://172.30.201.2")}

val bwPingRes=select {
bPingDeferred.onAwait{
if(it){
"dorm"
}else if(wPingDeferred.await()){
"wifi"
}else{""}
}
wPingDeferred.onAwait{
if(it){
"wifi"
}else if(bPingDeferred.await()){
"dorm"
}else{""}
}
}
if(bwPingRes!=""){
bwPingRes
}else {
val lres = lPingDeferred.await()
if (lres) {
"bjut"
}else {
"unknownType"
}
}
}
return "unknownType"
}
fun checkLoginStatusBjut():Pair<Boolean,Boolean> {
val urlString="https://lgn.bjut.edu.cn"
Expand Down Expand Up @@ -262,6 +284,18 @@ fun formatFlowSize(sizeInBytes: Long): String {
val pre = "KMGTPE"[exp - 1] + "i"
return String.format(Locale("zh", "CN"),"%.2f %sB", sizeInBytes / unit.toDouble().pow(exp.toDouble()), pre)
}
fun checkInBjut():Int{
return try {
val addresses = InetAddress.getAllByName("www.bjut.edu.cn")
if(addresses.any { isInternalIp(it.hostAddress) }) {
1
}else {
0
}
}catch (e: Exception) {
-1
}
}
class NetworkFragment : BaseFragment() {

private var _binding: FragmentNetworkBinding? = null
Expand Down Expand Up @@ -317,49 +351,40 @@ class NetworkFragment : BaseFragment() {
}


private fun checkNetworkStates(networkAccount: network_account_util,showToast: Boolean) {
private fun checkNetworkStates(networkAccount: network_account_util,showToastFlag: Boolean) {
showLoading()
lifecycleScope.launch {
val success = withContext(Dispatchers.IO) {
try {
val addresses = InetAddress.getAllByName("www.bjut.edu.cn")
//有一个是内网地址即可
if (addresses.any { isInternalIp(it.hostAddress!!) }) {
networkStates=checkBjutLocation()
val pair=checkLoginStatus(networkStates)
networkLoginipv4States=pair.first
networkLoginipv6States=pair.second
if(networkLoginipv4States) {
networkFlow=checkNetworkFlow()
}else{
networkFlow=-1
if (lastRequireLoginTime != 0L) {
if (System.currentTimeMillis() - lastRequireLoginTime < 5000) {
loginNetwork(networkAccount)
}
lastRequireLoginTime = 0L
}

val inBjut = withContext(Dispatchers.IO) { checkInBjut() }
if (inBjut == 1) {
networkStates=withContext(Dispatchers.IO) { checkBjutLocation() }
val pair=withContext(Dispatchers.IO) { checkLoginStatus(networkStates) }
networkLoginipv4States=pair.first
networkLoginipv6States=pair.second
if(networkLoginipv4States) {
networkFlow=withContext(Dispatchers.IO) { checkNetworkFlow() }
}else{
networkFlow=-1
if (lastRequireLoginTime != 0L) {
if (System.currentTimeMillis() - lastRequireLoginTime < 5000) {
loginNetwork(networkAccount)
}
}else {
networkStates = "outBJUT"
networkLoginipv4States=false
networkLoginipv6States=false
networkFlow=-1
lastRequireLoginTime = 0L
}
true
} catch (e: Exception) {
false

}
}
if(!success) {
} else if (inBjut == 0) {
networkStates = "outBJUT"
networkLoginipv4States = false
networkLoginipv6States = false
networkFlow = -1
} else if (inBjut == -1) {
networkStates = "noNetwork"
networkLoginipv4States=false
networkLoginipv6States=false
networkFlow=-1
networkLoginipv4States = false
networkLoginipv6States = false
networkFlow = -1
}
hideLoading()
if(showToast) {
if(showToastFlag) {
showToast("刷新完成")
}
updateShow()
Expand Down

0 comments on commit b2d1b3c

Please sign in to comment.