summaryrefslogtreecommitdiff
path: root/app/src/main/java/se/leap/bitmaskclient/Dashboard.java
blob: 761afc0aebe5e884808e6c8804900553d12ed8f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
/**
 * Copyright (c) 2013 LEAP Encryption Access Project and contributers
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
 package se.leap.bitmaskclient;

import org.json.JSONException;
import org.json.JSONObject;

import se.leap.bitmaskclient.R;
import se.leap.bitmaskclient.ProviderAPIResultReceiver.Receiver;
import se.leap.bitmaskclient.SignUpDialog;
import de.blinkt.openvpn.activities.LogWindow;
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.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.os.Handler;
import android.os.ResultReceiver;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

/**
 * The main user facing Activity of LEAP Android, consisting of status, controls,
 * and access to preferences.
 * 
 * @author Sean Leonard <meanderingcode@aetherislands.net>
 * @author parmegv
 */
public class Dashboard extends Activity implements LogInDialog.LogInDialogInterface, SignUpDialog.SignUpDialogInterface, Receiver {

	protected static final int CONFIGURE_LEAP = 0;
	protected static final int SWITCH_PROVIDER = 1;

    final public static String SHARED_PREFERENCES = "LEAPPreferences";
    final public static String ACTION_QUIT = "quit";
    public static final String REQUEST_CODE = "request_code";
    public static final String PARAMETERS = "dashboard parameters";
    public static final String START_ON_BOOT = "dashboard start on boot";
    final public static String ON_BOOT = "dashboard on boot";
    public static final String APP_VERSION = "bitmask version";


    private EipServiceFragment eipFragment;
	private ProgressBar mProgressBar;
	private TextView eipStatus;
	private static Context app;
	protected static SharedPreferences preferences;
	private static Provider provider;

	private TextView providerNameTV;

	private boolean authed_eip = false;

    public ProviderAPIResultReceiver providerAPI_result_receiver;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		app = this;		
		
		PRNGFixes.apply();

	    mProgressBar = (ProgressBar) findViewById(R.id.eipProgress);
	    
	    preferences = getSharedPreferences(SHARED_PREFERENCES, MODE_PRIVATE);
	    handleVersion();
	    
	    authed_eip = preferences.getBoolean(EIP.AUTHED_EIP, false);
		if (preferences.getString(Provider.KEY, "").isEmpty())
			startActivityForResult(new Intent(this,ConfigurationWizard.class),CONFIGURE_LEAP);
		else
		    buildDashboard(getIntent().getBooleanExtra(ON_BOOT, false));
	}

    private void handleVersion() {
	try {
	    int versionCode = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;
	    int lastDetectedVersion = preferences.getInt(APP_VERSION, 0);
	    preferences.edit().putInt(APP_VERSION, versionCode);
	    Log.d("Dashboard", "detected version code: " + versionCode);
	    Log.d("Dashboard", "last detected version code: " + lastDetectedVersion);

	    switch(versionCode) {
	    case 91: // 0.6.0 without Bug #5999
		if(!preferences.getString(EIP.KEY, "").isEmpty()) {
		    Intent rebuildVpnProfiles = new Intent(getApplicationContext(), EIP.class);
		    rebuildVpnProfiles.setAction(EIP.ACTION_REBUILD_PROFILES);
		    startService(rebuildVpnProfiles);
		}
	    }
	} catch (NameNotFoundException e) {
	}
    }

	@Override
	protected void onDestroy() {
	    
		super.onDestroy();
	}

    protected void onPause() {
	super.onPause();
    }
    
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data){
		if ( requestCode == CONFIGURE_LEAP || requestCode == SWITCH_PROVIDER) {
		// It should be equivalent: if ( (requestCode == CONFIGURE_LEAP) || (data!= null && data.hasExtra(STOP_FIRST))) {
			if ( resultCode == RESULT_OK ){
				preferences.edit().putInt(EIP.PARSED_SERIAL, 0).commit();
				preferences.edit().putBoolean(EIP.AUTHED_EIP, authed_eip).commit();
				Intent updateEIP = new Intent(getApplicationContext(), EIP.class);
				updateEIP.setAction(EIP.ACTION_UPDATE_EIP_SERVICE);
				startService(updateEIP);
				buildDashboard(false);
				invalidateOptionsMenu();
				if(data != null && data.hasExtra(LogInDialog.VERB)) {
					View view = ((ViewGroup)findViewById(android.R.id.content)).getChildAt(0);
					logInDialog(Bundle.EMPTY);
				}
			} else if(resultCode == RESULT_CANCELED && (data == null || data.hasExtra(ACTION_QUIT))) {
				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) {
				    preferences.edit().remove(Provider.KEY).commit();
					finish();
				}
			})
			.show();
	}
	
	/**
	 * Inflates permanent UI elements of the View and contains logic for what
	 * service dependent UI elements to include.
	 */
	private void buildDashboard(boolean hide_and_turn_on_eip) {
		provider = Provider.getInstance();
		provider.init( this );

		setContentView(R.layout.client_dashboard);
	    
		providerNameTV = (TextView) findViewById(R.id.providerName);
		providerNameTV.setText(provider.getDomain());
		providerNameTV.setTextSize(28);
		
	    mProgressBar = (ProgressBar) findViewById(R.id.eipProgress);

		FragmentManager fragMan = getFragmentManager();
		if ( provider.hasEIP()){
			eipFragment = new EipServiceFragment();
			if (hide_and_turn_on_eip) {
			    preferences.edit().remove(Dashboard.START_ON_BOOT).commit();
			    Bundle arguments = new Bundle();
			    arguments.putBoolean(EipServiceFragment.START_ON_BOOT, true);
			    eipFragment.setArguments(arguments);
			}
			fragMan.beginTransaction().replace(R.id.servicesCollection, eipFragment, EipServiceFragment.TAG).commit();

			if (hide_and_turn_on_eip) {
			    onBackPressed();
			}
		}
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		JSONObject provider_json;
		try {
			provider_json = new JSONObject(preferences.getString(Provider.KEY, ""));
			JSONObject service_description = provider_json.getJSONObject(Provider.SERVICE);
			boolean authed_eip = preferences.getBoolean(EIP.AUTHED_EIP, false);
			boolean allow_registered_eip = service_description.getBoolean(Provider.ALLOW_REGISTRATION);
			preferences.edit().putBoolean(EIP.ALLOWED_REGISTERED, allow_registered_eip);
			if(allow_registered_eip) {
				if(authed_eip) {
					menu.findItem(R.id.login_button).setVisible(false);
					menu.findItem(R.id.logout_button).setVisible(true);
				} else {
					menu.findItem(R.id.login_button).setVisible(true);
					menu.findItem(R.id.logout_button).setVisible(false);
				}
				menu.findItem(R.id.signup_button).setVisible(true);
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return true;
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.client_dashboard, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item){
		Intent intent;
		switch (item.getItemId()){
		case R.id.about_leap:
			intent = new Intent(this, AboutActivity.class);
			startActivity(intent);
			return true;
		case R.id.log_window:
		    Intent startLW = new Intent(getAppContext(), LogWindow.class);
		    startLW.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
		    startActivity(startLW);
		    return true;
		case R.id.switch_provider:
			if (Provider.getInstance().hasEIP()){
				if (preferences.getBoolean(EIP.AUTHED_EIP, false)){
					logOut();
				}				
				eipStop();
			}
			preferences.edit().clear().commit();
			startActivityForResult(new Intent(this,ConfigurationWizard.class), SWITCH_PROVIDER);
			return true;
		case R.id.login_button:
			View view = ((ViewGroup)findViewById(android.R.id.content)).getChildAt(0);
			logInDialog(Bundle.EMPTY);
			return true;
		case R.id.logout_button:
			logOut();
			return true;
		case R.id.signup_button:
			signUpDialog(((ViewGroup)findViewById(android.R.id.content)).getChildAt(0), Bundle.EMPTY);
			return true;
		default:
				return super.onOptionsItemSelected(item);
		}
		
	}

	@Override
	public void authenticate(String username, String password) {
	    mProgressBar = (ProgressBar) findViewById(R.id.eipProgress);
		eipStatus = (TextView) findViewById(R.id.eipStatus);
		
		providerAPI_result_receiver = new ProviderAPIResultReceiver(new Handler());
		providerAPI_result_receiver.setReceiver(this);
		
		Intent provider_API_command = new Intent(this, ProviderAPI.class);

		Bundle parameters = new Bundle();
		parameters.putString(LogInDialog.USERNAME, username);
		parameters.putString(LogInDialog.PASSWORD, password);

		JSONObject provider_json;
		try {
			provider_json = new JSONObject(preferences.getString(Provider.KEY, ""));
			parameters.putString(Provider.API_URL, provider_json.getString(Provider.API_URL) + "/" + provider_json.getString(Provider.API_VERSION));
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		provider_API_command.setAction(ProviderAPI.SRP_AUTH);
		provider_API_command.putExtra(ProviderAPI.PARAMETERS, parameters);
		provider_API_command.putExtra(ProviderAPI.RECEIVER_KEY, providerAPI_result_receiver);
		
		mProgressBar.setVisibility(ProgressBar.VISIBLE);
		eipStatus.setText(R.string.authenticating_message);
		//mProgressBar.setMax(4);
		startService(provider_API_command);
	}

    public void cancelAuthedEipOn() {
	EipServiceFragment eipFragment = (EipServiceFragment) getFragmentManager().findFragmentByTag(EipServiceFragment.TAG);
	eipFragment.checkEipSwitch(false);
    }

    public void cancelLoginOrSignup() {
	if(mProgressBar == null) mProgressBar = (ProgressBar) findViewById(R.id.eipProgress);
	if(mProgressBar != null) {
	    mProgressBar.setVisibility(ProgressBar.GONE);
	    if(eipStatus == null) eipStatus = (TextView) findViewById(R.id.eipStatus);
	    if(eipStatus != null) eipStatus.setText("");
	}
	cancelAuthedEipOn();
    }
	
	/**
	 * Asks ProviderAPI to log out.
	 */
	public void logOut() {
		providerAPI_result_receiver = new ProviderAPIResultReceiver(new Handler());
		providerAPI_result_receiver.setReceiver(this);
		Intent provider_API_command = new Intent(this, ProviderAPI.class);

		Bundle parameters = new Bundle();

		JSONObject provider_json;
		try {
			provider_json = new JSONObject(preferences.getString(Provider.KEY, ""));
			parameters.putString(Provider.API_URL, provider_json.getString(Provider.API_URL) + "/" + provider_json.getString(Provider.API_VERSION));
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		provider_API_command.setAction(ProviderAPI.LOG_OUT);
		provider_API_command.putExtra(ProviderAPI.PARAMETERS, parameters);
		provider_API_command.putExtra(ProviderAPI.RECEIVER_KEY, providerAPI_result_receiver);
		
		if(mProgressBar == null) mProgressBar = (ProgressBar) findViewById(R.id.eipProgress);
		mProgressBar.setVisibility(ProgressBar.VISIBLE);
		if(eipStatus == null) eipStatus = (TextView) findViewById(R.id.eipStatus);
		eipStatus.setText(R.string.logout_message);
	//	eipStatus.setText("Starting to logout");
		
		startService(provider_API_command);
		//mProgressBar.setMax(1);

	}
	
	/**
	 * Shows the log in dialog.
	 */
	public void logInDialog(Bundle resultData) {
	    Log.d("Dashboard", "Log In Dialog");
		FragmentTransaction fragment_transaction = getFragmentManager().beginTransaction();
	    Fragment previous_log_in_dialog = getFragmentManager().findFragmentByTag(LogInDialog.TAG);
	    if (previous_log_in_dialog != null) {
	        fragment_transaction.remove(previous_log_in_dialog);
	    }
	    fragment_transaction.addToBackStack(null);

	    DialogFragment newFragment = LogInDialog.newInstance();
	    if(resultData != null && !resultData.isEmpty()) {
	    	newFragment.setArguments(resultData);
	    }
	    newFragment.show(fragment_transaction, LogInDialog.TAG);
	}

    	@Override
	public void signUp(String username, String password) {
	    mProgressBar = (ProgressBar) findViewById(R.id.eipProgress);
	    eipStatus = (TextView) findViewById(R.id.eipStatus);
		
	    providerAPI_result_receiver = new ProviderAPIResultReceiver(new Handler());
	    providerAPI_result_receiver.setReceiver(this);
		
	    Intent provider_API_command = new Intent(this, ProviderAPI.class);

	    Bundle parameters = new Bundle();
	    parameters.putString(SignUpDialog.USERNAME, username);
	    parameters.putString(SignUpDialog.PASSWORD, password);

	    JSONObject provider_json;
	    try {
		provider_json = new JSONObject(preferences.getString(Provider.KEY, ""));
		parameters.putString(Provider.API_URL, provider_json.getString(Provider.API_URL) + "/" + provider_json.getString(Provider.API_VERSION));
	    } catch (JSONException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	    }

	    provider_API_command.setAction(ProviderAPI.SRP_REGISTER);
	    provider_API_command.putExtra(ProviderAPI.PARAMETERS, parameters);
	    provider_API_command.putExtra(ProviderAPI.RECEIVER_KEY, providerAPI_result_receiver);
		
	    mProgressBar.setVisibility(ProgressBar.VISIBLE);
	    eipStatus.setText(R.string.signingup_message);
	    //mProgressBar.setMax(4);
	    startService(provider_API_command);
	}
    
	/**
	 * Shows the sign up dialog.
	 * @param view from which the dialog is created.
	 */
	public void signUpDialog(View view, Bundle resultData) {
	    FragmentTransaction fragment_transaction = getFragmentManager().beginTransaction();
	    Fragment previous_sign_up_dialog = getFragmentManager().findFragmentByTag(SignUpDialog.TAG);
	    if (previous_sign_up_dialog != null) {
	        fragment_transaction.remove(previous_sign_up_dialog);
	    }
	    fragment_transaction.addToBackStack(null);

	    DialogFragment newFragment = SignUpDialog.newInstance();
	    if(resultData != null && !resultData.isEmpty()) {
	    	newFragment.setArguments(resultData);
	    }
	    newFragment.show(fragment_transaction, SignUpDialog.TAG);
	}

	/**
	 * Asks ProviderAPI to download an authenticated OpenVPN certificate.
	 * @param session_id cookie for the server to allow us to download the certificate.
	 */
	private void downloadAuthedUserCertificate(/*Cookie session_id*/) {
		providerAPI_result_receiver = new ProviderAPIResultReceiver(new Handler());
		providerAPI_result_receiver.setReceiver(this);
		
		Intent provider_API_command = new Intent(this, ProviderAPI.class);

		Bundle parameters = new Bundle();
		parameters.putString(ConfigurationWizard.TYPE_OF_CERTIFICATE, ConfigurationWizard.AUTHED_CERTIFICATE);
		/*parameters.putString(ConfigHelper.SESSION_ID_COOKIE_KEY, session_id.getName());
		parameters.putString(ConfigHelper.SESSION_ID_KEY, session_id.getValue());*/

		provider_API_command.setAction(ProviderAPI.DOWNLOAD_CERTIFICATE);
		provider_API_command.putExtra(ProviderAPI.PARAMETERS, parameters);
		provider_API_command.putExtra(ProviderAPI.RECEIVER_KEY, providerAPI_result_receiver);
		
		startService(provider_API_command);
	}

	@Override
	public void onReceiveResult(int resultCode, Bundle resultData) {
	    if(resultCode == ProviderAPI.SRP_REGISTRATION_SUCCESSFUL){
		authenticate(resultData.getString(LogInDialog.USERNAME), resultData.getString(LogInDialog.PASSWORD));
	    } else if(resultCode == ProviderAPI.SRP_REGISTRATION_FAILED){
		signUpDialog(((ViewGroup)findViewById(android.R.id.content)).getChildAt(0), resultData);
	    } else if(resultCode == ProviderAPI.SRP_AUTHENTICATION_SUCCESSFUL){
			String session_id_cookie_key = resultData.getString(ProviderAPI.SESSION_ID_COOKIE_KEY);
			String session_id_string = resultData.getString(ProviderAPI.SESSION_ID_KEY);
			setResult(RESULT_OK);

			authed_eip = true;
			preferences.edit().putBoolean(EIP.AUTHED_EIP, authed_eip).commit();

			invalidateOptionsMenu();
        	mProgressBar.setVisibility(ProgressBar.GONE);
    		changeStatusMessage(resultCode);

        	//Cookie session_id = new BasicClientCookie(session_id_cookie_key, session_id_string);
        	downloadAuthedUserCertificate(/*session_id*/);
		} else if(resultCode == ProviderAPI.SRP_AUTHENTICATION_FAILED) {
		    logInDialog(resultData);
		} else if(resultCode == ProviderAPI.LOGOUT_SUCCESSFUL) {
			authed_eip = false;
			preferences.edit().putBoolean(EIP.AUTHED_EIP, authed_eip).commit();
			mProgressBar.setVisibility(ProgressBar.GONE);
			mProgressBar.setProgress(0);
			invalidateOptionsMenu();
			setResult(RESULT_OK);
			changeStatusMessage(resultCode);

		} else if(resultCode == ProviderAPI.LOGOUT_FAILED) {
			setResult(RESULT_CANCELED);
			changeStatusMessage(resultCode);
        	mProgressBar.setVisibility(ProgressBar.GONE);		    
		} else if(resultCode == ProviderAPI.CORRECTLY_DOWNLOADED_CERTIFICATE) {
        	setResult(RESULT_OK);
    		changeStatusMessage(resultCode);
		if(mProgressBar != null)
		    mProgressBar.setVisibility(ProgressBar.GONE);
		if(EipServiceFragment.isEipSwitchChecked())
		    eipStart();
		else
		    eipStatus.setText(R.string.eip_state_not_connected);
		} else if(resultCode == ProviderAPI.INCORRECTLY_DOWNLOADED_CERTIFICATE) {
        	setResult(RESULT_CANCELED);
    		changeStatusMessage(resultCode);
        	mProgressBar.setVisibility(ProgressBar.GONE);
 		}
	}

	private void changeStatusMessage(final int previous_result_code) {
		// TODO Auto-generated method stub
		ResultReceiver eip_status_receiver = new ResultReceiver(new Handler()){
			protected void onReceiveResult(int resultCode, Bundle resultData){
				super.onReceiveResult(resultCode, resultData);
				String request = resultData.getString(EIP.REQUEST_TAG);
				if (request.equalsIgnoreCase(EIP.ACTION_IS_EIP_RUNNING)){					
					if (resultCode == Activity.RESULT_OK){

						switch(previous_result_code){
						case ProviderAPI.SRP_AUTHENTICATION_SUCCESSFUL: eipStatus.setText(R.string.succesful_authentication_message); break;
						case ProviderAPI.SRP_AUTHENTICATION_FAILED: eipStatus.setText(R.string.authentication_failed_message); break;
						case ProviderAPI.CORRECTLY_DOWNLOADED_CERTIFICATE: eipStatus.setText(R.string.authed_secured_status); break;
						case ProviderAPI.INCORRECTLY_DOWNLOADED_CERTIFICATE: eipStatus.setText(R.string.incorrectly_downloaded_certificate_message); break;
						case ProviderAPI.LOGOUT_SUCCESSFUL: eipStatus.setText(R.string.logged_out_message); break;
						case ProviderAPI.LOGOUT_FAILED: eipStatus.setText(R.string.log_out_failed_message); break;
						
						}	
					}
					else if(resultCode == Activity.RESULT_CANCELED){

						switch(previous_result_code){

						case ProviderAPI.SRP_AUTHENTICATION_SUCCESSFUL: eipStatus.setText(R.string.succesful_authentication_message); break;
						case ProviderAPI.SRP_AUTHENTICATION_FAILED: eipStatus.setText(R.string.authentication_failed_message); break;
						case ProviderAPI.CORRECTLY_DOWNLOADED_CERTIFICATE: break;
						case ProviderAPI.INCORRECTLY_DOWNLOADED_CERTIFICATE: eipStatus.setText(R.string.incorrectly_downloaded_certificate_message); break;
						case ProviderAPI.LOGOUT_SUCCESSFUL: eipStatus.setText(R.string.logged_out_message); break;
						case ProviderAPI.LOGOUT_FAILED: eipStatus.setText(R.string.log_out_failed_message); break;			
						}
					}
				}
					
			}
		};
		eipIsRunning(eip_status_receiver);		
	}

	/**
	 * 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 startActivityForResult(Intent intent, int requestCode) {
        intent.putExtra(Dashboard.REQUEST_CODE, requestCode);
        super.startActivityForResult(intent, requestCode);
    }
	/**
	 * Send a command to EIP
	 * 
	 * @param action	A valid String constant from EIP class representing an Intent
	 * 					filter for the EIP class 
	 */
	private void eipIsRunning(ResultReceiver eip_receiver){
		// TODO validate "action"...how do we get the list of intent-filters for a class via Android API?
		Intent eip_intent = new Intent(this, EIP.class);
		eip_intent.setAction(EIP.ACTION_IS_EIP_RUNNING);
		eip_intent.putExtra(EIP.RECEIVER_TAG, eip_receiver);
		startService(eip_intent);
	}
	
	/**
	 * Send a command to EIP
	 * 	
	 */
	private void eipStop(){
		// TODO validate "action"...how do we get the list of intent-filters for a class via Android API?
		Intent eip_intent = new Intent(this, EIP.class);
		eip_intent.setAction(EIP.ACTION_STOP_EIP);
	//	eip_intent.putExtra(EIP.RECEIVER_TAG, eip_receiver);fi
		startService(eip_intent);
		
	}

    private void eipStart(){
	Intent eip_intent = new Intent(this, EIP.class);
	eip_intent.setAction(EIP.ACTION_START_EIP);
	eip_intent.putExtra(EIP.RECEIVER_TAG, EipServiceFragment.getReceiver());
	startService(eip_intent);

    }

    protected void setProgressBarVisibility(int visibility) {
	if(mProgressBar == null)
	    mProgressBar = (ProgressBar) findViewById(R.id.eipProgress);	    
	mProgressBar.setVisibility(visibility);
    }
}