summaryrefslogtreecommitdiff
path: root/app/src/main/java/se/leap/bitmaskclient/base/BitmaskTileService.java
blob: 370a7af61773d432fbac75ad43d6f956950e1589 (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
package se.leap.bitmaskclient.base;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Intent;
import android.graphics.drawable.Icon;
import android.os.Build;
import android.service.quicksettings.Tile;
import android.service.quicksettings.TileService;

import java.util.Observable;
import java.util.Observer;

import se.leap.bitmaskclient.R;
import se.leap.bitmaskclient.eip.EipCommand;
import se.leap.bitmaskclient.eip.EipStatus;
import se.leap.bitmaskclient.base.models.Provider;
import se.leap.bitmaskclient.base.models.ProviderObservable;


@TargetApi(Build.VERSION_CODES.N)
public class BitmaskTileService extends TileService implements Observer {
    
    @SuppressLint("Override")
    @TargetApi(Build.VERSION_CODES.N)
    @Override
    public void onClick() {
        super.onClick();
        Provider provider = ProviderObservable.getInstance().getCurrentProvider();
        if (provider.isConfigured()) {
            if (!isLocked()) {
                onTileTap();
            } else {
                unlockAndRun(this::onTileTap);
            }
        } else {
            Intent intent = new Intent(getApplicationContext(), StartActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }
    }

    private void onTileTap() {
        EipStatus eipStatus = EipStatus.getInstance();
        if (eipStatus.isConnecting() || eipStatus.isBlocking() || eipStatus.isConnected() || eipStatus.isReconnecting()) {
            EipCommand.stopVPN(this);
        } else {
            EipCommand.startVPN(this, false);
        }
    }


    @TargetApi(Build.VERSION_CODES.N)
    @Override
    public void onTileAdded() {
    }

    @Override
    public void onStartListening() {
        super.onStartListening();
        EipStatus.getInstance().addObserver(this);
        update(EipStatus.getInstance(), null);
    }

    @Override
    public void onStopListening() {
        super.onStopListening();
        EipStatus.getInstance().deleteObserver(this);
    }

    @Override
    public void update(Observable o, Object arg) {
        Tile t = getQsTile();
        // Tile t should never be null according to https://developer.android.com/reference/kotlin/android/service/quicksettings/TileService.
        // Hovever we've got crash reports.
        if (t == null) {
            return;
        }

        if (o instanceof EipStatus) {
            EipStatus status = (EipStatus) o;
            Icon icon;
            String title;
            if (status.isConnecting() || status.isReconnecting()) {
                icon = Icon.createWithResource(getApplicationContext(), R.drawable.vpn_connecting);
                title = getResources().getString(R.string.cancel);
                t.setState(Tile.STATE_ACTIVE);
            } else if (status.isConnected()) {
                icon = Icon.createWithResource(getApplicationContext(), R.drawable.vpn_connected);
                title = String.format(getString(R.string.qs_disconnect), getString(R.string.app_name));
                t.setState(Tile.STATE_ACTIVE);
            } else if (status.isBlocking()) {
                icon = Icon.createWithResource(getApplicationContext(), R.drawable.vpn_blocking);
                title = getString(R.string.vpn_button_turn_off_blocking);
                t.setState(Tile.STATE_ACTIVE);
            } else {
                icon = Icon.createWithResource(getApplicationContext(), R.drawable.vpn_disconnected);
                title = String.format(getString(R.string.qs_enable_vpn), getString(R.string.app_name));
                t.setState(Tile.STATE_INACTIVE);
            }


            t.setIcon(icon);
            t.setLabel(title);

            t.updateTile();
        }
    }
}