JLChen
2020-06-04 6d55af8792cf8fbef0055e677b900fc352dba9a2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
using System;
using System.Collections.Generic;
using System.Text;
using Shared;
using System.Xml;
using Shared.SimpleControl.Phone.Music;
using Shared.SimpleControl.Phone;
using Shared.SimpleControl.R;
using Shared.SimpleControl;
 
namespace SmartHome.UI.SimpleControl.Phone.Music
{
    class A31Dlna : FrameLayout
    {
        public void show (DLNAServer dlnaServer, List<MusicInfo> list)
        {
 
            AddChidren (new Button {
                Height = Application.GetRealHeight (36),
                BackgroundColor = SkinStyle.Current.MusicTopFrameLayout,
            });
 
            var topFrameLayout = new FrameLayout {
                Height = Application.GetRealHeight (100),
                Y = Application.GetRealHeight (36),
                BackgroundColor = SkinStyle.Current.MusicTopFrameLayout,
            };
            AddChidren (topFrameLayout);
 
            var RadiolistName = new Button {
                TextID = MyInternationalizationString.Musicdlna,
                TextColor=SkinStyle.Current.MusicTextColor,
            };
            topFrameLayout.AddChidren (RadiolistName);
 
            var hdl = new Button {
                Width = Application.GetRealWidth (154),
                Height = Application.GetRealHeight (90),
                X = Application.GetRealWidth (486),
                Gravity = Gravity.CenterVertical,
                UnSelectedImagePath = MainPage.LogoString,
            };
            topFrameLayout.AddChidren (hdl);
 
            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 ();
            };
 
 
            FrameLayout TtopFrameLayout = new FrameLayout ();
            TtopFrameLayout.Width = LayoutParams.MatchParent;
            TtopFrameLayout.Height = Application.GetRealHeight (100);
            TtopFrameLayout.Y = topFrameLayout.Bottom;
            //TtopFrameLayout.BackgroundColor = 0xff121212;
            TtopFrameLayout.BackgroundColor = SkinStyle.Current.MusicMiddleBackgroundColor;
 
            AddChidren (TtopFrameLayout);
 
            var sourcephoto = new Button {
                Width = Application.GetRealWidth (80),
                Height = Application.GetRealHeight (76),
                UnSelectedImagePath = "MusicIcon/dlna.png",
                TextAlignment = TextAlignment.CenterLeft,
                X = Application.GetRealWidth (30),
                Gravity = Gravity.CenterVertical,
            };
            TtopFrameLayout.AddChidren (sourcephoto);
 
            var dlnamusic = new Button {
                Height = Application.GetRealHeight (100),
                TextID = MyInternationalizationString.Musicmediaplayer,
                TextAlignment = TextAlignment.CenterLeft,
                X = Application.GetRealWidth (130),
                Gravity = Gravity.CenterVertical,
                TextColor=SkinStyle.Current.MusicTextColor,
            };
            TtopFrameLayout.AddChidren (dlnamusic);
 
            var dropdown = new Button {
                Width = Application.GetRealWidth (65),
                Height = Application.GetRealHeight (49),
                UnSelectedImagePath = "MusicIcon/Homepagexiala.png",
                X = Application.GetRealWidth (550),
                Gravity = Gravity.CenterVertical,
            };
            TtopFrameLayout.AddChidren (dropdown);
 
 
            var middle = new VerticalScrolViewLayout ();
            middle.Y = TtopFrameLayout.Bottom;
            middle.Height = Application.GetRealHeight (Application.DesignHeight - 136 - 100);
            //middle.BackgroundColor = 0xff2F2F2F;
            middle.BackgroundColor = SkinStyle.Current.MusicVerticalScrolViewLayout;
            AddChidren (middle);
 
 
            for (int i = 0; i < list.Count; i++) {
                var music = list [i];
 
                var dlnarowLayout = new RowLayout {
                    Height = Application.GetRealHeight (100),
                    LineColor = SkinStyle.Current.MusicRowLayoutLineColor,
                };
                middle.AddChidren (dlnarowLayout);
 
                var radius = new Button {
                    Width = Application.GetRealWidth (36),
                    Height = Application.GetRealHeight (36),
                    TextAlignment = TextAlignment.CenterLeft,
                    X = Application.GetRealWidth (20),
                    UnSelectedImagePath = "MusicIcon/radius.png",
                    Gravity = Gravity.CenterVertical,
                };
                dlnarowLayout.AddChidren (radius);
 
                var listnext = new Button {
                    Width = Application.GetRealWidth (87),
                    Height = Application.GetRealHeight (100),
                    UnSelectedImagePath = "MusicIcon/Next.png",
                    X = Application.GetRealWidth (550),
                };
                dlnarowLayout.AddChidren (listnext);
 
                var dlnalists = new Button {
                    TextAlignment = TextAlignment.CenterLeft,
                    X = Application.GetRealWidth (80),
                    Text = music.dlnalistName,
                    TextColor=SkinStyle.Current.MusicTextColor,
                };
 
                dlnarowLayout.AddChidren (dlnalists);
 
                EventHandler<MouseEventArgs> A31dlnaserver = (sender, e) => {
                    MainPage.Loading.Start (Language.StringByID (MyInternationalizationString.load));
                    System.Threading.Tasks.Task.Run (() => {
                        try {
                            var dlnaString = readDlnaList (dlnaServer, music.ID);
                            if (dlnaString == null) {
                                return;
                            }
                            list.Clear ();
                            dlnaString = dlnaString.Replace ("&amp;", "&").Replace ("&lt;", "<").Replace ("&gt;", ">").Replace ("&quot;", "\"").Replace ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "").Replace ("&", "");
 
                            int StartIndex = dlnaString.IndexOf ("<DIDL-Lite");
                            int endIndex = dlnaString.IndexOf ("</DIDL-Lite>") + "</DIDL-Lite>".Length;
                            if (endIndex <= StartIndex) {
                                return;
                            }
                            var ss = dlnaString.Substring (StartIndex, endIndex - StartIndex);
                            var xml = new XmlDocument ();
                            xml.LoadXml (ss);
 
                            foreach (XmlNode node in xml.ChildNodes [0].ChildNodes) {
                                MusicInfo musicInfo = new MusicInfo ();
                                musicInfo.ID = node.Attributes ["id"].Value;
                                foreach (XmlNode temnode in node.ChildNodes) {
                                    switch (temnode.Name) {
                                    case "dc:title":
                                        musicInfo.dlnalistName = temnode.InnerText;
                                        break;
                                    }
                                }
                                list.Add (musicInfo);
                            }
 
                        } catch { } finally {
 
                            Application.RunOnMainThread (() => {
                                MainPage.Loading.Hide ();
                                A31DlnaList a31DlnaList = new A31DlnaList ();
                                MainPage.MainFrameLayout.AddChidren (a31DlnaList);
                                a31DlnaList.show (dlnaServer, music.dlnalistName, list);
                            });
 
                        }
 
                    });
 
 
                };
                dlnalists.MouseUpEventHandler += A31dlnaserver;
                listnext.MouseUpEventHandler += A31dlnaserver;
                radius.MouseUpEventHandler += A31dlnaserver;
 
            }
        }
 
        /// <summary>
        /// 读取DLNA的音乐列表
        /// </summary>
        /// <param name="id">列表ID</param>
        /// <returns>读取到的音乐列表信息,读取不到反馈为null</returns>
        string readDlnaList (DLNAServer dlnaServer, string id)
        {
            string tempString = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?><s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body><u:Browse xmlns:u=\"urn:schemas-upnp-org:service:ContentDirectory:1\"><ObjectID>" + id + "</ObjectID><BrowseFlag>BrowseDirectChildren</BrowseFlag><Filter>*</Filter><StartingIndex>0</StartingIndex><RequestedCount>999</RequestedCount><SortCriteria>+dc:title</SortCriteria></u:Browse></s:Body></s:Envelope>";
 
            Shared.Net.MyWebClient webClient = new Shared.Net.MyWebClient ();
            webClient.Proxy = null;
            webClient.Headers.Add ("Content-type", "text/xml;charset=\"utf-8\"");
            webClient.Headers.Add ("Soapaction", "\"urn:schemas-upnp-org:service:ContentDirectory:1#Browse\"");
            try {
                byte [] recevieBytes = webClient.UploadData (new Uri ("http://" + dlnaServer.URL + dlnaServer.ControlURL), "POST", System.Text.Encoding.UTF8.GetBytes (tempString));
                return System.Text.Encoding.UTF8.GetString (recevieBytes, 0, recevieBytes.Length);
            } catch (Exception e) {
                return null;
            }
        }
    }
}