+package net.sf.openrocket.android.filebrowser;\r
+\r
+import java.io.File;\r
+import java.io.FileFilter;\r
+import java.text.Collator;\r
+import java.util.ArrayList;\r
+import java.util.Arrays;\r
+import java.util.Comparator;\r
+import java.util.List;\r
+\r
+import net.sf.openrocket.R;\r
+import android.app.AlertDialog;\r
+import android.app.ListActivity;\r
+import android.content.DialogInterface;\r
+import android.content.Intent;\r
+import android.net.Uri;\r
+import android.os.Bundle;\r
+import android.os.Environment;\r
+import android.view.View;\r
+import android.widget.ArrayAdapter;\r
+import android.widget.ListView;\r
+import android.widget.TextView;\r
+\r
+public class SimpleFileBrowser extends ListActivity {\r
+\r
+ private List<String> item = null;\r
+ private List<String> path = null;\r
+ private String root = "/";\r
+ private TextView myPath;\r
+\r
+ private static final OrkFileFilter filter = new OrkFileFilter();\r
+ private static final Collator sorter = Collator.getInstance();\r
+ static {\r
+ sorter.setStrength(Collator.TERTIARY);\r
+ sorter.setDecomposition(Collator.CANONICAL_DECOMPOSITION);\r
+ }\r
+\r
+ /** Called when the activity is first created. */\r
+ @Override\r
+ public void onCreate(Bundle savedInstanceState) {\r
+ super.onCreate(savedInstanceState);\r
+ setContentView(R.layout.simplefilebrowser);\r
+ myPath = (TextView) findViewById(R.id.path);\r
+ getDir( Environment.getExternalStorageDirectory().getAbsolutePath() );\r
+ }\r
+\r
+ private static class OrkFileFilter implements FileFilter {\r
+\r
+ /* (non-Javadoc)\r
+ * @see java.io.FileFilter#accept(java.io.File)\r
+ */\r
+ @Override\r
+ public boolean accept(File arg0) {\r
+ if ( arg0.isDirectory() ) { \r
+ return true;\r
+ }\r
+ if ( arg0.getName().endsWith(".ork") ) {\r
+ return true;\r
+ }\r
+ return false;\r
+ }\r
+\r
+ }\r
+\r
+ private static class FileComparator implements Comparator<File> {\r
+\r
+ /* (non-Javadoc)\r
+ * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)\r
+ */\r
+ @Override\r
+ public int compare(File arg0, File arg1) {\r
+ // Directories come before files, otherwise alpha.\r
+ if ( arg0.isDirectory() ) {\r
+ if ( ! arg1.isDirectory() ) {\r
+ return -1;\r
+ }\r
+ return sorter.compare(arg0.getName(), arg1.getName());\r
+ }\r
+\r
+ // arg0 is not a directory.\r
+ if ( arg1.isDirectory() ) {\r
+ return 1;\r
+ }\r
+\r
+ return sorter.compare(arg0.getName(), arg1.getName());\r
+ }\r
+\r
+ }\r
+\r
+ private void getDir(String dirPath) {\r
+ myPath.setText("Location: " + dirPath);\r
+ item = new ArrayList<String>();\r
+ path = new ArrayList<String>();\r
+\r
+ File f = new File(dirPath);\r
+ File[] files = f.listFiles(filter);\r
+\r
+ if (!dirPath.equals(root)) {\r
+ item.add(root);\r
+ path.add(root);\r
+ item.add("../");\r
+ path.add(f.getParent());\r
+ }\r
+\r
+ Arrays.sort(files, new FileComparator() );\r
+ for (int i = 0; i < files.length; i++) {\r
+ File file = files[i];\r
+ path.add(file.getPath());\r
+ if (file.isDirectory())\r
+ item.add(file.getName() + "/");\r
+ else\r
+ item.add(file.getName());\r
+ }\r
+\r
+ ArrayAdapter<String> fileList = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, item);\r
+ setListAdapter(fileList);\r
+\r
+ }\r
+\r
+ @Override\r
+ protected void onListItemClick(ListView l, View v, int position, long id) {\r
+ final File file = new File(path.get(position));\r
+ if (file.isDirectory()) {\r
+ if (file.canRead())\r
+ getDir(path.get(position));\r
+ else {\r
+ new AlertDialog.Builder(this).setIcon(R.drawable.or_launcher)\r
+ .setTitle("[" + file.getName() + "] folder can't be read!")\r
+ .setPositiveButton("OK",\r
+ new DialogInterface.OnClickListener() {\r
+ @Override\r
+ public void onClick(DialogInterface dialog, int which) {\r
+ // TODO Auto-generated method stub\r
+ }\r
+ }).show();\r
+ }\r
+ }\r
+ else\r
+ {\r
+ Intent resultData = new Intent(Intent.ACTION_VIEW);\r
+ resultData.setData( Uri.fromFile(file) );\r
+ setResult(RESULT_OK,resultData);\r
+ finish();\r
+ }\r
+ }\r
+}\r