summaryrefslogtreecommitdiff
path: root/app/src/main/java/se/leap/bitmaskclient/base/BitmaskApp.java
blob: 3f73e49d166bd59fa94cdee46f1e70f16f35d5dc (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
/**
 * Copyright (c) 2021 LEAP Encryption Access Project and contributers
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package se.leap.bitmaskclient.base;

import android.content.Context;
import android.content.IntentFilter;
import android.content.SharedPreferences;

import androidx.appcompat.app.AppCompatDelegate;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.multidex.MultiDexApplication;

import com.squareup.leakcanary.LeakCanary;
import com.squareup.leakcanary.RefWatcher;

import se.leap.bitmaskclient.BuildConfig;
import se.leap.bitmaskclient.appUpdate.DownloadBroadcastReceiver;
import se.leap.bitmaskclient.eip.EipSetupObserver;
import se.leap.bitmaskclient.base.models.ProviderObservable;
import se.leap.bitmaskclient.tethering.TetheringStateManager;
import se.leap.bitmaskclient.base.utils.PRNGFixes;
import se.leap.bitmaskclient.tor.TorNotificationManager;
import se.leap.bitmaskclient.tor.TorStatusObservable;

import static android.content.Intent.CATEGORY_DEFAULT;
import static se.leap.bitmaskclient.base.models.Constants.BROADCAST_DOWNLOAD_SERVICE_EVENT;
import static se.leap.bitmaskclient.base.models.Constants.SHARED_PREFERENCES;
import static se.leap.bitmaskclient.appUpdate.DownloadBroadcastReceiver.ACTION_DOWNLOAD;
import static se.leap.bitmaskclient.appUpdate.DownloadServiceCommand.CHECK_VERSION_FILE;
import static se.leap.bitmaskclient.appUpdate.DownloadServiceCommand.DOWNLOAD_UPDATE;
import static se.leap.bitmaskclient.base.utils.ConfigHelper.isCalyxOSWithTetheringSupport;
import static se.leap.bitmaskclient.base.utils.PreferenceHelper.getSavedProviderFromSharedPreferences;

/**
 * Created by cyberta on 24.10.17.
 */

public class BitmaskApp extends MultiDexApplication {

    private final static String TAG = BitmaskApp.class.getSimpleName();
    private RefWatcher refWatcher;
    private ProviderObservable providerObservable;
    private DownloadBroadcastReceiver downloadBroadcastReceiver;
    private TorStatusObservable torStatusObservable;


    @Override
    public void onCreate() {
        super.onCreate();
        if (LeakCanary.isInAnalyzerProcess(this)) {
            // This process is dedicated to LeakCanary for heap analysis.
            // You should not init your app in this process.
            return;
        }
        refWatcher = LeakCanary.install(this);
        // Normal app init code...*/
        PRNGFixes.apply();
        SharedPreferences preferences = getSharedPreferences(SHARED_PREFERENCES, MODE_PRIVATE);
        providerObservable = ProviderObservable.getInstance();
        providerObservable.updateProvider(getSavedProviderFromSharedPreferences(preferences));
        torStatusObservable = TorStatusObservable.getInstance();
        EipSetupObserver.init(this, preferences);
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
        if (!isCalyxOSWithTetheringSupport(this)) {
            TetheringStateManager.getInstance().init(this);
        }
        if (BuildConfig.FLAVOR.contains("Fatweb")) {
            downloadBroadcastReceiver = new DownloadBroadcastReceiver();
            IntentFilter intentFilter = new IntentFilter(BROADCAST_DOWNLOAD_SERVICE_EVENT);
            intentFilter.addAction(ACTION_DOWNLOAD);
            intentFilter.addAction(CHECK_VERSION_FILE);
            intentFilter.addAction(DOWNLOAD_UPDATE);
            intentFilter.addCategory(CATEGORY_DEFAULT);
            LocalBroadcastManager.getInstance(this.getApplicationContext()).registerReceiver(downloadBroadcastReceiver, intentFilter);
        }
    }

    /**
     * Use this method to get a RefWatcher object that checks for memory leaks in the given context.
     * Call refWatcher.watch(this) to check if all references get garbage collected.
     * @param context
     * @return the RefWatcher object
     */
    public static RefWatcher getRefWatcher(Context context) {
        BitmaskApp application = (BitmaskApp) context.getApplicationContext();
        return application.refWatcher;
    }


}