d18ad1f3c6e83a9d63935f028b3e841244935bbd
[android6bed4] / src / nl / openfortress / android6bed4 / Android6bed4.java
1 package nl.openfortress.android6bed4;
2
3 import android.app.Activity;
4 import android.content.Intent;
5 import android.net.VpnService;
6 import android.net.VpnService.Builder;
7 import android.os.Bundle;
8 import android.os.ParcelFileDescriptor;
9 import android.widget.ProgressBar;
10
11 import java.io.IOException;
12 import java.net.*;
13
14
15 public class Android6bed4 extends Activity {
16
17         private DatagramSocket uplink = null;
18         private TunnelService downlink = null;
19         
20         private ProgressBar debug;
21         
22         public InetSocketAddress publicserver;
23         
24         /*
25          * State management interface, called from Android OS.  Thee onXXX() conform to a state diagram.
26          * 
27          * @see http://developer.android.com/reference/android/app/Activity.html#SavingPersistentState
28          */
29         protected void onCreate (Bundle savedInstanceState) {
30                 super.onCreate (savedInstanceState);
31                 try {
32                         publicserver = new InetSocketAddress (Inet4Address.getByName ("145.136.0.1"), 25788); /* TODO:FIXED */
33                 } catch (UnknownHostException uhe) {
34                         publicserver = null;
35                 }
36                 setContentView (R.layout.user_interface);
37                 debug = (ProgressBar) findViewById (R.id.progress_bar);
38                 
39         }
40         
41         protected void onStart () {
42                 super.onStart ();
43                 if (debug != null) debug.setProgress (10);
44                 try {
45                         uplink = new DatagramSocket (0xdebe); /* TODO:FIXED */
46                 } catch (SocketException se) {
47                         uplink = null;
48                 }
49                 try {
50                         setupTunnelService ((Inet6Address) Inet6Address.getByName ("::1"));     /* TODO: Note here, not with a static IPv6 address */
51                 } catch (UnknownHostException uhe) {
52                 }
53                 if (debug != null) debug.setProgress (20);
54                 if (debug != null) debug.setProgress (30);
55         }
56         
57         protected void onResume () {
58                 super.onResume ();
59                 /* TODO: Check if the IPv6 address is still the same?  Also run after onStart! */
60                 if (debug != null) debug.setProgress (50);
61         }
62
63         protected void onPause () {
64                 super.onPause ();
65                 /* TODO: Nothing to do I suppose... try to avoid loosing IPv6 address? */
66                 if (debug != null) debug.setProgress (70);              
67         }
68         
69         protected void onStop () {
70                 super.onStop ();
71                 if (uplink != null) {
72                         uplink.close ();
73                         uplink = null;
74                 }
75                 if (debug != null) debug.setProgress (80);
76         }
77
78         protected void onRestart () {
79                 super.onRestart ();
80                 /* TODO: Nothing? */
81                 if (debug != null) debug.setProgress (90);
82         }
83         
84         protected void onDestroy () {
85                 super.onDestroy ();
86                 if (downlink != null) {
87                         downlink.teardown ();
88                         downlink = null;
89                 }
90                 /* TODO: Nothing? */
91                 if (debug != null) debug.setProgress (100);
92         }
93
94
95         /***                                                ***
96          ***   Internal affairs -- tunnel service setup.    ***
97          ***                                                ***/
98
99         public void setupTunnelService (Inet6Address addr6bed4) {
100                 //
101                 // Prepare the context for the VPN
102                 Intent mktun_intent = VpnService.prepare (this);
103                 if (mktun_intent != null) {
104                         // This is a new start of the VPN
105                         this.startActivityForResult (mktun_intent, 0);
106                 } else {
107                         // Already started, apparently OK, so proceed to startup
108                         this.onActivityResult (0, Activity.RESULT_OK, null);
109                 }
110         }
111                 
112         synchronized protected void onActivityResult (int reqcode, int resultcode, Intent data) {
113                 if (resultcode == Activity.RESULT_OK) {
114                         //
115                         // Cleanup any prior tunnel file descriptors
116                         if (downlink != null) {
117                                 downlink.teardown ();
118                         }
119                         //
120                         // Setup a new tunnel
121                         // TODO: Due to this statement, two tunnel interfaces get created;
122                         //       without it, none are created.  Not sure what to think
123                         //       of it... need to leave it like this for now.
124                         downlink = new TunnelService (uplink, publicserver);
125                         if (downlink != null) {
126                                 downlink = new TunnelService (); /*TODO:HUH?*/
127                                 //
128                                 // Given the successful startup, avoid future cleanups so the
129                                 // TunnelService can continue to use these resources.  It has
130                                 // taken responsibility over their cleanip after returning.
131                                 uplink = null;
132                                 downlink = null;
133                                 publicserver = null;
134                         }
135                 }
136         }
137         
138         /*
139         synchronized public void onRevoke () {
140                 if (downlink != null) {
141                         downlink.teardown ();
142                 }
143         }
144         */
145
146 }