Skip to content

Commit

Permalink
feat(VibrationDevice): Support for Android
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed Oct 14, 2020
1 parent 1838e46 commit 1800b95
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
56 changes: 56 additions & 0 deletions src/Uno.UWP/Devices/Haptics/SimpleHapticsController.Android.cs
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");
}
}
}
}
30 changes: 30 additions & 0 deletions src/Uno.UWP/Devices/Haptics/VibrationDevice.Android.cs
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());
}
}
2 changes: 1 addition & 1 deletion src/Uno.UWP/Devices/Haptics/VibrationDevice.unsupported.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if NET461 || __SKIA__ || __NETSTD_REFERENCE__

using System.Threading.Tasks;

Expand Down

0 comments on commit 1800b95

Please sign in to comment.