package cgeo.geocaching.files; import cgeo.geocaching.Intents; import cgeo.geocaching.R; import cgeo.geocaching.activity.AbstractListActivity; import cgeo.geocaching.activity.ActivityMixin; import org.apache.commons.lang3.StringUtils; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.text.InputType; 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.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 AbstractListActivity { public static final String EXTRA_CHOOSE_FOR_WRITING = "chooseForWriting"; private static final String PARENT_DIR = ".. "; private File currentDir; private FileArrayAdapter adapter; private Button okButton = null; private int lastPosition = -1; private boolean chooseForWriting = false; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); final Bundle extras = getIntent().getExtras(); currentDir = dirContaining(extras.getString(Intents.EXTRA_START_DIR)); chooseForWriting = extras.getBoolean(SimpleDirChooser.EXTRA_CHOOSE_FOR_WRITING, false); ActivityMixin.setTheme(this); setContentView(R.layout.simple_dir_chooser); 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) { setResult(RESULT_OK, new Intent() .setData(Uri.fromFile(new File(currentDir, adapter.getItem(lastPosition).getName())))); 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(); } }); EditText pathField = (EditText) findViewById(R.id.simple_dir_chooser_path); pathField.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { editPath(); } }); } public void editPath() { AlertDialog.Builder builder = new AlertDialog.Builder(SimpleDirChooser.this); builder.setTitle(R.string.simple_dir_chooser_current_path); final EditText input = new EditText(SimpleDirChooser.this); input.setInputType(InputType.TYPE_CLASS_TEXT); input.setText(currentDir.getPath()); builder.setView(input); builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String pathText = input.getText().toString(); File newPathDir = new File(pathText); if (newPathDir.exists() && newPathDir.isDirectory()) { currentDir = newPathDir; fill(currentDir); } else { showToast(SimpleDirChooser.this.getResources().getString(R.string.simple_dir_chooser_invalid_path)); } } }); builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); builder.show(); } /** * Return the directory containing a given path, or a sensible default. * * @param path the path to get the enclosing directory from, can be null or empty * @return the directory containing path, or a sensible default if none */ private static File dirContaining(final String path) { return StringUtils.contains(path, File.separatorChar) ? new File(StringUtils.substringBeforeLast(path, Character.toString(File.separatorChar))) : Environment.getExternalStorageDirectory(); } private void fill(File dir) { lastPosition = -1; 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