-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworkMonitor.kt
More file actions
119 lines (100 loc) · 4.52 KB
/
Copy pathNetworkMonitor.kt
File metadata and controls
119 lines (100 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
* Copyright 2026 Mustafa Yasar Kar
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.bandmapper
import android.content.Context
import android.telephony.*
import cz.mroczis.netmonster.core.factory.NetMonsterFactory
import cz.mroczis.netmonster.core.model.cell.ICell
import cz.mroczis.netmonster.core.model.connection.PrimaryConnection
import cz.mroczis.netmonster.core.model.connection.SecondaryConnection
import cz.mroczis.netmonster.core.model.cell.CellNr
import cz.mroczis.netmonster.core.model.cell.CellLte
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
/**
* Şebeke bilgilerini ve 5G bandını tespit eden sınıf.
* NetMonster-core kütüphanesini kullanarak profesyonel tespit yapar.
*/
class NetworkMonitor(private val context: Context) {
private val netMonster = NetMonsterFactory.get(context)
private val subscriptionId = SubscriptionManager.DEFAULT_SUBSCRIPTION_ID
// Bağlı olunan bandın bilgisini tutan akış
private val _currentBand = MutableStateFlow<BandInfo>(BandInfo.Unknown)
val currentBand: StateFlow<BandInfo> = _currentBand
sealed class BandInfo {
object Unknown : BandInfo()
data class NR(val bandIndex: Int, val isSA: Boolean) : BandInfo()
data class LTE(val pci: Int) : BandInfo()
}
fun startMonitoring() {
// Periyodik güncelleme MainActivity'de yapılıyor
}
fun stopMonitoring() {
// Temizlik işlemleri
}
private fun processCells(cells: List<ICell>) {
// 1. Şebeke Tipini Al
val networkType = netMonster.getNetworkType(subscriptionId)
val networkTypeStr = networkType.toString()
LogManager.log("Network Type: $networkTypeStr, Cells: ${cells.size}")
// 2. 5G Hücresi Var mı? (Hem SA hem NSA için en güvenilir yol)
// SecondaryConnection NSA modunda veri taşıyan 5G hücresini temsil eder
val nrCell = cells.filterIsInstance<CellNr>().firstOrNull {
it.connectionStatus is PrimaryConnection || it.connectionStatus is SecondaryConnection
}
if (nrCell != null) {
val band = nrCell.band?.number ?: 0
// SA/NSA tespiti için en garantili yöntem:
// Eğer şebeke tipi NrSa değilse veya içinde "Nsa" geçiyorsa kesinlikle NSA'dir.
val isSA = networkType is cz.mroczis.netmonster.core.model.network.NetworkType.NrSa &&
!networkTypeStr.contains("Nsa", ignoreCase = true)
LogManager.log("NR Cell Found: Band $band, isSA: $isSA")
_currentBand.value = BandInfo.NR(band, isSA)
return
}
// 3. Eğer hücre listesinde NR yoksa ama şebeke tipi 5G diyorsa (Bazen hücre detayları gelmez)
if (networkTypeStr.contains("Nsa", ignoreCase = true)) {
LogManager.log("NSA Detected via NetworkType")
_currentBand.value = BandInfo.NR(0, false) // Band 0 = Bilinmiyor/Tespit Edilemedi
return
}
if (networkTypeStr.contains("Sa", ignoreCase = true)) {
LogManager.log("SA Detected via NetworkType")
_currentBand.value = BandInfo.NR(0, true)
return
}
// 4. Standart LTE Kontrolü
val lteCell = cells.filterIsInstance<CellLte>().firstOrNull { it.connectionStatus is PrimaryConnection }
if (lteCell != null) {
LogManager.log("LTE Cell Found: PCI ${lteCell.pci}")
_currentBand.value = BandInfo.LTE(lteCell.pci ?: 0)
return
}
LogManager.log("No recognized cells found")
_currentBand.value = BandInfo.Unknown
}
fun updateNetworkInfo() {
try {
val cells = netMonster.getCells()
processCells(cells)
} catch (e: SecurityException) {
LogManager.log("SecurityException: ${e.message}")
} catch (e: Exception) {
LogManager.log("Exception: ${e.message}")
_currentBand.value = BandInfo.Unknown
}
}
}