Android Open Source - vanilindy Readahead Thread






From Project

Back to project page vanilindy.

License

The source code is released under:

Apache License

If you think the Android project vanilindy 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) 2013 Adrian Ulrich <adrian@blinkenlights.ch>
 *//  w w w  .ja  v a 2  s .co  m
 * This program 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.
 *
 * This program 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 this program. If not, see <http://www.gnu.org/licenses/>. 
 */


package ch.blinkenlights.android.vanilla;

import android.util.Log;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

class ReadaheadThread extends Thread {
  private String mCurrentPath = null;
  private boolean mPaused = true;
  private Thread mThread;


  public ReadaheadThread() {
  }

  /**
   * Starts the persistent RA-Thread. This thread is never stopped: Destroying + creating
   * a new thread just because the user clicked on 'pause' doesn't make much sense
   */
  public void start() {
    mThread = new Thread(new Runnable() {
      public void run() {
        threadWorker();
      }
    });
    mThread.start();
  }

  /**
   * Updates the current read-ahead source and wakes up the ra-thread
   */
  public void setSource(String path) {
    mCurrentPath = path;
    mPaused = false;
    mThread.interrupt();
  }

  /**
   * Tells the ra-thread to pause the read-ahead work
   * Calling setSource with path = mCurrentPath will cause the
   * thread to RESUME (the file is not closed by calling pause())
   */
  public void pause() {
    mPaused = true;
  }

  /**
   * Sleep for x milli seconds
   */
  private static void sleep(int millis) {
    try { Thread.sleep(millis); }
    catch(InterruptedException e) {}
  }

  /**
   * Our thread mainloop
   * This thread will read from mCurrentPath until
   * we hit an EOF or mPaused is set to false.
   * The readahead speed is controlled by 'sleepTime'
   */
  private void threadWorker() {
    String path = null;
    FileInputStream fis = null;
    byte[] scratch = new byte[8192];             // Read 8kB per call to read()
    int sleepTime = (int)((1f/(256f/8f))*1000f); // We try to read 256kB/s (with 8kB blocks)

    for(;;) {
      if(mPaused) {
        sleep(600*1000); /* Sleep 10 minutes */
        continue;
      }

      if(path != mCurrentPath) {
        // File changed: First we try to close the old FIS
        // fis can be null or already closed, we therefore do
        // not care about the result. (the GC would take care of it anyway)
        try { fis.close(); } catch(Exception e) {}
        // We can now try to open the new file.
        // Errors are not fatal, we will simply switch into paused-mode
        try {
          fis = new FileInputStream(mCurrentPath);
          path = mCurrentPath;
          Log.v("VanillaMusic", "readahead of "+path+" starts");
        } catch(FileNotFoundException e) {
          path = null;
          mPaused = true;
          continue;
        }
      }

      // 'fis' is now an open FileInputStream. Read 8kB per go until
      // we hit EOF
      try {
        int br = fis.read(scratch);
        if(br < 0) { // no more data -> EOF
          mPaused = true;
          Log.v("VanillaMusic", "readahead of "+path+" finished");
        }
      } catch(IOException e) {
        path = null; // io error?! switch into paused mode
        mPaused = true;
      }
      sleep(sleepTime);
    }
  }
  
}




Java Source Code List

android.support.v4.view.PagerAdapter.java
android.support.v4.view.ViewPager.java
ch.blinkenlights.android.vanilla.ActionBarControls.java
ch.blinkenlights.android.vanilla.Action.java
ch.blinkenlights.android.vanilla.BastpUtil.java
ch.blinkenlights.android.vanilla.BuildConfig.java
ch.blinkenlights.android.vanilla.CompatHoneycomb.java
ch.blinkenlights.android.vanilla.CompatIcs.java
ch.blinkenlights.android.vanilla.CoverBitmap.java
ch.blinkenlights.android.vanilla.CoverView.java
ch.blinkenlights.android.vanilla.DragListView.java
ch.blinkenlights.android.vanilla.DragTextView.java
ch.blinkenlights.android.vanilla.FileSystemAdapter.java
ch.blinkenlights.android.vanilla.FilebrowserStartActivity.java
ch.blinkenlights.android.vanilla.FilebrowserStartAdapter.java
ch.blinkenlights.android.vanilla.FourLongWidget.java
ch.blinkenlights.android.vanilla.FourSquareWidget.java
ch.blinkenlights.android.vanilla.FourWhiteWidget.java
ch.blinkenlights.android.vanilla.FullPlaybackActivity.java
ch.blinkenlights.android.vanilla.IdlePreference.java
ch.blinkenlights.android.vanilla.LibraryActivity.java
ch.blinkenlights.android.vanilla.LibraryAdapter.java
ch.blinkenlights.android.vanilla.LibraryPagerAdapter.java
ch.blinkenlights.android.vanilla.Limiter.java
ch.blinkenlights.android.vanilla.ListPreferenceSummary.java
ch.blinkenlights.android.vanilla.MediaAdapter.java
ch.blinkenlights.android.vanilla.MediaButtonReceiver.java
ch.blinkenlights.android.vanilla.MediaUtils.java
ch.blinkenlights.android.vanilla.MiniPlaybackActivity.java
ch.blinkenlights.android.vanilla.MusicAlphabetIndexer.java
ch.blinkenlights.android.vanilla.NewPlaylistDialog.java
ch.blinkenlights.android.vanilla.OneCellWidget.java
ch.blinkenlights.android.vanilla.PlayCountsHelper.java
ch.blinkenlights.android.vanilla.PlaybackActivity.java
ch.blinkenlights.android.vanilla.PlaybackService.java
ch.blinkenlights.android.vanilla.PlaylistActivity.java
ch.blinkenlights.android.vanilla.PlaylistAdapter.java
ch.blinkenlights.android.vanilla.Playlist.java
ch.blinkenlights.android.vanilla.PrefKeys.java
ch.blinkenlights.android.vanilla.PreferencesActivity.java
ch.blinkenlights.android.vanilla.PreferencesBackupAgent.java
ch.blinkenlights.android.vanilla.QueryTask.java
ch.blinkenlights.android.vanilla.ReadaheadThread.java
ch.blinkenlights.android.vanilla.SeekBarPreference.java
ch.blinkenlights.android.vanilla.ShowQueueActivity.java
ch.blinkenlights.android.vanilla.ShowQueueAdapter.java
ch.blinkenlights.android.vanilla.SongTimeline.java
ch.blinkenlights.android.vanilla.Song.java
ch.blinkenlights.android.vanilla.TabOrderActivity.java
ch.blinkenlights.android.vanilla.TabOrderAdapter.java
ch.blinkenlights.android.vanilla.WidgetD.java
ch.blinkenlights.android.vanilla.WidgetE.java
ch.blinkenlights.bastp.Bastp.java
ch.blinkenlights.bastp.Common.java
ch.blinkenlights.bastp.FlacFile.java
ch.blinkenlights.bastp.ID3v2File.java
ch.blinkenlights.bastp.LameHeader.java
ch.blinkenlights.bastp.OggFile.java
com.viewpagerindicator.TabPageIndicator.java