bubbl
Posts: 85
Joined: Sun Jul 14, 2013 9:15 pm
Location: United Kingdom
Contact: Website

Google Alarm Clock goes JAVA

Mon Jun 09, 2014 1:03 pm

During the past few months I've been doing a lot of Java programming. A great opportunity occurred for me to actually grasp a new programming language. It was even more fascinating, as it didn't involve the ubiquitous in the Internet sphere 'learning to code' approach, i.e., take a course, learn to add up numbers and believe you can code. Instead of learning from scratch without a clue why would I be doing this or that, I have actually grasped Java in practice. I wrote two fully working Java desktop database applications that are now used by a company. Moreover, my home Linux server got another task - database server.

The Java experience made me want to rewrite all the pieces of Python code to a more cross-platform language. Java is cross platform in the sense that a compiled Java program runs on all platforms for which there exists a JVM (This holds for all major operating systems, including Windows, Mac OS and Linux.). First application to get Java implementation was the Google Alarm Clock. Since I am using the alarm on a daily basis, I thought to give it a try (after that I intend to test the Pi4j Java library to use Raspberry Pi GPIO with Java). The application works fine, but lacks some features, but I'm constantly improving it and making it better. Here's what I come up with so far.

How it works

The basic principle of the application is the same as the Python script I've introduced in September last year. We add an event to the Google calendar (it can be done within the application), and once the alarm clock is started it queries Google Calendar for events matching our criteria:

Code: Select all

    CalendarService myService =
        new CalendarService("SimpleGoogleAlarmClock");
    myService.setUserCredentials(mail, decryptedPass);
    URL feedUrl = new URL("http://www.google.com/calendar/feeds/default/private/full");

    CalendarQuery myQuery = new CalendarQuery(feedUrl);
    myQuery.setMinimumStartTime(s);
    myQuery.setMaximumStartTime(e);
    myQuery.setFullTextQuery(query);
    myQuery.setStringCustomParameter("singleevents", "true");
    myQuery.setStringCustomParameter("sortorder", "a");

    CalendarEventFeed myResultsFeed =
        myService.query(myQuery, CalendarEventFeed.class);

    for (int i = 0; i < myResultsFeed.getEntries().size(); i++) {
        CalendarEventEntry MatchEntry = (CalendarEventEntry) myResultsFeed.getEntries().get(i);
        String time = MatchEntry.getTimes().get(0).getStartTime().toUiString();
        String wakeup = MatchEntry.getTitle().getPlainText();
        queryArea.append("\t" + time + " " + wakeup + "\n");
    }
Once it reached start time of an event it starts music player. The Google password is encrypted within the application using Jasypt library.Once properties file is created at first run, the library saves your password in an encrypted form. When the password is needed, the same library decrypts it inside the application and passes to objects requesting your password.

The application would work both on Windows and Linux machines. I suppose Mac users would be able to use it fully as well. As long as you have Java installed on your machine, you are good to go.There is no need to download and install any additional libraries, programs, files, as all the libraries needed are included with the project. You can even run the application on Raspberry Pi (just type sudo apt-get update && sudo apt-get install oracle-java7-jdk from your console to install it). The application is set to recognize the operating system it is running on and adjust paths and external commands according to the OS. Moreover, there is no need for working directory. The application would work from anywhere, just make sure the lib subdirectory is placed in the same directory as the jar file.

To start the application, just double click on the GoogleAlarmClock.jar file from Desktop mode, or if you want to start it from command line (Desktop is needed though), type:

Code: Select all

    java -jar /path/to/GoogleAlarmClock.jar
Here's the main application window:

Image

Once the local date and time on your machine matches the event start date-time of an event, the application starts (depending on the operating system: Windows: Windows Media Player; Linux: mpg321) music player that plays random mp3 from pre-set directory.

If you want to read more about my Google Alarm Clock, head to my blog:

http://www.bartbania.com/index.php/goog ... goes-java/
We're not here because we are free. We're here because we are not free. There is no escaping reason. No denying purpose. Because we both know without purpose, we would not exist.
http://www.bartbania.com/

Return to “Java”