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>
This commit is contained in:
committed by
Jason A. Donenfeld
parent
8261a18472
commit
4d3043c041
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user