OSDN Git Service

Use System Resolver
[android-x86/packages-apps-CMFileManager.git] / src / com / cyanogenmod / filemanager / ui / widgets / NavigationView.java
index d947bb8..6d8ba67 100755 (executable)
@@ -21,7 +21,6 @@ import android.content.Context;
 import android.content.SharedPreferences;
 import android.content.res.TypedArray;
 import android.os.AsyncTask;
-import android.os.FileObserver;
 import android.os.storage.StorageVolume;
 import android.util.AttributeSet;
 import android.util.Log;
@@ -246,15 +245,9 @@ BreadcrumbListener, OnSelectionChangedListener, OnSelectionListener, OnRequestRe
             // is created)
             mNewDirChecked = checkChRootedNavigation(params[0]);
 
-            //Check that it is really necessary change the directory
-            if (!mReload && NavigationView.this.mCurrentDir != null &&
-                    NavigationView.this.mCurrentDir.compareTo(mNewDirChecked) == 0) {
-                return null;
-            }
-
-            mHasChanged = !(NavigationView.this.mCurrentDir != null &&
-                    NavigationView.this.mCurrentDir.compareTo(mNewDirChecked) == 0);
-            mIsNewHistory = (NavigationView.this.mCurrentDir != null);
+            mHasChanged = !(NavigationView.this.mPreviousDir != null &&
+                    NavigationView.this.mPreviousDir.compareTo(mNewDirChecked) == 0);
+            mIsNewHistory = (NavigationView.this.mPreviousDir != null);
 
             try {
                 //Reset the custom title view and returns to breadcrumb
@@ -386,13 +379,11 @@ BreadcrumbListener, OnSelectionChangedListener, OnSelectionListener, OnRequestRe
         @Override
         protected void onPostExecute(List<FileSystemObject> files) {
             // This means an exception. This method will be recalled then
-            if (files != null) {
-                onPostExecuteTask(files, mAddToHistory, mIsNewHistory, mHasChanged,
+            onPostExecuteTask(files, mAddToHistory, mIsNewHistory, mHasChanged,
                         mSearchInfo, mNewDirChecked, mScrollTo);
 
-                // Do animation
-                fadeEfect(false);
-            }
+            // Do animation
+            fadeEfect(false);
         }
 
         /**
@@ -419,6 +410,7 @@ BreadcrumbListener, OnSelectionChangedListener, OnSelectionListener, OnRequestRe
 
     private int mId;
     private String mCurrentDir;
+    private String mPreviousDir;
     private NavigationLayoutMode mCurrentMode;
     /**
      * @hide
@@ -439,7 +431,7 @@ BreadcrumbListener, OnSelectionChangedListener, OnSelectionListener, OnRequestRe
     // Restrictions
     private Map<DisplayRestrictions, Object> mRestrictions;
 
-    private FileObserver mCurrentDirObserver;
+    private NavigationTask mNavigationTask;
 
     /**
      * @hide
@@ -513,7 +505,7 @@ BreadcrumbListener, OnSelectionChangedListener, OnSelectionListener, OnRequestRe
         //Return the persistent the data
         NavigationViewInfoParcelable parcel = new NavigationViewInfoParcelable();
         parcel.setId(this.mId);
-        parcel.setCurrentDir(this.mCurrentDir);
+        parcel.setCurrentDir(this.mPreviousDir);
         parcel.setChRooted(this.mChRooted);
         parcel.setSelectedFiles(this.mAdapter.getSelectedItems());
         parcel.setFiles(this.mFiles);
@@ -821,8 +813,25 @@ BreadcrumbListener, OnSelectionChangedListener, OnSelectionListener, OnRequestRe
             return;
         }
 
+        boolean addToHistory = false;
+        boolean reload = true;
+        boolean useCurrent = false;
+        SearchInfoParcelable searchInfo = null;
+
+        String newDir = this.mCurrentDir;
+        if (this.mNavigationTask != null) {
+            addToHistory = this.mNavigationTask.mAddToHistory;
+            reload = this.mNavigationTask.mReload;
+            useCurrent = this.mNavigationTask.mUseCurrent;
+            searchInfo = this.mNavigationTask.mSearchInfo;
+            this.mNavigationTask.cancel(true);
+            this.mNavigationTask = null;
+            this.mCurrentDir = this.mPreviousDir;
+            this.mPreviousDir = null;
+        }
+
         //Reload data
-        changeCurrentDir(this.mCurrentDir, false, true, false, null, scrollTo);
+        changeCurrentDir(newDir, addToHistory, reload, useCurrent, searchInfo, scrollTo);
     }
 
     /**
@@ -831,7 +840,6 @@ BreadcrumbListener, OnSelectionChangedListener, OnSelectionListener, OnRequestRe
     public void recycle() {
         if (this.mAdapter != null) {
             this.mAdapter.dispose();
-            this.mCurrentDirObserver.stopWatching();
         }
     }
 
@@ -945,6 +953,38 @@ BreadcrumbListener, OnSelectionChangedListener, OnSelectionListener, OnRequestRe
     }
 
     /**
+     * Method that removes a {@link FileSystemObject} from the view
+     *
+     * @param fso The file system object
+     */
+    public void removeItem(FileSystemObject fso) {
+        // Delete also from internal list
+        if (fso != null) {
+            int cc = this.mFiles.size()-1;
+            for (int i = cc; i >= 0; i--) {
+                FileSystemObject f = this.mFiles.get(i);
+                if (f != null && f.compareTo(fso) == 0) {
+                    this.mFiles.remove(i);
+                    break;
+                }
+            }
+        }
+        this.mAdapter.remove(fso);
+    }
+
+    /**
+     * Method that removes a file system object from his path from the view
+     *
+     * @param path The file system object path
+     */
+    public void removeItem(String path) {
+        FileSystemObject fso = this.mAdapter.getItem(path);
+        if (fso != null) {
+            this.mAdapter.remove(fso);
+        }
+    }
+
+    /**
      * Method that returns the current directory.
      *
      * @return String The current directory
@@ -996,13 +1036,18 @@ BreadcrumbListener, OnSelectionChangedListener, OnSelectionListener, OnRequestRe
             final String newDir, final boolean addToHistory,
             final boolean reload, final boolean useCurrent,
             final SearchInfoParcelable searchInfo, final FileSystemObject scrollTo) {
-        if (mCurrentDirObserver != null) {
-            mCurrentDirObserver.stopWatching();
-            mCurrentDirObserver = null;
+        if (mNavigationTask != null) {
+            this.mCurrentDir = this.mPreviousDir;
+            this.mPreviousDir = null;
+            mNavigationTask.cancel(true);
+            mNavigationTask = null;
         }
-        NavigationTask task = new NavigationTask(useCurrent, addToHistory, reload,
+
+        this.mPreviousDir = this.mCurrentDir;
+        this.mCurrentDir = newDir;
+        mNavigationTask = new NavigationTask(useCurrent, addToHistory, reload,
                 searchInfo, scrollTo, mRestrictions, mChRooted);
-        task.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, newDir);
+        mNavigationTask.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, newDir);
     }
 
     /**
@@ -1042,10 +1087,11 @@ BreadcrumbListener, OnSelectionChangedListener, OnSelectionListener, OnRequestRe
     void onPostExecuteTask(
             List<FileSystemObject> files, boolean addToHistory, boolean isNewHistory,
             boolean hasChanged, SearchInfoParcelable searchInfo,
-            final String newDir, final FileSystemObject scrollTo) {
+            String newDir, final FileSystemObject scrollTo) {
         try {
             //Check that there is not errors and have some data
             if (files == null) {
+                this.mCurrentDir = this.mPreviousDir;
                 return;
             }
 
@@ -1080,77 +1126,20 @@ BreadcrumbListener, OnSelectionChangedListener, OnSelectionListener, OnRequestRe
             scrollTo(scrollTo);
 
             //The current directory is now the "newDir"
-            this.mCurrentDir = newDir;
-            mCurrentDirObserver = new FileObserver(newDir) {
-                @Override
-                public void onEvent(int event, final String path) {
-                    Runnable runnable = null;
-                    final String fullPath = newDir + '/' + path;
-                    // This is done because the implementation doesn't match the documentation
-                    // for FileObserver and we are given undocumented flags.
-                    event &= FileObserver.ALL_EVENTS;
-
-                    switch (event) {
-                        case FileObserver.CREATE:
-                        case FileObserver.MOVED_TO: {
-                            final FileSystemObject fso = FileHelper.createFileSystemObject(
-                                    new File(fullPath));
-
-                            if (FileHelper.shouldShow(fso, mRestrictions, mChRooted)) {
-                                runnable = new Runnable() {
-                                    @Override
-                                            public void run() {
-                                        mAdapter.add(fso);
-                                        // TODO: refactor to move this off of UI thread
-                                        mAdapter.sort(FileHelper.getSortComparator());
-                                    }
-                                };
-                                break;
-                            }
-                        } case FileObserver.DELETE:
-                          case FileObserver.MOVED_FROM: {
-                              runnable = new Runnable() {
-                                  @Override
-                                  public void run() {
-                                      mAdapter.remove(mAdapter.getItem(fullPath));
-                                  }
-                              };
-                            break;
-                        } case FileObserver.DELETE_SELF: {
-                            // TODO: Actually handle this
-                            break;
-
-                        } case FileObserver.MODIFY: {
-                            runnable = new Runnable() {
-                                @Override
-                                public void run() {
-                                    FileHelper.updateFileSystemObject(mAdapter.getItem(fullPath));
-                                    mAdapter.notifyDataSetChanged();
-                                }
-                            };
-                            break;
-                        }
-                        default:
-                            Log.w(TAG, "Unknown event " + event + " for " + fullPath);
-                    }
-
-                    if (runnable != null) {
-                        post(runnable);
-                    }
-                }
-            };
-            mCurrentDirObserver.startWatching();
-
             if (this.mOnDirectoryChangedListener != null) {
                 FileSystemObject dir = FileHelper.createFileSystemObject(new File(newDir));
                 this.mOnDirectoryChangedListener.onDirectoryChanged(dir);
             }
+
         } finally {
             //If calling activity is search, then save the search history
             if (searchInfo != null) {
                 this.mOnHistoryListener.onNewHistory(searchInfo);
             }
 
+            this.mPreviousDir = null;
+            mNavigationTask = null;
+
             //End of loading data
             try {
                 NavigationView.this.mBreadcrumb.endLoading();
@@ -1227,7 +1216,7 @@ BreadcrumbListener, OnSelectionChangedListener, OnSelectionListener, OnRequestRe
             changeCurrentDir(fso.getFullPath(), searchInfo);
         } else {
             // Open the file with the preferred registered app
-            IntentsActionPolicy.openFileSystemObject(getContext(), fso, false, null, null);
+            IntentsActionPolicy.openFileSystemObject(getContext(), fso, false, null);
         }
     }
 
@@ -1259,7 +1248,7 @@ BreadcrumbListener, OnSelectionChangedListener, OnSelectionListener, OnRequestRe
             // Open the file (edit or pick)
             if (this.mNavigationMode.compareTo(NAVIGATION_MODE.BROWSABLE) == 0) {
                 // Open the file with the preferred registered app
-                IntentsActionPolicy.openFileSystemObject(getContext(), fso, false, null, null);
+                IntentsActionPolicy.openFileSystemObject(getContext(), fso, false, null);
             } else {
                 // Request a file pick selection
                 if (this.mOnFilePickedListener != null) {
@@ -1276,6 +1265,11 @@ BreadcrumbListener, OnSelectionChangedListener, OnSelectionListener, OnRequestRe
      */
     @Override
     public void onRequestRefresh(Object o, boolean clearSelection) {
+        if (o instanceof FileSystemObject) {
+            refresh((FileSystemObject)o);
+        } else if (o == null) {
+            refresh();
+        }
         if (clearSelection) {
             onDeselectAll();
         }
@@ -1294,6 +1288,11 @@ BreadcrumbListener, OnSelectionChangedListener, OnSelectionListener, OnRequestRe
      */
     @Override
     public void onRequestRemove(Object o, boolean clearSelection) {
+        if (o != null && o instanceof FileSystemObject) {
+            removeItem((FileSystemObject)o);
+        } else {
+            onRequestRefresh(null, clearSelection);
+        }
         if (clearSelection) {
             onDeselectAll();
         }