From 90e2b66df719784a6c8f9e8673a14bb2861081fb Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Sun, 15 Dec 2019 00:35:28 +0530 Subject: 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 --- app/src/main/AndroidManifest.xml | 3 +- .../com/wireguard/android/QuickTileService.java | 23 +++++----- .../android/activity/TunnelToggleActivity.java | 51 ++++++++++++++++++++++ app/src/main/res/values/styles.xml | 13 ++++++ 4 files changed, 75 insertions(+), 15 deletions(-) create mode 100644 app/src/main/java/com/wireguard/android/activity/TunnelToggleActivity.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 67653221..21fc6eb3 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -26,11 +26,10 @@ android:theme="@style/AppTheme" tools:ignore="UnusedAttribute"> + - - 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(); + } +} diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index 2605691d..7da1aa9c 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -5,6 +5,19 @@ @color/accent + +