Update nuget packages
[profile/tv/apps/dotnet/home.git] / TVHome / TVHome.TizenTV / Sniper.cs
1 using System;
2 using System.IO;
3 using System.Runtime.CompilerServices;
4 using System.Runtime.InteropServices;
5 using Tizen;
6
7 namespace CoreApp
8 {
9         /// <summary>
10         /// Handles recent screen shot of launched applications
11         /// </summary>
12         public class Sniper
13         {
14                 /// <summary>
15                 /// Main window of the application
16                 /// </summary>
17                 private IntPtr nativeWindow;
18
19                 /// <summary>
20                 /// A path of storage for recent screen shot of launched application
21                 /// </summary>
22                 private string storagePath;
23
24                 /// <summary>
25                 /// A width of recent screen shot
26                 /// </summary>
27                 private int imageWidth;
28
29                 /// <summary>
30                 /// A height of recent screen shot
31                 /// </summary>
32                 private int imageHeight;
33
34                 /// <summary>
35                 /// A flag indicates whether updating recent screen shot or not
36                 /// </summary>
37                 public bool SkipUpdateFlag;
38
39                 /// <summary>
40                 /// Callbacks of sniper events
41                 /// </summary>
42                 private static InterOp.SniperCallback callbacks;
43
44                 /// <summary>
45                 /// A EventHandler handles recent screen shot update event
46                 /// </summary>
47                 public event EventHandler<Event> UpdatedEvent;
48
49                 /// <summary>
50                 /// A EventHandler handles add or remove application
51                 /// </summary>
52                 public event EventHandler<Event> AddRemoveEvent;
53
54                 /// <summary>
55                 /// A EventHandler handles skip update event
56                 /// </summary>
57                 public event EventHandler<Event> SkipUpdateEvent;
58
59                 /// <summary>
60                 /// A event argument class for app screen update notification.
61                 /// </summary>
62                 public class Event : EventArgs
63                 {
64                         public string AppId;
65                         public string InstanceId;
66                         public string Info;
67                 }
68
69                 /// <summary>
70                 /// A method for writing debug log
71                 /// </summary>
72                 /// <param name="message">A log message</param>
73                 /// <param name="file">A path of caller file</param>
74                 /// <param name="func">A name of caller function</param>
75                 /// <param name="line">A line number of caller line</param>
76                 private void WriteLog(string message, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
77                 {
78                         Log.Debug("sniper", message);
79                 }
80
81                 /// <summary>
82                 /// A method for handling launched application
83                 /// </summary>
84                 /// <param name="appId">An ID of launched application</param>
85                 /// <param name="instanceId">An instance ID of launched application</param>
86                 private void AddedCallback(string appId, string instanceId)
87                 {
88                         EventHandler<Event> handler = AddRemoveEvent;
89                         Event eventInfo;
90
91                         WriteLog("Added " + appId + " " + instanceId);
92
93                         if (handler == null)
94                         {
95                                 return;
96                         }
97
98                         try
99                         {
100                                 eventInfo = new Event();
101                         }
102                         catch (Exception e)
103                         {
104                                 WriteLog("Updated Exception : " + e.Message);
105                                 return;
106                         }
107
108                         eventInfo.AppId = appId;
109                         eventInfo.InstanceId = instanceId;
110                         eventInfo.Info = "Added";
111
112                         handler(this, eventInfo);
113                 }
114
115                 /// <summary>
116                 /// A method for handling terminated application
117                 /// </summary>
118                 /// <param name="appId">An ID of terminated application</param>
119                 /// <param name="instanceId">An instance ID of terminated application</param>
120                 private void RemovedCallback(string appId, string instanceId)
121                 {
122                         EventHandler<Event> handler = AddRemoveEvent;
123                         Event eventInfo;
124
125                         WriteLog("Removed " + appId + " " + instanceId);
126
127                         if (handler == null)
128                         {
129                                 return;
130                         }
131
132                         try
133                         {
134                                 eventInfo = new Event();
135                         }
136                         catch (Exception e)
137                         {
138                                 WriteLog("Updated Exception : " + e.Message);
139                                 return;
140                         }
141
142                         eventInfo.AppId = appId;
143                         eventInfo.InstanceId = instanceId;
144                         eventInfo.Info = "Removed";
145
146                         handler(this, eventInfo);
147                 }
148
149                 /// <summary>
150                 /// A method for handling application screen is updated
151                 /// </summary>
152                 /// <param name="appId">An ID of application that screen is updated</param>
153                 /// <param name="instanceId">An instance ID of application that screen is updated</param>
154                 /// <param name="Filename">A path of application screen shot</param>
155                 private void UpdatedCallback(string appId, string instanceId, string Filename)
156                 {
157                         EventHandler<Event> handler = UpdatedEvent;
158                         Event eventInfo;
159
160                         WriteLog("Updated " + appId + " " + instanceId + " " + Filename);
161
162                         if (handler == null)
163                         {
164                                 return;
165                         }
166
167                         try
168                         {
169                                 eventInfo = new Event();
170                         }
171                         catch (Exception e)
172                         {
173                                 WriteLog("Updated Exception : " + e.Message);
174                                 return;
175                         }
176
177                         eventInfo.Info = Filename;
178                         eventInfo.AppId = appId;
179                         eventInfo.InstanceId = instanceId;
180
181                         handler(this, eventInfo);
182                 }
183
184                 /// <summary>
185                 /// A method for handling screen update is skipped
186                 /// </summary>
187                 /// <param name="appId">An ID of application that screen update is skipped</param>
188                 /// <param name="instanceId">An instance ID of application that screen update is skipped</param>
189                 /// <param name="Filename">A path of application screen shot</param>
190                 /// <returns>Returns finish code</returns>
191                 private int SkipUpdateCallback(string appId, string instanceId, string Filename)
192                 {
193                         EventHandler<Event> handler = SkipUpdateEvent;
194                         Event eventInfo;
195
196                         WriteLog("SkipUpdate" + appId + " " + instanceId + " " + Filename);
197
198                         if (handler == null)
199                         {
200                                 return 0;
201                         }
202
203                         try
204                         {
205                                 eventInfo = new Event();
206                         }
207                         catch (Exception e)
208                         {
209                                 WriteLog("SkipUpdated Exception : " + e.Message);
210                                 return 0;
211                         }
212
213                         eventInfo.Info = Filename;
214                         eventInfo.AppId = appId;
215                         eventInfo.InstanceId = instanceId;
216
217                         handler(this, eventInfo);
218
219                         if (SkipUpdateFlag)
220                         {
221                                 WriteLog("Update is skipped : " + Filename);
222                                 SkipUpdateFlag = false;
223                                 return 1;
224                         }
225
226                         return 0;
227                 }
228
229                 /// <summary>
230                 /// Constructor
231                 /// </summary>
232                 /// <param name="window">Main window of this application</param>
233                 /// <param name="path">Storage path</param>
234                 /// <param name="width">Screen shot width</param>
235                 /// <param name="height">Screen shot height</param>
236                 public Sniper(IntPtr window, string path, int width, int height)
237                 {
238                         nativeWindow = window;
239                         storagePath = path;
240                         imageWidth = width;
241                         imageHeight = height;
242                         SkipUpdateFlag = false;
243                 }
244
245                 /// <summary>
246                 /// A method for starting monitoring
247                 /// Adds callbacks and initialize Sniper class
248                 /// </summary>
249                 public void StartMonitor()
250                 {
251                         try
252                         {
253                                 callbacks = new InterOp.SniperCallback();
254                                 callbacks.Added = new InterOp.CallbackAddedRemoved(AddedCallback);
255                                 callbacks.Removed = new InterOp.CallbackAddedRemoved(RemovedCallback);
256                                 callbacks.Updated = new InterOp.CallbackUpdated(UpdatedCallback);
257                                 callbacks.SkipUpdate = new InterOp.CallbackSkipUpdate(SkipUpdateCallback);
258                         }
259                         catch (Exception e)
260                         {
261                                 throw new SniperException(e.Message);
262                         }
263
264                         try
265                         {
266                                 InterOp.sniper_init(nativeWindow, callbacks, storagePath, imageWidth, imageHeight);
267                         }
268                         catch (DllNotFoundException e)
269                         {
270                                 WriteLog("Loadable library is not found " + e.StackTrace);
271                         }
272
273                         WriteLog("Sniper starts monitoring : " + storagePath + "ImageSize : " + imageWidth + "x" + imageHeight);
274                 }
275
276                 /// <summary>
277                 /// A method stops monitoring
278                 /// </summary>
279                 public void StopMonitor()
280                 {
281                         try
282                         {
283                                 InterOp.sniper_fini();
284                         }
285                         catch (DllNotFoundException e)
286                         {
287                                 WriteLog("Loadable library is not found " + e.StackTrace);
288                         }
289
290                         WriteLog("Sniper stops monitoring : " + storagePath + "ImageSize : " + imageWidth + "x" + imageHeight);
291                 }
292
293                 /// <summary>
294                 /// A method requests updating application screen shot
295                 /// </summary>
296                 /// <param name="instanceId">An instance ID of application</param>
297                 public void RequestUpdate(string instanceId)
298                 {
299                         try
300                         {
301                                 InterOp.sniper_request_update(instanceId);
302                         }
303                         catch (DllNotFoundException e)
304                         {
305                                 WriteLog("Loadable library is not found " + e.StackTrace);
306                         }
307
308                         WriteLog("Sniper requests update (" + instanceId + ") : " + storagePath + "ImageSize : " + imageWidth + "x" + imageHeight);
309                 }
310         }
311 }
312
313 /* End of a file */