summaryrefslogtreecommitdiff
path: root/src/se/leap/bitmaskclient/EipServiceFragment.java
blob: daf446a5f4e0726f5bd21e08f25da7db92d46e05 (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
package se.leap.bitmaskclient;

import se.leap.bitmaskclient.R;
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.MotionEvent;
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 static final String IS_EIP_PENDING = "is_eip_pending";
	
	private View eipFragment;
	private Switch eipSwitch;
	private View eipDetail;
	private TextView eipStatus;

	private boolean eipAutoSwitched = true;
	
	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!

		if (mEipStartPending)
			eipFragment.findViewById(R.id.eipProgress).setVisibility(View.VISIBLE);
		
		eipStatus = (TextView) eipFragment.findViewById(R.id.eipStatus);
		eipStatus.setOnClickListener(this);

		eipSwitch = (Switch) eipFragment.findViewById(R.id.eipSwitch);
		eipSwitch.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				eipAutoSwitched = false;
				return false;
			}
		});
		eipSwitch.setOnCheckedChangeListener(this);
		
		return eipFragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		mEIPReceiver = new EIPReceiver(new Handler());

		if (savedInstanceState != null)
			mEipStartPending = savedInstanceState.getBoolean(IS_EIP_PENDING);
	}

	@Override
	public void onResume() {
		super.onResume();

		OpenVPN.addStateListener(this);
	}

	@Override
	public void onPause() {
		super.onPause();

		OpenVPN.removeStateListener(this);
	}
	
	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putBoolean(IS_EIP_PENDING, mEipStartPending);
	}
	
	@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;
				eipFragment.findViewById(R.id.eipProgress).setVisibility(View.VISIBLE);
				((TextView) eipFragment.findViewById(R.id.eipStatus)).setText(R.string.eip_status_start_pending);
				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);
				}
			}
		}
		eipAutoSwitched = true;
	}
	
	/**
	 * 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(EIP.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";
						getActivity().findViewById(R.id.eipProgress).setVisibility(View.GONE);
						mEipStartPending = false;
					} else if (state.equals("BYTECOUNT")) {
						statusMessage = logmessage;
					} else if ( (state.equals("NOPROCESS") && !mEipStartPending ) || state.equals("EXITING")) {
						statusMessage = "Not running! Connection not secure!";
						getActivity().findViewById(R.id.eipProgress).setVisibility(View.GONE);
						mEipStartPending = false;
						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(EIP.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;
					eipFragment.findViewById(R.id.eipProgress).setVisibility(View.GONE);
					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;
		}
	}
}