1f0a7a965c3f2a117e8506b45e89fdf42d746cfd
[profile/tv/apps/dotnet/home.git] / TVHome / TVHome / TVHome.cs
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.1 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://floralicense.org/license/
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 using System;
18 using LibTVRefCommonPortable.Utils;
19 using TVHome.Views;
20 using Xamarin.Forms;
21
22 namespace TVHome
23 {
24     /// <summary>
25     /// A custom EventArgs with string field
26     /// </summary>
27     public class TVHomeEventArgs : EventArgs
28     {
29         public string arg;
30     }
31
32     /// <summary>
33     /// A TVHomeStatus type enumeration
34     /// </summary>
35     public enum TVHomeStatus
36     {
37         OnStart,
38         OnSleep,
39         OnResume,
40     }
41
42     /// <summary>
43     /// A class that represents TV Home application
44     /// </summary>
45     public class App : Application, IPlatformNotification
46     {
47         /// <summary>
48         /// A keyword for checking app status
49         /// </summary>
50         public static readonly string AppStatus = "appstatus";
51
52         /// <summary>
53         /// An event handler for handling Home key pressed event
54         /// </summary>
55         private static EventHandler<TVHomeEventArgs> HomeKeyListener;
56
57         /// <summary>
58         /// An event handler for handling Menu key pressed event
59         /// </summary>
60         private static EventHandler<TVHomeEventArgs> MenuKeyListener;
61
62         /// <summary>
63         /// An event handler for handling application installed event
64         /// </summary>
65         private static EventHandler<TVHomeEventArgs> AppInstalledListener;
66
67         /// <summary>
68         /// An event handler for handling application uninstalled event
69         /// </summary>
70         private static EventHandler<TVHomeEventArgs> AppUninstalledListener;
71
72         /// <summary>
73         /// An event handler for handling application pinned event
74         /// </summary>
75         private static EventHandler<TVHomeEventArgs> AppPinnedNotificationListener;
76
77         /// <summary>
78         /// A Constructor
79         /// Sets main page to MainPage instance
80         /// </summary>
81         /// <param name="screenWidth">Screen Width</param>
82         /// <param name="screenHeight">Screen Height</param>
83         /// <param name="dpi">Screen DPI</param>
84         /// <param name="scaleRatio">Scale ratio</param>
85         public App(int screenWidth, int screenHeight, int dpi, double scaleRatio)
86         {
87             SizeUtils.ScreenWidth = screenWidth;
88             SizeUtils.ScreenHeight = screenHeight;
89             SizeUtils.Dpi = dpi;
90             SizeUtils.ScaleRatio = scaleRatio;
91
92             try
93             {
94                 string modelName;
95                 if (DependencyService.Get<ISystemSettings>() != null && DependencyService.Get<ISystemSettings>().GetSystemModelName(out modelName))
96                 {
97                     SizeUtils.SetModelName(modelName);
98                 }
99             }
100             catch (Exception e)
101             {
102                 DebuggingUtils.Err("Cant get model name!!!, " + e.Message);
103             }
104
105             MainPage = new MainPage();
106         }
107
108         /// <summary>
109         /// A method is called when the application starts
110         /// </summary>
111         protected override void OnStart()
112         {
113             MessagingCenter.Send<App, TVHomeStatus>(this, "appstatus", TVHomeStatus.OnStart);
114         }
115
116         /// <summary>
117         /// A method is called when the application enters the sleeping state
118         /// </summary>
119         protected override void OnSleep()
120         {
121             MessagingCenter.Send<App, TVHomeStatus>(this, "appstatus", TVHomeStatus.OnSleep);
122         }
123
124         /// <summary>
125         /// A method is called when the application resumes from a sleeping state
126         /// </summary>
127         protected override void OnResume()
128         {
129             MessagingCenter.Send<App, TVHomeStatus>(this, "appstatus", TVHomeStatus.OnResume);
130         }
131
132         /// <summary>
133         /// A method adds EventHandler to HomeKeyListener
134         /// </summary>
135         /// <param name="listener">The EventHandler for adding</param>
136         public static void SetHomeKeyListener(EventHandler<TVHomeEventArgs> listener)
137         {
138             HomeKeyListener += listener;
139         }
140
141         /// <summary>
142         /// A method adds EventHandler to MenuKeyListener
143         /// </summary>
144         /// <param name="listener">The EventHandler for adding</param>
145         public static void SetMenuKeyListener(EventHandler<TVHomeEventArgs> listener)
146         {
147             MenuKeyListener += listener;
148         }
149
150         /// <summary>
151         /// A method adds EventHandler to AppInstalledListener
152         /// </summary>
153         /// <param name="listener">The EventHandler for adding</param>
154         public static void SetAppInstalledListener(EventHandler<TVHomeEventArgs> listener)
155         {
156             AppInstalledListener += listener;
157         }
158
159         /// <summary>
160         /// A method adds EventHandler to AppUninstalledListener
161         /// </summary>
162         /// <param name="listener">The EventHandler for adding</param>
163         public static void SetAppUninstalledListener(EventHandler<TVHomeEventArgs> listener)
164         {
165             AppUninstalledListener += listener;
166         }
167
168         /// <summary>
169         /// A method adds EventHandler to AppPinnedNotificationListener
170         /// </summary>
171         /// <param name="listener">The EventHandler for adding</param>
172         public static void SetAppPinnedNotificationListener(EventHandler<TVHomeEventArgs> listener)
173         {
174             AppPinnedNotificationListener += listener;
175         }
176
177         /// <summary>
178         /// A method for handling home key pressed event
179         /// </summary>
180         public void OnHomeKeyPressed()
181         {
182             DebuggingUtils.Dbg("[[[ Home Key ]]] ");
183             HomeKeyListener.Invoke(this, new TVHomeEventArgs()
184             {
185                 arg = "",
186             });
187         }
188
189         /// <summary>
190         /// A method for handling menu key pressed event
191         /// </summary>
192         public void OnMenuKeyPressed()
193         {
194             DebuggingUtils.Dbg("\" Menu Key \" ");
195             MenuKeyListener.Invoke(this, new TVHomeEventArgs()
196             {
197                 arg = "",
198             });
199         }
200
201         public void OnNavigationKeyPressed(string keyName)
202         {
203             DebuggingUtils.Dbg("OnNavigationKeyPressed, " + keyName);
204             MessagingCenter.Send<App, string>(this, "NavigationKeyPressed", keyName);
205         }
206
207         /// <summary>
208         /// A method for handling application installed event
209         /// </summary>
210         /// <param name="pkgID">An installed package ID</param>
211         public void OnAppInstalled(string pkgID)
212         {
213             DebuggingUtils.Dbg("[[[ App Installed ]]] " + pkgID);
214             AppInstalledListener?.Invoke(this, new TVHomeEventArgs()
215             {
216                 arg = pkgID,
217             });
218         }
219
220         /// <summary>
221         /// A method for handling application uninstalled event
222         /// </summary>
223         /// <param name="pkgID">A uninstalled package ID</param>
224         public void OnAppUninstalled(string pkgID)
225         {
226             DebuggingUtils.Dbg("[[[ App Uninstalled ]]] " + pkgID);
227             AppUninstalledListener.Invoke(this, new TVHomeEventArgs()
228             {
229                 arg = pkgID,
230             });
231         }
232
233         /// <summary>
234         /// A method for receiving a pin app App Control request
235         /// </summary>
236         public void OnPinAppRequestReceived()
237         {
238
239         }
240
241         /// <summary>
242         /// A method for receiving application pinned notification and invoke pinned event
243         /// </summary>
244         /// <param name="appID">A pinned app ID</param>
245         public void OnAppPinnedNotificationReceived(String appID)
246         {
247             DebuggingUtils.Dbg("[[[ App Pinned ]]] " + appID);
248             AppPinnedNotificationListener?.Invoke(this, new TVHomeEventArgs()
249             {
250                 arg = appID,
251             });
252         }
253     }
254 }