/*
* Copyright (C) 2012 The CyanogenMod Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.cyanogenmod.filemanager.util;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;
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;
import com.cyanogenmod.filemanager.console.ExecutionException;
import com.cyanogenmod.filemanager.console.InsufficientPermissionsException;
import com.cyanogenmod.filemanager.console.NoSuchFileOrDirectory;
import com.cyanogenmod.filemanager.console.OperationTimeoutException;
import com.cyanogenmod.filemanager.console.ReadOnlyFilesystemException;
import com.cyanogenmod.filemanager.console.RelaunchableException;
import com.cyanogenmod.filemanager.preferences.AccessMode;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.ParseException;
import java.util.List;
/**
* A helper class with useful methods for deal with exceptions.
*/
public final class ExceptionUtil {
/**
* An interface to communicate events related with the result of a command relaunch.
*/
public interface OnRelaunchCommandResult {
/**
* Method invoked when the relaunch operation was success
*/
void onSuccess();
/**
* Method invoked when the relaunch operation was cancelled by the user
*/
void onCancelled();
/**
* Method invoked when the relaunch operation was failed
*
* @param cause The cause of the failed operation
*/
void onFailed(Throwable cause);
}
/**
* Constructor of ExceptionUtil.
*/
private ExceptionUtil() {
super();
}
//Definition of known exceptions and his representation mode and resource identifiers
private static final Class>[] KNOWN_EXCEPTIONS = {
FileNotFoundException.class,
IOException.class,
InvalidCommandDefinitionException.class,
ConsoleAllocException.class,
NoSuchFileOrDirectory.class,
ReadOnlyFilesystemException.class,
InsufficientPermissionsException.class,
CommandNotFoundException.class,
OperationTimeoutException.class,
ExecutionException.class,
ParseException.class,
ActivityNotFoundException.class,
AuthenticationFailedException.class
};
private static final int[] KNOWN_EXCEPTIONS_IDS = {
R.string.msgs_file_not_found,
R.string.msgs_io_failed,
R.string.msgs_command_not_found,
R.string.msgs_console_alloc_failure,
R.string.msgs_file_not_found,
R.string.msgs_read_only_filesystem,
R.string.msgs_insufficient_permissions,
R.string.msgs_command_not_found,
R.string.msgs_operation_timeout,
R.string.msgs_operation_failure,
R.string.msgs_operation_failure,
R.string.msgs_not_registered_app,
0
};
private static final boolean[] KNOWN_EXCEPTIONS_TOAST = {
false,
false,
false,
false,
false,
true,
true,
false,
true,
true,
true,
false,
true
};
/**
* Method that attach a asynchronous task for executing when exception need
* to be re-executed.
*
* @param ex The exception
* @param task The task
* @see RelaunchableException
*/
public static void attachAsyncTask(Throwable ex, AsyncTask