summaryrefslogtreecommitdiff
path: root/app/src/main/java/se/leap/bitmaskclient/providersetup/ProviderSetupObservable.java
blob: 90a32feaa03d2e51628c92ebd35e2b2954821f05 (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
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.Handler;
import android.os.Looper;

import java.util.Observable;

import se.leap.bitmaskclient.tor.TorStatusObservable;

public class ProviderSetupObservable extends Observable {

    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 Handler handler = new Handler(Looper.getMainLooper());
    private long lastUpdate = 0;

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

    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().setChanged();
            getInstance().notifyObservers();
        }, 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().setChanged();
            getInstance().notifyObservers();
        }, now - getInstance().lastUpdate < 500L ? 500L : 0);
        getInstance().lastUpdate = System.currentTimeMillis() + 500;
    }

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

    public static void reset() {
        getInstance().progress = 0;
        getInstance().setChanged();
        getInstance().notifyObservers();
    }

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

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

    public static void startSetup() {
        getInstance().canceled = false;
    }
}