Android Open Source - droidcon-android-espresso World Destruction Countdown






From Project

Back to project page droidcon-android-espresso.

License

The source code is released under:

Apache License

If you think the Android project droidcon-android-espresso 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.xrigau.droidcon.espresso.presentation;
/*from  ww  w .j  ava  2  s.com*/
import android.os.Handler;
import android.os.Message;

import static android.os.SystemClock.sleep;

public class WorldDestructionCountdown {

    public static final int COUNTDOWN_STARTED = 0;
    public static final int COUNTDOWN_UPDATED = 1;
    public static final int COUNTDOWN_FINISHED = 2;

    private static final long WORLD_DESTRUCTION_COUNTDOWN_TIME_MS = 5000L;
    private static final long UPDATE_INTERVAL_MS = 1000L;
    private static final int IGNORED = -1;

    private final Handler handler;
    private CancellableRunnable runnable;

    public WorldDestructionCountdown(Handler handler) {
        this.handler = handler;
    }

    public void startCounting() {
        runnable = makeRunnable();
        new Thread(runnable).start();
    }

    private CancellableRunnable makeRunnable() {
        return new CancellableRunnable() {
            @Override
            public void run() {
                onCountdownStarted();
                notifyUpdate(COUNTDOWN_STARTED, IGNORED);

                long iterations = WORLD_DESTRUCTION_COUNTDOWN_TIME_MS / UPDATE_INTERVAL_MS;
                for (int i = 0; i < iterations; i++) {
                    notifyUpdate(COUNTDOWN_UPDATED, (int) (WORLD_DESTRUCTION_COUNTDOWN_TIME_MS - i * UPDATE_INTERVAL_MS));
                    sleep(UPDATE_INTERVAL_MS);
                    if (isCancelled) {
                        return;
                    }
                }

                notifyUpdate(COUNTDOWN_FINISHED, IGNORED);
                onCountdownFinished();
            }

            private void notifyUpdate(int what, int data) {
                Message message = handler.obtainMessage();
                message.what = what;
                message.arg1 = data;
                handler.sendMessage(message);
            }
        };
    }

    protected void onCountdownStarted() {
    }

    protected void onCountdownFinished() {
    }

    public void stop() {
        runnable.cancel();
    }

    private static abstract class CancellableRunnable implements Runnable {
        boolean isCancelled = false;

        void cancel() {
            isCancelled = true;
        }
    }
}




Java Source Code List

com.xrigau.droidcon.core.debug.MockClient.java
com.xrigau.droidcon.core.model.PostList.java
com.xrigau.droidcon.core.model.PostShould.java
com.xrigau.droidcon.core.model.Post.java
com.xrigau.droidcon.core.service.Services.java
com.xrigau.droidcon.core.task.NewsTask.java
com.xrigau.droidcon.core.task.TaskExecutorShould.java
com.xrigau.droidcon.core.task.TaskExecutor.java
com.xrigau.droidcon.core.task.Task.java
com.xrigau.droidcon.espresso.AndroidUnitTest.java
com.xrigau.droidcon.espresso.helper.DrawableMatcher.java
com.xrigau.droidcon.espresso.helper.EspressoTestsMatchers.java
com.xrigau.droidcon.espresso.loader.LoaderListener.java
com.xrigau.droidcon.espresso.loader.PostsTaskLoaderCallbacks.java
com.xrigau.droidcon.espresso.loader.PostsTaskLoader.java
com.xrigau.droidcon.espresso.presentation.IdlingResourceActivityTest.java
com.xrigau.droidcon.espresso.presentation.IdlingResourceActivity.java
com.xrigau.droidcon.espresso.presentation.MainActivityTest.java
com.xrigau.droidcon.espresso.presentation.MainActivity.java
com.xrigau.droidcon.espresso.presentation.PostListActivityTest.java
com.xrigau.droidcon.espresso.presentation.PostListActivity.java
com.xrigau.droidcon.espresso.presentation.VisitorActivityTest.java
com.xrigau.droidcon.espresso.presentation.VisitorActivity.java
com.xrigau.droidcon.espresso.presentation.WorldDestructionCountdown.java
com.xrigau.droidcon.espresso.presentation.adapter.EmptyAdapter.java
com.xrigau.droidcon.espresso.presentation.adapter.PostsAdapter.java
com.xrigau.droidcon.espresso.util.DeveloperError.java