summaryrefslogtreecommitdiff
path: root/app/src/main/java/se/leap/bitmaskclient/providersetup/ProviderSetupObservable.java
blob: 237f5bd27a19793761b589f0ae29b8d9a6edc55f (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
package se.leap.bitmaskclient.providersetup;
/**
 * Copyright (c) 2023 LEAP Encryption Access Project and contributors
 *
 * 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/>.
 */

import android.os.Bundle;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

import se.leap.bitmaskclient.base.utils.HandlerProvider;
import se.leap.bitmaskclient.base.utils.HandlerProvider.HandlerInterface;
import se.leap.bitmaskclient.tor.TorStatusObservable;

/**
 * This Observable tracks the progress of a started provider bootstrapping attempt.
 * Each required API call us taken into account as well as the state of tor's bootstrapping in case
 * it is used for censorship circumvention.
 */
public class ProviderSetupObservable {

    private static final String TAG = ProviderSetupObservable.class.getSimpleName();

    private int progress = 0;
    private boolean canceled = false;
    public static final int DOWNLOADED_PROVIDER_JSON = 20;
    public static final int DOWNLOADED_CA_CERT = 40;
    public static final int DOWNLOADED_EIP_SERVICE_JSON = 60;
    public static final int DOWNLOADED_GEOIP_JSON = 80;
    public static final int DOWNLOADED_VPN_CERTIFICATE = 100;

    private static ProviderSetupObservable instance;
    private final PropertyChangeSupport changeSupport;
    public static final String PROPERTY_CHANGE = "ProviderSetupObservable";
    private final HandlerInterface handler;
    private long lastUpdate = 0;
    private int resultCode = 0;
    private Bundle resultData;



    private ProviderSetupObservable() {
        handler = HandlerProvider.get();
        changeSupport = new PropertyChangeSupport(this);
        resultData = new Bundle();
    }

    public void addObserver(PropertyChangeListener propertyChangeListener) {
        changeSupport.addPropertyChangeListener(propertyChangeListener);
    }

    public void deleteObserver(PropertyChangeListener propertyChangeListener) {
        changeSupport.removePropertyChangeListener(propertyChangeListener);
    }


    public static ProviderSetupObservable getInstance() {
        if (instance == null) {
            instance = new ProviderSetupObservable();
        }
        return instance;
    }

    public static void storeLastResult(int resultCode, Bundle resultData) {
        if (getInstance().canceled) {
            return;
        }
        getInstance().resultCode = resultCode;
        getInstance().resultData = resultData;
    }

    public static void updateProgress(int progress) {
        if (getInstance().canceled) {
            return;
        }
        long now = System.currentTimeMillis();
        getInstance().handler.postDelayed(() -> {
            if (TorStatusObservable.isRunning()) {
                getInstance().progress = (TorStatusObservable.getBootstrapProgress() + progress) / 2;
            } else {
                getInstance().progress = progress;
            }

            getInstance().changeSupport.firePropertyChange(PROPERTY_CHANGE, null, getInstance());

        }, now - getInstance().lastUpdate < 500L ? 500L : 0L);
        getInstance().lastUpdate = System.currentTimeMillis() + 500;
    }

    public static void updateTorSetupProgress() {
        if (!TorStatusObservable.isRunning() || getInstance().canceled) {
            return;
        }
        long now = System.currentTimeMillis();
        getInstance().handler.postDelayed(() -> {
            getInstance().progress = (TorStatusObservable.getBootstrapProgress()) / 2;

            getInstance().changeSupport.firePropertyChange(PROPERTY_CHANGE, null, getInstance());
        }, now - getInstance().lastUpdate < 500L ? 500L : 0);
        getInstance().lastUpdate = System.currentTimeMillis() + 500;
    }

    public static int getProgress() {
        return getInstance().progress;
    }

    public static boolean isSetupRunning() {
        return getInstance().progress > 0;
    }

    public static void reset() {
        getInstance().progress = 0;
        getInstance().resultCode = 0;
        getInstance().resultData = new Bundle();
        getInstance().changeSupport.firePropertyChange(PROPERTY_CHANGE, null, getInstance());
    }

    public static void cancel() {
        getInstance().canceled = true;
        reset();
    }

    public static boolean isCanceled() {
        return getInstance().canceled;
    }

    public static void startSetup() {
        getInstance().canceled = false;
        getInstance().resultCode = 0;
        getInstance().progress = 1;
        getInstance().resultData = new Bundle();
    }

    public static int getResultCode() {
        return getInstance().resultCode;
    }

    public static Bundle getResultData() {
        return getInstance().resultData;
    }
}