Skip to content

Commit 9b42af8

Browse files
authored
Merge pull request #1464 from mortend/feature/sms
Fuse.Launcher.Phone: implement SMS launcher
2 parents 56dff68 + 9b39db7 commit 9b42af8

File tree

6 files changed

+145
-15
lines changed

6 files changed

+145
-15
lines changed

Source/Fuse.Launcher.Phone/Phone/JS.uno

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ namespace Fuse.Reactive.FuseJS
88
@scriptmodule FuseJS/Phone
99
1010
The Phone API allows you to launch your device's built-in
11-
phone app and make calls.
11+
phone app and make calls or send messages.
1212
1313
You need to add a reference to `"Fuse.Launcher"` in your project file to use this feature.
1414
1515
## Example
1616
```js
1717
var phone = require("FuseJS/Phone");
1818
phone.call("+47 123 45 678");
19+
phone.sms("+47 123 45 678", "Hi there");
1920
```
2021
*/
2122
public sealed class Phone : NativeModule
@@ -25,6 +26,7 @@ namespace Fuse.Reactive.FuseJS
2526
{
2627
Resource.SetGlobalKey(_instance = this, "FuseJS/Phone");
2728
AddMember(new NativeFunction("call", Call));
29+
AddMember(new NativeFunction("sms", Sms));
2830
}
2931

3032
/**
@@ -39,10 +41,31 @@ namespace Fuse.Reactive.FuseJS
3941
phone.call("+47 123 45 678");
4042
```
4143
*/
42-
public static object Call(Scripting.Context context, object[] args)
44+
public static object Call(Context context, object[] args)
4345
{
44-
string callString = (string)args[0];
45-
Fuse.LauncherImpl.PhoneLauncher.LaunchCall(callString);
46+
string phoneNumber = (string)args[0];
47+
Fuse.LauncherImpl.PhoneLauncher.LaunchCall(phoneNumber);
48+
return null;
49+
}
50+
51+
/**
52+
@scriptmethod sms(number, body)
53+
@param number (String) The number to to send a message
54+
@param body (String) The message to to send
55+
56+
Launches your device's messages app with the specified number.
57+
58+
## Example
59+
```js
60+
var phone = require("FuseJS/Phone");
61+
phone.sms("+47 123 45 678", "Hi there");
62+
```
63+
*/
64+
public static object Sms(Context context, object[] args)
65+
{
66+
string phoneNumber = (string)args[0];
67+
string body = args.Length > 1 ? (string)args[1] : null;
68+
Fuse.LauncherImpl.PhoneLauncher.LaunchSms(phoneNumber, body);
4669
return null;
4770
}
4871
}

Source/Fuse.Launcher.Phone/Phone/PhoneLauncher.uno

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ namespace Fuse.LauncherImpl
77
{
88
public static class PhoneLauncher
99
{
10-
public static void LaunchCall(string callString)
10+
public static void LaunchCall(string phoneNumber)
1111
{
1212
if defined(Android || iOS)
1313
{
14-
var uri = PhoneUriHelper.PhoneNumberToUri(callString);
14+
var uri = PhoneUriHelper.PhoneNumberToTelUri(phoneNumber);
1515

1616
if defined(Android)
1717
{
@@ -24,6 +24,24 @@ namespace Fuse.LauncherImpl
2424
}
2525
}
2626
}
27+
28+
public static void LaunchSms(string phoneNumber, string body)
29+
{
30+
if defined(Android || iOS)
31+
{
32+
var uri = PhoneUriHelper.PhoneNumberToSmsUri(phoneNumber, body);
33+
34+
if defined(Android)
35+
{
36+
var sms = new AndroidSms(uri);
37+
sms.Launch();
38+
}
39+
if defined(iOS)
40+
{
41+
iOSDeviceInterop.LaunchUriiOS(uri);
42+
}
43+
}
44+
}
2745
}
2846

2947
[ForeignInclude(Language.Java, "android.content.Intent", "android.net.Uri", "android.app.Activity")]
@@ -32,7 +50,7 @@ namespace Fuse.LauncherImpl
3250
string _uri;
3351

3452
[Foreign(Language.Java)]
35-
extern(Android) static string _actionCall
53+
extern(Android) static string _action
3654
{
3755
get
3856
@{
@@ -50,9 +68,34 @@ namespace Fuse.LauncherImpl
5068
Permissions.Request(Permissions.Android.CALL_PHONE).Then(OnPermitted);
5169
}
5270

53-
extern(Android) void OnPermitted(PlatformPermission permission)
71+
void OnPermitted(PlatformPermission permission)
72+
{
73+
AndroidDeviceInterop.LaunchIntent(_action, _uri);
74+
}
75+
}
76+
77+
[ForeignInclude(Language.Java, "android.content.Intent", "android.net.Uri", "android.app.Activity")]
78+
extern(Android) class AndroidSms
79+
{
80+
string _uri;
81+
82+
[Foreign(Language.Java)]
83+
extern(Android) static string _action
84+
{
85+
get
86+
@{
87+
return Intent.ACTION_VIEW;
88+
@}
89+
}
90+
91+
public AndroidSms(string uri)
92+
{
93+
_uri = uri;
94+
}
95+
96+
public void Launch()
5497
{
55-
AndroidDeviceInterop.LaunchIntent(_actionCall, _uri);
98+
AndroidDeviceInterop.LaunchIntent(_action, _uri);
5699
}
57100
}
58101
}

Source/Fuse.Launcher.Phone/Phone/PhoneTrigger.uno

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,41 @@ namespace Fuse.Triggers.Actions
3434
Fuse.LauncherImpl.PhoneLauncher.LaunchCall(Number);
3535
}
3636
}
37+
38+
/** Send a message to a phone number
39+
40+
You'll find this trigger action in the Fuse.Launcher package, which have to be referenced from your uno project.
41+
For example:
42+
```json
43+
{
44+
"Packages": [
45+
"Fuse",
46+
"FuseJS",
47+
"Fuse.Launcher"
48+
]
49+
}
50+
```
51+
## Example
52+
```xml
53+
<StackPanel Margin="20">
54+
<TextInput PlaceholderText="Enter the number here" ux:Name="numberInput" />
55+
<Button Margin="10" Text="Call" Alignment="Bottom">
56+
<Clicked>
57+
<Sms Number="{ReadProperty numberInput.Value}" Body="Hi there" />
58+
</Clicked>
59+
</Button>
60+
</StackPanel>
61+
```
62+
*/
63+
public class Sms : TriggerAction
64+
{
65+
public string Number {get; set;}
66+
67+
public string Body {get; set;}
68+
69+
protected override void Perform(Node target)
70+
{
71+
Fuse.LauncherImpl.PhoneLauncher.LaunchSms(Number, Body);
72+
}
73+
}
3774
}

Source/Fuse.Launcher.Phone/Phone/PhoneUriHelper.uno

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,27 @@ namespace Fuse
55
{
66
internal static class PhoneUriHelper
77
{
8-
public static string PhoneNumberToUri(string phoneNumber)
8+
public static string PhoneNumberToTelUri(string phoneNumber)
99
{
1010
var builder = new StringBuilder();
1111
builder.Append("tel:");
1212
builder.Append(Uri.EscapeDataString(phoneNumber));
1313
return builder.ToString();
1414
}
15+
16+
public static string PhoneNumberToSmsUri(string phoneNumber, string body)
17+
{
18+
var builder = new StringBuilder();
19+
builder.Append("sms:");
20+
builder.Append(Uri.EscapeDataString(phoneNumber));
21+
22+
if (!string.IsNullOrEmpty(body))
23+
{
24+
builder.Append("?body=");
25+
builder.Append(Uri.EscapeDataString(body));
26+
}
27+
28+
return builder.ToString();
29+
}
1530
}
1631
}

Source/Fuse.Launcher/Launcher.uno

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,19 @@ namespace Fuse
1111
{
1212
public static class Launcher
1313
{
14-
public static void LaunchUri(Uno.Net.Http.Uri uri)
14+
public static void LaunchUri(Uri uri)
1515
{
1616
InterAppLauncher.LaunchUri(uri);
1717
}
1818

19-
public static void LaunchCall(string callString)
19+
public static void LaunchCall(string phoneNumber)
2020
{
21-
PhoneLauncher.LaunchCall(callString);
21+
PhoneLauncher.LaunchCall(phoneNumber);
22+
}
23+
24+
public static void LaunchSms(string phoneNumber, string body)
25+
{
26+
PhoneLauncher.LaunchSms(phoneNumber, body);
2227
}
2328

2429
public static void LaunchMaps(double latitude, double longitude)

Source/Fuse.Launcher/Tests/PhoneUriHelper.Test.uno

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,17 @@ namespace Fuse.LauncherTest
88
public class PhoneUriHelperTest : TestBase
99
{
1010
[Test]
11-
public void PhoneNumberToUri_encodes_whitespace_correctly()
11+
public void PhoneNumberToTelUri_encodes_whitespace_correctly()
1212
{
13-
var phoneUri = PhoneUriHelper.PhoneNumberToUri("123 33 321");
13+
var phoneUri = PhoneUriHelper.PhoneNumberToTelUri("123 33 321");
1414
Assert.AreEqual("tel:123%2033%20321", phoneUri);
1515
}
16+
17+
[Test]
18+
public void PhoneNumberToSmsUri_encodes_whitespace_correctly()
19+
{
20+
var phoneUri = PhoneUriHelper.PhoneNumberToSmsUri("123 33 321", "hi");
21+
Assert.AreEqual("sms:123%2033%20321?body=hi", phoneUri);
22+
}
1623
}
1724
}

0 commit comments

Comments
 (0)