Skip to content

Commit

Permalink
calls handling fully working, reconnection changes
Browse files Browse the repository at this point in the history
  • Loading branch information
crc-32 committed Jun 14, 2024
1 parent af79a7b commit 8323fb8
Show file tree
Hide file tree
Showing 11 changed files with 314 additions and 295 deletions.
2 changes: 1 addition & 1 deletion android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ android {

defaultConfig {
applicationId "io.rebble.cobble"
minSdkVersion 23
minSdkVersion 29
targetSdkVersion 34
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
Expand Down
22 changes: 10 additions & 12 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<uses-permission android:name="android.permission.REQUEST_COMPANION_RUN_IN_BACKGROUND" />
<uses-permission android:name="android.permission.REQUEST_COMPANION_USE_DATA_IN_BACKGROUND" />
<uses-permission android:name="android.permission.REQUEST_COMPANION_PROFILE_WATCH" />
<uses-permission android:name="android.permission.REQUEST_OBSERVE_COMPANION_DEVICE_PRESENCE" />
<uses-permission
android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"
android:maxSdkVersion="26" />
Expand Down Expand Up @@ -139,14 +140,20 @@
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
<!--<service android:name=".notifications.InCallService"
<service android:name=".service.CompanionDeviceService"
android:permission="android.permission.BIND_COMPANION_DEVICE_SERVICE"
android:exported="true">
<intent-filter>
<action android:name="android.companion.CompanionDeviceService" />
</intent-filter>
</service>
<service android:name=".service.InCallService"
android:permission="android.permission.BIND_INCALL_SERVICE"
android:exported="true">
<meta-data android:name="android.telecom.IN_CALL_SERVICE_UI" android:value="true" />
<intent-filter>
<action android:name="android.telecom.InCallService"/>
</intent-filter>
</service>-->
</service>

<receiver android:name=".receivers.AppStartReceiver" android:exported="true">
<intent-filter>
Expand All @@ -163,15 +170,6 @@
</intent-filter>
</receiver>

<receiver android:name=".receivers.CallReceiver" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>

<receiver
android:name=".bridges.background.BackgroundTimelineFlutterBridge$Receiver"
android:exported="false" />
Expand Down
16 changes: 15 additions & 1 deletion android/app/src/main/kotlin/io/rebble/cobble/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.rebble.cobble.bridges.FlutterBridge
import io.rebble.cobble.datasources.PermissionChangeBus
import io.rebble.cobble.pigeons.Pigeons
import io.rebble.cobble.service.InCallService
import io.rebble.cobble.service.CompanionDeviceService
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.plus
import java.net.URI
Expand Down Expand Up @@ -134,9 +135,22 @@ class MainActivity : FlutterActivity() {
flutterBridges = activityComponent.createCommonBridges() +
activityComponent.createUiBridges()

startAdditionalServices()

handleIntent(intent)
}

/**
* Start the CompanionDeviceService and InCallService
*/
private fun startAdditionalServices() {
val companionDeviceServiceIntent = Intent(this, CompanionDeviceService::class.java)
startService(companionDeviceServiceIntent)

val inCallServiceIntent = Intent(this, InCallService::class.java)
startService(inCallServiceIntent)
}

override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
handleIntent(intent)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.rebble.cobble.bluetooth

import android.bluetooth.BluetoothAdapter
import android.companion.CompanionDeviceManager
import android.content.Context
import androidx.annotation.RequiresPermission
import io.rebble.cobble.handlers.SystemHandler
Expand All @@ -25,11 +26,14 @@ class ConnectionLooper @Inject constructor(
private val _connectionState: MutableStateFlow<ConnectionState> = MutableStateFlow(
ConnectionState.Disconnected
)
private val _watchPresenceState = MutableStateFlow<String?>(null)
val watchPresenceState: StateFlow<String?> get() = _watchPresenceState

private val coroutineScope: CoroutineScope = GlobalScope + errorHandler

private var currentConnection: Job? = null
private var lastConnectedWatch: String? = null
private var delayJob: Job? = null

fun negotiationsComplete(watch: PebbleDevice) {
if (connectionState.value is ConnectionState.Negotiating) {
Expand All @@ -47,6 +51,17 @@ class ConnectionLooper @Inject constructor(
}
}

fun signalWatchPresence(macAddress: String) {
_watchPresenceState.value = macAddress
if (lastConnectedWatch == macAddress) {
delayJob?.cancel()
}
}

fun signalWatchAbsence() {
_watchPresenceState.value = null
}

@RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
fun connectToWatch(macAddress: String) {
coroutineScope.launch {
Expand Down Expand Up @@ -100,7 +115,15 @@ class ConnectionLooper @Inject constructor(
}
Timber.d("Watch connection failed, waiting and reconnecting after $retryTime ms")
_connectionState.value = ConnectionState.WaitingForReconnect(lastWatch)
delay(retryTime)
delayJob = launch {
delay(retryTime)
}
try {
delayJob?.join()
} catch (_: CancellationException) {
Timber.i("Reconnect delay interrupted")
retryTime = HALF_OF_INITAL_RETRY_TIME
}
}
} finally {
_connectionState.value = ConnectionState.Disconnected
Expand All @@ -127,6 +150,10 @@ class ConnectionLooper @Inject constructor(
}

fun closeConnection() {
lastConnectedWatch?.let {
val companionDeviceManager = context.getSystemService(CompanionDeviceManager::class.java)
companionDeviceManager.stopObservingDevicePresence(it)
}
currentConnection?.cancel()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class DeviceTransport @Inject constructor(
private val gattServerManager: GattServerManager = GattServerManager(context)

private var externalIncomingPacketHandler: (suspend (ByteArray) -> Unit)? = null
private var lastMacAddress: String? = null

@OptIn(FlowPreview::class)
@RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
Expand All @@ -47,6 +48,11 @@ class DeviceTransport @Inject constructor(
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val companionDeviceManager = context.getSystemService(CompanionDeviceManager::class.java)
Timber.d("Companion device associated: ${macAddress in companionDeviceManager.associations}, associations: ${companionDeviceManager.associations}")
lastMacAddress?.let {
companionDeviceManager.stopObservingDevicePresence(it)
}
lastMacAddress = macAddress
companionDeviceManager.startObservingDevicePresence(macAddress)
}

val bluetoothDevice = if (BuildConfig.DEBUG && !macAddress.contains(":")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ class SystemHandler @Inject constructor(
listOf(
ProtocolCapsFlag.Supports8kAppMessage,
ProtocolCapsFlag.SupportsExtendedMusicProtocol,
ProtocolCapsFlag.SupportsTwoWayDismissal,
ProtocolCapsFlag.SupportsAppRunStateProtocol
)
)
Expand Down

This file was deleted.

Loading

0 comments on commit 8323fb8

Please sign in to comment.