KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Idle


1 import java.util.Timer JavaDoc;
2 import java.util.TimerTask JavaDoc;
3 import org.jivesoftware.smack.packet.Presence;
4
5 /** This class is used for detecting that the app is not being used.
6 * We can set the presence to away and clear the keypair, depending on the user preferences.*/

7 public final class Idle{
8     
9     private static final int ONE_MINUTE=60000; //one minute in milliseconds
10
private static long lastAction=0;
11     private static final Timer JavaDoc timer=new Timer JavaDoc(true); //daemon timer
12
private static final Task JavaDoc task=new Task JavaDoc();
13     private static long timeout;
14     private static boolean setToAway=false; // whether we set to away
15
private static String JavaDoc oldStatus="";
16     
17     private Idle(){ //prevents instantasation
18
//do nowt
19
}
20     
21     /** starts the timer.*/
22     public static void init(){
23         setTimeout();
24         lastAction=System.currentTimeMillis();
25         timer.scheduleAtFixedRate(task,0,ONE_MINUTE);
26     }
27     
28     /** Sets the timeout.
29     * Only needs to be called after the user has changed the value in options dialog.*/

30     public static void setTimeout(){
31         int to=10;
32         try{
33             to=Integer.parseInt(WhisperIM.UserPref.getProperty("idle_time"));
34         }
35         catch(NumberFormatException JavaDoc nfe){
36             to=10;
37         }
38         timeout=to*ONE_MINUTE; //convert minutes to milliseconds
39
}
40     
41     /** Called when ever the user clicks or presses a key.*/
42     public static void action(){
43         lastAction=System.currentTimeMillis();
44         if(setToAway){
45             setToAway=false;
46             // reset status
47
javax.swing.SwingUtilities.invokeLater(new Runnable JavaDoc(){
48                 public void run(){
49                     if(WhisperIM.MainWindow.currentPresence.equals("away")){
50                         WhisperIM.MainWindow.currentPresence=oldStatus;
51                         if(oldStatus.equals("online")){
52                             setStatus(Presence.Mode.AVAILABLE);
53                         }
54                         else{
55                             setStatus(Presence.Mode.CHAT);
56                         }
57                     }
58                 }
59             });
60         }
61     }
62     
63     /** Stops the timer.*/
64     public static void stop(){
65         timer.cancel();
66         task.cancel();
67     }
68     
69     /** Called every minute.*/
70     private static void doCheck(){
71         long now=System.currentTimeMillis();
72         if((now-lastAction)<=timeout){
73             return;
74         }
75         // set status
76
javax.swing.SwingUtilities.invokeLater(new Runnable JavaDoc(){
77             public void run(){
78                 if(WhisperIM.UserPref.getProperty("auto_away").equals("true") && (WhisperIM.MainWindow.currentPresence.equals("online") || WhisperIM.MainWindow.currentPresence.equals("available"))){
79                     setToAway=true;
80                     oldStatus=WhisperIM.MainWindow.currentPresence;
81                     WhisperIM.MainWindow.currentPresence="away";
82                     setStatus(Presence.Mode.AWAY);
83                 }
84             }
85         });
86         // clear keypair
87
// we do in swing EDT for synch
88
if(WhisperIM.UserPref.getProperty("clear_keypair").equals("true") && WhisperIM.KEYPAIR!=null){
89             javax.swing.SwingUtilities.invokeLater(new Runnable JavaDoc(){
90                 public void run(){
91                     if(WhisperIM.MainWindow.secureChannelPanel!=null){
92                         WhisperIM.MainWindow.secureChannelPanel.doClose();
93                     }
94                     WhisperIM.KEYPAIR.privateKey().wipe();
95                     whisper.Util.wipe(WhisperIM.MainWindow.keycachekey);
96                     WhisperIM.KEYPAIR=null;
97                     // show re-enter-password
98
(new PassphraseDialog()).show();
99                 }
100             });
101         }
102     }
103     
104     /** Sets the online status.
105     * Should be called in the Swing EDT.*/

106     private static void setStatus(Presence.Mode pm){
107         if(WhisperIM.MainWindow.Conn!=null && WhisperIM.MainWindow.Conn.isConnected()){
108             try{
109                 Presence p=new Presence(Presence.Type.AVAILABLE);
110                 p.setFrom(WhisperIM.MainWindow.Conn.getUser());
111                 p.setMode(pm);
112                 WhisperIM.MainWindow.Conn.sendPacket(p);
113             }
114             catch(Exception JavaDoc e){
115                 e.printStackTrace();
116                 return;
117             }
118             WhisperIM.MainWindow.setTitle("Whisper IM "+Lang.gs("status_"+WhisperIM.MainWindow.currentPresence));
119             WhisperIM.MainWindow.myStatus.setToolTipText(Lang.gs("menu_status_tt")+Lang.gs("status_"+WhisperIM.MainWindow.currentPresence));
120             WhisperIM.MainWindow.myStatus.setIcon(new javax.swing.ImageIcon JavaDoc("img/status_"+WhisperIM.MainWindow.currentPresence+".png"));
121         }
122     }
123     
124     final static class Task extends TimerTask JavaDoc{
125         public void run(){
126             doCheck();
127         }
128     }
129 }
Popular Tags