Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
gus33000 committed Aug 8, 2019
1 parent 9b837f6 commit cfc58f1
Show file tree
Hide file tree
Showing 22 changed files with 595 additions and 114 deletions.
15 changes: 8 additions & 7 deletions App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Chat"
xmlns:media="using:Microsoft.UI.Xaml.Media"
xmlns:flui="using:FluiFramework.UWP.Core">

<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
<flui:FluiFrameworkThemeResources/>
<ResourceDictionary>
<ResourceDictionary.ThemeDictionaries>
Expand All @@ -21,25 +23,24 @@
<x:Double x:Key="NavigationViewPaneHeaderRowMinHeight">80</x:Double>
<x:Double x:Key="NavigationViewItemOnLeftMinHeight">80</x:Double>

<AcrylicBrush x:Key="NavigationViewDefaultPaneBackground" BackgroundSource="HostBackdrop" TintColor="{StaticResource SystemChromeMediumLowColor}" TintOpacity="0.7" FallbackColor="{StaticResource SystemChromeMediumLowColor}"/>
<AcrylicBrush x:Key="NavigationViewTopPaneBackground" BackgroundSource="HostBackdrop" TintColor="{StaticResource SystemChromeMediumLowColor}" TintOpacity="0.7" FallbackColor="{StaticResource SystemChromeMediumLowColor}"/>
<AcrylicBrush x:Key="NavigationViewExpandedPaneBackground" BackgroundSource="HostBackdrop" TintColor="{StaticResource SystemChromeMediumLowColor}" TintOpacity="0.7" FallbackColor="{StaticResource SystemChromeMediumLowColor}"/>
<media:AcrylicBrush x:Key="NavigationViewDefaultPaneBackground" BackgroundSource="HostBackdrop" TintColor="{StaticResource SystemChromeMediumLowColor}" TintOpacity="0.7" FallbackColor="{StaticResource SystemChromeMediumLowColor}"/>
<media:AcrylicBrush x:Key="NavigationViewTopPaneBackground" BackgroundSource="HostBackdrop" TintColor="{StaticResource SystemChromeMediumLowColor}" TintOpacity="0.7" FallbackColor="{StaticResource SystemChromeMediumLowColor}"/>
<media:AcrylicBrush x:Key="NavigationViewExpandedPaneBackground" BackgroundSource="HostBackdrop" TintColor="{StaticResource SystemChromeMediumLowColor}" TintOpacity="0.7" FallbackColor="{StaticResource SystemChromeMediumLowColor}"/>
</ResourceDictionary>
<ResourceDictionary x:Key="Light">
<x:Double x:Key="NavigationViewTopPaneHeight">80</x:Double>
<x:Double x:Key="NavigationViewPaneHeaderRowMinHeight">80</x:Double>
<x:Double x:Key="NavigationViewItemOnLeftMinHeight">80</x:Double>

<AcrylicBrush x:Key="NavigationViewDefaultPaneBackground" BackgroundSource="HostBackdrop" TintColor="{StaticResource SystemChromeMediumLowColor}" TintOpacity="0.7" FallbackColor="{StaticResource SystemChromeMediumLowColor}"/>
<AcrylicBrush x:Key="NavigationViewTopPaneBackground" BackgroundSource="HostBackdrop" TintColor="{StaticResource SystemChromeMediumLowColor}" TintOpacity="0.7" FallbackColor="{StaticResource SystemChromeMediumLowColor}"/>
<AcrylicBrush x:Key="NavigationViewExpandedPaneBackground" BackgroundSource="HostBackdrop" TintColor="{StaticResource SystemChromeMediumLowColor}" TintOpacity="0.7" FallbackColor="{StaticResource SystemChromeMediumLowColor}"/>
<media:AcrylicBrush x:Key="NavigationViewDefaultPaneBackground" BackgroundSource="HostBackdrop" TintColor="{StaticResource SystemChromeMediumLowColor}" TintOpacity="0.7" FallbackColor="{StaticResource SystemChromeMediumLowColor}"/>
<media:AcrylicBrush x:Key="NavigationViewTopPaneBackground" BackgroundSource="HostBackdrop" TintColor="{StaticResource SystemChromeMediumLowColor}" TintOpacity="0.7" FallbackColor="{StaticResource SystemChromeMediumLowColor}"/>
<media:AcrylicBrush x:Key="NavigationViewExpandedPaneBackground" BackgroundSource="HostBackdrop" TintColor="{StaticResource SystemChromeMediumLowColor}" TintOpacity="0.7" FallbackColor="{StaticResource SystemChromeMediumLowColor}"/>
</ResourceDictionary>
<ResourceDictionary x:Key="HighContrast">
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>
</ResourceDictionary>
<ResourceDictionary>

<!-- TextBox with no 'active' background -->
<Style x:Key="TextBoxNoBackground" TargetType="TextBox">
<Setter Property="MinWidth" Value="{ThemeResource TextControlThemeMinWidth}"/>
Expand Down
48 changes: 46 additions & 2 deletions Chat.BackgroundTasks/BackgroundTaskUtils.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
using System.Collections.Generic;
using System.Linq;
using Windows.ApplicationModel.Background;
using Windows.ApplicationModel.Chat;
using Windows.Devices.Sms;
using System;

namespace Chat.BackgroundTasks
{
public sealed class BackgroundTaskUtils
{
public static void RegisterToastNotificationBackgroundTasks()
public static async void RegisterToastNotificationBackgroundTasks()
{
try
{
SmsFilterRule filter = new SmsFilterRule(SmsMessageType.Text);
SmsFilterActionType actionType = SmsFilterActionType.Accept;
SmsFilterRules filterRules = new SmsFilterRules(actionType);
IList<SmsFilterRule> rules = filterRules.Rules;

SmsFilterRule filter = new SmsFilterRule(SmsMessageType.Text);
rules.Add(filter);
filter = new SmsFilterRule(SmsMessageType.Wap);
rules.Add(filter);
filter = new SmsFilterRule(SmsMessageType.Status);
rules.Add(filter);
filter = new SmsFilterRule(SmsMessageType.App);
rules.Add(filter);

SmsMessageReceivedTrigger trigger = new SmsMessageReceivedTrigger(filterRules);

RegisterBackgroundTask<SmsBackgroundTask>(trigger);
Expand All @@ -25,6 +35,31 @@ public static void RegisterToastNotificationBackgroundTasks()

}

try
{
var transports = await ChatMessageManager.GetTransportsAsync();
foreach (var transport in transports)
{
if (!transport.IsAppSetAsNotificationProvider)// && transport.TransportKind == ChatMessageTransportKind.Text)
{
await transport.RequestSetAsNotificationProviderAsync();
}
}
}
catch
{

}

try
{
RegisterBackgroundTask<ChatMessageNotificationBackgroundTask>(new ChatMessageNotificationTrigger());
}
catch
{

}

try
{
RegisterBackgroundTask<SmsReplyBackgroundTask>(new ToastNotificationActionTrigger());
Expand All @@ -46,6 +81,15 @@ public static void UnRegisterToastNotificationBackgroundTasks()

}

try
{
UnRegisterBackgroundTask<ChatMessageNotificationBackgroundTask>();
}
catch
{

}

try
{
UnRegisterBackgroundTask<SmsReplyBackgroundTask>();
Expand Down
5 changes: 3 additions & 2 deletions Chat.BackgroundTasks/Chat.BackgroundTasks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<AssemblyName>Chat.BackgroundTasks</AssemblyName>
<DefaultLanguage>en-US</DefaultLanguage>
<TargetPlatformIdentifier>UAP</TargetPlatformIdentifier>
<TargetPlatformVersion Condition=" '$(TargetPlatformVersion)' == '' ">10.0.17763.0</TargetPlatformVersion>
<TargetPlatformVersion Condition=" '$(TargetPlatformVersion)' == '' ">10.0.18362.0</TargetPlatformVersion>
<TargetPlatformMinVersion>10.0.16299.0</TargetPlatformMinVersion>
<MinimumVisualStudioVersion>14</MinimumVisualStudioVersion>
<FileAlignment>512</FileAlignment>
Expand Down Expand Up @@ -123,6 +123,7 @@
<ItemGroup>
<Compile Include="BackgroundTaskUtils.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ChatMessageNotificationBackgroundTask.cs" />
<Compile Include="SmsBackgroundTask.cs" />
<Compile Include="SmsReplyBackgroundTask.cs" />
<Compile Include="UpdateTask.cs" />
Expand All @@ -132,7 +133,7 @@
<Version>6.2.8</Version>
</PackageReference>
<PackageReference Include="Microsoft.Toolkit.Uwp.Notifications">
<Version>5.1.1</Version>
<Version>5.0.0-preview.gb86cb1c4cb</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>
Expand Down
175 changes: 175 additions & 0 deletions Chat.BackgroundTasks/ChatMessageNotificationBackgroundTask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
using Chat.Common;
using Microsoft.Toolkit.Uwp.Notifications;
using System;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.ApplicationModel.Background;
using Windows.ApplicationModel.Chat;
using Windows.Devices.Sms;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.UI.Notifications;

namespace Chat.BackgroundTasks
{
public sealed class ChatMessageNotificationBackgroundTask : IBackgroundTask
{
public void Run(IBackgroundTaskInstance taskInstance)
{
Process(taskInstance).Wait();
}

private async Task Process(IBackgroundTaskInstance taskInstance)
{
try
{
ChatMessageNotificationTriggerDetails triggerDetails = (ChatMessageNotificationTriggerDetails)taskInstance.TriggerDetails;
if (triggerDetails.ShouldDisplayToast)
{
await DisplayToast(triggerDetails.ChatMessage);
}
}
catch
{

}
}

private async Task<string> SaveFile(IRandomAccessStreamReference stream, string filename)
{
StorageFolder storageFolder = ApplicationData.Current.LocalFolder;

StorageFile file = await storageFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);

using (var srcStream = await stream.OpenReadAsync())
using (var targetStream = await file.OpenAsync(FileAccessMode.ReadWrite))
using (var reader = new DataReader(srcStream.GetInputStreamAt(0)))
{
var output = targetStream.GetOutputStreamAt(0);
await reader.LoadAsync((uint)srcStream.Size);
while (reader.UnconsumedBufferLength > 0)
{
uint dataToRead = reader.UnconsumedBufferLength > 64
? 64
: reader.UnconsumedBufferLength;

IBuffer buffer = reader.ReadBuffer(dataToRead);
await output.WriteAsync(buffer);
}

await output.FlushAsync();

return file.Path;
}
}

private async Task DisplayToast(ChatMessage message)
{
var information = await ContactUtils.FindContactInformationFromSender(message.From);
string extra = "";
string thumbnailpath = "";
string text = "";

string deviceid = SmsDevice2.GetDefault().DeviceId;

foreach (var attachment in message.Attachments)
{
try
{
//extra += " " + attachment.MimeType;

if (attachment.MimeType == "application/smil")
{

}

if (attachment.MimeType == "text/vcard")
{
text += "Contact content in this message. ";
}

if (attachment.MimeType.StartsWith("image/"))
{
text += "Image content in this message. ";
var imageextension = attachment.MimeType.Split('/').Last();
thumbnailpath = await SaveFile(attachment.DataStreamReference, "messagepicture." + DateTimeOffset.Now.ToUnixTimeMilliseconds() + "." + imageextension);
}

if (attachment.MimeType.StartsWith("audio/"))
{
text += "Audio content in this message. ";
var audioextension = attachment.MimeType.Split('/').Last();
}
}
catch
{
//extra += " oops";
}
}

var toastContent = new ToastContent()
{
Visual = new ToastVisual()
{
BindingGeneric = new ToastBindingGeneric()
{
Children =
{
new AdaptiveText()
{
Text = information.DisplayName,
HintMaxLines = 1
},
new AdaptiveText()
{
Text = string.IsNullOrEmpty(message.Body) ? text : message.Body
}
},
HeroImage = new ToastGenericHeroImage()
{
Source = thumbnailpath
},
AppLogoOverride = new ToastGenericAppLogo()
{
Source = information.ThumbnailPath,
HintCrop = ToastGenericAppLogoCrop.Circle
},
Attribution = new ToastGenericAttributionText()
{
Text = information.PhoneNumberKind //+ " " + extra
}
}
},
Actions = new ToastActionsCustom()
{
Inputs =
{
new ToastTextBox("textBox")
{
PlaceholderContent = "Send a message"
}
},
Buttons =
{
new ToastButton("Send", "action=reply" + "&from=" + message.From + "&deviceid=" + deviceid)
{
ActivationType = ToastActivationType.Background,
ImageUri = "Assets/ToastIcons/Send.png",
TextBoxId = "textBox"
}
}
},
Launch = "action=openThread" + "&from=" + message.From + "&deviceid=" + deviceid,
Audio = new ToastAudio()
{
Src = new Uri("ms-winsoundevent:Notification.SMS")
}
};

var toastNotif = new ToastNotification(toastContent.GetXml());
ToastNotificationManager.CreateToastNotifier().Show(toastNotif);
}
}
}
Loading

0 comments on commit cfc58f1

Please sign in to comment.