OSDN Git Service

Fix InlineAutocompleteTextView algorithm
authorjruesga <jorge@ruesga.com>
Wed, 26 Sep 2012 23:04:40 +0000 (01:04 +0200)
committerjruesga <jorge@ruesga.com>
Wed, 26 Sep 2012 23:04:40 +0000 (01:04 +0200)
The actual implementation of InlineAutocompleteTextView had some
incorrect matches. xe: in /, only shows sys, but system exists also.
This change review the algorithm for found this kind of entries.

src/com/cyanogenmod/explorer/ui/widgets/DirectoryInlineAutocompleteTextView.java
src/com/cyanogenmod/explorer/ui/widgets/InlineAutocompleteTextView.java

index 21b6ea2..7ea67b1 100644 (file)
@@ -120,13 +120,15 @@ public class DirectoryInlineAutocompleteTextView
      */
     @Override
     public void onTextChanged(String newValue, List<String> currentFilterData) {
+        String value = newValue;
+
         //Check if directory is valid
-        if (newValue.length() == 0) {
+        if (value.length() == 0) {
             if (this.mOnValidationListener != null) {
                 this.mOnValidationListener.onVoidValue();
             }
         } else {
-            boolean relative = FileHelper.isRelativePath(newValue);
+            boolean relative = FileHelper.isRelativePath(value);
             if (relative) {
                 if (this.mOnValidationListener != null) {
                     this.mOnValidationListener.onInvalidValue();
@@ -139,31 +141,41 @@ public class DirectoryInlineAutocompleteTextView
         }
 
         //Ensure data
-        if (!newValue.startsWith(File.separator)) {
+        if (!value.startsWith(File.separator)) {
             currentFilterData.clear();
             this.mLastParent = ""; //$NON-NLS-1$
             return;
         }
 
-        //Get the new parent, or exits if not is the parent
-        String newParent = new File(newValue).getParent();
-        if (newValue.compareTo(FileHelper.ROOT_DIRECTORY) == 0) {
+        //Get the new parent
+        String newParent = new File(value).getParent();
+        if (newParent == null) {
+            newParent = FileHelper.ROOT_DIRECTORY;
+        }
+        if (!newParent.endsWith(FileHelper.ROOT_DIRECTORY)) {
+            newParent += FileHelper.ROOT_DIRECTORY;
+        }
+        if (value.compareTo(FileHelper.ROOT_DIRECTORY) == 0) {
             newParent = FileHelper.ROOT_DIRECTORY;
             currentFilterData.clear();
-        } else if (newValue.endsWith(FileHelper.ROOT_DIRECTORY)) {
+        } else if (value.endsWith(FileHelper.ROOT_DIRECTORY)) {
             //Force the change of parent
-            newParent = new File(newValue, "a").getParent(); //$NON-NLS-1$
+            newParent = new File(value, "a").getParent(); //$NON-NLS-1$
+            if (!newParent.endsWith(FileHelper.ROOT_DIRECTORY)) {
+                newParent += FileHelper.ROOT_DIRECTORY;
+            }
             currentFilterData.clear();
+        } else {
+            value = newParent;
         }
 
         //If a new path is detected, then load the new data
-        if (newParent != null
-                && (newParent.compareTo(this.mLastParent) != 0 || currentFilterData.isEmpty())) {
+        if (newParent.compareTo(this.mLastParent) != 0 || currentFilterData.isEmpty()) {
             this.mLastParent = newParent;
             currentFilterData.clear();
             try {
                 List<String> newData =
-                        CommandHelper.quickFolderSearch(getContext(), newValue, null);
+                        CommandHelper.quickFolderSearch(getContext(), value, null);
                 currentFilterData.addAll(newData);
             } catch (Throwable ex) {
                 Log.e(TAG, "Quick folder search failed", ex); //$NON-NLS-1$
index 5a8eb70..47e8e0b 100644 (file)
@@ -341,35 +341,45 @@ public class InlineAutocompleteTextView extends RelativeLayout
     private void doTab() {
         //Complete with current text
         String current = this.mForegroundText.getText().toString();
-        String filter = this.mBackgroundText.getText().toString();
         if (current.length() == 0) {
             return;
         }
-        if (this.mCompletionString != null
-                && current.endsWith(this.mCompletionString)) {
-            if (this.mData.size() <= this.mFilter) {
-                this.mFilter = 0;
-            }
-            if (this.mData.size() == 1 && this.mFilter == 0) {
-                //Autocomplete with the only autocomplete option
-                setText(this.mData.get(this.mFilter));
-            } else {
-                //Show the autocomplete options
-                if (this.mData.size() > 0) {
-                    this.mBackgroundText.setText(this.mData.get(this.mFilter));
-                    this.mBackgroundText.setVisibility(View.VISIBLE);
-                    this.mFilter++;
-                }
-            }
+
+        //Get the data
+        List<String> filteredData = filter(this.mData, current);
+        if (filteredData.size() <= this.mFilter) {
+            this.mFilter = 0;
+        }
+        if (filteredData.size() == 1 && this.mFilter == 0) {
+            //Autocomplete with the only autocomplete option
+            setText(filteredData.get(this.mFilter));
         } else {
-            //Autocomplete
-            if (filter != null && filter.length() > 0) {
-                //Ensure that filter wraps the current text
-                if (filter.startsWith(current)) {
-                    setText(filter);
-                }
+            //Show the autocomplete options
+            if (filteredData.size() > 0) {
+                this.mBackgroundText.setText(filteredData.get(this.mFilter));
+                this.mBackgroundText.setVisibility(View.VISIBLE);
+                this.mFilter++;
+            }
+        }
+    }
+
+    /**
+     * Method that creates a temporary filter based in the current text
+     *
+     * @param data The global data array
+     * @param current The current text
+     * @return The filtered data array
+     */
+    private static List<String> filter(List<String> data, String current) {
+        List<String> filter = new ArrayList<String>(data);
+        int size = filter.size();
+        for (int i=size-1; i>=0; i--) {
+            String s = filter.get(i);
+            if (!s.startsWith(current)) {
+                filter.remove(i);
             }
         }
+        return filter;
     }
 
     /**