Line data Source code
1 : import 'package:flutter/services.dart';
2 : import 'package:flutter_chrome_cast/enums/connection_state.dart';
3 : import 'package:flutter_chrome_cast/entities/cast_session.dart';
4 : import 'package:flutter_chrome_cast/entities/cast_device.dart';
5 : import 'package:flutter_chrome_cast/models/android/cast_device.dart';
6 : import 'package:flutter_chrome_cast/models/android/cast_session.dart';
7 : import 'package:rxdart/subjects.dart';
8 :
9 : import 'cast_session_manager_platform.dart';
10 :
11 : /// Android-specific implementation of Google Cast session manager functionality.
12 : class GoogleCastSessionManagerAndroidMethodChannel
13 : implements GoogleCastSessionManagerPlatformInterface {
14 : /// Creates a new Android session manager method channel.
15 0 : GoogleCastSessionManagerAndroidMethodChannel() {
16 0 : _channel.setMethodCallHandler(_onMethodCallHandler);
17 : }
18 : final _channel =
19 : const MethodChannel('com.felnanuke.google_cast.session_manager');
20 :
21 : final _currentSessionStreamController = BehaviorSubject<GoogleCastSession?>()
22 : ..add(null);
23 :
24 0 : @override
25 : GoogleCastConnectState get connectionState =>
26 0 : _currentSessionStreamController.value?.connectionState ??
27 : GoogleCastConnectState.disconnected;
28 :
29 0 : @override
30 : GoogleCastSession? get currentSession =>
31 0 : _currentSessionStreamController.value;
32 :
33 0 : @override
34 : Stream<GoogleCastSession?> get currentSessionStream =>
35 0 : _currentSessionStreamController.stream;
36 :
37 0 : @override
38 : Future<bool> endSession() async {
39 0 : return await _channel.invokeMethod('endSession');
40 : }
41 :
42 0 : @override
43 : Future<bool> endSessionAndStopCasting() async {
44 0 : return await _channel.invokeMethod('endSessionAndStopCasting');
45 : }
46 :
47 0 : @override
48 : bool get hasConnectedSession =>
49 0 : _currentSessionStreamController.value?.connectionState ==
50 : GoogleCastConnectState.connected;
51 :
52 0 : @override
53 : Future<void> setDefaultSessionOptions() {
54 0 : throw UnimplementedError('Only works in IOS');
55 : }
56 :
57 0 : @override
58 : Future<bool> startSessionWithDevice(GoogleCastDevice device) async {
59 : device as GoogleCastAndroidDevice;
60 0 : return (await _channel.invokeMethod(
61 : 'startSessionWithDeviceId',
62 0 : device.deviceID,
63 0 : )) ==
64 : true;
65 : }
66 :
67 0 : @override
68 : Future<bool> startSessionWithOpenURLOptions() {
69 : // TODO: implement startSessionWithOpenURLOptions
70 0 : throw UnimplementedError();
71 : }
72 :
73 0 : @override
74 : Future<bool> suspendSessionWithReason() {
75 : // TODO: implement suspendSessionWithReason
76 0 : throw UnimplementedError();
77 : }
78 :
79 0 : Future _onMethodCallHandler(MethodCall call) async {
80 0 : switch (call.method) {
81 0 : case "onSessionChanged":
82 0 : _onSessionChanged(call.arguments);
83 : return;
84 : default:
85 : }
86 : }
87 :
88 0 : void _onSessionChanged(dynamic arguments) {
89 : try {
90 : if (arguments == null) {
91 0 : _currentSessionStreamController.add(null);
92 : return;
93 : }
94 0 : final map = Map<String, dynamic>.from(arguments);
95 0 : final session = GoogleCastSessionAndroid.fromMap(map);
96 0 : _currentSessionStreamController.add(session);
97 : } catch (e) {
98 : rethrow;
99 : }
100 : }
101 :
102 0 : @override
103 : void setDeviceVolume(double value) {
104 0 : _channel.invokeMethod('setStreamVolume', value);
105 : }
106 : }
|