290 lines
10 KiB
C#
290 lines
10 KiB
C#
//-----------------------------------------------------------------------------
|
|
// <copyright file="Utilities.cs" company="BTU/IIT">
|
|
// Company copyright tag.
|
|
// </copyright>
|
|
// <author>fiedlchr</author>
|
|
// <sience>16.07.2013</sience>
|
|
//-----------------------------------------------------------------------------
|
|
namespace CampusAppWP8.Utility
|
|
{
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Device.Location;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
using System.Runtime.InteropServices.WindowsRuntime;
|
|
using System.Threading;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Media;
|
|
using CampusAppWP8.Resources;
|
|
using CampusAppWP8.Utility.NDEF;
|
|
using Windows.Networking.Proximity;
|
|
|
|
/// <summary>
|
|
/// Collection of utility functions.
|
|
/// </summary>
|
|
public static class Utilities
|
|
{
|
|
/// <summary>
|
|
/// ResetEvent for CampusDetermination
|
|
/// </summary>
|
|
private static ManualResetEvent waitForCampus = new ManualResetEvent(false);
|
|
|
|
/// <summary>
|
|
/// Comparison types.
|
|
/// </summary>
|
|
public enum DifferenceType
|
|
{
|
|
/// <summary>
|
|
/// Compare Equality.
|
|
/// </summary>
|
|
Equal,
|
|
|
|
/// <summary>
|
|
/// Compare if less.
|
|
/// </summary>
|
|
Less,
|
|
|
|
/// <summary>
|
|
/// Compare equality or less.
|
|
/// </summary>
|
|
LessEqual,
|
|
|
|
/// <summary>
|
|
/// Compare greater.
|
|
/// </summary>
|
|
Greater,
|
|
|
|
/// <summary>
|
|
/// Compare equality or greater.
|
|
/// </summary>
|
|
GreaterEqual
|
|
}
|
|
|
|
/// <summary>
|
|
/// Compares the difference between a specified DateTime and Now
|
|
/// and the specified time difference (in Days).
|
|
/// <example>
|
|
/// When type is DifferenceType.Less, the timespan between Now and date
|
|
/// is 5.0 (days) and totalDiff is 7.0, the function will return true,
|
|
/// because the timespan is Less then 7.0 .
|
|
/// </example>
|
|
/// </summary>
|
|
/// <param name="type">comparison type</param>
|
|
/// <param name="date">date to check</param>
|
|
/// <param name="totalDiff">difference to check</param>
|
|
/// <returns>true, if the comparison condition do not fail</returns>
|
|
public static bool DayDifference(DifferenceType type, DateTime date, double totalDiff)
|
|
{
|
|
bool retValue = false;
|
|
|
|
TimeSpan diff = DateTime.Now.Subtract(date);
|
|
|
|
if ((DifferenceType.Less == type) || (DifferenceType.LessEqual == type))
|
|
{
|
|
if (diff.TotalDays < totalDiff)
|
|
{
|
|
retValue = true;
|
|
}
|
|
}
|
|
|
|
if ((DifferenceType.Greater == type) || (DifferenceType.GreaterEqual == type))
|
|
{
|
|
if (diff.TotalDays > totalDiff)
|
|
{
|
|
retValue = true;
|
|
}
|
|
}
|
|
|
|
if ((DifferenceType.Equal == type) || (DifferenceType.LessEqual == type) || (DifferenceType.GreaterEqual == type))
|
|
{
|
|
if (diff.TotalDays == totalDiff)
|
|
{
|
|
retValue = true;
|
|
}
|
|
}
|
|
|
|
return retValue;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Return a list of child UIElements of the root object with the specified element name.
|
|
/// </summary>
|
|
/// <param name="rootObj">root object</param>
|
|
/// <param name="elemName">name of the element(s)</param>
|
|
/// <returns>list of elements</returns>
|
|
public static List<DependencyObject> GetChild(DependencyObject rootObj, string elemName)
|
|
{
|
|
List<DependencyObject> retValue = new List<DependencyObject>();
|
|
|
|
for (int k = 0; k < VisualTreeHelper.GetChildrenCount(rootObj); k++)
|
|
{
|
|
var child = VisualTreeHelper.GetChild(rootObj, k);
|
|
|
|
if ((child as FrameworkElement).Name.Equals(elemName))
|
|
{
|
|
retValue.Add(child);
|
|
}
|
|
|
|
var ret = retValue.Concat<DependencyObject>(GetChild(child as DependencyObject, elemName));
|
|
retValue = ret.ToList<DependencyObject>();
|
|
}
|
|
|
|
return retValue;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Search for a UIElement with the specified name in the parent Grid and set its visibility.
|
|
/// </summary>
|
|
/// <param name="rootObj">root object</param>
|
|
/// <param name="parentGridName">name of the parent grid</param>
|
|
/// <param name="elemName">name of the UIElement</param>
|
|
/// <param name="vis">new visibility property state</param>
|
|
/// <param name="index">index of the element in the parent child list</param>
|
|
public static void SetElementVisibility(DependencyObject rootObj, string parentGridName, string elemName, Visibility vis, int index = 0)
|
|
{
|
|
List<DependencyObject> l = Utilities.GetChild(rootObj, parentGridName);
|
|
Grid parentGrid = l[index] as Grid;
|
|
FrameworkElement elem = null;
|
|
|
|
foreach (FrameworkElement tempElem in parentGrid.Children)
|
|
{
|
|
if (tempElem.Name == elemName)
|
|
{
|
|
elem = tempElem;
|
|
}
|
|
}
|
|
|
|
if (elem != null)
|
|
{
|
|
elem.Visibility = vis;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Return the visibility property of a UIElement which is a child object of the specified parent grid element.
|
|
/// </summary>
|
|
/// <param name="rootObj">root object</param>
|
|
/// <param name="parentGridName">name of the parent grid</param>
|
|
/// <param name="elemName">name of the element</param>
|
|
/// <param name="index">index of the element in the child list of the parent</param>
|
|
/// <returns>visibility state</returns>
|
|
public static Visibility GetElementVisibility(DependencyObject rootObj, string parentGridName, string elemName, int index = 0)
|
|
{
|
|
Visibility retValue;
|
|
|
|
List<DependencyObject> l = Utilities.GetChild(rootObj, parentGridName);
|
|
|
|
if (index >= l.Count)
|
|
{
|
|
index = 0;
|
|
}
|
|
|
|
Grid parentGrid = l[index] as Grid;
|
|
FrameworkElement elem = null;
|
|
|
|
foreach (FrameworkElement tempElem in parentGrid.Children)
|
|
{
|
|
if (tempElem.Name == elemName)
|
|
{
|
|
elem = tempElem;
|
|
}
|
|
}
|
|
|
|
if (elem != null)
|
|
{
|
|
retValue = elem.Visibility;
|
|
}
|
|
else
|
|
{
|
|
throw new NotImplementedException("Could not find a UIElement with name (" + elemName + ")");
|
|
}
|
|
|
|
return retValue;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Method determine the current position of the phone
|
|
/// </summary>
|
|
/// <returns>the position of the phone</returns>
|
|
public static GeoPosition<GeoCoordinate> DetermineCurrentPosition()
|
|
{
|
|
if (!Settings.AppSetting.GeoWatchEnable)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
GeoCoordinateWatcher watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
|
|
bool success = watcher.TryStart(false, TimeSpan.FromMilliseconds(10000));
|
|
GeoPosition<GeoCoordinate> geoposition = null;
|
|
if (success)
|
|
{
|
|
geoposition = watcher.Position;
|
|
}
|
|
else if (watcher.Permission.Equals(GeoPositionPermission.Denied))
|
|
{
|
|
// set to 0 point, if access to device is not allow
|
|
geoposition = new GeoPosition<GeoCoordinate>();
|
|
geoposition.Location = new GeoCoordinate(0, 0);
|
|
geoposition.Timestamp = DateTime.Now;
|
|
}
|
|
|
|
watcher.Stop();
|
|
return geoposition;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Method determine and store the current position of the phone
|
|
/// </summary>
|
|
public static void DetermineAndStoreCurrentPositionForce()
|
|
{
|
|
GeoPosition<GeoCoordinate> geoposition = Utilities.DetermineCurrentPosition();
|
|
|
|
if (geoposition != null)
|
|
{
|
|
string lat = geoposition.Location.Latitude.ToString(CultureInfo.InvariantCulture);
|
|
string log = geoposition.Location.Longitude.ToString(CultureInfo.InvariantCulture);
|
|
string time = geoposition.Timestamp.Ticks.ToString();
|
|
try
|
|
{
|
|
App.SaveToAppState<string>(Constants.GeoWatch_CurrentPosition_Lat, lat);
|
|
App.SaveToAppState<string>(Constants.GeoWatch_CurrentPosition_Long, log);
|
|
App.SaveToAppState<string>(Constants.GeoWatch_CurrentPosition_Time, time);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger.LogException(ex);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Method determine and store the current position of the phone, in 15 min interval
|
|
/// </summary>
|
|
public static void DetermineAndStoreCurrentPosition()
|
|
{
|
|
string lat = App.LoadFromAppState<string>(Constants.GeoWatch_CurrentPosition_Lat);
|
|
string log = App.LoadFromAppState<string>(Constants.GeoWatch_CurrentPosition_Long);
|
|
string time = App.LoadFromAppState<string>(Constants.GeoWatch_CurrentPosition_Time);
|
|
if (lat == null || log == null || time == null || lat.Equals(string.Empty) || log.Equals(string.Empty) || time.Equals(string.Empty))
|
|
{
|
|
Utilities.DetermineAndStoreCurrentPositionForce();
|
|
}
|
|
else
|
|
{
|
|
long longTime = 0;
|
|
if (!long.TryParse(time, out longTime))
|
|
{
|
|
return;
|
|
}
|
|
|
|
DateTime expired = new DateTime(longTime).AddMinutes(15);
|
|
if (DateTime.Now.Ticks > expired.Ticks)
|
|
{
|
|
Utilities.DetermineAndStoreCurrentPositionForce();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |