Skip to content

Commit

Permalink
Merge pull request #191 from IsaacMorris1980/SyncAcrossDevicesOnSameN…
Browse files Browse the repository at this point in the history
…etwork

Sync settings over wifi
  • Loading branch information
IsaacMorris1980 committed Aug 7, 2023
2 parents 1d0216f + 9929e93 commit ee23357
Show file tree
Hide file tree
Showing 16 changed files with 353 additions and 84 deletions.
22 changes: 0 additions & 22 deletions appLauncher.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ VisualStudioVersion = 17.6.33815.320
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "appLauncher", "appLauncher\appLauncher.csproj", "{825249DF-2022-4222-96A1-69A32AA4E16C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GoogleAnalyticsv4SDK", "..\GoogleAnalyticsv4SDKFor.Net\GoogleAnalyticsv4SDK\GoogleAnalyticsv4SDK.csproj", "{80207DE7-F5EF-4B47-9C81-E65DEA1D4832}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -51,26 +49,6 @@ Global
{825249DF-2022-4222-96A1-69A32AA4E16C}.Release|x86.ActiveCfg = Release|x86
{825249DF-2022-4222-96A1-69A32AA4E16C}.Release|x86.Build.0 = Release|x86
{825249DF-2022-4222-96A1-69A32AA4E16C}.Release|x86.Deploy.0 = Release|x86
{80207DE7-F5EF-4B47-9C81-E65DEA1D4832}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{80207DE7-F5EF-4B47-9C81-E65DEA1D4832}.Debug|Any CPU.Build.0 = Debug|Any CPU
{80207DE7-F5EF-4B47-9C81-E65DEA1D4832}.Debug|ARM.ActiveCfg = Debug|ARM
{80207DE7-F5EF-4B47-9C81-E65DEA1D4832}.Debug|ARM.Build.0 = Debug|ARM
{80207DE7-F5EF-4B47-9C81-E65DEA1D4832}.Debug|ARM64.ActiveCfg = Debug|ARM64
{80207DE7-F5EF-4B47-9C81-E65DEA1D4832}.Debug|ARM64.Build.0 = Debug|ARM64
{80207DE7-F5EF-4B47-9C81-E65DEA1D4832}.Debug|x64.ActiveCfg = Debug|x64
{80207DE7-F5EF-4B47-9C81-E65DEA1D4832}.Debug|x64.Build.0 = Debug|x64
{80207DE7-F5EF-4B47-9C81-E65DEA1D4832}.Debug|x86.ActiveCfg = Debug|x86
{80207DE7-F5EF-4B47-9C81-E65DEA1D4832}.Debug|x86.Build.0 = Debug|x86
{80207DE7-F5EF-4B47-9C81-E65DEA1D4832}.Release|Any CPU.ActiveCfg = Release|Any CPU
{80207DE7-F5EF-4B47-9C81-E65DEA1D4832}.Release|Any CPU.Build.0 = Release|Any CPU
{80207DE7-F5EF-4B47-9C81-E65DEA1D4832}.Release|ARM.ActiveCfg = Release|ARM
{80207DE7-F5EF-4B47-9C81-E65DEA1D4832}.Release|ARM.Build.0 = Release|ARM
{80207DE7-F5EF-4B47-9C81-E65DEA1D4832}.Release|ARM64.ActiveCfg = Release|ARM64
{80207DE7-F5EF-4B47-9C81-E65DEA1D4832}.Release|ARM64.Build.0 = Release|ARM64
{80207DE7-F5EF-4B47-9C81-E65DEA1D4832}.Release|x64.ActiveCfg = Release|x64
{80207DE7-F5EF-4B47-9C81-E65DEA1D4832}.Release|x64.Build.0 = Release|x64
{80207DE7-F5EF-4B47-9C81-E65DEA1D4832}.Release|x86.ActiveCfg = Release|x86
{80207DE7-F5EF-4B47-9C81-E65DEA1D4832}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
237 changes: 203 additions & 34 deletions appLauncher/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
using appLauncher.Core.Helpers;
using appLauncher.Core.Model;
using appLauncher.Core.Pages;

using GoogleAnalyticsv4SDK.Validation;
using GoogleAnalyticsv4SDK.Helpers;
using GoogleAnalyticsv4SDK.Interfaces;

using Newtonsoft.Json;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.ApplicationModel.Core;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.UI;
using Windows.Networking;
using Windows.Networking.Connectivity;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
Expand All @@ -27,9 +35,16 @@ namespace appLauncher
/// </summary>
sealed partial class App : Application
{
public static ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
public ExceptionEventCalls reportException;
public ScreenViewEventCalls reportScreenViews;
public GoogleAnalyticsEndpoints tracker;
public List<IEvent> recordingEvents = new List<IEvent>();
public BackgroundWorker UDPServer = new BackgroundWorker();
public BackgroundWorker TCPServer = new BackgroundWorker();
public BackgroundWorker UDPClient = new BackgroundWorker();
public BackgroundWorker TCPClient = new BackgroundWorker();
private bool isnetworkstatuschangedregistered = false;
private NetworkStatusChangedEventHandler networkstatuschangedhandler;



/// <summary>
/// Initializes the singleton application object. This is the first line of authored code
Expand All @@ -42,28 +57,62 @@ public App()
App.Current.UnhandledException += App_UnhandledException;
TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;


}
private void OnNetworkStatusChange(object sender)
{
SettingsHelper.totalAppSettings.Sync = false;
ConnectionProfile profile = NetworkInformation.GetInternetConnectionProfile();
if (profile != null)
{

private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)

if (!profile.IsWlanConnectionProfile)
{

// SettingsHelper.totalAppSettings.Sync = false;
return;
}
// SettingsHelper.totalAppSettings.Sync = true;
if (!((App)Current).UDPServer.IsBusy)
{
((App)Current).UDPServer.RunWorkerAsync();
}
if (!((App)Current).UDPClient.IsBusy)
{
((App)Current).UDPClient.RunWorkerAsync();
}
if (!((App)Current).TCPServer.IsBusy)
{
((App)Current).TCPServer.RunWorkerAsync();
}

}
}

private void NetworkStatusChange()
{
// Dictionary<string, string> result = new Dictionary<string, string>();
// result.Add("UnhandedExceptionmessage", e.Exception.Message);
// result.Add("StackTrace", e.Exception.StackTrace);
// result.Add("TargetSite", e.Exception.TargetSite.Name);
// result.Add("ExceptionSource", e.Exception.Source);
// result.AddRange((IEnumerable<KeyValuePair<string, string>>)e.Exception.Data);
networkstatuschangedhandler = new NetworkStatusChangedEventHandler(OnNetworkStatusChange);
if (isnetworkstatuschangedregistered == false)
{
NetworkInformation.NetworkStatusChanged += networkstatuschangedhandler;
isnetworkstatuschangedregistered = true;
}

}

private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
{
GoogleAnalyticsv4SDK.Events.Mobile.Execeptions exec = new GoogleAnalyticsv4SDK.Events.Mobile.Execeptions(e.Exception);
((App)Application.Current).recordingEvents.Add(exec);
tracker.SendEvent(recordingEvents, SettingsHelper.totalAppSettings.ClientID, false);
}

private void App_UnhandledException(object sender, Windows.UI.Xaml.UnhandledExceptionEventArgs e)
{
//Dictionary<string, string> result = new Dictionary<string, string>();
//result.Add("Unhandedmessage", e.Message);
//result.Add("UnhandedExceptionmessage", e.Exception.Message);
//result.Add("StackTrace", e.Exception.StackTrace);
//result.Add("TargetSite", e.Exception.TargetSite.Name);
//result.Add("ExceptionSource", e.Exception.Source);
//result.AddRange((IEnumerable<KeyValuePair<string, string>>)e.Exception.Data);
GoogleAnalyticsv4SDK.Events.Mobile.Execeptions exec = new GoogleAnalyticsv4SDK.Events.Mobile.Execeptions(e.Exception);
((App)Application.Current).recordingEvents.Add(exec);
tracker.SendEvent(recordingEvents, SettingsHelper.totalAppSettings.ClientID, false);

}

Expand All @@ -77,29 +126,38 @@ protected override async void OnLaunched(LaunchActivatedEventArgs e)
{
try
{

UDPServer.WorkerSupportsCancellation = true;
UDPServer.DoWork += UDPServer_DoWork;
UDPClient.WorkerSupportsCancellation = true;
UDPClient.DoWork += UDPClient_DoWork;
TCPServer.WorkerSupportsCancellation = true;
TCPServer.DoWork += TCPServer_DoWork;
TCPClient.WorkerSupportsCancellation = true;
TCPClient.DoWork += TCPClient_DoWork;

//Extends view into status bar/title bar, depending on the device used.
await SettingsHelper.LoadAppSettingsAsync();
reportScreenViews = new ScreenViewEventCalls(SettingsHelper.totalAppSettings.MeasurementID, SettingsHelper.totalAppSettings.APISecret, SettingsHelper.totalAppSettings.ClientID);
reportException = new ExceptionEventCalls(SettingsHelper.totalAppSettings.MeasurementID, SettingsHelper.totalAppSettings.APISecret, SettingsHelper.totalAppSettings.ClientID);
tracker = new GoogleAnalyticsEndpoints(SettingsHelper.totalAppSettings.MeasurementID, SettingsHelper.totalAppSettings.APISecret);
ApplicationView appView = ApplicationView.GetForCurrentView();
appView.SetPreferredMinSize(new Size(360, 360));
appView.SetDesiredBoundsMode(ApplicationViewBoundsMode.UseCoreWindow);
IObservableMap<string, string> qualifiers = Windows.ApplicationModel.Resources.Core.ResourceContext.GetForCurrentView().QualifierValues;
SettingsHelper.SetApplicationResources();
if (qualifiers.ContainsKey("DeviceFamily") && qualifiers["DeviceFamily"] == "Desktop")
{
appView.TitleBar.ButtonBackgroundColor = Colors.Transparent;
appView.TitleBar.BackgroundColor = Colors.Transparent;
CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar = true;
}
OnNetworkStatusChange(new object());
NetworkStatusChange();

if (qualifiers.ContainsKey("DeviceFamily") && qualifiers["DeviceFamily"] == "Mobile")
{
ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.FullScreen;
////if (qualifiers.ContainsKey("DeviceFamily") && qualifiers["DeviceFamily"] == "Desktop")
////{
//// appView.TitleBar.ButtonBackgroundColor = Colors.Transparent;
//// appView.TitleBar.BackgroundColor = Colors.Transparent;
//// CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar = true;
////}

}
////if (qualifiers.ContainsKey("DeviceFamily") && qualifiers["DeviceFamily"] == "Mobile")
////{
//// ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.FullScreen;

////}


Frame rootFrame = Window.Current.Content as Frame;
Expand Down Expand Up @@ -141,6 +199,117 @@ protected override async void OnLaunched(LaunchActivatedEventArgs e)

}
}

private void TCPClient_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// throw new NotImplementedException();
}

private void TCPClient_DoWork(object sender, DoWorkEventArgs e)
{
if (SettingsHelper.totalAppSettings.RemoteIP != null)
{
TcpClient client = new TcpClient();
client.ConnectAsync(SettingsHelper.totalAppSettings.RemoteIP.Address, 13000);
using (var stream = client.GetStream())
{
string content = JsonConvert.SerializeObject(SettingsHelper.totalAppSettings);
byte[] sendbody = Encoding.UTF8.GetBytes(content);
stream.WriteAsync(sendbody, 0, sendbody.Length);
}
}
}

private void TCPServer_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{

}

private void TCPServer_DoWork(object sender, DoWorkEventArgs e)
{
TcpListener listener = new TcpListener(IPAddress.Any, 13000);
Task.Factory.StartNew(async () =>
{
listener.Start();
while (true)
{
TcpClient a = await listener.AcceptTcpClientAsync();
NetworkStream b = a.GetStream();
byte[] message = new byte[1024];
string test = string.Empty;
using (var stream = a.GetStream())
{
int ab = b.Read(message, 0, message.Length);
if (ab == 0)
{
return;
}
test += Encoding.UTF8.GetString(message, 0, ab);
SettingsHelper.totalAppSettings = JsonConvert.DeserializeObject<GlobalAppSettings>(test);
}
listener.Stop();
}
});
}

private void UDPClient_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
throw new NotImplementedException();
}

private async void UDPClient_DoWork(object sender, DoWorkEventArgs e)
{
UdpClient client = new UdpClient();
Task.Factory.StartNew(async () =>
{
while (true)
{
IPEndPoint ipep = new IPEndPoint(IPAddress.Broadcast, 32123);
IReadOnlyList<HostName> hosts = NetworkInformation.GetHostNames();
byte[] data = Encoding.UTF8.GetBytes(hosts.ToString());
var a = await client.SendAsync(data, data.Length, ipep);
if (a > -1)
{
break;
}
}
});

}

private void UDPServer_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// throw new NotImplementedException();
}

private void UDPServer_DoWork(object sender, DoWorkEventArgs e)
{
UdpClient server = new UdpClient();

//start listening for messages and copy the messages back to the client
Task.Factory.StartNew(async () =>
{
while (true)
{
var remoteEP = new IPEndPoint(IPAddress.Any, 32123);
UdpReceiveResult data = await server.ReceiveAsync();
var hosts = NetworkInformation.GetHostNames();
var es = Encoding.UTF8.GetString(data.Buffer);
if (es != hosts.ToString())
{
SettingsHelper.totalAppSettings.RemoteIP = data.RemoteEndPoint;
Console.WriteLine(data.RemoteEndPoint.Address);
Console.WriteLine(es);
break;
}
}
});
UDPServer.Dispose();
}

/// <summary>
/// Invoked when Navigation to a certain page fails
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions appLauncher/Core/Helpers/GlobalVariables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Diagnostics;
using System.Net;
using System.Threading.Tasks;

using Windows.Foundation;
Expand All @@ -12,6 +13,7 @@ namespace appLauncher.Core.Helpers
{
public static class GlobalVariables
{
public static IPEndPoint endPoint { get; set; }
public static int _appsPerScreen { get; private set; }
public static DraggedItem _Itemdragged { get; set; } = new DraggedItem();
public static int _columns { get; set; }
Expand Down
Loading

0 comments on commit ee23357

Please sign in to comment.