OSDN Git Service

Improve copy/move performance with nio and reintroduce cancel.
[android-x86/packages-apps-CMFileManager.git] / src / com / cyanogenmod / filemanager / util / ExceptionUtil.java
index 658a28c..723fc60 100644 (file)
@@ -29,6 +29,8 @@ import com.cyanogenmod.filemanager.FileManagerApplication;
 import com.cyanogenmod.filemanager.R;
 import com.cyanogenmod.filemanager.commands.SyncResultExecutable;
 import com.cyanogenmod.filemanager.commands.shell.InvalidCommandDefinitionException;
+import com.cyanogenmod.filemanager.console.AuthenticationFailedException;
+import com.cyanogenmod.filemanager.console.CancelledOperationException;
 import com.cyanogenmod.filemanager.console.CommandNotFoundException;
 import com.cyanogenmod.filemanager.console.ConsoleAllocException;
 import com.cyanogenmod.filemanager.console.ConsoleBuilder;
@@ -94,7 +96,8 @@ public final class ExceptionUtil {
                                                 OperationTimeoutException.class,
                                                 ExecutionException.class,
                                                 ParseException.class,
-                                                ActivityNotFoundException.class
+                                                ActivityNotFoundException.class,
+                                                AuthenticationFailedException.class
                                                       };
     private static final int[] KNOWN_EXCEPTIONS_IDS = {
                                                 R.string.msgs_file_not_found,
@@ -108,7 +111,8 @@ public final class ExceptionUtil {
                                                 R.string.msgs_operation_timeout,
                                                 R.string.msgs_operation_failure,
                                                 R.string.msgs_operation_failure,
-                                                R.string.msgs_not_registered_app
+                                                R.string.msgs_not_registered_app,
+                                                0
                                                      };
     private static final boolean[] KNOWN_EXCEPTIONS_TOAST = {
                                                             false,
@@ -122,7 +126,8 @@ public final class ExceptionUtil {
                                                             true,
                                                             true,
                                                             true,
-                                                            false
+                                                            false,
+                                                            true
                                                             };
 
     /**
@@ -181,16 +186,29 @@ public final class ExceptionUtil {
             final boolean quiet, final boolean askUser,
             final OnRelaunchCommandResult listener) {
 
+        // Is cancellable?
+        if (ex instanceof CancelledOperationException) {
+            return;
+        }
+
         //Get the appropriate message for the exception
         int msgResId = R.string.msgs_unknown;
         boolean toast = true;
-        int cc = KNOWN_EXCEPTIONS.length;
-        for (int i = 0; i < cc; i++) {
-            if (KNOWN_EXCEPTIONS[i].getCanonicalName().compareTo(
-                    ex.getClass().getCanonicalName()) == 0) {
-                msgResId = KNOWN_EXCEPTIONS_IDS[i];
-                toast = KNOWN_EXCEPTIONS_TOAST[i];
-                break;
+
+        // If an ExecutionException has specified a resource string to use,
+        // this is a special case and should be displayed as such.
+        if ((ex instanceof ExecutionException)
+            && ((ExecutionException)ex).getDetailMessageResId() != 0) {
+            msgResId = ((ExecutionException)ex).getDetailMessageResId();
+        } else {
+            int cc = KNOWN_EXCEPTIONS.length;
+            for (int i = 0; i < cc; i++) {
+                if (KNOWN_EXCEPTIONS[i].getCanonicalName().compareTo(
+                        ex.getClass().getCanonicalName()) == 0) {
+                    msgResId = KNOWN_EXCEPTIONS_IDS[i];
+                    toast = KNOWN_EXCEPTIONS_TOAST[i];
+                    break;
+                }
             }
         }
 
@@ -216,12 +234,18 @@ public final class ExceptionUtil {
                 @Override
                 public void run() {
                     try {
+                        String msg = null;
+                        if (fMsgResId > 0) {
+                            msg = context.getString(fMsgResId);
+                        } else {
+                            msg = ex.getMessage();
+                        }
                         if (fToast) {
-                            DialogHelper.showToast(context, fMsgResId, Toast.LENGTH_SHORT);
+                            DialogHelper.showToast(context, msg, Toast.LENGTH_SHORT);
                         } else {
                             AlertDialog dialog =
                                     DialogHelper.createErrorDialog(
-                                            context, R.string.error_title, fMsgResId);
+                                            context, R.string.error_title, msg);
                             DialogHelper.delegateDialogShow(context, dialog);
                         }
                     } catch (Exception e) {
@@ -269,61 +293,82 @@ public final class ExceptionUtil {
             }
             return;
         }
+        if (relaunchable instanceof InsufficientPermissionsException &&
+                !FileManagerApplication.isDeviceRooted()) {
+            DialogHelper.showToast(context, R.string.root_not_available_msg,
+                    Toast.LENGTH_SHORT);
+
+            // Operation failed. Root is not available
+            if (listener != null) {
+                listener.onFailed(relaunchable);
+            }
+            return;
+        }
 
         //Create a yes/no dialog and ask the user
+        final DialogInterface.OnClickListener clickListener =
+                new DialogInterface.OnClickListener() {
+            @Override
+            public void onClick(DialogInterface dialog, int which) {
+                if (which == DialogInterface.BUTTON_POSITIVE) {
+                    //Run the executable again
+                    try {
+                        //Prepare the system before re-launch the command
+                        prepare(context, relaunchable);
+
+                        //Re-execute the command
+                        List<SyncResultExecutable> executables = relaunchable.getExecutables();
+                        int cc = executables.size();
+                        for (int i = 0; i < cc; i++) {
+                            SyncResultExecutable executable = executables.get(i);
+                            Object result = CommandHelper.reexecute(context, executable, null);
+                            AsyncTask<Object, Integer, Boolean> task = relaunchable.getTask();
+                            if (task != null && task.getStatus() != AsyncTask.Status.RUNNING) {
+                                task.execute(result);
+                            }
+                        }
+
+                        // Operation complete
+                        if (listener != null) {
+                            listener.onSuccess();
+                        }
+
+                    } catch (Throwable ex) {
+                        //Capture the exception, this time in quiet mode, if the
+                        //exception is the same
+                        boolean ask = ex.getClass().getName().compareTo(
+                                relaunchable.getClass().getName()) == 0;
+                        translateException(context, ex, quiet, !ask, listener);
+
+                        // Operation failed
+                        if (listener != null) {
+                            listener.onFailed(ex);
+                        }
+                    }
+                } else {
+                    // Operation cancelled
+                    if (listener != null) {
+                        listener.onCancelled();
+                    }
+                }
+            }
+        };
+
         AlertDialog alert = DialogHelper.createYesNoDialog(
                     context,
                     R.string.confirm_operation,
                     relaunchable.getQuestionResourceId(),
-                    new DialogInterface.OnClickListener() {
-                        @Override
-                        public void onClick(DialogInterface dialog, int which) {
-                            if (which == DialogInterface.BUTTON_POSITIVE) {
-                                //Run the executable again
-                                try {
-                                    //Prepare the system before re-launch the command
-                                    prepare(context, relaunchable);
-
-                                    //Re-execute the command
-                                    List<SyncResultExecutable> executables =
-                                            relaunchable.getExecutables();
-                                    int cc = executables.size();
-                                    for (int i = 0; i < cc; i++) {
-                                        SyncResultExecutable executable = executables.get(i);
-                                        Object result = CommandHelper.reexecute(
-                                                context, executable, null);
-                                        if (relaunchable.getTask() != null) {
-                                            relaunchable.getTask().execute(result);
-                                        }
-                                    }
-
-                                    // Operation complete
-                                    if (listener != null) {
-                                        listener.onSuccess();
-                                    }
-
-                                } catch (Throwable ex) {
-                                    //Capture the exception, this time in quiet mode, if the
-                                    //exception is the same
-                                    boolean ask =
-                                            ex.getClass().getName().compareTo(
-                                                    relaunchable.getClass().getName()) == 0;
-                                    translateException(
-                                            context, ex, quiet, !ask, listener);
-
-                                    // Operation failed
-                                    if (listener != null) {
-                                        listener.onFailed(ex);
-                                    }
-                                }
-                            } else {
-                                // Operation cancelled
-                                if (listener != null) {
-                                    listener.onCancelled();
-                                }
-                            }
-                        }
-                    });
+                    clickListener);
+
+        alert.setOnDismissListener(new DialogInterface.OnDismissListener() {
+            @Override
+            public void onDismiss(DialogInterface dialog) {
+                // Operation cancelled
+                if (listener != null) {
+                    listener.onCancelled();
+                }
+            }
+        });
         DialogHelper.delegateDialogShow(context, alert);
     }