Skip to content

Commit

Permalink
improved notification system, also need HUGE refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
DjArt committed Jul 27, 2019
1 parent 3470a38 commit cb15f3e
Show file tree
Hide file tree
Showing 13 changed files with 336 additions and 174 deletions.
98 changes: 91 additions & 7 deletions WoADialer/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
using System;
using Internal.Windows.Calls;
using Microsoft.QueryStringDotNET;
using Microsoft.Toolkit.Uwp.Notifications;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading;
using System.Threading.Tasks;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.ApplicationModel.Background;
using Windows.ApplicationModel.Calls;
using Windows.ApplicationModel.Calls.Background;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Notifications;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
Expand All @@ -25,12 +32,18 @@ namespace WoADialer
{
sealed partial class App : Application
{
private NotificationShredder Shredder;
private CallWaiter Waiter;
private ManualResetEvent Event;
private Call Incoming;

public App()
{
this.InitializeComponent();
this.Suspending += OnSuspending;

if(SettingsManager.isFirstTimeRun()) SettingsManager.setDefaultSettings();
if (SettingsManager.isFirstTimeRun()) SettingsManager.setDefaultSettings();
MainEntities.Initialize();
}

protected override void OnActivated(IActivatedEventArgs args)
Expand All @@ -56,6 +69,23 @@ protected override void OnActivated(IActivatedEventArgs args)

case ActivationKind.LockScreenCall:

break;
case ActivationKind.ToastNotification:
var toastActivationArgs = args as ToastNotificationActivatedEventArgs;

QueryString str = QueryString.Parse(toastActivationArgs.Argument);

switch (str["action"])
{
case "answer":
uint callID = uint.Parse(str["callId"]);
MainEntities.CallManager.CurrentCalls.FirstOrDefault(x => x.ID == callID)?.AcceptIncomingEx();
break;
}
if (rootFrame.BackStack.Count == 0)
{
rootFrame.BackStack.Add(new PageStackEntry(typeof(MainPage), null, null));
}
break;
default:
rootFrame.Navigate(typeof(InCallUI));
Expand Down Expand Up @@ -95,20 +125,74 @@ protected override void OnLaunched(LaunchActivatedEventArgs e)
}
}

protected override void OnBackgroundActivated(BackgroundActivatedEventArgs args)
protected override async void OnBackgroundActivated(BackgroundActivatedEventArgs args)
{
base.OnBackgroundActivated(args);
BackgroundTaskDeferral deferral = args.TaskInstance.GetDeferral();
switch (args.TaskInstance.Task.Name)
{
case TaskManager.USER_NOTIFICATION_CHANGED:
NotificationHelper.RemoveSystemCallNotification();
default:
deferral.Complete();
break;
case TaskManager.LINE_STATE_CHANGED:

PhoneLineChangedTriggerDetails details = args.TaskInstance.TriggerDetails as PhoneLineChangedTriggerDetails;
if (Shredder == null && Waiter == null && Incoming == null)
{
Event = new ManualResetEvent(false);
Shredder = new NotificationShredder();
Waiter = new CallWaiter();
Shredder.NotificationRemoved += Shredder_NotificationRemoved;
Waiter.CallAppeared += Waiter_CallAppeared;
Shredder.RegisterListener();
Waiter.RegisterListener();
await Task.Run(() => Event.WaitOne(5000));
//TaskManager.ShowToast("Registered");
Event = null;
Incoming = null;
Waiter = null;
Shredder = null;
goto default;
}
else
{
//TaskManager.ShowToast("Not registered");
goto default;
}
break;
}
deferral.Complete();
}

private void Waiter_CallAppeared(object sender, Call e)
{
Incoming = e;
Waiter.UnregisterListener();
Waiter.CallAppeared -= Waiter_CallAppeared;
ToastNotification notification = TaskManager.ShowCallToast(e);
e.StateChanged += E_StateChanged;
notification.Activated += Notification_Activated;
}

private void E_StateChanged(Call sender, CallState args)
{
sender.StateChanged -= E_StateChanged;
if (args == CallState.Disconnected)
{
TaskManager.ShowToast($"Missed call: {sender.Number}");
}
Event.Set();
}

private void Notification_Activated(ToastNotification sender, object args)
{
Incoming.StateChanged -= E_StateChanged;
Event.Set();
}

private void Shredder_NotificationRemoved(object sender, EventArgs e)
{
Shredder.UnregisterListener();
Shredder.NotificationRemoved -= Shredder_NotificationRemoved;
//TaskManager.ShowToast($"{MainEntities.CallManager.CurrentCalls.FirstOrDefault(x => x.State == CallState.Incoming)?.Number}");
}

void OnNavigationFailed(object sender, NavigationFailedEventArgs e)
Expand Down
56 changes: 0 additions & 56 deletions WoADialer/Background/BackgroungCallMonitor.cs

This file was deleted.

49 changes: 49 additions & 0 deletions WoADialer/Background/CallWaiter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using Internal.Windows.Calls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Windows.ApplicationModel.Background;
using Windows.ApplicationModel.LockScreen;

using Microsoft.Toolkit.Uwp.Notifications;
using Windows.UI.Notifications;
using Windows.ApplicationModel.Calls.Background;

namespace WoADialer.Model
{
public sealed class CallWaiter
{
public event EventHandler<Call> CallAppeared;

public CallWaiter()
{

}

private void CallManager_CallAppeared(CallManager sender, Call args)
{
if (args.State == CallState.Incoming)
{
CallAppeared?.Invoke(this, args);
}
}

public void RegisterListener()
{
MainEntities.CallManager.CallAppeared += CallManager_CallAppeared;
Call call = MainEntities.CallManager.CurrentCalls.FirstOrDefault(x => x.State == CallState.Incoming);
if (call?.State == CallState.Incoming)
{
CallAppeared?.Invoke(this, call);
}
}

public void UnregisterListener()
{
MainEntities.CallManager.CallAppeared -= CallManager_CallAppeared;
}
}
}
50 changes: 0 additions & 50 deletions WoADialer/Background/NotificationHelper.cs

This file was deleted.

54 changes: 54 additions & 0 deletions WoADialer/Background/NotificationShredder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using Microsoft.Toolkit.Uwp.Notifications;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.ApplicationModel.Background;
using Windows.UI.Notifications;
using Windows.UI.Notifications.Management;

namespace WoADialer.Background
{
public sealed class NotificationShredder
{
private const string WINDOWS_SYSTEM_TOAST_CALLING = "Windows.SystemToast.Calling";

public event EventHandler NotificationRemoved;

public NotificationShredder()
{

}

public async void RegisterListener()
{
UserNotificationListener.Current.NotificationChanged += Listener_NotificationChanged;
IReadOnlyList<UserNotification> notifications = await UserNotificationListener.Current.GetNotificationsAsync(NotificationKinds.Toast);
UserNotification callToast = notifications.FirstOrDefault(x => x.AppInfo.AppUserModelId == WINDOWS_SYSTEM_TOAST_CALLING);
if (callToast != null)
{
UserNotificationListener.Current.RemoveNotification(callToast.Id);
NotificationRemoved?.Invoke(this, null);
}
}

public void UnregisterListener()
{
UserNotificationListener.Current.NotificationChanged -= Listener_NotificationChanged;
}

private void Listener_NotificationChanged(UserNotificationListener sender, UserNotificationChangedEventArgs args)
{
if (args.ChangeKind == UserNotificationChangedKind.Added)
{
UserNotification notification = sender.GetNotification(args.UserNotificationId);
if (notification.AppInfo.AppUserModelId == WINDOWS_SYSTEM_TOAST_CALLING)
{
sender.RemoveNotification(notification.Id);
NotificationRemoved?.Invoke(this, null);
}
}
}
}
}
Loading

0 comments on commit cb15f3e

Please sign in to comment.