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
154
155
156
|
package cgeo.geocaching.connector.gc;
import cgeo.geocaching.DataStore;
import cgeo.geocaching.Geocache;
import cgeo.geocaching.LogCacheActivity;
import cgeo.geocaching.R;
import cgeo.geocaching.TrackableLog;
import cgeo.geocaching.activity.ActivityMixin;
import cgeo.geocaching.connector.AbstractLoggingManager;
import cgeo.geocaching.connector.ImageResult;
import cgeo.geocaching.connector.LogResult;
import cgeo.geocaching.enumerations.LogType;
import cgeo.geocaching.enumerations.StatusCode;
import cgeo.geocaching.loaders.UrlLoader;
import cgeo.geocaching.network.Parameters;
import cgeo.geocaching.settings.Settings;
import cgeo.geocaching.utils.Log;
import cgeo.geocaching.utils.TextUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;
class GCLoggingManager extends AbstractLoggingManager implements LoaderManager.LoaderCallbacks<String> {
private final LogCacheActivity activity;
private final Geocache cache;
private String[] viewstates;
@NonNull private List<TrackableLog> trackables = Collections.emptyList();
private List<LogType> possibleLogTypes;
private boolean hasLoaderError = true;
GCLoggingManager(final LogCacheActivity activity, final Geocache cache) {
this.activity = activity;
this.cache = cache;
}
@Nullable
@Override
public Loader<String> onCreateLoader(final int arg0, final Bundle arg1) {
if (!Settings.isLogin()) { // allow offline logging
ActivityMixin.showToast(activity, activity.getResources().getString(R.string.err_login));
return null;
}
return new UrlLoader(activity.getBaseContext(), "http://www.geocaching.com/seek/log.aspx", new Parameters("ID", cache.getCacheId()));
}
@Override
public void onLoadFinished(final Loader<String> arg0, final String page) {
if (page == null) {
hasLoaderError = true;
} else {
viewstates = GCLogin.getViewstates(page);
trackables = GCParser.parseTrackableLog(page);
possibleLogTypes = GCParser.parseTypes(page);
if (StringUtils.isBlank(cache.getGuid())) {
// Acquire the cache GUID from the log page. This will not only complete the information in the database,
// but also allow the user to post a rating using GCVote since it requires the GUID to do so.
final String guid = TextUtils.getMatch(page, GCConstants.PATTERN_LOG_GUID, null);
if (StringUtils.isNotBlank(guid)) {
cache.setGuid(guid);
DataStore.saveChangedCache(cache);
} else {
Log.w("Could not acquire GUID from log page for " + cache.getGeocode());
}
}
hasLoaderError = possibleLogTypes.isEmpty();
}
activity.onLoadFinished();
}
@Override
public void onLoaderReset(final Loader<String> arg0) {
// nothing to do
}
@Override
public void init() {
activity.getSupportLoaderManager().initLoader(0, null, this);
}
@Override
@NonNull
public LogResult postLog(@NonNull final LogType logType, @NonNull final Calendar date, @NonNull final String log, @Nullable final String logPassword, @NonNull final List<TrackableLog> trackableLogs) {
try {
final ImmutablePair<StatusCode, String> postResult = GCParser.postLog(cache.getGeocode(), cache.getCacheId(), viewstates, logType,
date.get(Calendar.YEAR), (date.get(Calendar.MONTH) + 1), date.get(Calendar.DATE),
log, trackableLogs);
if (postResult.left == StatusCode.NO_ERROR) {
if (logType == LogType.TEMP_DISABLE_LISTING) {
cache.setDisabled(true);
} else if (logType == LogType.ENABLE_LISTING) {
cache.setDisabled(false);
}
}
return new LogResult(postResult.left, postResult.right);
} catch (final Exception e) {
Log.e("GCLoggingManager.postLog", e);
}
return new LogResult(StatusCode.LOG_POST_ERROR, "");
}
@Override
@NonNull
public ImageResult postLogImage(final String logId, final String imageCaption, final String imageDescription, final Uri imageUri) {
if (StringUtils.isNotBlank(imageUri.getPath())) {
final ImmutablePair<StatusCode, String> imageResult = GCParser.uploadLogImage(logId, imageCaption, imageDescription, imageUri);
return new ImageResult(imageResult.left, imageResult.right);
}
return new ImageResult(StatusCode.LOGIMAGE_POST_ERROR, "");
}
@Override
public boolean hasLoaderError() {
return hasLoaderError;
}
@Override
@NonNull
public List<TrackableLog> getTrackables() {
if (hasLoaderError) {
return Collections.emptyList();
}
return trackables;
}
@Override
@NonNull
public List<LogType> getPossibleLogTypes() {
if (hasLoaderError) {
return Collections.emptyList();
}
return possibleLogTypes;
}
}
|