Compare commits

..

No commits in common. "5bb64d98b8edf3ed42439f6fcc0d8a5b4c509cd8" and "dac6b53b2a9f0e9650fba84551586bfc1eb03242" have entirely different histories.

2 changed files with 105 additions and 290 deletions

View File

@ -1,4 +1,3 @@
import 'dart:async';
import 'dart:typed_data'; import 'dart:typed_data';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:dialer/services/call_service.dart'; import 'package:dialer/services/call_service.dart';
@ -29,58 +28,6 @@ class _CallPageState extends State<CallPage> {
bool isKeypadVisible = false; bool isKeypadVisible = false;
bool icingProtocolOk = true; bool icingProtocolOk = true;
String _typedDigits = ""; String _typedDigits = "";
Timer? _callTimer;
int _callSeconds = 0;
String _callStatus = "Calling...";
StreamSubscription<String>? _callStateSubscription;
@override
void initState() {
super.initState();
_listenToCallState();
}
@override
void dispose() {
_callTimer?.cancel();
_callStateSubscription?.cancel();
super.dispose();
}
void _listenToCallState() {
_callStateSubscription = _callService.callStateStream.listen((state) {
final eventTime = DateTime.now().millisecondsSinceEpoch;
print('CallPage: [${eventTime}ms] Call state changed to $state');
if (mounted) {
setState(() {
if (state == "active") {
_callStatus = "00:00";
_startCallTimer();
} else if (state == "disconnected" || state == "disconnecting") {
_callTimer?.cancel();
_callStatus = "Call Ended";
} else {
_callStatus = "Calling...";
}
});
}
});
}
void _startCallTimer() {
_callTimer?.cancel();
_callTimer = Timer.periodic(const Duration(seconds: 1), (timer) {
if (mounted) {
setState(() {
_callSeconds++;
final minutes = (_callSeconds ~/ 60).toString().padLeft(2, '0');
final seconds = (_callSeconds % 60).toString().padLeft(2, '0');
_callStatus = '$minutes:$seconds';
print('CallPage: [${DateTime.now().millisecondsSinceEpoch}ms] Timer updated, duration: $_callStatus');
});
}
});
}
void _addDigit(String digit) { void _addDigit(String digit) {
setState(() { setState(() {
@ -114,17 +61,16 @@ class _CallPageState extends State<CallPage> {
void _hangUp() async { void _hangUp() async {
try { try {
final hangUpStart = DateTime.now().millisecondsSinceEpoch;
print('CallPage: [${hangUpStart}ms] Initiating hangUp');
final result = await _callService.hangUpCall(context); final result = await _callService.hangUpCall(context);
print('CallPage: [${DateTime.now().millisecondsSinceEpoch}ms] Hang up result: $result'); print('CallPage: Hang up result: $result');
} catch (e) { if (result["status"] == "ended" && mounted && Navigator.canPop(context)) {
print('CallPage: [${DateTime.now().millisecondsSinceEpoch}ms] Error hanging up: $e'); Navigator.pop(context);
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text("Error hanging up: $e")),
);
} }
} catch (e) {
print("CallPage: Error hanging up: $e");
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text("Error hanging up: $e")),
);
} }
} }
@ -134,7 +80,6 @@ class _CallPageState extends State<CallPage> {
final double nameFontSize = isKeypadVisible ? 24.0 : 24.0; final double nameFontSize = isKeypadVisible ? 24.0 : 24.0;
final double statusFontSize = isKeypadVisible ? 16.0 : 16.0; final double statusFontSize = isKeypadVisible ? 16.0 : 16.0;
print('CallPage: Building UI, _callStatus: $_callStatus, route: ${ModalRoute.of(context)?.settings.name ?? "unknown"}');
return Scaffold( return Scaffold(
body: Container( body: Container(
color: Colors.black, color: Colors.black,
@ -146,7 +91,7 @@ class _CallPageState extends State<CallPage> {
child: Column( child: Column(
mainAxisSize: MainAxisSize.min, mainAxisSize: MainAxisSize.min,
children: [ children: [
const SizedBox(height: 35), SizedBox(height: 35),
ObfuscatedAvatar( ObfuscatedAvatar(
imageBytes: widget.thumbnail, imageBytes: widget.thumbnail,
radius: avatarRadius, radius: avatarRadius,
@ -189,7 +134,7 @@ class _CallPageState extends State<CallPage> {
fontSize: statusFontSize, color: Colors.white70), fontSize: statusFontSize, color: Colors.white70),
), ),
Text( Text(
_callStatus, 'Calling...',
style: TextStyle( style: TextStyle(
fontSize: statusFontSize, color: Colors.white70), fontSize: statusFontSize, color: Colors.white70),
), ),
@ -222,7 +167,8 @@ class _CallPageState extends State<CallPage> {
IconButton( IconButton(
padding: EdgeInsets.zero, padding: EdgeInsets.zero,
onPressed: _toggleKeypad, onPressed: _toggleKeypad,
icon: const Icon(Icons.close, color: Colors.white), icon:
const Icon(Icons.close, color: Colors.white),
), ),
], ],
), ),
@ -301,11 +247,8 @@ class _CallPageState extends State<CallPage> {
children: [ children: [
IconButton( IconButton(
onPressed: _toggleKeypad, onPressed: _toggleKeypad,
icon: const Icon( icon: const Icon(Icons.dialpad,
Icons.dialpad, color: Colors.white, size: 32),
color: Colors.white,
size: 32,
),
), ),
const Text( const Text(
'Keypad', 'Keypad',
@ -347,17 +290,12 @@ class _CallPageState extends State<CallPage> {
children: [ children: [
IconButton( IconButton(
onPressed: () {}, onPressed: () {},
icon: const Icon( icon: const Icon(Icons.person_add,
Icons.person_add, color: Colors.white, size: 32),
color: Colors.white,
size: 32,
),
),
const Text(
'Add Contact',
style: TextStyle(
color: Colors.white, fontSize: 14),
), ),
const Text('Add Contact',
style: TextStyle(
color: Colors.white, fontSize: 14)),
], ],
), ),
Column( Column(
@ -365,17 +303,12 @@ class _CallPageState extends State<CallPage> {
children: [ children: [
IconButton( IconButton(
onPressed: () {}, onPressed: () {},
icon: const Icon( icon: const Icon(Icons.sim_card,
Icons.sim_card, color: Colors.white, size: 32),
color: Colors.white,
size: 32,
),
),
const Text(
'Change SIM',
style: TextStyle(
color: Colors.white, fontSize: 14),
), ),
const Text('Change SIM',
style: TextStyle(
color: Colors.white, fontSize: 14)),
], ],
), ),
], ],

View File

@ -1,4 +1,3 @@
import 'dart:async';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import '../features/call/call_page.dart'; import '../features/call/call_page.dart';
@ -8,92 +7,33 @@ import '../services/contact_service.dart';
class CallService { class CallService {
static const MethodChannel _channel = MethodChannel('call_service'); static const MethodChannel _channel = MethodChannel('call_service');
static String? currentPhoneNumber; static String? currentPhoneNumber;
static String? currentDisplayName; static String? currentDisplayName;
static Uint8List? currentThumbnail; static Uint8List? currentThumbnail;
static bool _isCallPageVisible = false; static bool _isCallPageVisible = false;
static String? _currentCallState;
static Map<String, dynamic>? _pendingCall; static Map<String, dynamic>? _pendingCall;
static bool wasPhoneLocked = false; static bool wasPhoneLocked = false;
static String? _activeCallNumber;
static bool _isNavigating = false;
final ContactService _contactService = ContactService(); final ContactService _contactService = ContactService();
final _callStateController = StreamController<String>.broadcast();
static final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>(); static final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
Stream<String> get callStateStream => _callStateController.stream;
CallService() { CallService() {
_channel.setMethodCallHandler((call) async { _channel.setMethodCallHandler((call) async {
print('CallService: Handling method call: ${call.method}, with args: ${call.arguments}'); print('CallService: Received method ${call.method} with args ${call.arguments}');
switch (call.method) { switch (call.method) {
case "callAdded": case "callAdded":
final phoneNumber = call.arguments["callId"] as String?; final phoneNumber = call.arguments["callId"] as String;
final state = call.arguments["state"] as String?; final state = call.arguments["state"] as String;
if (phoneNumber == null || state == null) { currentPhoneNumber = phoneNumber.replaceFirst('tel:', '');
print('CallService: Invalid callAdded args: $call.arguments'); await _fetchContactInfo(currentPhoneNumber!);
return; print('CallService: Call added, number: $currentPhoneNumber, state: $state');
} _handleCallState(state);
final decodedPhoneNumber = Uri.decodeComponent(phoneNumber.replaceFirst('tel:', ''));
print('CallService: Decoded phone number: $decodedPhoneNumber');
if (_activeCallNumber != decodedPhoneNumber) {
currentPhoneNumber = decodedPhoneNumber;
if (currentDisplayName == null || currentDisplayName == currentPhoneNumber) {
await _fetchContactInfo(decodedPhoneNumber);
}
}
print('CallService: Call added, number: $currentPhoneNumber, displayName: $currentDisplayName, state: $state');
_callStateController.add(state);
if (state == "ringing") {
_handleIncomingCall(decodedPhoneNumber);
} else {
_navigateToCallPage();
}
break; break;
case "callStateChanged": case "callStateChanged":
final state = call.arguments["state"] as String?; final state = call.arguments["state"] as String;
wasPhoneLocked = call.arguments["wasPhoneLocked"] as bool? ?? false; wasPhoneLocked = call.arguments["wasPhoneLocked"] as bool? ?? false;
if (state == null) {
print('CallService: Invalid callStateChanged args: $call.arguments');
return;
}
print('CallService: State changed to $state, wasPhoneLocked: $wasPhoneLocked'); print('CallService: State changed to $state, wasPhoneLocked: $wasPhoneLocked');
_callStateController.add(state); _handleCallState(state);
if (state == "disconnected" || state == "disconnecting") {
_closeCallPage();
if (wasPhoneLocked) {
await _channel.invokeMethod("callEndedFromFlutter");
}
_activeCallNumber = null;
} else if (state == "active" || state == "dialing") {
final phoneNumber = call.arguments["callId"] as String?;
if (phoneNumber != null && _activeCallNumber != Uri.decodeComponent(phoneNumber.replaceFirst('tel:', ''))) {
currentPhoneNumber = Uri.decodeComponent(phoneNumber.replaceFirst('tel:', ''));
if (currentDisplayName == null || currentDisplayName == currentPhoneNumber) {
await _fetchContactInfo(currentPhoneNumber!);
}
} else if (currentPhoneNumber != null && _activeCallNumber != currentPhoneNumber) {
if (currentDisplayName == null || currentDisplayName == currentPhoneNumber) {
await _fetchContactInfo(currentPhoneNumber!);
}
} else {
print('CallService: Skipping fetch, active call: $_activeCallNumber, current: $currentPhoneNumber, displayName: $currentDisplayName');
}
_navigateToCallPage();
} else if (state == "ringing") {
final phoneNumber = call.arguments["callId"] as String?;
if (phoneNumber == null) {
print('CallService: Invalid ringing callId: $call.arguments');
return;
}
final decodedPhoneNumber = Uri.decodeComponent(phoneNumber.replaceFirst('tel:', ''));
if (_activeCallNumber != decodedPhoneNumber) {
currentPhoneNumber = decodedPhoneNumber;
if (currentDisplayName == null || currentDisplayName == currentPhoneNumber) {
await _fetchContactInfo(decodedPhoneNumber);
}
}
_handleIncomingCall(decodedPhoneNumber);
}
break; break;
case "callEnded": case "callEnded":
case "callRemoved": case "callRemoved":
@ -101,64 +41,40 @@ class CallService {
print('CallService: Call ended/removed, wasPhoneLocked: $wasPhoneLocked'); print('CallService: Call ended/removed, wasPhoneLocked: $wasPhoneLocked');
_closeCallPage(); _closeCallPage();
if (wasPhoneLocked) { if (wasPhoneLocked) {
await _channel.invokeMethod("callEndedFromFlutter"); _channel.invokeMethod("callEndedFromFlutter");
} }
currentPhoneNumber = null; currentPhoneNumber = null;
currentDisplayName = null; currentDisplayName = null;
currentThumbnail = null; currentThumbnail = null;
_activeCallNumber = null; _currentCallState = null;
break; break;
case "incomingCallFromNotification": case "incomingCallFromNotification":
final phoneNumber = call.arguments["phoneNumber"] as String?; final phoneNumber = call.arguments["phoneNumber"] as String;
wasPhoneLocked = call.arguments["wasPhoneLocked"] as bool? ?? false; wasPhoneLocked = call.arguments["wasPhoneLocked"] as bool? ?? false;
if (phoneNumber == null) { currentPhoneNumber = phoneNumber;
print('CallService: Invalid incomingCallFromNotification args: $call.arguments'); await _fetchContactInfo(currentPhoneNumber!);
return; print('CallService: Incoming call from notification: $phoneNumber, wasPhoneLocked: $wasPhoneLocked');
} _handleIncomingCall(phoneNumber);
final decodedPhoneNumber = Uri.decodeComponent(phoneNumber);
if (_activeCallNumber != decodedPhoneNumber) {
currentPhoneNumber = decodedPhoneNumber;
if (currentDisplayName == null || currentDisplayName == currentPhoneNumber) {
await _fetchContactInfo(decodedPhoneNumber);
}
}
print('CallService: Incoming call from notification: $decodedPhoneNumber, displayName: $currentDisplayName, wasPhoneLocked: $wasPhoneLocked');
_handleIncomingCall(decodedPhoneNumber);
break;
case "audioStateChanged":
final route = call.arguments["route"] as int?;
print('CallService: Audio state changed, route: $route');
break; break;
} }
}); });
} }
void dispose() {
_callStateController.close();
}
Future<void> _fetchContactInfo(String phoneNumber) async { Future<void> _fetchContactInfo(String phoneNumber) async {
try { try {
print('CallService: Fetching contact info for $phoneNumber');
final contacts = await _contactService.fetchContacts(); final contacts = await _contactService.fetchContacts();
print('CallService: Retrieved ${contacts.length} contacts');
final normalizedPhoneNumber = _normalizePhoneNumber(phoneNumber); final normalizedPhoneNumber = _normalizePhoneNumber(phoneNumber);
print('CallService: Normalized phone number: $normalizedPhoneNumber');
for (var contact in contacts) { for (var contact in contacts) {
for (var phone in contact.phones) { for (var phone in contact.phones) {
final normalizedContactNumber = _normalizePhoneNumber(phone.number); if (_normalizePhoneNumber(phone.number) == normalizedPhoneNumber) {
print('CallService: Comparing $normalizedPhoneNumber with contact number $normalizedContactNumber');
if (normalizedContactNumber == normalizedPhoneNumber) {
currentDisplayName = contact.displayName; currentDisplayName = contact.displayName;
currentThumbnail = contact.thumbnail; currentThumbnail = contact.thumbnail;
print('CallService: Found contact - displayName: $currentDisplayName, thumbnail: ${currentThumbnail != null ? "present" : "null"}');
return; return;
} }
} }
} }
currentDisplayName = phoneNumber; currentDisplayName = phoneNumber;
currentThumbnail = null; currentThumbnail = null;
print('CallService: No contact match, using phoneNumber as displayName: $currentDisplayName');
} catch (e) { } catch (e) {
print('CallService: Error fetching contact info: $e'); print('CallService: Error fetching contact info: $e');
currentDisplayName = phoneNumber; currentDisplayName = phoneNumber;
@ -167,16 +83,10 @@ class CallService {
} }
String _normalizePhoneNumber(String number) { String _normalizePhoneNumber(String number) {
return number.replaceAll(RegExp(r'[\s\-\(\)]'), '').replaceFirst(RegExp(r'^\+'), ''); return number.replaceAll(RegExp(r'[\s\-\(\)]'), '');
} }
void _handleIncomingCall(String phoneNumber) { void _handleIncomingCall(String phoneNumber) {
if (_activeCallNumber == phoneNumber && _isCallPageVisible) {
print('CallService: Incoming call for $phoneNumber already active, skipping');
return;
}
_activeCallNumber = phoneNumber;
final context = navigatorKey.currentContext; final context = navigatorKey.currentContext;
if (context == null) { if (context == null) {
print('CallService: Context is null, queuing incoming call: $phoneNumber'); print('CallService: Context is null, queuing incoming call: $phoneNumber');
@ -187,64 +97,53 @@ class CallService {
} }
} }
Future<void> _checkPendingCall() async { void _checkPendingCall() {
if (_pendingCall == null) { if (_pendingCall != null) {
print('CallService: No pending call to process'); final context = navigatorKey.currentContext;
return; if (context != null) {
} print('CallService: Processing queued call: ${_pendingCall!["phoneNumber"]}');
currentPhoneNumber = _pendingCall!["phoneNumber"];
final phoneNumber = _pendingCall!["phoneNumber"]; _navigateToIncomingCallPage(context);
if (_activeCallNumber == phoneNumber && _isCallPageVisible) { _pendingCall = null;
print('CallService: Pending call for $phoneNumber already active, clearing'); } else {
_pendingCall = null; print('CallService: Context still null, retrying...');
return; Future.delayed(Duration(milliseconds: 500), () => _checkPendingCall());
} }
final context = navigatorKey.currentContext;
if (context != null) {
print('CallService: Processing queued call: $phoneNumber');
currentPhoneNumber = phoneNumber;
_activeCallNumber = phoneNumber;
await _fetchContactInfo(phoneNumber);
_navigateToIncomingCallPage(context);
_pendingCall = null;
} else {
print('CallService: Context still null, retrying...');
Future.delayed(Duration(milliseconds: 500), () => _checkPendingCall());
} }
} }
void _navigateToCallPage() { void _handleCallState(String state) {
if (_isNavigating) {
print('CallService: Navigation already in progress, skipping');
return;
}
_isNavigating = true;
final context = navigatorKey.currentContext; final context = navigatorKey.currentContext;
if (context == null) { if (context == null) {
print('CallService: Cannot navigate to CallPage, context is null'); print('CallService: Navigator context is null, cannot navigate');
_isNavigating = false;
return; return;
} }
final currentRoute = ModalRoute.of(context)?.settings.name ?? 'unknown'; if (_currentCallState == state) {
print('CallService: Navigating to CallPage, Visible: $_isCallPageVisible, Current Route: $currentRoute, Active Call: $_activeCallNumber, DisplayName: $currentDisplayName'); print('CallService: State $state already handled, skipping');
if (_isCallPageVisible && currentRoute == '/call' && _activeCallNumber == currentPhoneNumber) {
print('CallService: CallPage already visible for $_activeCallNumber, skipping navigation');
_isNavigating = false;
return; return;
} }
if (_isCallPageVisible && currentRoute == '/incoming_call' && _activeCallNumber == currentPhoneNumber) { _currentCallState = state;
print('CallService: Popping IncomingCallPage before navigating to CallPage');
if (state == "disconnected" || state == "disconnecting") {
_closeCallPage();
} else if (state == "active" || state == "dialing") {
_navigateToCallPage(context);
} else if (state == "ringing") {
_navigateToIncomingCallPage(context);
}
}
void _navigateToCallPage(BuildContext context) {
final currentRoute = ModalRoute.of(context)?.settings.name;
print('CallService: Navigating to CallPage. Visible: $_isCallPageVisible, Current Route: $currentRoute');
if (_isCallPageVisible && currentRoute == '/call') {
print('CallService: CallPage already visible, skipping navigation');
return;
}
if (_isCallPageVisible && currentRoute == '/incoming_call') {
print('CallService: Replacing IncomingCallPage with CallPage');
Navigator.pop(context); Navigator.pop(context);
_isCallPageVisible = false;
} }
if (currentPhoneNumber == null) {
print('CallService: Cannot navigate to CallPage, currentPhoneNumber is null');
_isNavigating = false;
return;
}
_activeCallNumber = currentPhoneNumber;
Navigator.pushReplacement( Navigator.pushReplacement(
context, context,
MaterialPageRoute( MaterialPageRoute(
@ -256,35 +155,21 @@ class CallService {
), ),
), ),
).then((_) { ).then((_) {
print('CallService: CallPage popped');
_isCallPageVisible = false; _isCallPageVisible = false;
_isNavigating = false;
print('CallService: CallPage popped, _isCallPageVisible set to false');
}); });
_isCallPageVisible = true; _isCallPageVisible = true;
} }
void _navigateToIncomingCallPage(BuildContext context) { void _navigateToIncomingCallPage(BuildContext context) {
if (_isNavigating) { final currentRoute = ModalRoute.of(context)?.settings.name;
print('CallService: Navigation already in progress, skipping'); print('CallService: Navigating to IncomingCallPage. Visible: $_isCallPageVisible, Current Route: $currentRoute');
return; if (_isCallPageVisible && currentRoute == '/incoming_call') {
} print('CallService: IncomingCallPage already visible, skipping navigation');
_isNavigating = true;
final currentRoute = ModalRoute.of(context)?.settings.name ?? 'unknown';
print('CallService: Navigating to IncomingCallPage, Visible: $_isCallPageVisible, Current Route: $currentRoute, Active Call: $_activeCallNumber, DisplayName: $currentDisplayName');
if (_isCallPageVisible && currentRoute == '/incoming_call' && _activeCallNumber == currentPhoneNumber) {
print('CallService: IncomingCallPage already visible for $_activeCallNumber, skipping navigation');
_isNavigating = false;
return; return;
} }
if (_isCallPageVisible && currentRoute == '/call') { if (_isCallPageVisible && currentRoute == '/call') {
print('CallService: CallPage visible, not showing IncomingCallPage'); print('CallService: CallPage visible, not showing IncomingCallPage');
_isNavigating = false;
return;
}
if (currentPhoneNumber == null) {
print('CallService: Cannot navigate to IncomingCallPage, currentPhoneNumber is null');
_isNavigating = false;
return; return;
} }
Navigator.push( Navigator.push(
@ -298,9 +183,8 @@ class CallService {
), ),
), ),
).then((_) { ).then((_) {
print('CallService: IncomingCallPage popped');
_isCallPageVisible = false; _isCallPageVisible = false;
_isNavigating = false;
print('CallService: IncomingCallPage popped, _isCallPageVisible set to false');
}); });
_isCallPageVisible = true; _isCallPageVisible = true;
} }
@ -311,72 +195,70 @@ class CallService {
print('CallService: Cannot close page, context is null'); print('CallService: Cannot close page, context is null');
return; return;
} }
print('CallService: Closing call page, _isCallPageVisible: $_isCallPageVisible'); print('CallService: Attempting to close call page. Visible: $_isCallPageVisible');
if (!_isCallPageVisible) {
print('CallService: CallPage not visible, skipping pop');
return;
}
if (Navigator.canPop(context)) { if (Navigator.canPop(context)) {
print('CallService: Popping call page'); print('CallService: Popping CallPage. Current Route: ${ModalRoute.of(context)?.settings.name}');
Navigator.pop(context); Navigator.pop(context);
_isCallPageVisible = false; _isCallPageVisible = false;
} else { } else {
print('CallService: No page to pop'); print('CallService: Cannot pop, no routes to pop');
} }
_activeCallNumber = null;
} }
Future<void> makeGsmCall(
Future<Map<String, dynamic>> makeGsmCall(
BuildContext context, { BuildContext context, {
required String phoneNumber, required String phoneNumber,
String? displayName, String? displayName,
Uint8List? thumbnail, Uint8List? thumbnail,
}) async { }) async {
try { try {
if (_activeCallNumber == phoneNumber && _isCallPageVisible) {
print('CallService: Call already active for $phoneNumber, skipping');
return {"status": "already_active", "message": "Call already in progress"};
}
currentPhoneNumber = phoneNumber; currentPhoneNumber = phoneNumber;
currentDisplayName = displayName ?? phoneNumber; currentDisplayName = displayName ?? phoneNumber;
currentThumbnail = thumbnail; currentThumbnail = thumbnail;
if (displayName == null || thumbnail == null) { if (displayName == null || thumbnail == null) {
await _fetchContactInfo(phoneNumber); await _fetchContactInfo(phoneNumber);
} }
print('CallService: Making GSM call to $phoneNumber, displayName: $currentDisplayName'); print('CallService: Making GSM call to $phoneNumber');
final result = await _channel.invokeMethod('makeGsmCall', {"phoneNumber": phoneNumber}); final result = await _channel.invokeMethod('makeGsmCall', {"phoneNumber": phoneNumber});
print('CallService: makeGsmCall result: $result'); print('CallService: makeGsmCall result: $result');
final resultMap = Map<String, dynamic>.from(result as Map); if (result["status"] != "calling") {
if (resultMap["status"] != "calling") {
ScaffoldMessenger.of(context).showSnackBar( ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text("Failed to initiate call")), SnackBar(content: Text("Failed to initiate call")),
); );
return;
} }
return resultMap; _handleCallState("dialing");
} catch (e) { } catch (e) {
print("CallService: Error making call: $e"); print("CallService: Error making call: $e");
ScaffoldMessenger.of(context).showSnackBar( ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text("Error making call: $e")), SnackBar(content: Text("Error making call: $e")),
); );
return {"status": "error", "message": e.toString()}; rethrow;
} }
} }
Future<Map<String, dynamic>> hangUpCall(BuildContext context) async { Future<void> hangUpCall(BuildContext context) async {
try { try {
print('CallService: Hanging up call'); print('CallService: Hanging up call');
final result = await _channel.invokeMethod('hangUpCall'); final result = await _channel.invokeMethod('hangUpCall');
print('CallService: hangUpCall result: $result'); print('CallService: hangUpCall result: $result');
final resultMap = Map<String, dynamic>.from(result as Map); if (result["status"] != "ended") {
if (resultMap["status"] != "ended") {
ScaffoldMessenger.of(context).showSnackBar( ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text("Failed to end call")), SnackBar(content: Text("Failed to end call")),
); );
} else {
_closeCallPage();
} }
return resultMap;
} catch (e) { } catch (e) {
print("CallService: Error hanging up call: $e"); print("CallService: Error hanging up call: $e");
ScaffoldMessenger.of(context).showSnackBar( ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text("Error hanging up call: $e")), SnackBar(content: Text("Error hanging up call: $e")),
); );
return {"status": "error", "message": e.toString()}; rethrow;
} }
} }
} }