From: Geunsun, Lee Date: Fri, 21 Apr 2017 05:30:44 +0000 (+0900) Subject: Add Sniper method for recent apps X-Git-Tag: submit/tizen/20170808.015446~78 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=970525a251681e1369784e9f5503f9c2657541f9;p=profile%2Ftv%2Fapps%2Fdotnet%2Fhome.git Add Sniper method for recent apps Wrapping the InterOp invocation with try-catch. If the InterOp class could not find the "libsniper.so", exception will be raised. In that case the Sniper class does not do anything for catching the screen of applications. Change-Id: I8679b9e7aa8f0132706a82c520bd18556b12a64e Signed-off-by: Geunsun, Lee Signed-off-by: Sung-jae Park --- diff --git a/TVHome/TVHome.TizenTV/Sniper.cs b/TVHome/TVHome.TizenTV/Sniper.cs new file mode 100644 index 0000000..d75e0de --- /dev/null +++ b/TVHome/TVHome.TizenTV/Sniper.cs @@ -0,0 +1,223 @@ +using System; +using System.IO; +using System.Runtime.InteropServices; + +namespace CoreApp +{ + public class Sniper + { + private IntPtr nativeWindow; + private string storagePath; + private int imageWidth; + private int imageHeight; + public bool SkipUpdateFlag; + + public event EventHandler UpdatedEvent; + public event EventHandler AddRemoveEvent; + public event EventHandler SkipUpdateEvent; + + public class Event : EventArgs + { + public string AppId; + public string InstanceId; + public string Info; + } + + private void WriteLog(string message) + { + } + + private void AddedCallback(string appId, string instanceId) + { + EventHandler handler = AddRemoveEvent; + Event eventInfo; + + WriteLog("Added " + appId + " " + instanceId); + + if (handler == null) + { + return; + } + + try + { + eventInfo = new Event(); + } + catch (Exception e) + { + WriteLog("Updated Exception : " + e.Message); + return; + } + + eventInfo.AppId = appId; + eventInfo.InstanceId = instanceId; + eventInfo.Info = "Added"; + + handler(this, eventInfo); + } + + private void RemovedCallback(string appId, string instanceId) + { + EventHandler handler = AddRemoveEvent; + Event eventInfo; + + WriteLog("Removed " + appId + " " + instanceId); + + if (handler == null) + { + return; + } + + try + { + eventInfo = new Event(); + } + catch (Exception e) + { + WriteLog("Updated Exception : " + e.Message); + return; + } + + eventInfo.AppId = appId; + eventInfo.InstanceId = instanceId; + eventInfo.Info = "Removed"; + + handler(this, eventInfo); + } + + private void UpdatedCallback(string appId, string instanceId, string Filename) + { + EventHandler handler = UpdatedEvent; + Event eventInfo; + + WriteLog("Updated " + appId + " " + instanceId + " " + Filename); + + if (handler == null) + { + return; + } + + try + { + eventInfo = new Event(); + } + catch (Exception e) + { + WriteLog("Updated Exception : " + e.Message); + return; + } + + eventInfo.Info = Filename; + eventInfo.AppId = appId; + eventInfo.InstanceId = instanceId; + + handler(this, eventInfo); + } + + private int SkipUpdateCallback(string appId, string instanceId, string Filename) + { + EventHandler handler = SkipUpdateEvent; + Event eventInfo; + + WriteLog("SkipUpdate" + appId + " " + instanceId + " " + Filename); + + if (handler == null) + { + return 0; + } + + try + { + eventInfo = new Event(); + } + catch (Exception e) + { + WriteLog("SkipUpdated Exception : " + e.Message); + return 0; + } + + eventInfo.Info = Filename; + eventInfo.AppId = appId; + eventInfo.InstanceId = instanceId; + + handler(this, eventInfo); + + if (SkipUpdateFlag) + { + WriteLog("Update is skipped : " + Filename); + SkipUpdateFlag = false; + return 1; + } + + return 0; + } + + public Sniper(IntPtr win, string path, int w, int h) + { + nativeWindow = win; + storagePath = path; + imageWidth = w; + imageHeight = h; + SkipUpdateFlag = false; + } + + public void StartMonitor() + { + InterOp.SniperCallback callbacks; + + try + { + callbacks = new InterOp.SniperCallback(); + callbacks.Added = new InterOp.CallbackAddedRemoved(AddedCallback); + callbacks.Removed = new InterOp.CallbackAddedRemoved(RemovedCallback); + callbacks.Updated = new InterOp.CallbackUpdated(UpdatedCallback); + callbacks.SkipUpdate = new InterOp.CallbackSkipUpdate(SkipUpdateCallback); + } + catch (Exception e) + { + throw new SniperException(e.Message); + } + + try + { + InterOp.sniper_init(nativeWindow, callbacks, storagePath, imageWidth, imageHeight); + } + catch (DllNotFoundException e) + { + WriteLog("Loadable library is not found\n"); + } + + WriteLog("Sniper starts monitoring : " + storagePath + "ImageSize : " + imageWidth + "x" + imageHeight); + } + + public void StopMonitor() + { + try + { + InterOp.sniper_fini(); + } + catch (DllNotFoundException e) + { + WriteLog("Loadable library is not found\n"); + } + + WriteLog("Sniper stops monitoring : " + storagePath + "ImageSize : " + imageWidth + "x" + imageHeight); + } + + public void RequestUpdate(string instanceId) + { + try + { + InterOp.sniper_request_update(instanceId); + } + catch (DllNotFoundException e) + { + WriteLog("Loadable library is not found\n"); + } + + WriteLog("Sniper requests update (" + instanceId + ") : " + storagePath + "ImageSize : " + imageWidth + "x" + imageHeight); + } + } +} + +/* End of a file */ diff --git a/TVHome/TVHome.TizenTV/SniperException.cs b/TVHome/TVHome.TizenTV/SniperException.cs new file mode 100644 index 0000000..9394ad8 --- /dev/null +++ b/TVHome/TVHome.TizenTV/SniperException.cs @@ -0,0 +1,23 @@ +using System; + +namespace CoreApp +{ + public class SniperException : Exception + { + public SniperException() + { + } + + public SniperException(string message) + : base(message) + { + } + + public SniperException(string message, Exception inner) + : base(message, inner) + { + } + } +} + +/* End of a file */ diff --git a/TVHome/TVHome.TizenTV/SniperInterOp.cs b/TVHome/TVHome.TizenTV/SniperInterOp.cs new file mode 100644 index 0000000..8851996 --- /dev/null +++ b/TVHome/TVHome.TizenTV/SniperInterOp.cs @@ -0,0 +1,32 @@ +using System; +using System.Runtime.InteropServices; + +namespace CoreApp +{ + internal static partial class InterOp + { + public delegate void CallbackAddedRemoved(string appId, string instanceId); + public delegate void CallbackUpdated(string appId, string instanceId, string filename); + public delegate int CallbackSkipUpdate(string appid, string instanceId, string filename); + + [StructLayout(LayoutKind.Sequential)] + public struct SniperCallback + { + public CallbackAddedRemoved Added; + public CallbackAddedRemoved Removed; + public CallbackUpdated Updated; + public CallbackSkipUpdate SkipUpdate; + } + + [DllImport("sniper", CharSet = CharSet.Ansi)] + internal static extern int sniper_init(IntPtr win, SniperCallback callbacks, string path, int w, int h); + + [DllImport("sniper", CharSet = CharSet.Ansi)] + internal static extern int sniper_request_update(string instanceId); + + [DllImport("sniper", CharSet = CharSet.Ansi)] + internal static extern int sniper_fini(); + } +} + +/* End of a file */ diff --git a/TVHome/TVHome.TizenTV/TVHome.TizenTV.cs b/TVHome/TVHome.TizenTV/TVHome.TizenTV.cs index a19d631..c7a02d2 100755 --- a/TVHome/TVHome.TizenTV/TVHome.TizenTV.cs +++ b/TVHome/TVHome.TizenTV/TVHome.TizenTV.cs @@ -18,6 +18,8 @@ using Tizen.Applications; using LibTVRefCommonPortable.Utils; using LibTVRefCommonTizen.Ports; using Tizen.Xamarin.Forms.Extension.Renderer; +using CoreApp; +using System; namespace TVHome.TizenTV { @@ -66,7 +68,7 @@ namespace TVHome.TizenTV windowPort = new WindowPort(); windowPort.MainWindow = MainWindow; - // Grab key events + /// Grab key events MainWindow.KeyGrab(ElmSharp.EvasKeyEventArgs.PlatformHomeButtonName, true); MainWindow.KeyGrab(ElmSharp.EvasKeyEventArgs.PlatformMenuButtonName, true); MainWindow.KeyGrab("Up", false); @@ -74,6 +76,36 @@ namespace TVHome.TizenTV MainWindow.KeyGrab("Left", false); MainWindow.KeyGrab("Right", false); windowPort.SetKeyGrabExclusively(ElmSharp.EvasKeyEventArgs.PlatformHomeButtonName); + + /// Sniper + try + { + Sniper sniper = new Sniper((IntPtr)MainWindow, "/tmp", 960, 540); + sniper.UpdatedEvent += OnScreenUpdate; + sniper.AddRemoveEvent += SniperAddRemoveEvent; + sniper.SkipUpdateEvent += SniperSkipUpdateEvent; + sniper.StartMonitor(); + } + catch (SniperException e) + { + DbgPort.E("Failed to create sniper object : " + e.Message); + } + } + + private void SniperSkipUpdateEvent(object sender, Sniper.Event e) + { + DbgPort.D(" [Sniper SkipUpdateEvent] : " + e.Info); + (sender as Sniper).SkipUpdateFlag = true; + } + + private void SniperAddRemoveEvent(object sender, Sniper.Event e) + { + DbgPort.D(" [Sniper SniperAddRemoveEvent] : " + e.Info); + } + + private void OnScreenUpdate(object sender, Sniper.Event e) + { + DbgPort.D(" [Sniper OnScreenUpdate] : " + e.Info); } /// diff --git a/TVHome/TVHome.TizenTV/TVHome.TizenTV.csproj b/TVHome/TVHome.TizenTV/TVHome.TizenTV.csproj index 29ffaa2..193a32e 100755 --- a/TVHome/TVHome.TizenTV/TVHome.TizenTV.csproj +++ b/TVHome/TVHome.TizenTV/TVHome.TizenTV.csproj @@ -49,6 +49,9 @@ + + + @@ -183,4 +186,4 @@ - \ No newline at end of file +