aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/export/FieldnoteExport.java
diff options
context:
space:
mode:
Diffstat (limited to 'main/src/cgeo/geocaching/export/FieldnoteExport.java')
-rw-r--r--main/src/cgeo/geocaching/export/FieldnoteExport.java141
1 files changed, 66 insertions, 75 deletions
diff --git a/main/src/cgeo/geocaching/export/FieldnoteExport.java b/main/src/cgeo/geocaching/export/FieldnoteExport.java
index 028ad54..5e1805a 100644
--- a/main/src/cgeo/geocaching/export/FieldnoteExport.java
+++ b/main/src/cgeo/geocaching/export/FieldnoteExport.java
@@ -1,23 +1,28 @@
package cgeo.geocaching.export;
+import cgeo.geocaching.Geocache;
import cgeo.geocaching.LogEntry;
import cgeo.geocaching.R;
-import cgeo.geocaching.cgCache;
-import cgeo.geocaching.cgeoapplication;
+import cgeo.geocaching.cgData;
import cgeo.geocaching.activity.ActivityMixin;
import cgeo.geocaching.activity.Progress;
import cgeo.geocaching.connector.gc.Login;
import cgeo.geocaching.enumerations.StatusCode;
import cgeo.geocaching.network.Network;
import cgeo.geocaching.network.Parameters;
+import cgeo.geocaching.utils.IOUtils;
import cgeo.geocaching.utils.Log;
+import org.apache.commons.lang3.CharEncoding;
import org.apache.commons.lang3.StringUtils;
import android.app.Activity;
import android.app.AlertDialog;
+import android.app.Dialog;
+import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Environment;
+import android.view.ContextThemeWrapper;
import android.view.View;
import android.widget.CheckBox;
@@ -51,57 +56,54 @@ class FieldnoteExport extends AbstractExport {
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);
-
- final CheckBox uploadOption = (CheckBox) layout.findViewById(R.id.upload);
- final CheckBox onlyNewOption = (CheckBox) layout.findViewById(R.id.onlynew);
-
- uploadOption.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- onlyNewOption.setEnabled(uploadOption.isChecked());
- }
- });
-
- layout.findViewById(R.id.export).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- dismiss();
- new ExportTask(
- caches,
- activity,
- uploadOption.isChecked(),
- onlyNewOption.isChecked())
- .execute((Void) null);
- }
- });
- }
- }
-
@Override
- public void export(final List<cgCache> caches, final Activity activity) {
+ public void export(final List<Geocache> 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();
+ getExportOptionsDialog(caches, activity).show();
}
}
+ private Dialog getExportOptionsDialog(final List<Geocache> caches, final Activity activity) {
+ AlertDialog.Builder builder = new AlertDialog.Builder(activity);
+
+ // AlertDialog has always dark style, so we have to apply it as well always
+ View layout = View.inflate(new ContextThemeWrapper(activity, R.style.dark), R.layout.fieldnote_export_dialog, null);
+ builder.setView(layout);
+
+ final CheckBox uploadOption = (CheckBox) layout.findViewById(R.id.upload);
+ final CheckBox onlyNewOption = (CheckBox) layout.findViewById(R.id.onlynew);
+
+ uploadOption.setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ onlyNewOption.setEnabled(uploadOption.isChecked());
+ }
+ });
+
+ builder.setPositiveButton(R.string.export, new DialogInterface.OnClickListener() {
+
+ @Override
+ public void onClick(DialogInterface dialog, int which) {
+ dialog.dismiss();
+ new ExportTask(
+ caches,
+ activity,
+ uploadOption.isChecked(),
+ onlyNewOption.isChecked())
+ .execute((Void) null);
+ }
+ });
+
+ return builder.create();
+ }
+
private class ExportTask extends AsyncTask<Void, Integer, Boolean> {
- private final List<cgCache> caches;
+ private final List<Geocache> caches;
private final Activity activity;
private final boolean upload;
private final boolean onlyNew;
@@ -114,7 +116,7 @@ class FieldnoteExport extends AbstractExport {
* Instantiates and configurates the task for exporting field notes.
*
* @param caches
- * The {@link List} of {@link cgCache} to be exported
+ * The {@link List} of {@link cgeo.geocaching.Geocache} to be exported
* @param activity
* optional: Show a progress bar and toasts
* @param upload
@@ -122,7 +124,7 @@ class FieldnoteExport extends AbstractExport {
* @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) {
+ public ExportTask(final List<Geocache> caches, final Activity activity, final boolean upload, final boolean onlyNew) {
this.caches = caches;
this.activity = activity;
this.upload = upload;
@@ -139,13 +141,11 @@ class FieldnoteExport extends AbstractExport {
@Override
protected Boolean doInBackground(Void... params) {
final StringBuilder fieldNoteBuffer = new StringBuilder();
- final cgeoapplication app = cgeoapplication.getInstance();
-
try {
int i = 0;
- for (cgCache cache : caches) {
+ for (Geocache cache : caches) {
if (cache.isLogOffline()) {
- appendFieldNote(fieldNoteBuffer, cache, app.loadLogOffline(cache.getGeocode()));
+ appendFieldNote(fieldNoteBuffer, cache, cgData.loadLogOffline(cache.getGeocode()));
publishProgress(++i);
}
}
@@ -156,40 +156,30 @@ class FieldnoteExport extends AbstractExport {
fieldNoteBuffer.append('\n');
- if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
- exportLocation.mkdirs();
+ if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
+ return false;
+ }
- SimpleDateFormat fileNameDateFormat = new SimpleDateFormat("yyyyMMddHHmmss", Locale.US);
- exportFile = new File(exportLocation.toString() + '/' + fileNameDateFormat.format(new Date()) + ".txt");
+ exportLocation.mkdirs();
- OutputStream os;
- Writer fw = null;
- try {
- os = new FileOutputStream(exportFile);
- fw = new OutputStreamWriter(os, "UTF-16");
- 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 {
+ SimpleDateFormat fileNameDateFormat = new SimpleDateFormat("yyyyMMddHHmmss", Locale.US);
+ exportFile = new File(exportLocation.toString() + '/' + fileNameDateFormat.format(new Date()) + ".txt");
+
+ Writer fw = null;
+ try {
+ OutputStream os = new FileOutputStream(exportFile);
+ fw = new OutputStreamWriter(os, CharEncoding.UTF_16);
+ fw.write(fieldNoteBuffer.toString());
+ } catch (IOException e) {
+ Log.e("FieldnoteExport.ExportTask export", e);
return false;
+ } finally {
+ IOUtils.closeQuietly(fw);
}
if (upload) {
publishProgress(STATUS_UPLOAD);
- final String uri = "http://www.geocaching.com/my/uploadfieldnotes.aspx";
-
if (!Login.isActualLoginStatus()) {
// no need to upload (possibly large file) if we're not logged in
final StatusCode loginState = Login.login();
@@ -198,6 +188,7 @@ class FieldnoteExport extends AbstractExport {
}
}
+ final String uri = "http://www.geocaching.com/my/uploadfieldnotes.aspx";
String page = Network.getResponseData(Network.getRequest(uri));
if (!Login.getLoginStatus(page)) {
@@ -268,7 +259,7 @@ class FieldnoteExport extends AbstractExport {
}
}
- static void appendFieldNote(final StringBuilder fieldNoteBuffer, final cgCache cache, final LogEntry log) {
+ static void appendFieldNote(final StringBuilder fieldNoteBuffer, final Geocache cache, final LogEntry log) {
fieldNoteBuffer.append(cache.getGeocode())
.append(',')
.append(fieldNoteDateFormat.format(new Date(log.date)))