Android Open Source - cannonball-android Word Bank






From Project

Back to project page cannonball-android.

License

The source code is released under:

Apache License

If you think the Android project cannonball-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

/**
 * Copyright (C) 2014 Twitter Inc and other contributors.
 */*from   w  ww.  j  a  va  2 s. c o  m*/
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.fabric.samples.cannonball.model;

import android.content.Context;
import android.util.SparseArray;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class WordBank {
    private final ArrayList<String> words;
    private final SparseArray<List<String>> groups;
    private int groupSize;
    private int currentGroup = 0;

    // configure depending on the size of word container
    private static final int MAX_WORDS_IN_GROUP = 12;

    public WordBank(Context context, Theme theme) {
        groups = new SparseArray<List<String>>();
        final int id = context.getResources().getIdentifier("wordbank_" + theme.getDisplayName(),
                "array", context.getPackageName());
        words = new ArrayList<String>(Arrays.asList(context.getResources().getStringArray(id)));

        shuffle();
    }

    public List<String> loadWords() {
        final List<String> words = groups.get(currentGroup);
        incrementCurrentGroup();

        return words;
    }

    private void shuffle() {
        // Adding same size random set of words in each group that will be reloaded
        // when user clicks Shuffle in the UI

        final int size = words.size();
        groupSize = size / MAX_WORDS_IN_GROUP;
        final Random random = new Random();
        currentGroup = 0;
        final ArrayList<String> clonedWords = (ArrayList<String>) words.clone();

        for (int i = 0; i < groupSize; i++) {
            groups.put(i, new ArrayList<String>());
        }

        for (int i = clonedWords.size(); i > 0; i--) {
            final int index = random.nextInt(i);

            final List<String> list = groups.get(currentGroup);
            list.add(clonedWords.get(index));
            clonedWords.remove(index);

            incrementCurrentGroup();
        }

        currentGroup = 0;
    }

    private void incrementCurrentGroup() {
        currentGroup++;
        currentGroup = (currentGroup >= groupSize ? 0 : currentGroup);
    }

}




Java Source Code List

io.fabric.samples.cannonball.AppService.java
io.fabric.samples.cannonball.App.java
io.fabric.samples.cannonball.ApplicationTest.java
io.fabric.samples.cannonball.SessionRecorder.java
io.fabric.samples.cannonball.activity.AboutActivity.java
io.fabric.samples.cannonball.activity.InitialActivity.java
io.fabric.samples.cannonball.activity.LoginActivity.java
io.fabric.samples.cannonball.activity.PoemBuilderActivity.java
io.fabric.samples.cannonball.activity.PoemHistoryActivity.java
io.fabric.samples.cannonball.activity.PoemPopularActivity.java
io.fabric.samples.cannonball.activity.ThemeChooserActivity.java
io.fabric.samples.cannonball.db.DbHelper.java
io.fabric.samples.cannonball.db.PoemContract.java
io.fabric.samples.cannonball.db.PoemProvider.java
io.fabric.samples.cannonball.model.ThemeDrawables.java
io.fabric.samples.cannonball.model.Theme.java
io.fabric.samples.cannonball.model.WordBank.java
io.fabric.samples.cannonball.view.AvenirTextView.java
io.fabric.samples.cannonball.view.CannonballDigitsAuthButton.java
io.fabric.samples.cannonball.view.CannonballTwitterLoginButton.java
io.fabric.samples.cannonball.view.CountdownView.java
io.fabric.samples.cannonball.view.FlowLayout.java
io.fabric.samples.cannonball.view.ImageAdapter.java
io.fabric.samples.cannonball.view.ImageLoader.java
io.fabric.samples.cannonball.view.ThemeAdapter.java