Android Open Source - FPlayAndroid Timer






From Project

Back to project page FPlayAndroid.

License

The source code is released under:

Copyright (c) 2013-2014, Carlos Rafael Gimenes das Neves All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the followin...

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

//
// FPlayAndroid is distributed under the FreeBSD License
////  w ww  .  ja  v a 2 s .  com
// Copyright (c) 2013-2014, Carlos Rafael Gimenes das Neves
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
//    list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
//    this list of conditions and the following disclaimer in the documentation
//    and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// The views and conclusions contained in the software and documentation are those
// of the authors and should not be interpreted as representing official policies,
// either expressed or implied, of the FreeBSD Project.
//
// https://github.com/carlosrafaelgn/FPlayAndroid
//
package br.com.carlosrafaelgn.fplay.util;

import android.os.Message;
import android.os.SystemClock;

import br.com.carlosrafaelgn.fplay.activity.MainHandler;

public final class Timer implements MainHandler.Callback {
  public static interface TimerHandler {
    public void handleTimer(Timer timer, Object param);
  }
  
  private static final int MSG_ONESHOT = 0x0200;
  private static final int MSG_INTERVAL = 0x0201;
  private final Object sync;
  private final String name;
  private final boolean oneShot, handledOnMain, compensatingForDelays;
  private volatile Runnable runnable;
  private volatile TimerHandler timerHandler;
  private volatile int interval, version;
  private volatile TimerThread thread;
  private volatile boolean alive, paused;
  private volatile Object param;
  private long nextTime;
  
  private class TimerThread extends Thread {
    private final int myVersion;
    
    public TimerThread(int myVersion, String name) {
      super((name == null) ? "Timer Thread" : name);
      this.myVersion = myVersion;
    }
    
    @Override
    public void run() {
      long lastTime = SystemClock.uptimeMillis();
      while (version == myVersion) {
        if (paused) {
          synchronized (sync) {
            try {
              sync.wait();
            } catch (InterruptedException e) { }
          }
          if (version != myVersion)
            break;
        }
        if (compensatingForDelays) {
          final long now = SystemClock.uptimeMillis();
          final int actualInterval = interval - ((int)now - (int)lastTime);
          lastTime = now;
          if (actualInterval > 0) {
            synchronized (sync) {
              try {
                sync.wait(actualInterval);
              } catch (InterruptedException e) { }
            }
          } else {
            synchronized (sync) {
              try {
                sync.wait(1); //just not to hog the CPU!
              } catch (InterruptedException e) { }
            }
          }
        } else {
          synchronized (sync) {
            try {
              sync.wait(interval);
            } catch (InterruptedException e) { }
          }
        }
        if (paused)
          continue;
        try {
          if (version == myVersion) {
            if (runnable != null)
              runnable.run();
            else if (timerHandler != null)
              timerHandler.handleTimer(Timer.this, param);
          }
        } catch (Throwable ex) {
          ex.printStackTrace();
          break;
        }
        if (oneShot)
          break;
      }
      if (version == myVersion) {
        synchronized (sync) {
          if (version == myVersion) {
            thread = null;
            alive = false;
          }
        }
      }
    }
  }
  
  public Timer(Runnable runnable, String name, boolean oneShot, boolean handledOnMain, boolean compensatingForDelays) {
    this.sync = new Object();
    this.runnable = runnable;
    this.timerHandler = null;
    this.name = name;
    this.oneShot = oneShot;
    this.handledOnMain = handledOnMain;
    this.compensatingForDelays = compensatingForDelays;
  }
  
  public Timer(TimerHandler timerHandler, String name, boolean oneShot, boolean handledOnMain, boolean compensatingForDelays) {
    this.sync = new Object();
    this.runnable = null;
    this.timerHandler = timerHandler;
    this.name = name;
    this.oneShot = oneShot;
    this.handledOnMain = handledOnMain;
    this.compensatingForDelays = compensatingForDelays;
  }
  
  public void start(int interval) {
    if (handledOnMain) {
      version++;
      this.interval = interval;
      param = null;
      alive = true;
      thread = null;
      if (oneShot) {
        MainHandler.sendMessageDelayed(this, MSG_ONESHOT, version, 0, interval);
      } else {
        if (compensatingForDelays)
          nextTime = SystemClock.uptimeMillis() + interval;
        MainHandler.sendMessageDelayed(this, MSG_INTERVAL, version, 0, interval);
      }
    } else {
      synchronized (sync) {
        version++;
        this.interval = interval;
        param = null;
        alive = true;
        thread = new TimerThread(version, name);
        thread.start();
      }
    }
  }
  
  public void start(int interval, Object param) {
    if (handledOnMain) {
      version++;
      this.interval = interval;
      this.param = param;
      alive = true;
      paused = false;
      thread = null;
      if (oneShot) {
        MainHandler.sendMessageDelayed(this, MSG_ONESHOT, version, 0, interval);
      } else {
        if (compensatingForDelays)
          nextTime = SystemClock.uptimeMillis() + interval;
        MainHandler.sendMessageDelayed(this, MSG_INTERVAL, version, 0, interval);
      }
    } else {
      synchronized (sync) {
        version++;
        if (paused && thread != null)
          sync.notifyAll();
        this.interval = interval;
        this.param = param;
        alive = true;
        paused = false;
        thread = new TimerThread(version, name);
        thread.start();
      }
    }
  }
  
  public void pause() {
    if (alive && !handledOnMain)
      paused = true;
  }
  
  public void resume() {
    if (alive && !handledOnMain && paused) {
      paused = false;
      synchronized (sync) {
        sync.notifyAll();
      }
    }
  }
  
  public void stop() {
    paused = false;
    if (alive) {
      if (handledOnMain) {
        version++;
      } else {
        synchronized (sync) {
          version++;
          if (thread != null) {
            sync.notifyAll();
            thread = null;
          }
        }
      }
      alive = false;
    }
  }
  
  public void stopAndWait() {
    paused = false;
    if (alive) {
      if (handledOnMain) {
        version++;
      } else {
        Thread t;
        synchronized (sync) {
          version++;
          t = thread;
          if (thread != null) {
            sync.notifyAll();
            thread = null;
          }
        }
        if (t != null) {
          try {
            t.join();
          } catch (InterruptedException e) { }
        }
      }
      alive = false;
    }
  }
  
  public void release() {
    runnable = null;
    timerHandler = null;
    param = null;
  }
  
  @Override
  public boolean handleMessage(Message msg) {
    if (msg.arg1 == version) {
      switch (msg.what) {
      case MSG_INTERVAL:
        if (runnable != null)
          runnable.run();
        else if (timerHandler != null)
          timerHandler.handleTimer(this, param);
        if (alive) {
          if (compensatingForDelays) {
            final long now = SystemClock.uptimeMillis(), next = nextTime + interval;
            nextTime = ((next < now) ? now : next);
            MainHandler.sendMessageAtTime(this, MSG_INTERVAL, version, 0, nextTime);
          } else {
            MainHandler.sendMessageDelayed(this, MSG_INTERVAL, version, 0, interval);
          }
        }
        break;
      case MSG_ONESHOT:
        alive = false;
        if (runnable != null)
          runnable.run();
        else if (timerHandler != null)
          timerHandler.handleTimer(this, param);
        break;
      }
    }
    return true;
  }
  
  public int getInterval() {
    return interval;
  }
  
  public boolean isPaused() {
    return paused;
  }
  
  public boolean isAlive() {
    return alive;
  }
  
  public boolean isCompensatingForDelays() {
    return compensatingForDelays;
  }
  
  public boolean isHandledOnMain() {
    return handledOnMain;
  }
}




Java Source Code List

br.com.carlosrafaelgn.fplay.ActivityAbout.java
br.com.carlosrafaelgn.fplay.ActivityBrowser2.java
br.com.carlosrafaelgn.fplay.ActivityBrowserRadio.java
br.com.carlosrafaelgn.fplay.ActivityBrowserView.java
br.com.carlosrafaelgn.fplay.ActivityEffects.java
br.com.carlosrafaelgn.fplay.ActivityFileSelection.java
br.com.carlosrafaelgn.fplay.ActivityItemView.java
br.com.carlosrafaelgn.fplay.ActivityMain.java
br.com.carlosrafaelgn.fplay.ActivitySettings.java
br.com.carlosrafaelgn.fplay.ExternalReceiver.java
br.com.carlosrafaelgn.fplay.WidgetMain.java
br.com.carlosrafaelgn.fplay.activity.ActivityHost.java
br.com.carlosrafaelgn.fplay.activity.ActivityVisualizer.java
br.com.carlosrafaelgn.fplay.activity.ClientActivity.java
br.com.carlosrafaelgn.fplay.activity.MainHandler.java
br.com.carlosrafaelgn.fplay.list.AlbumArtFetcher.java
br.com.carlosrafaelgn.fplay.list.BaseItem.java
br.com.carlosrafaelgn.fplay.list.BaseList.java
br.com.carlosrafaelgn.fplay.list.FileFetcher.java
br.com.carlosrafaelgn.fplay.list.FileList.java
br.com.carlosrafaelgn.fplay.list.FileSt.java
br.com.carlosrafaelgn.fplay.list.RadioStationList.java
br.com.carlosrafaelgn.fplay.list.RadioStation.java
br.com.carlosrafaelgn.fplay.list.SongList.java
br.com.carlosrafaelgn.fplay.list.Song.java
br.com.carlosrafaelgn.fplay.playback.BassBoost.java
br.com.carlosrafaelgn.fplay.playback.Equalizer.java
br.com.carlosrafaelgn.fplay.playback.MetadataExtractor.java
br.com.carlosrafaelgn.fplay.playback.Player.java
br.com.carlosrafaelgn.fplay.playback.PresetReverb.java
br.com.carlosrafaelgn.fplay.playback.Virtualizer.java
br.com.carlosrafaelgn.fplay.ui.BgButton.java
br.com.carlosrafaelgn.fplay.ui.BgColorStateList.java
br.com.carlosrafaelgn.fplay.ui.BgListView.java
br.com.carlosrafaelgn.fplay.ui.BgSeekBar.java
br.com.carlosrafaelgn.fplay.ui.BgTextView.java
br.com.carlosrafaelgn.fplay.ui.ColorPickerView.java
br.com.carlosrafaelgn.fplay.ui.CustomContextMenu.java
br.com.carlosrafaelgn.fplay.ui.FileView.java
br.com.carlosrafaelgn.fplay.ui.InterceptableLayout.java
br.com.carlosrafaelgn.fplay.ui.ObservableScrollView.java
br.com.carlosrafaelgn.fplay.ui.RadioStationView.java
br.com.carlosrafaelgn.fplay.ui.SettingView.java
br.com.carlosrafaelgn.fplay.ui.SongAddingMonitor.java
br.com.carlosrafaelgn.fplay.ui.SongView.java
br.com.carlosrafaelgn.fplay.ui.UI.java
br.com.carlosrafaelgn.fplay.ui.drawable.BorderDrawable.java
br.com.carlosrafaelgn.fplay.ui.drawable.ColorDrawable.java
br.com.carlosrafaelgn.fplay.ui.drawable.NullDrawable.java
br.com.carlosrafaelgn.fplay.ui.drawable.StateDrawable.java
br.com.carlosrafaelgn.fplay.ui.drawable.TextIconDrawable.java
br.com.carlosrafaelgn.fplay.util.ArraySorter.java
br.com.carlosrafaelgn.fplay.util.BitmapLruCache.java
br.com.carlosrafaelgn.fplay.util.BluetoothConnectionManager.java
br.com.carlosrafaelgn.fplay.util.ColorUtils.java
br.com.carlosrafaelgn.fplay.util.ReleasableBitmapWrapper.java
br.com.carlosrafaelgn.fplay.util.SafeURLSpan.java
br.com.carlosrafaelgn.fplay.util.SerializableMap.java
br.com.carlosrafaelgn.fplay.util.Serializer.java
br.com.carlosrafaelgn.fplay.util.SlimLock.java
br.com.carlosrafaelgn.fplay.util.Timer.java
br.com.carlosrafaelgn.fplay.visualizer.BluetoothVisualizerJni.java
br.com.carlosrafaelgn.fplay.visualizer.OpenGLVisualizerJni.java
br.com.carlosrafaelgn.fplay.visualizer.SimpleVisualizerJni.java
br.com.carlosrafaelgn.fplay.visualizer.VisualizerView.java
br.com.carlosrafaelgn.fplay.visualizer.Visualizer.java