/// <summary> /// Starts the controller. /// </summary> public void Start() { if (!string.IsNullOrEmpty(this.address) && !string.IsNullOrEmpty(this.port)) { return; } Device device = this.FindDevice(); if (device == null) { throw new WindowsPhoneDriverException(string.Format(CultureInfo.InvariantCulture, "Found no matching devices for name '{0}'", this.deviceName)); } else { this.SendStatusUpdate("Connecting to device {0}.", device.Name); string assemblyDirectory = Path.GetDirectoryName(this.GetType().Assembly.Location); string xapPath = GetPackagePath(assemblyDirectory); ApplicationArchiveInfo appInfo = ApplicationArchiveInfo.ReadApplicationInfo(xapPath); Guid applicationId = appInfo.ApplicationId.Value; string iconPath = appInfo.ExtractIconFile(); bool isConnectedToDevice = false; try { device.Connect(); isConnectedToDevice = device.IsConnected(); } catch (SmartDeviceException ex) { this.SendStatusUpdate("WARNING! Exception encountered when connecting to device. HRESULT: {0:X}, message: {1}", ex.HResult, ex.Message); System.Threading.Thread.Sleep(500); } if (!isConnectedToDevice) { // TODO: Create connection mitigation routine. this.SendStatusUpdate("WARNING! Was unable to connect to device!"); } else { if (!device.IsApplicationInstalled(applicationId)) { this.SendStatusUpdate("Installing application {0}.", xapPath); this.browserApplication = device.InstallApplication(applicationId, applicationId, "WindowsPhoneDriverBrowser", iconPath, xapPath); } else { this.SendStatusUpdate("Application already installed."); this.browserApplication = device.GetApplication(applicationId); } } File.Delete(iconPath); } }
public static ApplicationArchiveInfo ReadApplicationInfo(string appArchiveFilePath) { ApplicationArchiveInfo appInfo = new ApplicationArchiveInfo(appArchiveFilePath); try { // Do not use "using" for the FileStream. The ZipArchive will close/dispose the stream unless // we specify otherwise. FileStream appArchiveFileStream = new FileStream(appArchiveFilePath, FileMode.Open, FileAccess.Read); using (ZipArchive zipArchive = new ZipArchive(appArchiveFileStream, ZipArchiveMode.Read)) { ZipArchiveEntry appManifestEntry = zipArchive.GetEntry("WMAppManifest.xml"); using (Stream appManifestFileStream = appManifestEntry.Open()) { XPathDocument manifestDocument = new XPathDocument(appManifestFileStream); XPathNavigator manifestNavigator = manifestDocument.CreateNavigator(); XPathNavigator appNodeNavigator = manifestNavigator.SelectSingleNode("//App"); appInfo.ApplicationId = new Guid?(new Guid(appNodeNavigator.GetAttribute("ProductID", string.Empty))); string attribute = appNodeNavigator.GetAttribute("RuntimeType", string.Empty); if (attribute.Equals("Modern Native", StringComparison.OrdinalIgnoreCase)) { appInfo.IsNative = true; } manifestNavigator.MoveToFirstChild(); appInfo.ManifestVersion = new Version(manifestNavigator.GetAttribute("AppPlatformVersion", string.Empty)); } } } catch (Exception ex) { throw new WindowsPhoneDriverException("Unexpected error reading application information.", ex); } return(appInfo); }
public static ApplicationArchiveInfo ReadApplicationInfo(string appArchiveFilePath) { string log = string.Empty; ApplicationArchiveInfo appInfo = new ApplicationArchiveInfo(appArchiveFilePath); try { // Do not use "using" for the FileStream. The ZipArchive will close/dispose the stream unless // we specify otherwise. FileStream appArchiveFileStream = new FileStream(appArchiveFilePath, FileMode.Open, FileAccess.Read); using (ZipArchive zipArchive = new ZipArchive(appArchiveFileStream, ZipArchiveMode.Read)) { ZipArchiveEntry appManifestEntry; // Xap and Appx packages use different types of Appmanifest files // Extract information from the files accordingly if (appArchiveFilePath.EndsWith("xap")) { appManifestEntry = zipArchive.GetEntry("WMAppManifest.xml"); using (Stream appManifestFileStream = appManifestEntry.Open()) { XPathDocument manifestDocument = new XPathDocument(appManifestFileStream); XPathNavigator manifestNavigator = manifestDocument.CreateNavigator(); XPathNavigator appNodeNavigator = manifestNavigator.SelectSingleNode("//App"); appInfo.ApplicationId = new Guid?(new Guid(appNodeNavigator.GetAttribute("ProductID", string.Empty))); string attribute = appNodeNavigator.GetAttribute("RuntimeType", string.Empty); if (attribute.Equals("Modern Native", StringComparison.OrdinalIgnoreCase)) { appInfo.IsNative = true; } manifestNavigator.MoveToFirstChild(); appInfo.ManifestVersion = new Version(manifestNavigator.GetAttribute("AppPlatformVersion", string.Empty)); appInfo.iconPath = manifestNavigator.SelectSingleNode("//App/IconPath").Value; } } else { appManifestEntry = zipArchive.GetEntry("AppxManifest.xml"); using (Stream appManifestFileStream = appManifestEntry.Open()) { // Load the document and set the root element. XmlDocument manifestDocument = new XmlDocument(); manifestDocument.Load(appManifestFileStream); XmlNode root = manifestDocument.DocumentElement; // Add the namespace. XmlNamespaceManager nsmgr = new XmlNamespaceManager(manifestDocument.NameTable); nsmgr.AddNamespace("a", "http://schemas.microsoft.com/appx/2010/manifest"); nsmgr.AddNamespace("b", "http://schemas.microsoft.com/developer/appx/2012/build"); XmlNode node = root.SelectSingleNode( "descendant::a:Identity", nsmgr); //log = node.Attributes.GetNamedItem("Name").Value; appInfo.ApplicationId = new Guid?(new Guid(node.Attributes.GetNamedItem("Name").Value)); node = root.SelectSingleNode("descendant::b:Item[attribute::Name='TargetFrameworkMoniker']", nsmgr); string ver = (node.Attributes.GetNamedItem("Value").Value); int symbolIndex = ver.LastIndexOf("="); appInfo.ManifestVersion = new Version(ver.Substring(symbolIndex + 2)); node = root.SelectSingleNode("descendant::a:Logo", nsmgr); appInfo.iconPath = node.FirstChild.Value; } } } } catch (Exception ex) { throw new WindowsPhoneDriverException(log + "Unexpected error reading application information.", ex); } return(appInfo); }
/// <summary> /// Starts the controller. /// </summary> public void Start() { if (!string.IsNullOrEmpty(this.address) && !string.IsNullOrEmpty(this.port)) { return; } Device device = this.FindDevice(); if (device == null) { throw new WindowsPhoneDriverException(string.Format(CultureInfo.InvariantCulture, "Found no matching devices for name '{0}'", this.deviceName)); } else { this.SendStatusUpdate("Connecting to device {0}.", device.Name); List <string> fileNames = new List <string>(); if (!this.appPath.Equals(string.Empty)) { this.assemblyDirectory = Path.GetDirectoryName(this.appPath); fileNames.Add(Path.GetFileName(this.appPath)); } else { this.assemblyDirectory = Path.GetDirectoryName(this.GetType().Assembly.Location); fileNames.Add("WindowsPhoneDriverBrowser.xap"); } string path = GetPackagePath(this.assemblyDirectory, fileNames); this.SendStatusUpdate("path: " + path); ApplicationArchiveInfo appInfo = ApplicationArchiveInfo.ReadApplicationInfo(path); Guid applicationId = appInfo.ApplicationId.Value; string iconPath = appInfo.ExtractIconFile(); bool isConnectedToDevice = false; try { device.Connect(); isConnectedToDevice = device.IsConnected(); } catch (SmartDeviceException ex) { this.SendStatusUpdate("WARNING! Exception encountered when connecting to device. HRESULT: {0:X}, message: {1}", ex.HResult, ex.Message); System.Threading.Thread.Sleep(500); } if (!isConnectedToDevice) { // TODO: Create connection mitigation routine. this.SendStatusUpdate("WARNING! Was unable to connect to device!"); } else { if (path.EndsWith("xap")) { if (device.IsApplicationInstalled(applicationId)) { this.SendStatusUpdate("Application already installed. Uninstalling.."); device.GetApplication(applicationId).Uninstall(); } this.SendStatusUpdate("Installing application {0}.", path); this.browserApplication = device.InstallApplication(applicationId, applicationId, "WindowsPhoneDriverBrowser", iconPath, path); } else { if (device.IsApplicationInstalled(applicationId)) { this.SendStatusUpdate("Application already installed. Uninstalling.."); device.GetApplication(applicationId).Uninstall(); } this.SendStatusUpdate("Installing application {0}.", path); IAppManifestInfo manifestInfo = Utils.ReadAppManifestInfoFromPackage(this.appPath); DeploymentOptions deploymentOptions = DeploymentOptions.None; var devices = Utils.GetDevices(); var utilDevice = devices.FirstOrDefault(d => d.ToString() == "Device"); Utils.InstallApplication(utilDevice, manifestInfo, deploymentOptions, this.appPath); } } File.Delete(iconPath); } }