summaryrefslogtreecommitdiff
path: root/app/src/main/java/se/leap/bitmaskclient/providersetup/ProviderApiSetupBroadcastReceiver.java
blob: 4b7d22fcbf9384e4393556cc4363b3a61d90ef51 (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
/**
 * Copyright (c) 2018 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.providersetup;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import java.lang.ref.WeakReference;

import se.leap.bitmaskclient.base.models.Constants;
import se.leap.bitmaskclient.base.models.Provider;
import se.leap.bitmaskclient.providersetup.ProviderSetupInterface.ProviderConfigState;
import se.leap.bitmaskclient.providersetup.activities.ProviderListBaseActivity;

/**
 * Broadcast receiver that handles callback intents of ProviderApi during provider setup.
 * It is used by CustomProviderSetupActivity for custom branded apps and ProviderListActivity
 * for 'normal' Bitmask.
 *
 * Created by cyberta on 17.08.18.
 */

public class ProviderApiSetupBroadcastReceiver extends BroadcastReceiver {
    private final WeakReference<ProviderSetupInterface> setupInterfaceRef;

    public ProviderApiSetupBroadcastReceiver(ProviderSetupInterface setupInterface) {
        this.setupInterfaceRef = new WeakReference<>(setupInterface);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(ProviderListBaseActivity.TAG, "received Broadcast");
        ProviderSetupInterface setupInterface = setupInterfaceRef.get();
        String action = intent.getAction();
        if (action == null || !action.equalsIgnoreCase(Constants.BROADCAST_PROVIDER_API_EVENT) || setupInterface == null) {
            return;
        }

        if (setupInterface.getConfigState() != null &&
                setupInterface.getConfigState() == ProviderConfigState.SETTING_UP_PROVIDER) {
            int resultCode = intent.getIntExtra(Constants.BROADCAST_RESULT_CODE, ProviderListBaseActivity.RESULT_CANCELED);
            Log.d(ProviderListBaseActivity.TAG, "Broadcast resultCode: " + resultCode);

            Bundle resultData = intent.getParcelableExtra(Constants.BROADCAST_RESULT_KEY);
            Provider handledProvider = resultData.getParcelable(Constants.PROVIDER_KEY);

            if (handledProvider != null && setupInterface.getProvider() != null &&
                    handledProvider.getMainUrlString().equalsIgnoreCase(setupInterface.getProvider().getMainUrlString())) {
                switch (resultCode) {
                    case ProviderAPI.PROVIDER_OK:
                        setupInterface.handleProviderSetUp(handledProvider);
                        break;
                    case ProviderAPI.MISSING_NETWORK_CONNECTION:
                    case ProviderAPI.TOR_TIMEOUT:
                    case ProviderAPI.PROVIDER_NOK:
                        setupInterface.handleError(resultData);
                        break;
                    case ProviderAPI.CORRECTLY_DOWNLOADED_VPN_CERTIFICATE:
                        setupInterface.handleCorrectlyDownloadedCertificate(handledProvider);
                        break;
                    case ProviderAPI.INCORRECTLY_DOWNLOADED_VPN_CERTIFICATE:
                        setupInterface.handleIncorrectlyDownloadedCertificate();
                        break;
                }
            }
        }
    }

}