Android Open Source - DualReader-Android Background Selector






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.util;
/*  w  w w .j a va2  s  .  c  o  m*/
import android.graphics.drawable.Drawable;

import com.diego4aim.dualreader.R;

/**
 * This class is meant to encaptulate the logic of selecting different
 * backgrounds (as Drawable resource Ids. It's two main methods are
 * {@link #getNextBackground()}, to always get a new resource Id, and
 * {@link #getCurrentBackground()}, that returns the one currently selected.<br/>
 * In its default implementation, this class returns backgrounds in a
 * round-robin fashion.
 */
public abstract class BackgroundSelector {

    /**
     * Constructs a new BackgroundSelector that could eventually be a subclass
     * implementing another strategy. In its default implementation, the created
     * instance selects backgrounds in a circular way.
     */
    public static BackgroundSelector createInstance() {
        return new CircularBackgroundSelector();
    }

    /**
     * Returns the current background as a {@link Drawable} resource id.
     */
    public abstract int getCurrentBackground();

    /**
     * Returns the next background of the palette as a {@link Drawable} resource
     * id. A correct implementation shouldn't return the same background two
     * consecutive times. See test case {@link BackgroundSelectorTest.}
     */
    public abstract int getNextBackground();

    // Default implementation, that ensures that the next background is always
    // to be different than its predecessor.
    private static class CircularBackgroundSelector extends BackgroundSelector {

        // this selector rotates through this palette. This drawables must be
        // availables in the drawable resource folder.
        private int[] mPalette = { R.drawable.yellow_square,
                R.drawable.fuchsia_square, R.drawable.red_square,
                R.drawable.silver_square, R.drawable.gray_square,
                R.drawable.olive_square, R.drawable.purple_square,
                R.drawable.maroon_square, R.drawable.aqua_square,
                R.drawable.lime_square, R.drawable.teal_square,
                R.drawable.green_square, R.drawable.blue_square,
                R.drawable.navy_square, R.drawable.white_square };
        private int mCurrentPaletteIndex;

        public CircularBackgroundSelector() {
            mCurrentPaletteIndex = 0;
        }

        public int getCurrentBackground() {
            return mPalette[mCurrentPaletteIndex];
        }

        public int getNextBackground() {
            // If incrementing the palette index, stands past-the-end, it's
            // reset to the beginning.
            if (++mCurrentPaletteIndex == mPalette.length) {
                mCurrentPaletteIndex = 0;
            }

            return getCurrentBackground();
        }

    }

}




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