PageRenderTime 48ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 1ms

/WindowsPhone.Tools/WindowsPhoneDevice.cs

#
C# | 362 lines | 264 code | 60 blank | 38 comment | 43 complexity | 338ea750ce49267d15888a4160b4accd MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.SmartDevice.Connectivity;
  6. using System.Collections.ObjectModel;
  7. using System.ComponentModel;
  8. using System.Globalization;
  9. using Microsoft.SmartDevice.MultiTargeting.Connectivity;
  10. using Microsoft.SmartDevice.Connectivity.Interface;
  11. using System.Runtime.CompilerServices;
  12. namespace WindowsPhone.Tools
  13. {
  14. public class WindowsPhoneDevice : INotifyPropertyChanged
  15. {
  16. #region Statics
  17. /// <summary>
  18. /// Minimum support build is one of the recent Mango previews and upwards (RTM is 7720)
  19. /// </summary>
  20. public const int MIN_SUPPORTED_BUILD_NUMBER = 7600;
  21. /// <summary>
  22. /// Retrieve possible devices from CoreCon
  23. /// </summary>
  24. /// <returns></returns>
  25. public static List<ConnectableDevice> GetDevices()
  26. {
  27. List<ConnectableDevice> list = new List<ConnectableDevice>();
  28. MultiTargetingConnectivity multiConnect = new MultiTargetingConnectivity(CultureInfo.CurrentCulture.LCID);
  29. foreach (ConnectableDevice device in multiConnect.GetConnectableDevices(false))
  30. {
  31. list.Add(device);
  32. }
  33. return list;
  34. }
  35. #endregion
  36. #region Properties
  37. private List<ConnectableDevice> _devices;
  38. public List<ConnectableDevice> Devices
  39. {
  40. get
  41. {
  42. if (_devices == null)
  43. {
  44. _devices = GetDevices();
  45. // set CurrentDevice to a default
  46. if (_devices != null)
  47. CurrentConnectableDevice = _devices[0];
  48. }
  49. return _devices;
  50. }
  51. set
  52. {
  53. if (_devices != value)
  54. {
  55. _devices = value;
  56. NotifyPropertyChanged();
  57. }
  58. }
  59. }
  60. /// <summary>
  61. ///
  62. /// </summary>
  63. private IDevice _currentDevice;
  64. public IDevice CurrentDevice
  65. {
  66. get {
  67. /*
  68. * The new ConnectableDevice model does not support an IsConnected flag, should possibly create a wrapper?
  69. *
  70. // attempt to reconnect the device if we previously connected it and
  71. // it is not longer connected
  72. if (_currentDevice != null && Connected && !_currentDevice.IsConnected())
  73. {
  74. // attempt to reconnect
  75. Connect();
  76. // still not connected?
  77. if (!_currentDevice.IsConnected())
  78. {
  79. CurrentDevice = null;
  80. throw new DeviceNotConnectedException(StatusMessage);
  81. }
  82. }
  83. */
  84. return _currentDevice;
  85. }
  86. set
  87. {
  88. if (_currentDevice != value)
  89. {
  90. _currentDevice = value;
  91. NotifyPropertyChanged();
  92. }
  93. }
  94. }
  95. private ConnectableDevice _currentConnectableDevice;
  96. public ConnectableDevice CurrentConnectableDevice
  97. {
  98. get
  99. {
  100. return _currentConnectableDevice;
  101. }
  102. set
  103. {
  104. if (_currentConnectableDevice != value)
  105. {
  106. _currentConnectableDevice = value;
  107. NotifyPropertyChanged();
  108. }
  109. }
  110. }
  111. private ISystemInfo _systemInfo;
  112. public ISystemInfo SystemInfo
  113. {
  114. get { return _systemInfo; }
  115. set
  116. {
  117. if (_systemInfo != value)
  118. {
  119. _systemInfo = value;
  120. NotifyPropertyChanged();
  121. }
  122. }
  123. }
  124. private bool _connected;
  125. public bool Connected
  126. {
  127. get { return _connected; }
  128. set
  129. {
  130. if (_connected != value)
  131. {
  132. _connected = value;
  133. NotifyPropertyChanged();
  134. if (_currentDevice != null)
  135. {
  136. DeviceType = (_currentConnectableDevice.IsEmulator() ? "EMULATOR" : "PHONE");
  137. }
  138. else
  139. {
  140. DeviceType = "UNKNOWN";
  141. }
  142. }
  143. }
  144. }
  145. private bool _isError;
  146. public bool IsError
  147. {
  148. get { return _isError; }
  149. set
  150. {
  151. if (_isError != value)
  152. {
  153. _isError = value;
  154. NotifyPropertyChanged();
  155. }
  156. }
  157. }
  158. private string _statusMessage;
  159. public string StatusMessage
  160. {
  161. get { return _statusMessage; }
  162. set
  163. {
  164. if (_statusMessage != value)
  165. {
  166. _statusMessage = value;
  167. NotifyPropertyChanged();
  168. }
  169. }
  170. }
  171. private string _deviceType;
  172. public string DeviceType
  173. {
  174. get { return _deviceType; }
  175. set
  176. {
  177. if (_deviceType != value)
  178. {
  179. _deviceType = value;
  180. NotifyPropertyChanged();
  181. }
  182. }
  183. }
  184. private Collection<RemoteApplicationEx> _installedApplications;
  185. public Collection<RemoteApplicationEx> InstalledApplications
  186. {
  187. get { return _installedApplications; }
  188. set
  189. {
  190. if (_installedApplications != value)
  191. {
  192. _installedApplications = value;
  193. NotifyPropertyChanged();
  194. }
  195. }
  196. }
  197. private List<RemoteAppIsoStoreItem> _remoteIsoStores;
  198. public List<RemoteAppIsoStoreItem> RemoteIsoStores
  199. {
  200. get { return _remoteIsoStores; }
  201. set
  202. {
  203. if (_remoteIsoStores != value)
  204. {
  205. _remoteIsoStores = value;
  206. NotifyPropertyChanged();
  207. }
  208. }
  209. }
  210. #endregion
  211. public bool Connect()
  212. {
  213. // do not use CurrentDevice here (rather, use _currentDevice) because CurrentDevice.Get()
  214. // can call back into Connect
  215. if (CurrentConnectableDevice != null)
  216. {
  217. // we're already connected to this device! :)
  218. //if (_currentDevice == _connectedDevice/* && _connectedDevice.IsConnected()*/)
  219. // return true;
  220. try
  221. {
  222. // disconnect the existing device
  223. if (CurrentDevice != null)
  224. CurrentDevice.Disconnect();
  225. CurrentDevice = CurrentConnectableDevice.Connect();
  226. SystemInfo = CurrentDevice.GetSystemInfo();
  227. if (SystemInfo.OSBuildNo < MIN_SUPPORTED_BUILD_NUMBER)
  228. {
  229. throw new Exception("Windows Phone Power Tools only support build " + MIN_SUPPORTED_BUILD_NUMBER + " and above. This device is on " + SystemInfo.OSBuildNo + ".");
  230. }
  231. StatusMessage = "Currently connected to " + _currentConnectableDevice.Name;
  232. Connected = true;
  233. IsError = false;
  234. RefreshInstalledApps();
  235. }
  236. catch (Exception ex)
  237. {
  238. SmartDeviceException smartDeviceEx = ex as SmartDeviceException;
  239. if (smartDeviceEx != null)
  240. {
  241. if (ex.Message == "0x89731811")
  242. {
  243. StatusMessage = "Connection Error! Zune is either not running or not connected to the device.";
  244. }
  245. else if (ex.Message == "0x89731812")
  246. {
  247. StatusMessage = "Connection Error! Unlock your phone and make sure it is paired with Zune";
  248. }
  249. else if (ex.Message == "0x89740005")
  250. {
  251. StatusMessage = "Developer unlock has expired. Lock and re-unlock your phone using the SDK registration tool";
  252. }
  253. else
  254. {
  255. StatusMessage = "Connection Error! Message: " + ex.Message;
  256. }
  257. }
  258. else
  259. {
  260. StatusMessage = ex.Message;
  261. }
  262. IsError = true;
  263. Connected = false;
  264. SystemInfo = null;
  265. }
  266. }
  267. return Connected;
  268. }
  269. public void RefreshInstalledApps()
  270. {
  271. //RemoteApplicationEx
  272. Collection<IRemoteApplication> installed = CurrentDevice.GetInstalledApplications();
  273. Collection<RemoteApplicationEx> installedCollection = new Collection<RemoteApplicationEx>();
  274. foreach (IRemoteApplication app in installed)
  275. installedCollection.Add(new RemoteApplicationEx(app));
  276. InstalledApplications = installedCollection;
  277. RefreshRemoteIsoStores();
  278. }
  279. private void RefreshRemoteIsoStores()
  280. {
  281. List<RemoteAppIsoStoreItem> xapIsoStores = new List<RemoteAppIsoStoreItem>();
  282. foreach (RemoteApplicationEx app in _installedApplications)
  283. {
  284. xapIsoStores.Add(new RemoteAppIsoStoreItem(CurrentDevice, app));
  285. }
  286. RemoteIsoStores = xapIsoStores;
  287. }
  288. # region INotifyPropertyChanged
  289. public event PropertyChangedEventHandler PropertyChanged;
  290. private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
  291. {
  292. if (PropertyChanged != null)
  293. {
  294. PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  295. }
  296. }
  297. #endregion
  298. }
  299. }