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. 55
      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 package co.zzyzx.sensorlogger
//import com.github.kittinunf.fuel.core.extensions.jsonBody
import android.app.* import android.app.*
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
@ -17,41 +18,25 @@ import android.os.Bundle
import android.os.IBinder import android.os.IBinder
import android.os.PowerManager import android.os.PowerManager
import android.provider.Settings import android.provider.Settings
import android.support.v4.app.ActivityCompat
import android.support.v4.app.NotificationCompat
import android.widget.Toast 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.text.SimpleDateFormat
import java.util.* 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, 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
)
}
}
}
override fun onLocationChanged(result: Location) { 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) log(txt)
val notification = createNotification(txt) val notification = createNotification(txt)
@ -102,7 +87,6 @@ class EndlessService : Service(), SensorEventListener, LocationListener,
) )
} }
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL) mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL)
// by returning this we make sure the service is restarted if the system kills the service // 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 = getSystemService(Context.LOCATION_SERVICE) as LocationManager
mLocationManager.requestLocationUpdates( mLocationManager.requestLocationUpdates(
LocationManager.PASSIVE_PROVIDER, LocationManager.PASSIVE_PROVIDER,
0L, 2000, // 2-sec
0.toFloat(), 2.toFloat(), // 2-meter
this this
) )
} else { mLocationManager.requestLocationUpdates(
// requestLocationPermission() LocationManager.GPS_PROVIDER,
log("No location permission!") 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() { override fun onDestroy() {
super.onDestroy() super.onDestroy()
mSensorManager.unregisterListener(this) mSensorManager.unregisterListener(this)
mLocationManager.removeUpdates(this)
log("The service has been destroyed".toUpperCase()) log("The service has been destroyed".toUpperCase())
Toast.makeText(this, "Service destroyed", Toast.LENGTH_SHORT).show() 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 package co.zzyzx.sensorlogger
import android.Manifest
import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Color
import android.location.LocationManager
import android.os.Build import android.os.Build
import android.support.v7.app.AppCompatActivity
import android.os.Bundle 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.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?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main) setContentView(R.layout.activity_main)
requestForNecessaryPermissions()
title = getString(R.string.app_name) title = getString(R.string.app_name)
findViewById<Button>(R.id.btnStartService).let { start_service_btn.let {
it.setOnClickListener { it.setOnClickListener {
log("START THE FOREGROUND SERVICE ON DEMAND") log("START THE FOREGROUND SERVICE ON DEMAND")
actionOnService(Actions.START) actionOnService(Actions.START)
} }
} }
findViewById<Button>(R.id.btnStopService).let { stop_service_btn.let {
it.setOnClickListener { it.setOnClickListener {
log("STOP THE FOREGROUND SERVICE ON DEMAND") log("STOP THE FOREGROUND SERVICE ON DEMAND")
actionOnService(Actions.STOP) 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) { private fun actionOnService(action: Actions) {
@ -43,4 +108,22 @@ class MainActivity : AppCompatActivity() {
startService(it) 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)
}
}
}
} }

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

@ -1,29 +1,66 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"> tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Status" />
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" 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 <Button
android:id="@+id/btnStartService" android:id="@+id/ask_location_permission_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:padding="0dp"
android:text="Permission Request" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="Start Foreground Service" android:paddingTop="20dp" android:layout_marginTop="20dp"/> android:orientation="horizontal">
<Button <Button
android:id="@+id/btnStopService" android:id="@+id/start_service_btn"
android:layout_width="match_parent" android:layout_width="wrap_content"
android:layout_height="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> </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="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color> <color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color> <color name="colorAccent">#D81B60</color>
<color name="green">#22DD22</color>
<color name="red">#DD2222</color>
</resources> </resources>

Loading…
Cancel
Save