1 package nl.openfortress.android6bed4;
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;
11 import java.io.IOException;
15 public class Android6bed4 extends Activity {
17 private DatagramSocket uplink = null;
18 private TunnelService downlink = null;
20 private ProgressBar debug;
22 public InetSocketAddress publicserver;
25 * State management interface, called from Android OS. Thee onXXX() conform to a state diagram.
27 * @see http://developer.android.com/reference/android/app/Activity.html#SavingPersistentState
29 protected void onCreate (Bundle savedInstanceState) {
30 super.onCreate (savedInstanceState);
32 publicserver = new InetSocketAddress (Inet4Address.getByName ("145.136.0.1"), 25788); /* TODO:FIXED */
33 } catch (UnknownHostException uhe) {
36 setContentView (R.layout.user_interface);
37 debug = (ProgressBar) findViewById (R.id.progress_bar);
41 protected void onStart () {
43 if (debug != null) debug.setProgress (10);
45 uplink = new DatagramSocket (0xdebe); /* TODO:FIXED */
46 } catch (SocketException se) {
50 setupTunnelService ((Inet6Address) Inet6Address.getByName ("::1")); /* TODO: Note here, not with a static IPv6 address */
51 } catch (UnknownHostException uhe) {
53 if (debug != null) debug.setProgress (20);
54 if (debug != null) debug.setProgress (30);
57 protected void onResume () {
59 /* TODO: Check if the IPv6 address is still the same? Also run after onStart! */
60 if (debug != null) debug.setProgress (50);
63 protected void onPause () {
65 /* TODO: Nothing to do I suppose... try to avoid loosing IPv6 address? */
66 if (debug != null) debug.setProgress (70);
69 protected void onStop () {
75 if (debug != null) debug.setProgress (80);
78 protected void onRestart () {
81 if (debug != null) debug.setProgress (90);
84 protected void onDestroy () {
86 if (downlink != null) {
91 if (debug != null) debug.setProgress (100);
96 *** Internal affairs -- tunnel service setup. ***
99 public void setupTunnelService (Inet6Address addr6bed4) {
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);
107 // Already started, apparently OK, so proceed to startup
108 this.onActivityResult (0, Activity.RESULT_OK, null);
112 synchronized protected void onActivityResult (int reqcode, int resultcode, Intent data) {
113 if (resultcode == Activity.RESULT_OK) {
115 // Cleanup any prior tunnel file descriptors
116 if (downlink != null) {
117 downlink.teardown ();
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?*/
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.
139 synchronized public void onRevoke () {
140 if (downlink != null) {
141 downlink.teardown ();