summaryrefslogtreecommitdiffhomepage
path: root/app/src/main/java/com/wireguard/android/Application.java
blob: b8562606522dd93255dbd6124c5b04fd2b9dc40c (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
 * 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.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Looper;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatDelegate;

import com.wireguard.android.backend.Backend;
import com.wireguard.android.backend.GoBackend;
import com.wireguard.android.backend.WgQuickBackend;
import com.wireguard.android.configStore.ConfigStore;
import com.wireguard.android.configStore.FileConfigStore;
import com.wireguard.android.model.TunnelManager;
import com.wireguard.android.util.AsyncWorker;
import com.wireguard.android.util.RootShell;
import com.wireguard.android.util.ToolsInstaller;
import com.wireguard.android.util.Topic;

import java.io.File;
import java.util.concurrent.Executor;

import javax.inject.Qualifier;
import javax.inject.Scope;

import dagger.Component;
import dagger.Module;
import dagger.Provides;

/**
 * Base context for the WireGuard Android application. This class (instantiated once during the
 * application lifecycle) maintains and mediates access to the global state of the application.
 */

public class Application extends android.app.Application {
    private static ApplicationComponent component;

    public static ApplicationComponent getComponent() {
        if (component == null)
            throw new IllegalStateException("Application instance not yet created");
        return component;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        component = DaggerApplication_ApplicationComponent.builder()
                .applicationModule(new ApplicationModule(this))
                .build();
        component.getTunnelManager().onCreate();
    }

    @ApplicationScope
    @Component(modules = ApplicationModule.class)
    public interface ApplicationComponent {
        AsyncWorker getAsyncWorker();

        Class getBackendType();

        ToolsInstaller getToolsInstaller();

        TunnelManager getTunnelManager();

        Topic getThemeChangeTopic();
    }

    @Qualifier
    public @interface ApplicationContext {
    }

    @Qualifier
    public @interface ApplicationHandler {
    }

    @Scope
    public @interface ApplicationScope {
    }

    @Module
    public static final class ApplicationModule {
        private final Context context;

        private ApplicationModule(final Application application) {
            context = application.getApplicationContext();

            AppCompatDelegate.setDefaultNightMode(
                    getPreferences(context).getBoolean("dark_theme", false) ?
                          AppCompatDelegate.MODE_NIGHT_YES : AppCompatDelegate.MODE_NIGHT_NO);
        }

        @ApplicationScope
        @Provides
        public static Backend getBackend(@ApplicationContext final Context context,
                                         final RootShell rootShell,
                                         final ToolsInstaller toolsInstaller) {
            if (new File("/sys/module/wireguard").exists())
                return new WgQuickBackend(context, rootShell, toolsInstaller);
            else
                return new GoBackend(context);
        }

        @ApplicationScope
        @Provides
        public static Class getBackendType(final Backend backend) {
            return backend.getClass();
        }

        @ApplicationScope
        @Provides
        public static ConfigStore getConfigStore(@ApplicationContext final Context context) {
            return new FileConfigStore(context);
        }

        @ApplicationScope
        @Provides
        public static Topic getThemeChangeTopic() {
            return new Topic();
        }


        @ApplicationScope
        @Provides
        public static Executor getExecutor() {
            return AsyncTask.SERIAL_EXECUTOR;
        }

        @ApplicationHandler
        @ApplicationScope
        @Provides
        public static Handler getHandler() {
            return new Handler(Looper.getMainLooper());
        }

        @ApplicationScope
        @Provides
        public static SharedPreferences getPreferences(@ApplicationContext final Context context) {
            return PreferenceManager.getDefaultSharedPreferences(context);
        }

        @ApplicationContext
        @ApplicationScope
        @Provides
        public Context getContext() {
            return context;
        }
    }
}