5

I want to record the active application and save a history of my active applications. I say active application because if I run an application and It's minimized, etc I would not count it as an active application. In order to make my question more clear let's look at this example. I open Firefox and surf the web for 20 minutes. Then I open a text editor and start writing for 5 minutes (Firefox is running but I don't use it and so Firefox should not be counted as an active application). So I should be able to record the following information:

Firefox -> 20 minutes 
Text editor -> 5 minutes

I want to track each used application during the OS (Linux is preferred) is on and make an open source application that says how much you use each application.

UPDATE : the application that I want to record is the application that user see on the display and is working with. For instance you might change the windows size in such a way that you can see both Firefox and Text editor (alongside, in different work-space, cascaded, etc), but you are typing in text editor, therefore, the text editor is the active application. In other word the application is active if you are interacting with or is the last interacted app that you are looking at to read some thing in pdf, text, etc. Interacting with the application: I mean clicking, typing, scrolling, etc.

NARROWING : The only thing that i can not figure out is how to get the active application with these conditions. I think if I get the topest application on the display in the current workspace that would solve the problem, hoever, we have a property to set to a windows (always on top) that might need to get without this property enabled. I have found this question that might help to answer.

Using Fedora 26,

Thanks in advance.

14
  • 1
    There is no core Java solution since Java has been written specifically to be as OS agnostic as possible. For solutions, you'll need to use 3rd party libraries (off topic) or use a C/C++ solution linked to Java via JNI / JNA. Commented Oct 8, 2017 at 1:06
  • 2
    @Martin: almost any language is better than Java in this regard. Again, Java was written to be as far from the metal and platform as possible, and so the reason that you're not finding the needed functionality is that it is not there on purpose. Much better to use C/C++ or if Windows, then possibly C#. Commented Oct 8, 2017 at 1:22
  • 1
    Too late. I already have used up my re-open vote. Damn system. Commented Oct 8, 2017 at 1:33
  • 1
    @Martin you need to add more constraints to the question... So is this an X application that the user runs connected to the display and only sees the apps on that display, yes? Commented Oct 8, 2017 at 4:22
  • 1
    And I've upvoted the question because I am in need of similar code (within X11). Commented Oct 8, 2017 at 4:22

2 Answers 2

2
  1. Whether or not an application is "minimized" is NOT a property of the Linux process. Rather, it's managed by your "desktop manager: software (e.g. Gnome), which in turn sits on top of X Windows.

  2. In other words, to find which applications are "minimized" and which are not, you generally have to query X Windows. For example:

How can you check if a window is minimized via the terminal in linux

if xwininfo -all -id $windowIdGoHere |grep "Hidden"; then
  echo "is hidden"
fi
  1. Should "xwininfo" work for you ... then you can certainly call it from Java, for example by using Process p = Runtime.getRuntime().exec(...).
Sign up to request clarification or add additional context in comments.

3 Comments

Why should I get the minimized application?
Again: from the OS perspective, your application is "always running". Whether it's "minimized" (which I thought from your original description) or simply "not in focus" ... IS A PROPERTY OF YOUR DESKTOP WINDOW MANAGER. Which, for Linux, us usually implemented using X Windows. So for Linux "X windows" is probably where you want to look. And "xwininfo" is a good place to start. 'Hope that helps.
Thanks, I found that another command xdotool and provided a solution according to your answer.
2
+50

Thanks to contributors, I have found a solution to do what I asked. I found this github project which does a similar job in C, and then implemented a solution in java (I prefer it because of maven and JavaFX).

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

public class Main {
    private static final DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    public static void main(String[] args) throws IOException, InterruptedException {
        System.out.println("Start :");

        while(true) {
            Date date = new Date();      
            Process proc = Runtime.getRuntime().exec("xdotool getactivewindow getwindowname");
            java.io.InputStream is = proc.getInputStream();
            java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
            String val = "";
            if (s.hasNext()) {
                val = s.next();
            }
            else {
                val = "";
            }
            System.out.print(val +" " + "at:");
            System.out.println(sdf.format(date));
            TimeUnit.SECONDS.sleep(10);
        }
    }
}

It returns the active windows name every 10 second and prints the title of the application which is in use:

Start :
ApplineBuilder - NetBeans IDE 8.2
 at:2017/10/12 02:58:58
ApplineBuilder - NetBeans IDE 8.2
 at:2017/10/12 02:59:08
GoldenDict
 at:2017/10/12 02:59:18
 at:2017/10/12 02:59:28
How to detect the active application using C/java? - Stack Overflow - Mozilla Firefox
 at:2017/10/12 02:59:38
ApplineBuilder - NetBeans IDE 8.2
 at:2017/10/12 02:59:48
Cancel Running Task
 at:2017/10/12 02:59:58

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.