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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
using System;
using System.Collections.Generic;
using System.Text;
using Shared;
using Shared.SimpleControl.R;
using System.Xml;
using Shared.SimpleControl.Phone;
using Shared.SimpleControl;
 
namespace SmartHome.UI.SimpleControl.Phone.Music
{
    /// <summary>
    /// 电台界面
    /// </summary>
    class A31CNRadio : FrameLayout
    {
        public void show (A31MusicModel a31, string data)
        {
            initList (a31, data);
 
            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.cnRadio,
                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 ();
            };
 
            var middle = new VerticalScrolViewLayout () {
                Y = topFrameLayout.Bottom,
                Height = Application.GetRealHeight (Application.DesignHeight - 136),
                //BackgroundColor = 0xff2F2F2F
                BackgroundColor=SkinStyle.Current.MusicVerticalScrolViewLayout,
            };
            AddChidren (middle);
 
            for (int i = 0; i < a31.CnRadioInfoList.Count; i++) {
                var cnrow = new RowLayout {
                    Height = Application.GetRealHeight (100),
                    LineColor = SkinStyle.Current.MusicRowLayoutLineColor,
                };
                middle.AddChidren (cnrow);
 
                var title = new Button {
                    Width = Application.GetRealWidth (61),
                    Height = Application.GetRealHeight (81),
                    X = Application.GetRealWidth (30),
                    Gravity = Gravity.CenterVertical,
                };
                cnrow.AddChidren (title);
 
                var cntext = new Button {
                    Gravity = Gravity.CenterVertical,
                    Text = a31.CnRadioInfoList [i].Cnkey,
                    X = Application.GetRealWidth (130),
                    Tag = a31.CnRadioInfoList [i].URL,
                    TextAlignment = TextAlignment.CenterLeft,
                    TextColor = SkinStyle.Current.MusicTextColor,
                };
                cnrow.AddChidren (cntext);
 
                var next = new Button {
                    Width = Application.GetRealWidth (87),
                    Height = Application.GetRealHeight (100),
                    UnSelectedImagePath = "MusicIcon/Next.png",
                    SelectedImagePath = "MusicIcon/NextSelecte.png",
                    X = Application.GetRealWidth (550),
                };
                cnrow.AddChidren (next);
 
                EventHandler<MouseEventArgs> CNresources = (sender, e) => {
                    MainPage.Loading.Start (Language.StringByID (MyInternationalizationString.load));
                    System.Threading.Tasks.Task.Run (() => {
                        try {
                            var dataString = readRadioList (cntext.Tag.ToString ());
                            if (dataString == null) {
                                return;
                            }
                            int startIndex1 = dataString.IndexOf ("<body>");
                            int endIndex1 = dataString.IndexOf ("</body>") + "</body>".Length;
                            if (endIndex1 <= startIndex1) {
                                return;
                            }
 
                            XmlDocument xml1 = new XmlDocument ();
                            xml1.LoadXml (dataString.Substring (startIndex1, endIndex1 - startIndex1));
                            var dataList = xml1.SelectSingleNode ("body");
                            if (dataList == null) {
                                return;
                            }
                            a31.CnRadioInfoList.Clear ();
                            foreach (XmlNode childNodes in dataList.ChildNodes) {
                                if (childNodes.Name == "outline" && childNodes.Attributes ["URL"] != null) {
                                    MusicInfo musicInfoCN = new MusicInfo ();
                                    musicInfoCN.URL = childNodes.Attributes ["URL"] == null ? "" : childNodes.Attributes ["URL"].Value;
                                    musicInfoCN.Title = childNodes.Attributes ["text"] == null ? "" : childNodes.Attributes ["text"].Value;
                                    musicInfoCN.Image = childNodes.Attributes ["image"] == null ? "" : childNodes.Attributes ["image"].Value;
 
                                    if (childNodes.Attributes ["image"] != null) {
                                        string path = "";
                                        if (Shared.Application.IsPad) {
                                            path = "";
                                        } else {
                                            path = "Phone/" + "MusicImage/";
                                        }
                                    }
 
                                    musicInfoCN.Cntype = childNodes.Attributes ["type"] == null ? "" : childNodes.Attributes ["type"].Value;
                                    a31.CnRadioInfoList.Add (musicInfoCN);
 
 
 
                                }
                                foreach (XmlNode a in childNodes.ChildNodes) {
                                    if (a.Name == "outline") {
                                        MusicInfo musicInfoCN = new MusicInfo ();
                                        musicInfoCN.URL = a.Attributes ["URL"] == null ? "" : a.Attributes ["URL"].Value;
                                        musicInfoCN.Title = a.Attributes ["text"] == null ? "" : a.Attributes ["text"].Value;
 
                                        if (a.Attributes ["image"] != null) {
                                            #region 初始化网络图片路径
                                            string path = "";
                                            if (Shared.Application.IsPad) {
                                                path = "Pad/" + "MusicImage/";
                                            } else {
                                                path = "Phone/" + "MusicImage/";
                                            }
                                            path = System.IO.Path.Combine (Shared.IO.FileUtils.RootPath, path + a.Attributes ["image"].Value.Replace ('/', '_').Replace ('\\', '_').Replace (':', '_').Replace ('*', '_').Replace ('?', '_').Replace ('"', '_').Replace ('<', '_').Replace ('>', '_').Replace ('|', '_'));
                                            musicInfoCN.Image = path;
                                            #endregion
                                            Shared.IO.FileUtils.DownLoadImage (musicInfoCN.Image, a.Attributes ["image"].Value);
                                        }
                                        musicInfoCN.Cntype = a.Attributes ["type"] == null ? "" : a.Attributes ["type"].Value;
                                        a31.CnRadioInfoList.Add (musicInfoCN);
 
                                    }
                                }
                            }
                        } catch { } finally {
                            Application.RunOnMainThread (() => {
                                MainPage.Loading.Hide ();
                                A31CnList a31CnList = new A31CnList ();
                                MainPage.MainFrameLayout.AddChidren (a31CnList);
                                a31CnList.Show (a31, cntext.Text);
                            });
                        }
                    });
                };
                title.MouseUpEventHandler += CNresources;
                next.MouseUpEventHandler += CNresources;
                cntext.MouseUpEventHandler += CNresources;
 
 
 
                switch (cntext.Text) {
                case "local":
                    title.UnSelectedImagePath = "MusicIcon/ocalradio.png";
                    break;
                case "recent":
                    title.UnSelectedImagePath = "MusicIcon/Lately.png";
                    break;
                case "trending":
                    title.UnSelectedImagePath = "MusicIcon/popular.png";
                    break;
                case "music":
                    title.UnSelectedImagePath = "MusicIcon/musiccn.png";
                    break;
                case "talk":
                    title.UnSelectedImagePath = "MusicIcon/Talk.png";
                    break;
                case "sports":
                    title.UnSelectedImagePath = "MusicIcon/sports.png";
                    break;
                case "language":
                    title.UnSelectedImagePath = "MusicIcon/language.png";
                    break;
                case "location":
                    title.UnSelectedImagePath = "MusicIcon/place.png";
                    break;
                case "news":
                    title.UnSelectedImagePath = "MusicIcon/news.png";
                    break;
                case "podcast":
                    title.UnSelectedImagePath = "MusicIcon/podcast.png";
                    break;
                default:
                    title.UnSelectedImagePath = "MusicIcon/podcast.png";
                    break;
                }
                cntext.Text = a31.CnRadioInfoList [i].Title;
            }
 
 
        }
 
        void initList (A31MusicModel a31, string data)
        {
            try {
                var readString = data;
                if (readString == null) {
                    return;
                }
                int startIndex1 = readString.IndexOf ("<body>");
                int endIndex = readString.IndexOf ("</body>") + "</body>".Length;
                if (endIndex <= startIndex1) {
                    return;
                }
                var aa = readString.Substring (startIndex1, endIndex - startIndex1);
                XmlDocument xml = new XmlDocument ();
                xml.LoadXml (aa);
                var CNList = xml.SelectSingleNode ("body");
                if (CNList == null) {
                    return;
                }
                a31.CnRadioInfoList.Clear ();
                foreach (XmlNode v in CNList.ChildNodes) {
                    switch (v.Name) {
                    case "outline":
                        MusicInfo musicInfo = new MusicInfo ();
                        musicInfo.Title = v.Attributes ["text"].Value;
                        musicInfo.URL = v.Attributes ["URL"].Value;
                        musicInfo.Cnkey = v.Attributes ["key"].Value;
 
                        a31.CnRadioInfoList.Add (musicInfo);
                        break;
                    }
                }
            } catch { }
        }
 
        /// <summary>
        /// 读取电台列表
        /// </summary>
        /// <returns>读取到的电台组列表信息,读取不到反馈为null</returns>
        string readRadioList (string url)
        {
            Shared.Net.MyWebClient webClient = new Shared.Net.MyWebClient (10000);
            webClient.Proxy = null;
            webClient.Headers.Add ("Content-type", "plain/text; charset=UTF-8");
            if (Language.CurrentLanguage == "Chinese") {
                webClient.Headers.Add ("Accept-Language", "zh-cn");
            } else {
                webClient.Headers.Add ("Accept-Language", "en-us");
            }
            try {
                byte [] recevieBytes = webClient.DownloadData (new Uri (url));
                return System.Text.Encoding.UTF8.GetString (recevieBytes, 0, recevieBytes.Length);
            } catch (Exception e) {
                return null;
            }
        }
    }
}