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
|
package cgeo.geocaching;
import org.apache.commons.lang3.StringUtils;
import android.util.Log;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* provides all the available templates for logging
*
*/
public class LogTemplateProvider {
public static abstract class LogTemplate {
private String template;
private int resourceId;
protected LogTemplate(String template, int resourceId) {
this.template = template;
this.resourceId = resourceId;
}
abstract String getValue(cgBase base, boolean offline);
public int getResourceId() {
return resourceId;
}
public int getItemId() {
return template.hashCode();
}
public String getTemplateString() {
return template;
}
protected String apply(String input, cgBase base, boolean offline) {
if (input.contains("[" + template + "]")) {
return input.replaceAll("\\[" + template + "\\]", getValue(base, 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
String getValue(final cgBase base, final boolean offline) {
return base.formatFullDate(System.currentTimeMillis());
}
},
new LogTemplate("TIME", R.string.init_signature_template_time) {
@Override
String getValue(final cgBase base, final boolean offline) {
return base.formatTime(System.currentTimeMillis());
}
},
new LogTemplate("DATETIME", R.string.init_signature_template_datetime) {
@Override
String getValue(final cgBase base, final boolean offline) {
final long currentTime = System.currentTimeMillis();
return base.formatFullDate(currentTime) + " " + base.formatTime(currentTime);
}
},
new LogTemplate("USER", R.string.init_signature_template_user) {
@Override
String getValue(final cgBase base, final boolean offline) {
return base.getUserName();
}
},
new LogTemplate("NUMBER", R.string.init_signature_template_number) {
@Override
String getValue(final cgBase base, final boolean offline) {
if (offline) {
return "";
}
String findCount = "";
final String page = cgBase.getResponseData(cgBase.request("http://www.geocaching.com/email/", null, false, false, false));
int current = parseFindCount(page);
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, cgBase base, boolean offline) {
if (signature == null) {
return "";
}
String result = signature;
for (LogTemplate template : getTemplates()) {
result = template.apply(result, base, offline);
}
return result;
}
private static int parseFindCount(String page) {
if (StringUtils.isBlank(page)) {
return -1;
}
int findCount = -1;
try {
final Pattern findPattern = Pattern.compile("<strong><img.+?icon_smile.+?title=\"Caches Found\" /> ([,\\d]+)", Pattern.CASE_INSENSITIVE);
final Matcher findMatcher = findPattern.matcher(page);
if (findMatcher.find()) {
if (findMatcher.groupCount() > 0) {
String count = findMatcher.group(1);
if (count != null) {
if (count.length() == 0) {
findCount = 0;
} else {
findCount = Integer.parseInt(count.replaceAll(",", ""));
}
}
}
}
} catch (Exception e) {
Log.w(Settings.tag, "cgBase.parseFindCount: " + e.toString());
}
return findCount;
}
}
|