summaryrefslogtreecommitdiffhomepage
path: root/app/src/main/java/com/wireguard/android/QuickTileService.java
blob: 3b652a5b70987f3ea7adfa95d3cfb9c68a013823 (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
176
177
178
179
180
181
182
package com.wireguard.android;

import android.annotation.TargetApi;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.databinding.Observable;
import android.databinding.Observable.OnPropertyChangedCallback;
import android.databinding.ObservableList;
import android.databinding.ObservableList.OnListChangedCallback;
import android.graphics.drawable.Icon;
import android.os.Build;
import android.service.quicksettings.Tile;
import android.service.quicksettings.TileService;
import android.util.Log;
import android.widget.Toast;

import com.wireguard.android.Application.ApplicationComponent;
import com.wireguard.android.activity.MainActivity;
import com.wireguard.android.activity.SettingsActivity;
import com.wireguard.android.model.Tunnel;
import com.wireguard.android.model.Tunnel.State;
import com.wireguard.android.model.TunnelManager;
import com.wireguard.android.util.ObservableKeyedList;

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 implements OnSharedPreferenceChangeListener {
    private static final String TAG = QuickTileService.class.getSimpleName();
    private final OnTunnelListChangedCallback listCallback = new OnTunnelListChangedCallback();
    private final OnTunnelStateChangedCallback tunnelCallback = new OnTunnelStateChangedCallback();
    private SharedPreferences preferences;
    private Tunnel tunnel;
    private TunnelManager tunnelManager;

    @Override
    public void onClick() {
        if (tunnel != null) {
            tunnel.setState(State.TOGGLE).handle(this::onToggleFinished);
        } else {
            if (tunnelManager.getTunnels().isEmpty()) {
                // Prompt the user to create or import a tunnel configuration.
                startActivityAndCollapse(new Intent(this, MainActivity.class));
            } else {
                // Prompt the user to select a tunnel for use with the quick settings tile.
                final Intent intent = new Intent(this, SettingsActivity.class);
                intent.putExtra(SettingsActivity.KEY_SHOW_QUICK_TILE_SETTINGS, true);
                startActivityAndCollapse(intent);
            }
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        final ApplicationComponent component = Application.getComponent();
        preferences = component.getPreferences();
        tunnelManager = component.getTunnelManager();
    }

    @Override
    public void onSharedPreferenceChanged(final SharedPreferences preferences, final String key) {
        if (!TunnelManager.KEY_PRIMARY_TUNNEL.equals(key))
            return;
        updateTile();
    }

    @Override
    public void onStartListening() {
        preferences.registerOnSharedPreferenceChangeListener(this);
        tunnelManager.getTunnels().addOnListChangedCallback(listCallback);
        if (tunnel != null)
            tunnel.addOnPropertyChangedCallback(tunnelCallback);
        updateTile();
    }

    @Override
    public void onStopListening() {
        preferences.unregisterOnSharedPreferenceChangeListener(this);
        tunnelManager.getTunnels().removeOnListChangedCallback(listCallback);
        if (tunnel != null)
            tunnel.removeOnPropertyChangedCallback(tunnelCallback);
    }

    @SuppressWarnings("unused")
    private Void onToggleFinished(final State state, final Throwable throwable) {
        if (throwable == null)
            return null;
        Log.e(TAG, "Cannot toggle tunnel", throwable);
        final String message = "Cannot toggle tunnel: " + throwable.getCause().getMessage();
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
        return null;
    }

    private void updateTile() {
        // Update the tunnel.
        final String currentName = tunnel != null ? tunnel.getName() : null;
        final String newName = preferences.getString(TunnelManager.KEY_PRIMARY_TUNNEL, null);
        if (!Objects.equals(currentName, newName)) {
            final ObservableKeyedList<String, Tunnel> tunnels = tunnelManager.getTunnels();
            final Tunnel newTunnel = newName != null ? tunnels.get(newName) : null;
            if (tunnel != null)
                tunnel.removeOnPropertyChangedCallback(tunnelCallback);
            tunnel = newTunnel;
            if (tunnel != null)
                tunnel.addOnPropertyChangedCallback(tunnelCallback);
        }
        // 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;
        }
        tile.setLabel(label);
        if (tile.getState() != state) {
            // The icon must be changed every time the state changes, or the shade will not change.
            final Integer iconResource = (state == Tile.STATE_ACTIVE)
                    ? R.drawable.ic_tile : R.drawable.ic_tile_disabled;
            tile.setIcon(Icon.createWithResource(this, iconResource));
            tile.setState(state);
        }
        tile.updateTile();
    }

    private final class OnTunnelListChangedCallback
            extends OnListChangedCallback<ObservableList<Tunnel>> {
        @Override
        public void onChanged(final ObservableList<Tunnel> sender) {
            updateTile();
        }

        @Override
        public void onItemRangeChanged(final ObservableList<Tunnel> sender,
                                       final int positionStart, final int itemCount) {
            updateTile();
        }

        @Override
        public void onItemRangeInserted(final ObservableList<Tunnel> sender,
                                        final int positionStart, final int itemCount) {
            // Do nothing.
        }

        @Override
        public void onItemRangeMoved(final ObservableList<Tunnel> sender,
                                     final int fromPosition, final int toPosition,
                                     final int itemCount) {
            // Do nothing.
        }

        @Override
        public void onItemRangeRemoved(final ObservableList<Tunnel> sender,
                                       final int positionStart, final int itemCount) {
            updateTile();
        }
    }

    private final class OnTunnelStateChangedCallback 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();
        }
    }
}