Browse Source

Updates

master
sipp11 5 years ago
parent
commit
28d286bef5
  1. 61
      app/src/main/java/co/zzyzx/sensorlogger/EndlessService.kt
  2. 93
      app/src/main/java/co/zzyzx/sensorlogger/MainActivity.kt
  3. 57
      app/src/main/res/layout/activity_main.xml
  4. 2
      app/src/main/res/values/colors.xml

61
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<out String>,
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()
}

93
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<Button>(R.id.btnStartService).let {
start_service_btn.let {
it.setOnClickListener {
log("START THE FOREGROUND SERVICE ON DEMAND")
actionOnService(Actions.START)
}
}
findViewById<Button>(R.id.btnStopService).let {
stop_service_btn.let {
it.setOnClickListener {
log("STOP THE FOREGROUND SERVICE ON DEMAND")
actionOnService(Actions.STOP)
}
}
ask_location_permission_button.let{
it.setOnClickListener { requestForNecessaryPermissions() }
}
}
private fun UIStatusUpdates(hasLocPerm : Boolean = true) {
val mLocationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
log("isLocationEnabled? ${mLocationManager.isLocationEnabled} - ${mLocationManager.allProviders.toString()}")
if (hasLocPerm && mLocationManager.isLocationEnabled) {
location_status_textview.text = "OK"
location_status_textview.setBackgroundColor(ContextCompat.getColor(applicationContext, R.color.green))
} else {
location_status_textview.text = "OFF"
location_status_textview.setBackgroundColor(ContextCompat.getColor(applicationContext, R.color.red))
}
}
private fun requestForNecessaryPermissions() {
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_FINE_LOCATION
)
!= PackageManager.PERMISSION_GRANTED
) {
ask_location_permission_button.visibility = View.INVISIBLE
// Permission is not granted
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(
this,
Manifest.permission.ACCESS_FINE_LOCATION
)
) {
val perms = arrayOf(Manifest.permission.ACCESS_FINE_LOCATION)
ActivityCompat.requestPermissions(
this, perms,
PERMISSION_LOCATION
)
}
UIStatusUpdates(false)
} else {
// Permission has already been granted
log("Permission has already been granted")
ask_location_permission_button.visibility = View.INVISIBLE
UIStatusUpdates(true)
}
}
private fun actionOnService(action: Actions) {
@ -43,4 +108,22 @@ class MainActivity : AppCompatActivity() {
startService(it)
}
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResult: IntArray
) {
when (requestCode) {
PERMISSION_LOCATION -> {
val hasPerm = grantResult[0] == PackageManager.PERMISSION_GRANTED
Toast.makeText(
applicationContext,
"${if (hasPerm) "Good" else "Bad"} Permission for location",
Toast.LENGTH_LONG
)
UIStatusUpdates(hasPerm)
}
}
}
}

57
app/src/main/res/layout/activity_main.xml

@ -1,29 +1,66 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Status" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/background_light"
android:text="GPS" />
<TextView
android:id="@+id/location_status_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:textAlignment="center"
android:background="@android:color/background_light"
android:text="" />
<Button
android:id="@+id/btnStartService"
android:layout_width="match_parent"
android:id="@+id/ask_location_permission_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Foreground Service" android:paddingTop="20dp" android:layout_marginTop="20dp"/>
android:layout_weight="3"
android:padding="0dp"
android:text="Permission Request" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btnStopService"
android:layout_width="match_parent"
android:id="@+id/start_service_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop Foreground Service"/>
android:layout_weight="1"
android:text="Start Foreground Service" />
<Button
android:id="@+id/stop_service_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Stop Foreground Service" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
</LinearLayout>

2
app/src/main/res/values/colors.xml

@ -3,4 +3,6 @@
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>
<color name="green">#22DD22</color>
<color name="red">#DD2222</color>
</resources>

Loading…
Cancel
Save