Merge commit '42b2e5ca519766e37ce6941ba4faecc9691cc403' into upstream
[debian/openrocket] / android / src / net / sf / openrocket / android / filebrowser / SimpleFileBrowser.java
index f7204de43bd9d5a5beea01c42ffe5b11f79135a4..1ca68f65c3a21a0c34aad10fe045906ef158ca57 100644 (file)
@@ -2,15 +2,14 @@ package net.sf.openrocket.android.filebrowser;
 \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 net.sf.openrocket.android.actionbarcompat.ActionBarListActivity;\r
 import android.app.AlertDialog;\r
+import android.app.Dialog;\r
 import android.content.DialogInterface;\r
 import android.content.Intent;\r
 import android.content.SharedPreferences;\r
@@ -26,11 +25,15 @@ import android.widget.ImageView;
 import android.widget.ListView;\r
 import android.widget.TextView;\r
 \r
-public class SimpleFileBrowser extends ActionBarListActivity {\r
+import com.actionbarsherlock.app.SherlockListActivity;\r
+\r
+public class SimpleFileBrowser extends SherlockListActivity {\r
 \r
        private List<File> path = null;\r
        private final static File root = new File("/");\r
 \r
+       private File previousDirectory = root;\r
+       \r
        private String baseDirPrefKey;\r
        private String baseDirName;\r
 \r
@@ -102,7 +105,35 @@ public class SimpleFileBrowser extends ActionBarListActivity {
 \r
        }\r
 \r
-       private void getDir(File dirPath) {\r
+       private void getDir(final File dirPath) {\r
+               \r
+               // A little sanity check.  It could be possible the directory saved in the preference\r
+               // is no longer mounted, is not a directory (any more), or cannot be read.\r
+               // if any of these are the case, we display a little dialog, then revert to the\r
+               // previousDirectory.\r
+               if ( !dirPath.exists() || !dirPath.isDirectory() || !dirPath.canRead() ) {\r
+                       \r
+                       AlertDialog.Builder builder = new AlertDialog.Builder(this);\r
+                       builder.setTitle("Unable to open directory " + dirPath.getAbsolutePath() );\r
+                       builder.setCancelable(true);\r
+                       builder.setOnCancelListener( new Dialog.OnCancelListener() {\r
+\r
+                               @Override\r
+                               public void onCancel(DialogInterface arg0) {\r
+                                       if ( root.getAbsolutePath().equals(dirPath.getAbsolutePath()) ) {\r
+                                               SimpleFileBrowser.this.finish();\r
+                                       } else {\r
+                                               SimpleFileBrowser.this.getDir( previousDirectory );\r
+                                       }\r
+                               }\r
+                               \r
+                       });\r
+                       builder.show();\r
+                       return;\r
+               }\r
+               \r
+               previousDirectory = dirPath;\r
+               \r
                setTitle(dirPath.getAbsolutePath());\r
                path = new ArrayList<File>();\r
 \r
@@ -130,7 +161,7 @@ public class SimpleFileBrowser extends ActionBarListActivity {
                final File file = path.get(position);\r
                if (file.isDirectory()) {\r
                        if (file.canRead())\r
-                               getDir(path.get(position));\r
+                               getDir(file);\r
                        else {\r
                                new AlertDialog.Builder(this).setIcon(R.drawable.or_launcher)\r
                                .setTitle("[" + file.getName() + "] folder can't be read!")\r
@@ -213,13 +244,10 @@ public class SimpleFileBrowser extends ActionBarListActivity {
                                }\r
                        }\r
 \r
-                       // Set the "base directory" thing.\r
+                       // Set the "favorite directory" thing.\r
                        {\r
                                ImageView v = (ImageView) (convertView.findViewById(R.id.filebrowser_list_item_homeicon));\r
-                               if ( !file.isDirectory() ) {\r
-                                       v.setVisibility(View.INVISIBLE);\r
-                                       v.setClickable(false);\r
-                               } else {\r
+                               if ( file.isDirectory() && hasUp && position > 1 ) {\r
                                        v.setVisibility(View.VISIBLE);\r
                                        if ( baseDirName.equals( file.getAbsolutePath() ) )  {\r
                                                v.setSelected(true);\r
@@ -228,6 +256,9 @@ public class SimpleFileBrowser extends ActionBarListActivity {
                                                v.setClickable(true);\r
                                                v.setOnClickListener( new ChangeBaseDirectory(file.getAbsolutePath()));\r
                                        }\r
+                               } else {\r
+                                       v.setVisibility(View.INVISIBLE);\r
+                                       v.setClickable(false);\r
                                }\r
                        }\r
                        return convertView;\r
@@ -247,8 +278,8 @@ public class SimpleFileBrowser extends ActionBarListActivity {
                        if ( v.isSelected() == false ) {\r
                                SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(SimpleFileBrowser.this);\r
                                baseDirName = dirname;\r
-                               pref.edit().putString(baseDirPrefKey, dirname).apply();\r
-                               SimpleFileBrowser.this.getDir(new File(dirname));\r
+                               pref.edit().putString(baseDirPrefKey, dirname).commit();\r
+                               ((BaseAdapter)SimpleFileBrowser.this.getListAdapter()).notifyDataSetChanged();\r
                        }\r
                }\r
                \r