X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=android%2Fsrc%2Fnet%2Fsf%2Fopenrocket%2Fandroid%2Ffilebrowser%2FSimpleFileBrowser.java;h=1ca68f65c3a21a0c34aad10fe045906ef158ca57;hb=9349577cdfdff682b2aabd6daa24fdc3a7449b58;hp=1c4ff406a72d7a8b6864535058ce8dfd8df646aa;hpb=6771676f0d4c8699bbcfc302d456e017298523d0;p=debian%2Fopenrocket diff --git a/android/src/net/sf/openrocket/android/filebrowser/SimpleFileBrowser.java b/android/src/net/sf/openrocket/android/filebrowser/SimpleFileBrowser.java index 1c4ff406..1ca68f65 100644 --- a/android/src/net/sf/openrocket/android/filebrowser/SimpleFileBrowser.java +++ b/android/src/net/sf/openrocket/android/filebrowser/SimpleFileBrowser.java @@ -2,43 +2,61 @@ package net.sf.openrocket.android.filebrowser; import java.io.File; import java.io.FileFilter; -import java.text.Collator; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.List; import net.sf.openrocket.R; -import net.sf.openrocket.android.actionbarcompat.ActionBarListActivity; import android.app.AlertDialog; +import android.app.Dialog; import android.content.DialogInterface; import android.content.Intent; +import android.content.SharedPreferences; +import android.content.res.Resources; import android.net.Uri; import android.os.Bundle; import android.os.Environment; +import android.preference.PreferenceManager; import android.view.View; -import android.widget.ArrayAdapter; +import android.view.ViewGroup; +import android.widget.BaseAdapter; +import android.widget.ImageView; import android.widget.ListView; +import android.widget.TextView; -public class SimpleFileBrowser extends ActionBarListActivity { +import com.actionbarsherlock.app.SherlockListActivity; - private List item = null; - private List path = null; - private String root = "/"; +public class SimpleFileBrowser extends SherlockListActivity { + private List path = null; + private final static File root = new File("/"); + + private File previousDirectory = root; + + private String baseDirPrefKey; + private String baseDirName; + + private boolean showOnlyOrkFiles; + private static final OrkFileFilter filter = new OrkFileFilter(); - private static final Collator sorter = Collator.getInstance(); - static { - sorter.setStrength(Collator.TERTIARY); - sorter.setDecomposition(Collator.CANONICAL_DECOMPOSITION); - } + private static final Comparator sorter = new AlphanumComparator(); /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.simplefilebrowser); - getDir( Environment.getExternalStorageDirectory().getAbsolutePath() ); + + Resources resources = this.getResources(); + baseDirPrefKey = resources.getString(R.string.PreferenceFileBrowserBaseDirectory); + SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this); + + String showOnlyOrkFilesKey = resources.getString(R.string.PreferenceShowOnlyOrkFiles); + showOnlyOrkFiles = pref.getBoolean(showOnlyOrkFilesKey, false); + + baseDirName = pref.getString(baseDirPrefKey, Environment.getExternalStorageDirectory().getAbsolutePath() ); + getDir( new File(baseDirName) ); } private static class OrkFileFilter implements FileFilter { @@ -51,12 +69,15 @@ public class SimpleFileBrowser extends ActionBarListActivity { if ( arg0.isDirectory() ) { return true; } + return isOrk(arg0); + } + + public boolean isOrk(File arg0) { if ( arg0.getName().endsWith(".ork") ) { return true; } return false; } - } private static class FileComparator implements Comparator { @@ -84,42 +105,63 @@ public class SimpleFileBrowser extends ActionBarListActivity { } - private void getDir(String dirPath) { - setTitle(dirPath); - item = new ArrayList(); - path = new ArrayList(); + private void getDir(final File dirPath) { + + // A little sanity check. It could be possible the directory saved in the preference + // is no longer mounted, is not a directory (any more), or cannot be read. + // if any of these are the case, we display a little dialog, then revert to the + // previousDirectory. + if ( !dirPath.exists() || !dirPath.isDirectory() || !dirPath.canRead() ) { + + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setTitle("Unable to open directory " + dirPath.getAbsolutePath() ); + builder.setCancelable(true); + builder.setOnCancelListener( new Dialog.OnCancelListener() { + + @Override + public void onCancel(DialogInterface arg0) { + if ( root.getAbsolutePath().equals(dirPath.getAbsolutePath()) ) { + SimpleFileBrowser.this.finish(); + } else { + SimpleFileBrowser.this.getDir( previousDirectory ); + } + } + + }); + builder.show(); + return; + } + + previousDirectory = dirPath; + + setTitle(dirPath.getAbsolutePath()); + path = new ArrayList(); - File f = new File(dirPath); - File[] files = f.listFiles(filter); + File[] files = dirPath.listFiles((showOnlyOrkFiles) ? filter : null ); - if (!dirPath.equals(root)) { - item.add(root); + boolean hasUp = false; + if ( !dirPath.getAbsolutePath().equals("/")) { path.add(root); - item.add("../"); - path.add(f.getParent()); + path.add( dirPath.getParentFile() ); + hasUp = true; } Arrays.sort(files, new FileComparator() ); - for (int i = 0; i < files.length; i++) { - File file = files[i]; - path.add(file.getPath()); - if (file.isDirectory()) - item.add(file.getName() + "/"); - else - item.add(file.getName()); + for( File file : files ) { + path.add(file); } - ArrayAdapter fileList = new ArrayAdapter(this, android.R.layout.simple_list_item_1, item); + DirectoryList fileList = new DirectoryList(hasUp, path); setListAdapter(fileList); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { - final File file = new File(path.get(position)); + final File file = path.get(position); if (file.isDirectory()) { if (file.canRead()) - getDir(path.get(position)); + getDir(file); else { new AlertDialog.Builder(this).setIcon(R.drawable.or_launcher) .setTitle("[" + file.getName() + "] folder can't be read!") @@ -140,4 +182,107 @@ public class SimpleFileBrowser extends ActionBarListActivity { finish(); } } + + private class DirectoryList extends BaseAdapter { + + List listing; + boolean hasUp; + + DirectoryList( boolean hasUp ,List listing ) { + this.listing = listing; + this.hasUp = hasUp; + } + + @Override + public int getCount() { + return listing.size(); + } + + @Override + public Object getItem(int arg0) { + return listing.get(arg0); + } + + @Override + public long getItemId(int position) { + return position; + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + if ( convertView == null ) { + convertView = getLayoutInflater().inflate(R.layout.filebrowser_list_item, parent, false); + } + + File file = (File) getItem(position); + + // Set the name of the field. + { + String fileName = file.getName(); + if ( hasUp ) { + if (position == 0 ) { + fileName = root.getAbsolutePath(); + } else if (position == 1) { + fileName = ".."; + } + } + + ((TextView) convertView.findViewById(R.id.filebrowser_list_item_name)).setText(fileName); + } + + // Set the "type icon" directory, ork file, or none. + { + ImageView v = (ImageView) (convertView.findViewById(R.id.filebrowser_list_item_typeicon)); + if ( file.isDirectory() ) { + v.setVisibility(View.VISIBLE); + v.setImageResource(R.drawable.ic_directory); + } else if ( filter.isOrk( file ) ) { + v.setVisibility(View.VISIBLE); + v.setImageResource(R.drawable.or_launcher); + } else { + v.setVisibility(View.INVISIBLE); + } + } + + // Set the "favorite directory" thing. + { + ImageView v = (ImageView) (convertView.findViewById(R.id.filebrowser_list_item_homeicon)); + if ( file.isDirectory() && hasUp && position > 1 ) { + v.setVisibility(View.VISIBLE); + if ( baseDirName.equals( file.getAbsolutePath() ) ) { + v.setSelected(true); + } else { + v.setSelected(false); + v.setClickable(true); + v.setOnClickListener( new ChangeBaseDirectory(file.getAbsolutePath())); + } + } else { + v.setVisibility(View.INVISIBLE); + v.setClickable(false); + } + } + return convertView; + } + } + + private class ChangeBaseDirectory implements View.OnClickListener { + + private final String dirname; + + ChangeBaseDirectory ( String dirname ) { + this.dirname = dirname; + } + + @Override + public void onClick(View v) { + if ( v.isSelected() == false ) { + SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(SimpleFileBrowser.this); + baseDirName = dirname; + pref.edit().putString(baseDirPrefKey, dirname).commit(); + ((BaseAdapter)SimpleFileBrowser.this.getListAdapter()).notifyDataSetChanged(); + } + } + + } + }