diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index daa6bef..ae47f34 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,23 +1,20 @@ - + - + + > + android:launchMode="singleTop" + > diff --git a/app/src/main/java/com/example/batterymanager_utility/DataCollectionService.kt b/app/src/main/java/com/example/batterymanager_utility/DataCollectionService.kt index 0643092..d03fc52 100644 --- a/app/src/main/java/com/example/batterymanager_utility/DataCollectionService.kt +++ b/app/src/main/java/com/example/batterymanager_utility/DataCollectionService.kt @@ -25,7 +25,7 @@ class DataCollectionService : Service() { val sampleRate: Int = intent!!.getIntExtra("sampleRate", 1000) Log.i(TAG, "onStartCommand: sampleRate => $sampleRate") val rawFields: String? = intent.getStringExtra("dataFields") - Log.i(TAG, "onStartCommand: rawFields => $rawFields") + Log.i(TAG, "onStartCommand: rawFields => Timestamp,$rawFields") dataFields = rawFields?.split(",") as ArrayList dataFields.add(0,"Timestamp") @@ -55,7 +55,8 @@ class DataCollectionService : Service() { val stats = collector.getData() Log.i(TAG, "stats => $stats") data.add(stats) - delay(sampleRate.toLong()) + if (sampleRate > 0) + delay(sampleRate.toLong()) } } @@ -70,6 +71,10 @@ class DataCollectionService : Service() { } } + override fun onBind(p0: Intent?): IBinder? { + return null + } + private fun createFile() : File { // Create a new file and write data to it. val file = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), "BatteryManager.csv") @@ -95,10 +100,6 @@ class DataCollectionService : Service() { } } - override fun onBind(intent: Intent): IBinder { - null!! - } - companion object { private const val TAG = "BatteryMgr:DataCollectionService" private const val NOTIFICATION_TITLE = "Battery Manager" diff --git a/app/src/main/java/com/example/batterymanager_utility/DataCollector.kt b/app/src/main/java/com/example/batterymanager_utility/DataCollector.kt index f7ae6e8..cfc478c 100644 --- a/app/src/main/java/com/example/batterymanager_utility/DataCollector.kt +++ b/app/src/main/java/com/example/batterymanager_utility/DataCollector.kt @@ -18,12 +18,17 @@ class DataCollector(private val context: Context, private val dataPoints: ArrayL } fun getData(): String { - var data = "${System.currentTimeMillis()}" + var data = "" + val receiver: Intent? = context.registerReceiver(null, intentFilter) for (dataPoint in dataPoints) { if (dataPoint.startsWith("EXTRA")) { - val receiver: Intent? = context.registerReceiver(null, intentFilter) - data += "," + receiver?.getIntExtra(dataPointsMapEXTRA[dataPoint], -1).toString() + if (dataPoint == "EXTRA_BATTERY_LOW" || dataPoint == "EXTRA_PRESENT") + data += "," + receiver?.getBooleanExtra(dataPointsMapEXTRA[dataPoint], false).toString() + else if (dataPoint == "EXTRA_TECHNOLOGY") + data += "," + receiver?.getStringExtra(dataPointsMapEXTRA[dataPoint]).toString() + else + data += "," + receiver?.getIntExtra(dataPointsMapEXTRA[dataPoint], Int.MIN_VALUE).toString() } else if (dataPoint == "ACTION_CHARGING") { data += "," + batteryManager.isCharging.toString() } else if (dataPoint == "ACTION_DISCHARGING") { @@ -33,7 +38,7 @@ class DataCollector(private val context: Context, private val dataPoints: ArrayL } } - return data + return "${System.currentTimeMillis()}" + data } companion object { diff --git a/app/src/main/java/com/example/batterymanager_utility/MainActivity.kt b/app/src/main/java/com/example/batterymanager_utility/MainActivity.kt index ba7c641..34f48fe 100644 --- a/app/src/main/java/com/example/batterymanager_utility/MainActivity.kt +++ b/app/src/main/java/com/example/batterymanager_utility/MainActivity.kt @@ -24,7 +24,6 @@ class MainActivity : ComponentActivity() { fun startService() { // adb shell am start-foreground-service -n "com.example.batterymanager_utility/com.example.batterymanager_utility.DataCollectionService" --ei sampleRate 800 \ --es "dataFields" "BATTERY_HEALTH_COLD,BATTERY_STATUS_CHARGING,EXTRA_VOLTAGE,EXTRA_TEMPERATURE" \ --ez toCSV True - val intent = Intent(this, DataCollectionService::class.java) intent.putExtra("sampleRate", 1000) intent.putExtra("dataFields", "BATTERY_HEALTH_COLD,BATTERY_STATUS_CHARGING,EXTRA_VOLTAGE,EXTRA_TEMPERATURE") @@ -48,7 +47,6 @@ fun HomeScreen(name: String, mainActivity: MainActivity) { Button(onClick = { mainActivity.startService() }) { Text(text = "Start Service") } - Button(onClick = { mainActivity.stopService() }) { Text(text = "Stop Service") }