Back to project page DualReader-Android.
The source code is released under:
Apache License
If you think the Android project DualReader-Android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.diego4aim.dualreader; //from ww w . ja va2 s . com import java.util.List; import android.os.AsyncTask; /** * This asynchronous page retriever refresh is meant to retrieve pages of lines * without blocking the UI thread. The data repository is accessed by the a * {@link PageRetrievalStrategy}. */ public class AsyncPageRetriever { private int mCurrentPageNumber = 0; private int mCurrentBookId = -1; private PageRetrievalStrategy mPageRetrievalStrategy = null; private OnRetrievalListener mOnRetrievalListener = null; /** * This constructor uses the default {@link PageRetrievalStrategy} * implementation. * * @param onRetrievalListener * {@link OnRetrievalListener} listener to be called back as the * {@link PageRetrievalStrategy} gets a new page. It can't be * null or a {@link RuntimeException} will be thrown. */ public AsyncPageRetriever(OnRetrievalListener onRetrievalListener) { this(null, onRetrievalListener); } /** * This constructor enables user-defined {@link PageRetrievalStrategy} * (e.g., RESTful-based, SQLite, etc.) * * @param pageRetrievalStrategy * A {@link PageRetrievalStrategy}. If null, the default * implementation is to be used. * @param onRetrievalListener * {@link OnRetrievalListener} listener to be called back as the * {@link PageRetrievalStrategy} gets a new page. It can't be * null or a {@link RuntimeException} will be thrown */ public AsyncPageRetriever(PageRetrievalStrategy pageRetrievalStrategy, OnRetrievalListener onRetrievalListener) { if (onRetrievalListener == null) { throw new RuntimeException( "AsynPageRetriever.OnRetrievalListener can't be null."); } else { mOnRetrievalListener = onRetrievalListener; } mPageRetrievalStrategy = (pageRetrievalStrategy == null) ? PageRetrievalStrategy .createInstance() : pageRetrievalStrategy; } /** * Given a book id, retrieves asynchronously its first page. Once the page * arrives, the {@link OnRetrievalListener} provided to the constructor will * be invoked. * * @param bookId */ public void requestBookFirstPage(int bookId) { new RetrievalTask().execute(bookId, 0); } /** * Retrieves asynchronously the page before the last one loaded. If no page * was loaded yet, no page will be retrieved. Once the page arrives, the * {@link OnRetrievalListener} provided to the constructor will be invoked. */ public void requestPreviousPage() { new RetrievalTask().execute(mCurrentBookId, mCurrentPageNumber - 1); } /** * Retrieves asynchronously the next page to the last one loaded. If no page * was loaded yet, no page will be retrieved. Once the page arrives, the * {@link OnRetrievalListener} provided to the constructor will be invoked. */ public void requestNextPage() { new RetrievalTask().execute(mCurrentBookId, mCurrentPageNumber + 1); } /** * Listener to be invoked every time that a page is asynchronously received. * See {@link #onPageRetrieved(List)}. */ public static interface OnRetrievalListener { /** * Receives the list of strings that compose the page retrieved. */ void onPageRetrieved(List<String> page); } // Background task that leverages the PageRetrievalStrategy to retrieve // pages without // blocking the UI thread. private class RetrievalTask extends AsyncTask<Integer, Void, List<String>> { private int mBookId = -1; private int mPageNumber = -1; @Override protected List<String> doInBackground(Integer... params) { List<String> pageToReturn = null; // this worker must receive two arguments: the book id and the page // number. If less arguments the PageRetrievalStrategy won't be // invoked. If more arguments, the extra one are to be ignored. if (params.length >= 2) { mBookId = params[0]; mPageNumber = params[1]; // Negative arguments are ignored if ((mBookId >= 0) && (mPageNumber >= 0)) { pageToReturn = mPageRetrievalStrategy.retrievePage(mBookId, mPageNumber); } } return pageToReturn; } @Override protected void onPostExecute(List<String> page) { // if a page was retrieved... if (page != null) { // ... then the current book and page number are updated. mCurrentBookId = mBookId; mCurrentPageNumber = mPageNumber; // Finally, the listener for this event is called back. mOnRetrievalListener.onPageRetrieved(page); } } } }