aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/ConversationsPlusApplication.java
blob: 2def66f72c1713836b79c1f90b57889757dc5f08 (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
package de.thedevstack.conversationsplus;

import android.app.Application;
import android.content.Context;
import android.content.pm.PackageManager;

import java.io.File;

import eu.siacs.conversations.R;

/**
 * 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;

    /**
     * Initializes the application and saves its instance.
     */
    public void onCreate(){
        super.onCreate();
        ConversationsPlusApplication.instance = this;
    }

    /**
     * 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();
    }

    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";
        }
    }

    public static String getName() {
        return ConversationsPlusApplication.getAppContext().getString(R.string.app_name);
    }

    public static String getNameAndVersion() {
        return getName() + " " + getVersion();
    }
}