aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/go4cache/Go4CacheUser.java
blob: 08fd02e8da016eed5bbfb17eb62b5919ad533d53 (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
package cgeo.geocaching.go4cache;

import cgeo.geocaching.R;
import cgeo.geocaching.cgeoapplication;
import cgeo.geocaching.geopoint.Geopoint;

import android.content.res.Resources;

import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Go4CacheUser {
    private static final Pattern patternGeocode = Pattern.compile("^(GC[A-Z0-9]+)(\\: ?(.+))?$", Pattern.CASE_INSENSITIVE);

    private final Date date;
    private final String username;
    private final Geopoint coords;
    private final String action;
    private final String client;

    private String actionForDisplay;
    private String geocode;
    private Resources res;

    public Go4CacheUser(final String username, final Geopoint coords, final Date date, final String action, final String client) {
        this.username = username;
        this.coords = coords;
        this.date = new Date(date.getTime());
        this.action = action;
        this.client = client;
    }

    public Date getDate() {
        return date;
    }

    public String getUsername() {
        return username;
    }

    public Geopoint getCoords() {
        return coords;
    }

    public int getIconId() {
        if (client == null) {
            return -1;
        }
        if (client.equalsIgnoreCase("c:geo")) {
            return R.drawable.client_cgeo;
        }
        if (client.equalsIgnoreCase("preCaching")) {
            return R.drawable.client_precaching;
        }
        if (client.equalsIgnoreCase("Handy Geocaching")) {
            return R.drawable.client_handygeocaching;
        }
        return -1;
    }

    private void getGeocodeAndAction() {
        final Matcher matcherGeocode = patternGeocode.matcher(action.trim());
        res = cgeoapplication.getInstance().getResources();

        geocode = "";
        if (0 == action.length() || action.equalsIgnoreCase("pending")) {
            actionForDisplay = res.getString(R.string.go4cache_looking_around);
        } else if (action.equalsIgnoreCase("tweeting")) {
            actionForDisplay = res.getString(R.string.go4cache_tweeting);
        } else if (matcherGeocode.find()) {
            if (null != matcherGeocode.group(1)) {
                geocode = matcherGeocode.group(1).trim().toUpperCase();
            }
            if (null != matcherGeocode.group(3)) {
                actionForDisplay = res.getString(R.string.go4cache_heading_to) + " " + geocode + " (" + matcherGeocode.group(3).trim() + ")";
            } else {
                actionForDisplay = res.getString(R.string.go4cache_heading_to) + " " + geocode;
            }
        } else {
            actionForDisplay = action;
        }
    }

    public String getAction() {
        if (null == actionForDisplay) {
            getGeocodeAndAction();
        }
        return actionForDisplay + getTime() + ".";
    }

    private String getTime() {
        int minutes = (int) ((System.currentTimeMillis() - date.getTime()) / 60000);
        if (minutes < 0) {
            minutes = 0;
        }
        return " (" + res.getQuantityString(R.plurals.go4cache_time_minutes, minutes, minutes) + ")";
    }

    public String getGeocode() {
        if (null == geocode) {
            getGeocodeAndAction();
        }
        return geocode;
    }
}