Android Open Source - WearPomodoro U I Timer






From Project

Back to project page WearPomodoro.

License

The source code is released under:

GNU General Public License

If you think the Android project WearPomodoro 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.alexkorovyansky.wearpomodoro.helpers;
//from  w  ww .j  av a2 s.c o m
import android.os.Handler;
import android.os.Looper;

import java.util.HashMap;
import java.util.Map;

public class UITimer {

    public static abstract class Task {
        UITimer uiTimer;
        String tag;

        public abstract void run();

        public UITimer uiTimer() {
            return uiTimer;
        }

        public String tag() {
            return tag;
        }

        public void cancelTask() {
            uiTimer.cancel(tag);
        }


    }

    private Handler handler;
    private Map<String, Runnable> runnablesMap;

    public UITimer() {
        this.handler = new Handler(Looper.myLooper());
        this.runnablesMap = new HashMap<String, Runnable>();
    }

    public void schedule(final Task task, int delayMs, String tag) {
        schedule(task, delayMs, -1, tag);
    }

    public void schedule(final Task task, int delayMs, final int periodMs, String tag) {
        cancel(tag);
        task.uiTimer = this;
        task.tag = tag;
        Runnable periodRunnable = new Runnable() {
            @Override
            public void run() {
                if (periodMs > 0) {
                    handler.postDelayed(this, periodMs);
                }
                task.run();
            }
        };
        runnablesMap.put(tag, periodRunnable);
        handler.postDelayed(periodRunnable, delayMs);
    }

    public boolean cancel(String tag) {
        Runnable runnable = runnablesMap.remove(tag);
        if (runnable != null) {
            handler.removeCallbacks(runnable);
            return true;
        }
        return false;
    }

    public void cancelAll() {
        for (Runnable runnable: runnablesMap.values()) {
            handler.removeCallbacks(runnable);
        }
        runnablesMap.clear();
    }
}




Java Source Code List

com.alexkorovyansky.wearpomodoro.app.MainActivity.java
com.alexkorovyansky.wearpomodoro.app.PomodoroConstants.java
com.alexkorovyansky.wearpomodoro.app.base.BasePomodoroActivity.java
com.alexkorovyansky.wearpomodoro.app.receivers.PomodoroAlarmReceiver.java
com.alexkorovyansky.wearpomodoro.app.receivers.PomodoroAlarmTickReceiver.java
com.alexkorovyansky.wearpomodoro.app.receivers.PomodoroControlReceiver.java
com.alexkorovyansky.wearpomodoro.app.services.PomodoroNotificationService.java
com.alexkorovyansky.wearpomodoro.app.ui.PomodoroEntryActivity.java
com.alexkorovyansky.wearpomodoro.app.ui.PomodoroTransitionActivity.java
com.alexkorovyansky.wearpomodoro.helpers.PersistentStorage.java
com.alexkorovyansky.wearpomodoro.helpers.PomodoroMaster.java
com.alexkorovyansky.wearpomodoro.helpers.PomodoroUtils.java
com.alexkorovyansky.wearpomodoro.helpers.ServiceProvider.java
com.alexkorovyansky.wearpomodoro.helpers.UITimer.java
com.alexkorovyansky.wearpomodoro.helpers.WakefulBroadcastReceiver.java
com.alexkorovyansky.wearpomodoro.model.ActivityType.java