michael@0: /* michael@0: Licensed under the Apache License, Version 2.0 (the "License"); michael@0: you may not use this file except in compliance with the License. michael@0: You may obtain a copy of the License at michael@0: michael@0: http://www.apache.org/licenses/LICENSE-2.0 michael@0: michael@0: Unless required by applicable law or agreed to in writing, software michael@0: distributed under the License is distributed on an "AS IS" BASIS, michael@0: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: See the License for the specific language governing permissions and michael@0: limitations under the License. michael@0: */ michael@0: michael@0: using System; michael@0: using System.Windows; michael@0: using System.Windows.Controls; michael@0: using Microsoft.Devices; michael@0: using System.Runtime.Serialization; michael@0: using System.Threading; michael@0: using System.Windows.Resources; michael@0: using Microsoft.Phone.Controls; michael@0: using System.Diagnostics; michael@0: michael@0: michael@0: namespace WPCordovaClassLib.Cordova.Commands michael@0: { michael@0: public class Vibration : BaseCommand michael@0: { michael@0: private static readonly int DEFAULT_DURATION = 200; michael@0: michael@0: public void vibrate(string vibrateDuration) michael@0: { michael@0: int msecs = DEFAULT_DURATION; // set default michael@0: michael@0: try michael@0: { michael@0: string[] args = JSON.JsonHelper.Deserialize(vibrateDuration); michael@0: michael@0: msecs = int.Parse(args[0]); michael@0: if (msecs < 1) michael@0: { michael@0: msecs = 1; michael@0: } michael@0: } michael@0: catch (FormatException) michael@0: { michael@0: michael@0: } michael@0: michael@0: vibrateMs(msecs); michael@0: michael@0: // TODO: may need to add listener to trigger DispatchCommandResult when the vibration ends... michael@0: DispatchCommandResult(); michael@0: } michael@0: michael@0: private static void vibrateMs(int msecs) michael@0: { michael@0: VibrateController.Default.Start(TimeSpan.FromMilliseconds(msecs)); michael@0: } michael@0: michael@0: public void vibrateWithPattern(string options) michael@0: { michael@0: // falling back to vibrate michael@0: vibrateMs(DEFAULT_DURATION); michael@0: michael@0: // TODO: may need to add listener to trigger DispatchCommandResult when the vibration ends... michael@0: DispatchCommandResult(); michael@0: } michael@0: michael@0: public void cancelVibration(string options) michael@0: { michael@0: VibrateController.Default.Stop(); michael@0: DispatchCommandResult(); michael@0: } michael@0: } michael@0: }