aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo
diff options
context:
space:
mode:
authorrsudev <rasch@munin-soft.de>2012-08-05 09:00:01 -0700
committerrsudev <rasch@munin-soft.de>2012-08-05 09:00:01 -0700
commitc51d3941e104179ab0f126dbac07a55222ecc2fc (patch)
tree8a5cd542c2f5fe0679575a4536867193c4c6d5a3 /main/src/cgeo
parent25b4d61e8b2cd5f13909d6a71e92efe6bb4d773a (diff)
parent67df433eafe003e226bda0d4a8c410a1170be5ad (diff)
downloadcgeo-c51d3941e104179ab0f126dbac07a55222ecc2fc.zip
cgeo-c51d3941e104179ab0f126dbac07a55222ecc2fc.tar.gz
cgeo-c51d3941e104179ab0f126dbac07a55222ecc2fc.tar.bz2
Merge pull request #1901 from marco-jacob/issue#1794retry
fixes #1794 - added gpx export dir setting with SimpleDirChooser
Diffstat (limited to 'main/src/cgeo')
-rw-r--r--main/src/cgeo/geocaching/Settings.java22
-rw-r--r--main/src/cgeo/geocaching/SettingsActivity.java28
-rw-r--r--main/src/cgeo/geocaching/export/AbstractExport.java13
-rw-r--r--main/src/cgeo/geocaching/export/GpxExport.java8
-rw-r--r--main/src/cgeo/geocaching/files/SimpleDirChooser.java238
5 files changed, 303 insertions, 6 deletions
diff --git a/main/src/cgeo/geocaching/Settings.java b/main/src/cgeo/geocaching/Settings.java
index bbcb80b..ee201e2 100644
--- a/main/src/cgeo/geocaching/Settings.java
+++ b/main/src/cgeo/geocaching/Settings.java
@@ -95,6 +95,7 @@ public final class Settings {
private static final String KEY_DB_ON_SDCARD = "dbonsdcard";
private static final String KEY_LAST_TRACKABLE_ACTION = "trackableaction";
private static final String KEY_SHARE_AFTER_EXPORT = "shareafterexport";
+ private static final String KEY_GPX_EXPORT_DIR = "gpxExportDir";
private final static int unitsMetric = 1;
@@ -1166,6 +1167,23 @@ public final class Settings {
});
}
+ public static String getGpxExportDir() {
+ return sharedPrefs.getString(KEY_GPX_EXPORT_DIR, "/sdcard/gpx");
+ }
+
+ public static void setGpxExportDir(final String gpxExportDir) {
+ editSharedSettings(new PrefRunnable() {
+ @Override
+ public void edit(Editor edit) {
+ edit.putString(KEY_GPX_EXPORT_DIR, gpxExportDir);
+ }
+ });
+ }
+
+ public static boolean getShareAfterExport() {
+ return sharedPrefs.getBoolean(KEY_SHARE_AFTER_EXPORT, true);
+ }
+
public static void setShareAfterExport(final boolean shareAfterExport) {
editSharedSettings(new PrefRunnable() {
@Override
@@ -1189,10 +1207,6 @@ public final class Settings {
});
}
- public static boolean getShareAfterExport() {
- return sharedPrefs.getBoolean(KEY_SHARE_AFTER_EXPORT, true);
- }
-
public static String getPreferencesName() {
// there is currently no Android API to get the file name of the shared preferences
return cgeoapplication.getInstance().getPackageName() + "_preferences";
diff --git a/main/src/cgeo/geocaching/SettingsActivity.java b/main/src/cgeo/geocaching/SettingsActivity.java
index d70b300..8d802bf 100644
--- a/main/src/cgeo/geocaching/SettingsActivity.java
+++ b/main/src/cgeo/geocaching/SettingsActivity.java
@@ -7,6 +7,7 @@ import cgeo.geocaching.compatibility.Compatibility;
import cgeo.geocaching.connector.gc.Login;
import cgeo.geocaching.enumerations.CacheType;
import cgeo.geocaching.enumerations.StatusCode;
+import cgeo.geocaching.files.SimpleDirChooser;
import cgeo.geocaching.maps.MapProviderFactory;
import cgeo.geocaching.maps.interfaces.MapSource;
import cgeo.geocaching.network.Cookies;
@@ -55,6 +56,8 @@ import java.util.List;
public class SettingsActivity extends AbstractActivity {
private final static int SELECT_MAPFILE_REQUEST = 1;
+ private final static int SELECT_GPXDIR_REQUEST = 2;
+
private ProgressDialog loginDialog = null;
private ProgressDialog webDialog = null;
@@ -577,6 +580,21 @@ public class SettingsActivity extends AbstractActivity {
}
});
+ // GPX Export directory
+ final EditText gpxExportDir = (EditText) findViewById(R.id.gpx_exportdir);
+ gpxExportDir.setText(Settings.getGpxExportDir());
+ Button selectGpxExportDir = (Button) findViewById(R.id.select_gpx_exportdir);
+ selectGpxExportDir.setOnClickListener(new View.OnClickListener() {
+
+ @Override
+ public void onClick(View v) {
+ Intent dirChooser = new Intent(SettingsActivity.this, SimpleDirChooser.class);
+ dirChooser.putExtra(SimpleDirChooser.START_DIR, Settings.getGpxExportDir());
+ startActivityForResult(dirChooser, SELECT_GPXDIR_REQUEST);
+ }
+ });
+
+ // Display trail on map
final CheckBox trailButton = (CheckBox) findViewById(R.id.trail);
trailButton.setChecked(Settings.isMapTrail());
trailButton.setOnClickListener(new View.OnClickListener() {
@@ -918,6 +936,16 @@ public class SettingsActivity extends AbstractActivity {
}
initMapfileEdittext(true);
}
+ if (requestCode == SELECT_GPXDIR_REQUEST) {
+ if (resultCode == RESULT_OK) {
+ if (data.hasExtra("chosenDir")) {
+ Settings.setGpxExportDir(data.getStringExtra("chosenDir"));
+ }
+ }
+ EditText gpxExportDir = (EditText) findViewById(R.id.gpx_exportdir);
+ gpxExportDir.setText(Settings.getGpxExportDir());
+ gpxExportDir.requestFocus();
+ }
}
public static void startActivity(Context fromActivity) {
diff --git a/main/src/cgeo/geocaching/export/AbstractExport.java b/main/src/cgeo/geocaching/export/AbstractExport.java
index 0923cef..72ea544 100644
--- a/main/src/cgeo/geocaching/export/AbstractExport.java
+++ b/main/src/cgeo/geocaching/export/AbstractExport.java
@@ -25,6 +25,19 @@ abstract class AbstractExport implements Export {
return cgeoapplication.getInstance().getString(resourceId);
}
+ /**
+ * Generates a localized string from a resource id.
+ *
+ * @param resourceId
+ * the resource id of the string
+ * @param params
+ * The parameter
+ * @return localized string
+ */
+ protected static String getString(int resourceId, Object... params) {
+ return cgeoapplication.getInstance().getString(resourceId, params);
+ }
+
@Override
public String toString() {
// used in the array adapter of the dialog showing the exports
diff --git a/main/src/cgeo/geocaching/export/GpxExport.java b/main/src/cgeo/geocaching/export/GpxExport.java
index 9bc3963..8af85a8 100644
--- a/main/src/cgeo/geocaching/export/GpxExport.java
+++ b/main/src/cgeo/geocaching/export/GpxExport.java
@@ -26,6 +26,7 @@ import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
+import android.widget.TextView;
import java.io.BufferedWriter;
import java.io.File;
@@ -38,7 +39,6 @@ import java.util.Date;
import java.util.List;
class GpxExport extends AbstractExport {
- private static final File exportLocation = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/gpx");
private static final SimpleDateFormat dateFormatZ = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
protected GpxExport() {
@@ -70,6 +70,9 @@ class GpxExport extends AbstractExport {
View layout = activity.getLayoutInflater().inflate(R.layout.gpx_export_dialog, null);
setView(layout);
+ final TextView text = (TextView) layout.findViewById(R.id.info);
+ text.setText(getString(R.string.export_gpx_info, Settings.getGpxExportDir()));
+
final CheckBox shareOption = (CheckBox) layout.findViewById(R.id.share);
shareOption.setChecked(Settings.getShareAfterExport());
@@ -127,10 +130,11 @@ class GpxExport extends AbstractExport {
}
try {
+ final File exportLocation = new File(Settings.getGpxExportDir());
exportLocation.mkdirs();
final SimpleDateFormat fileNameDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
- exportFile = new File(exportLocation.toString() + File.separatorChar + "export_" + fileNameDateFormat.format(new Date()) + ".gpx");
+ exportFile = new File(Settings.getGpxExportDir() + File.separatorChar + "export_" + fileNameDateFormat.format(new Date()) + ".gpx");
gpx = new BufferedWriter(new FileWriter(exportFile));
diff --git a/main/src/cgeo/geocaching/files/SimpleDirChooser.java b/main/src/cgeo/geocaching/files/SimpleDirChooser.java
new file mode 100644
index 0000000..57cbc0c
--- /dev/null
+++ b/main/src/cgeo/geocaching/files/SimpleDirChooser.java
@@ -0,0 +1,238 @@
+/**
+ *
+ */
+package cgeo.geocaching.files;
+
+import cgeo.geocaching.R;
+import cgeo.geocaching.activity.ActivityMixin;
+
+import android.app.ListActivity;
+import android.content.Context;
+import android.content.Intent;
+import android.os.Bundle;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.View.OnClickListener;
+import android.view.ViewGroup;
+import android.widget.ArrayAdapter;
+import android.widget.Button;
+import android.widget.CheckBox;
+import android.widget.EditText;
+import android.widget.ListView;
+import android.widget.TextView;
+
+import java.io.File;
+import java.io.FilenameFilter;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * Dialog for choosing a file or directory.
+ */
+public class SimpleDirChooser extends ListActivity {
+ public static final String START_DIR = "start_dir";
+ private static final String PARENT_DIR = ".. ";
+ private File currentDir;
+ private FileArrayAdapter adapter;
+ private CheckBox lastBoxChecked = null;
+ private Button okButton = null;
+ private String checkedText = null;
+
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ final Bundle extras = getIntent().getExtras();
+ String startDir = extras.getString(START_DIR);
+ if (startDir == null) {
+ startDir = "/sdcard";
+ } else {
+ startDir = startDir.substring(0, startDir.lastIndexOf(File.separatorChar));
+ }
+ currentDir = new File(startDir);
+
+ ActivityMixin.setTheme(this);
+ setContentView(R.layout.simple_dir_chooser);
+ setTitle(this.getResources().getString(R.string.simple_dir_chooser_title));
+
+ fill(currentDir);
+
+ okButton = (Button) findViewById(R.id.simple_dir_chooser_ok);
+ okButton.setEnabled(false);
+ okButton.setVisibility(View.INVISIBLE);
+ okButton.setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ Intent intent = new Intent();
+ String chosenDirName = File.separator + checkedText;
+ intent.putExtra("chosenDir", currentDir.getAbsolutePath() + chosenDirName);
+ setResult(RESULT_OK, intent);
+ finish();
+ }
+ });
+
+ Button cancelButton = (Button) findViewById(R.id.simple_dir_chooser_cancel);
+ cancelButton.setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ Intent intent = new Intent();
+ setResult(RESULT_CANCELED, intent);
+ finish();
+ }
+ });
+ }
+
+ private void fill(File dir) {
+ EditText path = (EditText) findViewById(R.id.simple_dir_chooser_path);
+ path.setText(this.getResources().getString(R.string.simple_dir_chooser_current_path) + " " + dir.getAbsolutePath());
+ final File[] dirs = dir.listFiles(new DirOnlyFilenameFilter());
+ List<Option> listDirs = new ArrayList<Option>();
+ try {
+ for (File currentDir : dirs) {
+ listDirs.add(new Option(currentDir.getName(), currentDir.getAbsolutePath()));
+ }
+ } catch (Exception e) {
+ }
+ Collections.sort(listDirs);
+ if (dir.getParent() != null) {
+ listDirs.add(0, new Option(PARENT_DIR, dir.getParent()));
+ }
+ this.adapter = new FileArrayAdapter(SimpleDirChooser.this, R.layout.simple_dir_item, listDirs);
+ this.setListAdapter(adapter);
+ }
+
+ @Override
+ protected void onListItemClick(ListView l, View v, int position, long id) {
+ super.onListItemClick(l, v, position, id);
+ }
+
+ public class FileArrayAdapter extends ArrayAdapter<Option> {
+
+ private Context c;
+ private int id;
+ private List<Option> items;
+
+ public FileArrayAdapter(Context context, int simpleDirItemResId, List<Option> objects) {
+ super(context, simpleDirItemResId, objects);
+ c = context;
+ id = simpleDirItemResId;
+ items = objects;
+ }
+
+ @Override
+ public Option getItem(int i) {
+ return items.get(i);
+ }
+
+ @Override
+ public View getView(int position, View convertView, ViewGroup parent) {
+ View v = convertView;
+ if (v == null) {
+ LayoutInflater vi = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
+ v = vi.inflate(id, null);
+ }
+
+ final Option o = items.get(position);
+ if (o != null) {
+ TextView t1 = (TextView) v.findViewById(R.id.TextView01);
+ if (t1 != null) {
+ t1.setOnClickListener(new OnTextViewClickListener(position));
+ t1.setText(o.getName());
+ }
+ CheckBox check = (CheckBox) v.findViewById(R.id.CheckBox);
+ if (check != null) {
+ check.setOnClickListener(new OnCheckBoxClickListener(o.getName()));
+ }
+ }
+ return v;
+ }
+ }
+
+ public class OnTextViewClickListener implements OnClickListener {
+ private int position;
+
+ OnTextViewClickListener(int position) {
+ this.position = position;
+ }
+
+ @Override
+ public void onClick(View arg0) {
+ Option o = adapter.getItem(position);
+ if (o.getName().equals(PARENT_DIR)) {
+ currentDir = new File(o.getPath());
+ fill(currentDir);
+ } else {
+ File dir = new File(o.getPath());
+ if (dir.list(new DirOnlyFilenameFilter()).length > 0) {
+ currentDir = dir;
+ fill(currentDir);
+ }
+ }
+ }
+ }
+
+ public class OnCheckBoxClickListener implements OnClickListener {
+ private String checkedText;
+
+ OnCheckBoxClickListener(String checkedText) {
+ this.checkedText = checkedText;
+ }
+
+ @Override
+ public void onClick(View arg0) {
+ CheckBox check = (CheckBox) arg0;
+ if (lastBoxChecked == check) {
+ check.setChecked(false);
+ lastBoxChecked = null;
+ okButton.setEnabled(false);
+ okButton.setVisibility(View.INVISIBLE);
+ SimpleDirChooser.this.checkedText = "";
+ } else {
+ if (lastBoxChecked != null) {
+ lastBoxChecked.setChecked(false);
+ }
+ check.setChecked(true);
+ lastBoxChecked = check;
+ okButton.setEnabled(true);
+ okButton.setVisibility(View.VISIBLE);
+ SimpleDirChooser.this.checkedText = checkedText;
+ }
+ }
+ }
+
+ public class Option implements Comparable<Option> {
+ private String name;
+ private String path;
+
+ public Option(String n, String p) {
+ name = n;
+ path = p;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public String getPath() {
+ return path;
+ }
+
+ @Override
+ public int compareTo(Option o) {
+ if (o != null && this.name != null) {
+ return this.name.toLowerCase().compareTo(o.getName().toLowerCase());
+ }
+ throw new IllegalArgumentException("");
+ }
+ }
+
+ public class DirOnlyFilenameFilter implements FilenameFilter {
+
+ @Override
+ public boolean accept(File dir, String filename) {
+ File file = new File(dir, filename);
+ return file.isDirectory();
+ }
+
+ }
+}