From: Marius Renn Date: Thu, 22 Sep 2011 19:28:47 +0000 (-0700) Subject: Fix for 5357970: Handle exceptions in AsyncRunner. X-Git-Tag: android-x86-7.1-r1~726^2 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=288bd9250220ca969c704f1a99e98837334dc671;p=android-x86%2Fsystem-media.git Fix for 5357970: Handle exceptions in AsyncRunner. The AsyncRunner now stores exceptions that occurred during runtime, and passes them back as part of the result. Applications can choose to throw this exception or deal otherwise. This affects all applications using AsyncRunners. Change-Id: I2685e4cfc56d7984425a3c3a8a3ac8cfe83bc8d2 --- diff --git a/mca/filterfw/java/android/filterfw/core/AsyncRunner.java b/mca/filterfw/java/android/filterfw/core/AsyncRunner.java index 8131961b..34b601a4 100644 --- a/mca/filterfw/java/android/filterfw/core/AsyncRunner.java +++ b/mca/filterfw/java/android/filterfw/core/AsyncRunner.java @@ -41,67 +41,84 @@ public class AsyncRunner extends GraphRunner{ private OnRunnerDoneListener mDoneListener; private boolean isProcessing; - private class AsyncRunnerTask extends AsyncTask { + private Exception mException; + + private class RunnerResult { + public int status = RESULT_UNKNOWN; + public Exception exception; + } + + private class AsyncRunnerTask extends AsyncTask { private static final String TAG = "AsyncRunnerTask"; @Override - protected Integer doInBackground(SyncRunner... runner) { - if (runner.length > 1) { - throw new RuntimeException("More than one runner received!"); - } + protected RunnerResult doInBackground(SyncRunner... runner) { + RunnerResult result = new RunnerResult(); + try { + if (runner.length > 1) { + throw new RuntimeException("More than one runner received!"); + } - runner[0].assertReadyToStep(); + runner[0].assertReadyToStep(); - // Preparation - if (mLogVerbose) Log.v(TAG, "Starting background graph processing."); - activateGlContext(); + // Preparation + if (mLogVerbose) Log.v(TAG, "Starting background graph processing."); + activateGlContext(); - if (mLogVerbose) Log.v(TAG, "Preparing filter graph for processing."); - runner[0].beginProcessing(); + if (mLogVerbose) Log.v(TAG, "Preparing filter graph for processing."); + runner[0].beginProcessing(); - if (mLogVerbose) Log.v(TAG, "Running graph."); + if (mLogVerbose) Log.v(TAG, "Running graph."); - // Run loop - int status = RESULT_RUNNING; - while (!isCancelled() && status == RESULT_RUNNING) { - if (!runner[0].performStep()) { - status = runner[0].determinePostRunState(); - if (status == GraphRunner.RESULT_SLEEPING) { - runner[0].waitUntilWake(); - status = RESULT_RUNNING; + // Run loop + result.status = RESULT_RUNNING; + while (!isCancelled() && result.status == RESULT_RUNNING) { + if (!runner[0].performStep()) { + result.status = runner[0].determinePostRunState(); + if (result.status == GraphRunner.RESULT_SLEEPING) { + runner[0].waitUntilWake(); + result.status = RESULT_RUNNING; + } } } - } - - // Cleanup - if (isCancelled()) { - status = RESULT_STOPPED; - } - deactivateGlContext(); + // Cleanup + if (isCancelled()) { + result.status = RESULT_STOPPED; + } + deactivateGlContext(); + } catch (Exception exception) { + result.exception = exception; + result.status = RESULT_ERROR; + } if (mLogVerbose) Log.v(TAG, "Done with background graph processing."); - return status; + return result; } @Override - protected void onCancelled(Integer result) { + protected void onCancelled(RunnerResult result) { onPostExecute(result); } @Override - protected void onPostExecute(Integer result) { + protected void onPostExecute(RunnerResult result) { if (mLogVerbose) Log.v(TAG, "Starting post-execute."); setRunning(false); - - if (result == RESULT_STOPPED) { + setException(result.exception); + if (result.status == RESULT_STOPPED || result.status == RESULT_ERROR) { if (mLogVerbose) Log.v(TAG, "Closing filters."); - mRunner.close(); + try { + mRunner.close(); + } catch (Exception exception) { + result.status = RESULT_ERROR; + setException(exception); + } } if (mDoneListener != null) { if (mLogVerbose) Log.v(TAG, "Calling graph done callback."); - mDoneListener.onRunnerDone(result); + mDoneListener.onRunnerDone(result.status); } if (mLogVerbose) Log.v(TAG, "Completed post-execute."); } @@ -162,6 +179,8 @@ public class AsyncRunner extends GraphRunner{ @Override synchronized public void run() { if (mLogVerbose) Log.v(TAG, "Running graph."); + setException(null); + if (isRunning()) { throw new RuntimeException("Graph is already running!"); } @@ -200,8 +219,16 @@ public class AsyncRunner extends GraphRunner{ return isProcessing; } + synchronized public Exception getError() { + return mException; + } + synchronized private void setRunning(boolean running) { isProcessing = running; } + synchronized private void setException(Exception exception) { + mException = exception; + } + } diff --git a/mca/filterfw/java/android/filterfw/core/GraphRunner.java b/mca/filterfw/java/android/filterfw/core/GraphRunner.java index 3ed71b95..96fda1b6 100644 --- a/mca/filterfw/java/android/filterfw/core/GraphRunner.java +++ b/mca/filterfw/java/android/filterfw/core/GraphRunner.java @@ -44,6 +44,7 @@ public abstract class GraphRunner { public static final int RESULT_SLEEPING = 3; public static final int RESULT_BLOCKED = 4; public static final int RESULT_STOPPED = 5; + public static final int RESULT_ERROR = 6; public GraphRunner(FilterContext context) { mFilterContext = context;