summaryrefslogtreecommitdiffhomepage
path: root/app/src/main/java/com/wireguard/android/BootShutdownReceiver.java
blob: 675bbb42a35096d2b4ad06c730713e18444fa053 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
 * Copyright © 2018 Samuel Holland <samuel@sholland.org>
 * Copyright © 2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

package com.wireguard.android;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.wireguard.android.backend.WgQuickBackend;
import com.wireguard.android.model.TunnelManager;
import com.wireguard.android.util.ExceptionLoggers;

public class BootShutdownReceiver extends BroadcastReceiver {
    private static final String TAG = "WireGuard/" + BootShutdownReceiver.class.getSimpleName();

    @Override
    public void onReceive(final Context context, final Intent intent) {
        if (Application.getComponent().getBackendType() != WgQuickBackend.class) {
            return;
        }
        final String action = intent.getAction();
        if (action == null)
            return;
        final TunnelManager tunnelManager = Application.getComponent().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();
        }
    }
}