Android Open Source - DualReader-Android Page Retrieval Strategy






From Project

Back to project page DualReader-Android.

License

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.

Java Source Code

package com.diego4aim.dualreader;
/* ww w. j  ava  2s . c o m*/
import java.util.Arrays;
import java.util.List;

import com.diego4aim.dualreader.util.GlobalContextApplication;

/**
 * Abstraction for the access details to the repositories where the lines of
 * Strings (pages) are stored. See method {@link #retrievePage(int, int)}. <br/>
 * There's a default, internal implementation as a reference, but developers are
 * expected to create their own (e.g., RESTful protocols, a local file, a
 * database, etc.
 */
public abstract class PageRetrievalStrategy {

    /**
     * Convenience method to receive the default strategy.
     */
    static public PageRetrievalStrategy createInstance() {
        return new StringArrayPageRetrievalStrategy();
    }

    public abstract List<String> retrievePage(int bookIndex, int pageNumber);

    // This is the default implementation of PageRetrievalStrategy, whose data
    // is self-contained (for that, it requires access to the application
    // resources via GlobalContextApplication.
    private static class StringArrayPageRetrievalStrategy extends
            PageRetrievalStrategy {

        // The first series of string lists in this sample application is
        // Edgar Allan Poe's tale "The Cask of Amontillado.'
        private int[] mPoe = { R.array.poe_1, R.array.poe_2, R.array.poe_3,
                R.array.poe_4, R.array.poe_5, R.array.poe_6, R.array.poe_7,
                R.array.poe_8, R.array.poe_9, R.array.poe_10, R.array.poe_11,
                R.array.poe_12, R.array.poe_13, R.array.poe_14 };
        // The second is Franz Kafka's tale "A Hunger Artist."
        private int[] mKafka = { R.array.kafka_1, R.array.kafka_2,
                R.array.kafka_3, R.array.kafka_4, R.array.kafka_5,
                R.array.kafka_6, R.array.kafka_7, R.array.kafka_8,
                R.array.kafka_9, R.array.kafka_10, R.array.kafka_11,
                R.array.kafka_12, R.array.kafka_13, R.array.kafka_14 };

        private int[][] mBooks = { {}, mPoe, mKafka };

        @Override
        public List<String> retrievePage(int bookId, int pageNumber) {
            // if input arguments are inside boundaries...
            if ((bookId >= 0) && (bookId < mBooks.length) && (pageNumber >= 0)
                    && (pageNumber < mBooks[bookId].length)) {
                int[] book = mBooks[bookId];
                int pageId = book[pageNumber];

                // ... the strings that compose a page is be retrieved
                String[] pageAsArray = GlobalContextApplication.getContext()
                        .getResources().getStringArray(pageId);

                return Arrays.asList(pageAsArray);
            }

            // in case of bad input, nothing is retrieved
            return null;
        }
    }

}




Java Source Code List

com.diego4aim.dualreader.AsyncPageRetriever.java
com.diego4aim.dualreader.DualReaderActivity.java
com.diego4aim.dualreader.PageRetrievalStrategy.java
com.diego4aim.dualreader.util.BackgroundSelector.java
com.diego4aim.dualreader.util.GlobalContextApplication.java