elxconnect 0.0.2-dev.4
elxconnect: ^0.0.2-dev.4 copied to clipboard
A new platform for calling.
example/lib/main.dart
import 'dart:ffi';
import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:developer';
import 'package:flutter/services.dart';
import 'package:elxconnect/elxconnect.dart';
final GlobalKey<NavigatorState> navigatorKey = new GlobalKey<NavigatorState>();
bool autoPutOnConference = false;
final List<String> onGoingCalls = <String>[];
_PhoneCallScreenContainer? _phoneCallScreenContainer;
String? globalCallId = "";
void main() {
runApp(MyApp());
ElxconnectCallService.registerOnCallAddedListener(
(String callId, Map call) async {
log("A new call is added with id $callId, caller name ${call['details']['callerDisplayName']} map $call");
if (_phoneCallScreenContainer != null) {
_phoneCallScreenContainer!.setState(() {
onGoingCalls.add(callId);
});
} else {
onGoingCalls.add(callId);
}
if (onGoingCalls.length > 1) {
log("The call is ${call['isConference']}, ${call['isConference'] == true}, was removed");
if (call['isConference']) {
globalCallId = callId;
}
return;
}
globalCallId = callId;
navigatorKey.currentState!.push(MaterialPageRoute(
builder: (context) => PhoneCallScreen(callId: callId)));
/*Navigator.pushReplacement(navigatorKey.currentContext!,
MaterialPageRoute(builder: (context) => PhoneCallScreen()));*/
});
ElxconnectCallService.registerOnCallRemovedListener(
(String callId, Map call) {
log("The call is added with id $callId, was removed");
onGoingCalls.remove(callId);
navigatorKey.currentState!.pop();
});
ElxconnectCallService.registerOnCallStateChangeListener(
(Map call, int state) {
log("State changed.........abs....................... $state");
});
ElxconnectCallService.registerOnOutgoingConnectingListener(
(String callId, Map call) {
log("SpecificDirection: The outgoing call with id $callId, is connecting");
});
ElxconnectCallService.registerOnOutgoingDialedListener(
(String callId, Map call) {
log("SpecificDirection: The outgoing call with id $callId, was dialed");
});
ElxconnectCallService.registerOnOutgoingAnsweredListener(
(String callId, Map call) {
log("SpecificDirection: The outgoing call with id $callId, was answerd");
});
ElxconnectCallService.registerOnOutgoingActiveListener(
(String callId, Map call) {
log("SpecificDirection: The outgoing call with id $callId, was activated");
});
ElxconnectCallService.registerOnOutgoingHoldingListener(
(String callId, Map call) {
log("SpecificDirection: The outgoing call with id $callId, was put on hold");
});
ElxconnectCallService.registerOnOutgoingUnholdingListener(
(String callId, Map call) {
log("SpecificDirection: The outgoing call with id $callId, was re activated");
});
ElxconnectCallService.registerOnOutgoingDisconectingListener(
(String callId, Map call) {
log("SpecificDirection: The outgoing call with id $callId, is currently disconnecting");
});
ElxconnectCallService.registerOnOutgoingDisconectedListener(
(String callId, Map call) {
log("SpecificDirection: The outgoing call with id $callId, was ended");
});
ElxconnectCallService.registerOnIncomingConectingListener(
(String callId, Map call) {
log("SpecificDirection: The incomming call with id $callId, is connecting");
});
ElxconnectCallService.registerOnIncomingConectingListener(
(String callId, Map call) {
log("SpecificDirection: The incomming call with id $callId, starts ringing");
});
ElxconnectCallService.registerOnIncomingAnsweredListener(
(String callId, Map call) {
log("SpecificDirection: The incomming call with id $callId, was answerd");
});
ElxconnectCallService.registerOnIncomingActiveListener(
(String callId, Map call) {
log("SpecificDirection: The incomming call with id $callId, was activated");
});
ElxconnectCallService.registerOnIncomingHoldingListener(
(String callId, Map call) {
log("SpecificDirection: The incomming call with id $callId, was put on hold");
});
ElxconnectCallService.registerOnIncomingUnholdingListener(
(String callId, Map call) {
log("SpecificDirection: The incomming call with id $callId, was re activated");
});
ElxconnectCallService.registerOnIncomingDisconectingListener(
(String callId, Map call) {
log("SpecificDirection: The incomming call with id $callId, is currently disconnecting");
});
ElxconnectCallService.registerOnIncomingDisconectedListener(
(String callId, Map call) {
log("SpecificDirection: The incomming call with id $callId, was ended");
});
}
void callCallback(Object call, int state) {
log("Call: $call, State: $state");
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
List<String> phonePermissions = <String>[
ElxconnectPhonePermission.PERMISSION_READ_CONTACTS,
ElxconnectPhonePermission.PERMISSION_CALL_PHONE,
ElxconnectPhonePermission.PERMISSION_WRITE_CONTACTS,
ElxconnectPhonePermission.PERMISSION_READ_CALL_LOG,
ElxconnectPhonePermission.PERMISSION_WRITE_CALL_LOG,
ElxconnectPhonePermission.PERMISSION_READ_PHONE_STATE,
ElxconnectPhonePermission.PERMISSION_VIBRATE,
ElxconnectPhonePermission.PERMISSION_ACCESS_FINE_LOCATION
];
@override
void initState() {
super.initState();
initPlatformState();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String packageName =
await Elxconnect.getPackageName ?? 'Unknown platform version';
bool arePermissionGranted =
await ElxconnectPhonePermission.arePermissionsGranted(
phonePermissions) ??
false;
log("full activity name ${await Elxconnect.getPackageActivityName}");
if (!arePermissionGranted) {
ElxconnectPhonePermission.requestPermissions(phonePermissions);
ElxconnectPhonePermission.setAppAsDefaultPhoneApp(packageName);
}
log("Number of Sim ${await Elxconnect.getActiveModemCount}");
log("Registered Cell Subscription ${await Elxconnect.getCellSubscriptions}");
if (!mounted) return;
setState(() {});
}
TextEditingController phoneNumberController =
new TextEditingController(text: "+23408132956138");
@override
Widget build(BuildContext context) {
return MaterialApp(
navigatorKey: navigatorKey,
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: ListView(
padding: EdgeInsets.only(left: 20, right: 20, top: 15),
children: [
TextFormField(
controller: phoneNumberController,
decoration: new InputDecoration.collapsed(
hintText: 'Enter phone number')),
Padding(
padding: const EdgeInsets.only(top: 15.0),
child: TextButton(
style: TextButton.styleFrom(
padding: const EdgeInsets.all(16.0),
backgroundColor: Colors.blueAccent,
primary: Colors.white,
textStyle: const TextStyle(fontSize: 20),
),
onPressed: () {
if (phoneNumberController.text.startsWith("*") ||
phoneNumberController.text.startsWith("#")) {
ElxconnectCall.dialUssd(phoneNumberController.text);
} else {
ElxconnectCall.dialNumber(phoneNumberController.text);
}
},
child: const Text('Call Number'),
),
)
]),
),
);
}
}
// ignore: must_be_immutable
class PhoneCallScreen extends StatefulWidget {
final String? callId;
PhoneCallScreen({this.callId});
@override
_PhoneCallScreenContainer createState() {
return _PhoneCallScreenContainer(callId: callId);
}
}
class _PhoneCallScreenContainer extends State<PhoneCallScreen> {
TextEditingController holdCallController =
new TextEditingController(text: "Hold Call");
TextEditingController conferenceNumberController =
new TextEditingController(text: "+2348177193066");
final String? callId;
var holdCallText = "Hold call";
_PhoneCallScreenContainer({this.callId});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text('Call on going')),
body: ListView(
padding: EdgeInsets.only(left: 20, right: 20, top: 15),
children: [
Padding(
padding: const EdgeInsets.only(top: 15.0, bottom: 30.0),
child: TextButton(
style: TextButton.styleFrom(
padding: const EdgeInsets.all(16.0),
backgroundColor: Colors.blueAccent,
primary: Colors.white,
textStyle: const TextStyle(fontSize: 20),
),
onPressed: () {
if (onGoingCalls.length > 1) {
log("SpecificDirection: The call is added with id ${onGoingCalls[1]}, was answered so putting in conference with ${onGoingCalls[0]}");
ElxconnectCall.conference(onGoingCalls[0], onGoingCalls[1]);
ElxconnectCall.mergeConference(onGoingCalls[0]);
}
},
child: const Text('Merge Calls/Start conference'),
),
),
Padding(
padding: const EdgeInsets.only(top: 15.0, bottom: 15.0),
child: ListView.builder(
shrinkWrap: true,
padding: const EdgeInsets.all(20.0),
itemCount: onGoingCalls.length,
itemBuilder: (BuildContext context, int index) {
return Container(
height: 50,
margin: EdgeInsets.all(2),
child: Center(
child: Text(
'${onGoingCalls[index]} ',
style: TextStyle(fontSize: 18),
)));
},
),
),
Padding(
padding: const EdgeInsets.only(top: 15.0),
child: TextButton(
style: TextButton.styleFrom(
padding: const EdgeInsets.all(16.0),
backgroundColor: Colors.blueAccent,
primary: Colors.white,
textStyle: const TextStyle(fontSize: 20),
),
onPressed: () {
log("To answer call from $callId");
ElxconnectCall.answerCall(callId!);
},
child: const Text('Answer call'),
),
),
Padding(
padding: const EdgeInsets.only(top: 15.0),
child: TextButton(
style: TextButton.styleFrom(
padding: const EdgeInsets.all(16.0),
backgroundColor: Colors.blueAccent,
primary: Colors.white,
textStyle: const TextStyle(fontSize: 20),
),
onPressed: () {
log("To handup call from $globalCallId");
ElxconnectCall.hangupCall(globalCallId!);
},
child: const Text('Hangup call'),
),
),
Padding(
padding: const EdgeInsets.only(top: 15.0),
child: TextButton(
style: TextButton.styleFrom(
padding: const EdgeInsets.all(16.0),
backgroundColor: Colors.blueAccent,
primary: Colors.white,
textStyle: const TextStyle(fontSize: 20),
),
onPressed: () {
if (holdCallText == "Hold call") {
log("To hold call from $callId");
setState(() => holdCallText = "Un Hold call");
ElxconnectCall.hold(callId!);
} else {
log("To unhold call from $callId");
setState(() => holdCallText = "Hold call");
ElxconnectCall.unhold(callId!);
}
},
child: Text(holdCallText),
),
),
TextField(
controller: conferenceNumberController,
decoration: new InputDecoration.collapsed(
hintText: 'Enter phone number to add to conference')),
Padding(
padding: const EdgeInsets.only(top: 15.0),
child: TextButton(
style: TextButton.styleFrom(
padding: const EdgeInsets.all(16.0),
backgroundColor: Colors.blueAccent,
primary: Colors.white,
textStyle: const TextStyle(fontSize: 20),
),
onPressed: () {
log("To add number to conference from $callId new join ${conferenceNumberController.text}");
_phoneCallScreenContainer = this;
ElxconnectCall.dialNumber(conferenceNumberController.text);
conferenceNumberController.text = "";
},
child: const Text('Add Call'),
),
),
]),
));
}
}