aboutsummaryrefslogtreecommitdiffstats
path: root/cgeo-calendar/src/cgeo/calendar/CalendarEntry.java
blob: 8308b7b8cf56de247691c786aa505d7a63268ec3 (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
package cgeo.calendar;

import cgeo.geocaching.utils.Log;

import org.apache.commons.lang3.CharEncoding;
import org.eclipse.jdt.annotation.NonNull;

import android.net.Uri;
import android.text.Html;
import android.text.Spanned;
import android.text.style.ImageSpan;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Calendar;
import java.util.Date;

class CalendarEntry {

    @NonNull
    private final String shortDesc;
    @NonNull
    private final String hiddenDate;
    @NonNull
    private final String url;
    @NonNull
    private final String personalNote;
    @NonNull
    private final String name;
    @NonNull
    private final String coords;
    private int startTimeMinutes = -1;
    @NonNull
    private final Uri uri;

    public CalendarEntry(@NonNull final Uri uri) {
        this.uri = uri;
        this.shortDesc = getParameter(ICalendar.PARAM_SHORT_DESC);
        this.hiddenDate = getParameter(ICalendar.PARAM_HIDDEN_DATE);
        this.url = getParameter(ICalendar.PARAM_URL);
        this.personalNote = getParameter(ICalendar.PARAM_NOTE);
        this.name = getParameter(ICalendar.PARAM_NAME);
        coords = getParameter(ICalendar.PARAM_COORDS);
        final String startTime = getParameter(ICalendar.PARAM_START_TIME_MINUTES);
        if (startTime.length() > 0) {
            try {
                this.startTimeMinutes = Integer.parseInt(startTime);
            } catch (final NumberFormatException e) {
                Log.e("CalendarEntry creation", e);
            }
        }
    }

    @NonNull
    private String getParameter(@NonNull final String paramKey) {
        try {
            final String param = uri.getQueryParameter(paramKey);
            if (param == null) {
                return "";
            }
            return URLDecoder.decode(param, CharEncoding.UTF_8).trim();
        } catch (final UnsupportedEncodingException e) {
            Log.e("CalendarEntry.getParameter", e);
        }
        return "";
    }

    public boolean isValid() {
        return getName().length() > 0 && getHiddenDate().length() > 0;
    }

    @NonNull
    public String getHiddenDate() {
        return hiddenDate;
    }

    @NonNull
    public String getUrl() {
        return url;
    }

    @NonNull
    public String getPersonalNote() {
        return personalNote;
    }

    @NonNull
    public String getShortDesc() {
        return shortDesc;
    }

    /**
     * @return <code>Date</code> based on hidden date. Time is set to 00:00:00.
     */
    @NonNull
    protected Date parseDate() {
        try {
            final Calendar cal = Calendar.getInstance();
            cal.setTimeInMillis(Long.parseLong(getHiddenDate()));
            cal.set(Calendar.HOUR_OF_DAY, 0);
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);

            return cal.getTime();
        } catch (final NumberFormatException e) {
            // cannot happen normally, but static code analysis does not know
            throw new IllegalStateException("hidden date must be a valid date for cache calendar entries");
        }
    }

    /**
     * @return description string with images removed and personal note included
     */
    @NonNull
    protected String parseDescription() {
        final StringBuilder description = new StringBuilder();
        description.append(getUrl());
        if (getShortDesc().length() > 0) {
            // remove images in short description
            final Spanned spanned = Html.fromHtml(getShortDesc(), null, null);
            String text = spanned.toString();
            final ImageSpan[] spans = spanned.getSpans(0, spanned.length(), ImageSpan.class);
            for (int i = spans.length - 1; i >= 0; i--) {
                text = text.substring(0, spanned.getSpanStart(spans[i])) + text.substring(spanned.getSpanEnd(spans[i]));
            }
            if (text.length() > 0) {
                description.append("\n\n");
                description.append(text);
            }
        }

        if (getPersonalNote().length() > 0) {
            description.append("\n\n").append(Html.fromHtml(getPersonalNote()).toString());
        }

        return description.toString();
    }

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

    public int getStartTimeMinutes() {
        return startTimeMinutes;
    }

    @NonNull
    public String getCoords() {
        return coords;
    }

}