Add Sniper method for recent apps
authorGeunsun, Lee <gs86.lee@samsung.com>
Fri, 21 Apr 2017 05:30:44 +0000 (14:30 +0900)
committerChulSeung Kim <charles0.kim@samsung.com>
Thu, 8 Jun 2017 09:34:53 +0000 (18:34 +0900)
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 <gs86.lee@samsung.com>
Signed-off-by: Sung-jae Park <nicesj.park@samsung.com>
TVHome/TVHome.TizenTV/Sniper.cs [new file with mode: 0644]
TVHome/TVHome.TizenTV/SniperException.cs [new file with mode: 0644]
TVHome/TVHome.TizenTV/SniperInterOp.cs [new file with mode: 0644]
TVHome/TVHome.TizenTV/TVHome.TizenTV.cs
TVHome/TVHome.TizenTV/TVHome.TizenTV.csproj

diff --git a/TVHome/TVHome.TizenTV/Sniper.cs b/TVHome/TVHome.TizenTV/Sniper.cs
new file mode 100644 (file)
index 0000000..d75e0de
--- /dev/null
@@ -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<Event> UpdatedEvent;
+        public event EventHandler<Event> AddRemoveEvent;
+        public event EventHandler<Event> 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<Event> 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<Event> 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<Event> 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<Event> 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 (file)
index 0000000..9394ad8
--- /dev/null
@@ -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 (file)
index 0000000..8851996
--- /dev/null
@@ -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 */
index a19d631..c7a02d2 100755 (executable)
@@ -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);
         }
 
         /// <summary>
index 29ffaa2..193a32e 100755 (executable)
@@ -49,6 +49,9 @@
     <None Include="shared\res\xahome.png" />
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="Sniper.cs" />
+    <Compile Include="SniperException.cs" />
+    <Compile Include="SniperInterOp.cs" />
     <Compile Include="TVHome.TizenTV.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
   </ItemGroup>
       </FlavorProperties>
     </VisualStudio>
   </ProjectExtensions>
-</Project>
\ No newline at end of file
+</Project>