List of usage examples for android.support.v4.content Loader stopLoading
public void stopLoading()
From source file:com.twofortyfouram.locale.sdk.host.test.Junit4SupportLoaderTestCase.java
/** * 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//from w w w. j av a 2 s. c o m */ 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 Loader.OnLoadCompleteListener<T> listener = new Loader.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; }
From source file:com.scvngr.levelup.core.test.SupportLoaderTestCase.java
/** * 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/* ww w. ja v a 2 s. co m*/ */ public <T> T getLoaderResultSynchronously(final Loader<T> loader) { // The test thread blocks on this queue until the loader puts its result in final ArrayBlockingQueue<T> queue = new ArrayBlockingQueue<T>(1); final CountDownLatch latch = new CountDownLatch(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(final Loader<T> completedLoader, final T data) { // Shut the loader down completedLoader.unregisterListener(this); completedLoader.stopLoading(); completedLoader.reset(); // Store the result, unblocking the test thread if (null != data) { queue.add(data); } latch.countDown(); } }; // 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(final 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 { latch.await(); result = queue.peek(); break; } catch (final InterruptedException e) { throw new RuntimeException("waiting thread interrupted", e); } } return result; }
From source file:org.mozilla.gecko.background.fxa.TestAccountLoader.java
/** * 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//from ww w . j a va2 s.c o m */ 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<AtomicReference<T>> queue = new ArrayBlockingQueue<AtomicReference<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(new AtomicReference<T>(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().get(); break; } catch (InterruptedException e) { throw new RuntimeException("waiting thread interrupted", e); } } return result; }
From source file:net.simonvt.cathode.ui.fragment.SeasonFragment.java
@Override public void onCreate(Bundle inState) { super.onCreate(inState); CathodeApp.inject(getActivity(), this); Bundle args = getArguments();/* w w w.jav a 2 s . co m*/ showId = args.getLong(ARG_SHOW_ID); seasonId = args.getLong(ARG_SEASONID); title = args.getString(ARG_SHOW_TITLE); seasonNumber = args.getInt(ARG_SEASON_NUMBER); type = (LibraryType) args.getSerializable(ARG_TYPE); setTitle(title); updateSubtitle(); getLoaderManager().initLoader(Loaders.LOADER_SEASON, null, episodesLoader); if (title == null) { SimpleCursorLoader loader = new SimpleCursorLoader(getActivity(), Shows.withId(showId), new String[] { ShowColumns.TITLE, }, null, null, null); loader.registerListener(0, new Loader.OnLoadCompleteListener<SimpleCursor>() { @Override public void onLoadComplete(Loader<SimpleCursor> cursorLoader, SimpleCursor cursor) { cursor.moveToFirst(); title = cursor.getString(cursor.getColumnIndex(ShowColumns.TITLE)); setTitle(title); cursorLoader.stopLoading(); } }); loader.startLoading(); } if (seasonNumber == -1) { new Thread(new Runnable() { @Override public void run() { Cursor c = getActivity().getContentResolver().query(Seasons.withId(seasonId), new String[] { SeasonColumns.SEASON, }, null, null, null); if (c.moveToFirst()) { seasonNumber = c.getInt(c.getColumnIndex(SeasonColumns.SEASON)); updateSubtitle(); } c.close(); } }).start(); } columnCount = getResources().getInteger(R.integer.episodesColumns); }