From 28d286bef54beccfab6609a5a240d04f7db6c2b5 Mon Sep 17 00:00:00 2001 From: sipp11 Date: Wed, 2 Oct 2019 02:00:56 +0900 Subject: [PATCH] Updates --- .../co/zzyzx/sensorlogger/EndlessService.kt | 61 +++++------- .../co/zzyzx/sensorlogger/MainActivity.kt | 93 ++++++++++++++++++- app/src/main/res/layout/activity_main.xml | 57 ++++++++++-- app/src/main/res/values/colors.xml | 2 + 4 files changed, 159 insertions(+), 54 deletions(-) diff --git a/app/src/main/java/co/zzyzx/sensorlogger/EndlessService.kt b/app/src/main/java/co/zzyzx/sensorlogger/EndlessService.kt index a8b0865..3fb1284 100644 --- a/app/src/main/java/co/zzyzx/sensorlogger/EndlessService.kt +++ b/app/src/main/java/co/zzyzx/sensorlogger/EndlessService.kt @@ -1,5 +1,6 @@ package co.zzyzx.sensorlogger +//import com.github.kittinunf.fuel.core.extensions.jsonBody import android.app.* import android.content.Context import android.content.Intent @@ -17,41 +18,25 @@ import android.os.Bundle import android.os.IBinder import android.os.PowerManager import android.provider.Settings -import android.support.v4.app.ActivityCompat -import android.support.v4.app.NotificationCompat import android.widget.Toast +import com.github.kittinunf.fuel.Fuel +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.delay +import kotlinx.coroutines.launch import java.text.SimpleDateFormat import java.util.* -import com.github.kittinunf.fuel.Fuel -//import com.github.kittinunf.fuel.core.extensions.jsonBody -import kotlinx.coroutines.* -import java.util.jar.Manifest - -const val PERMISSION_LOCATION = 0 +fun Double.format(digits: Int) = java.lang.String.format("%.${digits}f", this) -class EndlessService : Service(), SensorEventListener, LocationListener, - ActivityCompat.OnRequestPermissionsResultCallback { - - override fun onRequestPermissionsResult( - requestCode: Int, - permissions: Array, - grantResult: IntArray - ) { - when (requestCode) { - PERMISSION_LOCATION -> { - Toast.makeText( - applicationContext, - "${if (grantResult[0] == PackageManager.PERMISSION_GRANTED) "Good" else "Bad"} Permission for locaion", - Toast.LENGTH_LONG - ) - } - } - } +class EndlessService : Service(), SensorEventListener, LocationListener { override fun onLocationChanged(result: Location) { - val txt = "coords: ${result.longitude}, ${result.latitude} - ${result.time}" + val txt = + "coords: ${result.longitude.format(4)}, ${result.latitude.format(4)} - ${result.time.toDouble().format( + 0 + )}" log(txt) val notification = createNotification(txt) @@ -102,7 +87,6 @@ class EndlessService : Service(), SensorEventListener, LocationListener, ) } - mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL) // by returning this we make sure the service is restarted if the system kills the service @@ -123,25 +107,24 @@ class EndlessService : Service(), SensorEventListener, LocationListener, mLocationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager mLocationManager.requestLocationUpdates( LocationManager.PASSIVE_PROVIDER, - 0L, - 0.toFloat(), + 2000, // 2-sec + 2.toFloat(), // 2-meter this ) - } else { -// requestLocationPermission() - log("No location permission!") + mLocationManager.requestLocationUpdates( + LocationManager.GPS_PROVIDER, + 2000, // 2-sec + 2.toFloat(), // 2-meter + this + ) + log("ok permission to get location updates") } - -// val location = mLocationManager.addNmeaListener { s: String?, l: Long -> -// log("${s} = ${l.toString()}") -// } - - } override fun onDestroy() { super.onDestroy() mSensorManager.unregisterListener(this) + mLocationManager.removeUpdates(this) log("The service has been destroyed".toUpperCase()) Toast.makeText(this, "Service destroyed", Toast.LENGTH_SHORT).show() } diff --git a/app/src/main/java/co/zzyzx/sensorlogger/MainActivity.kt b/app/src/main/java/co/zzyzx/sensorlogger/MainActivity.kt index ccfaaa2..ef09f20 100644 --- a/app/src/main/java/co/zzyzx/sensorlogger/MainActivity.kt +++ b/app/src/main/java/co/zzyzx/sensorlogger/MainActivity.kt @@ -1,33 +1,98 @@ package co.zzyzx.sensorlogger +import android.Manifest +import android.content.Context import android.content.Intent +import android.content.pm.PackageManager +import android.graphics.Color +import android.location.LocationManager import android.os.Build -import android.support.v7.app.AppCompatActivity import android.os.Bundle -import android.util.Log +import android.support.v4.app.ActivityCompat +import android.support.v4.content.ContextCompat +import android.support.v7.app.AppCompatActivity +import android.view.View import android.widget.Button +import android.widget.Toast +import kotlinx.android.synthetic.main.activity_main.* + -class MainActivity : AppCompatActivity() { +const val PERMISSION_LOCATION = 0 + +class MainActivity : AppCompatActivity(), + ActivityCompat.OnRequestPermissionsResultCallback { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) + requestForNecessaryPermissions() title = getString(R.string.app_name) - findViewById