aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/apps/AbstractApp.java
blob: 1161a0564e742baf797f4af06a9571e8cee39c6c (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
package cgeo.geocaching.apps;

import cgeo.geocaching.cgeo;
import cgeo.geocaching.cgeoapplication;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;

public abstract class AbstractApp implements App {

    protected String packageName;

    private String intent;
    private String name;

    protected AbstractApp(final String name, final String intent,
            final String packageName) {
        this.name = name;
        this.intent = intent;
        this.packageName = packageName;
    }

    protected AbstractApp(final String name, final String intent) {
        this(name, intent, null);
    }

    protected Intent getLaunchIntent(Context context) {
        if (packageName == null) {
            return null;
        }
        final PackageManager packageManager = context.getPackageManager();
        try {
            // This can throw an exception where the exception type is only defined on API Level > 3
            // therefore surround with try-catch
            final Intent intent = packageManager.getLaunchIntentForPackage(packageName);
            return intent;
        } catch (Exception e) {
            return null;
        }
    }

    public boolean isInstalled(final Context context) {
        if (getLaunchIntent(context) != null) {
            return true;
        }
        return cgeo.isIntentAvailable(context, intent);
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public int getId() {
        return getName().hashCode();
    }

    protected static String getString(int ressourceId) {
        return cgeoapplication.getInstance().getString(ressourceId);
    }
}