Browse Source

WIP

androidx-act-recog
sipp11 5 years ago
parent
commit
ff4e84436f
  1. 1
      app/src/main/AndroidManifest.xml
  2. 30
      app/src/main/java/co/zzyzx/sensorlogger/ActivityTransitionIntentService.kt
  3. 47
      app/src/main/java/co/zzyzx/sensorlogger/EndlessService.kt
  4. 14
      app/src/main/java/co/zzyzx/sensorlogger/MainActivity.kt

1
app/src/main/AndroidManifest.xml

@ -9,6 +9,7 @@
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />
<application <application

30
app/src/main/java/co/zzyzx/sensorlogger/ActivityTransitionIntentService.kt

@ -0,0 +1,30 @@
package co.zzyzx.sensorlogger
import android.app.IntentService
import android.content.Intent
import android.preference.PreferenceManager
import android.widget.Toast
import com.google.android.gms.location.ActivityTransitionResult
private const val TAG = "ActivityTransitionIS"
class ActivityTransitionIntentService : IntentService(TAG) {
override fun onHandleIntent(intent: Intent) {
val result = ActivityTransitionResult.extractResult(intent) as ActivityTransitionResult
val events = result.transitionEvents
var txt = ArrayList<String>(0)
for (evt in events) {
txt.add("${evt.activityType} - ${evt.transitionType}")
}
log("ACT_TRANSITION: ${events.joinToString(",")}")
Toast.makeText(applicationContext, events.joinToString(","), Toast.LENGTH_SHORT)
PreferenceManager.getDefaultSharedPreferences(this).edit()
.putString("ACTIVITY", txt.joinToString("|"))
.apply()
}
}

47
app/src/main/java/co/zzyzx/sensorlogger/EndlessService.kt

@ -4,7 +4,6 @@ import android.app.*
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.graphics.Color
import android.hardware.Sensor import android.hardware.Sensor
import android.hardware.SensorEvent import android.hardware.SensorEvent
import android.hardware.SensorEventListener import android.hardware.SensorEventListener
@ -100,7 +99,7 @@ class EndlessService : Service(), SensorEventListener, LocationListener {
mSensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager mSensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
mGyroscope = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE) mGyroscope = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE)
// setupActivityRecognitionUpdates() setupActivityRecognitionUpdates()
if (checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { if (checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
mLocationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager mLocationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
@ -120,6 +119,12 @@ class EndlessService : Service(), SensorEventListener, LocationListener {
} }
} }
private fun getActivityTransitionPendingIntent(): PendingIntent {
val intent = Intent(this, ActivityTransitionIntentService::class.java)
return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}
private fun setupActivityRecognitionUpdates() { private fun setupActivityRecognitionUpdates() {
// ActivityRecognition // ActivityRecognition
val transitions = mutableListOf<ActivityTransition>() val transitions = mutableListOf<ActivityTransition>()
@ -128,35 +133,46 @@ class EndlessService : Service(), SensorEventListener, LocationListener {
.setActivityType(DetectedActivity.IN_VEHICLE) .setActivityType(DetectedActivity.IN_VEHICLE)
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER) .setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER)
.build() .build()
transitions += /*transitions +=
ActivityTransition.Builder() ActivityTransition.Builder()
.setActivityType(DetectedActivity.IN_VEHICLE) .setActivityType(DetectedActivity.IN_VEHICLE)
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT) .setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT)
.build() .build()*/
transitions += transitions +=
ActivityTransition.Builder() ActivityTransition.Builder()
.setActivityType(DetectedActivity.ON_BICYCLE) .setActivityType(DetectedActivity.ON_BICYCLE)
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER) .setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER)
.build() .build()
transitions += /*transitions +=
ActivityTransition.Builder() ActivityTransition.Builder()
.setActivityType(DetectedActivity.ON_BICYCLE) .setActivityType(DetectedActivity.ON_BICYCLE)
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT) .setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT)
.build() .build()*/
transitions += transitions +=
ActivityTransition.Builder() ActivityTransition.Builder()
.setActivityType(DetectedActivity.WALKING) .setActivityType(DetectedActivity.WALKING)
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER) .setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER)
.build() .build()
transitions += /*transitions +=
ActivityTransition.Builder() ActivityTransition.Builder()
.setActivityType(DetectedActivity.WALKING) .setActivityType(DetectedActivity.WALKING)
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT) .setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT)
.build()*/
transitions +=
ActivityTransition.Builder()
.setActivityType(DetectedActivity.STILL)
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER)
.build() .build()
val request = ActivityTransitionRequest(transitions) val request = ActivityTransitionRequest(transitions)
val pendingIntent = getActivityTransitionPendingIntent()
mActivityRecognitionClient = ActivityRecognition.getClient(applicationContext) mActivityRecognitionClient = ActivityRecognition.getClient(applicationContext)
mActivityRecognitionClient.requestActivityTransitionUpdates(request, null) val task =
mActivityRecognitionClient.requestActivityTransitionUpdates(request, pendingIntent)
task.addOnSuccessListener { log("ACT_TRANSITION: success listener") }
task.addOnFailureListener { exception ->
log("ACT_TRANSITION: failed $exception")
}
} }
override fun onDestroy() { override fun onDestroy() {
@ -310,7 +326,8 @@ class EndlessService : Service(), SensorEventListener, LocationListener {
val (bytes, error) = result val (bytes, error) = result
if (bytes != null) { if (bytes != null) {
log("[response bytes] ${String(bytes)}") log("[response bytes] ${String(bytes)}")
val notification = createNotification("[req] ${gmtTime} - ${String(bytes)}") val notification =
createNotification("[req] ${gmtTime} - ${String(bytes)}")
nm.notify(notificationId, notification) nm.notify(notificationId, notification)
} else { } else {
log("[response error] ${error?.message}") log("[response error] ${error?.message}")
@ -330,13 +347,13 @@ class EndlessService : Service(), SensorEventListener, LocationListener {
val channel = NotificationChannel( val channel = NotificationChannel(
notificationChannelId, notificationChannelId,
"SL notifications channel", "SL notifications channel",
NotificationManager.IMPORTANCE_DEFAULT NotificationManager.IMPORTANCE_LOW
).let { ).let {
it.description = "SL Service channel" it.description = "SL Service channel"
it.enableLights(true) it.enableLights(false)
it.lightColor = Color.RED //it.lightColor = Color.RED
it.enableVibration(true) it.enableVibration(false)
it.vibrationPattern = longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400) //it.vibrationPattern = longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400)
it it
} }
nm.createNotificationChannel(channel) nm.createNotificationChannel(channel)

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

@ -4,12 +4,15 @@ import android.Manifest
import android.app.ActivityManager import android.app.ActivityManager
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.SharedPreferences
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.location.LocationManager import android.location.LocationManager
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.os.Environment import android.os.Environment
import android.os.Handler import android.os.Handler
import android.preference.Preference
import android.preference.PreferenceManager
import android.view.View import android.view.View
import android.widget.Toast import android.widget.Toast
@ -24,13 +27,15 @@ import kotlinx.coroutines.launch
import java.io.File import java.io.File
import java.io.FileWriter import java.io.FileWriter
import java.time.Instant import java.time.Instant
import java.util.prefs.PreferenceChangeListener
const val PERMISSION_LOCATION = 0 const val PERMISSION_LOCATION = 0
const val WRITE_EXTERNAL_STORAGE = 1 const val WRITE_EXTERNAL_STORAGE = 1
class MainActivity : AppCompatActivity(), class MainActivity : AppCompatActivity(),
ActivityCompat.OnRequestPermissionsResultCallback { ActivityCompat.OnRequestPermissionsResultCallback,
SharedPreferences.OnSharedPreferenceChangeListener {
private lateinit var recRepo: RecordRepository private lateinit var recRepo: RecordRepository
private var handler = Handler() private var handler = Handler()
@ -334,4 +339,11 @@ class MainActivity : AppCompatActivity(),
} }
} }
} }
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: String) {
if (key == "ACTIVITY") {
status_textview.text = PreferenceManager.getDefaultSharedPreferences(this).getString(key, "- Nah -")
}
}
} }

Loading…
Cancel
Save