This repository has been archived by the owner on May 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 506
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adding SMS API * Added tests * Added docs * Made some changes to the way SMS works * Fix the tests
- Loading branch information
1 parent
608c70d
commit 13bb42c
Showing
23 changed files
with
558 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace Microsoft.Caboodle.Tests | ||
{ | ||
public class Sms_Tests | ||
{ | ||
[Fact] | ||
public Task Sms_Fail_On_NetStandard() | ||
{ | ||
return Assert.ThrowsAsync<NotImplementedInReferenceAssemblyException>(() => Sms.ComposeAsync()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using System.Threading.Tasks; | ||
using Android.Content; | ||
using Android.OS; | ||
using Android.Provider; | ||
|
||
using AndroidUri = Android.Net.Uri; | ||
|
||
namespace Microsoft.Caboodle | ||
{ | ||
public static partial class Sms | ||
{ | ||
static bool IsComposeSupported | ||
=> Platform.IsIntentSupported(CreateIntent("0000000000")); | ||
|
||
public static Task ComposeAsync(SmsMessage message) | ||
{ | ||
if (!IsComposeSupported) | ||
throw new FeatureNotSupportedException(); | ||
|
||
var intent = CreateIntent(message); | ||
Platform.CurrentContext.StartActivity(intent); | ||
|
||
return Task.FromResult(true); | ||
} | ||
|
||
static Intent CreateIntent(SmsMessage message) | ||
=> CreateIntent(message?.Recipient, message?.Body); | ||
|
||
static Intent CreateIntent(string recipient, string body = null) | ||
{ | ||
Intent intent; | ||
if (!string.IsNullOrWhiteSpace(recipient)) | ||
{ | ||
var uri = AndroidUri.Parse("smsto:" + recipient); | ||
intent = new Intent(Intent.ActionSendto, uri); | ||
|
||
if (!string.IsNullOrWhiteSpace(body)) | ||
intent.PutExtra("sms_body", body); | ||
} | ||
else | ||
{ | ||
var pm = Platform.CurrentContext.PackageManager; | ||
var packageName = Telephony.Sms.GetDefaultSmsPackage(Platform.CurrentContext); | ||
intent = pm.GetLaunchIntentForPackage(packageName); | ||
} | ||
|
||
return intent; | ||
} | ||
|
||
public static string GetDefaultSmsPackage(Context context) | ||
{ | ||
if (Build.VERSION.SdkInt >= BuildVersionCodes.Kitkat) | ||
{ | ||
return Telephony.Sms.GetDefaultSmsPackage(context); | ||
} | ||
else | ||
{ | ||
var defApp = Settings.Secure.GetString(context.ContentResolver, "sms_default_application"); | ||
var pm = context.ApplicationContext.PackageManager; | ||
var intent = pm.GetLaunchIntentForPackage(defApp); | ||
var mInfo = pm.ResolveActivity(intent, 0); | ||
return mInfo.ActivityInfo.PackageName; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System.Threading.Tasks; | ||
using MessageUI; | ||
|
||
namespace Microsoft.Caboodle | ||
{ | ||
public static partial class Sms | ||
{ | ||
static bool IsComposeSupported | ||
=> MFMessageComposeViewController.CanSendText; | ||
|
||
public static Task ComposeAsync(SmsMessage message) | ||
{ | ||
if (!IsComposeSupported) | ||
throw new FeatureNotSupportedException(); | ||
|
||
// do this first so we can throw as early as possible | ||
var controller = Platform.GetCurrentViewController(); | ||
|
||
// create the controller | ||
var messageController = new MFMessageComposeViewController(); | ||
if (!string.IsNullOrWhiteSpace(message?.Body)) | ||
messageController.Body = message.Body; | ||
if (!string.IsNullOrWhiteSpace(message?.Recipient)) | ||
messageController.Recipients = new[] { message.Recipient }; | ||
|
||
// show the controller | ||
var tcs = new TaskCompletionSource<bool>(); | ||
messageController.Finished += (sender, e) => | ||
{ | ||
messageController.DismissViewController(true, null); | ||
tcs.SetResult(e.Result == MessageComposeResult.Sent); | ||
}; | ||
controller.PresentViewController(messageController, true, null); | ||
|
||
return tcs.Task; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.Caboodle | ||
{ | ||
public static partial class Sms | ||
{ | ||
public static Task ComposeAsync(SmsMessage message) | ||
=> throw new NotImplementedInReferenceAssemblyException(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.Caboodle | ||
{ | ||
public static partial class Sms | ||
{ | ||
public static Task ComposeAsync() | ||
=> ComposeAsync(null); | ||
} | ||
|
||
public class SmsMessage | ||
{ | ||
public SmsMessage() | ||
{ | ||
} | ||
|
||
public SmsMessage(string body, string recipient) | ||
{ | ||
Body = body; | ||
Recipient = recipient; | ||
} | ||
|
||
public string Body { get; set; } | ||
|
||
public string Recipient { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Windows.ApplicationModel.Chat; | ||
using Windows.Foundation.Metadata; | ||
|
||
namespace Microsoft.Caboodle | ||
{ | ||
public static partial class Sms | ||
{ | ||
static bool IsComposeSupported | ||
=> ApiInformation.IsTypePresent("Windows.ApplicationModel.Chat.ChatMessageManager"); | ||
|
||
public static Task ComposeAsync(SmsMessage message) | ||
{ | ||
if (!IsComposeSupported) | ||
throw new FeatureNotSupportedException(); | ||
|
||
var chat = new ChatMessage(); | ||
if (!string.IsNullOrWhiteSpace(message?.Body)) | ||
chat.Body = message.Body; | ||
if (!string.IsNullOrWhiteSpace(message?.Recipient)) | ||
chat.Recipients.Add(message.Recipient); | ||
|
||
return ChatMessageManager.ShowComposeSmsMessageAsync(chat).AsTask(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System.Threading.Tasks; | ||
using Microsoft.Caboodle; | ||
using Xunit; | ||
|
||
namespace Caboodle.DeviceTests | ||
{ | ||
public class Sms_Tests | ||
{ | ||
[Fact] | ||
public Task Sms_ComposeAsync_Does_Not_Throw() | ||
{ | ||
return Utils.OnMainThread(() => | ||
{ | ||
if (Utils.IsiOSSimulator) | ||
return Assert.ThrowsAsync<FeatureNotSupportedException>(() => Sms.ComposeAsync()); | ||
else | ||
return Sms.ComposeAsync(); | ||
}); | ||
} | ||
|
||
[Fact] | ||
public Task Sms_ComposeAsync_Does_Not_Throw_When_Empty() | ||
{ | ||
var message = new SmsMessage(); | ||
return Utils.OnMainThread(() => | ||
{ | ||
if (Utils.IsiOSSimulator) | ||
return Assert.ThrowsAsync<FeatureNotSupportedException>(() => Sms.ComposeAsync(message)); | ||
else | ||
return Sms.ComposeAsync(message); | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<Application xmlns="http://xamarin.com/schemas/2014/forms" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | ||
xmlns:converters="clr-namespace:Caboodle.Samples.Converters" | ||
x:Class="Caboodle.Samples.App"> | ||
<Application.Resources> | ||
|
||
<!-- Application resource dictionary --> | ||
|
||
<ResourceDictionary> | ||
<converters:NegativeConverter x:Key="NegativeConverter" /> | ||
</ResourceDictionary> | ||
</Application.Resources> | ||
</Application> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System; | ||
using System.Globalization; | ||
using Xamarin.Forms; | ||
|
||
namespace Caboodle.Samples.Converters | ||
{ | ||
public class NegativeConverter : IValueConverter | ||
{ | ||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) | ||
{ | ||
if (value is bool v) | ||
return !v; | ||
else | ||
return false; | ||
} | ||
|
||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) | ||
{ | ||
if (value is bool v) | ||
return !v; | ||
else | ||
return true; | ||
} | ||
} | ||
} |
Oops, something went wrong.