From e81ce6af3d97a51e51f676f32e739d0f174323c7 Mon Sep 17 00:00:00 2001 From: bt Date: Thu, 7 May 2026 01:02:21 +0200 Subject: Add power graph --- app/build.gradle.kts | 1 + app/src/main/java/net/rctt/netmon/CellView.kt | 102 ++++++++++++++++------ app/src/main/java/net/rctt/netmon/MainActivity.kt | 64 +++++++------- app/src/main/res/layout/activity_main.xml | 30 +------ app/src/main/res/layout/cell_view.xml | 88 +++++++++++++++---- app/src/main/res/values-night/themes.xml | 4 +- 6 files changed, 186 insertions(+), 103 deletions(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 5daff8f..a5348d6 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -50,4 +50,5 @@ dependencies { testImplementation(libs.junit) androidTestImplementation(libs.androidx.junit) androidTestImplementation(libs.androidx.espresso.core) + implementation("com.androidplot:androidplot-core:1.5.11") } \ No newline at end of file diff --git a/app/src/main/java/net/rctt/netmon/CellView.kt b/app/src/main/java/net/rctt/netmon/CellView.kt index 3f00caf..d991d48 100644 --- a/app/src/main/java/net/rctt/netmon/CellView.kt +++ b/app/src/main/java/net/rctt/netmon/CellView.kt @@ -1,57 +1,109 @@ package net.rctt.netmon import android.content.Context -import android.telephony.CellIdentityNr +import android.graphics.Color import android.telephony.CellInfoGsm import android.telephony.CellInfoLte import android.telephony.CellInfoNr import android.telephony.CellInfoTdscdma import android.telephony.CellInfoWcdma +import android.util.Log import android.view.LayoutInflater import android.widget.TextView import androidx.constraintlayout.widget.ConstraintLayout - +import com.androidplot.xy.LineAndPointFormatter +import com.androidplot.xy.SimpleXYSeries +import com.androidplot.xy.XYPlot class CellView : ConstraintLayout{ - var type: TextView - var id: TextView - var power: TextView + var cellId: Number + var power: Int + var powerHistory: MutableList + + var typeView: TextView + var idView: TextView + var powerView: TextView + var powerChartView: XYPlot + var powerChartSeries: SimpleXYSeries + + constructor(ctx: Context, id: Number) : super(ctx) { + cellId = id + power = 0 - constructor(ctx: Context) : super(ctx) { LayoutInflater.from(context).inflate(R.layout.cell_view, this) - type = findViewById(R.id.type) - id = findViewById(R.id.id) - power = findViewById(R.id.power) + typeView = findViewById(R.id.type) + idView = findViewById(R.id.id) + powerView = findViewById(R.id.power) + powerChartView = findViewById(R.id.power_chart) + powerHistory = mutableListOf() + powerChartSeries= SimpleXYSeries( + powerHistory, + SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, + "Series" + ) + populatePowerChart() + + idView.text = cellId.toString() } fun set(cell: CellInfoGsm){ - type.text = "gsm" - id.text = cell.cellIdentity.cid.toString() - power.text = cell.cellSignalStrength.dbm.toString() + power = cell.cellSignalStrength.dbm + typeView.text = "GSM" + powerView.text = power.toString() } fun set(cell: CellInfoLte){ - type.text = "lte" - id.text= cell.cellIdentity.ci.toString() - power.text = cell.cellSignalStrength.dbm.toString() + power = cell.cellSignalStrength.dbm + typeView.text = "LTE" + powerView.text = power.toString() } fun set(cell: CellInfoNr){ - type.text = "nr" - var cellId = cell.cellIdentity as CellIdentityNr - id.text = cellId.nci.toString() - power.text = cell.cellSignalStrength.dbm.toString() + power = cell.cellSignalStrength.dbm + typeView.text = "NR" + powerView.text = power.toString() } fun set(cell: CellInfoTdscdma){ - type.text = "tdscdma" - id.text = cell.cellIdentity.cid.toString() - power.text = cell.cellSignalStrength.dbm.toString() + power = cell.cellSignalStrength.dbm + typeView.text = "TDSCDMA" + powerView.text = power.toString() } fun set(cell: CellInfoWcdma){ - type.text = "wcmda" - id .text= cell.cellIdentity.cid.toString() - power.text = cell.cellSignalStrength.dbm.toString() + power = cell.cellSignalStrength.dbm + typeView.text = "WCDMA" + powerView.text = power.toString() + } + + fun refresh() { + powerChartView.removeSeries(powerChartSeries) + + powerHistory.removeAt(1) + powerHistory.add(power) + + val seriesData = mutableListOf() + + for (p in powerHistory) { + seriesData.add(p) + } + + powerChartSeries = SimpleXYSeries( + seriesData, + SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, + "Series" + ) + + val series1Format = LineAndPointFormatter(Color.RED, Color.GREEN, Color.BLUE, null) + + Log.d("ASD", seriesData.toString()) + + powerChartView.addSeries(powerChartSeries, series1Format) + } + + fun populatePowerChart() { + repeat(20) { + powerHistory.add(-100) + } } } diff --git a/app/src/main/java/net/rctt/netmon/MainActivity.kt b/app/src/main/java/net/rctt/netmon/MainActivity.kt index 9b6c7ec..bc0f136 100644 --- a/app/src/main/java/net/rctt/netmon/MainActivity.kt +++ b/app/src/main/java/net/rctt/netmon/MainActivity.kt @@ -5,6 +5,7 @@ import android.annotation.SuppressLint import android.os.Bundle import android.os.Handler import android.os.Looper +import android.telephony.CellIdentityNr import android.telephony.CellInfo import android.telephony.CellInfoGsm import android.telephony.CellInfoLte @@ -25,10 +26,9 @@ import java.text.SimpleDateFormat import java.util.Date class MainActivity : AppCompatActivity() { - lateinit var statusView: LinearLayout - lateinit var cellsList: LinearLayout - + lateinit var cellsListView: LinearLayout lateinit var tel: TelephonyManager + lateinit var cellsList: HashMap override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -40,18 +40,8 @@ class MainActivity : AppCompatActivity() { insets } - statusView = findViewById(R.id.statusView) - cellsList = findViewById(R.id.cellsList) - - val pLoc = ContextCompat.checkSelfPermission( - applicationContext, - Manifest.permission.ACCESS_FINE_LOCATION - ) - if (pLoc == -1) { - log("Location permission required") - return - } - log("Ready") + cellsList = HashMap() + cellsListView = findViewById(R.id.cellsList) tel = getSystemService(TELEPHONY_SERVICE) as TelephonyManager @@ -71,7 +61,7 @@ class MainActivity : AppCompatActivity() { tel.requestCellInfoUpdate(mainExecutor, object : CellInfoCallback() { @RequiresPermission(Manifest.permission.ACCESS_FINE_LOCATION) override fun onCellInfo(cellList: List) { - cellsList.removeAllViews() + cellsListView.removeAllViews() for (cell in cellList) { if (cell == null) { @@ -83,26 +73,36 @@ class MainActivity : AppCompatActivity() { }) } - @SuppressLint("SimpleDateFormat", "SetTextI18n") - fun log(text: String) { - val sdf = SimpleDateFormat("hh:mm:ss") - val date = sdf.format(Date()) - val msg = TextView(this) - msg.text = "$date $text" - statusView.addView(msg) - } - @SuppressLint("SetTextI18n") fun addCellView(cell :CellInfo){ - val cellData = CellView(this) + val id = getCellId(cell) + var cellView = cellsList[id] + if (cellView == null) { + cellView = CellView(this, id) + cellsList[id] = cellView + } + when (cell) { - is CellInfoGsm -> cellData.set(cell) - is CellInfoLte -> cellData.set(cell) - is CellInfoNr -> cellData.set(cell) - is CellInfoTdscdma -> cellData.set(cell) - is CellInfoWcdma -> cellData.set(cell) + is CellInfoGsm -> cellView.set(cell) + is CellInfoLte -> cellView.set(cell) + is CellInfoNr -> cellView.set(cell) + is CellInfoTdscdma -> cellView.set(cell) + is CellInfoWcdma -> cellView.set(cell) } - cellsList.addView(cellData) + cellView.refresh() + cellsListView.addView(cellView) } } + +fun getCellId(cell: CellInfo) : Number { + when (cell) { + is CellInfoGsm -> return cell.cellIdentity.cid + is CellInfoLte -> return cell.cellIdentity.ci + is CellInfoNr -> return (cell.cellIdentity as CellIdentityNr).nci + is CellInfoTdscdma -> return cell.cellIdentity.cid + is CellInfoWcdma -> return cell.cellIdentity.cid + } + + return 0 +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index f3a3014..993e933 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -9,12 +9,13 @@ tools:context=".MainActivity"> + app:layout_constraintTop_toTopOf="parent"> + android:layout_height="wrap_content"> - - - - - - - - \ No newline at end of file diff --git a/app/src/main/res/layout/cell_view.xml b/app/src/main/res/layout/cell_view.xml index 4b58958..7361930 100644 --- a/app/src/main/res/layout/cell_view.xml +++ b/app/src/main/res/layout/cell_view.xml @@ -1,32 +1,86 @@ + android:layout_width="wrap_content" + android:layout_height="wrap_content" + tools:ignore="HardcodedText"> - + android:orientation="horizontal"> - + - + + + + + + + + + + + + + + + + + + + + + - \ No newline at end of file -- cgit v1.2.3