aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/utils/Version.java
blob: 52ebf7ad8fddedeacae69c64e4bc85f885f3ee7e (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
package cgeo.geocaching.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;

public class Version {

    private Version() {
        // Do not instantiate
    }

    private static PackageInfo getPackageInfo(final Context context) {
        try {
            return context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
        } catch (final NameNotFoundException e) {
            Log.e("Version.getPackageInfo: unable to get package information", e);
            return null;
        }
    }

    /**
     * Get the current package version name if available.
     *
     * @param context the context to use
     * @return the current package version name, or "" if unavailable
     */
    public static String getVersionName(final Context context) {
        final PackageInfo packageInfo = getPackageInfo(context);
        return packageInfo != null ? packageInfo.versionName : "";
    }

    /**
     * Get the current package version code if available.
     *
     * @param context the context to use
     * @return the current package version code, or -1 if unavailable
     */
    public static int getVersionCode(final Context context) {
        final PackageInfo packageInfo = getPackageInfo(context);
        return packageInfo != null ? packageInfo.versionCode : -1;
    }

}