Update nuget packages
[profile/tv/apps/dotnet/home.git] / TVHome / TVHome.TizenTV / TVHome.TizenTV.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 Tizen.Applications;
18 using LibTVRefCommonPortable.Utils;
19 using LibTVRefCommonTizen.Ports;
20 using Tizen.Xamarin.Forms.Extension.Renderer;
21 using CoreApp;
22 using System;
23
24 namespace TVHome.TizenTV
25 {
26     /// <summary>
27     /// TV Home application main entry point
28     /// </summary>
29     class Program : global::Xamarin.Forms.Platform.Tizen.FormsApplication
30     {
31         /// <summary>
32         /// An interface for the  platform notification
33         /// </summary>
34         IPlatformNotification notification;
35         /// <summary>
36         /// A port for handling Window APIs
37         /// </summary>
38         WindowPort windowPort;
39
40         /// <summary>
41         /// A method will be called when application is created
42         /// </summary>
43         protected override void OnCreate()
44         {
45             base.OnCreate();
46
47             FileSystemPort.AppDataStroagePath = DirectoryInfo.Data;
48             FileSystemPort.AppResourceStoragePath = DirectoryInfo.Resource;
49             string platformShareStoragePath = "/opt/usr/home/owner/share/";
50             FileSystemPort.PlatformShareStroagePath = platformShareStoragePath;
51
52             var app = new App(MainWindow.ScreenSize.Width,
53                 MainWindow.ScreenSize.Height,
54                 MainWindow.ScreenDpi.X,
55                 ElmSharp.Elementary.GetScale());
56
57             notification = app;
58
59             DbgPort.D("-----------------------------------");
60             DbgPort.D("Home application is being loaded...");
61             DbgPort.D("-----------------------------------");
62             LoadApplication(app);
63
64             PackageManagerPort.RegisterCallbacks(notification);
65
66             MainWindow.KeyUp += KeyUpListener;
67             MainWindow.Alpha = true;
68
69             windowPort = new WindowPort();
70             windowPort.MainWindow = MainWindow;
71
72             /// Grab key events
73             MainWindow.KeyGrab(ElmSharp.EvasKeyEventArgs.PlatformHomeButtonName, true);
74             MainWindow.KeyGrab(ElmSharp.EvasKeyEventArgs.PlatformMenuButtonName, true);
75             MainWindow.KeyGrab("XF86Info", true);
76             MainWindow.KeyGrab("Up", false);
77             MainWindow.KeyGrab("Down", false);
78             MainWindow.KeyGrab("Left", false);
79             MainWindow.KeyGrab("Right", false);
80             windowPort.SetKeyGrabExclusively(ElmSharp.EvasKeyEventArgs.PlatformHomeButtonName);
81
82             /// Sniper
83             try
84             {
85                 Sniper sniper = new Sniper((IntPtr)MainWindow, platformShareStoragePath, 960, 540);
86                 sniper.UpdatedEvent += OnScreenUpdate;
87                 sniper.AddRemoveEvent += SniperAddRemoveEvent;
88                 sniper.SkipUpdateEvent += SniperSkipUpdateEvent;
89                 sniper.StartMonitor();
90             }
91             catch (SniperException e)
92             {
93                 DbgPort.E("Failed to create sniper object : " + e.Message);
94             }
95         }
96
97         private void SniperSkipUpdateEvent(object sender, Sniper.Event e)
98         {
99             DbgPort.D(" [Sniper SkipUpdateEvent] : " + e.Info);
100             (sender as Sniper).SkipUpdateFlag = true;
101         }
102
103         private void SniperAddRemoveEvent(object sender, Sniper.Event e)
104         {
105             DbgPort.D(" [Sniper SniperAddRemoveEvent] : " + e.Info);
106         }
107
108         private void OnScreenUpdate(object sender, Sniper.Event e)
109         {
110             DbgPort.D(" [Sniper OnScreenUpdate] : " + e.Info);
111         }
112
113         /// <summary>
114         /// A method will be called when application receives KeyUp event
115         /// </summary>
116         /// <param name="sender">The source of the event</param>
117         /// <param name="e">A EvasKey event's argument</param>
118         private void KeyUpListener(object sender, ElmSharp.EvasKeyEventArgs e)
119         {
120             DbgPort.D("Key Pressed :" + e.KeyName);
121             if (e.KeyName.CompareTo(ElmSharp.EvasKeyEventArgs.PlatformHomeButtonName) == 0)
122             {
123                 if (notification != null)
124                 {
125                     notification.OnHomeKeyPressed();
126                 }
127             }
128             else if (e.KeyName.CompareTo(ElmSharp.EvasKeyEventArgs.PlatformMenuButtonName) == 0 ||
129                 e.KeyName.CompareTo("XF86Info") == 0)
130             {
131                 if (notification != null)
132                 {
133                     notification.OnMenuKeyPressed();
134                 }
135             }
136             else if (e.KeyName.Equals("Up") || e.KeyName.Equals("Down") || e.KeyName.Equals("Left") || e.KeyName.Equals("Right"))
137             {
138                 if (notification != null)
139                 {
140                     notification.OnNavigationKeyPressed(e.KeyName);
141                 }
142             }
143         }
144
145         /// <summary>
146         /// A method will be called when application is terminated
147         /// </summary>
148         protected override void OnTerminate()
149         {
150             base.OnTerminate();
151
152             notification = null;
153             PackageManagerPort.UnregisterCallbacks();
154             MainWindow.KeyUngrab(ElmSharp.EvasKeyEventArgs.PlatformHomeButtonName);
155             MainWindow.KeyUngrab(ElmSharp.EvasKeyEventArgs.PlatformMenuButtonName);
156             MainWindow.KeyUngrab("XF86Info");
157             MainWindow.KeyUngrab("Up");
158             MainWindow.KeyUngrab("Down");
159             MainWindow.KeyUngrab("Left");
160             MainWindow.KeyUngrab("Right");
161         }
162
163         /// <summary>
164         /// A method will be called when application receives app control
165         /// </summary>
166         /// <param name="e">AppControl event argument</param>
167         protected override void OnAppControlReceived(AppControlReceivedEventArgs e)
168         {
169             if (AppControlPort.AppAddedNotifyOperation.CompareTo(e.ReceivedAppControl.Operation) == 0)
170             {
171                 DbgPort.D("App Added Notification");
172                 string pinnedApp;
173                 if (e.ReceivedAppControl.ExtraData.TryGet(AppControlPort.KeyAddedAppID, out pinnedApp))
174                 {
175                     notification.OnAppPinnedNotificationReceived(pinnedApp);
176                 }
177                 else
178                 {
179                     notification.OnAppPinnedNotificationReceived("");
180                 }
181             }
182         }
183
184         /// <summary>
185         /// The entry point for the application
186         /// </summary>
187         /// <param name="args">A list of command line arguments</param>
188         static void Main(string[] args)
189         {
190             var app = new Program();
191
192             DbgPort.Prefix = "Home";
193
194             global::Xamarin.Forms.DependencyService.Register<DbgPort>();
195             global::Xamarin.Forms.DependencyService.Register<AppControlPort>();
196             global::Xamarin.Forms.DependencyService.Register<PackageManagerPort>();
197             global::Xamarin.Forms.DependencyService.Register<FileSystemWatcherPort>();
198             global::Xamarin.Forms.DependencyService.Register<ApplicationManagerPort>();
199             global::Xamarin.Forms.DependencyService.Register<FileSystemPort>();
200             global::Xamarin.Forms.DependencyService.Register<WindowPort>();
201             global::Xamarin.Forms.DependencyService.Register<SystemSettingsPort>();
202             global::Xamarin.Forms.DependencyService.Register<MediaContentPort>();
203             TizenFormsExtension.Init();
204             global::Xamarin.Forms.Platform.Tizen.Forms.Init(app);
205             app.Run(args);
206         }
207     }
208 }