Commit 4c359f3d authored by Sylvain Berfini's avatar Sylvain Berfini 🎩

Improved demo app

parent 832e4fc4
Pipeline #235 failed with stage
in 0 seconds
......@@ -13,6 +13,7 @@ using System.Collections.Generic;
using Xamarin.Forms;
using Android.Views;
using Android.Widget;
using System.Security.AccessControl;
namespace Xamarin.Droid
{
......@@ -43,16 +44,11 @@ namespace Xamarin.Droid
AssetManager assets = Assets;
string path = FilesDir.AbsolutePath;
string rc_path = path + "/default_rc";
using (var br = new BinaryReader(Assets.Open("linphonerc_default")))
if (!File.Exists(rc_path))
{
using (var bw = new BinaryWriter(new FileStream(rc_path, FileMode.Create)))
using (StreamReader sr = new StreamReader(assets.Open("linphonerc_default")))
{
byte[] buffer = new byte[2048];
int length = 0;
while ((length = br.Read(buffer, 0, buffer.Length)) > 0)
{
bw.Write(buffer, 0, length);
}
File.WriteAllText(rc_path, sr.ReadToEnd());
}
}
......
#pragma warning disable 1591
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.42000
// Ce code a été généré par un outil.
// Version du runtime :4.0.30319.42000
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// Les modifications apportées à ce fichier peuvent provoquer un comportement incorrect et seront perdues si
// le code est régénéré.
// </auto-generated>
//------------------------------------------------------------------------------
......@@ -2342,9 +2342,27 @@ namespace Xamarin.Droid
// aapt resource value: 0x7f020053
public const int avd_hide_password = 2130837587;
// aapt resource value: 0x7f02012f
public const int avd_hide_password_1 = 2130837807;
// aapt resource value: 0x7f020130
public const int avd_hide_password_2 = 2130837808;
// aapt resource value: 0x7f020131
public const int avd_hide_password_3 = 2130837809;
// aapt resource value: 0x7f020054
public const int avd_show_password = 2130837588;
// aapt resource value: 0x7f020132
public const int avd_show_password_1 = 2130837810;
// aapt resource value: 0x7f020133
public const int avd_show_password_2 = 2130837811;
// aapt resource value: 0x7f020134
public const int avd_show_password_3 = 2130837812;
// aapt resource value: 0x7f020055
public const int design_bottom_navigation_item_background = 2130837589;
......
......@@ -5,10 +5,6 @@ using System.Diagnostics;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
#if ANDROID
using Android.Util;
#endif
[assembly: XamlCompilation (XamlCompilationOptions.Compile)]
namespace Xamarin
{
......@@ -16,27 +12,22 @@ namespace Xamarin
{
public string ConfigFilePath { get; set; }
public Core Core { get; set; }
public LinphoneManager Manager { get; set; }
public Core Core
{
get
{
return Manager.Core;
}
}
public App ()
{
InitializeComponent();
LinphoneWrapper.setNativeLogHandler();
Factory.Instance.EnableLogCollection(LogCollectionState.Enabled);
LoggingService.Instance.Listener.OnLogMessageWritten = OnLog;
CoreListener listener = Factory.Instance.CreateCoreListener();
listener.OnGlobalStateChanged = OnGlobal;
#if ANDROID
// Giving app context in CreateCore is mandatory for Android to be able to load grammars (and other assets) from AAR
Core = Factory.Instance.CreateCore(listener, ConfigFilePath, null, IntPtr.Zero, LinphoneAndroid.AndroidContext);
// Required to be able to store logs as file
Core.SetLogCollectionPath(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData));
#else
Core = Factory.Instance.CreateCore(listener, ConfigFilePath, null);
#endif
Core.NetworkReachable = true;
Manager = new LinphoneManager();
Manager.Init(ConfigFilePath);
MainPage = new MainPage();
}
......@@ -45,91 +36,11 @@ namespace Xamarin
{
return MainPage.FindByName<StackLayout>("stack_layout");
}
private void OnLog(LoggingService logService, string domain, LogLevel lev, string message)
{
string now = DateTime.Now.ToString("hh:mm:ss");
string log = now + " [";
switch (lev)
{
case LogLevel.Debug:
log += "DEBUG";
#if ANDROID
Log.Debug(domain, message);
#endif
break;
case LogLevel.Error:
log += "ERROR";
#if ANDROID
Log.Error(domain, message);
#endif
break;
case LogLevel.Message:
log += "MESSAGE";
#if ANDROID
Log.Info(domain, message);
#endif
break;
case LogLevel.Warning:
log += "WARNING";
#if ANDROID
Log.Warn(domain, message);
#endif
break;
case LogLevel.Fatal:
log += "FATAL";
#if ANDROID
Log.Error(domain, message);
#endif
break;
default:
break;
}
log += "] (" + domain + ") " + message;
#if WINDOWS_UWP
Debug.WriteLine(log);
#endif
}
private void OnGlobal(Core lc, GlobalState gstate, string message)
{
Debug.WriteLine("Global state changed: " + gstate);
}
#if WINDOWS_UWP
private void LinphoneCoreIterate(ThreadPoolTimer timer) {
#else
private void LinphoneCoreIterate()
{
#endif
while (true)
{
#if WINDOWS_UWP
CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High,
() => {
LinphoneCore.Iterate();
});
#else
Device.BeginInvokeOnMainThread(() =>
{
Core.Iterate();
});
Thread.Sleep(50);
#endif
}
}
protected override void OnStart ()
{
// Handle when your app starts
#if WINDOWS_UWP
TimeSpan period = TimeSpan.FromSeconds(1);
ThreadPoolTimer PeriodicTimer = ThreadPoolTimer.CreatePeriodicTimer(LinphoneCoreIterate , period);
#else
Thread iterate = new Thread(LinphoneCoreIterate);
iterate.IsBackground = false;
iterate.Start();
#endif
Manager.Start();
}
protected override void OnSleep ()
......
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Diagnostics;
using Linphone;
using Xamarin.Forms;
#if ANDROID
using Android.Util;
#endif
namespace Xamarin
{
public class LinphoneManager
{
public Core Core { get; set; }
public LinphoneManager()
{
LinphoneWrapper.setNativeLogHandler();
Factory.Instance.EnableLogCollection(LogCollectionState.Enabled);
LoggingService.Instance.LogLevel = LogLevel.Debug;
LoggingService.Instance.Listener.OnLogMessageWritten = OnLog;
Debug.WriteLine("LinphoneWrapper.cs version is " + LinphoneWrapper.VERSION);
}
public void Init(string configPath)
{
CoreListener listener = Factory.Instance.CreateCoreListener();
listener.OnGlobalStateChanged = OnGlobal;
#if ANDROID
// Giving app context in CreateCore is mandatory for Android to be able to load grammars (and other assets) from AAR
Core = Factory.Instance.CreateCore(listener, configPath, null, IntPtr.Zero, LinphoneAndroid.AndroidContext);
// Required to be able to store logs as file
Core.SetLogCollectionPath(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
#else
Core = Factory.Instance.CreateCore(listener, ConfigFilePath, null);
#endif
Core.NetworkReachable = true;
}
public void Start()
{
#if WINDOWS_UWP
TimeSpan period = TimeSpan.FromSeconds(1);
ThreadPoolTimer PeriodicTimer = ThreadPoolTimer.CreatePeriodicTimer(LinphoneCoreIterate , period);
#else
Thread iterate = new Thread(LinphoneCoreIterate);
iterate.IsBackground = false;
iterate.Start();
#endif
}
#if WINDOWS_UWP
private void LinphoneCoreIterate(ThreadPoolTimer timer) {
#else
private void LinphoneCoreIterate()
{
#endif
while (true)
{
#if WINDOWS_UWP
CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High,
() => {
LinphoneCore.Iterate();
});
#else
Device.BeginInvokeOnMainThread(() =>
{
Core.Iterate();
});
Thread.Sleep(50);
#endif
}
}
private void OnLog(LoggingService logService, string domain, LogLevel lev, string message)
{
string now = DateTime.Now.ToString("hh:mm:ss");
string log = now + " [";
switch (lev)
{
case LogLevel.Debug:
log += "DEBUG";
#if ANDROID
Log.Debug(domain, message);
#endif
break;
case LogLevel.Error:
log += "ERROR";
#if ANDROID
Log.Error(domain, message);
#endif
break;
case LogLevel.Message:
log += "MESSAGE";
#if ANDROID
Log.Info(domain, message);
#endif
break;
case LogLevel.Warning:
log += "WARNING";
#if ANDROID
Log.Warn(domain, message);
#endif
break;
case LogLevel.Fatal:
log += "FATAL";
#if ANDROID
Log.Error(domain, message);
#endif
break;
default:
break;
}
log += "] (" + domain + ") " + message;
#if WINDOWS_UWP
Debug.WriteLine(log);
#endif
}
private void OnGlobal(Core lc, GlobalState gstate, string message)
{
Debug.WriteLine("Global state changed: " + gstate);
}
}
}
......@@ -8,9 +8,10 @@
<Label x:Name="welcome" Text="Welcome to Linphone Xamarin!" HorizontalOptions="Center" />
<StackLayout x:Name="stack_registrar">
<Entry x:Name="username" Placeholder="Username" />
<Entry x:Name="password" Placeholder="Password" IsPassword="true"/>
<Entry x:Name="username" Placeholder="Username" Text="" />
<Entry x:Name="password" Placeholder="Password" IsPassword="true" Text=""/>
<Entry x:Name="domain" Placeholder="Domain" Text="sip.linphone.org"/>
<Picker x:Name="transport"/>
<Button x:Name="register" Text="Register" Clicked="OnRegisterClicked" HorizontalOptions="Center" />
<Label x:Name="registration_status" HorizontalOptions="Center" />
</StackLayout>
......
......@@ -18,6 +18,8 @@ namespace Xamarin
}
private CoreListener Listener;
private Dictionary<string, TransportType> Transports;
private void OnRegistration(Core lc, ProxyConfig config, RegistrationState state, string message)
{
Debug.WriteLine("Registration state changed: " + state);
......@@ -101,6 +103,16 @@ namespace Xamarin
Listener.OnCallStatsUpdated = OnStats;
Listener.OnLogCollectionUploadStateChanged = OnLogCollectionUpload;
Core.AddListener(Listener);
Transports = new Dictionary<string, TransportType>
{
{ "UDP", TransportType.Udp }, { "TCP", TransportType.Tcp }, { "TLS", TransportType.Tls },
};
foreach (string protocol in Transports.Keys)
{
transport.Items.Add(protocol);
}
transport.SelectedIndex = 2;
}
private void OnRegisterClicked(object sender, EventArgs e)
......@@ -112,6 +124,7 @@ namespace Xamarin
var identity = Factory.Instance.CreateAddress("sip:sample@domain.tld");
identity.Username = username.Text;
identity.Domain = domain.Text;
identity.Transport = Transports.Values.ElementAt(transport.SelectedIndex);
proxyConfig.Edit();
proxyConfig.IdentityAddress = identity;
proxyConfig.ServerAddr = domain.Text;
......
......@@ -16,6 +16,7 @@
<Compile Include="$(MSBuildThisFileDirectory)App.xaml.cs">
<DependentUpon>App.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)LinphoneManager.cs" />
<Compile Include="$(MSBuildThisFileDirectory)LinphoneWrapper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)MainPage.xaml.cs">
<DependentUpon>MainPage.xaml</DependentUpon>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment