package org.za.hem.ipsec_tools;


import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
import android.view.View.OnClickListener;


/*
 * Register
 * android.telephony.TelephonyManager.DATA_CONNECTED
 * android.telephony.TelephonyManager.DATA_DISCONNECTED
 * 
 * Context.getSystemService(Context.CONNECTIVITY_SERVICE).
 * CONNECTIVITY_ACTION
 */

/**
 * 
 * @author mikael
 *
 */

public class IPsecToolsActivity extends Activity {
	private File filesDir;
	private TextView outputView;
	private Handler handler = new Handler();
	
	/*
	public String getLocalIpAddress() {
	    try {
	        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
	            NetworkInterface intf = en.nextElement();
	            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
	                InetAddress inetAddress = enumIpAddr.nextElement();
	                if (!inetAddress.isLoopbackAddress()) {
	                    return inetAddress.getHostAddress().toString();
	                }
	            }
	        }
	    } catch (SocketException ex) {
	        Log.e(LOG_TAG, ex.toString());
	    }
	    return null;
*/
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.ipsec_tools_activity);
            filesDir = getDir("bin", MODE_PRIVATE);
            putBinary("setkey");
            putBinary("setkey.sh");
            outputView = (TextView)findViewById(R.id.output);
            Button prefBtn = (Button) findViewById(R.id.pref_button);
            if (prefBtn != null)
            {
            prefBtn.setOnClickListener(new OnClickListener() {
                    public void onClick(View v) {
                            Intent settingsActivity = new Intent(getBaseContext(),
                                            Preferences.class);
                            startActivity(settingsActivity);
                    }
            });
            }
    		output(execute("/system/bin/ls " + new File(filesDir, "lib").getAbsolutePath()));
    }
    
    /*
     */
    private void putBinary(String fileName) {
    	try {
    		File file = new File(filesDir, fileName);
    		InputStream input = getAssets().open(fileName);
    		int read;
    		byte[] buffer = new byte[4096];
    		OutputStream output = new FileOutputStream(file);
    		
    		while ((read = input.read(buffer)) > 0) {
    			output.write(buffer, 0, read);
    		}
    		input.close();
    		output.close();
    		execute("/system/bin/chmod 711 " + file.getAbsolutePath());
    	} catch (IOException e) {
    		throw new RuntimeException(e);
    	}
    }
    
    private String execute(String cmd) {
    	try {
    		// Executes the command.
    		Process process = Runtime.getRuntime().exec(cmd);
        
    		// Reads stdout.
    		// NOTE: You can write to stdin of the command using
    		//       process.getOutputStream().
    		BufferedReader reader = new BufferedReader(
    				new InputStreamReader(process.getInputStream()));
    		int read;
    		char[] buffer = new char[4096];
    		StringBuffer output = new StringBuffer();
    		while ((read = reader.read(buffer)) > 0) {
    			output.append(buffer, 0, read);
    		}
    		reader.close();
        
    		// Waits for the command to finish.
    		process.waitFor();
        
    		return output.toString();
    	} catch (IOException e) {
    		throw new RuntimeException(e);
    	} catch (InterruptedException e) {
    		throw new RuntimeException(e);
    	}
    }
    
    private void output(final String str) {
    	Runnable proc = new Runnable() {
			public void run() {
				outputView.setText(str);
			}
    	};
    	handler.post(proc);
    }
}

@@ -790,21 +790,20 @@
 te File 
-files
+mBin
 Dir;%0A%09pr
@@ -1742,21 +1742,20 @@
         
-files
+mBin
 Dir = ge
@@ -2419,61 +2419,31 @@
 put(
-execute(%22/system/bin/ls %22 + new File(filesDir, %22lib%22)
+ls(new String%5B%5D%7BmBinDir
 .get
@@ -2456,16 +2456,17 @@
 tePath()
+%7D
 ));%0A    
@@ -2476,18 +2476,181 @@
    %0A    
-/*
+private String ls(String%5B%5D parameters) %7B%0A    %09return system(%22/system/bin/ls%22, parameters);%0A    %7D%0A    %0A    /**%0A     * Copy binary file from assets into bin directory.
 %0A     */
@@ -2738,13 +2738,12 @@
 ile(
-files
+mBin
 Dir,
@@ -3072,65 +3072,23 @@
   %09%09
-execute(%22/system/bin/chmod 711 %22 + file.getAbsolutePath()
+chmod(file, 711
 );%0A 
@@ -3180,30 +3180,388 @@
     
-private String execute
+/**%0A     * Set file mode%0A     * @param file File to modify%0A     * @param mode New file mode%0A     */%0A    private void chmod(File file, int mode) %7B%0A%09%09system(%22/system/bin/chmod %22 + mode + %22 %22 + file.getAbsolutePath());%0A    %7D%0A%0A%0A    /**%0A     * Run system command wait for and return result%0A     * @param cmd System command to run%0A     * @return stdout%0A     */%0A    private String system
 (Str
@@ -4402,32 +4402,320 @@
   %09%7D%0A    %7D%0A    %0A
+    private String system(String prog, String%5B%5D parameters)%0A    %7B%0A    %09StringBuffer buf = new StringBuffer(4096);%0A    %09%0A    %09buf.append(prog);%0A    %09for ( String str : parameters ) %7B%0A    %09%09buf.append(' ');%0A    %09%09buf.append(str);%0A    %09%7D%0A    %09%0A    %09return system(buf.toString());%0A    %7D%0A    %0A
     private void

Improve system command execution