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
|
package cgeo.geocaching.utils;
import cgeo.geocaching.R;
import cgeo.geocaching.Settings;
import cgeo.geocaching.connector.gc.GCConstants;
import cgeo.geocaching.connector.gc.Login;
import cgeo.geocaching.network.Network;
import cgeo.geocaching.ui.Formatter;
import org.apache.commons.lang3.StringUtils;
/**
* provides all the available templates for logging
*
*/
public class LogTemplateProvider {
public static abstract class LogTemplate {
private final String template;
private final int resourceId;
protected LogTemplate(String template, int resourceId) {
this.template = template;
this.resourceId = resourceId;
}
abstract public String getValue(boolean offline);
public int getResourceId() {
return resourceId;
}
public int getItemId() {
return template.hashCode();
}
public String getTemplateString() {
return template;
}
protected String apply(String input, boolean offline) {
if (input.contains("[" + template + "]")) {
return StringUtils.replace(input, "[" + template + "]", getValue(offline));
}
return input;
}
}
private static LogTemplate[] templates;
public static LogTemplate[] getTemplates() {
if (templates == null) {
templates = new LogTemplate[] {
new LogTemplate("DATE", R.string.init_signature_template_date) {
@Override
public String getValue(final boolean offline) {
return Formatter.formatFullDate(System.currentTimeMillis());
}
},
new LogTemplate("TIME", R.string.init_signature_template_time) {
@Override
public String getValue(final boolean offline) {
return Formatter.formatTime(System.currentTimeMillis());
}
},
new LogTemplate("DATETIME", R.string.init_signature_template_datetime) {
@Override
public String getValue(final boolean offline) {
final long currentTime = System.currentTimeMillis();
return Formatter.formatFullDate(currentTime) + " " + Formatter.formatTime(currentTime);
}
},
new LogTemplate("USER", R.string.init_signature_template_user) {
@Override
public String getValue(final boolean offline) {
return Settings.getUsername();
}
},
new LogTemplate("NUMBER", R.string.init_signature_template_number) {
@Override
public String getValue(final boolean offline) {
int current = Login.getActualCachesFound();
if (current == 0) {
if (offline) {
return "";
}
final String page = Network.getResponseData(Network.getRequest("http://www.geocaching.com/email/"));
current = parseFindCount(page);
}
String findCount = "";
if (current >= 0) {
findCount = String.valueOf(current + 1);
}
return findCount;
}
}
};
}
return templates;
}
public static LogTemplate getTemplate(int itemId) {
for (LogTemplate template : getTemplates()) {
if (template.getItemId() == itemId) {
return template;
}
}
return null;
}
public static String applyTemplates(String signature, boolean offline) {
if (signature == null) {
return "";
}
String result = signature;
for (LogTemplate template : getTemplates()) {
result = template.apply(result, offline);
}
return result;
}
private static int parseFindCount(String page) {
if (StringUtils.isBlank(page)) {
return -1;
}
try {
return Integer.parseInt(BaseUtils.getMatch(page, GCConstants.PATTERN_CACHES_FOUND, true, "-1").replaceAll("[,.]", ""));
} catch (NumberFormatException e) {
Log.e("parseFindCount", e);
return -1;
}
}
}
|