forked from microsoft/react-native-windows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VibrationModule.cs
79 lines (71 loc) · 2.16 KB
/
VibrationModule.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright (c) Microsoft Corporation. All rights reserved.
// Portions derived from React Native:
// Copyright (c) 2015-present, Facebook, Inc.
// Licensed under the MIT License.
using ReactNative.Bridge;
using ReactNative.Common;
using System;
using Windows.Phone.Devices.Notification;
namespace ReactNative.Modules.Vibration
{
/// <summary>
/// Represents the module for vibration. Note this is only available on Windows Mobile.
/// </summary>
public class VibrationModule : NativeModuleBase
{
private readonly bool _isMobile;
/// <summary>
/// Creates a new instance of the Vibration Module.
/// </summary>
public VibrationModule() : this(IsMobile())
{
}
/// <summary>
/// Creates a new instance of the Vibration Module with a flag of whether the platform is mobile.
/// </summary>
/// <param name="isMobile"></param>
public VibrationModule(bool isMobile)
{
_isMobile = isMobile;
}
private static bool IsMobile()
{
return WindowsPlatformHelper.DeviceFamily == DeviceFamilyType.Mobile;
}
/// <summary>
/// The name of the module.
/// </summary>
public override string Name
{
get
{
return "Vibration";
}
}
/// <summary>
/// Vibrates the device for the specified milliseconds.
/// </summary>
/// <param name="duration">The duration in milliseconds to vibrate.</param>
[ReactMethod]
public void vibrate(int duration)
{
if (_isMobile)
{
var vibrationDevice = VibrationDevice.GetDefault();
vibrationDevice.Vibrate(TimeSpan.FromMilliseconds(duration));
}
}
/// <summary>
/// Cancels the current vibration.
/// </summary>
[ReactMethod]
public void cancel()
{
if (_isMobile)
{
var vibrationDevice = VibrationDevice.GetDefault();
vibrationDevice.Cancel();
}
}
}
}