Browse Source

Export works!

master
sipp11 5 years ago
parent
commit
2318bb9f94
  1. 2
      .idea/misc.xml
  2. 4
      app/build.gradle
  3. 1
      app/src/main/AndroidManifest.xml
  4. 170
      app/src/main/java/co/zzyzx/sensorlogger/MainActivity.kt
  5. 8
      app/src/main/java/co/zzyzx/sensorlogger/db/Database.kt
  6. 5
      app/src/main/java/co/zzyzx/sensorlogger/db/dao.kt

2
.idea/misc.xml

@ -5,7 +5,7 @@
<configuration PROFILE_NAME="Debug" CONFIG_NAME="Debug" />
</configurations>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">

4
app/build.gradle

@ -36,7 +36,9 @@ dependencies {
implementation 'android.arch.persistence.room:runtime:1.1.1'
annotationProcessor 'android.arch.persistence.room:compiler:1.1.1'
kapt 'android.arch.persistence.room:compiler:1.1.1'
// implementation 'android.arch.lifecycle:extensions:1.1.1'
implementation 'com.opencsv:opencsv:4.0'
// implementation 'android.arch.lifecycle:excompile 'com.opencsv:opencsv:4.0'tensions:1.1.1'
// kapt 'android.arch.lifecycle:compiler:1.1.1'
testImplementation 'junit:junit:4.12'

1
app/src/main/AndroidManifest.xml

@ -6,6 +6,7 @@
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application

170
app/src/main/java/co/zzyzx/sensorlogger/MainActivity.kt

@ -1,13 +1,14 @@
package co.zzyzx.sensorlogger
//import co.zzyzx.sensorlogger.db.RecordDatabase
import android.Manifest
import android.app.ActivityManager
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.location.LocationManager
import android.os.Build
import android.os.Bundle
import android.os.Environment
import android.os.Handler
import android.support.v4.app.ActivityCompat
import android.support.v4.content.ContextCompat
@ -15,10 +16,17 @@ import android.support.v7.app.AppCompatActivity
import android.view.View
import android.widget.Toast
import co.zzyzx.sensorlogger.db.RecordRepository
import com.opencsv.CSVWriter
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.io.File
import java.io.FileWriter
import java.time.Instant
const val PERMISSION_LOCATION = 0
const val WRITE_EXTERNAL_STORAGE = 1
class MainActivity : AppCompatActivity(),
ActivityCompat.OnRequestPermissionsResultCallback {
@ -60,8 +68,122 @@ class MainActivity : AppCompatActivity(),
}
}
reset_record_btn.let {
it.setOnClickListener {
recRepo.resetAllData()
}
}
export_record_btn.let {
it.setOnClickListener {
log("exporting something")
actionOnService(Actions.STOP)
if (ContextCompat.checkSelfPermission(
applicationContext,
Manifest.permission.WRITE_EXTERNAL_STORAGE
)
!= PackageManager.PERMISSION_GRANTED
) {
val perms = arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE)
ActivityCompat.requestPermissions(
this, perms,
WRITE_EXTERNAL_STORAGE
)
} else {
GlobalScope.launch {
exportToCSV()
recRepo.resetAllData()
}
}
}
}
updateCounter()
if (isMyServiceRunning(EndlessService::class.java)) {
start_service_btn.visibility = View.GONE
stop_service_btn.visibility = View.VISIBLE
} else {
start_service_btn.visibility = View.VISIBLE
stop_service_btn.visibility = View.GONE
}
}
private fun exportToCSV() {
val outputPath =
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
var epochSec = Instant.now().epochSecond
val acceleroFileName = "SensorLogger-${epochSec}-accelero.csv"
val gyroFileName = "SensorLogger-${epochSec}-gyro.csv"
var writerAccelero = CSVWriter(FileWriter("$outputPath${File.separator}$acceleroFileName"))
var writerGyro = CSVWriter(FileWriter("$outputPath${File.separator}$gyroFileName"))
writerAccelero.writeNext(
arrayOf(
"timestamp",
"lat",
"lon",
"speed",
"gps_timestamp",
"accel-x",
"accel-y",
"accel-z"
)
)
writerGyro.writeNext(
arrayOf(
"timestamp",
"lat",
"lon",
"speed",
"gps_timestamp",
"gyro-x",
"gyro-y",
"gyro-z"
)
)
val records = recRepo.getAll()
var lat = ""
var lon = ""
var speed = ""
var time = ""
for (rec in records) {
if (rec.sensor == "location") {
val one = rec.data?.split(",") as List<String>
/*
result.time,
result.longitude.format(7),
result.latitude.format(7),
result.speed,
result.altitude,
result.bearing,
result.accuracy,
result.provider,
result.isFromMockProvider
*/
if (one.size != 0) {
time = one[0]
lon = one[1]
lat = one[2]
speed = one[3]
}
} else if (rec.sensor == "accelerometer") {
val data = rec.data?.split(",") as List<String>
val row =
arrayOf("${rec.timestamp}", lat, lon, speed, time, data[0], data[1], data[2])
writerAccelero.writeNext(row)
} else if (rec.sensor == "gyroscope") {
val data = rec.data?.split(",") as List<String>
val row =
arrayOf("${rec.timestamp}", lat, lon, speed, time, data[0], data[1], data[2])
writerGyro.writeNext(row)
}
}
writerAccelero.close()
writerGyro.close()
}
private fun updateCounter() {
@ -79,7 +201,6 @@ class MainActivity : AppCompatActivity(),
reset_record_btn.visibility = View.VISIBLE
export_record_btn.visibility = View.VISIBLE
} else {
reset_record_btn.visibility = View.GONE
export_record_btn.visibility = View.GONE
}
@ -91,8 +212,15 @@ class MainActivity : AppCompatActivity(),
private fun UIStatusUpdates(hasLocPerm: Boolean = true) {
val mLocationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
log("isLocationEnabled? ${mLocationManager.isLocationEnabled} - ${mLocationManager.allProviders}")
if (hasLocPerm && mLocationManager.isLocationEnabled) {
val isLocationEnabled = (
Build.VERSION.SDK_INT < Build.VERSION_CODES.P || (
Build.VERSION.SDK_INT >= Build.VERSION_CODES.P &&
mLocationManager.isLocationEnabled)
)
log("isLocationEnabled? $isLocationEnabled - ${mLocationManager.allProviders}")
if (hasLocPerm && isLocationEnabled) {
location_status_textview.text = "OK"
location_status_textview.setBackgroundColor(
ContextCompat.getColor(
@ -113,11 +241,11 @@ class MainActivity : AppCompatActivity(),
}
}
private fun requestForNecessaryPermissions() {
private fun requestForNecessaryPermissions(perm: String = Manifest.permission.ACCESS_FINE_LOCATION) {
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_FINE_LOCATION
perm
)
!= PackageManager.PERMISSION_GRANTED
) {
@ -125,14 +253,13 @@ class MainActivity : AppCompatActivity(),
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(
this,
Manifest.permission.ACCESS_FINE_LOCATION
perm
)
) {
val perms = arrayOf(Manifest.permission.ACCESS_FINE_LOCATION)
ActivityCompat.requestPermissions(
this, perms,
PERMISSION_LOCATION
)
val perms = arrayOf(perm)
val permResult =
if (perm == Manifest.permission.ACCESS_FINE_LOCATION) PERMISSION_LOCATION else WRITE_EXTERNAL_STORAGE
ActivityCompat.requestPermissions(this, perms, permResult)
}
UIStatusUpdates(false)
} else {
@ -144,6 +271,16 @@ class MainActivity : AppCompatActivity(),
}
private fun isMyServiceRunning(serviceClass: Class<*>): Boolean {
val manager = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
for (service in manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.name == service.service.className) {
return true
}
}
return false
}
private fun actionOnService(action: Actions) {
when (action) {
Actions.START -> {
@ -185,6 +322,15 @@ class MainActivity : AppCompatActivity(),
)
UIStatusUpdates(hasPerm)
}
WRITE_EXTERNAL_STORAGE -> {
val hasPerm = grantResult[0] == PackageManager.PERMISSION_GRANTED
Toast.makeText(
applicationContext,
"${if (hasPerm) "Good" else "Bad"} Permission for storage",
Toast.LENGTH_LONG
)
// UIStatusUpdates(hasPerm)
}
}
}
}

8
app/src/main/java/co/zzyzx/sensorlogger/db/Database.kt

@ -40,4 +40,12 @@ class RecordRepository {
return dao.getCount(sensor)
}
fun getAll(): List<Record> {
return dao.getAll()
}
fun resetAllData() {
dao.nukeTable()
}
}

5
app/src/main/java/co/zzyzx/sensorlogger/db/dao.kt

@ -8,7 +8,7 @@ interface RecordDao {
@Query("SELECT COUNT(*) FROM record WHERE sensor = :sensor")
fun getCount(sensor: String): Int
@Query("SELECT * FROM record")
@Query("SELECT * FROM record ORDER BY timestamp ASC")
fun getAll(): List<Record>
@Query("SELECT * FROM record WHERE sensor LIKE :sensor ORDER BY timestamp DESC LIMIT 1")
@ -26,4 +26,7 @@ interface RecordDao {
@Delete
fun delete(record: Record)
@Query("DELETE FROM record")
fun nukeTable()
}

Loading…
Cancel
Save