aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/ConversationsPlusApplication.java
blob: d4e3ebc272f9d4afdf052e136eaa94c46258608e (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
156
157
158
159
160
161
package de.thedevstack.conversationsplus;

import android.app.Application;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.PowerManager;
import android.preference.PreferenceManager;

import java.io.File;
import java.security.SecureRandom;

import de.duenndns.ssl.MemorizingTrustManager;
import de.thedevstack.conversationsplus.http.HttpClient;
import de.thedevstack.conversationsplus.http.HttpConnectionManager;
import de.thedevstack.conversationsplus.persistance.FileBackend;
import de.thedevstack.conversationsplus.services.filetransfer.FileTransferManager;
import de.thedevstack.conversationsplus.services.filetransfer.http.upload.HttpUploadFileTransferService;
import de.thedevstack.conversationsplus.services.filetransfer.jingle.JingleFileTransferService;
import de.thedevstack.conversationsplus.utils.ImageUtil;
import de.thedevstack.conversationsplus.utils.PRNGFixes;
import de.thedevstack.conversationsplus.utils.SerialSingleThreadExecutor;
import okhttp3.OkHttpClient;

/**
 * This class is used to provide static access to the applicationcontext.
 */
public class ConversationsPlusApplication extends Application {
    /**
     * Application instance for static access
     */
    private static ConversationsPlusApplication instance;

    private final SerialSingleThreadExecutor mFileAddingExecutor = new SerialSingleThreadExecutor();
    private final SerialSingleThreadExecutor mDatabaseExecutor = new SerialSingleThreadExecutor();

    private MemorizingTrustManager memorizingTrustManager;
    private SecureRandom secureRandom;

    /**
     * Initializes the application and saves its instance.
     */
    public void onCreate(){
        super.onCreate();
        ConversationsPlusApplication.instance = this;
        ConversationsPlusPreferences.init(PreferenceManager.getDefaultSharedPreferences(getAppContext()));
        this.initializeSecurity();
        ImageUtil.initBitmapCache();
        FileBackend.init();
        FileTransferManager.init(new HttpUploadFileTransferService(), new JingleFileTransferService());
        HttpConnectionManager.init();
        HttpClient.init();
    }

    /**
     * Initializes security features.
     */
    private void initializeSecurity() {
        PRNGFixes.apply();
        this.secureRandom = new SecureRandom();
        ConversationsPlusApplication.updateMemorizingTrustmanager();
    }

    /**
     * Returns the instance of the application
     * @return this application instance
     */
    public static ConversationsPlusApplication getInstance() {
        return ConversationsPlusApplication.instance;
    }

    public static void executeFileAdding(Runnable r) {
        getInstance().mFileAddingExecutor.execute(r);
    }

    public static void executeDatabaseOperation(Runnable r) {
        getInstance().mDatabaseExecutor.execute(r);
    }

    /**
     * Returns the application's context.
     * @return Context the application's context
     */
    public static Context getAppContext() {
        return ConversationsPlusApplication.instance.getApplicationContext();
    }

    /**
     * Returns the application's private data directory.
     * @return File the application's private data dir
     */
    public static File getPrivateFilesDir() {
        return ConversationsPlusApplication.instance.getFilesDir();
    }

    /**
     * Returns the version of the application.
     * @see android.content.pm.PackageInfo#versionName
     * @return a string representation of the version stored in packageInfo
     */
    public static String getVersion() {
        final String packageName = ConversationsPlusApplication.getAppContext().getPackageName();
        if (packageName != null) {
            try {
                return ConversationsPlusApplication.getAppContext().getPackageManager().getPackageInfo(packageName, 0).versionName;
            } catch (final PackageManager.NameNotFoundException e) {
                return "unknown";
            }
        } else {
            return "unknown";
        }
    }

    /**
     * Returns the application's name.
     * @return the name as it is defined in R.string.app_name
     */
    public static String getName() {
        return ConversationsPlusApplication.getAppContext().getString(R.string.app_name);
    }

    /**
     * Returns the name and the version of this application.
     * @see #getName() and #getVersion
     * @return a concatination of name and version with a whitespace in between
     */
    public static String getNameAndVersion() {
        return getName() + " " + getVersion();
    }

    public static PowerManager.WakeLock createPartialWakeLock(String name) {
        PowerManager powerManager = (PowerManager) getAppContext().getSystemService(Context.POWER_SERVICE);
        return powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, name);
    }

    public static MemorizingTrustManager getMemorizingTrustManager() {
        return getInstance().memorizingTrustManager;
    }

    public void setMemorizingTrustManager(MemorizingTrustManager trustManager) {
        this.memorizingTrustManager = trustManager;
    }

    public static void updateMemorizingTrustmanager() {
        final MemorizingTrustManager tm;
        if (ConversationsPlusPreferences.dontTrustSystemCAs()) {
            tm = new MemorizingTrustManager(getAppContext(), null);
        } else {
            tm = new MemorizingTrustManager(getAppContext());
        }
        getInstance().setMemorizingTrustManager(tm);
    }

    private static void initHttpClient() {
        OkHttpClient client = new OkHttpClient.Builder()
                .build();
    }

    public static SecureRandom getSecureRandom() {
        return getInstance().secureRandom;
    }
}