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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
|
package cgeo.geocaching;
import cgeo.geocaching.activity.AbstractActivity;
import cgeo.geocaching.connector.gc.GCParser;
import cgeo.geocaching.connector.gc.Login;
import cgeo.geocaching.enumerations.LogType;
import cgeo.geocaching.enumerations.StatusCode;
import cgeo.geocaching.network.Network;
import cgeo.geocaching.network.Parameters;
import cgeo.geocaching.twitter.Twitter;
import cgeo.geocaching.ui.DateDialog;
import cgeo.geocaching.ui.Formatter;
import cgeo.geocaching.utils.Log;
import cgeo.geocaching.utils.LogTemplateProvider;
import cgeo.geocaching.utils.LogTemplateProvider.LogTemplate;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
public class LogTrackableActivity extends AbstractActivity implements DateDialog.DateDialogParent {
private List<LogType> possibleLogTypes = new ArrayList<LogType>();
private ProgressDialog waitDialog = null;
private String guid = null;
private String geocode = null;
private String[] viewstates = null;
private boolean gettingViewstate = true;
private Calendar date = Calendar.getInstance();
private LogType typeSelected = LogType.getById(Settings.getTrackableAction());
private int attempts = 0;
private CheckBox tweetCheck = null;
private LinearLayout tweetBox = null;
private static final int MENU_SIGNATURE = 1;
private Handler showProgressHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
showProgress(true);
}
};
private Handler loadDataHandler = new Handler() {
@Override
public void handleMessage(final Message msg) {
if (!possibleLogTypes.contains(typeSelected)) {
setType(possibleLogTypes.get(0));
showToast(res.getString(R.string.info_log_type_changed));
}
if (Login.isEmpty(viewstates)) {
if (attempts < 2) {
showToast(res.getString(R.string.err_log_load_data_again));
new LoadDataThread().start();
} else {
showToast(res.getString(R.string.err_log_load_data));
showProgress(false);
}
return;
}
gettingViewstate = false; // we're done, user can post log
final Button buttonPost = (Button) findViewById(R.id.post);
buttonPost.setEnabled(true);
buttonPost.setOnClickListener(new PostListener());
showProgress(false);
}
};
private final Handler postLogHandler = new Handler() {
@Override
public void handleMessage(final Message msg) {
if (waitDialog != null) {
waitDialog.dismiss();
}
final StatusCode error = (StatusCode) msg.obj;
if (error == StatusCode.NO_ERROR) {
showToast(res.getString(R.string.info_log_posted));
finish();
} else {
showToast(error.getErrorString(res));
}
}
};
public LogTrackableActivity() {
super("c:geo-log-trackable");
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme();
setContentView(R.layout.touch);
setTitle(res.getString(R.string.trackable_touch));
// get parameters
final Bundle extras = getIntent().getExtras();
if (extras != null) {
geocode = extras.getString("geocode");
guid = extras.getString("guid");
if (StringUtils.isNotBlank(extras.getString("trackingcode"))) {
((EditText) findViewById(R.id.tracking)).setText(extras.getString("trackingcode"));
}
}
final cgTrackable trackable = app.getTrackableByGeocode(geocode);
if (StringUtils.isNotBlank(trackable.getName())) {
setTitle(res.getString(R.string.trackable_touch) + ": " + trackable.getName());
} else {
setTitle(res.getString(R.string.trackable_touch) + ": " + trackable.getGeocode().toUpperCase());
}
if (guid == null) {
showToast(res.getString(R.string.err_tb_forgot_saw));
finish();
return;
}
init();
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
init();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
final SubMenu subMenu = menu.addSubMenu(0, 0, 0, res.getString(R.string.log_add)).setIcon(R.drawable.ic_menu_add);
for (LogTemplate template : LogTemplateProvider.getTemplates()) {
subMenu.add(0, template.getItemId(), 0, template.getResourceId());
}
subMenu.add(0, MENU_SIGNATURE, 0, res.getString(R.string.init_signature));
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.findItem(MENU_SIGNATURE).setVisible(Settings.getSignature() != null);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
final int id = item.getItemId();
if (id == MENU_SIGNATURE) {
insertIntoLog(LogTemplateProvider.applyTemplates(Settings.getSignature(), false), true);
return true;
}
final LogTemplate template = LogTemplateProvider.getTemplate(id);
if (template != null) {
insertIntoLog(template.getValue(false), true);
return true;
}
return false;
}
private void insertIntoLog(String newText, final boolean moveCursor) {
final EditText log = (EditText) findViewById(R.id.log);
insertAtPosition(log, newText, moveCursor);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo info) {
super.onCreateContextMenu(menu, view, info);
final int viewId = view.getId();
if (viewId == R.id.type) {
for (final LogType typeOne : possibleLogTypes) {
menu.add(viewId, typeOne.id, 0, typeOne.getL10n());
}
}
}
@Override
public boolean onContextItemSelected(MenuItem item) {
final int group = item.getGroupId();
final int id = item.getItemId();
if (group == R.id.type) {
setType(LogType.getById(id));
return true;
}
return false;
}
public void init() {
final Button typeButton = (Button) findViewById(R.id.type);
registerForContextMenu(typeButton);
typeButton.setText(typeSelected.getL10n());
typeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openContextMenu(view);
}
});
final Button dateButton = (Button) findViewById(R.id.date);
dateButton.setOnClickListener(new DateListener());
setDate(date);
if (tweetBox == null) {
tweetBox = (LinearLayout) findViewById(R.id.tweet_box);
}
if (tweetCheck == null) {
tweetCheck = (CheckBox) findViewById(R.id.tweet);
}
tweetCheck.setChecked(true);
if (CollectionUtils.isEmpty(possibleLogTypes)) {
possibleLogTypes = cgTrackable.getPossibleLogTypes();
}
final Button buttonPost = (Button) findViewById(R.id.post);
if (Login.isEmpty(viewstates)) {
buttonPost.setEnabled(false);
buttonPost.setOnTouchListener(null);
buttonPost.setOnClickListener(null);
new LoadDataThread().start();
} else {
buttonPost.setEnabled(true);
buttonPost.setOnClickListener(new PostListener());
}
disableSuggestions((EditText) findViewById(R.id.tracking));
}
@Override
public void setDate(Calendar dateIn) {
date = dateIn;
final Button dateButton = (Button) findViewById(R.id.date);
dateButton.setText(Formatter.formatShortDateVerbally(date.getTime().getTime()));
}
public void setType(LogType type) {
final Button typeButton = (Button) findViewById(R.id.type);
typeSelected = type;
typeButton.setText(typeSelected.getL10n());
if (Settings.isUseTwitter()) {
tweetBox.setVisibility(View.VISIBLE);
} else {
tweetBox.setVisibility(View.GONE);
}
}
private class DateListener implements View.OnClickListener {
@Override
public void onClick(View arg0) {
final Dialog dateDialog = new DateDialog(LogTrackableActivity.this, LogTrackableActivity.this, date);
dateDialog.setCancelable(true);
dateDialog.show();
}
}
private class PostListener implements View.OnClickListener {
@Override
public void onClick(View arg0) {
if (!gettingViewstate) {
waitDialog = ProgressDialog.show(LogTrackableActivity.this, null, res.getString(R.string.log_saving), true);
waitDialog.setCancelable(true);
Settings.setTrackableAction(typeSelected.id);
final String tracking = ((EditText) findViewById(R.id.tracking)).getText().toString();
final String log = ((EditText) findViewById(R.id.log)).getText().toString();
new PostLogThread(postLogHandler, tracking, log).start();
} else {
showToast(res.getString(R.string.err_log_load_data_still));
}
}
}
private class LoadDataThread extends Thread {
public LoadDataThread() {
super("Load data for logging trackable");
if (guid == null) {
showToast(res.getString(R.string.err_tb_forgot_saw));
finish();
}
}
@Override
public void run() {
final Parameters params = new Parameters();
showProgressHandler.sendEmptyMessage(0);
gettingViewstate = true;
attempts++;
try {
if (StringUtils.isNotBlank(guid)) {
params.put("wid", guid);
} else {
loadDataHandler.sendEmptyMessage(0);
return;
}
final String page = Network.getResponseData(Network.getRequest("http://www.geocaching.com/track/log.aspx", params));
viewstates = Login.getViewstates(page);
final List<LogType> typesPre = GCParser.parseTypes(page);
if (CollectionUtils.isNotEmpty(typesPre)) {
possibleLogTypes.clear();
possibleLogTypes.addAll(typesPre);
}
} catch (Exception e) {
Log.e("LogTrackableActivity.LoadDataThread.run: " + e.toString());
}
loadDataHandler.sendEmptyMessage(0);
}
}
private class PostLogThread extends Thread {
final private Handler handler;
final private String tracking;
final private String log;
public PostLogThread(final Handler handlerIn, final String trackingIn, final String logIn) {
super("Post trackable log");
handler = handlerIn;
tracking = trackingIn;
log = logIn;
}
@Override
public void run() {
final StatusCode status = postLogFn(tracking, log);
handler.sendMessage(handler.obtainMessage(0, status));
}
}
public StatusCode postLogFn(String tracking, String log) {
try {
final StatusCode status = GCParser.postLogTrackable(guid, tracking, viewstates, typeSelected, date.get(Calendar.YEAR), (date.get(Calendar.MONTH) + 1), date.get(Calendar.DATE), log);
if (status == StatusCode.NO_ERROR && Settings.isUseTwitter() &&
Settings.isTwitterLoginValid() &&
tweetCheck.isChecked() && tweetBox.getVisibility() == View.VISIBLE) {
Twitter.postTweetTrackable(geocode);
}
return status;
} catch (Exception e) {
Log.e("LogTrackableActivity.postLogFn: " + e.toString());
}
return StatusCode.LOG_POST_ERROR;
}
public static void startActivity(final Context context, final cgTrackable trackable) {
final Intent logTouchIntent = new Intent(context, LogTrackableActivity.class);
logTouchIntent.putExtra("geocode", trackable.getGeocode().toUpperCase());
logTouchIntent.putExtra("guid", trackable.getGuid());
logTouchIntent.putExtra("trackingcode", trackable.getTrackingcode());
context.startActivity(logTouchIntent);
}
}
|