Skip to content

Commit

Permalink
Start android service and call its methods (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
Izchomatik authored and gavv committed Oct 5, 2024
1 parent 813f6e7 commit a39cd68
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,35 @@ import org.rocstreaming.service.SenderReceiverService
private const val LOG_TAG = "[rocdroid.Connector]"

class AndroidConnectorImpl : AndroidConnector {
//private var senderReceiverService: SenderReceiverService = SenderReceiverService()
companion object {
lateinit var senderReceiverService : SenderReceiverService
}

override fun startReceiver() {
Log.d(LOG_TAG, "Start Receiver")
//senderReceiverService.startReceiver()
Log.d(LOG_TAG, "Try start Receiver")
senderReceiverService.startReceiver()
}

override fun stopReceiver() {
Log.d(LOG_TAG, "Stop Receiver")
//senderReceiverService.stopReceiver()
Log.d(LOG_TAG, "Try stop Receiver")
senderReceiverService.stopReceiver()
}

override fun isReceiverAlive(): Boolean {
//return senderReceiverService.isReceiverAlive()
return true
return senderReceiverService.isReceiverAlive()
}

override fun startSender(ip: String) {
Log.d(LOG_TAG, "Start Sender")
//senderReceiverService.startSender(ip, null)
Log.d(LOG_TAG, "Try start Sender")
senderReceiverService.startSender(ip, null)
}

override fun stopSender() {
Log.d(LOG_TAG, "Stop Sender")
//senderReceiverService.stopSender()
Log.d(LOG_TAG, "Try stop Sender")
senderReceiverService.stopSender()
}

override fun isSenderAlive(): Boolean {
//return senderReceiverService.isSenderAlive()
return true
return senderReceiverService.isReceiverAlive()
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,63 @@
package org.rocstreaming.rocdroid

import AndroidConnector
import android.content.ComponentName
import android.content.Intent
import android.content.ServiceConnection
import android.media.AudioManager
import android.os.Bundle
import android.os.IBinder
import android.util.Log
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.embedding.engine.plugins.FlutterPlugin
import org.rocstreaming.connector.AndroidConnectorImpl
import org.rocstreaming.service.SenderReceiverService

private const val LOG_TAG = "[rocdroid.MainActivity]"

class MainActivity: FlutterActivity() {
private lateinit var senderReceiverService: SenderReceiverService

override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)

AndroidConnector.setUp(flutterEngine.dartExecutor.binaryMessenger,
AndroidConnectorImpl())
}

private val senderReceiverServiceConnection = object : ServiceConnection {
override fun onServiceConnected(componentName: ComponentName, binder: IBinder) {
senderReceiverService = (binder as SenderReceiverService.LocalBinder).getService()

Log.d(LOG_TAG, "On service connected")

AndroidConnectorImpl.senderReceiverService = senderReceiverService
}

override fun onServiceDisconnected(componentName: ComponentName) {

Log.d(LOG_TAG, "On service disconnected")

senderReceiverService.removeListeners()
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

Log.d(LOG_TAG, "Create Main Activity")

val serviceIntent = Intent(this, SenderReceiverService::class.java)
bindService(serviceIntent, senderReceiverServiceConnection, BIND_AUTO_CREATE)
}

override fun onResume() {
super.onResume()
volumeControlStream = AudioManager.STREAM_MUSIC
}

override fun onDestroy() {
super.onDestroy()
unbindService(senderReceiverServiceConnection)
}
}
26 changes: 16 additions & 10 deletions lib/src/model/receiver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,30 +49,36 @@ abstract class _Receiver with Store {

// Start current receiver.
@action
void start() {
Future<bool> start() async {
if (isStarted) {
return;
_logger.i('Attempt to start an already running receiver.');
return _isStarted;
}

// Main backend call
_backend.startReceiver();
await _backend.startReceiver();

_isStarted = !_isStarted;
_logger.i('Receiver started');
var status = await _backend.isReceiverAlive();
_logger.i('Trying to start the receiver. roc service status: $status');
_isStarted = status;
return _isStarted;
}

// Stop current receiver.
@action
void stop() {
Future<bool> stop() async {
if (!isStarted) {
return;
_logger.i('Attempt to stop inactive receiver.');
return isStarted;
}

// Main backend call
_backend.stopReceiver();
await _backend.stopReceiver();

_isStarted = !_isStarted;
_logger.i('Receiver stopped');
var status = await _backend.isReceiverAlive();
_logger.i('Trying to stop the receiver. roc service status: $status');
_isStarted = status;
return _isStarted;
}

// Update collection of available receiver IP addresses.
Expand Down
30 changes: 12 additions & 18 deletions lib/src/model/receiver.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 16 additions & 10 deletions lib/src/model/sender.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,30 +55,36 @@ abstract class _Sender with Store {

// Start current sender.
@action
void start() {
Future<bool> start() async {
if (isStarted) {
return;
_logger.i('Attempt to start an already running sender.');
return isStarted;
}

// Main backend call
_backend.startSender(receiverIP);
await _backend.startSender(receiverIP);

_isStarted = !_isStarted;
_logger.i('Sender started');
var status = await _backend.isSenderAlive();
_logger.i('Trying to start the sender. roc service status: $status');
_isStarted = status;
return _isStarted;
}

// Stop current sender.
@action
void stop() {
Future<bool> stop() async {
if (!isStarted) {
return;
_logger.i('Attempt to stop inactive sender.');
return isStarted;
}

// Main backend call
_backend.stopSender();
await _backend.stopSender();

_isStarted = !_isStarted;
_logger.i('Sender stopped');
var status = await _backend.isSenderAlive();
_logger.i('Trying to stop the sender. roc service status: $status');
_isStarted = status;
return _isStarted;
}

// Update source port value.
Expand Down
29 changes: 11 additions & 18 deletions lib/src/model/sender.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 12 additions & 11 deletions lib/src/ui/components/roc_stateful_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import '../styles/roc_button_styles.dart';
/// Roc's custom stateful button widget.
class RocStatefulButton extends StatefulWidget {
final bool _isActive;
final void Function() _inactiveFunction;
final void Function() _activeFunction;
final Future<bool> Function() _inactiveFunction;
final Future<bool> Function() _activeFunction;
final String _inactiveText;
final String _activeText;

const RocStatefulButton(
{required bool isActive,
required void Function() inactiveFunction,
required void Function() activeFunction,
required Future<bool> Function() inactiveFunction,
required Future<bool> Function() activeFunction,
required String inactiveText,
required String activeText})
: _isActive = isActive,
Expand All @@ -34,15 +34,15 @@ class RocStatefulButton extends StatefulWidget {

class _RocStatefulButtonState extends State<RocStatefulButton> {
bool _isActive;
final void Function() _inactiveFunction;
final void Function() _activeFunction;
final Future<bool> Function() _inactiveFunction;
final Future<bool> Function() _activeFunction;
final String _inactiveText;
final String _activeText;

_RocStatefulButtonState(
{required bool isActive,
required void Function() inactiveFunction,
required void Function() activeFunction,
required Future<bool> Function() inactiveFunction,
required Future<bool> Function() activeFunction,
required String inactiveText,
required String activeText})
: _isActive = isActive,
Expand All @@ -51,10 +51,11 @@ class _RocStatefulButtonState extends State<RocStatefulButton> {
_inactiveText = inactiveText,
_activeText = activeText;

void _onPressed() {
void _onPressed() async {
var result =
_isActive ? await _activeFunction() : await _inactiveFunction();
setState(() {
_isActive ? _activeFunction() : _inactiveFunction();
_isActive = !_isActive;
_isActive = result;
});
}

Expand Down

0 comments on commit a39cd68

Please sign in to comment.