Files
win8phoneApp/CampusAppWP8/CampusAppWP8/Utility/Utilities.cs
2013-10-10 11:45:19 +02:00

375 lines
13 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.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using CampusAppWP8.Resources;
using Microsoft.Phone.Net.NetworkInformation;
using CampusAppWPortalLib8.Utility;
using CampusAppWPortalLib8.Model;
using Microsoft.Phone.Shell;
/// <summary>
/// Collection of utility functions.
/// </summary>
public static class Utilities
{
#region Enums
/// <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
}
#endregion
#region Method
/// <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);
}
else
{
retValue.AddRange(Utilities.GetChild(child as DependencyObject, elemName));
}
}
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);
// hotfix: OutofRangeIndex Assert
if (index > l.Count - 1)
{
return;
}
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 set current positon to Zero
/// </summary>
public static void SetGeoPositionToZero()
{
string time = DateTime.Now.Ticks.ToString();
App.SaveToAppState<string>(Constants.GeoWatch_CurrentPosition_Lat, "0");
App.SaveToAppState<string>(Constants.GeoWatch_CurrentPosition_Long, "0");
App.SaveToAppState<string>(Constants.GeoWatch_CurrentPosition_Time, time);
}
/// <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();
}
}
}
/// <summary>Query if the phone is in the uni network. Method compares only Network name and Description!</summary>
/// <remarks>Stubbfel, 26.08.2013.</remarks>
/// <returns>true if uni network is available, false if not.</returns>
public static bool IsUniNetworkAvailable()
{
NetworkInterfaceList networkInterfaceList = new NetworkInterfaceList();
foreach (NetworkInterfaceInfo networkInterfaceInfo in networkInterfaceList)
{
if (networkInterfaceInfo.InterfaceType == NetworkInterfaceType.Wireless80211
&& networkInterfaceInfo.InterfaceSubtype == NetworkInterfaceSubType.WiFi
&& networkInterfaceInfo.InterfaceName.Equals(Constants.AppSetting_UniNetworkName)
&& networkInterfaceInfo.Description.Equals(Constants.AppSetting_UniNetworkDesc)
&& networkInterfaceInfo.InterfaceState == ConnectState.Connected)
{
return true;
}
}
return false;
}
/// <summary>Queries if a wifi is available.</summary>
/// <remarks>Stubbfel, 26.08.2013.</remarks>
/// <returns>true if a wifi is available, false if not.</returns>
public static bool IsWifiAvailable()
{
NetworkInterfaceList networkInterfaceList = new NetworkInterfaceList();
foreach (NetworkInterfaceInfo networkInterfaceInfo in networkInterfaceList)
{
if (networkInterfaceInfo.InterfaceType == NetworkInterfaceType.Wireless80211
&& networkInterfaceInfo.InterfaceSubtype == NetworkInterfaceSubType.WiFi
&& networkInterfaceInfo.InterfaceState == ConnectState.Connected)
{
return true;
}
}
return false;
}
/// <summary>Gets load modus. Is check if the only Wifi option is active</summary>
/// <remarks>Stubbfel, 27.08.2013.</remarks>
/// <typeparam name="T">Generic type parameter.</typeparam>
/// <returns>The load modus&lt; t&gt;</returns>
public static ForceType GetLoadModus<T>()
{
if (Settings.AppSetting.OnlyWifi && !Settings.AppSetting.WifiEnable)
{
return ForceType.FORCE_FILE;
}
else
{
return ForceType.INVALID;
}
}
public static bool IsRoomId(string placeId)
{
if (Wp8StringManager.IsDigitsOnly(placeId) && placeId.Length > 7)
{
return true;
}
return false;
}
#endregion
}
}