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.os.Environment;
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 EXTRA_CHOSEN_DIR = "chosenDir";
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 = Environment.getExternalStorageDirectory().getPath();
} 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(EXTRA_CHOSEN_DIR, 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