aboutsummaryrefslogtreecommitdiffstats
path: root/src/cgeo/geocaching/LogTemplateProvider.java
blob: 09acd0d69220904717eb8a5c56f749e13e384962 (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
package cgeo.geocaching;

import java.util.HashMap;


/**
 * provides all the available templates for logging
 *
 */
public class LogTemplateProvider {
	public static abstract class LogTemplate {
		private String template;
		private int resourceId;

		public LogTemplate(String template, int resourceId) {
			this.template = template;
			this.resourceId = resourceId;
		}

		abstract String getValue(cgBase base);

		public int getResourceId() {
			return resourceId;
		}

		public int getItemId() {
			return template.hashCode();
		}

		public String getTemplateString() {
			return template;
		}

		protected String apply(String input, cgBase base) {
			return input.replaceAll("\\[" + template + "\\]", getValue(base));
		}
	}

	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) {
					return base.formatFullDate(System.currentTimeMillis());
				}
			},
			new LogTemplate("TIME", R.string.init_signature_template_time) {

				@Override
				String getValue(final cgBase base) {
					return base.formatTime(System.currentTimeMillis());
				}
			},
			new LogTemplate("USER", R.string.init_signature_template_user) {

				@Override
				String getValue(final cgBase base) {
					return base.getUserName();
				}
			},
			new LogTemplate("NUMBER", R.string.init_signature_template_number) {

				@Override
				String getValue(final cgBase base) {
					String findCount = "";
					final HashMap<String, String> params = new HashMap<String, String>();
					final String page = base.request(false, "www.geocaching.com", "/my/", "GET", params, false, false, false).getData();
					int current = cgBase.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) {
		if (signature == null) {
			return "";
		}
		String result = signature;
		for (LogTemplate template : getTemplates()) {
			result = template.apply(result, base);
		}
		return result;
	}

}