Created
September 6, 2012 13:40
-
-
Save nicwise/3656362 to your computer and use it in GitHub Desktop.
utils class
This file contains hidden or 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
using System; | |
using MonoTouch.Foundation; | |
using MonoTouch.UIKit; | |
using MonoTouch.Dialog; | |
using System.IO; | |
using MonoTouch.MessageUI; | |
using System.Threading; | |
using MonoTouch.ObjCRuntime; | |
using System.Globalization; | |
using System.Xml.Linq; | |
using System.Drawing; | |
using MonoTouch.CoreGraphics; | |
using System.Runtime.InteropServices; | |
using System.Diagnostics; | |
using System.Text; | |
namespace BigTed | |
{ | |
public class Util | |
{ | |
public class DeviceHardware | |
{ | |
public const string HardwareProperty = "hw.machine"; | |
// Changing the constant to "/usr/bin/libSystem.dylib" allows this P/Invoke to work on Mac OS X | |
// Using "hw.model" as property gives Macintosh model, "hw.machine" kernel arch (ppc, ppc64, i386, x86_64) | |
[DllImport(MonoTouch.Constants.SystemLibrary)] | |
internal static extern int sysctlbyname( [MarshalAs(UnmanagedType.LPStr)] string property, // name of the property | |
IntPtr output, // output | |
IntPtr oldLen, // IntPtr.Zero | |
IntPtr newp, // IntPtr.Zero | |
uint newlen // 0 | |
); | |
public static string Version | |
{ | |
get | |
{ | |
// get the length of the string that will be returned | |
var pLen = Marshal.AllocHGlobal(sizeof(int)); | |
sysctlbyname(DeviceHardware.HardwareProperty, IntPtr.Zero, pLen, IntPtr.Zero, 0); | |
var length = Marshal.ReadInt32(pLen); | |
// check to see if we got a length | |
if (length == 0) | |
{ | |
Marshal.FreeHGlobal(pLen); | |
return "Unknown"; | |
} | |
// get the hardware string | |
var pStr = Marshal.AllocHGlobal(length); | |
sysctlbyname(DeviceHardware.HardwareProperty, pStr, pLen, IntPtr.Zero, 0); | |
// convert the native string into a C# string | |
var hardwareStr = Marshal.PtrToStringAnsi(pStr); | |
var ret = hardwareStr; | |
// cleanup | |
Marshal.FreeHGlobal(pLen); | |
Marshal.FreeHGlobal(pStr); | |
return ret; | |
} | |
} | |
} | |
public static void LogStartup() | |
{ | |
if (DebugMode) | |
{ | |
Log("MobileAgent Startup"); | |
Log("SysName: {0}", UIDevice.CurrentDevice.SystemName); | |
Log("SysVer: {0}", UIDevice.CurrentDevice.SystemVersion); | |
Log("Name: {0}", UIDevice.CurrentDevice.Name); | |
Log("Model: {0}", UIDevice.CurrentDevice.Model); | |
Log("Local Model: {0}", UIDevice.CurrentDevice.LocalizedModel); | |
Log("Device: {0}", DeviceHardware.Version); | |
Log("Multitask: {0}", UIDevice.CurrentDevice.IsMultitaskingSupported.ToString()); | |
Log("Log filename: {0}", LogFilename); | |
} | |
} | |
public static string DeviceInfo() | |
{ | |
StringBuilder sb = new StringBuilder(); | |
sb.AppendFormat("SysName: {0}\n", UIDevice.CurrentDevice.SystemName); | |
sb.AppendFormat("SysVer: {0}\n", UIDevice.CurrentDevice.SystemVersion); | |
sb.AppendFormat("Name: {0}\n", UIDevice.CurrentDevice.Name); | |
sb.AppendFormat("Model: {0}\n", UIDevice.CurrentDevice.Model); | |
sb.AppendFormat("Local Model: {0}\n", UIDevice.CurrentDevice.LocalizedModel); | |
sb.AppendFormat("Device: {0}\n", DeviceHardware.Version); | |
sb.AppendFormat("Multitask: {0}\n", UIDevice.CurrentDevice.IsMultitaskingSupported.ToString()); | |
return sb.ToString(); | |
} | |
public static string RootPath | |
{ | |
get | |
{ | |
return Path.Combine(Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments), ".."); | |
} | |
} | |
public static string DocumentPath | |
{ | |
get | |
{ | |
return Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments); | |
} | |
} | |
public static string LibraryPath | |
{ | |
get | |
{ | |
return Path.Combine(RootPath, "Library"); | |
} | |
} | |
public static string TempPath | |
{ | |
get | |
{ | |
return Path.Combine(RootPath, "tmp"); | |
} | |
} | |
public static string AppVersion | |
{ | |
get | |
{ | |
return NSBundle.MainBundle.ObjectForInfoDictionary("CFBundleVersion").ToString(); | |
} | |
} | |
public static string CurrentVersion | |
{ | |
get | |
{ | |
using (NSUserDefaults defaults = NSUserDefaults.StandardUserDefaults) { | |
return defaults.StringForKey ("appversion") ?? "1.0"; | |
} | |
} | |
set | |
{ | |
using (NSUserDefaults defaults = NSUserDefaults.StandardUserDefaults) { | |
defaults.SetString(AppVersion, "appversion"); | |
defaults.Synchronize(); | |
} | |
} | |
} | |
public static void Log(string message, params object[] param) | |
{ | |
string str = ""; | |
try | |
{ | |
str = message.Fmt (param); | |
} catch { | |
str = message; | |
} | |
#if DEBUG | |
Debug.WriteLine(str); | |
return; | |
#endif | |
if (DebugMode) | |
{ | |
//if (Util.UseTestFlight) MonoTouch.TestFlight.TestFlight.Log (str); | |
Debug.WriteLine(str); | |
string msg = string.Format("{0}: {1}", DateTime.Now.ToString("yyyyMMdd/HHmmss"), str); | |
lock(loggingGate) | |
{ | |
using (StreamWriter sw = File.AppendText(LogFilename)) | |
{ | |
sw.WriteLine(msg); | |
sw.Flush(); | |
sw.Close(); | |
} | |
} | |
} | |
} | |
public static void LogException(Exception ex) | |
{ | |
Log("Exception: " + ex.ToString()); | |
Log("StackTrace: " + ex.StackTrace.ToString()); | |
if (ex.InnerException != null) | |
{ | |
LogException(ex.InnerException); | |
} | |
} | |
public static void DeleteLogFile() | |
{ | |
if (File.Exists(LogFilename)) | |
{ | |
File.Delete(LogFilename); | |
} | |
} | |
public static string LogFilename | |
{ | |
get | |
{ | |
return Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments), "mobileagent.log"); | |
} | |
} | |
public static object loggingGate = new object(); | |
public static bool DebugMode = false; | |
public static bool UseTestFlight = true; | |
public static void CheckDebug() | |
{ | |
using (var defaults = NSUserDefaults.StandardUserDefaults) | |
{ | |
Util.DebugMode = defaults.BoolForKey("IsDebug"); | |
} | |
if (Util.DebugMode) UseTestFlight = true; | |
} | |
public class MailAttachment | |
{ | |
public string Filename; | |
public string FileType; | |
public string EmailFilename; | |
public MailAttachment(string filename, string filetype, string emailFilename) | |
{ | |
Filename = filename; | |
FileType = filetype; | |
EmailFilename = emailFilename; | |
} | |
} | |
private static MFMailComposeViewController mailCompose = null; | |
public static void SendMail(UIViewController parent, string to, string subject, MailAttachment attachment, string body, Action<bool> onDone) | |
{ | |
if (MFMailComposeViewController.CanSendMail) | |
{ | |
mailCompose = new MFMailComposeViewController(); | |
mailCompose.SetSubject(subject); | |
if (!string.IsNullOrEmpty(to)) mailCompose.SetToRecipients(new string[] {to}); | |
if (attachment != null) | |
{ | |
NSData att = NSData.FromFile(attachment.Filename); | |
if (att != null) | |
{ | |
mailCompose.AddAttachmentData(att, attachment.FileType, attachment.EmailFilename); | |
} | |
} | |
mailCompose.SetMessageBody(body, false); | |
mailCompose.Finished += delegate(object sender, MFComposeResultEventArgs e) { | |
mailCompose.DismissModalViewControllerAnimated(true); | |
bool res = true; | |
if (e.Result == MFMailComposeResult.Cancelled || e.Result == MFMailComposeResult.Failed) | |
{ | |
res = false; | |
} | |
onDone(res); | |
}; | |
parent.PresentModalViewController(mailCompose, true); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment