diff options
author | Harsh Shandilya <me@msfjarvis.dev> | 2019-12-15 00:35:28 +0530 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2019-12-20 11:26:33 +0100 |
commit | 4d3043c0419a4c44c1004d13a8510557c61b1878 (patch) | |
tree | c7d7edc17812a7d6b55663ef683ab2af774c0fd4 /app/src/main/java | |
parent | 8261a184721c58366bc9f0a00487d585ff45fb27 (diff) |
Introduce TunnelToggleActivity
On Android 10, apps cannot start services when they're in the
background. This means that starting VpnService from within
QuickTileService when the app is not active ends badly. To mitigate this
situation, we introduce a proxy activity of sorts that will handle
starting VpnService for us. The activity is completely transparent and
invisible, and does only four things:
- Toggle the tunnel state
- Request the Tile bound by QuickTileService to refresh its state
- Handle any error that might have been thrown during toggle
- Call finishAffinity() and go away
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
Diffstat (limited to 'app/src/main/java')
-rw-r--r-- | app/src/main/java/com/wireguard/android/QuickTileService.java | 23 | ||||
-rw-r--r-- | app/src/main/java/com/wireguard/android/activity/TunnelToggleActivity.java | 51 |
2 files changed, 61 insertions, 13 deletions
diff --git a/app/src/main/java/com/wireguard/android/QuickTileService.java b/app/src/main/java/com/wireguard/android/QuickTileService.java index d296a88c..8909beec 100644 --- a/app/src/main/java/com/wireguard/android/QuickTileService.java +++ b/app/src/main/java/com/wireguard/android/QuickTileService.java @@ -18,12 +18,11 @@ import android.service.quicksettings.TileService; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; import android.util.Log; -import android.widget.Toast; import com.wireguard.android.activity.MainActivity; +import com.wireguard.android.activity.TunnelToggleActivity; 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; @@ -66,7 +65,15 @@ public class QuickTileService extends TileService { tile.setIcon(tile.getIcon() == iconOn ? iconOff : iconOn); tile.updateTile(); } - tunnel.setState(State.TOGGLE).whenComplete(this::onToggleFinished); + tunnel.setState(State.TOGGLE).whenComplete((v, t) -> { + if (t == null) { + updateTile(); + } else { + final Intent toggleIntent = new Intent(this, TunnelToggleActivity.class); + toggleIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + startActivity(toggleIntent); + } + }); }); } else { final Intent intent = new Intent(this, MainActivity.class); @@ -112,16 +119,6 @@ public class QuickTileService extends TileService { 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(); diff --git a/app/src/main/java/com/wireguard/android/activity/TunnelToggleActivity.java b/app/src/main/java/com/wireguard/android/activity/TunnelToggleActivity.java new file mode 100644 index 00000000..69c995fe --- /dev/null +++ b/app/src/main/java/com/wireguard/android/activity/TunnelToggleActivity.java @@ -0,0 +1,51 @@ +/* + * Copyright © 2017-2019 WireGuard LLC. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +package com.wireguard.android.activity; + +import androidx.annotation.Nullable; +import androidx.annotation.RequiresApi; +import androidx.appcompat.app.AppCompatActivity; + +import android.content.ComponentName; +import android.os.Bundle; +import android.os.Build; +import android.service.quicksettings.TileService; +import android.util.Log; +import android.widget.Toast; + +import com.wireguard.android.Application; +import com.wireguard.android.QuickTileService; +import com.wireguard.android.R; +import com.wireguard.android.model.Tunnel; +import com.wireguard.android.model.Tunnel.State; +import com.wireguard.android.util.ErrorMessages; + +@RequiresApi(Build.VERSION_CODES.N) +public class TunnelToggleActivity extends AppCompatActivity { + private static final String TAG = "WireGuard/" + TunnelToggleActivity.class.getSimpleName(); + + @Override + protected void onCreate(@Nullable final Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + final Tunnel tunnel = Application.getTunnelManager().getLastUsedTunnel(); + if (tunnel == null) + return; + tunnel.setState(State.TOGGLE).whenComplete((v, t) -> { + TileService.requestListeningState(this, new ComponentName(this, QuickTileService.class)); + onToggleFinished(t); + finishAffinity(); + }); + } + + private void onToggleFinished(@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(); + } +} |