Lots of fresh code! You have been warned!!! Do pls report trouble!
[android6bed4] / src / nl / openfortress / android6bed4 / EventMonitor.java
1
2 package nl.openfortress.android6bed4;
3
4
5 import java.net.Inet6Address;
6 import java.net.NetworkInterface;
7 import java.net.InetAddress;
8 import java.net.SocketException;
9 import java.util.Enumeration;
10 import java.util.Collection;
11 import java.util.Vector;
12
13
14 import android.app.Activity;
15 import android.content.BroadcastReceiver;
16 import android.content.Intent;
17 import android.content.IntentFilter;
18 import android.content.Context;
19 import android.content.ComponentName;
20 import android.net.ConnectivityManager;
21 import android.net.VpnService;
22 import android.util.Log;
23
24
25 /* The EventMonitor class picks up signals from the Android OS,
26  * and processes them internally.
27  * 
28  * Upon receiving BOOT_COMPLETED, it will see if the user wishes
29  * to start 6bed4 at boot time. Since this presents a popup, this
30  * is a setting that must be explicitly configured.  More details:
31  * http://stackoverflow.com/questions/5051687/broadcastreceiver-not-receiving-boot-completed
32  * 
33  * Upon receiving CONNECTIVITY_ACTION, it will see if there is a
34  * default route over IPv6 that does not a 6bed4 address.  See:
35  * http://stackoverflow.com/questions/5276032/connectivity-action-intent-recieved-twice-when-wifi-connected
36  * http://developer.android.com/reference/android/net/ConnectivityManager.html 
37  */
38 public class EventMonitor extends BroadcastReceiver {
39         
40         public final static String TAG = "android6bed4.EventMonitor";
41         
42         public void onReceive (Context ctx, Intent intent) {
43                 String act = intent.getAction ();
44                 TunnelService tunsvc = TunnelService.theTunnelService ();
45         
46 /* TODO: MALFUNCTIONING NETWORK ADDRESS PICKUPS?
47                 if (act.equals (ConnectivityManager.CONNECTIVITY_ACTION)) {
48                         //
49                         // The Network Configuration has changed.
50                         // The NetworkInfo for the affected network is sent as an extra
51                         if (tunsvc == null) {
52                                 return;
53                         }
54                         try {
55                                 Collection <byte[]> ipv6list = new Vector <byte[]> ();
56                                 Enumeration <NetworkInterface> nif_iter = NetworkInterface.getNetworkInterfaces ();
57                                 while (nif_iter.hasMoreElements ()) { 
58                                         NetworkInterface nif = nif_iter.nextElement ();
59                                         try {
60                                                 if (nif.isUp ()) {
61                                                         Enumeration <InetAddress> adr_iter = nif.getInetAddresses ();
62                                                         while (adr_iter.hasMoreElements ()) {
63                                                                 InetAddress adr = adr_iter.nextElement ();
64                                                                 if (adr.isLoopbackAddress () || adr.isLinkLocalAddress () || adr.isAnyLocalAddress () || adr.isSiteLocalAddress () || adr.isMulticastAddress ()) {
65                                                                         continue;
66                                                                 }
67                                                                 byte ipv6addr [] = adr.getAddress ();
68                                                                 if (ipv6addr.length != 16) {
69                                                                         continue;
70                                                                 }
71                                                                 ipv6list.add (ipv6addr);
72                                                                 Log.v (TAG, "Tunnel Service notified of IPv6 address list");
73                                                         }
74                                                 }
75                                         } catch (SocketException se) {
76                                                 ;       // Ignore
77                                         }
78                                         // Now tell the tunnel service about ipv6list
79                                         tunsvc.notify_ipv6_addresses (ipv6list);
80                                 }
81                         } catch (SocketException se) {
82                                 ;       // Ignore
83                         }
84                         Log.v (TAG, "Network Interface Change Processed");
85                         
86                 } else 
87 */
88                 if (act.equals (Intent.ACTION_BOOT_COMPLETED)) {
89                         //
90                         // The system has finished booting.  Consider starting 6bed4
91                         // at this point (if the user is willing to deal with the popup
92                         // from the VPN toolkit at this point.
93                         Intent main = new Intent (Intent.ACTION_MAIN);
94                         main.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
95                         // main.addCategory (Intent.CATEGORY_HOME);
96                         main.addCategory (Intent.CATEGORY_LAUNCHER);
97                         main.setComponent (new ComponentName (ctx, Android6bed4.class));
98                         ctx.startActivity (main);
99                         Log.v (TAG, "Boot action completed");
100                 }
101                 
102         }
103
104 }