diff options
| -rw-r--r-- | res/layout/client_dashboard.xml | 11 | ||||
| -rw-r--r-- | res/layout/eip_service_fragment.xml (renamed from res/layout/eip_overview.xml) | 6 | ||||
| -rw-r--r-- | src/se/leap/leapclient/Dashboard.java | 241 | ||||
| -rw-r--r-- | src/se/leap/leapclient/EipServiceFragment.java | 234 | 
4 files changed, 247 insertions, 245 deletions
diff --git a/res/layout/client_dashboard.xml b/res/layout/client_dashboard.xml index 9d9b7a4..9f05cfb 100644 --- a/res/layout/client_dashboard.xml +++ b/res/layout/client_dashboard.xml @@ -65,15 +65,6 @@          android:layout_width="match_parent"          android:layout_height="0dp"          android:layout_weight="0.11" -        android:orientation="vertical" > - -        <ViewStub -            android:id="@+id/eipOverviewStub" -            android:layout_width="wrap_content" -            android:layout_height="wrap_content" -            android:inflatedId="@+id/eipOverview" -            android:layout="@layout/eip_overview" /> - -    </LinearLayout> +        android:orientation="vertical" />  </LinearLayout>
\ No newline at end of file diff --git a/res/layout/eip_overview.xml b/res/layout/eip_service_fragment.xml index 738c82e..8f7c1d2 100644 --- a/res/layout/eip_overview.xml +++ b/res/layout/eip_service_fragment.xml @@ -1,7 +1,8 @@  <?xml version="1.0" encoding="utf-8"?>  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" +    android:id="@+id/eipServiceFragment"      android:layout_width="match_parent" -    android:layout_height="match_parent" +    android:layout_height="wrap_content"      android:layout_marginTop="10dp" >      <TextView @@ -12,7 +13,6 @@          android:layout_alignParentTop="true"          android:layout_marginLeft="10dp"          android:clickable="true" -        android:onClick="toggleEipOverview"          android:text="@string/eip_service_label"          android:textAppearance="?android:attr/textAppearanceLarge" /> @@ -67,10 +67,8 @@              android:layout_alignParentLeft="true"              android:layout_centerVertical="true"              android:clickable="true" -            android:onClick="showEIPLog"              android:text="@string/eip_status" />      </RelativeLayout> -  </RelativeLayout>
\ No newline at end of file diff --git a/src/se/leap/leapclient/Dashboard.java b/src/se/leap/leapclient/Dashboard.java index ebce35e..4cd517a 100644 --- a/src/se/leap/leapclient/Dashboard.java +++ b/src/se/leap/leapclient/Dashboard.java @@ -6,14 +6,12 @@ import org.json.JSONException;  import org.json.JSONObject;  import se.leap.leapclient.ProviderAPIResultReceiver.Receiver; -import se.leap.openvpn.LogWindow;  import se.leap.openvpn.MainActivity; -import se.leap.openvpn.OpenVPN; -import se.leap.openvpn.OpenVPN.StateListener;  import android.app.Activity;  import android.app.AlertDialog;  import android.app.DialogFragment;  import android.app.Fragment; +import android.app.FragmentManager;  import android.app.FragmentTransaction;  import android.app.ProgressDialog;  import android.content.Context; @@ -22,15 +20,10 @@ import android.content.Intent;  import android.content.SharedPreferences;  import android.os.Bundle;  import android.os.Handler; -import android.os.ResultReceiver;  import android.view.Menu;  import android.view.MenuItem;  import android.view.View;  import android.view.ViewGroup; -import android.view.ViewStub; -import android.widget.CompoundButton; -import android.widget.RelativeLayout; -import android.widget.Switch;  import android.widget.TextView;  import android.widget.Toast; @@ -40,26 +33,23 @@ import android.widget.Toast;   *    * @author Sean Leonard <meanderingcode@aetherislands.net>   */ -public class Dashboard extends Activity implements LogInDialog.LogInDialogInterface,Receiver,StateListener { +public class Dashboard extends Activity implements LogInDialog.LogInDialogInterface,Receiver {  	protected static final int CONFIGURE_LEAP = 0; +	private static final String TAG_EIP_FRAGMENT = "EIP_DASHBOARD_FRAGMENT"; +  	private ProgressDialog mProgressDialog; +	  	private static Context app;  	private static SharedPreferences preferences;  	private static Provider provider;  	private TextView providerNameTV; -	private TextView eipTypeTV; -	private Switch eipSwitch; -	private View eipDetail; -	private TextView eipStatus; -	 -	private boolean mEipStartPending = false; +  	private boolean authed = false;      public ProviderAPIResultReceiver providerAPI_result_receiver; -    private EIPReceiver mEIPReceiver;  	@Override  	protected void onCreate(Bundle savedInstanceState) { @@ -77,23 +67,6 @@ public class Dashboard extends Activity implements LogInDialog.LogInDialogInterf  		else  			buildDashboard();  	} - -	@Override -	protected void onStop() { -		super.onStop(); -		if (provider != null) -			if (provider.hasEIP() && provider.getEIPType() == "OpenVPN") -				OpenVPN.removeStateListener(this); -	} -	 -	@Override -	protected void onResume() { -		super.onResume(); -		 -		if (provider != null) -			if (provider.hasEIP() && provider.getEIPType() == "OpenVPN") -				OpenVPN.addStateListener(this); -	}  	@Override  	protected void onActivityResult(int requestCode, int resultCode, Intent data){ @@ -150,111 +123,13 @@ public class Dashboard extends Activity implements LogInDialog.LogInDialogInterf  		providerNameTV.setText(provider.getName());  		providerNameTV.setTextSize(28); -		if ( provider.hasEIP() ){ -			startService( new Intent(EIP.ACTION_UPDATE_EIP_SERVICE) ); -			if (provider.getEIPType() == "OpenVPN") -				OpenVPN.addStateListener(this); -			serviceItemEIP(); +		FragmentManager fragMan = getFragmentManager(); +		if ( provider.hasEIP() && fragMan.findFragmentByTag(TAG_EIP_FRAGMENT) == null){ +			EipServiceFragment eipFragment = new EipServiceFragment(); +			fragMan.beginTransaction().add(R.id.servicesCollection, eipFragment, TAG_EIP_FRAGMENT).commit();  		}  	} -	/** -	 * Builds the UI for the EIP service Dashboard component -	 */ -	private void serviceItemEIP() { -		mEIPReceiver = new EIPReceiver(new Handler()); -		mEIPReceiver.setReceiver(this); - -		Intent intent = new Intent(this,EIP.class); -		intent.setAction(EIP.ACTION_IS_EIP_RUNNING); -		intent.putExtra(ConfigHelper.RECEIVER_TAG, mEIPReceiver); -		startService(intent); -		 -		ViewStub eip_overview_stub = ((ViewStub) findViewById(R.id.eipOverviewStub)); -		if(eip_overview_stub != null) -			eip_overview_stub.inflate(); - -		eipTypeTV = (TextView) findViewById(R.id.eipType); -		eipTypeTV.setText(provider.getEIPType()); -		 -		eipDetail = ((RelativeLayout) findViewById(R.id.eipDetail)); -		View eipSettings = findViewById(R.id.eipSettings); -		eipSettings.setVisibility(View.GONE); // FIXME too! -		eipDetail.setVisibility(View.VISIBLE); -		eipStatus = (TextView) findViewById(R.id.eipStatus); - -		eipSwitch = (Switch) findViewById(R.id.eipSwitch); -		eipSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { -			@Override -			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { - -				if (isChecked){ -					mEipStartPending = true; -					eipCommand(EIP.ACTION_START_EIP); -				} else { -					if (mEipStartPending){ -						AlertDialog.Builder alertBuilder = new AlertDialog.Builder(getAppContext()); -						alertBuilder.setTitle(getResources().getString(R.string.eip_cancel_connect_title)); -						alertBuilder -						.setMessage(getResources().getString(R.string.eip_cancel_connect_text)) -						.setPositiveButton(getResources().getString(R.string.eip_cancel_connect_cancel), new DialogInterface.OnClickListener() { -							@Override -							public void onClick(DialogInterface dialog, int which) { -								eipCommand(EIP.ACTION_STOP_EIP); -							} -						}) -						.setNegativeButton(getResources().getString(R.string.eip_cancel_connect_false), new DialogInterface.OnClickListener() { -							@Override -							public void onClick(DialogInterface dialog, int which) { -								eipSwitch.setChecked(true); -							} -						}) -						.show(); -					} else { -						eipCommand(EIP.ACTION_STOP_EIP); -					} -				} -			} -		}); -	} -	 -	/** -	 * Send a command to EIP -	 *  -	 * @param action	A valid String constant from EIP class representing an Intent -	 * 					filter for the EIP class  -	 */ -	private void eipCommand(String action){ -		// TODO validate "action"...how do we get the list of intent-filters for a class via Android API? -		Intent vpnIntent = new Intent(action); -		vpnIntent.putExtra(ConfigHelper.RECEIVER_TAG, mEIPReceiver); -		startService(vpnIntent); -	} -	 -	/** -	 * Expands the EIP Dashboard component for extra details view. -	 * Called by onClick property in client_dashboard.xml layout. -	 *  -	 * @param view (Unused) The View calling this method by its onClick property -	 */ -	public void toggleEipOverview(View view) { -		if (eipDetail.isShown()) -			eipDetail.setVisibility(View.GONE); -		else -			eipDetail.setVisibility(View.VISIBLE); -	} -	 -	/** -	 * Launches the se.leap.openvpn.LogWindow Activity showing detailed OpenVPN log -	 *  -	 * @param view (Unused) The View calling this method by its onClick property -	 */ -	public void showEIPLog(View view){ -		Intent intent = new Intent(getBaseContext(),LogWindow.class); -		intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); -		startActivity(intent); -	} -  	@Override  	public boolean onPrepareOptionsMenu(Menu menu) {  		JSONObject provider_json; @@ -452,100 +327,4 @@ public class Dashboard extends Activity implements LogInDialog.LogInDialogInterf  		return app;  	} -	@Override -	public void updateState(final String state, final String logmessage, final int localizedResId) { -		// Note: "states" are not organized anywhere...collected state strings: -		//		NOPROCESS,NONETWORK,BYTECOUNT,AUTH_FAILED + some parsing thing ( WAIT(?),AUTH,GET_CONFIG,ASSIGN_IP,CONNECTED,SIGINT ) -		runOnUiThread(new Runnable() { - -			@Override -			public void run() { -				if (eipStatus != null) { -					boolean switchState = true; -					String statusMessage = ""; -					String prefix = getString(localizedResId); -					if (state.equals("CONNECTED")){ -						statusMessage = "Connection Secure"; -						mEipStartPending = false; -					} else if (state.equals("BYTECOUNT")) { -						statusMessage = logmessage; -					} else if (state.equals("NOPROCESS") || state.equals("EXITING")) { -						statusMessage = "Not running! Connection not secure!"; -						switchState = false; -					} else { -						statusMessage = prefix + logmessage; -					} -					 -					eipSwitch.setChecked(switchState); -					eipStatus.setText(statusMessage); -				} -			} -		}); -	} - -	/** -	 * Inner class for handling messages related to EIP status and control requests -	 *  -	 * @author Sean Leonard <meanderingcode@aetherislands.net> -	 */ -	protected class EIPReceiver extends ResultReceiver { -		 -		Dashboard mDashboard; -		 -		protected EIPReceiver(Handler handler){ -			super(handler); -		} -		 -		public void setReceiver(Dashboard receiver) { -			mDashboard = receiver; -		} - -		@Override -		protected void onReceiveResult(int resultCode, Bundle resultData) { -			super.onReceiveResult(resultCode, resultData); -			 -			String request = resultData.getString(ConfigHelper.REQUEST_TAG); -			boolean checked = false; -			 -			if (request == EIP.ACTION_IS_EIP_RUNNING) { -				switch (resultCode){ -				case RESULT_OK: -					checked = true; -					break; -				case RESULT_CANCELED: -					checked = false; -					break; -				} -			} else if (request == EIP.ACTION_START_EIP) { -				switch (resultCode){ -				case RESULT_OK: -					checked = true; -					break; -				case RESULT_CANCELED: -					checked = false; -					break; -				} -			} else if (request == EIP.ACTION_STOP_EIP) { -				switch (resultCode){ -				case RESULT_OK: -					checked = false; -					break; -				case RESULT_CANCELED: -					checked = true; -					break; -				} -			} else if (request == EIP.EIP_NOTIFICATION) { -				switch  (resultCode){ -				case RESULT_OK: -					checked = true; -					break; -				case RESULT_CANCELED: -					checked = false; -					break; -				} -			} -			 -			eipSwitch.setChecked(checked); -		} -	}  } diff --git a/src/se/leap/leapclient/EipServiceFragment.java b/src/se/leap/leapclient/EipServiceFragment.java new file mode 100644 index 0000000..485a06a --- /dev/null +++ b/src/se/leap/leapclient/EipServiceFragment.java @@ -0,0 +1,234 @@ +package se.leap.leapclient; + +import se.leap.openvpn.LogWindow; +import se.leap.openvpn.OpenVPN; +import se.leap.openvpn.OpenVPN.StateListener; +import android.app.Activity; +import android.app.AlertDialog; +import android.app.Fragment; +import android.content.DialogInterface; +import android.content.Intent; +import android.os.Bundle; +import android.os.Handler; +import android.os.ResultReceiver; +import android.view.LayoutInflater; +import android.view.View; +import android.view.View.OnClickListener; +import android.view.ViewGroup; +import android.widget.CompoundButton.OnCheckedChangeListener; +import android.widget.CompoundButton; +import android.widget.RelativeLayout; +import android.widget.Switch; +import android.widget.TextView; + +public class EipServiceFragment extends Fragment implements StateListener, OnClickListener, OnCheckedChangeListener { +	 +	private View eipFragment; +	private Switch eipSwitch; +	private View eipDetail; +	private TextView eipStatus; + +	private boolean eipAutoSwitched = false; +	 +	private boolean mEipStartPending = false; + +    private EIPReceiver mEIPReceiver; + +	@Override +	public View onCreateView(LayoutInflater inflater, ViewGroup container, +			Bundle savedInstanceState) { +		eipFragment = inflater.inflate(R.layout.eip_service_fragment, container, false); +		 +		eipDetail = ((RelativeLayout) eipFragment.findViewById(R.id.eipDetail)); +		eipDetail.setVisibility(View.VISIBLE); + +		View eipSettings = eipFragment.findViewById(R.id.eipSettings); +		eipSettings.setVisibility(View.GONE); // FIXME too! +		 +		eipStatus = (TextView) eipFragment.findViewById(R.id.eipStatus); +		eipStatus.setOnClickListener(this); + +		eipSwitch = (Switch) eipFragment.findViewById(R.id.eipSwitch); +		eipSwitch.setOnCheckedChangeListener(this); +		 +		return eipFragment; +	} + +	@Override +	public void onCreate(Bundle savedInstanceState) { +		super.onCreate(savedInstanceState); +		 +		mEIPReceiver = new EIPReceiver(new Handler()); +	} + +	@Override +	public void onResume() { +		super.onResume(); + +		OpenVPN.addStateListener(this); +	} + +	@Override +	public void onPause() { +		super.onPause(); + +		OpenVPN.removeStateListener(this); +	} +	 +	@Override +	public void onClick(View buttonView) { +		if (buttonView.equals(eipStatus)) +			showEIPLog(); +	} +	 +	/** +	 * Launches the se.leap.openvpn.LogWindow Activity showing detailed OpenVPN log +	 */ +	public void showEIPLog(){ +		Intent intent = new Intent(getActivity().getBaseContext(),LogWindow.class); +		intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); +		startActivity(intent); +	} +	 +	@Override +	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { +		if (buttonView.equals(eipSwitch) && !eipAutoSwitched){ +			if (isChecked){ +				mEipStartPending = true; +				eipCommand(EIP.ACTION_START_EIP); +			} else { +				if (mEipStartPending){ +					AlertDialog.Builder alertBuilder = new AlertDialog.Builder(getActivity()); +					alertBuilder.setTitle(getResources().getString(R.string.eip_cancel_connect_title)); +					alertBuilder +					.setMessage(getResources().getString(R.string.eip_cancel_connect_text)) +					.setPositiveButton(getResources().getString(R.string.eip_cancel_connect_cancel), new DialogInterface.OnClickListener() { +						@Override +						public void onClick(DialogInterface dialog, int which) { +							eipCommand(EIP.ACTION_STOP_EIP); +						} +					}) +					.setNegativeButton(getResources().getString(R.string.eip_cancel_connect_false), new DialogInterface.OnClickListener() { +						@Override +						public void onClick(DialogInterface dialog, int which) { +							eipAutoSwitched = true; +							eipSwitch.setChecked(true); +							eipAutoSwitched = false; +						} +					}) +					.show(); +				} else { +					eipCommand(EIP.ACTION_STOP_EIP); +				} +			} +		} +	} +	 +	/** +	 * Send a command to EIP +	 *  +	 * @param action	A valid String constant from EIP class representing an Intent +	 * 					filter for the EIP class  +	 */ +	private void eipCommand(String action){ +		// TODO validate "action"...how do we get the list of intent-filters for a class via Android API? +		Intent vpnIntent = new Intent(action); +		vpnIntent.putExtra(ConfigHelper.RECEIVER_TAG, mEIPReceiver); +		getActivity().startService(vpnIntent); +	} +	 +	@Override +	public void updateState(final String state, final String logmessage, final int localizedResId) { +		// Note: "states" are not organized anywhere...collected state strings: +		//		NOPROCESS,NONETWORK,BYTECOUNT,AUTH_FAILED + some parsing thing ( WAIT(?),AUTH,GET_CONFIG,ASSIGN_IP,CONNECTED,SIGINT ) +		getActivity().runOnUiThread(new Runnable() { + +			@Override +			public void run() { +				if (eipStatus != null) { +					boolean switchState = true; +					String statusMessage = ""; +					String prefix = getString(localizedResId); +					if (state.equals("CONNECTED")){ +						statusMessage = "Connection Secure"; +						mEipStartPending = false; +					} else if (state.equals("BYTECOUNT")) { +						statusMessage = logmessage; +					} else if (state.equals("NOPROCESS") || state.equals("EXITING")) { +						statusMessage = "Not running! Connection not secure!"; +						switchState = false; +					} else { +						statusMessage = prefix + logmessage; +					} +					 +					eipAutoSwitched = true; +					eipSwitch.setChecked(switchState); +					eipAutoSwitched = false; +					eipStatus.setText(statusMessage); +				} +			} +		}); +	} + +	/** +	 * Inner class for handling messages related to EIP status and control requests +	 *  +	 * @author Sean Leonard <meanderingcode@aetherislands.net> +	 */ +	protected class EIPReceiver extends ResultReceiver { +		 +		protected EIPReceiver(Handler handler){ +			super(handler); +		} + +		@Override +		protected void onReceiveResult(int resultCode, Bundle resultData) { +			super.onReceiveResult(resultCode, resultData); +			 +			String request = resultData.getString(ConfigHelper.REQUEST_TAG); +			boolean checked = false; +			 +			if (request == EIP.ACTION_IS_EIP_RUNNING) { +				switch (resultCode){ +				case Activity.RESULT_OK: +					checked = true; +					break; +				case Activity.RESULT_CANCELED: +					checked = false; +					break; +				} +			} else if (request == EIP.ACTION_START_EIP) { +				switch (resultCode){ +				case Activity.RESULT_OK: +					checked = true; +					break; +				case Activity.RESULT_CANCELED: +					checked = false; +					break; +				} +			} else if (request == EIP.ACTION_STOP_EIP) { +				switch (resultCode){ +				case Activity.RESULT_OK: +					checked = false; +					break; +				case Activity.RESULT_CANCELED: +					checked = true; +					break; +				} +			} else if (request == EIP.EIP_NOTIFICATION) { +				switch  (resultCode){ +				case Activity.RESULT_OK: +					checked = true; +					break; +				case Activity.RESULT_CANCELED: +					checked = false; +					break; +				} +			} +			 +			eipAutoSwitched = true; +			eipSwitch.setChecked(checked); +			eipAutoSwitched = false; +		} +	} +}  | 
