summaryrefslogtreecommitdiff
path: root/app/src/main/java/se/leap/bitmaskclient/EipFragment.java
blob: 02eedd777dbe4ad33f88d1c50a16c4ba3fa53582 (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
/**
 * 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 android.app.*;
import android.content.*;
import android.os.*;
import android.util.*;
import android.view.*;
import android.widget.*;

import org.jetbrains.annotations.*;

import java.util.*;

import butterknife.*;
import de.blinkt.openvpn.activities.*;
import se.leap.bitmaskclient.eip.*;

public class EipFragment extends Fragment implements Observer {

    public static String TAG = EipFragment.class.getSimpleName();

    protected static final String IS_PENDING = TAG + ".is_pending";
    protected static final String IS_CONNECTED = TAG + ".is_connected";
    protected static final String STATUS_MESSAGE = TAG + ".status_message";
    public static final String START_ON_BOOT = "start on boot";

    @InjectView(R.id.eipSwitch)
    Switch eip_switch;
    @InjectView(R.id.status_message)
    TextView status_message;
    @InjectView(R.id.eipProgress)
    ProgressBar progress_bar;

    private static Dashboard dashboard;
    private static EIPReceiver mEIPReceiver;
    private static EipStatus eip_status;
    private boolean is_starting_to_connect;
    private boolean wants_to_connect;

    public void onAttach(Activity activity) {
        super.onAttach(activity);

        dashboard = (Dashboard) activity;
        dashboard.providerApiCommand(Bundle.EMPTY, 0, ProviderAPI.DOWNLOAD_EIP_SERVICE);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        eip_status = EipStatus.getInstance();
        eip_status.addObserver(this);
        mEIPReceiver = new EIPReceiver(new Handler());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.eip_service_fragment, container, false);
        ButterKnife.inject(this, view);

        if (eip_status.isConnecting())
            eip_switch.setVisibility(View.VISIBLE);

        Bundle arguments = getArguments();
        if (arguments != null && arguments.containsKey(START_ON_BOOT) && arguments.getBoolean(START_ON_BOOT))
            startEipFromScratch();
        if (savedInstanceState != null) restoreState(savedInstanceState);

        return view;
    }

    private void restoreState(@NotNull Bundle savedInstanceState) {
        if (savedInstanceState.getBoolean(IS_PENDING))
            eip_status.setConnecting();
        else if (savedInstanceState.getBoolean(IS_CONNECTED))
            eip_status.setConnectedOrDisconnected();
        else
            status_message.setText(savedInstanceState.getString(STATUS_MESSAGE));
    }

    @Override
    public void onResume() {
        super.onResume();
        eipCommand(Constants.ACTION_CHECK_CERT_VALIDITY);
        handleNewState(eip_status);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putBoolean(IS_PENDING, eip_status.isConnecting());
        outState.putBoolean(IS_CONNECTED, eip_status.isConnected());
        outState.putString(STATUS_MESSAGE, status_message.getText().toString());
        super.onSaveInstanceState(outState);
    }

    protected void saveStatus() {
        boolean is_on = eip_switch.isChecked();
        Dashboard.preferences.edit().putBoolean(Dashboard.START_ON_BOOT, is_on).commit();
    }

    void handleNewVpnCertificate() {
        handleSwitch(!eip_switch.isEnabled());
    }

    @OnCheckedChanged(R.id.eipSwitch)
    void handleSwitch(boolean isChecked) {
        if (isChecked)
            handleSwitchOn();
        else
            handleSwitchOff();

        saveStatus();
    }

    private void handleSwitchOn() {
        if (canStartEIP())
            startEipFromScratch();
        else if (canLogInToStartEIP()) {
            wants_to_connect = true;
            Bundle bundle = new Bundle();
            bundle.putBoolean(IS_PENDING, true);
            dashboard.sessionDialog(bundle);
        }
    }

    private boolean canStartEIP() {
        boolean certificateExists = !Dashboard.preferences.getString(Constants.CERTIFICATE, "").isEmpty();
        boolean isAllowedAnon = Dashboard.preferences.getBoolean(Constants.ALLOWED_ANON, false);
        return (isAllowedAnon || certificateExists) && !eip_status.isConnected() && !eip_status.isConnecting();
    }

    private boolean canLogInToStartEIP() {
        boolean isAllowedRegistered = Dashboard.preferences.getBoolean(Constants.ALLOWED_REGISTERED, false);
        boolean isLoggedIn = !LeapSRPSession.getToken().isEmpty();
        return isAllowedRegistered && !isLoggedIn && !eip_status.isConnecting() && !eip_status.isConnected();
    }

    private void handleSwitchOff() {
        if (eip_status.isConnecting()) {
            askPendingStartCancellation();
        } else if (eip_status.isConnected()) {
            askToStopEIP();
        } else
            setDisconnectedUI();
    }

    private void askPendingStartCancellation() {
        AlertDialog.Builder alertBuilder = new AlertDialog.Builder(dashboard);
        alertBuilder.setTitle(dashboard.getString(R.string.eip_cancel_connect_title))
                .setMessage(dashboard.getString(R.string.eip_cancel_connect_text))
                .setPositiveButton((R.string.yes), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        askToStopEIP();
                    }
                })
                .setNegativeButton(dashboard.getString(R.string.no), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        eip_switch.setChecked(true);
                    }
                })
                .show();
    }

    public void startEipFromScratch() {
        wants_to_connect = false;
        is_starting_to_connect = true;
        progress_bar.setVisibility(View.VISIBLE);
        eip_switch.setVisibility(View.VISIBLE);
        String status = dashboard.getString(R.string.eip_status_start_pending);
        status_message.setText(status);

        if (!eip_switch.isChecked()) {
            eip_switch.setChecked(true);
        }
        saveStatus();
        eipCommand(Constants.ACTION_START_EIP);
    }

    private void stop() {
        if (eip_status.isConnecting())
            VoidVpnService.stop();
        disconnect();
    }

    private void disconnect() {
        Intent disconnect_vpn = new Intent(dashboard, DisconnectVPN.class);
        dashboard.startActivityForResult(disconnect_vpn, EIP.DISCONNECT);
        eip_status.setDisconnecting();
    }

    protected void stopEipIfPossible() {

        hideProgressBar();

        String message = dashboard.getString(R.string.eip_state_not_connected);
        status_message.setText(message);

        eipCommand(Constants.ACTION_STOP_EIP);
    }

    private void askToStopEIP() {
        AlertDialog.Builder alertBuilder = new AlertDialog.Builder(dashboard);
        alertBuilder.setTitle(dashboard.getString(R.string.eip_cancel_connect_title))
                .setMessage(dashboard.getString(R.string.eip_warning_browser_inconsistency))
                .setPositiveButton((R.string.yes), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        stopEipIfPossible();
                    }
                })
                .setNegativeButton(dashboard.getString(R.string.no), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        eip_switch.setChecked(true);
                    }
                })
                .show();
    }

    protected void updateEipService() {
        eipCommand(Constants.ACTION_UPDATE_EIP_SERVICE);
    }

    /**
     * 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 vpn_intent = new Intent(dashboard.getApplicationContext(), EIP.class);
        vpn_intent.setAction(action);
        vpn_intent.putExtra(Constants.RECEIVER_TAG, mEIPReceiver);
        dashboard.startService(vpn_intent);
    }

    @Override
    public void update(Observable observable, Object data) {
        if (observable instanceof EipStatus) {
            eip_status = (EipStatus) observable;
            final EipStatus eip_status = (EipStatus) observable;
            dashboard.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    handleNewState(eip_status);
                }
            });
        }
    }

    private void handleNewState(EipStatus eip_status) {
        if (eip_status.wantsToDisconnect())
            setDisconnectedUI();
        else if (eip_status.isConnecting() || is_starting_to_connect)
            setInProgressUI(eip_status);
        else if (eip_status.isConnected())
            setConnectedUI();
        else if (eip_status.isDisconnected() && !eip_status.isConnecting())
            setDisconnectedUI();
    }

    private void setConnectedUI() {
        hideProgressBar();
        adjustSwitch();
        is_starting_to_connect = false;
        status_message.setText(dashboard.getString(R.string.eip_state_connected));
    }

    private void setDisconnectedUI() {
        hideProgressBar();
        adjustSwitch();
        if (eip_status.errorInLast(5, dashboard.getApplicationContext())
                && !status_message.getText().toString().equalsIgnoreCase(dashboard.getString(R.string.eip_state_not_connected))) {
            dashboard.showLog();
            VoidVpnService.stop();
        }
        status_message.setText(dashboard.getString(R.string.eip_state_not_connected));
    }

    private void adjustSwitch() {
        if (eip_status.isConnected() || eip_status.isConnecting() || is_starting_to_connect) {
            if (!eip_switch.isChecked()) {
                eip_switch.setChecked(true);
            }
        } else {

            if (eip_switch.isChecked()) {
                eip_switch.setChecked(false);
            }
        }
    }

    private void setInProgressUI(EipStatus eip_status) {
        int localizedResId = eip_status.getLocalizedResId();
        String logmessage = eip_status.getLogMessage();
        String prefix = dashboard.getString(localizedResId);

        showProgressBar();
        status_message.setText(prefix + " " + logmessage);
        is_starting_to_connect = false;
        adjustSwitch();
    }

    private void updatingCertificateUI() {
        showProgressBar();
        status_message.setText(getString(R.string.updating_certificate_message));
    }

    private void showProgressBar() {
        if (progress_bar != null)
            progress_bar.setVisibility(View.VISIBLE);
    }

    private void hideProgressBar() {
        if (progress_bar != null)
            progress_bar.setVisibility(View.GONE);
    }

    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(Constants.REQUEST_TAG);

            if (request.equals(Constants.ACTION_START_EIP)) {
                switch (resultCode) {
                    case Activity.RESULT_OK:
                        break;
                    case Activity.RESULT_CANCELED:

                        break;
                }
            } else if (request.equals(Constants.ACTION_STOP_EIP)) {
                switch (resultCode) {
                    case Activity.RESULT_OK:
                        stop();
                        break;
                    case Activity.RESULT_CANCELED:
                        break;
                }
            } else if (request.equals(Constants.EIP_NOTIFICATION)) {
                switch (resultCode) {
                    case Activity.RESULT_OK:
                        break;
                    case Activity.RESULT_CANCELED:
                        break;
                }
            } else if (request.equals(Constants.ACTION_CHECK_CERT_VALIDITY)) {
                switch (resultCode) {
                    case Activity.RESULT_OK:
                        break;
                    case Activity.RESULT_CANCELED:
                        updatingCertificateUI();
                        dashboard.downloadVpnCertificate();
                        break;
                }
            } else if (request.equals(Constants.ACTION_UPDATE_EIP_SERVICE)) {
                switch (resultCode) {
                    case Activity.RESULT_OK:
                        if (wants_to_connect)
                            startEipFromScratch();
                        break;
                    case Activity.RESULT_CANCELED:
                        handleNewState(eip_status);
                        break;
                }
            }
        }
    }


    public static EIPReceiver getReceiver() {
        return mEIPReceiver;
    }
}