Bash Vocal Java Mail Checker (Works on any OS) – 2011

Same thing as before, except now it works on any operating system. I use bash still to start the program after I unlock my screen. Replace USER and PASS with your username and password.

filename: Unlock_Screen.java

import operations.Voice;

public class Unlock_Screen {

	public static void main(String[] args) {

		Voice.get_time();		
		Voice.get_gmail();	
        
	}
}
filename: Voice.java

package operations;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import com.sun.speech.freetts.VoiceManager;
import com.sun.syndication.feed.synd.SyndEntryImpl;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;

@SuppressWarnings("rawtypes")
public class Voice {
	
	public static void get_gmail() {
		
		//Integrating Sound
		
		String voiceName = "kevin16";
		
        VoiceManager voiceManager = VoiceManager.getInstance();
        com.sun.speech.freetts.Voice voice = voiceManager.getVoice(voiceName);
        {
        
        //Reading Gmail atom feed and vocally announcing it
        
		try {
			URL feedUrl = new URL("https://gmail.google.com/gmail/feed/atom");

			HttpURLConnection httpcon = (HttpURLConnection)feedUrl.openConnection();
			String encoding = new sun.misc.BASE64Encoder().encode("USER:PASS".getBytes()); 
			httpcon.setRequestProperty ("Authorization", "Basic " + encoding);
			SyndFeedInput input = new SyndFeedInput();
			SyndFeed feed = input.build(new XmlReader(httpcon));

//			System.out.println(feed.getTitle());         // Inbox for [username]
			List entries = feed.getEntries();
//			System.out.println(entries.size());         // Number of E-mails in the inbox
			Iterator it = entries.iterator();
			int emails = entries.size();
			
			if (emails == 0){
//				System.out.println("You have no new messages in your inbox");
				voice.allocate();
				voice.speak("You have no new messages in your inbox");
			}
			if (emails == 1){
//				System.out.println("You have a new message in your inbox");
				voice.allocate();
				voice.speak("You have a new message in your inbox");
			}
			if (emails > 1) {
//				System.out.println("You have " + entries.size() + " new e mails.");
				voice.allocate();
				voice.speak("You have " + entries.size() + " new e mails.");
			}
			
			while (it.hasNext()) {
				SyndEntryImpl e = (SyndEntryImpl) it.next();

				voice.speak("An E Mail From " + e.getAuthor() + " Pertaining to " + e.getTitle());
				voice.deallocate();
				
//				System.out.println("From " + e.getAuthor() + " Pertaining to " + e.getTitle());
//				System.out.println(e.getDescription());
//				System.out.println();
			}

		} catch (Exception ex) {
			ex.printStackTrace();
			System.out.println("ERROR: " + ex.getMessage());
		}
	}
	}
	@SuppressWarnings("deprecation")
	public static void get_time() {
		
		//initiate sound
		String voiceName = "kevin16";
		
        VoiceManager voiceManager = VoiceManager.getInstance();
        com.sun.speech.freetts.Voice voice = voiceManager.getVoice(voiceName);
		
		
		Date date = new Date();

		int hour;
		hour = date.getHours();
		
		int minute;
		minute = date.getMinutes();
		
		String amPM = null;
		String greeting = null;
		String minuteHolder = "";

		// Formatting the time
		if (hour < 12) {
			amPM = "A M";
			greeting = "Good Morning";
		}
		if (hour == 12) {
			amPM = "P M";
			greeting = "Good Afternoon";
		}
		if (hour > 12) {
			amPM = "P M";
			greeting = "Good Afternoon";
			hour = hour - 12;
		}
		if (hour > 18) {
			greeting = "Good Evening";
		}
		if (minute < 10) {
			minuteHolder = "O";
		}
		
//		System.out.println(greeting + " " + "Mister Edwards " + "The time is " + hour + " " + minuteHolder + minute + " " + amPM);
		voice.allocate();
		voice.speak("Authentication verified " + greeting + " " + "Mister Edwards. " + "The time is " + hour + " " + minuteHolder +
		 minute + " " + amPM);
	}
}

filename: check_mail.sh

#!/bin/bash

	while true; 
	do 
	    java -jar Check_Gmail.jar
	    sleep 300
	done

Leave a Reply

Your email address will not be published.