summaryrefslogtreecommitdiffhomepage
path: root/app/src/main/java/com/wireguard/android/QuickTileService.java
blob: 198684c17ecdf6bfca6ff84e92f475759511e2dd (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
/*
 * Copyright © 2017-2018 WireGuard LLC. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

package com.wireguard.android;

import android.annotation.TargetApi;
import android.content.Intent;
import android.databinding.Observable;
import android.databinding.Observable.OnPropertyChangedCallback;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.Icon;
import android.os.Build;
import android.os.IBinder;
import android.service.quicksettings.Tile;
import android.service.quicksettings.TileService;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;

import com.wireguard.android.activity.MainActivity;
import com.wireguard.android.model.Tunnel;
import com.wireguard.android.model.Tunnel.State;
import com.wireguard.android.util.ErrorMessages;
import com.wireguard.android.widget.SlashDrawable;

import java.util.Objects;

/**
 * Service that maintains the application's custom Quick Settings tile. This service is bound by the
 * system framework as necessary to update the appearance of the tile in the system UI, and to
 * forward click events to the application.
 */

@TargetApi(Build.VERSION_CODES.N)
public class QuickTileService extends TileService {
    private static final String TAG = "WireGuard/" + QuickTileService.class.getSimpleName();

    private final OnStateChangedCallback onStateChangedCallback = new OnStateChangedCallback();
    private final OnTunnelChangedCallback onTunnelChangedCallback = new OnTunnelChangedCallback();
    @Nullable private Icon iconOff;
    @Nullable private Icon iconOn;
    @Nullable private Tunnel tunnel;

    /* This works around an annoying unsolved frameworks bug some people are hitting. */
    @Override
    @Nullable
    public IBinder onBind(final Intent intent) {
        IBinder ret = null;
        try {
            ret = super.onBind(intent);
        } catch (final Exception e) {
            Log.d(TAG, "Failed to bind to TileService", e);
        }
        return ret;
    }

    @Override
    public void onClick() {
        if (tunnel != null) {
            final Tile tile = getQsTile();
            if (tile != null) {
                tile.setIcon(tile.getIcon() == iconOn ? iconOff : iconOn);
                tile.updateTile();
            }
            tunnel.setState(State.TOGGLE).whenComplete(this::onToggleFinished);
        } else {
            final Intent intent = new Intent(this, MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivityAndCollapse(intent);
        }
    }

    @Override
    public void onCreate() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            iconOff = iconOn = Icon.createWithResource(this, R.drawable.ic_tile);
            return;
        }
        final SlashDrawable icon = new SlashDrawable(getResources().getDrawable(R.drawable.ic_tile, Application.get().getTheme()));
        icon.setAnimationEnabled(false); /* Unfortunately we can't have animations, since Icons are marshaled. */
        icon.setSlashed(false);
        Bitmap b = Bitmap.createBitmap(icon.getIntrinsicWidth(), icon.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(b);
        icon.setBounds(0, 0, c.getWidth(), c.getHeight());
        icon.draw(c);
        iconOn = Icon.createWithBitmap(b);
        icon.setSlashed(true);
        b = Bitmap.createBitmap(icon.getIntrinsicWidth(), icon.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        c = new Canvas(b);
        icon.setBounds(0, 0, c.getWidth(), c.getHeight());
        icon.draw(c);
        iconOff = Icon.createWithBitmap(b);
    }

    @Override
    public void onStartListening() {
        Application.getTunnelManager().addOnPropertyChangedCallback(onTunnelChangedCallback);
        if (tunnel != null)
            tunnel.addOnPropertyChangedCallback(onStateChangedCallback);
        updateTile();
    }

    @Override
    public void onStopListening() {
        if (tunnel != null)
            tunnel.removeOnPropertyChangedCallback(onStateChangedCallback);
        Application.getTunnelManager().removeOnPropertyChangedCallback(onTunnelChangedCallback);
    }

    private void onToggleFinished(@SuppressWarnings("unused") final State state,
                                  @Nullable final Throwable throwable) {
        if (throwable == null)
            return;
        final String error = ErrorMessages.get(throwable);
        final String message = getString(R.string.toggle_error, error);
        Log.e(TAG, message, throwable);
        Toast.makeText(this, message, Toast.LENGTH_LONG).show();
    }

    private void updateTile() {
        // Update the tunnel.
        final Tunnel newTunnel = Application.getTunnelManager().getLastUsedTunnel();
        if (newTunnel != tunnel) {
            if (tunnel != null)
                tunnel.removeOnPropertyChangedCallback(onStateChangedCallback);
            tunnel = newTunnel;
            if (tunnel != null)
                tunnel.addOnPropertyChangedCallback(onStateChangedCallback);
        }
        // Update the tile contents.
        final String label;
        final int state;
        final Tile tile = getQsTile();
        if (tunnel != null) {
            label = tunnel.getName();
            state = tunnel.getState() == Tunnel.State.UP ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE;
        } else {
            label = getString(R.string.app_name);
            state = Tile.STATE_INACTIVE;
        }
        if (tile == null)
            return;
        tile.setLabel(label);
        if (tile.getState() != state) {
            tile.setIcon(state == Tile.STATE_ACTIVE ? iconOn : iconOff);
            tile.setState(state);
        }
        tile.updateTile();
    }

    private final class OnStateChangedCallback extends OnPropertyChangedCallback {
        @Override
        public void onPropertyChanged(final Observable sender, final int propertyId) {
            if (!Objects.equals(sender, tunnel)) {
                sender.removeOnPropertyChangedCallback(this);
                return;
            }
            if (propertyId != 0 && propertyId != BR.state)
                return;
            updateTile();
        }
    }

    private final class OnTunnelChangedCallback extends OnPropertyChangedCallback {
        @Override
        public void onPropertyChanged(final Observable sender, final int propertyId) {
            if (propertyId != 0 && propertyId != BR.lastUsedTunnel)
                return;
            updateTile();
        }
    }
}