]> git.gag.com Git - debian/openrocket/commitdiff
Rework the filebrowser some to add some functionality. A "hidden" preference now...
authorkruland2607 <kruland2607@180e2498-e6e9-4542-8430-84ac67f01cd8>
Tue, 14 Feb 2012 18:33:31 +0000 (18:33 +0000)
committerkruland2607 <kruland2607@180e2498-e6e9-4542-8430-84ac67f01cd8>
Tue, 14 Feb 2012 18:33:31 +0000 (18:33 +0000)
git-svn-id: https://openrocket.svn.sourceforge.net/svnroot/openrocket/trunk@415 180e2498-e6e9-4542-8430-84ac67f01cd8

android/res/drawable-mdpi/ic_directory.png [new file with mode: 0644]
android/res/drawable-mdpi/ic_star_select.png [new file with mode: 0644]
android/res/drawable-mdpi/ic_star_unselected.png [new file with mode: 0644]
android/res/drawable/filebrowser_home.xml [new file with mode: 0644]
android/res/layout/filebrowser_list_item.xml [new file with mode: 0644]
android/res/values/pref_strings.xml
android/res/values/strings.xml
android/res/xml/preferences.xml
android/src/net/sf/openrocket/android/filebrowser/AlphanumComparator.java [new file with mode: 0644]
android/src/net/sf/openrocket/android/filebrowser/SimpleFileBrowser.java

diff --git a/android/res/drawable-mdpi/ic_directory.png b/android/res/drawable-mdpi/ic_directory.png
new file mode 100644 (file)
index 0000000..a4599e3
Binary files /dev/null and b/android/res/drawable-mdpi/ic_directory.png differ
diff --git a/android/res/drawable-mdpi/ic_star_select.png b/android/res/drawable-mdpi/ic_star_select.png
new file mode 100644 (file)
index 0000000..4c47931
Binary files /dev/null and b/android/res/drawable-mdpi/ic_star_select.png differ
diff --git a/android/res/drawable-mdpi/ic_star_unselected.png b/android/res/drawable-mdpi/ic_star_unselected.png
new file mode 100644 (file)
index 0000000..98203a7
Binary files /dev/null and b/android/res/drawable-mdpi/ic_star_unselected.png differ
diff --git a/android/res/drawable/filebrowser_home.xml b/android/res/drawable/filebrowser_home.xml
new file mode 100644 (file)
index 0000000..1ca0fef
--- /dev/null
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="utf-8"?>\r
+<selector xmlns:android="http://schemas.android.com/apk/res/android" >\r
+    \r
+    <item android:state_selected="true" android:drawable="@drawable/ic_star_select"/>\r
+    <item android:drawable="@drawable/ic_star_unselected"/>\r
+    \r
+</selector>
\ No newline at end of file
diff --git a/android/res/layout/filebrowser_list_item.xml b/android/res/layout/filebrowser_list_item.xml
new file mode 100644 (file)
index 0000000..9e8de5e
--- /dev/null
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    android:orientation="horizontal" >
+
+    <ImageView
+        android:id="@+id/filebrowser_list_item_typeicon"
+        android:layout_width="48dp"
+        android:layout_height="48dp" />
+
+    <TextView
+        android:id="@+id/filebrowser_list_item_name"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_weight="1"
+        android:gravity="center_vertical"
+        android:minHeight="?android:attr/listPreferredItemHeight"
+        android:paddingLeft="6dip"
+        android:textAppearance="?android:attr/textAppearanceLarge" />
+
+    <ImageView
+        android:id="@+id/filebrowser_list_item_homeicon"
+        android:layout_width="48dp"
+        android:layout_height="48dp"
+        android:gravity="right"
+        android:paddingRight="15dp"
+        android:src="@drawable/filebrowser_home" />
+
+</LinearLayout>
\ No newline at end of file
index 5bf182126570eedca325c692783c0a150f0e1dc5..07eaa47ddf357c5c185adb219901d6c8720ac02d 100644 (file)
@@ -4,6 +4,8 @@
     <string name="PreferenceMotorBrowserGroupingOption">PreferenceMotorBrowserGroupingOption</string>\r
 \r
     <string name="PreferenceUseInternalFileBrowserOption">PreferenceUseInternalFileBrowserOpion</string>\r
+    <string name="PreferenceFileBrowserBaseDirectory">PreferenceFileBrowserBaseDirectory</string>\r
+    <string name="PreferenceShowOnlyOrkFiles">PreferenceShowOnlyOrkFiles</string>\r
     \r
     <string-array name="PreferenceMotorBrowserGroupingValues">\r
         <item>0</item>\r
index 46a5104fe853ba1e9c3d278ddfa91bf56823ad51..f1202d31aca1bb7d9d36df7ed2e4ac355eef7871 100644 (file)
     <string name="simulationSeries2Label">Series 2</string>\r
 \r
     <string name="motorbrowsergrouptitle">Motor Browser Grouping</string>\r
+    \r
     <string name="useinternalfilebrowsertitle">Use Internal File Browser</string>\r
     <string name="useinternalfilebrowsersummary">Check to use built in file browser instead of external file browser</string>\r
     \r
+    <string name="showonlyorkfiles">Show only ORK files in Internal File Browser</string>\r
+    <string name="showonlyorkfilessummary">Check to show only .ork files in internal file browser</string>\r
+    \r
     <string-array name="PreferenceUnitLengthEntries">\r
         <item>Millimeters</item>\r
         <item>Centimeters</item>\r
index 77b1cdefbf65e3abae792981baa8c79d5838dcc6..2c1a667f456e4b2e686b97f597cb91139d63247c 100644 (file)
         android:key="@string/PreferenceUseInternalFileBrowserOption"
         android:summary="@string/useinternalfilebrowsersummary"
         />
-        
+     
+    <CheckBoxPreference
+        android:defaultValue="false"
+        android:title="@string/showonlyorkfiles"
+        android:key="@string/PreferenceShowOnlyOrkFiles"
+        android:summary="@string/showonlyorkfilessummary"
+        android:dependency="@string/PreferenceUseInternalFileBrowserOption" 
+        />
+    
+    <!-- This preference is hidden - not to be configured in the Preference screen
+    <Preference
+        android:defaultValue="/"
+        android:key="@string/PreferenceFileBrowserBaseDirectory"
+        />
+     -->
+     
     <PreferenceScreen
         android:key="UnitPrefences"
         android:summary="Configure units"
diff --git a/android/src/net/sf/openrocket/android/filebrowser/AlphanumComparator.java b/android/src/net/sf/openrocket/android/filebrowser/AlphanumComparator.java
new file mode 100644 (file)
index 0000000..1bc762c
--- /dev/null
@@ -0,0 +1,138 @@
+/*\r
+ * The Alphanum Algorithm is an improved sorting algorithm for strings\r
+ * containing numbers.  Instead of sorting numbers in ASCII order like\r
+ * a standard sort, this algorithm sorts numbers in numeric order.\r
+ *\r
+ * The Alphanum Algorithm is discussed at http://www.DaveKoelle.com\r
+ *\r
+ *\r
+ * This library is free software; you can redistribute it and/or\r
+ * modify it under the terms of the GNU Lesser General Public\r
+ * License as published by the Free Software Foundation; either\r
+ * version 2.1 of the License, or any later version.\r
+ *\r
+ * This library is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
+ * Lesser General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU Lesser General Public\r
+ * License along with this library; if not, write to the Free Software\r
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA\r
+ *\r
+ */\r
+\r
+/*\r
+ * Subsequently this code had been hacked up to make it genericized and support\r
+ * folding upper/lower case.\r
+ */\r
+package net.sf.openrocket.android.filebrowser;\r
+\r
+import java.text.Collator;\r
+import java.util.Comparator;\r
+\r
+/**\r
+ * This is an updated version with enhancements made by Daniel Migowski,\r
+ * Andre Bogus, and David Koelle\r
+ *\r
+ * To convert to use Templates (Java 1.5+):\r
+ *   - Change "implements Comparator" to "implements Comparator<String>"\r
+ *   - Change "compare(Object o1, Object o2)" to "compare(String s1, String s2)"\r
+ *   - Remove the type checking and casting in compare().\r
+ *\r
+ * To use this class:\r
+ *   Use the static "sort" method from the java.util.Collections class:\r
+ *   Collections.sort(your list, new AlphanumComparator());\r
+ */\r
+public class AlphanumComparator implements Comparator<String>\r
+{\r
+       \r
+       private static final Collator sorter = Collator.getInstance();\r
+       static {\r
+               sorter.setStrength(Collator.TERTIARY);\r
+               sorter.setDecomposition(Collator.CANONICAL_DECOMPOSITION);\r
+       }\r
+\r
+    private final boolean isDigit(char ch)\r
+    {\r
+        return ch >= 48 && ch <= 57;\r
+    }\r
+\r
+    /** Length of string is passed in for improved efficiency (only need to calculate it once) **/\r
+    private final String getChunk(String s, int slength, int marker)\r
+    {\r
+        StringBuilder chunk = new StringBuilder();\r
+        char c = s.charAt(marker);\r
+        chunk.append(c);\r
+        marker++;\r
+        if (isDigit(c))\r
+        {\r
+            while (marker < slength)\r
+            {\r
+                c = s.charAt(marker);\r
+                if (!isDigit(c))\r
+                    break;\r
+                chunk.append(c);\r
+                marker++;\r
+            }\r
+        } else\r
+        {\r
+            while (marker < slength)\r
+            {\r
+                c = s.charAt(marker);\r
+                if (isDigit(c))\r
+                    break;\r
+                chunk.append(c);\r
+                marker++;\r
+            }\r
+        }\r
+        return chunk.toString();\r
+    }\r
+\r
+    public int compare(String s1, String s2)\r
+    {\r
+\r
+        int thisMarker = 0;\r
+        int thatMarker = 0;\r
+        int s1Length = s1.length();\r
+        int s2Length = s2.length();\r
+\r
+        while (thisMarker < s1Length && thatMarker < s2Length)\r
+        {\r
+            String thisChunk = getChunk(s1, s1Length, thisMarker);\r
+            thisMarker += thisChunk.length();\r
+\r
+            String thatChunk = getChunk(s2, s2Length, thatMarker);\r
+            thatMarker += thatChunk.length();\r
+\r
+            // If both chunks contain numeric characters, sort them numerically\r
+            int result = 0;\r
+            if (isDigit(thisChunk.charAt(0)) && isDigit(thatChunk.charAt(0)))\r
+            {\r
+                // Simple chunk comparison by length.\r
+                int thisChunkLength = thisChunk.length();\r
+                result = thisChunkLength - thatChunk.length();\r
+                // If equal, the first different number counts\r
+                if (result == 0)\r
+                {\r
+                    for (int i = 0; i < thisChunkLength; i++)\r
+                    {\r
+                        result = thisChunk.charAt(i) - thatChunk.charAt(i);\r
+                        if (result != 0)\r
+                        {\r
+                            return result;\r
+                        }\r
+                    }\r
+                }\r
+            } else\r
+            {\r
+               result = sorter.compare(thisChunk, thatChunk);\r
+            }\r
+\r
+            if (result != 0)\r
+                return result;\r
+        }\r
+\r
+        return s1Length - s2Length;\r
+    }\r
+}\r
index 1c4ff406a72d7a8b6864535058ce8dfd8df646aa..f7204de43bd9d5a5beea01c42ffe5b11f79135a4 100644 (file)
@@ -13,32 +13,47 @@ import net.sf.openrocket.android.actionbarcompat.ActionBarListActivity;
 import android.app.AlertDialog;\r
 import android.content.DialogInterface;\r
 import android.content.Intent;\r
+import android.content.SharedPreferences;\r
+import android.content.res.Resources;\r
 import android.net.Uri;\r
 import android.os.Bundle;\r
 import android.os.Environment;\r
+import android.preference.PreferenceManager;\r
 import android.view.View;\r
-import android.widget.ArrayAdapter;\r
+import android.view.ViewGroup;\r
+import android.widget.BaseAdapter;\r
+import android.widget.ImageView;\r
 import android.widget.ListView;\r
+import android.widget.TextView;\r
 \r
 public class SimpleFileBrowser extends ActionBarListActivity {\r
 \r
-       private List<String> item = null;\r
-       private List<String> path = null;\r
-       private String root = "/";\r
+       private List<File> path = null;\r
+       private final static File root = new File("/");\r
 \r
+       private String baseDirPrefKey;\r
+       private String baseDirName;\r
+\r
+       private boolean showOnlyOrkFiles;\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
+       private static final Comparator<String> sorter = new AlphanumComparator();\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
-               getDir( Environment.getExternalStorageDirectory().getAbsolutePath() );\r
+\r
+               Resources resources = this.getResources();\r
+               baseDirPrefKey = resources.getString(R.string.PreferenceFileBrowserBaseDirectory);\r
+               SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);\r
+\r
+               String showOnlyOrkFilesKey = resources.getString(R.string.PreferenceShowOnlyOrkFiles);\r
+               showOnlyOrkFiles = pref.getBoolean(showOnlyOrkFilesKey, false);\r
+\r
+               baseDirName = pref.getString(baseDirPrefKey, Environment.getExternalStorageDirectory().getAbsolutePath() );\r
+               getDir( new File(baseDirName) );\r
        }\r
 \r
        private static class OrkFileFilter implements FileFilter {\r
@@ -51,12 +66,15 @@ public class SimpleFileBrowser extends ActionBarListActivity {
                        if ( arg0.isDirectory() ) { \r
                                return true;\r
                        }\r
+                       return isOrk(arg0);\r
+               }\r
+\r
+               public boolean isOrk(File arg0) {\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
@@ -84,39 +102,32 @@ public class SimpleFileBrowser extends ActionBarListActivity {
 \r
        }\r
 \r
-       private void getDir(String dirPath) {\r
-               setTitle(dirPath);\r
-               item = new ArrayList<String>();\r
-               path = new ArrayList<String>();\r
+       private void getDir(File dirPath) {\r
+               setTitle(dirPath.getAbsolutePath());\r
+               path = new ArrayList<File>();\r
 \r
-               File f = new File(dirPath);\r
-               File[] files = f.listFiles(filter);\r
+               File[] files = dirPath.listFiles((showOnlyOrkFiles) ? filter : null );\r
 \r
-               if (!dirPath.equals(root)) {\r
-                       item.add(root);\r
+               boolean hasUp = false;\r
+               if ( !dirPath.getAbsolutePath().equals("/")) {\r
                        path.add(root);\r
-                       item.add("../");\r
-                       path.add(f.getParent());\r
+                       path.add( dirPath.getParentFile() );\r
+                       hasUp = true;\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
+               for( File file : files ) {\r
+                       path.add(file);\r
                }\r
 \r
-               ArrayAdapter<String> fileList = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, item);\r
+               DirectoryList fileList = new DirectoryList(hasUp, path);\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
+               final File file = path.get(position);\r
                if (file.isDirectory()) {\r
                        if (file.canRead())\r
                                getDir(path.get(position));\r
@@ -140,4 +151,107 @@ public class SimpleFileBrowser extends ActionBarListActivity {
                        finish();\r
                }\r
        }\r
+\r
+       private class DirectoryList extends BaseAdapter {\r
+\r
+               List<File> listing;\r
+               boolean hasUp;\r
+\r
+               DirectoryList( boolean hasUp ,List<File> listing ) {\r
+                       this.listing = listing;\r
+                       this.hasUp = hasUp;\r
+               }\r
+\r
+               @Override\r
+               public int getCount() {\r
+                       return listing.size();\r
+               }\r
+\r
+               @Override\r
+               public Object getItem(int arg0) {\r
+                       return listing.get(arg0);\r
+               }\r
+\r
+               @Override\r
+               public long getItemId(int position) {\r
+                       return position;\r
+               }\r
+\r
+               @Override\r
+               public View getView(int position, View convertView, ViewGroup parent) {\r
+                       if ( convertView == null ) {\r
+                               convertView = getLayoutInflater().inflate(R.layout.filebrowser_list_item, parent, false);\r
+                       }\r
+\r
+                       File file = (File) getItem(position);\r
+\r
+                       // Set the name of the field.\r
+                       {\r
+                               String fileName = file.getName();\r
+                               if ( hasUp ) {\r
+                                       if (position == 0 ) {\r
+                                               fileName = root.getAbsolutePath();\r
+                                       } else if (position == 1) {\r
+                                               fileName = "..";\r
+                                       }\r
+                               }\r
+\r
+                               ((TextView) convertView.findViewById(R.id.filebrowser_list_item_name)).setText(fileName);\r
+                       }\r
+                       \r
+                       // Set the "type icon"  directory, ork file, or none.\r
+                       {\r
+                               ImageView v = (ImageView) (convertView.findViewById(R.id.filebrowser_list_item_typeicon));\r
+                               if ( file.isDirectory() ) {\r
+                                       v.setVisibility(View.VISIBLE);\r
+                                       v.setImageResource(R.drawable.ic_directory);\r
+                               } else if ( filter.isOrk( file ) ) {\r
+                                       v.setVisibility(View.VISIBLE);\r
+                                       v.setImageResource(R.drawable.or_launcher);\r
+                               } else {\r
+                                       v.setVisibility(View.INVISIBLE);\r
+                               }\r
+                       }\r
+\r
+                       // Set the "base 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
+                                       v.setVisibility(View.VISIBLE);\r
+                                       if ( baseDirName.equals( file.getAbsolutePath() ) )  {\r
+                                               v.setSelected(true);\r
+                                       } else {\r
+                                               v.setSelected(false);\r
+                                               v.setClickable(true);\r
+                                               v.setOnClickListener( new ChangeBaseDirectory(file.getAbsolutePath()));\r
+                                       }\r
+                               }\r
+                       }\r
+                       return convertView;\r
+               }\r
+       }\r
+\r
+       private class ChangeBaseDirectory implements View.OnClickListener {\r
+\r
+               private final String dirname;\r
+               \r
+               ChangeBaseDirectory ( String dirname ) {\r
+                       this.dirname = dirname;\r
+               }\r
+               \r
+               @Override\r
+               public void onClick(View v) {\r
+                       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
+                       }\r
+               }\r
+               \r
+       }\r
+       \r
 }\r