summaryrefslogtreecommitdiff
path: root/src/se/leap/leapclient/Dashboard.java
diff options
context:
space:
mode:
authorParménides GV <parmegv@ma.sdf.org>2013-06-24 19:37:35 +0200
committerParménides GV <parmegv@ma.sdf.org>2013-06-24 19:37:35 +0200
commit50db5b5816476751f39bb7b8adc927562a0cc677 (patch)
treefa8fc602518430137895fbdbaba439f6c3d92321 /src/se/leap/leapclient/Dashboard.java
parent195d5ab2d518f6f3960edd3c636e941830c2664d (diff)
parent304b29d6cb85d710756a56bc9c009429b8407d69 (diff)
Merge branch 'feature/eip' into develop
Diffstat (limited to 'src/se/leap/leapclient/Dashboard.java')
-rw-r--r--src/se/leap/leapclient/Dashboard.java296
1 files changed, 227 insertions, 69 deletions
diff --git a/src/se/leap/leapclient/Dashboard.java b/src/se/leap/leapclient/Dashboard.java
index dd8ee335..3c9df56c 100644
--- a/src/se/leap/leapclient/Dashboard.java
+++ b/src/se/leap/leapclient/Dashboard.java
@@ -6,9 +6,11 @@ import org.json.JSONException;
import org.json.JSONObject;
import se.leap.leapclient.ProviderAPIResultReceiver.Receiver;
-import se.leap.leapclient.R;
import se.leap.openvpn.AboutFragment;
+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;
@@ -20,17 +22,25 @@ 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;
-public class Dashboard extends Activity implements LogInDialog.LogInDialogInterface, Receiver {
+/**
+ * The main user facing Activity of LEAP Android, consisting of status, controls,
+ * and access to preferences.
+ *
+ * @author Sean Leonard <meanderingcode@aetherislands.net>
+ */
+public class Dashboard extends Activity implements LogInDialog.LogInDialogInterface,Receiver,StateListener {
protected static final int CONFIGURE_LEAP = 0;
@@ -40,8 +50,14 @@ public class Dashboard extends Activity implements LogInDialog.LogInDialogInterf
private TextView providerNameTV;
private TextView eipTypeTV;
+ private Switch eipSwitch;
+ private View eipDetail;
+ private TextView eipStatus;
+
+ private boolean mEipWait = false;
public ProviderAPIResultReceiver providerAPI_result_receiver;
+ private EIPReceiver mEIPReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -55,89 +71,150 @@ public class Dashboard extends Activity implements LogInDialog.LogInDialogInterf
if(ConfigHelper.shared_preferences == null)
ConfigHelper.setSharedPreferences(preferences);
- // Check if we have preferences, run configuration wizard if not
- // TODO We should do a better check for config that this!
if (preferences.contains("provider") && preferences.getString(ConfigHelper.PROVIDER_KEY, null) != null)
buildDashboard();
else
startActivityForResult(new Intent(this,ConfigurationWizard.class),CONFIGURE_LEAP);
}
+
+ @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){
if ( requestCode == CONFIGURE_LEAP ) {
if ( resultCode == RESULT_OK ){
- // Configuration done, get our preferences again
- preferences = getSharedPreferences(ConfigHelper.PREFERENCES_KEY,MODE_PRIVATE);
-
buildDashboard();
- } else {
- // Something went wrong in configuration
- AlertDialog.Builder alertBuilder = new AlertDialog.Builder(getAppContext());
- alertBuilder.setTitle(getResources().getString(R.string.setup_error_title));
- alertBuilder
- .setMessage(getResources().getString(R.string.setup_error_text))
- .setCancelable(false)
- .setPositiveButton(getResources().getString(R.string.setup_error_configure_button), new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- startActivityForResult(new Intent(getAppContext(),ConfigurationWizard.class),CONFIGURE_LEAP);
- }
- })
- .setNegativeButton(getResources().getString(R.string.setup_error_close_button), new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- SharedPreferences.Editor prefsEdit = getSharedPreferences(ConfigHelper.PREFERENCES_KEY, MODE_PRIVATE).edit();
- prefsEdit.remove(ConfigHelper.PROVIDER_KEY).commit();
- finish();
- }
- });
- }
+ } else
+ configErrorDialog();
}
}
+ /**
+ * Dialog shown when encountering a configuration error. Such errors require
+ * reconfiguring LEAP or aborting the application.
+ */
+ private void configErrorDialog() {
+ AlertDialog.Builder alertBuilder = new AlertDialog.Builder(getAppContext());
+ alertBuilder.setTitle(getResources().getString(R.string.setup_error_title));
+ alertBuilder
+ .setMessage(getResources().getString(R.string.setup_error_text))
+ .setCancelable(false)
+ .setPositiveButton(getResources().getString(R.string.setup_error_configure_button), new DialogInterface.OnClickListener() {
+ @Override
+ public void onClick(DialogInterface dialog, int which) {
+ startActivityForResult(new Intent(getAppContext(),ConfigurationWizard.class),CONFIGURE_LEAP);
+ }
+ })
+ .setNegativeButton(getResources().getString(R.string.setup_error_close_button), new DialogInterface.OnClickListener() {
+ @Override
+ public void onClick(DialogInterface dialog, int which) {
+ SharedPreferences.Editor prefsEdit = getSharedPreferences(ConfigHelper.PREFERENCES_KEY, MODE_PRIVATE).edit();
+ prefsEdit.remove(ConfigHelper.PROVIDER_KEY).commit();
+ finish();
+ }
+ });
+ }
+
+ /**
+ * Inflates permanent UI elements of the View and contains logic for what
+ * service dependent UI elements to include.
+ */
private void buildDashboard() {
- // Get our provider
- provider = Provider.getInstance();
- provider.init( this );
-
- // Set provider name in textview
- providerNameTV = (TextView) findViewById(R.id.providerName);
- providerNameTV.setText(provider.getName());
- providerNameTV.setTextSize(28); // TODO maybe to some calculating, or a marquee?
-
- // TODO Inflate layout fragments for provider's services
- if ( provider.hasEIP() )
- serviceItemEIP();
+ provider = Provider.getInstance();
+ provider.init( this );
+
+ providerNameTV = (TextView) findViewById(R.id.providerName);
+ 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();
+ }
}
+ /**
+ * Builds the UI for the EIP service Dashboard component
+ */
private void serviceItemEIP() {
- // FIXME Provider service (eip/openvpn)
+ 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) findViewById(R.id.eipOverviewStub)).inflate();
- // Set our EIP type title
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);
- // TODO Bind our switch to run our EIP
- // What happens when our VPN stops running? does it call the listener?
- Switch eipSwitch = (Switch) findViewById(R.id.eipSwitch);
+ eipSwitch = (Switch) findViewById(R.id.eipSwitch);
eipSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
- if ( isChecked ){
- //TODO startVPN();
- } else {
- //TODO stopVPN();
+ if (!mEipWait){
+ buttonView.setClickable(false);
+ mEipWait = true;
+
+ Intent vpnIntent;
+ if (isChecked){
+ vpnIntent = new Intent(EIP.ACTION_START_EIP);
+ } else {
+ vpnIntent = new Intent(EIP.ACTION_STOP_EIP);
+ }
+ vpnIntent.putExtra(ConfigHelper.RECEIVER_TAG, mEIPReceiver);
+ startService(vpnIntent);
}
}
});
-
- //TODO write our info into the view fragment that will expand with details and a settings button
- // TODO set eip overview subview
- // TODO make eip type clickable, show subview
- // TODO attach vpn status feedback to eip overview view
- // TODO attach settings button to something
+ }
+
+ /**
+ * 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
@@ -159,7 +236,6 @@ public class Dashboard extends Activity implements LogInDialog.LogInDialogInterf
@Override
public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.client_dashboard, menu);
return true;
}
@@ -167,21 +243,15 @@ public class Dashboard extends Activity implements LogInDialog.LogInDialogInterf
@Override
public boolean onOptionsItemSelected(MenuItem item){
Intent intent;
- // Handle item selection
switch (item.getItemId()){
case R.id.about_leap:
- // TODO move se.leap.openvpn.AboutFragment into our package
Fragment aboutFragment = new AboutFragment();
FragmentTransaction trans = getFragmentManager().beginTransaction();
trans.replace(R.id.dashboardLayout, aboutFragment);
trans.addToBackStack(null);
trans.commit();
-
- //intent = new Intent(this,AboutFragment.class);
- //startActivity(intent);
return true;
case R.id.legacy_interface:
- // TODO call se.leap.openvpn.MainActivity
intent = new Intent(this,MainActivity.class);
startActivity(intent);
return true;
@@ -197,11 +267,6 @@ public class Dashboard extends Activity implements LogInDialog.LogInDialogInterf
}
}
-
- @SuppressWarnings("unused")
- private void toggleOverview() {
- // TODO Expand the one line overview item to show some details
- }
@Override
public void authenticate(String username, String password) {
@@ -319,8 +384,101 @@ public class Dashboard extends Activity implements LogInDialog.LogInDialogInterf
}
}
- // Used for getting Context when outside of a class extending Context
+ /**
+ * For retrieving the base application Context in classes that don't extend
+ * Android's Activity class
+ *
+ * @return Application Context as defined by <code>this</code> for Dashboard instance
+ */
public static Context getAppContext() {
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(?) )
+ runOnUiThread(new Runnable() {
+
+ @Override
+ public void run() {
+ if (eipStatus != null) {
+ String prefix = getString(localizedResId) + ":";
+ if (state.equals("BYTECOUNT") || state.equals("NOPROCESS"))
+ prefix = "";
+ eipStatus.setText(prefix + logmessage);
+ }
+ }
+ });
+ }
+
+ /**
+ * 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);
+ mEipWait = true;
+ 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;
+ }
+ }
+
+ Switch eipS = ((Switch) mDashboard.findViewById(R.id.eipSwitch));
+ eipS.setChecked(checked);
+ eipS.setClickable(true);
+ mEipWait = false;
+ }
+ }
}