Skip to content

Commit

Permalink
Merge pull request #2 from Raiduy/dev
Browse files Browse the repository at this point in the history
EXTRA_* fixes
  • Loading branch information
Raiduy authored May 3, 2023
2 parents 3040d18 + 226b2c9 commit 6f9effd
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 19 deletions.
12 changes: 5 additions & 7 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.HIGH_SAMPLING_RATE_SENSORS" />

<application
android:name=".App"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@drawable/ic_launcher_foreground"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.Batterymanager_utility"
tools:targetApi="31">
>

<activity
android:name=".MainActivity"
Expand All @@ -32,7 +29,8 @@
<service
android:name=".DataCollectionService"
android:exported="true"
android:launchMode="singleTop">
android:launchMode="singleTop"
>
<intent-filter>
<action android:name="com.example.batterymanager_utility.DataCollectionService" />
<action android:name="android.intent.action.MAIN" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>
dataFields.add(0,"Timestamp")

Expand Down Expand Up @@ -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())
}
}

Expand All @@ -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")
Expand All @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand All @@ -33,7 +38,7 @@ class DataCollector(private val context: Context, private val dataPoints: ArrayL
}
}

return data
return "${System.currentTimeMillis()}" + data
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
}
Expand Down

0 comments on commit 6f9effd

Please sign in to comment.