diff options
author | Bananeweizen <bananeweizen@gmx.de> | 2012-09-01 15:28:08 +0200 |
---|---|---|
committer | Bananeweizen <bananeweizen@gmx.de> | 2012-09-01 15:28:08 +0200 |
commit | 126868467cd1911071b0d25fa3943918ddb330cf (patch) | |
tree | 969cbfe2910e882158a1a8def9458048a7d641d1 /main | |
parent | 3cb970e064bd5bac7b13f4130dcea8c9030027a2 (diff) | |
download | cgeo-126868467cd1911071b0d25fa3943918ddb330cf.zip cgeo-126868467cd1911071b0d25fa3943918ddb330cf.tar.gz cgeo-126868467cd1911071b0d25fa3943918ddb330cf.tar.bz2 |
#1995: several fixes for pull request
* store directories if changed directly in text field
* clean up simple dir chooser and settings activity
* remove code duplication, extract constants
Diffstat (limited to 'main')
-rw-r--r-- | main/res/values-de/strings.xml | 8 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/SettingsActivity.java | 86 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/files/SimpleDirChooser.java | 55 |
3 files changed, 78 insertions, 71 deletions
diff --git a/main/res/values-de/strings.xml b/main/res/values-de/strings.xml index d1dc829..e0ca7a5 100644 --- a/main/res/values-de/strings.xml +++ b/main/res/values-de/strings.xml @@ -425,12 +425,12 @@ <string name="init_select_mapfile">…</string> <string name="init_select_gpx_exportdir">…</string> <string name="init_select_gpx_importdir">…</string> - <string name="init_gpx_exportdir">Verzeichnis für GPX Exporte</string> - <string name="init_gpx_importdir">Verzeichnis für GPX Importe</string> + <string name="init_gpx_exportdir">Verzeichnis für GPX-Exporte</string> + <string name="init_gpx_importdir">Verzeichnis für GPX-Importe</string> <string name="init_gpx_exportdir_description">Hier kann das Verzeichnis für GPX-Exporte gewählt werden.</string> <string name="init_gpx_importdir_description">Hier kann das Verzeichnis für GPX-Importe gewählt werden.</string> - <string name="init_gpx_exportdir_select">Wähle Verzeichnis für GPX Exporte</string> - <string name="init_gpx_importdir_select">Wähle Verzeichnis für GPX Importe</string> + <string name="init_gpx_exportdir_select">Wähle Verzeichnis für GPX-Exporte</string> + <string name="init_gpx_importdir_select">Wähle Verzeichnis für GPX-Importe</string> <string name="init_maptrail">Zeige Spur auf Karte</string> <string name="init_share_after_export">Weiterleiten nach dem Export</string> <string name="init_trackautovisit">Trackables automatisch auf \"besuchen\" setzen</string> diff --git a/main/src/cgeo/geocaching/SettingsActivity.java b/main/src/cgeo/geocaching/SettingsActivity.java index af0a6bd..448407f 100644 --- a/main/src/cgeo/geocaching/SettingsActivity.java +++ b/main/src/cgeo/geocaching/SettingsActivity.java @@ -18,6 +18,7 @@ import cgeo.geocaching.ui.Formatter; import cgeo.geocaching.utils.Log; import cgeo.geocaching.utils.LogTemplateProvider; import cgeo.geocaching.utils.LogTemplateProvider.LogTemplate; +import cgeo.geocaching.utils.RunnableWithArgument; import ch.boye.httpclientandroidlib.HttpResponse; @@ -56,8 +57,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 final static int SELECT_IMPGPXDIR_REQUEST = 3; + private final static int SELECT_GPX_EXPORT_REQUEST = 2; + private final static int SELECT_GPX_IMPORT_REQUEST = 3; private ProgressDialog loginDialog = null; @@ -176,14 +177,11 @@ public class SettingsActivity extends AbstractActivity { @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == 0) { - boolean status; - ((EditText) findViewById(R.id.username)).setText(""); ((EditText) findViewById(R.id.password)).setText(""); ((EditText) findViewById(R.id.passvote)).setText(""); - status = saveValues(); - if (status) { + if (saveValues()) { showToast(res.getString(R.string.init_cleared)); } else { showToast(res.getString(R.string.err_init_cleared)); @@ -570,7 +568,7 @@ public class SettingsActivity extends AbstractActivity { 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); + startActivityForResult(dirChooser, SELECT_GPX_EXPORT_REQUEST); } }); @@ -584,7 +582,7 @@ public class SettingsActivity extends AbstractActivity { public void onClick(View v) { Intent dirChooser = new Intent(SettingsActivity.this, SimpleDirChooser.class); dirChooser.putExtra(SimpleDirChooser.START_DIR, Settings.getGpxImportDir()); - startActivityForResult(dirChooser, SELECT_IMPGPXDIR_REQUEST); + startActivityForResult(dirChooser, SELECT_GPX_IMPORT_REQUEST); } }); @@ -780,31 +778,31 @@ public class SettingsActivity extends AbstractActivity { String passvoteNew = StringUtils.trimToEmpty(((EditText) findViewById(R.id.passvote)).getText().toString()); // don't trim signature, user may want to have whitespace at the beginning String signatureNew = ((EditText) findViewById(R.id.signature)).getText().toString(); + String mapFileNew = StringUtils.trimToEmpty(((EditText) findViewById(R.id.mapfile)).getText().toString()); + String altitudeNew = StringUtils.trimToNull(((EditText) findViewById(R.id.altitude)).getText().toString()); - String mfmapFileNew = StringUtils.trimToEmpty(((EditText) findViewById(R.id.mapfile)).getText().toString()); + int altitudeNewInt = parseNumber(altitudeNew, 0); - int altitudeNewInt = 0; - if (altitudeNew != null) { - try { - altitudeNewInt = Integer.parseInt(altitudeNew); - } catch (NumberFormatException e) { - altitudeNewInt = 0; - } - } + TextView field = (TextView) findViewById(R.id.showwaypointsthreshold); + final int waypointThreshold = parseNumber(field.getText().toString(), 5); final boolean status1 = Settings.setLogin(usernameNew, passwordNew); final boolean status2 = Settings.setGCvoteLogin(passvoteNew); final boolean status3 = Settings.setSignature(signatureNew); final boolean status4 = Settings.setAltCorrection(altitudeNewInt); - final boolean status5 = Settings.setMapFile(mfmapFileNew); - TextView field = (TextView) findViewById(R.id.showwaypointsthreshold); - Settings.setShowWaypointsThreshold(safeParse(field, 5)); + final boolean status5 = Settings.setMapFile(mapFileNew); + Settings.setShowWaypointsThreshold(waypointThreshold); + + String importNew = StringUtils.trimToEmpty(((EditText) findViewById(R.id.gpx_importdir)).getText().toString()); + String exportNew = StringUtils.trimToEmpty(((EditText) findViewById(R.id.gpx_exportdir)).getText().toString()); + Settings.setGpxImportDir(importNew); + Settings.setGpxExportDir(exportNew); return status1 && status2 && status3 && status4 && status5; } /** - * Returns the Int Value in the Field + * Returns the integer value from the string * * @param field * the field to retrieve the integer value from @@ -813,9 +811,9 @@ public class SettingsActivity extends AbstractActivity { * @return either the field content or the default value */ - static private int safeParse(final TextView field, int defaultValue) { + static private int parseNumber(final String number, int defaultValue) { try { - return Integer.parseInt(field.getText().toString()); + return Integer.parseInt(number); } catch (NumberFormatException e) { return defaultValue; } @@ -916,7 +914,7 @@ public class SettingsActivity extends AbstractActivity { } @Override - protected void onActivityResult(int requestCode, int resultCode, Intent data) { + protected void onActivityResult(int requestCode, int resultCode, final Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == SELECT_MAPFILE_REQUEST) { @@ -930,25 +928,35 @@ 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")); + if (requestCode == SELECT_GPX_EXPORT_REQUEST) { + checkDirectory(resultCode, data, R.id.gpx_exportdir, new RunnableWithArgument<String>() { + + @Override + public void run(String directory) { + Settings.setGpxExportDir(directory); } - } - EditText gpxExportDir = (EditText) findViewById(R.id.gpx_exportdir); - gpxExportDir.setText(Settings.getGpxExportDir()); - gpxExportDir.requestFocus(); + }); } - if (requestCode == SELECT_IMPGPXDIR_REQUEST) { - if (resultCode == RESULT_OK) { - if (data.hasExtra("chosenDir")) { - Settings.setGpxImportDir(data.getStringExtra("chosenDir")); + if (requestCode == SELECT_GPX_IMPORT_REQUEST) { + checkDirectory(resultCode, data, R.id.gpx_importdir, new RunnableWithArgument<String>() { + + @Override + public void run(String directory) { + Settings.setGpxImportDir(directory); } + }); + } + } + + private void checkDirectory(int resultCode, Intent data, int textField, RunnableWithArgument<String> runnableSetDir) { + if (resultCode == RESULT_OK) { + if (data.hasExtra(SimpleDirChooser.EXTRA_CHOSEN_DIR)) { + final String directory = data.getStringExtra(SimpleDirChooser.EXTRA_CHOSEN_DIR); + runnableSetDir.run(directory); + EditText directoryText = (EditText) findViewById(textField); + directoryText.setText(directory); + directoryText.requestFocus(); } - EditText gpxImportDir = (EditText) findViewById(R.id.gpx_importdir); - gpxImportDir.setText(Settings.getGpxExportDir()); - gpxImportDir.requestFocus(); } } diff --git a/main/src/cgeo/geocaching/files/SimpleDirChooser.java b/main/src/cgeo/geocaching/files/SimpleDirChooser.java index 57cbc0c..c5ebb9b 100644 --- a/main/src/cgeo/geocaching/files/SimpleDirChooser.java +++ b/main/src/cgeo/geocaching/files/SimpleDirChooser.java @@ -1,6 +1,3 @@ -/** - * - */ package cgeo.geocaching.files; import cgeo.geocaching.R; @@ -10,6 +7,7 @@ import android.app.ListActivity; import android.content.Context; import android.content.Intent; import android.os.Bundle; +import android.os.Environment; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; @@ -31,6 +29,7 @@ import java.util.List; * Dialog for choosing a file or directory. */ public class SimpleDirChooser extends ListActivity { + public static final String EXTRA_CHOSEN_DIR = "chosenDir"; public static final String START_DIR = "start_dir"; private static final String PARENT_DIR = ".. "; private File currentDir; @@ -45,7 +44,7 @@ public class SimpleDirChooser extends ListActivity { final Bundle extras = getIntent().getExtras(); String startDir = extras.getString(START_DIR); if (startDir == null) { - startDir = "/sdcard"; + startDir = Environment.getExternalStorageDirectory().getPath(); } else { startDir = startDir.substring(0, startDir.lastIndexOf(File.separatorChar)); } @@ -65,7 +64,7 @@ public class SimpleDirChooser extends ListActivity { public void onClick(View v) { Intent intent = new Intent(); String chosenDirName = File.separator + checkedText; - intent.putExtra("chosenDir", currentDir.getAbsolutePath() + chosenDirName); + intent.putExtra(EXTRA_CHOSEN_DIR, currentDir.getAbsolutePath() + chosenDirName); setResult(RESULT_OK, intent); finish(); } @@ -108,40 +107,40 @@ public class SimpleDirChooser extends ListActivity { public class FileArrayAdapter extends ArrayAdapter<Option> { - private Context c; + private Context content; 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; + this.content = context; + this.id = simpleDirItemResId; + this.items = objects; } @Override - public Option getItem(int i) { - return items.get(i); + public Option getItem(int index) { + return items.get(index); } @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); + LayoutInflater vi = (LayoutInflater) content.getSystemService(Context.LAYOUT_INFLATER_SERVICE); v = vi.inflate(id, null); } - final Option o = items.get(position); - if (o != null) { + final Option option = items.get(position); + if (option != null) { TextView t1 = (TextView) v.findViewById(R.id.TextView01); if (t1 != null) { t1.setOnClickListener(new OnTextViewClickListener(position)); - t1.setText(o.getName()); + t1.setText(option.getName()); } CheckBox check = (CheckBox) v.findViewById(R.id.CheckBox); if (check != null) { - check.setOnClickListener(new OnCheckBoxClickListener(o.getName())); + check.setOnClickListener(new OnCheckBoxClickListener(option.getName())); } } return v; @@ -157,12 +156,12 @@ public class SimpleDirChooser extends ListActivity { @Override public void onClick(View arg0) { - Option o = adapter.getItem(position); - if (o.getName().equals(PARENT_DIR)) { - currentDir = new File(o.getPath()); + Option option = adapter.getItem(position); + if (option.getName().equals(PARENT_DIR)) { + currentDir = new File(option.getPath()); fill(currentDir); } else { - File dir = new File(o.getPath()); + File dir = new File(option.getPath()); if (dir.list(new DirOnlyFilenameFilter()).length > 0) { currentDir = dir; fill(currentDir); @@ -201,12 +200,12 @@ public class SimpleDirChooser extends ListActivity { } public class Option implements Comparable<Option> { - private String name; - private String path; + private final String name; + private final String path; - public Option(String n, String p) { - name = n; - path = p; + public Option(String name, String path) { + this.name = name; + this.path = path; } public String getName() { @@ -218,9 +217,9 @@ public class SimpleDirChooser extends ListActivity { } @Override - public int compareTo(Option o) { - if (o != null && this.name != null) { - return this.name.toLowerCase().compareTo(o.getName().toLowerCase()); + public int compareTo(Option other) { + if (other != null && this.name != null) { + return String.CASE_INSENSITIVE_ORDER.compare(this.name, other.getName()); } throw new IllegalArgumentException(""); } |