2018-05-02 17:29:58 +02:00
|
|
|
/*
|
2019-01-02 01:57:15 +01:00
|
|
|
* Copyright © 2017-2019 WireGuard LLC. All Rights Reserved.
|
2018-07-06 04:09:48 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2018-05-02 17:29:58 +02:00
|
|
|
*/
|
|
|
|
|
2018-01-01 09:06:37 +01:00
|
|
|
package com.wireguard.android;
|
|
|
|
|
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.util.Log;
|
|
|
|
|
2018-05-27 18:57:52 +02:00
|
|
|
import com.wireguard.android.backend.WgQuickBackend;
|
2018-01-01 09:06:37 +01:00
|
|
|
import com.wireguard.android.model.TunnelManager;
|
|
|
|
import com.wireguard.android.util.ExceptionLoggers;
|
2020-03-09 19:21:35 +01:00
|
|
|
import com.wireguard.util.NonNullForAll;
|
2018-01-01 09:06:37 +01:00
|
|
|
|
2020-03-09 19:01:52 +01:00
|
|
|
@NonNullForAll
|
2018-01-01 09:06:37 +01:00
|
|
|
public class BootShutdownReceiver extends BroadcastReceiver {
|
2018-01-08 20:29:51 +01:00
|
|
|
private static final String TAG = "WireGuard/" + BootShutdownReceiver.class.getSimpleName();
|
2018-01-01 09:06:37 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onReceive(final Context context, final Intent intent) {
|
2018-07-26 03:17:22 +02:00
|
|
|
Application.getBackendAsync().thenAccept(backend -> {
|
2018-07-08 23:59:25 +02:00
|
|
|
if (!(backend instanceof WgQuickBackend))
|
2018-06-14 04:59:24 +02:00
|
|
|
return;
|
|
|
|
final String action = intent.getAction();
|
|
|
|
if (action == null)
|
|
|
|
return;
|
|
|
|
final TunnelManager tunnelManager = Application.getTunnelManager();
|
|
|
|
if (Intent.ACTION_BOOT_COMPLETED.equals(action)) {
|
|
|
|
Log.i(TAG, "Broadcast receiver restoring state (boot)");
|
|
|
|
tunnelManager.restoreState(false).whenComplete(ExceptionLoggers.D);
|
|
|
|
} else if (Intent.ACTION_SHUTDOWN.equals(action)) {
|
|
|
|
Log.i(TAG, "Broadcast receiver saving state (shutdown)");
|
|
|
|
tunnelManager.saveState();
|
|
|
|
}
|
|
|
|
});
|
2018-01-01 09:06:37 +01:00
|
|
|
}
|
|
|
|
}
|