Exemplo n.º 1
0
 /// <summary>
 /// Event called when the application is about to exit.
 /// Saves the current window and activity.
 /// </summary>
 /// <param name="sender">The class that sent the event</param>
 /// <param name="e">The event</param>
 private void Application_Exit(object sender, ExitEventArgs e)
 {
     if (AppStateTracker != null)
     {
         AppStateTracker.SaveCurrentWindow();
         AppStateTracker.SaveCurrentActivity();
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// Checks if the activity has (presumably) changed.
        /// Saves the current window if the program has changed.
        /// If the activity has not changed but no activity is running, also creates a new activity (without notifying the user).
        /// </summary>
        /// <param name="windowTitle">The name of the new window/program</param>
        /// <returns>True, if the activity has (presumably) changed; otherwise False</returns>
        private bool ActivitySwitched(string windowTitle)
        {
            if (AppStateTracker.Paused)
            {
                return(false);
            }

            string[] SplitWindowTitle = windowTitle.Split(new string[] { "- " }, StringSplitOptions.None);

            if (SplitWindowTitle.Length < 1) // Ignore empty window titles
            {
                return(false);
            }

            string ProgramTitle  = SplitWindowTitle.Last();
            string WindowDetails = SplitWindowTitle.Length > 1 ? string.Join("- ", SplitWindowTitle.Take(SplitWindowTitle.Length - 1)) : "";

            bool     HasBeenSeen = false;
            DateTime LastSeen;

            // Load need variables from settings
            long TimeNotUsed = Settings.Default.TimeSinceAppLastUsed * 60 * 1000;  // Convert to ms
            long Timeout2    = Settings.Default.TimeBeforeAskingAgain;

            // Stop if the current activity is blacklisted or a file path
            if (Settings.Default.Blacklist.Contains(ProgramTitle) || ProgramTitle.Contains("\\"))
            {
                if (!windowTitle.Equals("NotificationsWindow - TimeTracker"))
                {
                    AppStateTracker.SaveCurrentWindow();
                }
                return(false);
            }

            // Check if the window has not been see for more than whatever is specified in the settings
            if (AppStateTracker.WindowsLastSeen.TryGetValue(ProgramTitle, out LastSeen))
            {
                HasBeenSeen = TimeNotUsed > DateTime.Now.Subtract(LastSeen).TotalMilliseconds;
            }

            // If window has not changed do nothing
            if (AppStateTracker.CurrentWindow != null && AppStateTracker.CurrentWindow.Name.Equals(ProgramTitle))
            {
                return(false);
            }

            AppStateTracker.SaveCurrentWindow();
            AppStateTracker.CreateCurrentWindow(ProgramTitle, WindowDetails);

            // Show notification if app has not been seen in last few minutes
            if ((AppStateTracker.LastConfirmed == null || DateTime.Now.Subtract((DateTime)AppStateTracker.LastConfirmed).TotalSeconds > Timeout2) && !HasBeenSeen && AppStateTracker.Disturb)
            {
                return(true);
            }
            // Handle case that window has been seen shortly before but still no activity is running. In this case just start up another activity of the same kind
            else if (AppStateTracker.CurrentActivity == null)
            {
                AppStateTracker.CreateCurrentActivity(); // Creates a new activity using the name of the last activity.
            }

            return(false);
        }