From 456e16f59f03d0799b201622aae0c25bf89da8b1 Mon Sep 17 00:00:00 2001
From: x <x@x>
Date: Tue, 5 Jul 2016 18:39:10 +0100
Subject: [PATCH] Automatically change channels for reminders in the Program
 Guide.

---
 .../TVLibrary/TvPlugin/TvPlugin/TvNotifyManager.cs | 150 +++++++++++++++++++--
 1 file changed, 142 insertions(+), 8 deletions(-)

diff --git a/TvEngine3/TVLibrary/TvPlugin/TvPlugin/TvNotifyManager.cs b/TvEngine3/TVLibrary/TvPlugin/TvPlugin/TvNotifyManager.cs
index f620961..1108594 100644
--- a/TvEngine3/TVLibrary/TvPlugin/TvPlugin/TvNotifyManager.cs
+++ b/TvEngine3/TVLibrary/TvPlugin/TvPlugin/TvNotifyManager.cs
@@ -31,6 +31,8 @@ using MediaPortal.Util;
 using TvControl;
 using TvDatabase;
 using Timer = System.Windows.Forms.Timer;
+using Action = MediaPortal.GUI.Library.Action;
+using MediaPortal.GUI.Video;
 
 namespace TvPlugin
 {
@@ -201,6 +203,95 @@ namespace TvPlugin
       return true;
     }
 
+    private bool NotifyChannelChange(TVProgramDescription tvProg, int onScreenTimeSeconds)
+    {
+        // TvHome.cs @ 1885
+            TVNotifyYesNoDialog tvNotifyDlg = (TVNotifyYesNoDialog)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_TVNOTIFYYESNO);
+
+            TVProgramDescription notify = tvProg;
+            if (tvNotifyDlg == null || notify == null)
+            {
+              return false;
+            }
+            int minUntilStart = _preNotifyConfig / 60;
+            if (notify.StartTime > DateTime.Now)
+            {
+              if (minUntilStart > 1)
+              {
+                tvNotifyDlg.SetHeading(String.Format(GUILocalizeStrings.Get(1018), minUntilStart));
+              }
+              else
+              {
+                tvNotifyDlg.SetHeading(1019); // Program is about to begin
+              }
+            }
+            else
+            {
+              tvNotifyDlg.SetHeading(String.Format(GUILocalizeStrings.Get(1206), (DateTime.Now - notify.StartTime).Minutes.ToString()));
+            }
+            tvNotifyDlg.SetHeading("Switch to " + tvProg.Channel.DisplayName);
+            tvNotifyDlg.SetLine(1, notify.Title);
+            tvNotifyDlg.SetLine(2, notify.Description);
+            tvNotifyDlg.SetLine(4, String.Format(GUILocalizeStrings.Get(1207), notify.Channel.DisplayName));
+            Channel c = notify.Channel;
+            string strLogo = string.Empty;
+            if (c.IsTv)
+            {
+              strLogo = MediaPortal.Util.Utils.GetCoverArt(Thumbs.TVChannel, c.DisplayName);
+            }
+            else if (c.IsRadio)
+            {
+              strLogo = MediaPortal.Util.Utils.GetCoverArt(Thumbs.Radio, c.DisplayName);
+            }
+
+            tvNotifyDlg.SetImage(strLogo);
+            tvNotifyDlg.TimeOut = onScreenTimeSeconds;
+            //if (TVHome._playNotifyBeep)
+            {
+              MediaPortal.Util.Utils.PlaySound("notify.wav", false, true);
+            }
+            tvNotifyDlg.SetDefaultToYes(true);
+            tvNotifyDlg.DoModal(GUIWindowManager.ActiveWindow);
+
+            if (tvNotifyDlg.IsConfirmed)
+            {
+                return true;
+            }
+            else
+            {
+                return false;
+            }
+    }
+
+    private bool PausePlaybackDelayed(int pauseDelayMilliseconds)
+    {
+        SleepYield(pauseDelayMilliseconds);
+        g_Player.Pause();   
+        Log.Info("Pausing g_player");
+
+        return true;
+    }
+
+    private void SleepYield(int sleepMilliseconds)
+    {
+        if (sleepMilliseconds < 1)
+        {
+            sleepMilliseconds = 1;
+        }
+
+        int millisecondsYieldTime = 1;
+        int elapsedCountMilliseconds = 0;
+        while ((elapsedCountMilliseconds) <= (sleepMilliseconds))
+        {
+            if (elapsedCountMilliseconds % 100 == 0)
+            {
+                g_Player.Process();
+            }
+            Thread.Sleep(millisecondsYieldTime);
+            elapsedCountMilliseconds += millisecondsYieldTime;
+        }
+    }
+
     private void ProcessNotifies(DateTime preNotifySecs)
     {
       if (_notifiesListChanged)
@@ -212,8 +303,10 @@ namespace TvPlugin
       {
         foreach (Program program in _notifiesList)
         {
-          if (preNotifySecs > program.StartTime)
-          {
+          // switching now, so switch near program start time
+          //if (preNotifySecs > program.StartTime)
+          if (System.DateTime.Now.AddSeconds(95) > program.StartTime)
+            {
             Log.Info("Notify {0} on {1} start {2}", program.Title, program.ReferencedChannel().DisplayName,
                      program.StartTime);
             program.Notify = false;
@@ -227,12 +320,53 @@ namespace TvPlugin
             tvProg.EndTime = program.EndTime;
 
             _notifiesList.Remove(program);
-            Log.Info("send notify");
-            GUIMessage msg = new GUIMessage(GUIMessage.MessageType.GUI_MSG_NOTIFY_TV_PROGRAM, 0, 0, 0, 0, 0, null);
-            msg.Object = tvProg;
-            GUIGraphicsContext.SendMessage(msg);
-            msg = null;
-            Log.Info("send notify done");
+            //Log.Info("send notify");
+            //GUIMessage msg = new GUIMessage(GUIMessage.MessageType.GUI_MSG_NOTIFY_TV_PROGRAM, 0, 0, 0, 0, 0, null);
+            //msg.Object = tvProg;
+            //GUIGraphicsContext.SendMessage(msg);
+            //msg = null;
+            //Log.Info("send notify done");
+
+            if (NotifyChannelChange(tvProg, 30))
+            {
+                Channel channel = tvProg.Channel;
+                if (channel.IsTv)
+                {
+                    MediaPortal.GUI.Library.GUIWindowManager.ActivateWindow((int)MediaPortal.GUI.Library.GUIWindow.Window.WINDOW_TVFULLSCREEN);
+                    if (g_Player.Paused)
+                    {
+                        GUIWaitCursor.EnableControl(g_Player.GetHDC(), 0);
+                        GUIWaitCursor.Show();
+                        GUIWaitCursor.Render();
+
+                        int oldVolume = g_Player.Volume;
+                        g_Player.Volume = 0;
+
+                        TVHome.ViewChannel(channel);
+                        g_Player.ShowFullScreenWindowTVDefault();
+
+                        PausePlaybackDelayed(5000);
+
+                        g_Player.Volume = oldVolume;
+                        GUIWaitCursor.Hide();
+                    }
+                    else
+                    {
+                        TVHome.ViewChannel(channel);
+                        g_Player.ShowFullScreenWindow();
+                    }
+                }
+                else if (channel.IsRadio)
+                {
+                    MediaPortal.GUI.Library.GUIWindowManager.ActivateWindow((int)MediaPortal.GUI.Library.GUIWindow.Window.WINDOW_RADIO);
+                    Radio.CurrentChannel = channel;
+                    Radio.Play();
+                }
+
+                //// Now set a bookmark to help those who are pausing or wishing to go back in the buffer
+                GUIVideoFullscreen videoWindow = (GUIVideoFullscreen)GUIWindowManager.GetWindow((int)MediaPortal.GUI.Library.GUIWindow.Window.WINDOW_FULLSCREEN_VIDEO);
+                videoWindow.CreateBookmark();
+            }
             return;
           }
         }
-- 
2.9.0.windows.1

