package ca.bmaupin.androidplayground;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import android.support.v7.app.ActionBarActivity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ToggleButton;

public class MainActivity extends ActionBarActivity {
	private static final String TAG = "MainActivity";
	private ConnectivityManager dataManager;
	private Method dataMtd;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
        dataManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
        dataMtd = null;
        try {
            dataMtd = ConnectivityManager.class.getDeclaredMethod("setMobileDataEnabled", boolean.class);
        } catch (NoSuchMethodException e) {
            Log.e(TAG, Log.getStackTraceString(e)); 
        }
        dataMtd.setAccessible(true);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	public void onToggleClicked(View view) {
	    // Is the toggle on?
	    boolean on = ((ToggleButton) view).isChecked();
	    
	    if (on) {
	        try {
	            dataMtd.invoke(dataManager, true);
	        } catch (IllegalArgumentException e) {
	        	Log.e(TAG, Log.getStackTraceString(e));
	        } catch (IllegalAccessException e) {
	        	Log.e(TAG, Log.getStackTraceString(e));
	        } catch (InvocationTargetException e) {
	        	Log.e(TAG, Log.getStackTraceString(e));
	        }
	    } else {
	        try {
	            dataMtd.invoke(dataManager, false);
	        } catch (IllegalArgumentException e) {
	        	Log.e(TAG, Log.getStackTraceString(e));
	        } catch (IllegalAccessException e) {
	        	Log.e(TAG, Log.getStackTraceString(e));
	        } catch (InvocationTargetException e) {
	        	Log.e(TAG, Log.getStackTraceString(e));
	        }
	    }
	}
}

@@ -517,75 +517,8 @@
 ty%22;
-%0A%09private ConnectivityManager dataManager;%0A%09private Method dataMtd;
 %0A%0A%09@
@@ -664,390 +664,8 @@
 n);%0A
-%09%09%0A        dataManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);%0A        dataMtd = null;%0A        try %7B%0A            dataMtd = ConnectivityManager.class.getDeclaredMethod(%22setMobileDataEnabled%22, boolean.class);%0A        %7D catch (NoSuchMethodException e) %7B%0A            Log.e(TAG, Log.getStackTraceString(e)); %0A        %7D%0A        dataMtd.setAccessible(true);%0A
 %09%7D%0A%0A
@@ -1404,62 +1404,94 @@
     
-    try %7B%0A%09            dataMtd.invoke(dataManager, tru
+%09setMobileDataEnabled(this, true);%0A%09    %7D else %7B%0A%09    %09setMobileDataEnabled(this, fals
 e);%0A
@@ -1499,330 +1499,516 @@
     
- 
-   %7D catch (IllegalArgumentException e) %7B%0A%09        %09Log.e(TAG, Log.getStackTraceString(e));%0A%09        %7D catch (IllegalAccessException e) %7B%0A%09        %09Log.e(TAG, Log.getStackTraceString(e));%0A%09        %7D catch (InvocationTargetException e) %7B%0A%09        %09Log.e(TAG, Log.getStackTraceString(e));%0A%09        %7D%0A%09    %7D else %7B%0A%09  
+%7D%0A%09%7D%0A%09%0A%09private void setMobileDataEnabled(Context context, boolean enabled) %7B%0A%09    ConnectivityManager dataManager;%0A%09    dataManager  = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);%0A%09    Method dataMtd = null;%0A%09    try %7B%0A%09        dataMtd = ConnectivityManager.class.getDeclaredMethod(%22setMobileDataEnabled%22, boolean.class);%0A%09    %7D catch (NoSuchMethodException e) %7B%0A%09        // TODO Auto-generated catch block%0A%09        e.printStackTrace();%0A%09    %7D%0A%09    dataMtd.setAccessible(true);%0A%09
     
-  
 try 
@@ -2014,28 +2014,24 @@
  %7B%0A%09        
-    
 dataMtd.invo
@@ -2046,29 +2046,27 @@
 anager, 
-false);%0A%09    
+enabled);%0A%09
     %7D ca
@@ -2109,34 +2109,66 @@
         
-%09Log.e(TAG, Log.ge
+// TODO Auto-generated catch block%0A%09        e.prin
 tStackTr
@@ -2170,33 +2170,25 @@
 ackTrace
-String(e)
+(
 );%0A%09    
     %7D ca
@@ -2171,36 +2171,32 @@
 ckTrace();%0A%09    
-    
 %7D catch (Illegal
@@ -2229,158 +2229,191 @@
     
-%09Log.e(TAG, Log.getStackTraceString(e));%0A%09        %7D catch (InvocationTargetException e) %7B%0A%09        %09Log.e(TAG, Log.getStackTraceString(e));%0A%09        %7D
+// TODO Auto-generated catch block%0A%09        e.printStackTrace();%0A%09    %7D catch (InvocationTargetException e) %7B%0A%09        // TODO Auto-generated catch block%0A%09        e.printStackTrace();
 %0A%09  

Put data code in it's own method