wireguard-android/ui/src/main/java/com/wireguard/android/activity/TunnelToggleActivity.java
Jason A. Donenfeld 40ebf8006e global: optimize imports
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-03-09 12:21:35 -06:00

54 lines
1.8 KiB
Java

/*
* Copyright © 2017-2019 WireGuard LLC. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package com.wireguard.android.activity;
import android.content.ComponentName;
import android.os.Build;
import android.os.Bundle;
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.backend.Tunnel.State;
import com.wireguard.android.model.ObservableTunnel;
import com.wireguard.android.util.ErrorMessages;
import com.wireguard.util.NonNullForAll;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
@RequiresApi(Build.VERSION_CODES.N)
@NonNullForAll
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 ObservableTunnel 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();
}
}