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 505
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
47 changed files
with
586 additions
and
73 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
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,15 @@ | ||
using System; | ||
using Xamarin.Essentials; | ||
using Xunit; | ||
|
||
namespace DeviceTests | ||
{ | ||
public class HapticFeedback_Tests | ||
{ | ||
[Fact] | ||
public void Click() => HapticFeedback.Perform(HapticFeedbackType.Click); | ||
|
||
[Fact] | ||
public void LongPress() => HapticFeedback.Perform(HapticFeedbackType.LongPress); | ||
} | ||
} |
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,24 @@ | ||
<views:BasePage xmlns="http://xamarin.com/schemas/2014/forms" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | ||
xmlns:views="clr-namespace:Samples.View" | ||
xmlns:viewmodels="clr-namespace:Samples.ViewModel" | ||
x:Class="Samples.View.HapticFeedbackPage" | ||
Title="Vibration"> | ||
<views:BasePage.BindingContext> | ||
<viewmodels:HapticFeedbackViewModel /> | ||
</views:BasePage.BindingContext> | ||
|
||
<StackLayout> | ||
<Label Text="Quickly and easily make the device provide haptic feedback." FontAttributes="Bold" Margin="12" /> | ||
|
||
<ScrollView> | ||
<StackLayout Padding="12,0,12,12" Spacing="6"> | ||
<Button Text="Click" Command="{Binding ClickCommand}" /> | ||
<Button Text="LongPress" Command="{Binding LongPressCommand}" /> | ||
<Label Text="HapticFeadback is not supported." TextColor="Red" FontAttributes="Italic" | ||
IsVisible="{Binding IsSupported, Converter={StaticResource NegativeConverter}}" /> | ||
</StackLayout> | ||
</ScrollView> | ||
</StackLayout> | ||
|
||
</views:BasePage> |
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 @@ | ||
namespace Samples.View | ||
{ | ||
public partial class HapticFeedbackPage : BasePage | ||
{ | ||
public HapticFeedbackPage() | ||
{ | ||
InitializeComponent(); | ||
} | ||
} | ||
} |
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,60 @@ | ||
using System; | ||
using System.Windows.Input; | ||
using Xamarin.Essentials; | ||
using Xamarin.Forms; | ||
|
||
namespace Samples.ViewModel | ||
{ | ||
public class HapticFeedbackViewModel : BaseViewModel | ||
{ | ||
bool isSupported = true; | ||
|
||
public HapticFeedbackViewModel() | ||
{ | ||
ClickCommand = new Command(OnClick); | ||
LongPressCommand = new Command(OnLongPress); | ||
} | ||
|
||
public ICommand ClickCommand { get; } | ||
|
||
public ICommand LongPressCommand { get; } | ||
|
||
public bool IsSupported | ||
{ | ||
get => isSupported; | ||
set => SetProperty(ref isSupported, value); | ||
} | ||
|
||
void OnClick() | ||
{ | ||
try | ||
{ | ||
HapticFeedback.Perform(HapticFeedbackType.Click); | ||
} | ||
catch (FeatureNotSupportedException) | ||
{ | ||
IsSupported = false; | ||
} | ||
catch (Exception ex) | ||
{ | ||
DisplayAlertAsync($"Unable to HapticFeedback: {ex.Message}"); | ||
} | ||
} | ||
|
||
void OnLongPress() | ||
{ | ||
try | ||
{ | ||
HapticFeedback.Perform(HapticFeedbackType.LongPress); | ||
} | ||
catch (FeatureNotSupportedException) | ||
{ | ||
IsSupported = false; | ||
} | ||
catch (Exception ex) | ||
{ | ||
DisplayAlertAsync($"Unable to HapticFeedback: {ex.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
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
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
33 changes: 33 additions & 0 deletions
33
Xamarin.Essentials/HapticFeedback/HapticFeedback.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,33 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Threading.Tasks; | ||
using Android.Views; | ||
|
||
namespace Xamarin.Essentials | ||
{ | ||
public static partial class HapticFeedback | ||
{ | ||
internal static bool IsSupported => true; | ||
|
||
static void PlatformPerform(HapticFeedbackType type) | ||
{ | ||
Permissions.EnsureDeclared<Permissions.Vibrate>(); | ||
|
||
try | ||
{ | ||
Platform.CurrentActivity?.Window?.DecorView?.PerformHapticFeedback(ConvertType(type)); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Debug.WriteLine($"HapticFeedback Exception: {ex.Message}"); | ||
} | ||
} | ||
|
||
static FeedbackConstants ConvertType(HapticFeedbackType type) => | ||
type switch | ||
{ | ||
HapticFeedbackType.LongPress => FeedbackConstants.LongPress, | ||
_ => FeedbackConstants.ContextClick | ||
}; | ||
} | ||
} |
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,46 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using UIKit; | ||
|
||
namespace Xamarin.Essentials | ||
{ | ||
public static partial class HapticFeedback | ||
{ | ||
internal static bool IsSupported => true; | ||
|
||
static void PlatformPerform(HapticFeedbackType type) | ||
{ | ||
switch (type) | ||
{ | ||
case HapticFeedbackType.LongPress: | ||
PlatformLongPress(); | ||
break; | ||
default: | ||
PlatformClick(); | ||
break; | ||
} | ||
} | ||
|
||
static void PlatformClick() | ||
{ | ||
if (Platform.HasOSVersion(10, 0)) | ||
{ | ||
var impact = new UIImpactFeedbackGenerator(UIImpactFeedbackStyle.Light); | ||
impact.Prepare(); | ||
impact.ImpactOccurred(); | ||
impact.Dispose(); | ||
} | ||
} | ||
|
||
static void PlatformLongPress() | ||
{ | ||
if (Platform.HasOSVersion(10, 0)) | ||
{ | ||
var impact = new UIImpactFeedbackGenerator(UIImpactFeedbackStyle.Medium); | ||
impact.Prepare(); | ||
impact.ImpactOccurred(); | ||
impact.Dispose(); | ||
} | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
Xamarin.Essentials/HapticFeedback/HapticFeedback.netstandard.tvos.watchos.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,14 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace Xamarin.Essentials | ||
{ | ||
public static partial class HapticFeedback | ||
{ | ||
internal static bool IsSupported | ||
=> throw ExceptionUtils.NotSupportedOrImplementedException; | ||
|
||
static void PlatformPerform(HapticFeedbackType type) | ||
=> throw ExceptionUtils.NotSupportedOrImplementedException; | ||
} | ||
} |
Oops, something went wrong.