add openhack files
This commit is contained in:
@ -0,0 +1,136 @@
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for details.
|
||||
|
||||
using Android.Content;
|
||||
using Android.Hardware;
|
||||
using Java.Lang;
|
||||
using Exception = System.Exception;
|
||||
using Math = System.Math;
|
||||
|
||||
namespace MyDriving.Droid.Helpers
|
||||
{
|
||||
public interface IAccelerometerListener
|
||||
{
|
||||
void OnAccelerationChanged(float x, float y, float z);
|
||||
void OnShake(float force);
|
||||
}
|
||||
|
||||
public class AccelerometerManager
|
||||
{
|
||||
readonly ShakeSensorEventListener eventListener;
|
||||
readonly SensorManager sensorManager;
|
||||
Sensor sensor;
|
||||
|
||||
public AccelerometerManager(Context context, IAccelerometerListener listener)
|
||||
{
|
||||
eventListener = new ShakeSensorEventListener(listener);
|
||||
sensorManager = (SensorManager) context.GetSystemService(Context.SensorService);
|
||||
IsSupported = sensorManager.GetSensorList(SensorType.Accelerometer).Count > 0;
|
||||
}
|
||||
|
||||
public bool IsSupported { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets if the manager is listening to orientation changes
|
||||
/// </summary>
|
||||
public bool IsListening { get; private set; }
|
||||
|
||||
public void Configure(int threshold, int interval)
|
||||
{
|
||||
eventListener.Threshold = threshold;
|
||||
eventListener.Interval = interval;
|
||||
}
|
||||
|
||||
public void StopListening()
|
||||
{
|
||||
IsListening = false;
|
||||
try
|
||||
{
|
||||
if (sensorManager != null && eventListener != null)
|
||||
{
|
||||
sensorManager.UnregisterListener(eventListener);
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public void StartListening()
|
||||
{
|
||||
var sensors = sensorManager.GetSensorList(SensorType.Accelerometer);
|
||||
if (sensors.Count > 0)
|
||||
{
|
||||
sensor = sensors[0];
|
||||
IsListening = sensorManager.RegisterListener(eventListener, sensor, SensorDelay.Game);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class ShakeSensorEventListener : Object, ISensorEventListener
|
||||
{
|
||||
readonly IAccelerometerListener listener;
|
||||
long now, timeDiff, lastUpdate, lastShake;
|
||||
float x, y, z, lastX, lastY, lastZ, force;
|
||||
|
||||
|
||||
public ShakeSensorEventListener(IAccelerometerListener listener)
|
||||
{
|
||||
this.listener = listener;
|
||||
Threshold = 25.0f;
|
||||
Interval = 200;
|
||||
}
|
||||
|
||||
//Accuracy Configuration
|
||||
public float Threshold { get; set; }
|
||||
public int Interval { get; set; }
|
||||
|
||||
public void OnAccuracyChanged(Sensor sensor, SensorStatus accuracy)
|
||||
{
|
||||
}
|
||||
|
||||
public void OnSensorChanged(SensorEvent e)
|
||||
{
|
||||
try
|
||||
{
|
||||
now = e.Timestamp;
|
||||
x = e.Values[0];
|
||||
y = e.Values[1];
|
||||
z = e.Values[2];
|
||||
|
||||
if (lastUpdate == 0)
|
||||
{
|
||||
lastUpdate = now;
|
||||
lastShake = now;
|
||||
lastX = x;
|
||||
lastY = y;
|
||||
lastZ = z;
|
||||
}
|
||||
else
|
||||
{
|
||||
timeDiff = now - lastUpdate;
|
||||
if (timeDiff <= 0)
|
||||
return;
|
||||
|
||||
force = Math.Abs(x + y + z - lastX - lastY - lastZ);
|
||||
if (Float.Compare(force, Threshold) > 0)
|
||||
{
|
||||
if (now - lastShake >= Interval)
|
||||
listener.OnShake(force);
|
||||
|
||||
lastShake = now;
|
||||
}
|
||||
lastX = x;
|
||||
lastY = y;
|
||||
lastZ = z;
|
||||
lastUpdate = now;
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
listener.OnAccelerationChanged(x, y, z);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for details.
|
||||
|
||||
using Android.Gms.Maps.Model;
|
||||
using MyDriving.DataObjects;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace MyDriving.Droid.Helpers
|
||||
{
|
||||
public static class LocationExtensions
|
||||
{
|
||||
public static LatLng ToLatLng(this TripPoint point) => new LatLng(point.Latitude, point.Longitude);
|
||||
|
||||
public static LatLng ToLatLng(this Plugin.Geolocator.Abstractions.Position point)
|
||||
=> new LatLng(point.Latitude, point.Longitude);
|
||||
|
||||
public static List<LatLng> ToLatLngs(this IEnumerable<TripPoint> points)
|
||||
=> new List<LatLng>(points.Select(s => s.ToLatLng()));
|
||||
}
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for details.
|
||||
|
||||
using System;
|
||||
using MyDriving.Utils;
|
||||
using MyDriving.Utils.Interfaces;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.WindowsAzure.MobileServices;
|
||||
using Plugin.CurrentActivity;
|
||||
|
||||
namespace MyDriving.Droid.Helpers
|
||||
{
|
||||
public class Authentication : IAuthentication
|
||||
{
|
||||
public async Task<MobileServiceUser> LoginAsync(IMobileServiceClient client, MobileServiceAuthenticationProvider provider)
|
||||
{
|
||||
MobileServiceUser user = null;
|
||||
|
||||
try
|
||||
{
|
||||
Settings.Current.LoginAttempts++;
|
||||
|
||||
user = await client.LoginAsync(CrossCurrentActivity.Current.Activity, provider);
|
||||
Settings.Current.AuthToken = user?.MobileServiceAuthenticationToken ?? string.Empty;
|
||||
Settings.Current.AzureMobileUserId = user?.UserId ?? string.Empty;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
//Don't log if the user cancelled out of the login screen
|
||||
if (!e.Message.Contains("cancelled"))
|
||||
{
|
||||
e.Data["method"] = "LoginAsync";
|
||||
Logger.Instance.Report(e);
|
||||
}
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
public void ClearCookies()
|
||||
{
|
||||
try
|
||||
{
|
||||
if ((int) Android.OS.Build.VERSION.SdkInt >= 21)
|
||||
Android.Webkit.CookieManager.Instance.RemoveAllCookies(null);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Instance.Report(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for details.
|
||||
|
Reference in New Issue
Block a user