using System;
using System.Collections.Generic;
using System.Text;
using Shared;
using Shared.SimpleControl.R;
using System.Xml;
using Shared.SimpleControl;
namespace SmartHome.UI.SimpleControl.Pad.Music
{
class A31UserLogin : FrameLayout
{
///
/// 用户名
///
EditText username;
///
/// 密码
///
EditText password;
A31MusicModel currentMusiceA31;
public void show(A31MusicModel a31, string TextUserName, string TextPasswrod,FrameLayout SettingView,FrameLayout PalyPage)
{
currentMusiceA31 = a31;
var topFrameLayout = new FrameLayout
{
Height = Application.GetRealHeight(100),
BackgroundColor = SkinStyle.Current.MainColor,
};
AddChidren(topFrameLayout);
var RadiolistName = new Button
{
TextID = MyInternationalizationString.PANDORA,
};
topFrameLayout.AddChidren(RadiolistName);
var back = new Button
{
Width = Application.GetRealWidth(82),
Height = Application.GetRealHeight(89),
X = Application.GetRealWidth(10),
Gravity = Gravity.CenterVertical,
UnSelectedImagePath = "MusicIcon/HomepageBack.png",
};
topFrameLayout.AddChidren(back);
back.MouseDownEventHandler += (sender, e) =>
{
RemoveFromParent();
};
var frameLayout = new FrameLayout
{
Height = Application.GetRealHeight(Application.DesignHeight - 100-150),
Y = Application.GetRealHeight(100),
BackgroundColor = 0xff2F2F2F,
};
AddChidren(frameLayout);
var tologin = new Button
{
Height = Application.GetRealHeight(100),
TextID = MyInternationalizationString.Tologin,
TextAlignment = TextAlignment.CenterLeft,
X = Application.GetRealWidth(25),
Y = Application.GetRealHeight(10),
};
frameLayout.AddChidren(tologin);
//用户名
username = new EditText
{
Width = Application.GetRealWidth(528),
Height = Application.GetRealHeight(80),
X = Application.GetRealWidth(25),
Y = Application.GetRealHeight(130),
TextAlignment = TextAlignment.CenterLeft,
Text= TextUserName,
//Radius = (uint)Application.GetRealHeight(6),
BackgroundColor = 0xFF4D4D4D,
};
frameLayout.AddChidren(username);
//密码
password = new EditText
{
Width = Application.GetRealWidth(528),
Height = Application.GetRealHeight(80),
X = Application.GetRealWidth(25),
Y = Application.GetRealHeight(230),
TextAlignment = TextAlignment.CenterLeft,
Text= TextPasswrod,
SecureTextEntry=true,
//Radius = (uint)Application.GetRealHeight(6),
BackgroundColor = 0xFF4D4D4D,
};
frameLayout.AddChidren(password);
var notologin = new Button
{
Height = Application.GetRealHeight(100),
TextID = MyInternationalizationString.Toregister,
TextAlignment = TextAlignment.CenterLeft,
X = Application.GetRealWidth(25),
Y = Application.GetRealHeight(350),
};
frameLayout.AddChidren(notologin);
//登陆
var login = new Button
{
Width = Application.GetRealWidth(528),
Height = Application.GetRealHeight(100),
TextID = MyInternationalizationString.login,
TextSize = 18,
X = Application.GetRealWidth(25),
Y = Application.GetRealHeight(450),
BackgroundColor = 0xff009999,
//Radius = (uint)Application.GetRealHeight(6),
};
frameLayout.AddChidren(login);
login.MouseUpEventHandler += (sender, e) =>
{
login.BackgroundColor = 0xff009999;
if (username.Text.Trim () == "" || password.Text.Trim () == "") {
new Alert (Language.StringByID (MyInternationalizationString.Tip),
Language.StringByID (MyInternationalizationString.TipPleaseEnterTheCorrectData),
Language.StringByID (MyInternationalizationString.Close)).Show ();
return;
}
MainPage.Loading.Start("Loging...");
var tempusername = username.Text;
var temppassword = password.Text;
System.Threading.Tasks.Task.Run(() =>
{
try
{
var dd = Login(tempusername,temppassword);
if (dd == null)
{
return;
}
int number = 3;
System.DateTime dateTime = DateTime.Now;
while ((DateTime.Now - dateTime).TotalMilliseconds < 9* 1000)
{
System.Threading.Thread.Sleep(500);
var pandoraString = readList();
if (pandoraString == null)
{
continue;
}
pandoraString = pandoraString.Replace("&", "&").Replace("<", "<").Replace(">", ">").Replace(""", "\"").Replace("", "").Replace("<", "").Replace(">", "").Replace("&", "").Replace(""", "");
int startIndex = pandoraString.IndexOf("");
int endIndex = pandoraString.IndexOf("") + "".Length;
if (endIndex <= startIndex)
{
continue;
}
var ss = pandoraString.Substring(startIndex, endIndex - startIndex);
XmlDocument xml = new XmlDocument();
xml.LoadXml(ss);
var playList = xml.SelectSingleNode("PlayList");
var listNameP = playList.SelectSingleNode("ListName").InnerText;
var listInfo = playList.SelectSingleNode("ListInfo");
number = int.Parse(listInfo.SelectSingleNode("MarkSearch").InnerText);
if (number == 1)
{
break;
}
}
if (number == 1)
{
var pandoraListString = PandoraList();
if (pandoraListString == null)
{
return;
}
pandoraListString = pandoraListString.Replace("&", "&").Replace("<", "<").Replace(">", ">").Replace(""", "\"").Replace("", "").Replace("<", "").Replace(">", "").Replace("&", "").Replace(""", "");
int startIndex1 = pandoraListString.IndexOf("");
int endIndex1 = pandoraListString.IndexOf("") + "".Length;
var aa = pandoraListString.Substring(startIndex1, endIndex1 - startIndex1);
if (endIndex1 <= startIndex1)
{
return;
}
XmlDocument xml1 = new XmlDocument();
xml1.LoadXml(aa);
var pandoraList = xml1.SelectSingleNode("PlayQueue");
int number1 = int.Parse(pandoraList.SelectSingleNode("TotalQueue").InnerText);
var playListInfo = pandoraList.SelectSingleNode("PlayListInfo");
a31.PanRadioInfoList.Clear();
for (int i = 1; i <= number1; i++)
{
var list = playListInfo.SelectSingleNode("List" + i);
MusicInfo musicInfo = new MusicInfo();
musicInfo.Title = list.SelectSingleNode("Name").InnerText;
var ListInfo = list.SelectSingleNode("ListInfo");
foreach (XmlNode childNode in ListInfo.ChildNodes)
{
switch (childNode.Name)
{
case "SearchUrl":
musicInfo.URL = childNode.InnerText;
break;
case "StationID":
musicInfo.ID = childNode.InnerText;
break;
case "PicUrl":
#region 初始化网络图片路径
string path = "";
if (Shared.Application.IsPad)
{
path = "Pad/" + "MusicImage/";
}
else
{
path = "Phone/" + "MusicImage/";
}
path = System.IO.Path.Combine(Shared.IO.FileUtils.RootPath, path + childNode.InnerText.Replace('/', '_').Replace('\\', '_').Replace(':', '_').Replace('*', '_').Replace('?', '_').Replace('"', '_').Replace('<', '_').Replace('>', '_').Replace('|', '_'));
musicInfo.Image = path;
#endregion
Shared.IO.FileUtils.DownLoadImage(musicInfo.Image, childNode.InnerText);
//musicInfo.Image = childNode.InnerText;
break;
}
}
if (musicInfo.ID == null)
{
continue;
}
currentMusiceA31.PanRadioInfoList.Add(musicInfo);
}
var pandoraConfigBytes = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(new PandoraConfig { UserName =tempusername, Passwrod = temppassword }));
Shared.IO.FileUtils.WriteFileByBytes(PandoraConfig.FileName, pandoraConfigBytes);
Application.RunOnMainThread(() =>
{
MainPage.Loading.Hide();
MainPage.AddTip (Language.StringByID (MyInternationalizationString.successful), 1000);
this.Parent.RemoveAt(Parent.ChildrenCount - 2);
this.RemoveFromParent();
A31PandoraList a31Pandoralist = new A31PandoraList();
SettingView.AddChidren(a31Pandoralist);
a31Pandoralist.show(a31, username.Text,password.Text, SettingView, PalyPage);
});
}
else if (number == 3)
{
Application.RunOnMainThread (() => {
MainPage.Loading.Hide ();
new Alert (Language.StringByID (MyInternationalizationString.Tip),
Language.StringByID (MyInternationalizationString.Accountorpassworderro),
Language.StringByID (MyInternationalizationString.Close)).Show ();
});
}
else
{
Application.RunOnMainThread (() => {
MainPage.Loading.Hide ();
new Alert (Language.StringByID (MyInternationalizationString.Tip),
Language.StringByID (MyInternationalizationString.networkerrors),
Language.StringByID (MyInternationalizationString.Close)).Show ();
});
}
}
catch { }
});
};
login.MouseDownEventHandler += (sender, e) =>
{
login.BackgroundColor = 0xffFE5E00;
};
}
///
/// 登录信息
///
/// 用户名
/// 密码
///
string Login(string username, string password)
{
StringBuilder getPlayList = new StringBuilder();
getPlayList.AppendLine("");
getPlayList.AppendLine("");
getPlayList.AppendLine("");
getPlayList.AppendLine("");
getPlayList.AppendLine("");
getPlayList.AppendLine("<?xml version="1.0"?>");
getPlayList.AppendLine("<PlayList>");
getPlayList.AppendLine("<ListName>Pandora</ListName>");
getPlayList.AppendLine("<ListInfo>");
getPlayList.AppendLine("<TrackNumber>0</TrackNumber>");
getPlayList.AppendLine("<SearchUrl>tuner.pandora.com</SearchUrl>");
getPlayList.AppendLine("<Quality>0</Quality>");
getPlayList.AppendLine("<UpdateTime>0</UpdateTime>");
getPlayList.AppendLine("<Http_proxy></Http_proxy>");
getPlayList.AppendLine("<Login_username>" + username + "</Login_username>");//2532767200@qq.com
getPlayList.AppendLine("<Login_password>" + password + "</Login_password>");//85521566
getPlayList.AppendLine("</ListInfo>");
getPlayList.AppendLine("</PlayList>");
getPlayList.AppendLine("");
getPlayList.AppendLine("");
getPlayList.AppendLine("");
getPlayList.AppendLine("");
System.Net.WebClient webClient = new System.Net.WebClient();
webClient.Headers.Add("SOAPACTION", "\"urn:schemas-wiimu-com:service:PlayQueue:1#BackUpQueue\"");
webClient.Headers.Add("CONTENT-TYPE", "text/xml;charset=\"utf-8\"");
try
{
byte[] recevieBytes = webClient.UploadData(new Uri("http://" + currentMusiceA31.IPAddress + ":" + currentMusiceA31.Port + "/upnp/control/PlayQueue1"), "POST", System.Text.Encoding.UTF8.GetBytes(getPlayList.ToString()));
return System.Text.Encoding.UTF8.GetString(recevieBytes, 0, recevieBytes.Length);
}
catch (Exception e)
{
return null;
}
}
///
/// 登录成功
///
///
string readList()
{
StringBuilder ss = new StringBuilder();
ss.AppendLine("");
ss.AppendLine("");
ss.AppendLine("");
ss.AppendLine("");
ss.AppendLine("Pandora");
ss.AppendLine("");
ss.AppendLine("");
ss.AppendLine("");
System.Net.WebClient webClient = new System.Net.WebClient();
webClient.Proxy = null;
webClient.Headers.Add("CONTENT-TYPE", "text/xml;charset=\"utf-8\"");
webClient.Headers.Add("SOAPACTION", "\"urn:schemas-wiimu-com:service:PlayQueue:1#BrowseQueue\"");
try
{
byte[] recevieBytes = webClient.UploadData(new Uri("http://" + currentMusiceA31.IPAddress + ":" + currentMusiceA31.Port + "/upnp/control/PlayQueue1"), "POST", System.Text.Encoding.UTF8.GetBytes(ss.ToString()));
return System.Text.Encoding.UTF8.GetString(recevieBytes, 0, recevieBytes.Length);
}
catch (Exception e)
{
return null;
}
}
///
/// 获取电台列表信息
///
/// 列表名
///
string PandoraList()
{
StringBuilder stringList = new StringBuilder();
stringList.AppendLine("");
stringList.AppendLine("");
stringList.AppendLine("");
stringList.AppendLine("");
stringList.AppendLine("TotalQueue");
stringList.AppendLine("");
stringList.AppendLine("");
stringList.AppendLine("");
System.Net.WebClient webClient = new System.Net.WebClient();
webClient.Proxy = null;
webClient.Headers.Add("CONTENT-TYPE", "text/xml;charset=\"utf-8\"");
webClient.Headers.Add("SOAPACTION", "\"urn:schemas-wiimu-com:service:PlayQueue:1#BrowseQueue\"");
try
{
byte[] recevieBytes = webClient.UploadData(new Uri("http://" + currentMusiceA31.IPAddress + ":" + currentMusiceA31.Port + "/upnp/control/PlayQueue1"), "POST", System.Text.Encoding.UTF8.GetBytes(stringList.ToString()));
return System.Text.Encoding.UTF8.GetString(recevieBytes, 0, recevieBytes.Length);
}
catch (Exception e)
{
return null;
}
}
}
}