WIP: GMaps Parser

This commit is contained in:
Piotr Dec 2023-08-13 23:17:13 +02:00
parent 307343c2eb
commit e5b2e811ce
5 changed files with 118 additions and 41 deletions

View file

@ -2,6 +2,7 @@ package eu.ztsh.garmin
import android.Manifest
import android.annotation.SuppressLint
import android.app.PendingIntent
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothManager
@ -11,31 +12,84 @@ import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.widget.CompoundButton
import androidx.activity.result.ActivityResultCallback
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import eu.ztsh.garmin.databinding.ActivityMainBinding
import me.trevi.navparser.lib.NavigationData
import me.trevi.navparser.service.*
import java.io.IOException
import java.util.*
@SuppressLint("MissingPermission")
class MainActivity : AppCompatActivity() {
var navigating: Boolean = false
lateinit var garmin: Garmin
private lateinit var binding : ActivityMainBinding
private val navDataModel: NavigationDataModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
bluetoothInit()
}
override fun onPause() {
super.onPause()
// bluetoothSerial.onPause()
binding.enabledSwitch.setOnCheckedChangeListener {
_, isChecked ->
run {
navigating = isChecked
setNavigationData(NavigationData(true))
navDataModel.liveData.observe(this) { setNavigationData(it) }
Log.d(TAG, "OK")
}
}
}
override fun onResume() {
super.onResume()
// bluetoothSerial.onResume()
override fun onStart() {
super.onStart()
startServiceListener()
checkNotificationsAccess()
}
override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?) {
super.onActivityResult(requestCode, resultCode, intent)
Log.d(TAG, "Got activity result: $intent, ${intent?.extras}, ${intent?.action}")
when (intent?.action) {
NOTIFICATIONS_ACCESS_RESULT -> {
intent.getBooleanExtra(NOTIFICATIONS_ACCESS_RESULT, false).also {
val notificationAccess = it
if (!notificationAccess) {
Log.e(TAG, "No notification access for ${NavigationListenerEmitter::class.qualifiedName}")
}
}
}
NAVIGATION_DATA_UPDATED -> {
// gotoFragment(R.id.NavigationFragment)
val navData = intent.getParcelableExtra<NavigationData>(NAVIGATION_DATA)
Log.d(TAG, "Got navigation data $navData")
navDataModel.data = navData
}
NAVIGATION_STARTED -> {
Log.d(TAG, "Started")
}
NAVIGATION_STOPPED -> {
Log.d(TAG, "Stopped")
}
}
}
private fun bluetoothInit() {
@ -84,38 +138,30 @@ class MainActivity : AppCompatActivity() {
return true
}
private inner class ConnectThread(val device: BluetoothDevice, val adapter: BluetoothAdapter) : Thread() {
private fun startServiceListener() {
Log.d(TAG, "Start Service Listener")
setServiceListenerIntent(createPendingResult(100, Intent(), 0))
}
private val mmSocket: BluetoothSocket? by lazy(LazyThreadSafetyMode.NONE) {
checkBt()
// device.createInsecureRfcommSocketToServiceRecord(UUID.fromString("7d00d7f5-921b-450c-8eda-26e1d4a15c61"))
device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"))
}
fun stopServiceListener() {
Log.d(TAG, "Stopping Service Listener")
setServiceListenerIntent(null)
}
override fun run() {
// Cancel discovery because it otherwise slows down the connection.
checkBt()
adapter.cancelDiscovery()
private fun setServiceListenerIntent(pendingIntent: PendingIntent?) {
startService(serviceIntent(SET_INTENT).putExtra(PENDING_INTENT, pendingIntent))
}
mmSocket?.let { socket ->
// Connect to the remote device through the socket. This call blocks
// until it succeeds or throws an exception.
socket.connect()
private fun checkNotificationsAccess() {
startService(serviceIntent(CHECK_NOTIFICATIONS_ACCESS))
}
// The connection attempt succeeded. Perform work associated with
// the connection in a separate thread.
// manageMyConnectedSocket(socket)
}
}
// Closes the client socket and causes the thread to finish.
fun cancel() {
try {
mmSocket?.close()
} catch (e: IOException) {
Log.e(Companion.TAG, "Could not close the client socket", e)
}
}
private fun serviceIntent(action: String): Intent {
return Intent(applicationContext, NavigationListenerEmitter::class.java).setAction(action)
}
private fun setNavigationData(navData: NavigationData) {
Log.d(TAG, "Sending $navData")
garmin.send(navData)
}
companion object {

View file

@ -0,0 +1,14 @@
package eu.ztsh.garmin
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import me.trevi.navparser.lib.NavigationData
class NavigationDataModel : ViewModel() {
private val mutableData = MutableLiveData<NavigationData>()
val liveData: LiveData<NavigationData> get() = mutableData
var data: NavigationData?
get() = mutableData.value
set(value) { mutableData.value = value }
}