aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/export/FieldnoteExport.java
blob: 50cc620af58a4bf83e48da350554e9e9c7fa3a66 (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
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
package cgeo.geocaching.export;

import cgeo.geocaching.LogEntry;
import cgeo.geocaching.R;
import cgeo.geocaching.cgCache;
import cgeo.geocaching.cgeoapplication;
import cgeo.geocaching.activity.ActivityMixin;
import cgeo.geocaching.activity.Progress;
import cgeo.geocaching.enumerations.LogType;
import cgeo.geocaching.utils.Log;

import org.apache.commons.lang3.StringUtils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Exports offline-logs in the Groundspeak Field Note format.<br>
 * <br>
 *
 * Field Notes are simple plain text files, but poorly documented. Syntax:<br>
 * <code>GCxxxxx,yyyy-mm-ddThh:mm:ssZ,Found it,"logtext"</code>
 */
class FieldnoteExport extends AbstractExport {
    private static final File exportLocation = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/field-notes");
    private static final SimpleDateFormat fieldNoteDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");

    protected FieldnoteExport() {
        super(getString(R.string.export_fieldnotes));
    }

    /**
     * A dialog to allow the user to set options for the export.
     *
     * Currently available options are: upload field notes, only new logs since last export/upload
     */
    private class ExportOptionsDialog extends AlertDialog {
        public ExportOptionsDialog(final List<cgCache> caches, final Activity activity) {
            super(activity);

            View layout = activity.getLayoutInflater().inflate(R.layout.fieldnote_export_dialog, null);
            setView(layout);

            ((Button) layout.findViewById(R.id.export)).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                    new ExportTask(
                            caches,
                            activity,
                            ((CheckBox) findViewById(R.id.upload)).isChecked(),
                            ((CheckBox) findViewById(R.id.onlynew)).isChecked())
                            .execute((Void) null);
                }
            });
        }
    }

    @Override
    public void export(final List<cgCache> caches, final Activity activity) {
        if (null == activity) {
            // No activity given, so no user interaction possible.
            // Start export with default parameters.
            new ExportTask(caches, null, false, false).execute((Void) null);
        } else {
            // Show configuration dialog
            new ExportOptionsDialog(caches, activity).show();
        }
    }

    private class ExportTask extends AsyncTask<Void, Integer, Boolean> {
        private final List<cgCache> caches;
        private final Activity activity;
        private final boolean onlyNew;
        private final Progress progress = new Progress();
        private File exportFile;

        private static final int STATUS_UPLOAD = -1;

        /**
         * Instantiates and configurates the task for exporting field notes.
         *
         * @param caches
         *            The {@link List} of {@link cgCache} to be exported
         * @param activity
         *            optional: Show a progress bar and toasts
         * @param upload
         *            Upload the Field Note to geocaching.com
         * @param onlyNew
         *            Upload/export only new logs since last export
         */
        public ExportTask(final List<cgCache> caches, final Activity activity, final boolean upload, final boolean onlyNew) {
            this.caches = caches;
            this.activity = activity;
            this.onlyNew = onlyNew;
        }

        @Override
        protected void onPreExecute() {
            if (null != activity) {
                progress.show(activity, null, getString(R.string.export) + ": " + getName(), ProgressDialog.STYLE_HORIZONTAL, null);
                progress.setMaxProgressAndReset(caches.size());
            }
        }

        @Override
        protected Boolean doInBackground(Void... params) {
            final StringBuilder fieldNoteBuffer = new StringBuilder();

            // We need our own HashMap because LogType will give us localized and maybe
            // different strings than gc.com expects in the field note
            // We only need such logtypes that are possible to log via c:geo
            Map<LogType, String> logTypes = new HashMap<LogType, String>();
            logTypes.put(LogType.LOG_FOUND_IT, "Found it");
            logTypes.put(LogType.LOG_DIDNT_FIND_IT, "Didn't find it");
            logTypes.put(LogType.LOG_NOTE, "Write Note");
            logTypes.put(LogType.LOG_NEEDS_ARCHIVE, "Needs archived");
            logTypes.put(LogType.LOG_NEEDS_MAINTENANCE, "Needs Maintenance");
            logTypes.put(LogType.LOG_WILL_ATTEND, "Will Attend");
            logTypes.put(LogType.LOG_ATTENDED, "Attended");
            logTypes.put(LogType.LOG_WEBCAM_PHOTO_TAKEN, "Webcam Photo Taken");

            for (int i = 0; i < caches.size(); i++) {
                try {
                    final cgCache cache = caches.get(i);
                    if (cache.isLogOffline()) {
                        LogEntry log = cgeoapplication.getInstance().loadLogOffline(cache.getGeocode());
                        if (null != logTypes.get(log.type)) {
                            fieldNoteBuffer.append(cache.getGeocode())
                                    .append(',')
                                    .append(fieldNoteDateFormat.format(new Date(log.date)))
                                    .append(',')
                                    .append(logTypes.get(log.type))
                                    .append(",\"")
                                    .append(StringUtils.replaceChars(log.log, '"', '\''))
                                    .append("\"\n");
                        }
                    }
                    publishProgress(i + 1);
                } catch (Exception e) {
                    Log.e("FieldnoteExport.ExportTask generation", e);
                    return false;
                }
            }

            fieldNoteBuffer.append('\n');

            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                exportLocation.mkdirs();

                SimpleDateFormat fileNameDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
                exportFile = new File(exportLocation.toString() + '/' + fileNameDateFormat.format(new Date()) + ".txt");

                OutputStream os = null;
                Writer fw = null;
                try {
                    os = new FileOutputStream(exportFile);
                    fw = new OutputStreamWriter(os, "ISO-8859-1"); // TODO: gc.com doesn't support UTF-8
                    fw.write(fieldNoteBuffer.toString());
                } catch (IOException e) {
                    Log.e("FieldnoteExport.ExportTask export", e);
                    return false;
                } finally {
                    if (fw != null) {
                        try {
                            fw.close();
                        } catch (IOException e) {
                            Log.e("FieldnoteExport.ExportTask export", e);
                            return false;
                        }
                    }
                }
            } else {
                return false;
            }

            /*
             * if (upload) {
             * TODO Use multipart POST request for uploading
             * publishProgress(STATUS_UPLOAD);
             *
             * final Parameters uploadParams = new Parameters(
             * "__EVENTTARGET", "",
             * "__EVENTARGUMENT", "",
             * "__VIEWSTATE", "",
             * //TODO "ctl00$ContentBody$chkSuppressDate", "on",
             * "ctl00$ContentBody$FieldNoteLoader", fieldNoteBuffer.toString(),
             * "ctl00$ContentBody$btnUpload", "Upload Field Note");
             * final String uri = "http://www.geocaching.com/my/uploadfieldnotes.aspx";
             *
             * String page = Network.getResponseData(Network.postRequest(uri, uploadParams));
             * if (!Login.getLoginStatus(page)) {
             * final StatusCode loginState = Login.login();
             * if (loginState == StatusCode.NO_ERROR) {
             * page = Network.getResponseData(Network.postRequest(uri, uploadParams));
             * } else {
             * Log.e(Settings.tag, "FieldnoteExport.ExportTask upload: No login (error: " + loginState + ")");
             * return false;
             * }
             * }
             *
             * if (StringUtils.isBlank(page)) {
             * Log.e(Settings.tag, "FieldnoteExport.ExportTask upload: No data from server");
             * return false;
             * }
             * }
             */

            return true;
        }

        @Override
        protected void onPostExecute(Boolean result) {
            if (null != activity) {
                progress.dismiss();

                if (result) {
                    if (onlyNew) {
                        // update last export time in settings
                    }
                    ActivityMixin.showToast(activity, getName() + ' ' + getString(R.string.export_exportedto) + ": " + exportFile.toString());
                } else {
                    ActivityMixin.showToast(activity, getString(R.string.export_failed));
                }
            }
        }

        @Override
        protected void onProgressUpdate(Integer... status) {
            if (null != activity) {
                if (STATUS_UPLOAD == status[0]) {
                    progress.setMessage(getString(R.string.export_fieldnotes_uploading));
                } else {
                    progress.setProgress(status[0]);
                }
            }
        }
    }
}