-
Notifications
You must be signed in to change notification settings - Fork 725
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(VibrationDevice): Support for Android
- Loading branch information
1 parent
1838e46
commit 1800b95
Showing
3 changed files
with
87 additions
and
1 deletion.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
src/Uno.UWP/Devices/Haptics/SimpleHapticsController.Android.cs
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,56 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Android.App; | ||
using Android.Views; | ||
using Microsoft.Extensions.Logging; | ||
using Uno.Extensions; | ||
using Uno.UI; | ||
|
||
namespace Windows.Devices.Haptics | ||
{ | ||
public partial class SimpleHapticsController | ||
{ | ||
public IReadOnlyList<SimpleHapticsControllerFeedback> SupportedFeedback { get; } = new SimpleHapticsControllerFeedback[] | ||
{ | ||
new SimpleHapticsControllerFeedback(KnownSimpleHapticsControllerWaveforms.Click, TimeSpan.Zero), | ||
new SimpleHapticsControllerFeedback(KnownSimpleHapticsControllerWaveforms.Press, TimeSpan.Zero) | ||
}; | ||
|
||
public void SendHapticFeedback(SimpleHapticsControllerFeedback feedback) | ||
{ | ||
if (ContextHelper.Current == null) | ||
{ | ||
throw new InvalidOperationException($"Context must be initialized before {nameof(SendHapticFeedback)} is called."); | ||
} | ||
try | ||
{ | ||
var activity = (Activity)ContextHelper.Current; | ||
var androidFeedback = FeedbackToAndroidFeedback(feedback); | ||
activity.Window.DecorView.PerformHapticFeedback(androidFeedback); | ||
} | ||
catch (Exception ex) | ||
{ | ||
if (this.Log().IsEnabled(LogLevel.Error)) | ||
{ | ||
this.Log().LogError($"Could not send haptic feedback: {ex}"); | ||
} | ||
} | ||
} | ||
|
||
private static FeedbackConstants FeedbackToAndroidFeedback(SimpleHapticsControllerFeedback feedback) | ||
{ | ||
if (feedback.Waveform == KnownSimpleHapticsControllerWaveforms.Click) | ||
{ | ||
return FeedbackConstants.ContextClick; | ||
} | ||
else if (feedback.Waveform == KnownSimpleHapticsControllerWaveforms.Press) | ||
{ | ||
return FeedbackConstants.LongPress; | ||
} | ||
else | ||
{ | ||
throw new NotSupportedException("Unsupported feedback waveform"); | ||
} | ||
} | ||
} | ||
} |
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,30 @@ | ||
using System.Threading.Tasks; | ||
using Android.App; | ||
using Android.Content.PM; | ||
using AndroidX.Core.Content; | ||
using Microsoft.Extensions.Logging; | ||
using Uno.Extensions; | ||
|
||
namespace Windows.Devices.Haptics | ||
{ | ||
public partial class VibrationDevice | ||
{ | ||
private const string VibratePermission = "android.permission.VIBRATE"; | ||
|
||
private static Task<VibrationAccessStatus> RequestAccessTaskAsync() | ||
{ | ||
if (ContextCompat.CheckSelfPermission(Application.Context, VibratePermission) == Permission.Denied) | ||
{ | ||
if (typeof(VibrationDevice).Log().IsEnabled(LogLevel.Warning)) | ||
{ | ||
typeof(VibrationDevice).Log().LogWarning($"Permission '{VibratePermission}' must be declared " + | ||
$"for the application to use {nameof(VibrationDevice)}."); | ||
} | ||
return Task.FromResult(VibrationAccessStatus.DeniedBySystem); | ||
} | ||
return Task.FromResult(VibrationAccessStatus.Allowed); | ||
} | ||
|
||
private static Task<VibrationDevice> GetDefaultTaskAsync() => Task.FromResult(new VibrationDevice()); | ||
} | ||
} |
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