2017-08-17 12:45:40 +02:00
|
|
|
package com.wireguard.android;
|
|
|
|
|
|
|
|
import android.annotation.TargetApi;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.SharedPreferences;
|
2018-01-01 09:06:37 +01:00
|
|
|
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
|
|
|
|
import android.databinding.Observable;
|
|
|
|
import android.databinding.Observable.OnPropertyChangedCallback;
|
2018-01-06 11:04:42 +01:00
|
|
|
import android.databinding.ObservableList;
|
|
|
|
import android.databinding.ObservableList.OnListChangedCallback;
|
2017-08-17 12:45:40 +02:00
|
|
|
import android.graphics.drawable.Icon;
|
|
|
|
import android.os.Build;
|
|
|
|
import android.service.quicksettings.Tile;
|
|
|
|
import android.service.quicksettings.TileService;
|
2018-01-01 09:06:37 +01:00
|
|
|
import android.util.Log;
|
|
|
|
import android.widget.Toast;
|
2017-08-17 12:45:40 +02:00
|
|
|
|
2018-01-01 09:06:37 +01:00
|
|
|
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;
|
2018-01-06 11:04:42 +01:00
|
|
|
import com.wireguard.android.util.KeyedObservableList;
|
2018-01-01 09:06:37 +01:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
2017-08-17 12:45:40 +02:00
|
|
|
|
|
|
|
@TargetApi(Build.VERSION_CODES.N)
|
2018-01-01 09:06:37 +01:00
|
|
|
public class QuickTileService extends TileService implements OnSharedPreferenceChangeListener {
|
|
|
|
private static final String TAG = QuickTileService.class.getSimpleName();
|
2018-01-06 11:04:42 +01:00
|
|
|
private final OnTunnelListChangedCallback listCallback = new OnTunnelListChangedCallback();
|
2018-01-01 09:06:37 +01:00
|
|
|
private final OnTunnelStateChangedCallback tunnelCallback = new OnTunnelStateChangedCallback();
|
2017-08-17 12:45:40 +02:00
|
|
|
private SharedPreferences preferences;
|
2018-01-01 09:06:37 +01:00
|
|
|
private Tunnel tunnel;
|
|
|
|
private TunnelManager tunnelManager;
|
2017-08-17 12:45:40 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onClick() {
|
2018-01-01 09:06:37 +01:00
|
|
|
if (tunnel != null) {
|
|
|
|
tunnel.setState(State.TOGGLE).handle(this::onToggleFinished);
|
2017-11-27 00:43:34 +01:00
|
|
|
} else {
|
2018-01-01 09:06:37 +01:00
|
|
|
if (tunnelManager.getTunnels().isEmpty()) {
|
|
|
|
// Prompt the user to create or import a tunnel configuration.
|
|
|
|
startActivityAndCollapse(new Intent(this, MainActivity.class));
|
2017-11-27 03:13:34 +01:00
|
|
|
} else {
|
2018-01-01 09:06:37 +01:00
|
|
|
// Prompt the user to select a tunnel for use with the quick settings tile.
|
2017-11-27 03:13:34 +01:00
|
|
|
final Intent intent = new Intent(this, SettingsActivity.class);
|
2018-01-01 09:06:37 +01:00
|
|
|
intent.putExtra(SettingsActivity.KEY_SHOW_QUICK_TILE_SETTINGS, true);
|
2017-11-27 03:13:34 +01:00
|
|
|
startActivityAndCollapse(intent);
|
|
|
|
}
|
2017-08-17 12:45:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate() {
|
2018-01-01 09:06:37 +01:00
|
|
|
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();
|
2017-08-17 12:45:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onStartListening() {
|
2018-01-01 09:06:37 +01:00
|
|
|
preferences.registerOnSharedPreferenceChangeListener(this);
|
2018-01-06 11:04:42 +01:00
|
|
|
tunnelManager.getTunnels().addOnListChangedCallback(listCallback);
|
2018-01-01 09:06:37 +01:00
|
|
|
if (tunnel != null)
|
|
|
|
tunnel.addOnPropertyChangedCallback(tunnelCallback);
|
|
|
|
updateTile();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onStopListening() {
|
|
|
|
preferences.unregisterOnSharedPreferenceChangeListener(this);
|
2018-01-06 11:04:42 +01:00
|
|
|
tunnelManager.getTunnels().removeOnListChangedCallback(listCallback);
|
2018-01-01 09:06:37 +01:00
|
|
|
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)) {
|
2018-01-06 11:04:42 +01:00
|
|
|
final KeyedObservableList<String, Tunnel> tunnels = tunnelManager.getTunnels();
|
2018-01-01 09:06:37 +01:00
|
|
|
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;
|
2017-08-17 12:45:40 +02:00
|
|
|
final Tile tile = getQsTile();
|
2018-01-01 09:06:37 +01:00
|
|
|
if (tunnel != null) {
|
|
|
|
label = tunnel.getName();
|
|
|
|
state = tunnel.getState() == Tunnel.State.UP ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE;
|
2017-08-17 12:45:40 +02:00
|
|
|
} else {
|
2018-01-01 09:06:37 +01:00
|
|
|
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);
|
2017-08-17 12:45:40 +02:00
|
|
|
}
|
|
|
|
tile.updateTile();
|
|
|
|
}
|
|
|
|
|
2018-01-06 11:04:42 +01:00
|
|
|
private final class OnTunnelListChangedCallback
|
|
|
|
extends OnListChangedCallback<ObservableList<Tunnel>> {
|
2017-08-17 12:45:40 +02:00
|
|
|
@Override
|
2018-01-06 11:04:42 +01:00
|
|
|
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) {
|
2018-01-01 09:06:37 +01:00
|
|
|
updateTile();
|
2017-08-17 12:45:40 +02:00
|
|
|
}
|
2018-01-01 09:06:37 +01:00
|
|
|
}
|
2017-08-17 12:45:40 +02:00
|
|
|
|
2018-01-01 09:06:37 +01:00
|
|
|
private final class OnTunnelStateChangedCallback extends OnPropertyChangedCallback {
|
2017-08-17 12:45:40 +02:00
|
|
|
@Override
|
2018-01-01 09:06:37 +01:00
|
|
|
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();
|
2017-08-17 12:45:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|