Search in sources :

Example 1 with OnLoadCompleteListener

use of android.content.Loader.OnLoadCompleteListener in project android_frameworks_base by ParanoidAndroid.

the class LoaderTestCase method getLoaderResultSynchronously.

/**
     * Runs a Loader synchronously and returns the result of the load. The loader will
     * be started, stopped, and destroyed by this method so it cannot be reused.
     *
     * @param loader The loader to run synchronously
     * @return The result from the loader
     */
public <T> T getLoaderResultSynchronously(final Loader<T> loader) {
    // The test thread blocks on this queue until the loader puts it's result in
    final ArrayBlockingQueue<T> queue = new ArrayBlockingQueue<T>(1);
    // This callback runs on the "main" thread and unblocks the test thread
    // when it puts the result into the blocking queue
    final OnLoadCompleteListener<T> listener = new OnLoadCompleteListener<T>() {

        @Override
        public void onLoadComplete(Loader<T> completedLoader, T data) {
            // Shut the loader down
            completedLoader.unregisterListener(this);
            completedLoader.stopLoading();
            completedLoader.reset();
            // Store the result, unblocking the test thread
            queue.add(data);
        }
    };
    // This handler runs on the "main" thread of the process since AsyncTask
    // is documented as needing to run on the main thread and many Loaders use
    // AsyncTask
    final Handler mainThreadHandler = new Handler(Looper.getMainLooper()) {

        @Override
        public void handleMessage(Message msg) {
            loader.registerListener(0, listener);
            loader.startLoading();
        }
    };
    // Ask the main thread to start the loading process
    mainThreadHandler.sendEmptyMessage(0);
    // Block on the queue waiting for the result of the load to be inserted
    T result;
    while (true) {
        try {
            result = queue.take();
            break;
        } catch (InterruptedException e) {
            throw new RuntimeException("waiting thread interrupted", e);
        }
    }
    return result;
}
Also used : ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Message(android.os.Message) OnLoadCompleteListener(android.content.Loader.OnLoadCompleteListener) Loader(android.content.Loader) Handler(android.os.Handler)

Example 2 with OnLoadCompleteListener

use of android.content.Loader.OnLoadCompleteListener in project android_frameworks_base by ResurrectionRemix.

the class LoaderTestCase method getLoaderResultSynchronously.

/**
     * Runs a Loader synchronously and returns the result of the load. The loader will
     * be started, stopped, and destroyed by this method so it cannot be reused.
     *
     * @param loader The loader to run synchronously
     * @return The result from the loader
     */
public <T> T getLoaderResultSynchronously(final Loader<T> loader) {
    // The test thread blocks on this queue until the loader puts it's result in
    final ArrayBlockingQueue<T> queue = new ArrayBlockingQueue<T>(1);
    // This callback runs on the "main" thread and unblocks the test thread
    // when it puts the result into the blocking queue
    final OnLoadCompleteListener<T> listener = new OnLoadCompleteListener<T>() {

        @Override
        public void onLoadComplete(Loader<T> completedLoader, T data) {
            // Shut the loader down
            completedLoader.unregisterListener(this);
            completedLoader.stopLoading();
            completedLoader.reset();
            // Store the result, unblocking the test thread
            queue.add(data);
        }
    };
    // This handler runs on the "main" thread of the process since AsyncTask
    // is documented as needing to run on the main thread and many Loaders use
    // AsyncTask
    final Handler mainThreadHandler = new Handler(Looper.getMainLooper()) {

        @Override
        public void handleMessage(Message msg) {
            loader.registerListener(0, listener);
            loader.startLoading();
        }
    };
    // Ask the main thread to start the loading process
    mainThreadHandler.sendEmptyMessage(0);
    // Block on the queue waiting for the result of the load to be inserted
    T result;
    while (true) {
        try {
            result = queue.take();
            break;
        } catch (InterruptedException e) {
            throw new RuntimeException("waiting thread interrupted", e);
        }
    }
    return result;
}
Also used : ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Message(android.os.Message) OnLoadCompleteListener(android.content.Loader.OnLoadCompleteListener) Loader(android.content.Loader) Handler(android.os.Handler)

Example 3 with OnLoadCompleteListener

use of android.content.Loader.OnLoadCompleteListener in project android_frameworks_base by DirtyUnicorns.

the class LoaderTestCase method getLoaderResultSynchronously.

/**
     * Runs a Loader synchronously and returns the result of the load. The loader will
     * be started, stopped, and destroyed by this method so it cannot be reused.
     *
     * @param loader The loader to run synchronously
     * @return The result from the loader
     */
public <T> T getLoaderResultSynchronously(final Loader<T> loader) {
    // The test thread blocks on this queue until the loader puts it's result in
    final ArrayBlockingQueue<T> queue = new ArrayBlockingQueue<T>(1);
    // This callback runs on the "main" thread and unblocks the test thread
    // when it puts the result into the blocking queue
    final OnLoadCompleteListener<T> listener = new OnLoadCompleteListener<T>() {

        @Override
        public void onLoadComplete(Loader<T> completedLoader, T data) {
            // Shut the loader down
            completedLoader.unregisterListener(this);
            completedLoader.stopLoading();
            completedLoader.reset();
            // Store the result, unblocking the test thread
            queue.add(data);
        }
    };
    // This handler runs on the "main" thread of the process since AsyncTask
    // is documented as needing to run on the main thread and many Loaders use
    // AsyncTask
    final Handler mainThreadHandler = new Handler(Looper.getMainLooper()) {

        @Override
        public void handleMessage(Message msg) {
            loader.registerListener(0, listener);
            loader.startLoading();
        }
    };
    // Ask the main thread to start the loading process
    mainThreadHandler.sendEmptyMessage(0);
    // Block on the queue waiting for the result of the load to be inserted
    T result;
    while (true) {
        try {
            result = queue.take();
            break;
        } catch (InterruptedException e) {
            throw new RuntimeException("waiting thread interrupted", e);
        }
    }
    return result;
}
Also used : ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Message(android.os.Message) OnLoadCompleteListener(android.content.Loader.OnLoadCompleteListener) Loader(android.content.Loader) Handler(android.os.Handler)

Example 4 with OnLoadCompleteListener

use of android.content.Loader.OnLoadCompleteListener in project platform_frameworks_base by android.

the class LoaderTestCase method getLoaderResultSynchronously.

/**
     * Runs a Loader synchronously and returns the result of the load. The loader will
     * be started, stopped, and destroyed by this method so it cannot be reused.
     *
     * @param loader The loader to run synchronously
     * @return The result from the loader
     */
public <T> T getLoaderResultSynchronously(final Loader<T> loader) {
    // The test thread blocks on this queue until the loader puts it's result in
    final ArrayBlockingQueue<T> queue = new ArrayBlockingQueue<T>(1);
    // This callback runs on the "main" thread and unblocks the test thread
    // when it puts the result into the blocking queue
    final OnLoadCompleteListener<T> listener = new OnLoadCompleteListener<T>() {

        @Override
        public void onLoadComplete(Loader<T> completedLoader, T data) {
            // Shut the loader down
            completedLoader.unregisterListener(this);
            completedLoader.stopLoading();
            completedLoader.reset();
            // Store the result, unblocking the test thread
            queue.add(data);
        }
    };
    // This handler runs on the "main" thread of the process since AsyncTask
    // is documented as needing to run on the main thread and many Loaders use
    // AsyncTask
    final Handler mainThreadHandler = new Handler(Looper.getMainLooper()) {

        @Override
        public void handleMessage(Message msg) {
            loader.registerListener(0, listener);
            loader.startLoading();
        }
    };
    // Ask the main thread to start the loading process
    mainThreadHandler.sendEmptyMessage(0);
    // Block on the queue waiting for the result of the load to be inserted
    T result;
    while (true) {
        try {
            result = queue.take();
            break;
        } catch (InterruptedException e) {
            throw new RuntimeException("waiting thread interrupted", e);
        }
    }
    return result;
}
Also used : ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Message(android.os.Message) OnLoadCompleteListener(android.content.Loader.OnLoadCompleteListener) Loader(android.content.Loader) Handler(android.os.Handler)

Example 5 with OnLoadCompleteListener

use of android.content.Loader.OnLoadCompleteListener in project android_frameworks_base by crdroidandroid.

the class LoaderTestCase method getLoaderResultSynchronously.

/**
     * Runs a Loader synchronously and returns the result of the load. The loader will
     * be started, stopped, and destroyed by this method so it cannot be reused.
     *
     * @param loader The loader to run synchronously
     * @return The result from the loader
     */
public <T> T getLoaderResultSynchronously(final Loader<T> loader) {
    // The test thread blocks on this queue until the loader puts it's result in
    final ArrayBlockingQueue<T> queue = new ArrayBlockingQueue<T>(1);
    // This callback runs on the "main" thread and unblocks the test thread
    // when it puts the result into the blocking queue
    final OnLoadCompleteListener<T> listener = new OnLoadCompleteListener<T>() {

        @Override
        public void onLoadComplete(Loader<T> completedLoader, T data) {
            // Shut the loader down
            completedLoader.unregisterListener(this);
            completedLoader.stopLoading();
            completedLoader.reset();
            // Store the result, unblocking the test thread
            queue.add(data);
        }
    };
    // This handler runs on the "main" thread of the process since AsyncTask
    // is documented as needing to run on the main thread and many Loaders use
    // AsyncTask
    final Handler mainThreadHandler = new Handler(Looper.getMainLooper()) {

        @Override
        public void handleMessage(Message msg) {
            loader.registerListener(0, listener);
            loader.startLoading();
        }
    };
    // Ask the main thread to start the loading process
    mainThreadHandler.sendEmptyMessage(0);
    // Block on the queue waiting for the result of the load to be inserted
    T result;
    while (true) {
        try {
            result = queue.take();
            break;
        } catch (InterruptedException e) {
            throw new RuntimeException("waiting thread interrupted", e);
        }
    }
    return result;
}
Also used : ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Message(android.os.Message) OnLoadCompleteListener(android.content.Loader.OnLoadCompleteListener) Loader(android.content.Loader) Handler(android.os.Handler)

Aggregations

Loader (android.content.Loader)5 OnLoadCompleteListener (android.content.Loader.OnLoadCompleteListener)5 Handler (android.os.Handler)5 Message (android.os.Message)5 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)5