Android Open Source - CoinFlip Self Test Task






From Project

Back to project page CoinFlip.

License

The source code is released under:

GNU General Public License

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

/*
 *========================================================================
 * SelfTestAsyncTask.java/*from   ww w . ja v a  2  s.c  o m*/
 * Jul 12, 2014 4:31 PM | variable
 * Copyright (c) 2014 Richard Banasiak
 *========================================================================
 * This file is part of CoinFlip.
 *
 *    CoinFlip is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    CoinFlip is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with CoinFlip.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.banasiak.coinflip;

import com.banasiak.coinflip.lib.Coin;

import android.os.AsyncTask;
import android.util.Log;

public class SelfTestTask extends AsyncTask<SelfTestStatus, SelfTestStatus, SelfTestStatus> {

    // debugging tag
    private static final String TAG = SelfTestTask.class.getSimpleName();

    private final SelfTestActivity activity;

    private final Coin theCoin;

    public SelfTestTask(final SelfTestActivity iActivity) {
        Log.d(TAG, "SelfTestTask()");
        activity = iActivity;
        theCoin = new Coin();
    }

    @Override
    protected SelfTestStatus doInBackground(SelfTestStatus... params) {
        //Log.d(TAG, "doInBackground()");

        SelfTestStatus taskStatus;
        if (params.length < 1) {
            // must be the first time we've been called
            taskStatus = new SelfTestStatus();
        } else {
            // otherwise it's recursive! :)
            taskStatus = params[0];
            taskStatus.setStartTime(System.currentTimeMillis());
            for (int total = 0; total < activity.getMaxNumberFlips(); total++) {
                // if the self test activity has been closed, might as well terminate the self test
                if (isCancelled()) {
                    break;
                }

                // flip the coin and update the model
                if (theCoin.flip()) {
                    taskStatus.incrementHeads();
                } else {
                    taskStatus.incrementTails();
                }
                taskStatus.setEndTime(System.currentTimeMillis());

                if (total % 100 == 0) {
                    // update the UI thread with our current progress
                    publishProgress(taskStatus);

                    try {
                        // this tiny sleep smoothes out the GUI updating, particularly on fast CPUs.
                        Thread.sleep(5);
                    } catch (InterruptedException e) {
                        // don't care
                    }
                }
            }

            Log.d(TAG, "heads: " + taskStatus.getHeads());
            Log.d(TAG, "tails: " + taskStatus.getTails());
            Log.d(TAG, "total: " + taskStatus.getTotal());
            Log.d(TAG, "time: " + Long.toString(taskStatus.getElapsedTime()));
        }

        return taskStatus;
    }

    @Override
    protected void onProgressUpdate(SelfTestStatus... values) {
        //Log.d(TAG, "onProgressUpdate()");
        super.onProgressUpdate(values);
        if (values.length > 0) {
            // update the SelfTest activity with our current values
            activity.updateDialog(values[0]);
        }
    }

    @Override
    protected void onPostExecute(SelfTestStatus result) {
        Log.d(TAG, "onPostExecute()");
        super.onPostExecute(result);
        activity.updateDialog(result);
    }
}




Java Source Code List

com.banasiak.coinflip.AboutActivity.java
com.banasiak.coinflip.CoinFlipActivity.java
com.banasiak.coinflip.CoinFlipWearActivity.java
com.banasiak.coinflip.DataLayerListenerService.java
com.banasiak.coinflip.InstallReceiver.java
com.banasiak.coinflip.SelfTestActivity.java
com.banasiak.coinflip.SelfTestStatus.java
com.banasiak.coinflip.SelfTestTask.java
com.banasiak.coinflip.Settings.java
com.banasiak.coinflip.ShakeListener.java
com.banasiak.coinflip.SliderPreference.java
com.banasiak.coinflip.lib.Animation.java
com.banasiak.coinflip.lib.ApplicationTest.java
com.banasiak.coinflip.lib.Coin.java
com.banasiak.coinflip.lib.CustomAnimationDrawable.java
com.banasiak.coinflip.lib.Util.java